Package Bio :: Package Wise
[hide private]
[frames] | no frames]

Source Code for Package Bio.Wise

  1  #!/usr/bin/env python 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  # 
  6  # Bio.Wise contains modules for running and processing the output of 
  7  # some of the models in the Wise2 package by Ewan Birney available from: 
  8  # ftp://ftp.ebi.ac.uk/pub/software/unix/wise2/ 
  9  # http://www.ebi.ac.uk/Wise2/ 
 10  #  
 11  # Bio.Wise.psw is for protein Smith-Waterman alignments 
 12  # Bio.Wise.dnal is for Smith-Waterman DNA alignments 
 13   
 14  __version__ = "$Revision: 1.17 $" 
 15   
 16  import os 
 17  import sys 
 18  import tempfile 
 19   
 20  from Bio import SeqIO 
 21   
 22   
23 -def _build_align_cmdline(cmdline, pair, output_filename, kbyte=None, force_type=None, quiet=False):
24 """ 25 >>> os.environ["WISE_KBYTE"]="300000" 26 >>> _build_align_cmdline(["dnal"], ("seq1.fna", "seq2.fna"), "/tmp/output", kbyte=100000) 27 'dnal -kbyte 100000 seq1.fna seq2.fna > /tmp/output' 28 >>> _build_align_cmdline(["psw"], ("seq1.faa", "seq2.faa"), "/tmp/output_aa") 29 'psw -kbyte 300000 seq1.faa seq2.faa > /tmp/output_aa' 30 31 """ 32 cmdline = cmdline[:] 33 34 ### XXX: force_type ignored 35 36 if kbyte is None: 37 try: 38 cmdline.extend(("-kbyte", os.environ["WISE_KBYTE"])) 39 except KeyError: 40 pass 41 else: 42 cmdline.extend(("-kbyte", str(kbyte))) 43 44 if not os.isatty(sys.stderr.fileno()): 45 cmdline.append("-quiet") 46 47 cmdline.extend(pair) 48 cmdline.extend((">", output_filename)) 49 if quiet: 50 cmdline.extend(("2>", "/dev/null")) 51 cmdline_str = ' '.join(cmdline) 52 53 return cmdline_str
54
55 -def align(cmdline, pair, kbyte=None, force_type=None, dry_run=False, quiet=False, debug=False):
56 """ 57 Returns a filehandle 58 """ 59 assert len(pair) == 2 60 61 output_file = tempfile.NamedTemporaryFile(mode='r') 62 input_files = tempfile.NamedTemporaryFile(mode="w"), tempfile.NamedTemporaryFile(mode="w") 63 64 if dry_run: 65 print _build_align_cmdline(cmdline, 66 pair, 67 output_file.name, 68 kbyte, 69 force_type, 70 quiet) 71 return 72 73 for filename, input_file in zip(pair, input_files): 74 # Pipe the file through Biopython's Fasta parser/writer 75 # to make sure it conforms to the Fasta standard (in particular, 76 # Wise2 may choke on long lines in the Fasta file) 77 records = SeqIO.parse(open(filename), 'fasta') 78 SeqIO.write(records, input_file, 'fasta') 79 input_file.flush() 80 81 input_file_names = [input_file.name for input_file in input_files] 82 83 cmdline_str = _build_align_cmdline(cmdline, 84 input_file_names, 85 output_file.name, 86 kbyte, 87 force_type, 88 quiet) 89 90 if debug: 91 print >>sys.stderr, cmdline_str 92 93 status = os.system(cmdline_str) >> 8 94 95 if status > 1: 96 if kbyte != 0: # possible memory problem; could be None 97 print >>sys.stderr, "INFO trying again with the linear model" 98 return align(cmdline, pair, 0, force_type, dry_run, quiet, debug) 99 else: 100 raise OSError("%s returned %s" % (" ".join(cmdline), status)) 101 102 return output_file
103
104 -def all_pairs(singles):
105 """ 106 Generate pairs list for all-against-all alignments 107 108 >>> all_pairs(range(4)) 109 [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] 110 """ 111 pairs = [] 112 113 singles = list(singles) 114 while singles: 115 suitor = singles.pop(0) # if sorted, stay sorted 116 pairs.extend([(suitor, single) for single in singles]) 117 118 return pairs
119
120 -def main():
121 pass
122
123 -def _test(*args, **keywds):
124 import doctest, sys 125 doctest.testmod(sys.modules[__name__], *args, **keywds)
126 127 if __name__ == "__main__": 128 if __debug__: 129 _test() 130 main() 131