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 """Helper function to build a command line string (PRIVATE). 25 26 >>> os.environ["WISE_KBYTE"]="300000" 27 >>> if os.isatty(sys.stderr.fileno()): 28 ... c = _build_align_cmdline(["dnal"], ("seq1.fna", "seq2.fna"), 29 ... "/tmp/output", kbyte=100000) 30 ... assert c == 'dnal -kbyte 100000 seq1.fna seq2.fna > /tmp/output', c 31 ... c = _build_align_cmdline(["psw"], ("seq1.faa", "seq2.faa"), 32 ... "/tmp/output_aa") 33 ... assert c == 'psw -kbyte 300000 seq1.faa seq2.faa > /tmp/output_aa', c 34 ... else: 35 ... c = _build_align_cmdline(["dnal"], ("seq1.fna", "seq2.fna"), 36 ... "/tmp/output", kbyte=100000) 37 ... assert c == 'dnal -kbyte 100000 -quiet seq1.fna seq2.fna > /tmp/output', c 38 ... c = _build_align_cmdline(["psw"], ("seq1.faa", "seq2.faa"), 39 ... "/tmp/output_aa") 40 ... assert c == 'psw -kbyte 300000 -quiet seq1.faa seq2.faa > /tmp/output_aa', c 41 42 """ 43 cmdline = cmdline[:] 44 45 ### XXX: force_type ignored 46 47 if kbyte is None: 48 try: 49 cmdline.extend(("-kbyte", os.environ["WISE_KBYTE"])) 50 except KeyError: 51 pass 52 else: 53 cmdline.extend(("-kbyte", str(kbyte))) 54 55 if not os.isatty(sys.stderr.fileno()): 56 cmdline.append("-quiet") 57 58 cmdline.extend(pair) 59 cmdline.extend((">", output_filename)) 60 if quiet: 61 cmdline.extend(("2>", "/dev/null")) 62 cmdline_str = ' '.join(cmdline) 63 64 return cmdline_str
65
66 -def align(cmdline, pair, kbyte=None, force_type=None, dry_run=False, quiet=False, debug=False):
67 """ 68 Returns a filehandle 69 """ 70 assert len(pair) == 2 71 72 output_file = tempfile.NamedTemporaryFile(mode='r') 73 input_files = tempfile.NamedTemporaryFile(mode="w"), tempfile.NamedTemporaryFile(mode="w") 74 75 if dry_run: 76 print _build_align_cmdline(cmdline, 77 pair, 78 output_file.name, 79 kbyte, 80 force_type, 81 quiet) 82 return 83 84 for filename, input_file in zip(pair, input_files): 85 # Pipe the file through Biopython's Fasta parser/writer 86 # to make sure it conforms to the Fasta standard (in particular, 87 # Wise2 may choke on long lines in the Fasta file) 88 records = SeqIO.parse(open(filename), 'fasta') 89 SeqIO.write(records, input_file, 'fasta') 90 input_file.flush() 91 92 input_file_names = [input_file.name for input_file in input_files] 93 94 cmdline_str = _build_align_cmdline(cmdline, 95 input_file_names, 96 output_file.name, 97 kbyte, 98 force_type, 99 quiet) 100 101 if debug: 102 print >>sys.stderr, cmdline_str 103 104 status = os.system(cmdline_str) >> 8 105 106 if status > 1: 107 if kbyte != 0: # possible memory problem; could be None 108 print >>sys.stderr, "INFO trying again with the linear model" 109 return align(cmdline, pair, 0, force_type, dry_run, quiet, debug) 110 else: 111 raise OSError("%s returned %s" % (" ".join(cmdline), status)) 112 113 return output_file
114
115 -def all_pairs(singles):
116 """ 117 Generate pairs list for all-against-all alignments 118 119 >>> all_pairs(range(4)) 120 [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] 121 """ 122 pairs = [] 123 124 singles = list(singles) 125 while singles: 126 suitor = singles.pop(0) # if sorted, stay sorted 127 pairs.extend([(suitor, single) for single in singles]) 128 129 return pairs
130
131 -def main():
132 pass
133
134 -def _test(*args, **keywds):
135 import doctest, sys 136 doctest.testmod(sys.modules[__name__], *args, **keywds)
137 138 if __name__ == "__main__": 139 if __debug__: 140 _test() 141 main() 142