1
2
3 __version__ = "$Revision: 1.15 $"
4
5 import os
6 import sys
7 import tempfile
8
9 from Bio import SeqIO
10
11
13 """
14 >>> os.environ["WISE_KBYTE"]="300000"
15 >>> _build_align_cmdline(["dnal"], ("seq1.fna", "seq2.fna"), "/tmp/output", kbyte=100000)
16 'dnal -kbyte 100000 seq1.fna seq2.fna > /tmp/output'
17 >>> _build_align_cmdline(["psw"], ("seq1.faa", "seq2.faa"), "/tmp/output_aa")
18 'psw -kbyte 300000 seq1.faa seq2.faa > /tmp/output_aa'
19
20 """
21 cmdline = cmdline[:]
22
23
24
25 if kbyte is None:
26 try:
27 cmdline.extend(("-kbyte", os.environ["WISE_KBYTE"]))
28 except KeyError:
29 pass
30 else:
31 cmdline.extend(("-kbyte", str(kbyte)))
32
33 if not os.isatty(sys.stderr.fileno()):
34 cmdline.append("-quiet")
35
36 cmdline.extend(pair)
37 cmdline.extend((">", output_filename))
38 if quiet:
39 cmdline.extend(("2>", "/dev/null"))
40 cmdline_str = ' '.join(cmdline)
41
42 return cmdline_str
43
45 """
46 Returns a filehandle
47 """
48 assert len(pair) == 2
49
50 output_file = tempfile.NamedTemporaryFile(mode='r')
51 input_files = tempfile.NamedTemporaryFile(mode="w"), tempfile.NamedTemporaryFile(mode="w")
52
53 if dry_run:
54 print _build_align_cmdline(cmdline,
55 pair,
56 output_file.name,
57 kbyte,
58 force_type,
59 quiet)
60 return
61
62 for filename, input_file in zip(pair, input_files):
63
64
65
66 records = SeqIO.parse(open(filename), 'fasta')
67 SeqIO.write(records, input_file, 'fasta')
68 input_file.flush()
69
70 input_file_names = [input_file.name for input_file in input_files]
71
72 cmdline_str = _build_align_cmdline(cmdline,
73 input_file_names,
74 output_file.name,
75 kbyte,
76 force_type,
77 quiet)
78
79 if debug:
80 print >>sys.stderr, cmdline_str
81
82 status = os.system(cmdline_str) >> 8
83
84 if status > 1:
85 if kbyte != 0:
86 print >>sys.stderr, "INFO trying again with the linear model"
87 return align(cmdline, pair, 0, force_type, dry_run, quiet, debug)
88 else:
89 raise OSError, "%s returned %s" % (" ".join(cmdline), status)
90
91 return output_file
92
94 """
95 Generate pairs list for all-against-all alignments
96
97 >>> all_pairs(range(4))
98 [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
99 """
100 pairs = []
101
102 singles = list(singles)
103 while singles:
104 suitor = singles.pop(0)
105 pairs.extend([(suitor, single) for single in singles])
106
107 return pairs
108
111
112 -def _test(*args, **keywds):
113 import doctest, sys
114 doctest.testmod(sys.modules[__name__], *args, **keywds)
115
116 if __name__ == "__main__":
117 if __debug__:
118 _test()
119 main()
120