-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_jobs.py
executable file
·344 lines (309 loc) · 19.2 KB
/
generate_jobs.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
__author__ = 'alipirani'
import os
import argparse
import re
parser = argparse.ArgumentParser(description='Generate PBS scripts for Different Pipelines for multiple samples: varcall/assembly/new_assembly/rna/ptr')
parser.add_argument('-dir', action='store', dest="dir", help='directory of fastq files')
parser.add_argument('-filenames', action='store', dest="filenames", help='These file should contain name of forward fastq files. \
One file per line. \
These can be obtained by running: ls *_R1_001.fastq.gz > filenames \
Make Sure your Forward reads extension ends with \"_R1_001.fastq.gz\" ')
parser.add_argument('-out_dir', action='store', dest="out_dir", help='Provide a path where you want to save the assembly output')
parser.add_argument('-pipeline', action='store', dest="pipeline", help='Generating Jobs for which pipeline? varcall/assembly/new_assembly/new_assembly_SE/rna/ptr/ariba')
parser.add_argument('-reference', action='store', dest="reference", help='Reference Genome to be used for pipeline')
parser.add_argument('-type', action='store', dest="type", help='Type of Fastq files: PE or SE')
parser.add_argument('-ariba_db', action='store', dest="ariba_db", help='Path to Ariba Database')
parser.add_argument('-reference_key', action='store', dest="reference_key", help='Reference Genome to be used for each sample. \
Provide a tab-seperated file with fastq filename in first column and reference genome name in corresponding second column')
parser.add_argument('-depthofcoverage', action='store', dest="depthofcoverage", help='Varcall pipeline until GATK Depth of Coverage')
parser.add_argument('-steps', action='store', dest="steps", help='Variant Calling Steps in sequential order.\n'
'1. All : This will run all the steps starting from cleaning the reads to variant calling;\n'
'2. clean,align,post-align,varcall,filter,stats : This will also run all steps starting from cleaning to variant calling. \nYou can also run part of the pipeline by giving "align,post-align,varcall,filter,stats" which will skip the cleaning part.\nThe order is required to be sequential. Also, while skipping any of the step make sure you have results already present in your output folder.\n'
'3. coverage_depth_stats: Run Only Depth of Coverage Stats module after cleaning and read mapping steps')
parser.add_argument('-email', action='store', dest="email", help='email for PBS job notifications')
parser.add_argument('-resources', action='store', dest="resources", help='PBS resources for jobs')
parser.add_argument('-assembly', action='store', dest="assembly", help='assembly: wga/plasmid/both. Default is both ')
args = parser.parse_args()
# read filenames file
filenames_array = []
with open(args.filenames) as fp:
for line in fp:
line = line.strip()
line = args.dir + "/" + line
filenames_array.append(line)
# Function to create (old)assembly pipeline pbs scripts for each fastq files
def create_assembly_jobs():
##Change the email address; nodes and processor requirements accordingly
Pbs_model_lines = "#PBS -M %s\n" \
"#PBS -m a\n" \
"#PBS -V\n" \
"#PBS -l %s\n" \
"#PBS -q fluxod\n" \
"#PBS -A esnitkin_fluxod\n" \
"#PBS -l qos=flux\n" % (args.email, args.resources)
for file in filenames_array:
# Forward reads file name and get analysis name from its name
filename_base = os.path.basename(file)
first_part_split = filename_base.split('.')
first_part = first_part_split[0]
first_file = file
second_part = filename_base.replace("_R1_", "_R2_")
#second_part = filename_base.replace("forward", "reverse")
#second_part = filename_base.replace("1_sequence", "2_sequence")
second_file = args.dir + "/" + second_part
# Change these directory path to where your pipeline code is located
cd_command = "cd /nfs/esnitkin/bin_group/pipeline/assembly_umich/"
command = "/nfs/esnitkin/bin_group/anaconda2/bin//python pipeline.py -f1 %s -f2 %s -o %s/%s -start_step 1 -end_step 4 -A spades -reference %s" % (first_file, second_file, args.out_dir, first_part, args.reference)
job_name = "./" + first_part + ".pbs"
with open(job_name, 'w') as out:
job_title = "#PBS -N %s" % first_part
out.write(job_title+'\n')
out.write(Pbs_model_lines+'\n')
out.write(cd_command+'\n')
out.write(command+'\n')
# Function to create assembly pbs scripts for each fastq files
def create_new_assembly_jobs():
##Change the email address; nodes and processor requirements accordingly
Pbs_model_lines = "#PBS -M %s\n" \
"#PBS -m a\n" \
"#PBS -V\n" \
"#PBS -l %s\n" \
"#PBS -q fluxod\n" \
"#PBS -A esnitkin_fluxod\n" \
"#PBS -l qos=flux\n" % (args.email, args.resources)
for file in filenames_array:
first_file = file
# Forward file name and get analysis name from its name
filename_base = os.path.basename(file)
if "R1_001_final.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001_final.fastq.gz", "R2_001_final.fastq.gz")
first_part_split = filename_base.split('R1_001_final.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1.fastq.gz" in filename_base:
second_part = filename_base.replace("R1.fastq.gz", "R2.fastq.gz")
first_part_split = filename_base.split('R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_combine.fastq.gz" in filename_base:
second_part = filename_base.replace("1_combine.fastq.gz", "2_combine.fastq.gz")
first_part_split = filename_base.split('1_combine.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_sequence.fastq.gz" in filename_base:
second_part = filename_base.replace("1_sequence.fastq.gz", "2_sequence.fastq.gz")
first_part_split = filename_base.split('1_sequence.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_forward.fastq.gz" in filename_base:
second_part = filename_base.replace("_forward.fastq.gz", "_reverse.fastq.gz")
first_part_split = filename_base.split('_forward.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1_001.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001.fastq.gz", "R2_001.fastq.gz")
first_part_split = filename_base.split('R1_001.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
#first_part = first_part.replace('_S*_', '')
first_part = re.sub(r'_S.*_', '', first_part)
elif "_1.fastq.gz" in filename_base:
second_part = filename_base.replace("_1.fastq.gz", "_2.fastq.gz")
first_part_split = filename_base.split('_1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_R1.fastq.gz" in filename_base:
second_part = filename_base.replace("_R1.fastq.gz", "_R2.fastq.gz")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
else:
print "Using Standard second file naming convention"
second_part = filename_base.replace("_R1_", "_R2_")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
# Get the name of reverse reads files
#second_part = filename_base.replace("_R1_", "_R2_")
second_file = args.dir + "/" + second_part
# Change these directory paths to where your pipeline code is located. Also make sure the path to config file is correct.
cd_command = "cd /nfs/esnitkin/bin_group/pipeline/Github/assembly_umich/"
config = "/nfs/esnitkin/bin_group/pipeline/Github/assembly_umich/config"
job_name = "./" + first_part + ".pbs"
if args.assembly:
assembly_para = "-assembly " + args.assembly
else:
assembly_para = "-assembly both"
if args.reference:
command = "module load perl-modules\n/nfs/esnitkin/bin_group/anaconda2/bin//python pipeline.py -f1 %s -f2 %s -o %s/%s -start_step 1 -end_step 4 -A spades -reference %s -type PE -analysis %s -config %s %s" % (first_file, second_file, args.out_dir, first_part, args.reference, first_part, config, assembly_para)
else:
command = "module load perl-modules\n/nfs/esnitkin/bin_group/anaconda2/bin//python pipeline.py -f1 %s -f2 %s -o %s/%s -start_step 1 -end_step 4 -A spades -type PE -analysis %s -config %s %s" % (first_file, second_file, args.out_dir, first_part, first_part, config, assembly_para)
with open(job_name, 'w') as out:
job_title = "#PBS -N %s" % first_part
out.write(job_title+'\n')
out.write(Pbs_model_lines+'\n')
out.write(cd_command+'\n')
out.write(command+'\n')
# Function to create assembly pbs scripts for each fastq files
def create_new_assembly_SE_jobs():
##Change the email address; nodes and processor requirements accordingly
Pbs_model_lines = "#PBS -M %s\n" \
"#PBS -m a\n" \
"#PBS -V\n" \
"#PBS -l %s\n" \
"#PBS -q fluxod\n" \
"#PBS -A esnitkin_fluxod\n" \
"#PBS -l qos=flux\n" % (args.email, args.resources)
for file in filenames_array:
first_file = file
# Forward file name and get analysis name from its name
filename_base = os.path.basename(file)
if "R1_001_final.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001_final.fastq.gz", "R2_001_final.fastq.gz")
first_part_split = filename_base.split('R1_001_final.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1.fastq.gz" in filename_base:
second_part = filename_base.replace("R1.fastq.gz", "R2.fastq.gz")
first_part_split = filename_base.split('R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_combine.fastq.gz" in filename_base:
second_part = filename_base.replace("1_combine.fastq.gz", "2_combine.fastq.gz")
first_part_split = filename_base.split('1_combine.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_sequence.fastq.gz" in filename_base:
second_part = filename_base.replace("1_sequence.fastq.gz", "2_sequence.fastq.gz")
first_part_split = filename_base.split('1_sequence.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_forward.fastq.gz" in filename_base:
second_part = filename_base.replace("_forward.fastq.gz", "_reverse.fastq.gz")
first_part_split = filename_base.split('_forward.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1_001.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001.fastq.gz", "R2_001.fastq.gz")
first_part_split = filename_base.split('R1_001.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
#first_part = first_part.replace('_S*_', '')
first_part = re.sub(r'_S.*_', '', first_part)
elif "_1.fastq.gz" in filename_base:
second_part = filename_base.replace("_1.fastq.gz", "_2.fastq.gz")
first_part_split = filename_base.split('_1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_R1.fastq.gz" in filename_base:
second_part = filename_base.replace("_R1.fastq.gz", "_R2.fastq.gz")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_unclassified.fastq.gz" in filename_base:
second_part = filename_base.replace("_unclassified.fastq.gz", "_unclassified.fastq.gz")
first_part_split = filename_base.split('_unclassified.fastq.gz')
first_part = first_part_split[0].replace('_L00', '')
else:
print "Using Standard second file naming convention"
second_part = filename_base.replace("_R1_", "_R2_")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
# Get the name of reverse reads files
#second_part = filename_base.replace("_R1_", "_R2_")
second_file = args.dir + "/" + second_part
# Change these directory paths to where your pipeline code is located. Also make sure the path to config file is correct.
cd_command = "cd /nfs/esnitkin/bin_group/pipeline/Github/assembly_umich/"
config = "/nfs/esnitkin/bin_group/pipeline/Github/assembly_umich/config"
job_name = "./" + first_part + ".pbs"
if args.reference:
command = "module load perl-modules\n/nfs/esnitkin/bin_group/anaconda2/bin//python pipeline.py -f1 %s -o %s/%s -start_step 1 -end_step 4 -A spades -reference %s -type SE -analysis %s -config %s" % (first_file, args.out_dir, first_part, args.reference, first_part, config)
else:
command = "module load perl-modules\n/nfs/esnitkin/bin_group/anaconda2/bin//python pipeline.py -f1 %s -o %s/%s -start_step 1 -end_step 4 -A spades -type SE -analysis %s -config %s" % (first_file, args.out_dir, first_part, first_part, config)
with open(job_name, 'w') as out:
job_title = "#PBS -N %s" % first_part
out.write(job_title+'\n')
out.write(Pbs_model_lines+'\n')
out.write(cd_command+'\n')
out.write(command+'\n')
# Function to create assembly pbs scripts for each fastq files
def create_ariba_jobs():
##Change the email address; nodes and processor requirements accordingly
Pbs_model_lines = "#PBS -M %s\n" \
"#PBS -m a\n" \
"#PBS -V\n" \
"#PBS -l nodes=1:ppn=4,pmem=4000mb,walltime=60:00:00\n" \
"#PBS -q fluxod\n" \
"#PBS -A esnitkin_fluxod\n" \
"#PBS -l qos=flux\n" % (args.email, args.resources)
for file in filenames_array:
first_file = file
# Forward file name and get analysis name from its name
filename_base = os.path.basename(file)
if "R1_001_final.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001_final.fastq.gz", "R2_001_final.fastq.gz")
first_part_split = filename_base.split('R1_001_final.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1.fastq.gz" in filename_base:
second_part = filename_base.replace("R1.fastq.gz", "R2.fastq.gz")
first_part_split = filename_base.split('R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_combine.fastq.gz" in filename_base:
second_part = filename_base.replace("1_combine.fastq.gz", "2_combine.fastq.gz")
first_part_split = filename_base.split('1_combine.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "1_sequence.fastq.gz" in filename_base:
second_part = filename_base.replace("1_sequence.fastq.gz", "2_sequence.fastq.gz")
first_part_split = filename_base.split('1_sequence.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_forward.fastq.gz" in filename_base:
second_part = filename_base.replace("_forward.fastq.gz", "_reverse.fastq.gz")
first_part_split = filename_base.split('_forward.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "R1_001.fastq.gz" in filename_base:
second_part = filename_base.replace("R1_001.fastq.gz", "R2_001.fastq.gz")
first_part_split = filename_base.split('R1_001.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
#first_part = first_part.replace('_S*_', '')
first_part = re.sub(r'_S.*_', '', first_part)
elif "_1.fastq.gz" in filename_base:
second_part = filename_base.replace("_1.fastq.gz", "_2.fastq.gz")
first_part_split = filename_base.split('_1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
elif "_R1.fastq.gz" in filename_base:
second_part = filename_base.replace("_R1.fastq.gz", "_R2.fastq.gz")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
else:
print "Using Standard second file naming convention"
second_part = filename_base.replace("_R1_", "_R2_")
first_part_split = filename_base.split('_R1.fastq.gz')
first_part = first_part_split[0].replace('_L001', '')
# Get the name of reverse reads files
#second_part = filename_base.replace("_R1_", "_R2_")
second_file = args.dir + "/" + second_part
# Change these directory paths to where your pipeline code is located. Also make sure the path to config file is correct.
#mkdir_cmd = "mkdir %s/%s" % (args.out_dir, first_part)
cd_command = "cd %s" % (args.out_dir)
job_name = "./" + first_part + ".pbs"
command = "module load cd-hit\n/nfs/esnitkin/bin_group/anaconda3/bin/ariba run %s %s %s %s" % (args.ariba_db, first_file, second_file, first_part)
with open(job_name, 'w') as out:
job_title = "#PBS -N %s" % first_part
out.write(job_title+'\n')
out.write(Pbs_model_lines+'\n')
#out.write(mkdir_cmd+'\n')
out.write(cd_command+'\n')
out.write(command+'\n')
# Main Function; Check which Pipeline function to use based on command-line argument: pipeline
if args.pipeline == "varcall":
print "Generating Variant Calling PBS scripts for samples in args.filenames"
create_varcall_jobs()
elif args.pipeline == "assembly":
print "Generating Assembly PBS scripts for samples in args.filenames"
create_assembly_jobs()
elif args.pipeline == "new_assembly":
print "Generating new Assembly PBS scripts for samples in args.filenames"
create_new_assembly_jobs()
elif args.pipeline == "a5_assembly":
print "Generating a5 Assembly PBS scripts for samples in args.filenames"
create_a5_jobs()
elif args.pipeline == "rna":
print "Generating RNA Seq PBS scripts for samples in args.filenames"
create_RNA_seq_jobs()
elif args.pipeline == "ptr":
print "Generating PTR pipeline PBS scripts for samples in args.filenames"
create_PTR_jobs()
elif args.pipeline == "new_assembly_SE":
print "Generating new Assembly PBS scripts for samples in args.filenames"
create_new_assembly_SE_jobs()
elif args.pipeline == "ariba":
print "Generating new Ariba PBS scripts for samples in args.filenames"
create_ariba_jobs()
elif args.pipeline == "srst2_plasmid":
print "Generating new SRST2 Plasmid PBS scripts for samples in args.filenames"
create_srst2_jobs()
else:
print "Please provide args.pipeline command-line argument"