-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_lastz.py
executable file
·201 lines (179 loc) · 6.68 KB
/
run_lastz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# encoding: utf-8
"""
run_lastz.py
Created by Brant Faircloth on 2010-02-24.
Copyright (c) 2010 Brant Faircloth. All rights reserved.
# Description
A helper script to run lastz.
"""
import pdb
import sys
import os
import time
import optparse
import tempfile
import subprocess
import bx.seq.twobit
import multiprocessing
def interface():
'''Get the starting parameters from a configuration file'''
usage = "usage: %prog [options]"
p = optparse.OptionParser(usage)
p.add_option('--target', dest = 'target', action='store', \
type='string', default = None, help='The path to the target file (2bit)', \
metavar='FILE')
p.add_option('--query', dest = 'query', action='store', \
type='string', default = None, help='The path to the query file (2bit)', \
metavar='FILE')
p.add_option('--output', dest = 'output', action='store', \
type='string', default = None, help='The path to the output file', \
metavar='FILE')
p.add_option('--nprocs', dest = 'nprocs', action='store', \
type='int', default = 1, help='The number of processors to use')
p.add_option('--huge', dest = 'huge', action='store_true', default=False, \
help='Deal with poorly assembled (many scaffolds) genome sequences')
p.add_option('--size', dest = 'size', action='store', \
type='int', default = 10000000, help='The chunk size (in bp) to stick in a \
file while using the --huge option')
(options,arg) = p.parse_args()
for f in [options.target, options.query, options.output]:
if not f:
p.print_help()
sys.exit(2)
if f != options.output and not os.path.isfile(f):
print "You must provide a valid path to the query/target file."
p.print_help()
sys.exit(2)
return options, arg
def q_runner(n_procs, list_item, function, *args):
'''generic function used to start worker processes'''
task_queue = multiprocessing.Queue()
results_queue = multiprocessing.JoinableQueue()
if args:
arguments = (task_queue, results_queue,) + args
else:
arguments = (task_queue, results_queue,)
results = []
# reduce processer count if proc count > files
if len(list_item) < n_procs:
n_procs = len(list_item)
for l in list_item:
task_queue.put(l)
for _ in range(n_procs):
p = multiprocessing.Process(target=function, args=arguments).start()
#print 'Starting %s' % function
for _ in range(len(list_item)):
# indicated done results processing
results.append(results_queue.get())
results_queue.task_done()
#tell child processes to stop
for _ in range(n_procs):
task_queue.put('STOP')
# join the queue until we're finished processing results
results_queue.join()
# not closing the Queues caused me untold heartache and suffering
task_queue.close()
results_queue.close()
return results
def lastzParams(query, target, temp_out):
cli = \
'lastz {0}[nameparse=full] {1}[nameparse=full]\
--hspthresh=3000 \
--gappedthresh=3000 \
--ydrop=9400 \
--inner=0 \
--gap=400,30 \
--output={2} \
--format=lav'.format(query, target, temp_out)
return cli
def lastz(input, output):
'''docstring for worker2'''
for chromo, probe in iter(input.get, 'STOP'):
print '\t%s' % chromo
temp_fd, temp_out = tempfile.mkstemp(suffix='.lastz')
os.close(temp_fd)
cli = lastzParams(chromo, probe, temp_out)
lzstdout, lztstderr = subprocess.Popen(cli, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(None)
if lztstderr:
output.put(lztstderr)
else:
output.put(temp_out)
def SingleProcLastz(input, output):
'''docstring for worker2'''
#pdb.set_trace()
chromo, probe = input
temp_fd, temp_out = tempfile.mkstemp(suffix='.lastz')
os.close(temp_fd)
cli = lastzParams(chromo, probe, temp_out)
#pdb.set_trace()
lzstdout, lztstderr = subprocess.Popen(cli, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(None)
if lztstderr:
output.append(lztstderr)
else:
output.append(tmp_out)
return output
def main():
start_time = time.time()
print 'Started: ', time.strftime("%a %b %d, %Y %H:%M:%S", time.localtime(start_time))
options, arg = interface()
if not options.huge:
# get individual records from the 2bit file
chromos = [os.path.join(options.target, c) for c in bx.seq.twobit.TwoBitFile(file(options.target)).keys()]
else:
chromos = []
# split target file into `options.size` (~10 Mbp) chunks
temp_fd, temp_out = tempfile.mkstemp(suffix='.fasta')
os.close(temp_fd)
temp_out_handle = open(temp_out, 'w')
tb = bx.seq.twobit.TwoBitFile(file(options.target))
sequence_length = 0
print 'Running with the --huge option. Chunking files into {0} bp...'.format(options.size)
for seq in tb.keys():
sequence = tb[seq][0:]
sequence_length += len(sequence)
# write it to the outfile
temp_out_handle.write('>{0}\n{1}\n'.format(seq, sequence))
if sequence_length > options.size:
temp_out_handle.close()
# put tempfile name on stack
chromos.append(temp_out + '[multiple]')
# open a new temp file
temp_fd, temp_out = tempfile.mkstemp(suffix='.fasta')
os.close(temp_fd)
temp_out_handle = open(temp_out, 'w')
# reset sequence length
sequence_length = 0
probes = (options.query,) * len(chromos)
cp = zip(chromos, probes)
# put those record names on the stack
print "Running the targets against %s queries..." % len(chromos)
if options.nprocs == 1:
results = []
for each in cp:
print each
print results
results = SingleProcLastz(each, results)
else:
results = q_runner(options.nprocs, cp, lastz)
outp = open(options.output, 'wb')
print "Writing the results file..."
#pdb.set_trace()
for f in results:
print '\t%s' % f
# read the file
outp.write(open(f, 'rb').read())
# cleanup the lastz output files
os.remove(f)
outp.close()
print 'Cleaning up the chunked files...'
if options.huge:
for f in chromos:
# cleanup the chunked files
os.remove(f.strip('[multiple]'))
# stats
end_time = time.time()
print 'Ended: ', time.strftime("%a %b %d, %Y %H:%M:%S", time.localtime(end_time))
print 'Time for execution: ', (end_time - start_time) / 60, 'minutes'
if __name__ == '__main__':
main()