-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_adapter_mask.py
executable file
·298 lines (278 loc) · 9.61 KB
/
batch_adapter_mask.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# There's no good way in cosmos to create a tag
# based on output or to conditionally run a process
# This is a wrapper to run the umi utilities.
#
# Run a program (SeqPrep) for a directory in batches
# This is a helper script for Martin Aryee's
# scripts to demultiplex Illumina sequencing
# reads with sample specific and molecule
# specific tags.
# https://github.com/aryeelab/umi/wiki
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__author__ = 'Allison MacLeay'
import sys
import os
import argparse
import time
# -----------------------------------------
# Get bash command for specified
# file name prefix
# -----------------------------------------
def get_cmd(group, prog, params):
path = '.'
outdir = ''
cmd = ''
job_group = ''
a1 = ''
a2 = ''
olen = ''
if 'path' in params.keys():
path = params['path']
if 'out' in params.keys():
outdir = params['out']
if (outdir[0] == '/'):
outdir = outdir[1:]
else:
outdir = os.path.abspath(outdir)[1:]
if (outdir[-1] != '/'):
outdir = outdir + '/'
if 'job_group' in params.keys():
job_group = params['job_group']
if 'a1' in params.keys():
a1 = params['a1']
if 'a1' in params.keys():
a2 = params['a2']
if 'olen' in params.keys():
olen = '-k ' + str(params['olen'])
fastq_r1 = os.path.join(path, group + '.r1.fastq.gz')
fastq_r2 = os.path.join(path ,group + '.r2.fastq.gz')
out1 = '' + group + '_trim.r1.fastq.gz'
out2 = '' + group + '_trim.r2.fastq.gz'
cmd = (cmd + """ \
{prog} -f {fastq_r1} \
-r {fastq_r2} \
-1 {outdir}{out1} \
-2 {outdir}{out2} \
-A {adapter1} \
-B {adapter2} \
-z {olen} -X 100 \
""" ).format(
prog=prog,
fastq_r1=fastq_r1,
fastq_r2=fastq_r2,
outdir='/' + outdir,
out1=out1,
out2=out2,
adapter1=a1,
adapter2=a2,
olen=olen,
)
return cmd
# -----------------------------------------
# Return all unique file prefixes
# -----------------------------------------
def get_names(dir):
files = next(os.walk(dir))[2]
files = map(lambda x: x.split('.')[0], files)
dfiles = set(files)
return dfiles
# -----------------------------------------
# Delay completion of script until all
# files are written
#-----------------------------------------
def check_done(group_id,ct):
start = time.time()
timeout = (24 * 60 * 60) # 24 hours
done = 0
pr_ct = 0
while done==0:
pr_ct+=1
if (time.time()-start)>timeout:
print 'Job timed out'
done=1
else:
time.sleep(10)
if pr_ct % 6 == 0:
print 'checking for job completion after waiting %d seconds' % (time.time()-start)
if are_jobs_done(group_id,ct):
print 'No running jobs found in group ' + group_id
done=1
return
def wait_while_job_running(jobname,maxtm):
done = is_job_done(jobname)
tmst = time.time()
duration = 0
while done == False and (duration < maxtm):
print "Waiting for job " + jobname + " to complete \n" + str(duration) + " seconds have elapsed."
time.sleep(10)
duration = time.time() - tmst
done = is_job_done(jobname)
return
# -----------------------------------------
# Zip it
#-----------------------------------------
def gzip_if_not(folder,bsub_off,out,err,group_id,user,flags):
zipping = False
zipped = {}
for file in next(os.walk(folder))[2]:
parts = file.split('.')
if parts[-1] != 'gz':
if parts[0] not in zipped.keys():
zipped[parts[0]]=1
zipping = True
gzip(os.path.join(folder,parts[0] + '*'),'gz'+group_id+parts[0],bsub_off,out,err,user,flags)
return zipping
def gzip(filename,jobname,bsub_off,out,err,user,flags):
cmd = 'gzip ' + filename
if not bsub_off:
flags=flags+' -J '+ jobname
cmd = bsub_cmd(out,err,user,flags) + cmd
os.system(cmd)
return
# -----------------------------------------
# LSF utilities
#-----------------------------------------
def bsub_cmd(out,err,user,flags):
if flags != '':
if flags[0] != ' ':
flags = ' ' + flags
if flags[-1] != ' ':
flags = flags + ' '
txt = 'bsub -q medium -u ' + user + ' -o ' + out + ' -e ' + err + ' ' + flags
return txt
def is_job_done(jobname):
done = True
ct=0
jobs = os.popen("bjobs -J " + jobname).read().split('\n')
for line in jobs:
ct+=1
if ct == 1:
continue
status = get_job_status(line)
if status == 'RUN':
done = False
if status == 'PEND':
done = False
return done
def are_jobs_done(group,lsf_ct):
jobs=os.popen("bjobs -g " + group).read().split('\n')
ct=0
run_ct=0
group_status=True
for line in jobs:
ct+=1
if ct==1:
continue
status=get_job_status(line)
if status== 'RUN':
group_status=False
run_ct+=1
if status== 'PEND':
group_status=False
run_ct+=1
if( (ct-1) != lsf_ct):
print 'Different number of jobs passed in ('+str(lsf_ct)+') and recovered (' + str(ct-1) + ')'
print "" + str(run_ct) + " jobs are currently running or pending in group " + group
return group_status
def get_job_status(line):
status=''
status_arr=line.strip().split()
if len(status_arr)>2:
status=status_arr[2]
else:
status=''
return status
def get_group_id(group):
i = os.popen("bjgroup | grep "+ group).read()
i = i.split('\n')
num=''
max=0
for line in i:
group_arr=line.split()
if len(group_arr)>1:
group_name=group_arr[0]
if are_jobs_done(group_name,0):
# clean old jobs
os.system("bgdel " + group_name)
num=line.strip().split('_')[0].split('/')[-1]
if str(num)=='':
num=0
if type(num) is str:
if not num.isdigit():
num=0
if int(num) > max:
max = int(num)
print 'last id was ' + str(max)
return group + '/' + str(max+1) + "_trim"
#-----------------------------------------
# MAIN
# run a program (SeqPrep) for all files in a directory
# that have the same prefix
#-----------------------------------------
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Run command for removing adapter sequecnes in batches of similarly prefixed names.")
parser.add_argument('--dir', default='.', help='directory containing output of umi demultiplex')
parser.add_argument('--script', default='./SeqPrep', help='SeqPrep absolute path. default is SeqPrep in current directory')
parser.add_argument('--a1', required=True, help='Adapter 1')
parser.add_argument('--a2', required=True, help='Adapter 2')
parser.add_argument('--olen', required=False, help='max length of output reads')
parser.add_argument('--out', default='tagout', help='directory to deposit output files')
parser.add_argument('--log', default='batch_log', help='directory to deposit bsub log files')
parser.add_argument('--bsub_off', action='store_true', help='turn bsub off to test on systems without lsf')
parser.add_argument('--bsub_user', default='am282', help='username for bsub command')
parser.add_argument('--bsub_flags', default='', help='extra flags for bsub command')
#parser.add_argument('--undet', action='store_false', help='include reads less than parameter set my min reads. Default will skip files named undetermined')
args = parser.parse_args()
p = {}
lsf_group=''
lsf_group_cmd=''
if hasattr(args, 'dir'):
p['path'] = args.dir
if hasattr(args, 'out'):
p['out'] = args.out
os.system('mkdir -p ' + args.out)
if hasattr(args, 'log'):
os.system('mkdir -p ' + args.log)
os.system('ls ' + p['path'] + ' >> ' + args.log + '/ls_inputdir.txt')
if hasattr(args, 'a1'):
p['a1']=args.a1
if hasattr(args, 'a2'):
p['a2']=args.a2
if hasattr(args, 'olen'):
if args.olen is not None:
p['olen']=args.olen
lsf_out = ''
lsf_err = ''
if not args.bsub_off:
lsf_out = os.path.join(args.log, 'lsf_out.log')
lsf_err = os.path.join(args.log, 'lsf_err.log')
f = get_names(args.dir)
if len(f) < 1:
print "Error: No file prefixes were found in " + args.dir + "."
count_lsf = 0
group_id = 'groupID'
if not args.bsub_off:
lsf_group = get_group_id("/demux")
group_id = lsf_group.split("/demux/")[1]
lsf_group_cmd=' -g ' + lsf_group
needed_zip = gzip_if_not(args.dir,args.bsub_off,lsf_out,lsf_err,group_id,args.bsub_user,args.bsub_flags)
for tag in f:
if (tag.find('undetermined') > -1 ):
# skip undeterminded for now
cmd = 'echo skipping undetermined files'
elif (args.bsub_off):
cmd = get_cmd(tag, args.script, p)
else:
if needed_zip:
wait_while_job_running('gz'+group_id+tag,60*10) # ten minutes max
cmd = bsub_cmd(lsf_out,lsf_err,args.bsub_user,args.bsub_flags) + lsf_group_cmd + ' ' + get_cmd(tag, args.script, p)
# Keep track of lsf job for listener
count_lsf = count_lsf + 1
print 'batch process running command:\n' + cmd
os.system(cmd)
if (count_lsf > 0):
if lsf_group != '':
check_done(lsf_group, count_lsf)
print 'batch_process done'