-
Notifications
You must be signed in to change notification settings - Fork 2
/
pengu.nf
1941 lines (1362 loc) · 59.9 KB
/
pengu.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
// INPUT RUN FOLDER
runDir = "${params.dir}"
// INPUT READS
allFastq = "${runDir}/Data/Intensities/BaseCalls/*_R{1,2}_001.fastq.gz"
// MultiQC config file
multiQCConf = "${params.multiqcconf}"
// Centrifuge DB
centrifugeDB = "${params.centrifugedbpath}"
// SORT READS BY SIZE AND TAKE THE LARGEST ONE - MAKES SURE IT HAS AT LEAST ONE READ
//allFilesBySize = file(allFastq).listFiles().sort{ it.size() }.reverse()
//largestFile = allFilesBySize[0]
// THIS IS HORRIBLE BUT IT WORKS - CALL BASH SCRIPT TO GET RUNID
//RunIDCmd = "getRunID ${largestFile}"
File runDir = new File("${runDir}")
RunID = runDir.getName().toString()
// Sequencing metadata channel
Date today = new Date()
YMD = today.format( "yyyy-MM-dd" )
// OUTPUT DIR
if ( RunID == "000000_M04531_0000_000000000_T3STP" ) {
//Date today = new Date()
YMD_time = today.format( "yyyy-MM-dd_HH-mm-ss" )
outDir = "${params.testoutdir}/${YMD_time}"
}
else {
outDir = "${params.outdir}"
}
// PROJECT ID LIST - these should map to shares and are defined in config file
projects = params.projectlist
// Setup input channels
// This takes all fastqs and filters so that only those defined in config file (projectlist) get processed
Channel.fromFilePairs( "${allFastq}" , flat: true)
.filter { it[0].tokenize("-")[0].toLowerCase() in projects }
//.into{ CopyRawFastq; MakeProjectFastq }
.set{ InputReads }
// Channel for MiSeq run directory, needed for InterOp process
Channel.fromPath( runDir, type: 'dir', maxDepth: 1 )
.into{ IlluminaRunDir ; backupRunDir }
// Channel for centrifuge database files, so that they are symlinked into WORKDIR
Channel.fromPath( centrifugeDB ).set{ CentrifugeRefs }
// Channel for NCBI taxIDs for each project
Channel
.from( params.taxIDdict )
.set { taxID }
// Channel for multiqc config file
Channel
.fromPath( multiQCConf )
.set{ multiQCConfYaml }
process IlluminaInteropStats {
tag{ RunID }
container "file:///${params.simgdir}/interop.simg"
input:
file(interopDir) from IlluminaRunDir
output:
file "*.summary.csv" into InterOpMultiQC
script:
"""
interop_summary --csv=1 ${interopDir} > ${RunID}.summary.csv
"""
}
backupHost = params.backupHost
backupUser = params.backupUser
backupPath = params.backupPath
process BackupRunDirectory {
tag{RunID}
when: workflow.profile == 'prod'
input:
file(runDir) from backupRunDir
script:
miseqID = "${runDir}".tokenize("_")[1]
"""
tar -czhf ${runDir}.tgz ${runDir}
rsync -av ${runDir}.tgz ${backupUser}@${backupHost}:${backupPath}/${miseqID}/
"""
}
InputReads.map{ [ it[0] , it[0].tokenize("-")[0].toLowerCase() , it[1] , it[2] ]}.into{ inputReadsProject ; countProject }
projectCount = countProject.map{ [ it[0] , it[1] ]}.countBy{ it[1] }.getVal().toString()
process TrimReads {
tag { dataset_id }
container "file:///${params.simgdir}/trim_galore.simg"
publishDir "${outDir}/${project}/${RunID}/qc/trimmed_reads", pattern: '*_val_{1,2}.fq.gz', mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/qc/fastqc", pattern: '*_fastqc.{zip,html}', mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/qc/trim_galore" , pattern: '*_trimming_report.txt', mode: 'copy'
cpus 2
input:
set dataset_id, project, file(forward), file(reverse) from inputReadsProject
output:
set dataset_id, project, file("*_val_1.fq.gz"), file("*_val_2.fq.gz") optional true into TrimmedReadsLength, TrimmedReadsInsert, TrimmedReadsQC, TrimmedReadsHIV, TrimmedReadsFLU, TrimmedReadsWCMTB, TrimmedReadsARG, TrimmedReadsDIGCD
set project, file("*trimming_report.txt"), file("*_fastqc.{zip,html}") optional true into TrimGaloreResults
script:
"""
if [[ \$(zcat ${forward} | head -n4 | wc -l) -eq 0 ]]; then
exit 0
else
trim_galore --fastqc --paired $forward $reverse
fi
"""
}
process calculateInsertSize {
tag { dataset_id }
container "file:///${params.simgdir}/bbtools.simg"
cpus 2
input:
set dataset_id, project, file(forward), file(reverse) from TrimmedReadsInsert
output:
set project, file("${dataset_id}.ihist") into insertSizeHitogram
script:
"""
bbmerge.sh strict=t reads=1000000 ihist=${dataset_id}.ihist in=${forward} in2=${reverse}
"""
}
process MeanTrimmedReadLength {
tag { dataset_id }
container "file:///${params.simgdir}/seqtk.simg"
input:
set dataset_id, project, file(forward), file(reverse) from TrimmedReadsLength
output:
set project, dataset_id, stdout into ReadsLengthSummary
script:
"""
zcat $forward $reverse | seqtk fqchk - | head -n1 | cut -d ";" -f3 | cut -d " " -f3 | tr -d '\n'
"""
}
process Centrifuge {
tag { dataset_id }
container "file:///${params.simgdir}/centrifuge.simg"
publishDir "${outDir}/${project}/${RunID}/qc/centrifuge", mode: 'copy'
cpus 8
input:
set dataset_id, project, file(forward), file(reverse) from TrimmedReadsQC
file dbs from CentrifugeRefs.toList()
output:
set project, file("*_centrifugereport.tab") into CentrifugeReport
set project, dataset_id, file("*_centrifugereport.tab") into CentrifugeSummary
script:
"""
centrifuge --mm -q -p ${task.cpus} -x phw -1 $forward -2 $reverse -S /dev/null --report-file ${dataset_id}_centrifugereport.tab
"""
}
process CentrifugeSummary {
tag { dataset_id }
container "file:///${params.simgdir}/taxonkit.simg"
cpus 2
input:
set project, dataset_id, file(centrifuge_report), taxID from CentrifugeSummary.combine(taxID, by:0)
output:
set dataset_id, project, stdout into TotalBpCalc
set project, taxID into MagnitudePrepare, TranslateTaxonomy
script:
"""
centrifuge-summary -a -i ${taxID} -r ${centrifuge_report} | sed 's/No matching reads found in this sample/0/g' | cut -f2 | tr -d '\n'
"""
}
process TranslateTaxonomy {
tag { project }
container "file:///${params.simgdir}/taxonkit.simg"
cpus 2
input:
set project, taxID from TranslateTaxonomy.unique()
output:
set project, stdout into MagnitudeSummaryTaxName
script:
"""
taxonkit list --indent "" --show-name --ids ${taxID} | head -n1 | cut -d " " -f2- | tr -d '\n'
"""
}
process PrepareMagnitudeSummary {
tag { project }
executor 'local'
input:
set project, taxID from MagnitudePrepare.unique()
output:
set taxID, project into MagnitudeSummaryTaxID
exec:
def file = new File("${outDir}/${project}/${RunID}/qc/magnitude_summary.csv")
if (file.exists()){
file.delete();
}
file.append("Sample,Taxon,NCBI TaxID,Number of reads matching taxon,Mean read length,Total bp attributed to taxon\n")
}
process CalculateMagnitudeSummary {
tag { dataset_id }
executor 'local'
input:
set project, dataset_id, numberReads, taxID, taxName, avgReadLength from TotalBpCalc.combine(MagnitudeSummaryTaxID, by: 1).combine(MagnitudeSummaryTaxName, by: 0).combine(ReadsLengthSummary, by:[0,1])
exec:
matching = Float.parseFloat(numberReads) * Float.parseFloat(avgReadLength)
matchingBP = matching.toInteger()
def file = new File("${outDir}/${project}/${RunID}/qc/magnitude_summary.csv")
file.append("${dataset_id},${taxName},${taxID},${numberReads},${avgReadLength},${matchingBP}\n")
}
process MultiQC {
tag { proctag }
container "file:///${params.simgdir}/multiqc.simg"
publishDir "${outDir}/${project}/${RunID}/qc", mode: 'copy'
input:
set project, file("*"), file("*"), file("*"), file(interop), file(multiqcconf) from TrimGaloreResults.groupTuple(by: 0).map{ [ it[0], it[1].flatten().toList(), it[2].flatten().toList() ] }.join(insertSizeHitogram.groupTuple(), by: 0).combine(InterOpMultiQC).combine(multiQCConfYaml)
output:
file "multiqc_report.html"
file "*_data"
script:
projectUpper = "${project}".toUpperCase()
proctag = RunID + "-" + projectUpper
"""
multiqc -m bbmap -m cutadapt -m fastqc -m interop -i "${projectUpper} ${RunID}" -n multiqc_report.html .
"""
}
process Krona {
tag { proctag }
container "file:///${params.simgdir}/kronatools.simg"
publishDir "${outDir}/${project}/${RunID}/qc", mode: 'copy'
input:
set project, file("centrifuge_reports/*") from CentrifugeReport.groupTuple(by: 0)
output:
file "centrifuge_report.html"
script:
projectUpper = "${project}".toUpperCase()
proctag = RunID + "-" + projectUpper
"""
ktImportTaxonomy -o centrifuge_report.html -m 5 -s 7 centrifuge_reports/*
"""
}
// ** ## HIV PIPELINE ## ** //
// Setup HIV trimmed reads channel
HIVTrimmedReads = TrimmedReadsHIV.filter { it[1] == 'hiv' }
// Get today's year and month
Date date = new Date()
YearMonth = date.format( "yyyy/MM-MMM" )
// Make reports directory if it doesn't exist
/*
ReportsDir = file("/mnt/datastore/hiv/reports/${YearMonth}")
mkdirResult = ReportsDir.mkdirs()
println mkdirResult ? "Made new reports directory" : "Cannot create directory: $ReportsDir"
*/
// Setup references
HIVComp = Channel.fromPath( "${params.subref}" )
HIVHXB2 = Channel.fromPath( "${params.HXB2ref}" )
// Setup init_dir for shiver
ShiverInit = Channel.fromPath( "${params.shiverinit}", type: 'dir', maxDepth: 1)
ShiverConf = Channel.fromPath( "${params.shiverconf}" )
// Minor variant frequency list
Channel.from(params.minvarfreq).set{ MinVarFreq }
process CleanHIVReads {
tag { dataset_id }
container "file:///${params.simgdir}/minimap2.simg"
cpus 4
input:
set dataset_id, project, file(forward), file(reverse), file(ref) from HIVTrimmedReads.combine(HIVComp)
output:
set dataset_id, project, file("${dataset_id}.clean_1.fq.gz"), file("${dataset_id}.clean_2.fq.gz") into HIVCleanReadsAssembly, HIVCleanReadsPolishing, HIVCleanReadsVariantCalling
script:
"""
minimap2 -t ${task.cpus} -x sr -a $ref $forward $reverse | samtools view -F 4 -@ 2 -b > clean.bam
picard SamToFastq VALIDATION_STRINGENCY=LENIENT I=clean.bam F=${dataset_id}.clean_1.fq.gz F2=${dataset_id}.clean_2.fq.gz
"""
}
process SampleHIVReads {
tag { dataset_id }
container "file:///${params.simgdir}/bbtools.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/01-clean_subsampled_reads", pattern: '*.clean.sampled_{1,2}.fq.gz', mode: 'copy'
input:
set dataset_id, project, file(forward), file(reverse) from HIVCleanReadsAssembly
output:
set dataset_id, project, file("${dataset_id}.clean.sampled_1.fq.gz"), file("${dataset_id}.clean.sampled_2.fq.gz") into HIVSampledReadsAssembly
script:
"""
reformat.sh samplebasestarget=${params.samplebases} in=$forward in2=$reverse out=formatted_1.fastq.gz out2=formatted_2.fastq.gz
shuffle.sh -Xmx2g in=formatted_1.fastq.gz in2=formatted_2.fastq.gz out=${dataset_id}.clean.sampled_1.fq.gz out2=${dataset_id}.clean.sampled_2.fq.gz
"""
}
process AssembleHIVReads {
tag { dataset_id }
publishDir "${outDir}/${project}/${RunID}/analysis/02-assembly", pattern: "${dataset_id}.iva.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/02-assembly/logs", pattern: "${dataset_id}.iva*.log", mode: 'copy'
container "file:///${params.simgdir}/iva.simg"
cpus 4
input:
set dataset_id, project, file(forward), file(reverse) from HIVSampledReadsAssembly
output:
set dataset_id, project, file("${dataset_id}.iva.fa") optional true into HIVIVAAssembly
set project, file("*.log") optional true
script:
"""
if iva -v --threads ${task.cpus} -f $forward -r $reverse iva_assembly 2>&1 > ${dataset_id}.iva.log ; then
mv iva_assembly/contigs.fasta ${dataset_id}.iva.fa
else
mv ${dataset_id}.iva.log ${dataset_id}.iva.fail.log
fi
"""
}
if(params.shiver == 'false'){
process OrderHIVContigs {
tag { dataset_id }
container "file:///${params.simgdir}/assembly_improvement.simg"
input:
set dataset_id, project, file(assembly), file(ref) from HIVIVAAssembly.combine(HIVHXB2)
output:
set dataset_id, project, file("${dataset_id}.ordered.fa") into HIVIVAAssemblyOrdered
script:
"""
order_contigs_with_abacas -a $assembly -c $ref
mv ${dataset_id}.iva.fa.scaffolded.filtered ${dataset_id}.ordered.fa
sed -i "s/>/>${dataset_id}_ordered_/g" ${dataset_id}.ordered.fa
"""
}
process GapfillHIVContigs {
tag { dataset_id }
publishDir "${outDir}/${project}/${RunID}/analysis/02-assembly", pattern: "${dataset_id}.polished.fa", mode: 'copy'
container "file:///${params.simgdir}/assembly_improvement.simg"
cpus 4
input:
set dataset_id, project, file(assembly), file(forward), file(reverse) from HIVIVAAssemblyOrdered.join(HIVCleanReadsPolishing)
output:
set dataset_id, project, file("${dataset_id}.polished.fa") into HIVAssemblyBAM, HIVAssemblyVariants
script:
// NEED TO TOUCH FILES OR GAP2SEQ FALLS OVER ON CONTIGS WITH NO GAP
"""
touch tmp.gaps
touch tmp.fill
Gap2Seq.sh -nb-cores ${task.cpus} -k 51 -scaffolds $assembly -filled ${dataset_id}.polished.fa -reads ${forward},${reverse}
sed -i "s/ordered/polished/g" ${dataset_id}.polished.fa
"""
}}
else {
process HIVShiver {
tag { dataset_id }
container "file:///${params.simgdir}/shiver.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/02-assembly", pattern: "${dataset_id}.shiver.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/02-assembly", pattern: "${dataset_id}.shiverlog.txt", mode: 'copy'
input:
set dataset_id, project, file(assembly), file(forward), file(reverse), file(shiverconf), file(shiverinit) from HIVIVAAssembly.join(HIVCleanReadsPolishing, by: [0,1]).combine(ShiverConf).combine(ShiverInit)
output:
set dataset_id, project, file("${dataset_id}.shiver.fa") optional true into HIVAssemblyBAM, HIVAssemblyVariants
file("${dataset_id}.shiver.txt") optional true
script:
"""
if shiver_align_contigs.sh ${shiverinit} ${shiverconf} ${assembly} ${dataset_id}; then
if [ -f ${dataset_id}_cut_wRefs.fasta ]; then
shiver_map_reads.sh ${shiverinit} ${shiverconf} ${assembly} ${dataset_id} ${dataset_id}.blast ${dataset_id}_cut_wRefs.fasta ${forward} ${reverse}
else
shiver_map_reads.sh ${shiverinit} ${shiverconf} ${assembly} ${dataset_id} ${dataset_id}.blast ${dataset_id}_raw_wRefs.fasta ${forward} ${reverse}
fi
seqtk seq -l0 ${dataset_id}_remap_consensus_MinCov_15_30.fasta | head -n2 | sed '/>/!s/-//g' | sed 's/\\?/N/g' | sed 's/_remap_consensus//g' | seqtk seq -l80 > ${dataset_id}.shiver.fa
else
echo "No HIV contigs found. This sample is likely to be purely contamination" > ${dataset_id}.shiverlog.txt
fi
"""
}
}
process HIVMappingVariantCalling {
tag { dataset_id }
cpus 4
container "file:///${params.simgdir}/minimap2.simg"
input:
set dataset_id, project, file(forward), file(reverse), file(assembly) from HIVCleanReadsVariantCalling.join(HIVAssemblyBAM, by: [0,1])
output:
set dataset_id, project, file("${dataset_id}.variants.bam") into HIVMappingNoDupsBAM
script:
"""
samtools faidx $assembly
minimap2 -t ${task.cpus} -x sr -a $assembly $forward $reverse | samtools view -@ 2 -b | samtools sort -@ 2 -o ${dataset_id}.variants.bam
"""
}
process HIVVariantCallingVarScan {
tag { dataset_id }
container "file:///${params.simgdir}/variant_calling.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/minor_variants", pattern: "${dataset_id}.${minvarfreq}.minor.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/IUPAC", pattern: "${dataset_id}.${minvarfreq}.iupac.consensus.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/vcf", pattern: "${dataset_id}.${minvarfreq}.consensus.vcf", mode: 'copy'
input:
set dataset_id, project, file(bam), file(assembly), minvarfreq from HIVMappingNoDupsBAM.join(HIVAssemblyVariants, by: [0,1]).combine(MinVarFreq)
output:
set dataset_id, project, minvarfreq, file("*.${minvarfreq}.iupac.consensus.fa") into HIVAssemblyWithVariants
file "${dataset_id}.${minvarfreq}.minor.fa"
file "${dataset_id}.${minvarfreq}.consensus.vcf"
script:
"""
samtools mpileup --max-depth 10000000 --redo-BAQ --min-MQ 17 --min-BQ 20 --output ${dataset_id}.mpileup --fasta-ref ${assembly} ${bam}
java -Xmx17G -jar /usr/local/bin/varscan.jar mpileup2cns ${dataset_id}.mpileup --min-var-freq ${minvarfreq} --p-value 95e-02 --min-coverage 100 --output-vcf 1 > ${dataset_id}.varscan.cns.vcf
bgzip ${dataset_id}.varscan.cns.vcf
tabix -p vcf ${dataset_id}.varscan.cns.vcf.gz
bcftools view -i'FILTER="PASS"' -Oz -o ${dataset_id}.varscan.cns.filtered.vcf.gz ${dataset_id}.varscan.cns.vcf.gz
zcat ${dataset_id}.varscan.cns.filtered.vcf.gz > ${dataset_id}.${minvarfreq}.consensus.vcf
tabix -p vcf ${dataset_id}.varscan.cns.filtered.vcf.gz
bcftools consensus -f $assembly ${dataset_id}.varscan.cns.filtered.vcf.gz --output ${dataset_id}.${minvarfreq}.minor.fa
bcftools consensus -I -f $assembly ${dataset_id}.varscan.cns.filtered.vcf.gz --output ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i 's/polished/consensus-minor/g' ${dataset_id}.${minvarfreq}.minor.fa
sed -i 's/polished/consensus-iupac/g' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variant bases IUPAC] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variants bases ONLY] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.minor.fa
"""
}
/*
process HIVVariantCallingBCFtools {
tag { dataset_id }
container "file:///${params.simgdir}/variant_calling.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/minor_variants", pattern: "${dataset_id}.${minvarfreq}.minor.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/IUPAC", pattern: "${dataset_id}.${minvarfreq}.iupac.consensus.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/vcf", pattern: "${dataset_id}.${minvarfreq}.consensus.vcf", mode: 'copy'
input:
set dataset_id, project, file(bam), file(assembly), minvarfreq from HIVMappingNoDupsBAMBCFtools.join(HIVAssemblyVariantsBCFtools, by: [0,1]).combine(MinVarFreqBCFtools)
output:
set dataset_id, project, minvarfreq, file("*.${minvarfreq}.iupac.consensus.fa") into HIVAssemblyWithVariantsBCFtools
file "${dataset_id}.${minvarfreq}.minor.fa"
file "${dataset_id}.${minvarfreq}.consensus.vcf"
script:
"""
samtools faidx $assembly
bcftools mpileup --redo-BAQ --min-MQ 20 -Ou -f $assembly ${bam} | bcftools call --ploidy 1 -mv -Ov | bcftools view -q ${minvarfreq}:nref -Oz -o ${dataset_id}.variants.vcf.gz
zcat ${dataset_id}.variants.vcf.gz > ${dataset_id}.${minvarfreq}.consensus.vcf
tabix ${dataset_id}.variants.vcf.gz
bcftools consensus -f $assembly ${dataset_id}.variants.vcf.gz --output ${dataset_id}.${minvarfreq}.minor.fa
bcftools consensus -I -f $assembly ${dataset_id}.variants.vcf.gz --output ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i 's/polished/consensus-minor/g' ${dataset_id}.${minvarfreq}.minor.fa
sed -i 's/polished/consensus-iupac/g' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variant bases IUPAC] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variants bases ONLY] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.minor.fa
"""
}
process HIVVariantCallingLoFreq {
tag { dataset_id }
container "file:///${params.simgdir}/variant_calling.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/minor_variants", pattern: "${dataset_id}.${minvarfreq}.minor.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/fasta/IUPAC", pattern: "${dataset_id}.${minvarfreq}.iupac.consensus.fa", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/03-call_variants/vcf", pattern: "${dataset_id}.${minvarfreq}.consensus.vcf", mode: 'copy'
input:
set dataset_id, project, file(bam), file(assembly), minvarfreq from HIVMappingNoDupsBAMLofreq.join(HIVAssemblyVariantsLofreq, by: [0,1]).combine(MinVarFreqLofreq)
output:
set dataset_id, project, minvarfreq, file("*.${minvarfreq}.iupac.consensus.fa") into HIVAssemblyWithVariantsLofreq
file "${dataset_id}.${minvarfreq}.minor.fa"
file "${dataset_id}.${minvarfreq}.consensus.vcf"
script:
"""
samtools faidx $assembly
lofreq call -C 100 --call-indels -D -f $assembly -o - ${bam} | bcftools view -i'AF>${minvarfreq}' -Oz -o ${dataset_id}.variants.vcf.gz
zcat ${dataset_id}.variants.vcf.gz > ${dataset_id}.${minvarfreq}.consensus.vcf
tabix ${dataset_id}.variants.vcf.gz
bcftools consensus -f $assembly ${dataset_id}.variants.vcf.gz --output ${dataset_id}.${minvarfreq}.minor.fa
bcftools consensus -I -f $assembly ${dataset_id}.variants.vcf.gz --output ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i 's/polished/consensus-minor/g' ${dataset_id}.${minvarfreq}.minor.fa
sed -i 's/polished/consensus-iupac/g' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variant bases IUPAC] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.iupac.consensus.fa
sed -i '/^>/ s/\$/ [Variant caller: ${params.variantstrategy}] [Minor variants bases ONLY] [Variant frequency: ${minvarfreq}] /' ${dataset_id}.${minvarfreq}.minor.fa
"""
}
*/
HIVAssemblyWithSelectedMinVarFreq = HIVAssemblyWithVariants.filter{ it[2] == params.selectedminvarfreq }
process HIVMakeResistanceReport {
tag { dataset_id }
container "file:///${params.simgdir}/sierrapy.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/04-call_resistance", pattern: "${dataset_id}.json", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/05-generate_report", pattern: "${dataset_id}.rtf", mode: 'copy'
publishDir "${outDir}/${project}/reports/${YearMonth}", pattern: "${dataset_id}.rtf", mode: 'copy'
input:
set dataset_id, project, minvarfreq, file(variantassembly) from HIVAssemblyWithSelectedMinVarFreq
output:
file("${dataset_id}.json")
file("${dataset_id}.rtf")
script:
episodenumber = dataset_id.split('-').last()
"""
buildreport.pl -i ${variantassembly} -n ${dataset_id} -l PHW_Cardiff
"""
}
// ** ## FLU PIPELINE ## ** //
// Setup FLU trimmed reads channel
FLUTrimmedReads = TrimmedReadsFLU.filter { it[1] == 'flu' }
// Setup init_dir for shiver
FLUShiverInit = Channel.fromPath( "${params.flushiverinitroot}", type: 'dir')
FLUShiverConf = Channel.fromPath( "${params.flushiverconf}" )
// Setup references
FLURef = Channel.from(['NA', file(params.flurefNA)],
['HA', file(params.flurefHA)],
['M1', file(params.flurefM1)],
['PB1', file(params.flurefPB1)],
['PB2', file(params.flurefPB2)],
['NP', file(params.flurefNP)],
['NS1', file(params.flurefNS1)],
['PA', file(params.flurefPA)])
process SeparateFLUSegmentReads {
tag { proctag }
container "file:///${params.simgdir}/minimap2.simg"
cpus 4
input:
set dataset_id, project, file(forward), file(reverse), segment, file(ref) from FLUTrimmedReads.combine(FLURef)
output:
set dataset_id, project, segment, file("${dataset_id}.${segment}_1.fq.gz"), file("${dataset_id}.${segment}_2.fq.gz") into FLUCleanReadsAssembly,FLUCleanReadsShiver
script:
proctag = dataset_id + "-" + segment
"""
minimap2 -t ${task.cpus} -x sr -a $ref $forward $reverse | samtools view -F 4 -@ 2 -b > mapped.bam
picard SamToFastq VALIDATION_STRINGENCY=LENIENT I=mapped.bam F=${dataset_id}.${segment}_1.fq.gz F2=${dataset_id}.${segment}_2.fq.gz
"""
}
process AssembleFLUReads {
tag { proctag }
container "file:///${params.simgdir}/iva.simg"
cpus 4
input:
set dataset_id, project, segment, file(forward), file(reverse) from FLUCleanReadsAssembly
output:
set dataset_id, project, segment, file("${dataset_id}.${segment}.iva.fa") optional true into FLUIVAAssembly
set project, file("*.assembly.failed") optional true into FLUIVAFail
script:
proctag = dataset_id + "-" + segment
"""
if iva --threads ${task.cpus} -f $forward -r $reverse iva_assembly &> ${dataset_id}.${segment}.assembly.failed ; then
mv iva_assembly/contigs.fasta ${dataset_id}.${segment}.iva.fa
sed -i "s/>/>${dataset_id}\\.${segment}\\./g" ${dataset_id}.${segment}.iva.fa
else
sed -i "1s/^/${dataset_id}\\.${segment}\t/" ${dataset_id}.${segment}.assembly.failed
fi
"""
}
fluAssemblyShiver = Channel.create()
fluAssemblyNotShiverReconstitute = Channel.create()
FLUIVAAssembly.choice( fluAssemblyShiver, fluAssemblyNotShiverReconstitute){ it[2] in params.shiversegs ? 0 : 1 }
process shiverFLU {
tag { proctag }
container "file:///${params.simgdir}/shiver.simg"
cpus 4
input:
set dataset_id, project, segment, file(assembly), file(forward), file(reverse), file(shiverconf), file(shiverinit) from fluAssemblyShiver.combine(FLUCleanReadsShiver, by: [ 0, 1, 2 ]).combine(FLUShiverConf).combine(FLUShiverInit)
output:
set dataset_id, project, segment, file("${dataset_id}.${segment}.shiver.fa") into fluAssemblyShiverReconstitute
script:
proctag = dataset_id + "-" + segment
"""
shiver_align_contigs.sh ${shiverinit}/${segment} ${shiverconf} ${assembly} ${dataset_id}
if [ -f ${dataset_id}_cut_wRefs.fasta ]; then
shiver_map_reads.sh ${shiverinit}/${segment} ${shiverconf} ${assembly} ${dataset_id} ${dataset_id}.blast ${dataset_id}_cut_wRefs.fasta ${forward} ${reverse}
else
shiver_map_reads.sh ${shiverinit}/${segment} ${shiverconf} ${assembly} ${dataset_id} ${dataset_id}.blast ${dataset_id}_raw_wRefs.fasta ${forward} ${reverse}
fi
seqtk seq -l0 ${dataset_id}_remap_consensus_MinCov_15_30.fasta | head -n2 | sed '/>/!s/-//g' | sed 's/\\?/N/g' | sed "s/_remap_consensus/\\.${segment}/g" > ${dataset_id}.${segment}.shiver.fa
"""
}
fluAssemblyNotShiverReconstitute.mix(fluAssemblyShiverReconstitute).map{ [ it[0], it[1], it[3] ] }.groupTuple(by: [ 0, 1]).set{ fluAssemblyReconstitute }
process ReconstituteFLUGenome {
tag { dataset_id }
container "file:///${params.simgdir}/seqtk.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/assembly", pattern: "${dataset_id}.fasta", mode: 'copy'
input:
set dataset_id, project, file('*') from fluAssemblyReconstitute
output:
file "${dataset_id}.fasta"
script:
"""
cat *.fa | seqtk seq -l0 > ${dataset_id}.fasta
"""
}
process CollectFailedFLUAssemblies {
tag { RunID }
publishDir "${outDir}/${project}/${RunID}/analysis", pattern: "IVA.assembly.failed.txt", mode: 'copy'
input:
set project, file("*") from FLUIVAFail.groupTuple()
output:
file "IVA.assembly.failed.txt"
script:
"""
cat *.failed > IVA.assembly.failed.txt
"""
}
// ** ## WCM PIPELINE ## ** //
// Setup WCM trimmed reads channel
WCMTBTrimmedReads = TrimmedReadsWCMTB.filter { it[1] == 'wcmtb' || it[1] == 'wcmid' }
WCMTBTrimmedReads.into{ WCMTBTrimmedReadsMD5; WCMTrimmedReadsTyping; WCMMakeUploadDir; WCMTrimmedReadsKraken; WCMTrimmedReadsShovill; WCMTrimmedReadsPostiveControlMapping }
mykrobepanel = params.mykrobepanel
WCMKrakenDB = params.wcmkrakendbdir
WCMRefGenomeName = params.wcmrefgenome
Channel.fromPath( "${params.wcmH37Rvref}", type: 'dir', maxDepth: 1).set{ wcmH37RvRefDir }
Channel.fromPath( "${params.wcmphenixconf}" ).set{ wcmPHEnixConf }
Channel
.fromPath( WCMKrakenDB )
.set{ WCMKrakenRefs }
Date day = new Date()
YearMonthDay = day.format( "yyyy-MM-dd" )
shortRunID = RunID.tokenize("_")[0, 1].join("_")
sshkey = "${params.wcmtbkey}"
endserver = "${params.wcmtbserver}"
user = "${params.wcmtbuser}"
endpath = "${params.wcmtbpath}/${YearMonthDay}-${shortRunID}"
process assembleShovillWCM {
tag { dataset_id }
errorStrategy 'ignore'
container "file:///${params.simgdir}/shovill.simg"
cpus 5
memory '8 GB'
input:
set dataset_id, project, file(forward), file(reverse) from WCMTrimmedReadsShovill
output:
set dataset_id, project, file("${dataset_id}.fasta") optional true into WCMProkkaAssembly
set project, file("${dataset_id}.fasta") optional true into WCMquast
set dataset_id, project, file("${dataset_id}.fasta") into WCMkrakenAssembly
script:
"""
shovill --cpus ${task.cpus} --R1 ${forward} --R2 ${reverse} --minlen 500 --outdir shovill
mv shovill/contigs.fa ${dataset_id}.fasta
"""
}
process quastWCM {
tag { project }
publishDir "${outDir}/${project}/${RunID}/analysis", pattern: "*_assembly_summary.csv", mode: 'copy'
container "file:///${params.simgdir}/quast.simg"
cpus 4
input:
set project, file(assembly) from WCMquast.groupTuple(by: 0)
output:
file("*_assembly_summary.csv")
script:
"""
quast.py -t ${task.cpus} -o . --contig-thresholds 0 --no-html --no-plots *.fasta
sed 's/\t/,/g' transposed_report.tsv > ${shortRunID}_assembly_summary.csv
"""
}
process annotateProkkaWCM {
tag { dataset_id }
publishDir "${outDir}/${project}/${RunID}/analysis/annotation/", pattern: "${dataset_id}/${dataset_id}.*", mode: 'copy'
container "file:///${params.simgdir}/prokka.simg"
cpus 5
input:
set dataset_id, project, file(assembly) from WCMProkkaAssembly.filter{ it[2].size()>1000 }
output:
file("${dataset_id}/${dataset_id}.*")
set project, file("${dataset_id}.fna") into WCMresAssembly, WCMvirAssembly
script:
locusTag = dataset_id.tokenize("_")[0].tokenize("-")[1]
prefix = project.toUpperCase()
"""
prokka --outdir ${dataset_id} --locustag ${prefix}_${locusTag} --prefix ${dataset_id} --centre PHW --cpus ${task.cpus} --compliant ${assembly}
cp ${dataset_id}/${dataset_id}.fna ${dataset_id}.fna
"""
}
process callResistanceWCM {
tag { project }
publishDir "${outDir}/${project}/${RunID}/analysis", pattern: "*resistance_genes.csv", mode: 'copy'
container "file:///${params.simgdir}/abricate.simg"
input:
set project, file(assembly) from WCMresAssembly.groupTuple(by: 0)
output:
file "*_resistance_genes.csv"
script:
"""
abricate --db ${params.resistancedb} --csv *.fna > ${shortRunID}_resistance_genes.csv
"""
}
process callVirulenceWCM {
tag { project }
publishDir "${outDir}/${project}/${RunID}/analysis", pattern: "*_virulence_genes.csv", mode: 'copy'
container "file:///${params.simgdir}/abricate.simg"
input:
set project, file(assembly) from WCMvirAssembly.groupTuple(by: 0)
output:
file "*_virulence_genes.csv"
script:
"""
abricate --db ${params.virulencedb} --csv *.fna > ${shortRunID}_virulence_genes.csv
"""
}
process krakenWCM {
tag { dataset_id }
publishDir "${outDir}/${project}/${RunID}/analysis/kraken", pattern: "${dataset_id}_krakenreport.txt", mode: 'copy'
container "file:///${params.simgdir}/kraken2.simg"
cpus 5
input:
set dataset_id, project, file(assembly) from WCMkrakenAssembly
file dbs from WCMKrakenRefs.toList()
output:
file("*_krakenreport.txt")
set project, file("${dataset_id}.tab") into WCMKrakenReport
script:
"""
sed -i 's/ /|/g' ${assembly}
kraken2 --db . --threads ${task.cpus} --output ${dataset_id}_kraken.txt --report ${dataset_id}_krakenreport.txt ${assembly}
sed 's/|/\\t/g' ${dataset_id}_kraken.txt | sed 's/len=//g' | cut -f2,3,7 > ${dataset_id}.tab
#cut -f2,3 ${dataset_id}_kraken.tab > ${dataset_id}.tab
"""
}
process kronaWCM {
tag { proctag }
container "file:///${params.simgdir}/kronatools.simg"
publishDir "${outDir}/${project}/${RunID}/analysis/", pattern: "${shortRunID}_wcm-kraken_report.html", mode: 'copy'
input:
set project, file(centrifuge) from WCMKrakenReport.groupTuple(by: 0)
output:
file "${shortRunID}_wcm-kraken_report.html"
script:
projectUpper = "${project}".toUpperCase()
proctag = RunID + "-" + projectUpper
"""
ktImportTaxonomy -m 2 -t 3 -q 1 -o ${shortRunID}_wcm-kraken_report.html *.tab
"""
}
process typingMykrobeWCM {
tag { dataset_id }
publishDir "${outDir}/${project}/${RunID}/analysis/typing/json", pattern: "${dataset_id}.json", mode: 'copy'
publishDir "${outDir}/${project}/${RunID}/analysis/typing/csv", pattern: "${dataset_id}.csv", mode: 'copy'
container "file:///${params.simgdir}/mykrobe-atlas.simg"
errorStrategy 'ignore'
cpus 1
input:
set dataset_id, project, file(forward), file(reverse) from WCMTrimmedReadsTyping
output:
set project, file("${dataset_id}.json") into WCMTypingResistanceReport
set dataset_id, project, file("${dataset_id}.csv") into WCMTypingCsv
script: