1  """ 
  2  Python bindings for the HHsearch program. Capable of executing multiple 
  3  HHsearch jobs in parallel. 
  4  """ 
  5   
  6  import multiprocessing as mp 
  7   
  8  import csb.apps 
  9  import csb.io 
 10  import csb.bio.io 
 18       
 21       
 22      @property 
 25       
 27           
 28          cpu = mp.cpu_count() 
 29          cmd = csb.apps.ArgHandler(self.program, __doc__) 
 30             
 31          cmd.add_scalar_option('binary', 'b', str, 'full path to the HHsearch binary ', default='hhsearch') 
 32          cmd.add_scalar_option('cpu', 'c', int, 'maximum degree of parallelism', default=cpu)             
 33          cmd.add_scalar_option('database', 'd', str, 'the subject (database) HMM file', required=True)      
 34          cmd.add_array_argument('query', str, 'query HMM file(s)')       
 35           
 36          return cmd 
   37       
 74   
 75   
 76 -class Context(object): 
  77       
 78 -    def __init__(self, query): 
  79           
 80          self.__query = query 
 81          self.__result = None 
  82       
 83      @property 
 86       
 87      @property 
 90      @result.setter 
 91 -    def result(self, result): 
  92          self.__result = result 
   93       
103       
106       
107      try: 
108          binary, db, cpu, context = args 
109          return HHsearch(binary, db, cpu=cpu).run(context) 
110      except (KeyboardInterrupt, SystemExit): 
111          return 
 112   
120       
122       
132       
133 -    def __init__(self, binary, db, cpu=None): 
 147   
148      @property 
151      @program.setter 
153          self._program = value 
 154         
155      @property 
158      @db.setter 
159 -    def db(self, value): 
 161       
162      @property 
165      @parser.setter 
168       
169      @property 
172      @cpu.setter 
173 -    def cpu(self, value): 
 175                           
176      @property 
179      @ss.setter 
180 -    def ss(self, value): 
 182       
183      @property 
186      @mac_threshold.setter 
189       
190      @property 
193      @max_hits.setter 
196       
197      @property 
200      @max_alignments.setter 
203       
204      @property 
207      @max_evalue.setter 
210       
211      @property 
214      @min_probability.setter 
217       
218 -    def _get(self, option): 
 219           
220          if option in self._opt: 
221              return self._opt[option] 
222          else: 
223              return None 
 224           
226           
227          options = [] 
228           
229          for option in self._opt: 
230              value = self._opt[option] 
231               
232              if value is not None and value != '': 
233                  if isinstance(value, bool): 
234                      options.append('-{0}'.format(option)) 
235                  else:     
236                      options.append('-{0} {1}'.format(option, value))          
237           
238          return ' '.join(options) 
 239                           
240 -    def run(self, context): 
 254               
255 -    def runmany(self, contexts, workers=mp.cpu_count(), cpu=1): 
 256           
257          if workers > len(contexts): 
258              workers = len(contexts) 
259   
260          results = [] 
261          taskargs = [(self.program, self.db, cpu, c) for c in contexts] 
262   
263          pool = mp.Pool(workers) 
264           
265          try: 
266              for c in pool.map(_task, taskargs): 
267                  results.append(c) 
268          except KeyboardInterrupt: 
269              pass 
270          finally: 
271              pool.terminate() 
272           
273          return results 
  274   
278       
279       
280  if __name__ == '__main__': 
281      main() 
282