-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
549 lines (446 loc) · 15 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env nextflow
// Setup the various inputs, defined in nexflow.config
if (params.input_source == "flat_folder") {
fastq_pair_ch = Channel.fromFilePairs(params.input_folder + '*{1,2}.fastq.gz', flat: true)
.view()
.into{align_input; fastqc_ch}
} else {
// eg "s3://uwlm-personal/umi_development/fastq/294R/demux-nf-umi/libraries/**/{1,2}.fastq.gz"
fastq_pair_ch = Channel.fromPath(params.input_folder + "**/{1,2}.fastq.gz")
.map { path ->
def (fastq, readgroup, library_type, sample_id, rest) = path.toString().tokenize("/").reverse()
return [sample_id, path]
}
.groupTuple()
.map{
// ensure two files present for each sample
key, files -> if (files.size() != 2) error "Samples must each have exactly two FASTQ files."
return [key, files[0], files[1]]
}
.into{align_input; fastqc_ch}
}
// initialize optional parameters
params.downsample_reads = null
params.save_intermediate_output = false
// Assay specific files
picard_targets = file(params.picard_targets)
picard_baits = file(params.picard_baits)
bed_file = file(params.bed)
// Reference genome is used multiple times
reference_fasta = file(params.ref_fasta)
reference_index = Channel.fromPath(params.ref_index).into {
bwa_ref_index;
bwa_realign_ref_index;
picard_ref_index;
qc_ref_index;
filter_con_ref_index
}
process bwa {
// Align fastqs
label 'bwa'
tag "${sample_id}"
input:
file(reference_fasta) from reference_fasta
file("*") from bwa_ref_index.collect()
tuple sample_id, file(fastq1), file(fastq2) from align_input
output:
tuple val(sample_id), file('*.bam') into align_ch
tuple val(sample_id), val("standard"), file('*.standard.bam'), file('*.bai') into qc_standard_bam
//"standar" bam is coordinate sorted for use in QC metrics and IGV
publishDir params.output, overwrite: true
cpus 8
script:
// bwa mem options:
// -K seed, -C pass tags from FASTQ -> alignment, -Y recommended by GATK?, -p using paired end input
// seqtk sample options:
// -s seed
// -2 -- use a two-pass approach to reduce memory consumption
if ("downsample_reads" in params)
"""
seqtk mergepe \
<(seqtk sample -2 -s 10000000 ${fastq1} ${params.downsample_reads}) \
<(seqtk sample -2 -s 10000000 ${fastq2} ${params.downsample_reads}) \
| bwa mem \
-R'@RG\\tID:${sample_id}\\tSM:${sample_id}' \
-K 10000000 \
-C \
-Y \
-t${task.cpus} \
${reference_fasta} \
-p - \
2> log.txt \
| samtools sort -t${task.cpus} -m4G - -o ${sample_id}.standard.bam
samtools index ${sample_id}.standard.bam
"""
else
"""
bwa mem \
-R'@RG\\tID:${sample_id}\\tSM:${sample_id}' \
-K 10000000 \
-C \
-Y \
-t${task.cpus} \
${reference_fasta} ${fastq1} ${fastq2} 2> log.txt \
| samtools sort -t${task.cpus} -m4G - -o ${sample_id}.standard.bam
samtools index ${sample_id}.standard.bam
"""
}
process sort_bam {
// Sort alignment by query name
label 'sambamba'
tag "${sample_id}"
input:
tuple val(sample_id), file(bam) from align_ch
output:
tuple val(sample_id), file('*.sorted.bam') into set_mate_ch
script:
"""
sambamba sort --tmpdir=./ \
--sort-picard \
--nthreads ${task.cpus} \
--memory-limit ${task.memory.toGiga()-1}GB \
--out=${sample_id}.sorted.bam \
${bam}
"""
}
process fgbio_setmateinformation{
// Adds and/or fixes mate information on paired-end reads
// tuples the MQ (mate mapping quality), MC (mate cigar string),
// ensures all mate-related flag fields are tuple correctly,
// and that the mate reference and mate start position are correct.
label "fgbio"
tag "${sample_id}"
input:
tuple sample_id, path(bam) from set_mate_ch
output:
tuple sample_id, "${sample_id}.mateinfo.bam" into mate_info_bam_ch
tuple val(sample_id), val("set_mate"), file('*.bam') into temp_qc_mate_bam
memory "32G"
publishDir path: params.output, overwrite: true, enabled: params.save_intermediate_output
script:
"""
fgbio -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./\
SetMateInformation \
--input ${bam} \
--output ${sample_id}.mateinfo.bam
"""
}
process fgbio_group_umi {
// Groups reads together that appear to have come from the same original molecule.
label "fgbio"
tag "${sample_id}"
input:
tuple sample_id, path(bam) from mate_info_bam_ch
output:
tuple val(sample_id), file('*.grpumi.bam') into grp_umi_bam_ch
tuple val(sample_id), val("grpumi"), file('*.grpumi.bam') into qc_grpumi_bam
file('*.grpumi.histogram') into histogram_ch
memory "32G"
publishDir path: params.output, overwrite: true, enabled: params.save_intermediate_output
script:
"""
fgbio -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./\
GroupReadsByUmi \
--input=${bam} \
--output=${sample_id}.grpumi.bam \
--family-size-histogram=${sample_id}.grpumi.histogram \
--strategy=adjacency
"""
}
process fgbio_callconsensus{
// Combined each set of reads to generate consensus reads
// 1. base qualities are adjusted
// 2. consensus sequence called for all reads with the same UMI, base-by-base.
// 3. consensus raw base quality is modified by incorporating the probability of an error prior to
// calls each end of a pair independently, and does not jointly call bases that overlap within a pair. Insertion or deletion
// errors in the reads are not considered in the consensus model.integrating the unique molecular tags
label 'fgbio'
tag "${sample_id}"
input:
tuple sample_id, path(bam) from grp_umi_bam_ch
output:
tuple val(sample_id), file('*.consensus.bam') into consensus_bam_ch
tuple val(sample_id), val("consensus"), file('*.consensus.bam') into qc_consensus_bam
memory "32G"
publishDir path: params.output, overwrite: true, enabled: params.save_intermediate_output
script:
"""
fgbio -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./\
CallMolecularConsensusReads \
--input=${bam} \
--output=${sample_id}.consensus.bam \
--min-reads=1 \
--read-name-prefix=${sample_id} \
--read-group-id=${sample_id} \
--error-rate-pre-umi=45 \
--error-rate-post-umi=40 \
--min-input-base-quality=10 \
--output-per-base-tags=true \
--sort-order=queryname
"""
}
process fgbio_filterconsensus{
label "fgbio"
tag "${sample_id}"
input:
file(reference_fasta) from reference_fasta
file("*") from filter_con_ref_index.collect()
tuple val(sample_id), file(bam) from consensus_bam_ch
output:
tuple val(sample_id), file('*.filtered_consensus.bam') into filter_consensus_bam_ch
tuple val(sample_id), val("filtered_consensus"), file('*.filtered_consensus.bam') into qc_filtered_consensus_bam
memory "32G"
publishDir path: params.output, overwrite: true, enabled: params.save_intermediate_output
script:
"""
fgbio -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./\
FilterConsensusReads \
--input=${bam} \
--output=${sample_id}.filtered_consensus.bam \
--ref=${reference_fasta} \
--min-reads=2 \
--max-read-error-rate 0.05 \
--min-base-quality 10 \
--max-base-error-rate 0.1 \
--max-no-call-fraction 0.1
"""
}
process sort_filter_bam {
// Sort alignment by query name
label "sambamba"
tag "${sample_id}"
input:
tuple sample_id, path(bam) from filter_consensus_bam_ch
output:
tuple sample_id, "${sample_id}.sorted_consensus.bam" into (sorted_consensus_ch, sorted_consensus_realignment_ch)
publishDir path: params.output, overwrite: true, enabled: params.save_intermediate_output
script:
"""
sambamba sort --tmpdir=./ \
--sort-picard \
--nthreads ${task.cpus} \
--memory-limit ${task.memory.toGiga()-1}GB \
--out=${sample_id}.sorted_consensus.bam \
${bam}
"""
}
process realign_consensus {
//-p Assume the first input query file is interleaved paired-end FASTA/Q.
//-Y use soft clipping for supplementary alignment
//-K process INT input bases in each batch regardless of nThreads (for reproducibility)
label "bwa"
tag "${sample_id}"
input:
file(reference_fasta) from reference_fasta
file("*") from bwa_realign_ref_index.collect()
tuple sample_id, path(bam) from sorted_consensus_realignment_ch
output:
tuple sample_id, "${sample_id}.realigned.bam" into realign_ch
tuple val(sample_id), val("realigned"), file('*realigned.bam'), file('*.bai') into qc_sorted_final_bam
//"realigned" bam is coordinate sorted, for QC and IGV viewing
cpus 8
script:
"""
samtools bam2fq -n ${bam} | \
bwa mem \
-R "@RG\\tID:${sample_id}\\tSM:${sample_id}" \
-K 10000000 \
-p \
-Y \
-t ${task.cpus} \
${reference_fasta} \
-p - 2> log.txt \
| samtools sort -t${task.cpus} -m4G - -o ${sample_id}.realigned.bam
samtools index ${sample_id}.realigned.bam
"""
}
process sort_realign_bam {
// Sort alignment by query name
label 'sambamba'
tag "${sample_id}"
input:
tuple sample_id, path(bam) from realign_ch
output:
tuple sample_id, "${sample_id}.sorted.bam" into sorted_realign_consensus_ch
script:
"""
sambamba sort --tmpdir=./ \
--sort-picard \
--nthreads ${task.cpus} \
--memory-limit ${task.memory.toGiga()-1}GB \
--out=${sample_id}.sorted.bam \
${bam}
"""
}
merge_ch = sorted_realign_consensus_ch.join(sorted_consensus_ch, remainder: true)
process final_bam {
//Merge consensus bam (unaligned) with aligned bam, which is queryname sorted
label 'picard'
tag "${sample_id}"
input:
file(reference_fasta) from reference_fasta
file("*") from picard_ref_index.collect()
tuple sample_id, path(sorted_bam), path(sorted_filtered_bam) from merge_ch
output:
tuple val(sample_id), val("final"), file('*.final.bam'), file('*.bai') into qc_final_bam
publishDir params.output, overwrite: true
memory "32G"
script:
"""
picard -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./ -Dpicard.useLegacyParser=false\
MergeBamAlignment \
-UNMAPPED ${sorted_filtered_bam} \
-ALIGNED ${sorted_bam} \
-O ${sample_id}.final.bam \
-R ${reference_fasta} \
-VALIDATION_STRINGENCY SILENT \
-SORT_ORDER coordinate \
-CREATE_INDEX true
mv ${sample_id}.final.bai ${sample_id}.final.bam.bai
"""
}
// ######### QC ######### //
// Combine channels for quality here
// into a single quality_ch for processing below.
qc_final_bam.mix(
qc_standard_bam,
).into{ hs_metrics_ch; mosdepth_qc_ch; temp_x }
temp_x.mix(
qc_grpumi_bam,
qc_consensus_bam,
qc_filtered_consensus_bam
).map { [it[0], it[1], it[2]] } // excludes bai file which may not be present in stream
.into { simple_count_qc_ch }
process simple_quality_metrics {
label 'sambamba'
tag "${sample_id}"
cpus 2
memory "2GB"
input:
tuple val(sample_id), val(bam_type), file(bam) from simple_count_qc_ch
output:
file("${sample_id}.${bam_type}.flagstats.txt") into simple_count_out_ch
script:
"""
sambamba flagstat ${bam} \
2> /dev/null \
> ${sample_id}.${bam_type}.flagstats.txt
"""
}
process quality_metrics {
label 'picard'
tag "${sample_id}"
input:
file(picard_targets) from picard_targets
file(picard_baits) from picard_baits
file(reference_fasta) from reference_fasta
file("*") from qc_ref_index.collect()
tuple val(sample_id), val(bam_type), file(bam), file(bai) from hs_metrics_ch
output:
path("${sample_id}.${bam_type}.hs_metrics") into hs_metrics_out_ch
path("${sample_id}.${bam_type}.insert_size_metrics") into insert_size_metrics_ch
publishDir params.output, overwrite: true
memory "32G"
script:
"""
picard -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./ -Dpicard.useLegacyParser=false \
CollectHsMetrics \
-TARGET_INTERVALS ${picard_targets} \
-BAIT_INTERVALS ${picard_baits} \
-COVERAGE_CAP 100000 \
-REFERENCE_SEQUENCE ${reference_fasta} \
-INPUT ${bam} \
-OUTPUT ${sample_id}.${bam_type}.hs_metrics
picard -Xmx${task.memory.toGiga()}g -Djava.io.tmpdir=./ -Dpicard.useLegacyParser=false \
CollectInsertSizeMetrics \
-INCLUDE_DUPLICATES true \
-INPUT ${bam} \
-OUTPUT ${sample_id}.${bam_type}.insert_size_metrics \
-H ${sample_id}.${bam_type}.insert_size_histogram.pdf
"""
}
process fastqc {
label 'fastqc'
tag "${sample_id}"
input:
tuple sample_id, file(fastq1), file(fastq2) from fastqc_ch
output:
path "fastqc/*", type:"dir" into fastqc_report_ch
cpus 2
memory '8 GB'
publishDir params.output, pattern: "*.html", mode: "copy", overwrite: true
script:
fastqc_path = "fastqc/${sample_id}/"
"""
mkdir -p ${fastqc_path}
zcat ${fastq1} ${fastq2} | fastqc --quiet -o ${fastqc_path} stdin:${sample_id}
"""
}
process mosdepth {
label 'mosdepth'
tag "${sample_id}"
input:
file(bed) from bed_file
tuple val(sample_id), val(bam_type), file(bam), file(bai) from mosdepth_qc_ch
output:
file "${sample_id}.${bam_type}.regions.bed.gz"
file "${sample_id}.${bam_type}.mosdepth.region.dist.txt" into mosdepth_out_ch
memory '4 GB'
cpus 4 // per docs, no benefit after 4 threads
publishDir params.output
script:
"""
mosdepth -t ${task.cpus} --by ${bed} --no-per-base --fast-mode ${sample_id}.${bam_type} ${bam}
"""
}
process multiqc {
label 'multiqc'
input:
path('*') from fastqc_report_ch.flatMap().collect()
path('*') from hs_metrics_out_ch.flatMap().collect()
path('*') from simple_count_out_ch.flatMap().collect()
path('*') from insert_size_metrics_ch.flatMap().collect()
path('*') from histogram_ch.flatMap().collect()
path("*") from mosdepth_out_ch.flatMap().collect()
output:
file "multiqc_report.${params.run_id}.html"
file "multiqc_report.${params.run_id}_data/multiqc_data.json"
file "qc_summary.${params.run_id}_mqc.csv"
memory '4 GB'
cpus 4
publishDir params.output, saveAs: {f -> "multiqc/${f}"}, mode: "copy", overwrite: true
script:
"""
preprocess_qc.py counts *.flagstats.txt --output qc_counts.${params.run_id}_mqc.csv
rm *.flagstats.txt
multiqc -d --filename "multiqc_report_pre.${params.run_id}.html" .
preprocess_qc.py summary multiqc_report_pre.${params.run_id}_data/multiqc_data.json qc_summary.${params.run_id}_mqc.csv
rm -rf multiqc_report_pre.${params.run_id}_data
multiqc -d -e general_stats --filename "multiqc_report.${params.run_id}.html" .
"""
}
/*
VarDict \
-G ${reference_fasta} \
-C \
-F 0 \
-f 0.000000000001 \
-N ${sample_id} \
-b ${bam} \
-c 1 \
-S 2 \
-E 3 \
-g 4 \
-r 1 \
-q 1 \
-VS SILENT \
-th ${task.cpus} \
${bed_file} |
teststrandbias.R |
var2vcf_valid.pl \
-N ${sample_id} \
-f 0.000000000001 \
> ${sample_id}.vardict.vcf
"""
}
*/