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  from __future__ import print_function 
 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
67 -def align(cmdline, pair, kbyte=None, force_type=None, dry_run=False, quiet=False, debug=False):
68 """ 69 Returns a filehandle 70 """ 71 if not pair or len(pair) != 2: 72 raise ValueError("Expected pair of filename, not %s" % repr(pair)) 73 74 output_file = tempfile.NamedTemporaryFile(mode='r') 75 input_files = tempfile.NamedTemporaryFile(mode="w"), tempfile.NamedTemporaryFile(mode="w") 76 77 if dry_run: 78 print(_build_align_cmdline(cmdline, 79 pair, 80 output_file.name, 81 kbyte, 82 force_type, 83 quiet)) 84 return 85 86 for filename, input_file in zip(pair, input_files): 87 # Pipe the file through Biopython's Fasta parser/writer 88 # to make sure it conforms to the Fasta standard (in particular, 89 # Wise2 may choke on long lines in the Fasta file) 90 records = SeqIO.parse(open(filename), 'fasta') 91 SeqIO.write(records, input_file, 'fasta') 92 input_file.flush() 93 94 input_file_names = [input_file.name for input_file in input_files] 95 96 cmdline_str = _build_align_cmdline(cmdline, 97 input_file_names, 98 output_file.name, 99 kbyte, 100 force_type, 101 quiet) 102 103 if debug: 104 sys.stderr.write("%s\n" % cmdline_str) 105 106 status = os.system(cmdline_str) >> 8 107 108 if status > 1: 109 if kbyte != 0: # possible memory problem; could be None 110 sys.stderr.write("INFO trying again with the linear model\n") 111 return align(cmdline, pair, 0, force_type, dry_run, quiet, debug) 112 else: 113 raise OSError("%s returned %s" % (" ".join(cmdline), status)) 114 115 return output_file
116 117
118 -def all_pairs(singles):
119 """ 120 Generate pairs list for all-against-all alignments 121 122 >>> all_pairs(range(4)) 123 [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] 124 """ 125 pairs = [] 126 127 singles = list(singles) 128 while singles: 129 suitor = singles.pop(0) # if sorted, stay sorted 130 pairs.extend((suitor, single) for single in singles) 131 132 return pairs
133 134
135 -def main():
136 pass
137 138
139 -def _test(*args, **keywds):
140 import doctest 141 doctest.testmod(sys.modules[__name__], *args, **keywds)
142 143 if __name__ == "__main__": 144 if __debug__: 145 _test() 146 main() 147