1
2
3
4
5
6
7
8
9
10
11
12
13
14 __version__ = "$Revision: 1.16 $"
15
16 import os
17 import sys
18 import tempfile
19
20 from Bio import SeqIO
21
22
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
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
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
75
76
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:
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
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)
116 pairs.extend([(suitor, single) for single in singles])
117
118 return pairs
119
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