-
Notifications
You must be signed in to change notification settings - Fork 22
/
Cecret.nf
1583 lines (1298 loc) · 52.9 KB
/
Cecret.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
println("Currently using the Cecret workflow for use with amplicon-based Illumina hybrid library prep on MiSeq\n")
println("Author: Erin Young")
println("email: eriny@utah.gov")
println("Version: v.20210416")
println("")
params.reads = workflow.launchDir + '/reads'
params.single_reads = workflow.launchDir + '/single_reads'
if ( params.reads == params.single_reads ) {
println("'params.reads' and 'params.single_reads' cannot point to the same directory!")
println("'params.reads' is set to " + params.reads)
println("'params.single_reads' is set to " + params.single_reads)
exit 1
}
params.outdir = workflow.launchDir + '/cecret'
// reference files for SARS-CoV-2 (part of the github repository)
params.reference_genome = workflow.projectDir + "/configs/MN908947.3.fasta"
params.gff_file = workflow.projectDir + "/configs/MN908947.3.gff"
params.primer_bed = workflow.projectDir + "/configs/artic_V3_nCoV-2019.bed"
params.amplicon_bed = workflow.projectDir + "/configs/nCoV-2019.insert.bed"
params.trimmer = 'ivar'
params.cleaner = 'seqyclean'
params.aligner = 'bwa'
// to toggle off processes
params.bcftools_variants = false // fails to download a lot
params.fastqc = true
params.ivar_variants = true
params.samtools_stats = true
params.samtools_coverage = true
params.samtools_flagstat = true
params.samtools_ampliconstats = true
params.samtools_plot_ampliconstats = false
params.bedtools_multicov = true
params.nextclade = true
params.pangolin = true
params.bamsnap = false // can be really slow
params.rename = false
params.filter = false
params.vadr = true
// for optional contamination determination
params.kraken2 = false
params.kraken2_db = ''
params.kraken2_organism = "Severe acute respiratory syndrome coronavirus 2"
// for optional route of tree generation and counting snps between samples
params.relatedness = false
params.snpdists = true
params.iqtree = true
params.max_ambiguous = '0.50'
params.outgroup = 'MN908947.3'
params.mode='GTR'
// for optional renaming of files for GISAID and GenBank submissions
params.sample_file = workflow.launchDir + '/covid_samples.csv'
params.gisaid_threshold = '25000'
params.genbank_threshold = '15000'
params.maxcpus = Runtime.runtime.availableProcessors()
println("The maximum number of CPUS used in this workflow is ${params.maxcpus}")
if ( params.maxcpus < 5 ) {
params.medcpus = params.maxcpus
} else {
params.medcpus = 5
}
// This is where the results will be
println("The files and directory for results is " + params.outdir)
println("A table summarizing results will be created: ${params.outdir}/summary.txt and ${workflow.launchDir}/run_results.txt\n")
Channel
.fromPath(params.reference_genome, type:'file')
.ifEmpty{
println("No reference genome was selected. Set with 'params.reference_genome'")
exit 1
}
.view { "Reference Genome : $it"}
.into { reference_genome ; reference_genome2 ; reference_genome_mafft ; reference_genome_bamsnap }
Channel
.fromPath(params.gff_file, type:'file')
.view { "GFF file for Reference Genome : $it"}
.set { gff_file }
Channel
.fromPath(params.primer_bed, type:'file')
.ifEmpty{
println("A bedfile for primers is required. Set with 'params.primer_bed'.")
exit 1
}
.view { "Primer BedFile : $it"}
.into { primer_bed ; primer_bed_bedtools ; primer_bed_ampliconstats }
Channel
.fromFilePairs(["${params.reads}/*_R{1,2}*.fastq.gz",
"${params.reads}/*_{1,2}.fastq*"], size: 2 )
.map{ reads -> tuple(reads[0].replaceAll(~/_S[0-9]+_L[0-9]+/,""), reads[1], "paired" ) }
.set { paired_reads }
Channel
.fromFilePairs("${params.single_reads}/*.fastq*", size: 1 )
.map{ reads -> tuple(reads[0].replaceAll(~/_S[0-9]+_L[0-9]+/,""), reads[1], "single" ) }
.set { single_reads }
amplicon_bed = params.bedtools_multicov
? Channel.fromPath(params.amplicon_bed, type:'file').view { "Amplicon BedFile : $it"}
: Channel.empty()
kraken2_db = params.kraken2
? Channel.fromPath(params.kraken2_db, type:'dir').view { "Kraken2 database : $it" }
: Channel.empty()
paired_reads
.concat(single_reads)
.ifEmpty{
println("No fastq or fastq.gz files were found at ${params.reads} or ${params.single_reads}")
println("Set 'params.reads' to directory with paired-end reads")
println("Set 'params.single_reads' to directory with single-end reads")
exit 1
}
.into { fastq_reads_seqyclean ; fastq_reads_fastp ; fastq_reads_fastqc ; fastq_reads_rename }
println("") // just for aesthetics
// TBA : param that coincides with the staphb/seqyclean:1.10.09 container run with singularity
params.seqyclean_contaminant_file="/Adapters_plus_PhiX_174.fasta"
params.seqyclean_minlen = 25
process seqyclean {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/seqyclean:latest'
stageInMode = 'symlink'
when:
params.cleaner == 'seqyclean'
input:
set val(sample), file(reads), val(paired_single) from fastq_reads_seqyclean
output:
tuple sample, file("${task.process}/${sample}_clean_PE{1,2}.fastq.gz") optional true into seqyclean_paired_files
tuple sample, file("${task.process}/${sample}_cln_SE.fastq.gz") optional true into seqyclean_single_file
tuple sample, file("${task.process}/${sample}_clean_PE{1,2}.fastq.gz"), val(paired_single) optional true into seqyclean_paired_files_classification
tuple sample, file("${task.process}/${sample}_cln_SE.fastq.gz"), val(paired_single) optional true into seqyclean_single_file_classification
file("${task.process}/${sample}_cl*n_SummaryStatistics.{txt,tsv}")
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(perc_kept) into seqyclean_perc_kept_results
tuple sample, env(kept) into seqyclean_pairskept_results
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
echo "seqyclean version: $(seqyclean -h | grep Version)" >> $log_file
kept=''
perc_kept=''
if [ "!{paired_single}" == "single" ]
then
seqyclean -minlen !{params.seqyclean_minlen} -qual -c !{params.seqyclean_contaminant_file} -U !{reads} -o !{task.process}/!{sample}_cln 2>> $err_file >> $log_file
kept=$(cut -f 36 !{task.process}/!{sample}_cln_SummaryStatistics.tsv | grep -v "Kept" | head -n 1)
perc_kept=$(cut -f 37 !{task.process}/!{sample}_cln_SummaryStatistics.tsv | grep -v "Kept" | head -n 1)
else
seqyclean -minlen !{params.seqyclean_minlen} -qual -c !{params.seqyclean_contaminant_file} -1 !{reads[0]} -2 !{reads[1]} -o !{task.process}/!{sample}_clean 2>> $err_file >> $log_file
kept=$(cut -f 58 !{task.process}/!{sample}_clean_SummaryStatistics.tsv | grep -v "Kept" | head -n 1)
perc_kept=$(cut -f 59 !{task.process}/!{sample}_clean_SummaryStatistics.tsv | grep -v "Kept" | head -n 1)
fi
gzip !{task.process}/!{sample}_clean*.fastq
if [ -z "$kept" ] ; then kept="0" ; fi
if [ -z "$perc_kept" ] ; then perc_kept="0" ; fi
'''
}
process fastp {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'bromberglab/fastp:latest'
stageInMode = 'symlink'
when:
params.cleaner == 'fastp'
input:
set val(sample), file(reads), val(paired_single) from fastq_reads_fastp
output:
tuple sample, file("${task.process}/${sample}_clean_PE{1,2}.fastq.gz") optional true into fastp_paired_files
tuple sample, file("${task.process}/${sample}_cln.fastq.gz") optional true into fastp_single_file
tuple sample, file("${task.process}/${sample}_clean_PE{1,2}.fastq.gz"), val(paired_single) optional true into fastp_paired_files_classification
tuple sample, file("${task.process}/${sample}_cln.fastq.gz"), val(paired_single) optional true into fastp_single_file_classification
file("${task.process}/${sample}_fastp.{html,json}")
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(passed_reads) into fastp_results
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
fastp --version >> $log_file
if [ "!{paired_single}" == "single" ]
then
fastp -i !{reads} \
-o !{task.process}/!{sample}_cln.fastq.gz \
-h !{task.process}/!{sample}_fastp.html \
-j !{task.process}/!{sample}_fastp.json \
2>> $err_file >> $log_file
else
fastp -i !{reads[0]} \
-I !{reads[1]} \
-o !{task.process}/!{sample}_clean_PE1.fastq.gz \
-O !{task.process}/!{sample}_clean_PE2.fastq.gz \
-h !{task.process}/!{sample}_fastp.html \
-j !{task.process}/!{sample}_fastp.json \
2>> $err_file >> $log_file
fi
passed_reads=$(grep "reads passed filter" $err_file | tail -n 1 | cut -f 2 -d ":" | sed 's/ //g' )
if [ -z "$passed_reads" ] ; then passed_reads="0" ; fi
'''
}
seqyclean_paired_files
.concat(fastp_paired_files)
.concat(seqyclean_single_file)
.concat(fastp_single_file)
.combine(reference_genome)
.into { clean_reads_bwa ; clean_reads_minimap2 }
seqyclean_paired_files_classification
.concat(fastp_paired_files_classification)
.concat(seqyclean_single_file_classification)
.concat(fastp_single_file_classification)
.set { clean_reads_classification }
process bwa {
publishDir "${params.outdir}", mode: 'copy', pattern: "logs/bwa/*.{log,err}"
tag "${sample}"
echo false
cpus params.maxcpus
container 'staphb/bwa:latest'
stageInMode = 'symlink'
when:
params.aligner == 'bwa'
input:
set val(sample), file(reads), file(reference_genome) from clean_reads_bwa
output:
tuple sample, file("aligned/${sample}.sam") into bwa_sams
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(bwa_version) into bwa_version
shell:
'''
mkdir -p aligned logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
echo "bwa $(bwa 2>&1 | grep Version )" >> $log_file
bwa_version="bwa : "$(bwa 2>&1 | grep Version)
# index the reference fasta file
bwa index !{reference_genome}
# bwa mem command
bwa mem -t !{task.cpus} !{reference_genome} !{reads} 2>> $err_file > aligned/!{sample}.sam
'''
}
// minimap2 paramaters
params.minimap2_K = '20M' // stolen from monroe
params.minimap2_options = ''
process minimap2 {
publishDir "${params.outdir}", mode: 'copy', pattern: "logs/minimap2/*.{log,err}"
tag "${sample}"
echo false
cpus params.maxcpus
container 'staphb/minimap2:latest'
stageInMode = 'symlink'
when:
params.aligner == 'minimap2'
input:
set val(sample), file(reads), file(reference_genome) from clean_reads_minimap2
output:
tuple sample, file("aligned/${sample}.sam") into minimap2_sams
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(minimap2_version) into minimap2_version
shell:
'''
mkdir -p aligned logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
minimap2 --version >> $log_file
minimap2_version=$(echo "minimap2 : "$(minimap2 --version))
minimap2 !{params.minimap2_options} \
-K !{params.minimap2_K} \
-ax sr -t !{task.cpus} \
-o aligned/!{sample}.sam \
!{reference_genome} !{reads} 2>> $err_file >> $log_file
'''
}
bwa_version
.concat(minimap2_version)
.set { aligner_version }
bwa_sams
.concat(minimap2_sams)
.into { sams ; sams_filter }
params.fastqc_options = ''
process fastqc {
publishDir "${params.outdir}", mode: 'copy'
tag "$sample"
echo false
cpus 1
container 'staphb/fastqc:latest'
stageInMode = 'symlink'
when:
params.fastqc
input:
set val(sample), file(raw), val(type) from fastq_reads_fastqc
output:
file("${task.process}/*.{html,zip}")
tuple sample, env(raw_1) into fastqc_1_results
tuple sample, env(raw_2) into fastqc_2_results
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
fastqc --version >> $log_file
fastqc !{params.fastqc_options} \
--outdir !{task.process} \
--threads !{task.cpus} \
!{raw} \
2>> $err_file >> $log_file
zipped_fastq=($(ls !{task.process}/*fastqc.zip) "")
raw_1=$(unzip -p ${zipped_fastq[0]} */fastqc_data.txt | grep "Total Sequences" | awk '{ print $3 }' )
raw_2=NA
if [ -f "${zipped_fastq[1]}" ] ; then raw_2=$(unzip -p !{task.process}/*fastqc.zip */fastqc_data.txt | grep "Total Sequences" | awk '{ print $3 }' ) ; fi
if [ -z "$raw_1" ] ; then raw_1="0" ; fi
if [ -z "$raw_2" ] ; then raw_2="0" ; fi
'''
}
process sort {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus params.maxcpus
container 'staphb/samtools:latest'
stageInMode = 'symlink'
input:
set val(sample), file(sam) from sams
output:
tuple sample, file("aligned/${sample}.sorted.bam") into pre_trim_bams, pre_trim_bams2
tuple sample, file("aligned/${sample}.sorted.bam"), file("aligned/${sample}.sorted.bam.bai") into pre_trim_bams_bamsnap
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p aligned logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
samtools sort !{sam} 2>> $err_file | \
samtools view -F 4 -o aligned/!{sample}.sorted.bam 2>> $err_file >> $log_file
# indexing the bams
samtools index aligned/!{sample}.sorted.bam 2>> $err_file >> $log_file
'''
}
params.filter_options = ''
process filter {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/samtools:latest'
stageInMode = 'symlink'
when:
params.filter
input:
set val(sample), file(sam) from sams_filter
output:
tuple sample, file("${task.process}/${sample}_filtered_{R1,R2}.fastq.gz") optional true into filtered_reads
file("${task.process}/${sample}_filtered_unpaired.fastq.gz") optional true
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
samtools sort -n !{sam} 2>> $err_file | \
samtools fastq -F 4 !{params.filter_options} \
-s !{task.process}/!{sample}_filtered_unpaired.fastq.gz \
-1 !{task.process}/!{sample}_filtered_R1.fastq.gz \
-2 !{task.process}/!{sample}_filtered_R2.fastq.gz \
2>> $err_file >> $log_file
'''
}
pre_trim_bams
.combine(primer_bed)
.into {pre_trim_bams_ivar ; pre_trim_bams_samtools }
// for ivar
params.ivar_quality = 20
params.ivar_frequencing_threshold = 0.6
params.ivar_minimum_read_depth = 10
params.mpileup_depth = 8000
process ivar_trim {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/ivar:latest'
stageInMode = 'symlink'
when:
params.trimmer == 'ivar'
input:
set val(sample), file(bam), file(primer_bed) from pre_trim_bams_ivar
output:
tuple sample, file("${task.process}/${sample}.primertrim.sorted.bam") into ivar_bams
tuple sample, file("${task.process}/${sample}.primertrim.sorted.bam"), file("ivar_trim/${sample}.primertrim.sorted.bam.bai") into ivar_bam_bai
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
ivar version >> $log_file
# trimming the reads
ivar trim -e -i !{bam} -b !{primer_bed} -p !{task.process}/!{sample}.primertrim 2>> $err_file >> $log_file
# sorting and indexing the trimmed bams
samtools sort !{task.process}/!{sample}.primertrim.bam -o !{task.process}/!{sample}.primertrim.sorted.bam 2>> $err_file >> $log_file
samtools index !{task.process}/!{sample}.primertrim.sorted.bam 2>> $err_file >> $log_file
'''
}
params.samtools_ampliconclip_options = ''
process samtools_ampliconclip {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/samtools:latest'
stageInMode = 'symlink'
when:
params.trimmer == 'samtools'
input:
set val(sample), file(bam), file(primer_bed) from pre_trim_bams_samtools
output:
tuple sample, file("${task.process}/${sample}.primertrim.sorted.bam") into samtools_bams
tuple sample, file("${task.process}/${sample}.primertrim.sorted.bam"), file("samtools_trim/${sample}.primertrim.sorted.bam.bai") into samtools_bam_bai
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
# trimming the reads
samtools ampliconclip !{params.samtools_ampliconclip_options} -b !{primer_bed} !{bam} 2>> $err_file | \
samtools sort 2>> $err_file | \
samtools view -F 4 -o !{task.process}/!{sample}.primertrim.sorted.bam 2>> $err_file >> $log_file
samtools index !{task.process}/!{sample}.primertrim.sorted.bam 2>> $err_file >> $log_file
'''
}
ivar_bams
.concat(samtools_bams)
.into { trimmed_bams ; trimmed_bams4 ; trimmed_bams5 }
trimmed_bams5
.combine(primer_bed_ampliconstats)
.set { trimmed_bams_ampliconstats }
trimmed_bams
.combine(reference_genome2)
.into { trimmed_bams_genome ; trimmed_bams_ivar_consensus ; trimmed_bams_bcftools_variants }
trimmed_bams_genome
.combine(gff_file)
.set { trimmed_bams_ivar_variants }
ivar_bam_bai
.concat(samtools_bam_bai)
.combine(amplicon_bed)
.set { trimmed_bam_bai }
process ivar_variants {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/ivar:latest'
memory {2.GB * task.attempt}
errorStrategy {'retry'}
maxRetries 2
stageInMode = 'symlink'
memory {2.GB * task.attempt}
errorStrategy {'retry'}
maxRetries 2
when:
params.ivar_variants
input:
set val(sample), file(bam), file(reference_genome), file(gff_file) from trimmed_bams_ivar_variants
output:
tuple sample, file("${task.process}/${sample}.variants.tsv")
tuple sample, file("${task.process}/${sample}.ivar_variants.vcf") into ivar_variant_file
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(variants_num) into ivar_variants_results
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
ivar version >> $log_file
samtools mpileup -A -d !{params.mpileup_depth} -B -Q 0 --reference !{reference_genome} !{bam} 2>> $err_file | \
ivar variants -p !{task.process}/!{sample}.variants -q !{params.ivar_quality} -t !{params.ivar_frequencing_threshold} -m !{params.ivar_minimum_read_depth} -r !{reference_genome} -g !{gff_file} 2>> $err_file >> $log_file
variants_num=$(grep "TRUE" !{task.process}/!{sample}.variants.tsv | wc -l)
if [ -z "$variants_num" ] ; then variants_num="0" ; fi
echo '##fileformat=VCFv4.2' > !{task.process}/!{sample}.ivar_variants.vcf
echo '##source=iVar' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FILTER=<ID=PASS,Description="Result of p-value <= 0.05">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FILTER=<ID=FAIL,Description="Result of p-value > 0.05">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=REF_DP,Number=1,Type=Integer,Description="Depth of reference base">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=REF_RV,Number=1,Type=Integer,Description="Depth of reference base on reverse reads">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=REF_QUAL,Number=1,Type=Integer,Description="Mean quality of reference base">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=ALT_DP,Number=1,Type=Integer,Description="Depth of alternate base">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=ALT_RV,Number=1,Type=Integer,Description="Deapth of alternate base on reverse reads">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=ALT_QUAL,Number=1,Type=String,Description="Mean quality of alternate base">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo '##FORMAT=<ID=ALT_FREQ,Number=1,Type=String,Description="Frequency of alternate base">' >> !{task.process}/!{sample}.ivar_variants.vcf
echo -e '#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t!{sample}' >> !{task.process}/!{sample}.ivar_variants.vcf
tail -n+2 !{task.process}/!{sample}.variants.tsv | \
awk '{print $1 "\t" $2 "\t.\t" $3 "\t" $4 "\t.\t.\tREF_DP=" $5 ";REF_RV=" $6 ";REF_QUAL=" $7 ";ALT_DP=" $8 ";ALT_RV=" $9 ";ALT_QUAL=" $10 "\tGT:PL\t1/1:" $12 "," $12-$8 "," $8 }' \
>> !{task.process}/!{sample}.ivar_variants.vcf
'''
}
process ivar_consensus {
publishDir "${params.outdir}", mode: 'copy', pattern: "logs/ivar_consensus/*.{log,err}"
publishDir "${params.outdir}", mode: 'copy', pattern: "consensus/*.consensus.fa"
tag "${sample}"
echo false
cpus 1
container 'staphb/ivar:latest'
memory {2.GB * task.attempt}
errorStrategy {'retry'}
maxRetries 2
stageInMode = 'symlink'
input:
set val(sample), file(bam), file(reference_genome) from trimmed_bams_ivar_consensus
output:
tuple sample, file("consensus/${sample}.consensus.fa") into consensus_nextclade, consensus_rename
file("consensus/${sample}.consensus.fa") into consensus_pangolin, consensus_vadr
tuple sample, file("consensus/qc_consensus/15000/${sample}.consensus.fa") optional true into qc_consensus_15000_mafft
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(num_N), env(num_ACTG), env(num_degenerate), env(num_total) into consensus_results
tuple sample, env(ivar_version) into ivar_version
shell:
'''
mkdir -p consensus/qc_consensus/{15000,25000} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
ivar version >> $log_file
ivar_version=$(ivar version | grep "version")
samtools mpileup -A -d !{params.mpileup_depth} -B -Q 0 --reference !{reference_genome} !{bam} 2>> $err_file | \
ivar consensus -q !{params.ivar_quality} -t !{params.ivar_frequencing_threshold} -m !{params.ivar_minimum_read_depth} -p consensus/!{sample}.consensus -n N 2>> $err_file >> $log_file
num_N=$(grep -v ">" consensus/!{sample}.consensus.fa | grep -o 'N' | wc -l )
if [ -z "$num_N" ] ; then num_N="0" ; fi
num_ACTG=$(grep -v ">" consensus/!{sample}.consensus.fa | grep -o -E "C|A|T|G" | wc -l )
if [ -z "$num_ACTG" ] ; then num_ACTG="0" ; fi
if [ "$num_ACTG" -gt 15000 ] ; then cp consensus/!{sample}.consensus.fa consensus/qc_consensus/15000/!{sample}.consensus.fa ; fi
num_degenerate=$(grep -v ">" consensus/!{sample}.consensus.fa | grep -o -E "B|D|E|F|H|I|J|K|L|M|O|P|Q|R|S|U|V|W|X|Y|Z" | wc -l )
if [ -z "$num_degenerate" ] ; then num_degenerate="0" ; fi
num_total=$(( $num_N + $num_degenerate + $num_ACTG ))
'''
}
process bcftools_variants {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/bcftools:latest'
stageInMode = 'symlink'
when:
params.bcftools_variants
input:
set val(sample), file(bam), file(reference_genome) from trimmed_bams_bcftools_variants
output:
tuple sample, file("${task.process}/${sample}.vcf") into bcftools_variants_file
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(variants_num) into bcftools_variants_results
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
# time stamp + capturing tool versions
date | tee -a $log_file $err_file > /dev/null
bcftools --version >> $log_file
bcftools mpileup -A -d !{params.mpileup_depth} -B -Q 0 -f !{reference_genome} !{bam} 2>> $err_file | \
bcftools call -mv -Ov -o !{task.process}/!{sample}.vcf 2>> $err_file >> $log_file
variants_num=$(grep -v "#" bcftools_variants/!{sample}.vcf | wc -l)
if [ -z "$variants_num" ] ; then variants_num="0" ; fi
'''
}
pre_trim_bams_bamsnap
.join(ivar_variant_file, remainder: true, by:0)
.join(bcftools_variants_file, remainder: true, by:0)
.combine(reference_genome_bamsnap)
.set { bamsnap_files }
params.bamsnap_options = ''
process bamsnap {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus params.medcpus
errorStrategy 'ignore'
container 'danielmsk/bamsnap:latest'
stageInMode = 'symlink'
time '1h'
when:
params.bamsnap
input:
tuple val(sample), file(bam), file(bai), file(ivar), file(bcftools), file(reference_genome) from bamsnap_files
output:
file("${task.process}/${sample}/{ivar,bcftools}/*.{png,log}") optional true
file("${task.process}/${sample}/*.{png,log}") optional true
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
bamsnap --version >> $log_file
if [[ "!{ivar}" != *"input"* ]]
then
mkdir -p bamsnap/!{sample}
bamsnap \
-draw coordinates bamplot coverage base \
!{params.bamsnap_options} \
-process !{task.cpus} \
-ref !{reference_genome} \
-bam !{bam} \
-vcf !{ivar} \
-out !{task.process}/!{sample}/ivar \
-imagetype png \
-save_image_only 2>> $err_file >> $log_file
fi
if [[ "!{bcftools}" != *"input"* ]]
then
mkdir -p bamsnap/!{sample}
bamsnap \
-draw coordinates bamplot coverage base \
!{params.bamsnap_options} \
-process !{task.cpus} \
-ref !{reference_genome} \
-bam !{bam} \
-vcf !{bcftools} \
-out !{task.process}/!{sample}/bcftools \
-imagetype png \
-save_image_only 2>> $err_file >> $log_file
fi
'''
}
pre_trim_bams2
.combine(trimmed_bams4, by: 0)
.into { pre_post_bams ; pre_post_bams2 ; pre_post_bams3 }
params.samtools_stats_options = ''
process samtools_stats {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/samtools:latest'
stageInMode = 'symlink'
when:
params.samtools_stats
input:
set val(sample), file(aligned), file(trimmed) from pre_post_bams
output:
file("${task.process}/aligned/${sample}.stats.txt")
file("${task.process}/trimmed/${sample}.stats.trim.txt")
tuple sample, env(insert_size_before_trimming) into samtools_stats_before_size_results
tuple sample, env(insert_size_after_trimming) into samtools_stats_after_size_results
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process}/aligned !{task.process}/trimmed logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
samtools stats !{params.samtools_stats_options} !{aligned} 2>> $err_file > !{task.process}/aligned/!{sample}.stats.txt
samtools stats !{params.samtools_stats_options} !{trimmed} 2>> $err_file > !{task.process}/trimmed/!{sample}.stats.trim.txt
insert_size_before_trimming=$(grep "insert size average" !{task.process}/aligned/!{sample}.stats.txt | cut -f 3)
insert_size_after_trimming=$(grep "insert size average" !{task.process}/trimmed/!{sample}.stats.trim.txt | cut -f 3)
'''
}
params.samtools_coverage_options = ''
process samtools_coverage {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/samtools:latest'
stageInMode = 'symlink'
when:
params.samtools_coverage
input:
set val(sample), file(aligned), file(trimmed) from pre_post_bams2
output:
file("${task.process}/{aligned,trimmed}/${sample}.cov.{txt,hist}")
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(coverage) into samtools_coverage_results
tuple sample, env(depth) into samtools_depth_results
shell:
'''
mkdir -p !{task.process}/aligned !{task.process}/trimmed logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
samtools coverage !{params.samtools_coverage_options} !{aligned} -m -o !{task.process}/aligned/!{sample}.cov.hist 2>> $err_file >> $log_file
samtools coverage !{params.samtools_coverage_options} !{aligned} -o !{task.process}/aligned/!{sample}.cov.txt 2>> $err_file >> $log_file
samtools coverage !{params.samtools_coverage_options} !{trimmed} -m -o !{task.process}/trimmed/!{sample}.cov.trim.hist 2>> $err_file >> $log_file
samtools coverage !{params.samtools_coverage_options} !{trimmed} -o !{task.process}/trimmed/!{sample}.cov.trim.txt 2>> $err_file >> $log_file
coverage=$(cut -f 6 !{task.process}/trimmed/!{sample}.cov.trim.txt | tail -n 1)
depth=$(cut -f 7 !{task.process}/trimmed/!{sample}.cov.trim.txt | tail -n 1)
if [ -z "$coverage" ] ; then coverage_trim="0" ; fi
if [ -z "$depth" ] ; then depth_trim="0" ; fi
'''
}
params.samtools_flagstat_options = ''
process samtools_flagstat {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/samtools:latest'
stageInMode = 'symlink'
input:
set val(sample), file(aligned), file(trimmed) from pre_post_bams3
when:
params.samtools_flagstat
output:
file("${task.process}/{aligned,trimmed}/${sample}.flagstat.txt")
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
shell:
'''
mkdir -p !{task.process}/aligned !{task.process}/trimmed logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
samtools --version >> $log_file
samtools flagstat !{params.samtools_flagstat_options} \
!{aligned} \
2>> $err_file > !{task.process}/aligned/!{sample}.flagstat.txt
samtools flagstat !{params.samtools_flagstat_options} \
!{trimmed} \
2>> $err_file > !{task.process}/trimmed/!{sample}.flagstat.txt
'''
}
clean_reads_classification
.combine(kraken2_db)
.set{ clean_reads_kraken2 }
params.kraken2_options = ''
process kraken2 {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus params.maxcpus
container 'staphb/kraken2:latest'
stageInMode = 'symlink'
when:
params.kraken2
input:
set val(sample), file(clean), val(paired_single), path(kraken2_db) from clean_reads_kraken2
output:
file("${task.process}/${sample}_kraken2_report.txt")
file("logs/${task.process}/${sample}.${workflow.sessionId}.{log,err}")
tuple sample, env(percentage_cov) into kraken2_sars_results
tuple sample, env(percentage_human) into kraken2_human_results
shell:
'''
mkdir -p !{task.process} logs/!{task.process}
log_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
err_file=logs/!{task.process}/!{sample}.!{workflow.sessionId}.err
date | tee -a $log_file $err_file > /dev/null
kraken2 --version >> $log_file
if [ ! -d !{kraken2_db} ]
then
echo "Kraken2 database could not be found. Please specify with params.kraken2_db" | tee -a $err_file
fi
if [ "!{paired_single}" == "single" ]
then
kraken2 !{params.kraken2_options} \
--classified-out cseqs#.fq \
--threads !{task.cpus} \
--db !{kraken2_db} \
!{clean} \
--report !{task.process}/!{sample}_kraken2_report.txt \
2>> $err_file >> $log_file
else
kraken2 !{params.kraken2_options} \
--paired \
--classified-out cseqs#.fq \
--threads !{task.cpus} \
--db !{kraken2_db} \
!{clean} \
--report !{task.process}/!{sample}_kraken2_report.txt \
2>> $err_file >> $log_file
fi
percentage_human=$(grep "Homo sapiens" !{task.process}/!{sample}_kraken2_report.txt | awk '{print $1}')
percentage_cov=$(grep "!{params.kraken2_organism}" !{task.process}/!{sample}_kraken2_report.txt | awk '{print $1}')
if [ -z "$percentage_human" ] ; then percentage_human="0" ; fi
if [ -z "$percentage_cov" ] ; then percentage_cov="0" ; fi
'''
}
params.bedtools_multicov_options = '-f .1'
process bedtools_multicov {
publishDir "${params.outdir}", mode: 'copy'
tag "${sample}"
echo false
cpus 1
container 'staphb/bedtools:latest'
stageInMode = 'symlink'
when:
params.bedtools_multicov
input:
set val(sample), file(bam), file(bai), file(amplicon_bed) from trimmed_bam_bai
output:
file("${task.process}/${sample}.multicov.txt")
tuple sample, env(num_failed_amplicons) into bedtools_results