-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.nf
535 lines (398 loc) · 12.7 KB
/
main.nf
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/env nextflow
def helpMessage() {
log.info"""
Usage:
nextflow run main.nf -profile [PROFILE] [ARGS]
Required Arguments:
-profile Executor profile name (e.g. local)
--organism Name of organism
--metadata Path to metadata file
--sequence_dir Directory containing *.fasta and *.gff3 files
Optional Arguments:
--outdir Directory to place outputs
--force Overwrite existing processed data
""".stripIndent()
}
// Show help message
if (params.help){
helpMessage()
exit 0
}
if ( params.organism == "None" ) {
log.info"""
Missing required argument --organism
""".stripIndent()
helpMessage()
exit 0
}
if ( params.metadata == "None" ) {
log.info"""
Missing required argument --metadata
""".stripIndent()
helpMessage()
exit 0
}
if ( params.sequence_dir == "None" ) {
log.info"""
Missing required argument --sequence_dir
""".stripIndent()
helpMessage()
exit 0
}
@Grab('com.xlson.groovycsv:groovycsv:1.3')
import static com.xlson.groovycsv.CsvParser.parseCsv
// ********************************
// * Step 1: Process genome files *
// ********************************
// Read in fasta and gff file for genome and plasmids (if any)
Channel
.fromFilePairs("${params.sequence_dir}/*.{fasta,gff3}", checkIfExists:true)
.into{ genome_ch1; genome_ch2 }
// Isolate fasta files for bowtie
genome_ch1
.flatten()
.filter(~/.*\.fasta$/)
.set{ fasta_ch }
// Isolate GFF files for featureCounts
genome_ch2
.flatten()
.filter(~/.*\.gff3$/)
.tap{ gff_ch }
// Save only one gff for strand inference
.first()
.set{ bedtools_gff_ch }
// Build bowtie index
process bowtie_build {
label 'bowtie'
label 'small'
input:
file(fasta) from fasta_ch.collect()
output:
file('index*') into index_ch
file('cspace_index*') into cspace_index_ch
script:
full_fasta = "${params.organism}.fasta"
"""
cat ${fasta} > ${full_fasta}
bowtie-build --threads ${task.cpus} ${full_fasta} index
bowtie-build -C --threads ${task.cpus} ${full_fasta} cspace_index
"""
}
// Convert GFF to BED file for strand inference
process gff2bed {
label 'small'
input:
file(gff) from bedtools_gff_ch
output:
file('genome.bed') into bed_file_ch
script:
"""
gff2bed < ${gff} > genome.bed
"""
}
// *******************************
// * Step 2: Parse metadata file *
// *******************************
// Ensure file exists
File csv = new File(params.metadata)
assert(csv.exists())
// Load metadata file
csv_text = file(params.metadata).text
csv_data = parseCsv(csv_text,separator:'\t')
// Loop through rows
sample_ids = csv_data.collect { row ->
// Ensure that Layout is either SINGLE or PAIRED
assert((row['LibraryLayout'] == 'SINGLE') ||
(row['LibraryLayout'] == 'PAIRED'))
// Save Experiment ID in sample_ids
row['Experiment']
}
// Ensure that sample IDs are unique
assert(sample_ids.clone().unique().size() == sample_ids.size())
// Check if results already exist
run_list = []
if (!params.force) {
if (params.outdir.startsWith('s3://')) {
// Use AWS CLI to get list of runs already completed
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = "aws s3 ls ${params.outdir}/featureCounts/".execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(10000)
// Parse AWS CLI output
def raw_list = "$sout".split('\n')
for (item in raw_list) {
match = item =~ ".*\\s(.*)_cds.txt"
if (match.find()) {
run_list << match.group(1)
}
}
}
else {
dir = new File("${params.outdir}/featureCounts")
if (dir.exists()) {
// Loop through results directory and get experiment names
dir.eachFile {
if (it.name.endsWith('_cds.txt')) {
run_list << it.name.minus('_cds.txt')
}
}
}
}
}
Channel
.fromPath(params.metadata,checkIfExists:true)
.splitCsv(header:true,sep:'\t')
.filter { row ->
!run_list.contains(row.Experiment)
}
.branch { row ->
sra: row.R1 == ""
return tuple(row.Experiment,
row.LibraryLayout,
row.Platform,
row.Run)
local_paired: row.LibraryLayout == "PAIRED"
return tuple(row.Experiment,
row.LibraryLayout,
row.Platform,
// Allow for multiple ';'-separated R1/R2 files
tuple(row.R1.split(';').collect{ x -> file(x) }),
tuple(row.R2.split(';').collect{ x -> file(x) }))
local_single: row.LibraryLayout == "SINGLE"
return tuple(row.Experiment,
row.LibraryLayout,
row.Platform,
// Allow for multiple ';'-separated R1 files
tuple(row.R1.split(';').collect{ x -> file(x) }))
}
.set{ metadata_ch }
// ********************************************
// * Step 3: Stage FASTQ files for processing *
// ********************************************
// Download from SRA
process download_fastq {
maxRetries 1
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
label 'fastq'
label 'medium'
label 'stage'
input:
tuple sample_id, layout, platform, run_ids from metadata_ch.sra
output:
tuple sample_id, layout, platform, file("${sample_id}_[12].fastq.gz") into sra_output_ch
script:
"""
for run in ${run_ids.replace(';',' ')}; do
prefetch --max-size 1000000000000 \$run
fasterq-dump \$run -e ${task.cpus}
done
if [ "${layout}" = "SINGLE" ]; then
pigz -c *.fastq > ${sample_id}_1.fastq.gz
else
pigz -c *_1.fastq > ${sample_id}_1.fastq.gz
pigz -c *_2.fastq > ${sample_id}_2.fastq.gz
fi
"""
}
// Stage local file
process stage_fastq_single {
label 'fastq'
label 'medium'
label 'stage'
input:
tuple sample_id, layout, platform, file(R1) from metadata_ch.local_single
output:
tuple sample_id, layout, platform, file("${sample_id}_1.fastq.gz") into single_output_ch
script:
"""
stage_fastq.sh --name ${sample_id} -r1 "${R1}"
"""
}
process stage_fastq_paired {
label 'fastq'
label 'medium'
label 'stage'
input:
tuple sample_id, layout, platform, file(R1),file(R2) from metadata_ch.local_paired
output:
tuple sample_id, layout, platform, file("${sample_id}_[12].fastq.gz") into paired_output_ch
script:
"""
stage_fastq.sh --name ${sample_id} -r1 "${R1}" -r2 "${R2}"
"""
}
// Combine SRA and local fastq channels
fastq_output_ch = sra_output_ch.mix(single_output_ch).mix(paired_output_ch)
// *****************************
// * Step 4: Run Trim Galore! *
// *****************************
process trim_galore {
time '8h'
maxRetries 2
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
label 'large'
label 'trim_galore'
publishDir "${params.outdir}/trim_reports", mode: 'copy', pattern: '*trimming_report.txt'
publishDir "${params.outdir}/fastqc", mode: 'copy', pattern: '*_fastqc.zip'
input:
tuple sample_id, layout, platform, file(fastq) from fastq_output_ch
output:
tuple sample_id, layout, platform, file("*.fq.gz") into bowtie_input_ch
file "*trimming_report.txt" optional true into cutadapt_results_ch
file "*_fastqc.{zip,html}" into fastqc_results_ch
script:
if (platform == 'ABI_SOLID')
"""
fastqc -f fastq -t ${task.cpus} ${fastq}
for f in ${fastq}; do
mv -- "\$f" "\${f%.fastq}.fq.gz"
done
"""
if (layout == 'SINGLE')
"""
trim_galore --cores ${task.cpus} --fastqc --basename ${sample_id} ${fastq}
"""
else
"""
trim_galore --cores ${task.cpus} --fastqc --paired --basename ${sample_id} ${fastq}
"""
}
// *********************************
// * Step 5: Align reads to genome *
// *********************************
process bowtie_align {
maxRetries 3
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
publishDir "${params.outdir}/bowtie", mode: 'copy', pattern: '*_bowtie.txt'
label 'bowtie'
label 'large'
input:
tuple sample_id, layout, platform, file(fastq) from bowtie_input_ch
file(index) from index_ch.collect()
file(cspace_index) from cspace_index_ch.collect()
output:
tuple sample_id, layout, platform, file("*.sam") into sam_ch, sam_ch2
file('*_bowtie.txt') into bowtie_results_ch
script:
if ( platform == 'ABI_SOLID' )
index_arg = "-C cspace_index"
else
index_arg = "index"
if ( layout == 'SINGLE')
"""
bowtie -X 1000 -3 3 -n 2 -p ${task.cpus} -S ${index_arg} ${fastq} 1> ${sample_id}.sam 2> ${sample_id}_bowtie.txt
"""
else
"""
bowtie -X 1000 -3 3 -n 2 -p ${task.cpus} -S ${index_arg} -1 ${fastq[0]} -2 ${fastq[1]} 1> ${sample_id}.sam 2> ${sample_id}_bowtie.txt
"""
}
// ********************************
// * Step 5B: Convert to BAM file *
// ********************************
process sam2bam {
maxRetries 3
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
label 'large'
input:
tuple sample_id,layout,platform,file(samfile) from sam_ch
output:
tuple sample_id,layout,platform,file("*.bam") into bam_ch
script:
"""
samtools view -b ${samfile} -@ ${task.cpus} -o ${sample_id}.unsorted
samtools sort ${sample_id}.unsorted -@ ${task.cpus} -o ${sample_id}.bam
"""
}
// ********************************
// * Step 6: Infer read direction *
// ********************************
process get_read_direction {
maxRetries 3
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
label 'python'
label 'small'
publishDir "${params.outdir}/rseqc", mode: 'copy', pattern: '*.infer_experiment.txt'
input:
tuple sample_id,layout,platform,file(samfile) from sam_ch2
file bed_file from bed_file_ch
output:
tuple val(sample_id),stdout into direction_ch
file("*.txt") into rseqc_results_ch
script:
"""
infer_experiment.py -r ${bed_file} -i ${samfile} > ${sample_id}.infer_experiment.txt
parse_direction.py ${sample_id}.infer_experiment.txt ${layout} | tr -d '\n'
"""
}
bam_ch2 = bam_ch.join(direction_ch)
// *****************************
// * Step 6: Run featureCounts *
// *****************************
process featureCounts {
maxRetries 3
errorStrategy { task.attempt <= maxRetries ? 'retry' : 'ignore' }
publishDir "${params.outdir}/featureCounts", mode: 'copy', pattern: '*.txt*'
label 'large'
input:
tuple sample_id,layout,platform,file(bam_file),val(orientation) from bam_ch2
file(gff) from gff_ch.collect()
output:
file("*_all.txt.summary") into fc_results_ch
file("*_cds.txt") into counts_ch
script:
if ( layout == 'PAIRED')
type = '-p -B -C -P'
else
type = ''
args = "${type} --fracOverlap 0.5 -T ${task.cpus} ${orientation} -a all.gff"
"""
cat ${gff} > all.gff
featureCounts ${args} \
-t CDS -g locus_tag \
-o ${sample_id}_cds.txt \
${bam_file}
featureCounts ${args} \
-t rRNA -g locus_tag \
-o ${sample_id}_rRNA.txt \
${bam_file}
merge_summaries.sh ${sample_id}
"""
}
// ****************************
// * Step 7: Compile all data *
// ****************************
multiqc_config_ch = Channel.fromPath(params.multiqc_config,checkIfExists:true)
process multiqc {
publishDir "${params.outdir}", mode:'copy'
label 'large'
input:
file('fastqc/*') from fastqc_results_ch.collect().ifEmpty([])
file('cutadapt/*') from cutadapt_results_ch.collect().ifEmpty([])
file('rseqc/*') from rseqc_results_ch.collect().ifEmpty([])
file('bowtie/*') from bowtie_results_ch.collect().ifEmpty([])
file('featureCounts/*') from fc_results_ch.collect().ifEmpty([])
file(myconfig) from multiqc_config_ch
output:
file "multiqc_report.html" into multiqc_report_ch
file "multiqc_data"
file "multiqc_stats.tsv"
script:
"""
multiqc -f -c ${myconfig} .
assemble_qc_stats.py multiqc_data
"""
}
process assemble_tpm {
publishDir "${params.outdir}", mode:'copy'
label 'python'
label 'large'
input:
file('featureCounts/*') from counts_ch.collect().ifEmpty([])
output:
file('log_tpm.csv')
script:
"""
assemble_tpm.py -d featureCounts -o .
"""
}