-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.nf
More file actions
1353 lines (1092 loc) · 47.4 KB
/
main.nf
File metadata and controls
1353 lines (1092 loc) · 47.4 KB
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
nextflow.enable.dsl=2
/*
*
* Pipeline NF-SPANDx
* Version v4.2
* Description A comparative genomics pipeline
* Authors Derek Sarovich, Erin Price
*
*/
log.info """
================================================================================
NF-SPANDx
v4.2
================================================================================
Thanks for using SPANDx!!
USAGE: nextflow run dsarov/spandx --ref <reference file>
SPANDx by default expects reads to be paired end, in the following format: STRAIN_1.fastq.gz
for the first pair and STRAIN_2.fastq.gz for the second pair.
Reads not in this format will be ignored although you can use a different read
name format by specifying the --fastq parameter
SPANDx expects at least a reference file in FASTA format.
By default all read files present in the current working directory will be processed.
Sequence files within current directory will be aligned against the reference
using BWA, SNPs and indels will be called with GATK and a SNP
matrix will be generated with GATK and VCFTools
Written by Derek Sarovich and Erin Price - University of the Sunshine Coast, Sippy Downs, Australia
Please send bug reports to dereksarovich@gmail.com
If you find SPANDx useful and use it in published work please cite - SPANDx: a
genomics pipeline for comparative analysis of large haploid whole genome
re-sequencing datasets - BMC Research Notes 2014, 7:618"
################################################################################
Required Parameters:
--ref Reference genome for alignment. Must match genome used
in --database (default: k96243.fasta)
Currently you are using $params.ref
Optional Parameters:
--fastq Input PE read file wildcard (default: "*_{1,2}.fastq.gz")
Currently this is set to $params.fastq
--annotation Optionally output annotated variant tables.
If you want to annotate the variant output then
set this parameter to the name of the variant file in snpEff
(default: false)
Currently annotation is set to $params.annotation
--database If you want to annotate the variant output then set this
parameter to the name of the variant file in snpEff
(default: false)
Currently, database is set to $params.database
--phylogeny If you would like to switch off phylogenetic reconstruction
and just generate a list of SNPs/indels then swith this parameter
to false. (default: true)
Currently phylogeny is set to $params.phylogeny
--single_end Optionally use single-end Illumina data. By default SPANDx expects
paired-end read data. Set this variable to the directory containing
single end reads
Currently pairing is set to $params.single_end
--window Default window size used in the bedcov coverage assessment
(default: 1kb)
Currently window is set to $params.window
--assemblies Optionally include a directory of assembled genomes in the
analysis. Set this parameter to 'true' if you wish to included
assembled genomes and place all assembled genomes in a
subdirectory called 'assemblies'. (default: false)
Currently assemblies is set to $params.assemblies
--size SPANDx can optionally down-sample your read data to
run through the pipeline quicker. Set to 0 to skip downsampling
(default: 0). NB the number specified here refers to the number
of reads kept in each pair. Genome coverage will vary with genome
size and sequence length.
Currently size is set to $params.size
--tri_allelic Set to true if you would like tri-allelic SNPs/indels used
in the phylogenetic analysis (default: false).
Currently tri_allelic is set to $params.tri_allelic
--indels Set to true if you would like indels used
in the phylogenetic analysis (default: true).
Currently indels is set to $params.indels
--mixtures Optionally perform within species mixtures analysis.
Set this parameter to 'true' if you are dealing with
multiple strains within the same WGS sample (default: false).
Currently mixtures is set to $params.mixtures
--structural Set to true if you would like to identify structural variants
Note that this step can take a considerable amount of time if
you have deep sequencing data (default: false).
Currently structural variant assessment is set to $params.structural
--notrim Although not generally recommended to switch off, set to true
if you want to skip the timmomatic step (default: false).
Currently notrim is set to $params.notrim
--unaligned Optionally output unaligned reads. Useful for identifying
accessory genome in comparison to a reference or removing
unwanted contamination from raw read data (default: false).
Currently experimental
Currently unaligned is set to $params.unaligned
--fast Uses a different set of tools (i.e. strobealign for alignment)
to complete the pipeline quicker. Currently experimental
Currently fast is set to $params.fast
--ont Set to true if you have Oxford Nanopore Technologies (ONT; nanopore) data
Currently ont is set to $params.ont
--ont_dir Optionally specify a directory containing ONT data
Currently ont_dir set to $params.ont_dir
--freebayes Optionally switch to using freebayes as a variant caller instead of the GATK
Currently freebayes is set to $params.freebayes
If you want to make changes to the default `nextflow.config` file
clone the workflow into a local directory and change parameters
in `nextflow.config`:
nextflow clone dsarov/spandx outdir/
Update to the local cache of this workflow:
nextflow pull dsarov/spandx
==================================================================
==================================================================
"""
/* Index Section
* Create a bunch of indices for SPANDx
*/
ref=params.ref
snpeff_database=params.database
/*
* Improved Nextflow code to detect and load both single-end and paired-end FASTQ files
* into separate channels.
*/
// Initialize the paired_end_fastq channel from file pairs
/*
paired_end_fastq = Channel
.fromFilePairs("${params.fastq}", size: 2, flat: true)
.ifEmpty {
log.info("No paired-end FASTQ files found matching pattern: ${params.fastq}")
return Channel.empty() // Return an empty channel to avoid null issues
}
.map { id, files -> tuple(id, files) }
*/
fastq = Channel
.fromFilePairs("${params.fastq}", flat: true)
.ifEmpty { exit 1, """ Input read files could not be found.
Have you included the read files in the current directory and do they have the correct naming?
With the parameters specified, SPANDx is looking for reads named ${params.fastq}.
To fix this error either rename your reads to match this formatting or specify the desired format
when initializing SPANDx e.g. --fastq "*_{1,2}_sequence.fastq.gz"
"""
}
/*
Channel
.fromFilePairs("${params.fastq}", size: 2, flat: true)
.ifEmpty {
log.info("No paired-end FASTQ files found matching pattern: ${params.fastq}") }
.map { id, files -> tuple(id, files) }
.set { paired_end_fastq }
log.info("Found paired end reads matching pattern: ${params.fastq}")
*/
// --- Local copies of params for channel closures ---
def run_se = params.single_end
def run_ont = params.ont
def run_asm = params.assemblies
// --- Single-End Reads ---
def logged_se = false
def single_end_files = Channel.empty() // 1. Initialize as empty
if (run_se) {
single_end_files = Channel.fromPath("${params.single_end_dir}/*.{fastq,fq}.gz", checkIfExists: true)
}
single_end_fastq = single_end_files
.map { file ->
if (!logged_se) { // 4. "Log once" logic is now safely inside the map
log.info("Found SE data. Adding to analysis")
logged_se = true
}
def id = file.name.toString().tokenize('.').get(0)
return tuple(id, file)
}
.ifEmpty { // 5. Log if the channel was empty (no files found or param was false)
log.info("No single-end FASTQ files found.")
}
// --- ONT Reads ---
def logged_ont = false
def ont_files = Channel.empty()
if (run_ont) {
ont_files = Channel.fromPath("${params.ont_dir}/*.{fastq,fq}.gz", checkIfExists: true)
}
ont_reads = ont_files
.map { file ->
if (!logged_ont) {
log.info("Found ont data. Adding to analysis")
logged_ont = true
}
def id = file.name.find(/(.+?)\.(fastq|fq)\.gz$/) { it[1] }
return tuple(id, file)
}
.ifEmpty {
log.info("No ont files found or ont analysis was not requested")
}
// --- Assembly Files ---
def logged_asm = false
def assembly_files = Channel.empty()
if (run_asm) { // This is a boolean check (true/false)
// This now looks for uncompressed files in the 'assemblies' directory
assembly_files = Channel.fromPath("assemblies/*.{fasta,fa,fna}", checkIfExists: true)
}
assembly_ch = assembly_files
.map { file ->
if (!logged_asm) {
log.info("Found assembly data. Adding to analysis")
logged_asm = true
}
// Removed .gz from the regex
def id = file.name.find(/(.+?)\.(fasta|fa|fna)$/) { it[1] }
return tuple(id, file)
}
.ifEmpty {
log.info("No assembly files found or assembly analysis was not requested")
}
/*
Input read files could not be found.
Please check that you have included the read files in the current directory and that they have the correct naming.
SPANDx is looking for reads named ${params.fastq}.
To fix this error, either rename your reads to match the expected formatting or specify the desired format
when initializing SPANDx e.g. --fastq "*_1_sequence.fastq.gz" for single-end or "*_{1,2}_sequence.fastq.gz" for paired-end.
*/
reference_file = file(params.ref)
reference = params.ref
reference_name = reference_file.baseName
if( !reference_file.exists() ) {
exit 1, """
SPANDx can't find the reference file.
It is currently looking for this file --> ${params.ref}
Please check that you have included the reference file in the current directory and rerun
"""
}
process check_and_dl_database {
conda "$baseDir/env_spandx.yaml"
label "snpeff_dl_db"
input:
file reference
conda = ""
executor 'local'
script:
"""
bash ${baseDir}/bin/Check_and_DL_SnpEff_database.sh ${params.database} ${baseDir} ${ref} ${params.snpeff_cache} ${params.snpeff_config}
"""
}
/*
======================================================================
Part 1: create reference indices, dict files and bed files
======================================================================
*/
process IndexReference {
conda "$baseDir/env_spandx.yaml"
label "index"
input:
file reference_file
output:
path "ref.*", emit: ref_indices
path "*.fai", emit: fai_files
path "*.dict", emit: dict_files
path "*.bed", emit: bed_files
script:
reference_path = file(params.ref)
reference_name = reference_path.baseName
"""
bwa index -a is -p ref $reference
samtools faidx $reference
picard CreateSequenceDictionary R=$reference O=${reference_name}.dict
bedtools makewindows -g ${reference}.fai -w $params.window > ${reference}.bed
"""
}
/*
======================================================================
Part 1B: create synthetic reads from reference files
======================================================================
*/
process Read_synthesis {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
input:
tuple val(id), file(assembly_ch)
output:
tuple val(id), path("${id}_1_cov.fq.gz"), path("${id}_2_cov.fq.gz")
"""
art_illumina -i ${assembly_ch} -p -l 150 -f 30 -m 500 -s 10 -ss HS25 -na -o ${id}_out
mv ${id}_out1.fq ${id}_1_cov.fq
mv ${id}_out2.fq ${id}_2_cov.fq
gzip ${id}_1_cov.fq
gzip ${id}_2_cov.fq
"""
}
/*
=======================================================================
Part 2: read processing, reference alignment and variant identification
=======================================================================
// Variant calling sub-workflow - basically SPANDx with a tonne of updates
// Careful here, not sure if the output overwrites the symlinks
// created by Nextflow (if input is .fq.gz) and would do weird stuff?
*/
/*
=======================================================================
Part 2A: Trim reads with light quality filter and remove adapters
=======================================================================
*/
process Trimmomatic {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
input:
tuple val(id), file(forward), file(reverse)
output:
tuple val(id), path("${id}_1.fq.temp.gz"), path("${id}_2.fq.temp.gz")
script:
if (params.notrim) {
"""
mv ${forward} ${id}_1.fq.temp.gz
mv ${reverse} ${id}_2.fq.temp.gz
"""
} else {
"""
trimmomatic PE -threads 1 ${forward} ${reverse} \
${id}_1.fq.temp.gz ${id}_1_u.fq.gz ${id}_2.fq.temp.gz ${id}_2_u.fq.gz \
ILLUMINACLIP:${projectDir}/resources/all_adapters.fa:2:30:10: \
LEADING:10 TRAILING:10 SLIDINGWINDOW:4:15 MINLEN:36
rm ${id}_1_u.fq.gz ${id}_2_u.fq.gz
"""
}
}
// ont processing
process minimapAlign {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
publishDir "./Outputs/bams", mode: 'copy', pattern: "*.dedup*", overwrite: true
input:
file ref_indices
tuple val(id), file(ont_reads)
file reference
file bed_files
output:
tuple val(id), path("${id}.dedup.bam"), path("${id}.dedup.bam.bai"), emit: dedup_bams
tuple val(id), path("${id}.depth.txt"), emit:depth_files
script:
"""
minimap2 -ax map-ont -t ${task.cpus} ${reference} ${ont_reads} | samtools sort -o ${id}.dedup.bam
samtools index ${id}.dedup.bam
mosdepth --by ${ref}.bed ${id}.output ${id}.dedup.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
}
/*
=======================================================================
Part 2B: Downsample reads to increase speed
=======================================================================
*/
process Downsample {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
// publishDir "./Clean_reads", mode: 'copy', overwrite: false
input:
tuple val(id), file(forward), file(reverse)
output:
tuple val(id), path("${id}_1_cov.fq.gz"), path("${id}_2_cov.fq.gz")
script:
if (params.size > 0) {
"""
seqtk sample -s 11 ${forward} $params.size | gzip - > ${id}_1_cov.fq.gz
seqtk sample -s 11 ${reverse} $params.size | gzip - > ${id}_2_cov.fq.gz
"""
} else {
// Rename files if not downsampled and feed into alignment channel
"""
mv ${forward} ${id}_1_cov.fq.gz
mv ${reverse} ${id}_2_cov.fq.gz
"""
}
}
/*
=======================================================================
Part 2C: Align reads against the reference
=======================================================================
*/
process ReferenceAlignment_assembly {
conda "$baseDir/env_spandx.yaml"
label "spandx_alignment"
tag { "$id" }
publishDir "./Outputs/bams", mode: 'copy', pattern: "*.dedup*", overwrite: true
input:
file ref_indices
tuple val(id), file(forward), file(reverse)
file reference
file bed_files
output:
tuple val(id), path("${id}.dedup.bam"), path("${id}.dedup.bam.bai"), emit: dedup_bams
tuple val(id), path("${id}.depth.txt"), emit:depth_files
script:
if(params.fast) {
"""
strobealign --rg-id ID:${id} --rg SM:${id} --rg PL:ILLUMINA ${ref} ${forward} ${reverse} > ${id}.sam
samtools view -h -b -@ 1 -q 1 -o ${id}.bam_tmp ${id}.sam
samtools sort -@ 1 -o ${id}.dedup.bam ${id}.bam_tmp
samtools index ${id}.dedup.bam
rm ${id}.sam ${id}.bam_tmp
mosdepth --by ${ref}.bed ${id}.output ${id}.dedup.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
} else {
"""
bwa mem -R '@RG\\tID:${params.org}\\tSM:${id}\\tPL:ILLUMINA' -a \
-t $task.cpus ref ${forward} ${reverse} > ${id}.sam
samtools view -h -b -@ 1 -q 1 -o ${id}.bam_tmp ${id}.sam
samtools sort -@ 1 -o ${id}.dedup.bam ${id}.bam_tmp
samtools index ${id}.dedup.bam
rm ${id}.sam ${id}.bam_tmp
mosdepth --by ${ref}.bed ${id}.output ${id}.dedup.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
}
}
process ReferenceAlignment {
conda "$baseDir/env_spandx.yaml"
label "spandx_alignment"
tag { "$id" }
input:
file ref_indices
tuple val(id), file(forward), file(reverse)
file reference
file bed_files
output:
tuple val(id), path("${id}.bam"), path("${id}.bam.bai"), emit:bams
tuple val(id), path("${id}.depth.txt"), emit:depth_files
script:
if(params.fast) {
"""
strobealign --rg-id ID:${id} --rg SM:${id} --rg PL:ILLUMINA ${ref} ${forward} ${reverse} > ${id}.sam
samtools view -h -b -@ 1 -q 1 -o ${id}.bam_tmp ${id}.sam
samtools sort -@ 1 -o ${id}.bam ${id}.bam_tmp
samtools index ${id}.bam
rm ${id}.sam ${id}.bam_tmp
mosdepth --by ${ref}.bed ${id}.output ${id}.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
} else {
"""
bwa mem -R '@RG\\tID:${params.org}\\tSM:${id}\\tPL:ILLUMINA' -a \
-t $task.cpus ref ${forward} ${reverse} > ${id}.sam
samtools view -h -b -@ 1 -q 1 -o ${id}.bam_tmp ${id}.sam
samtools sort -@ 1 -o ${id}.bam ${id}.bam_tmp
samtools index ${id}.bam
rm ${id}.sam ${id}.bam_tmp
mosdepth --by ${ref}.bed ${id}.output ${id}.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
}
}
/*
=======================================================================
Part 2: read processing, reference alignment and variant identification
=======================================================================
// Variant calling sub-workflow - basically SPANDx with a tonne of updates
// Careful here, not sure if the output overwrites the symlinks
// created by Nextflow (if input is .fq.gz) and would do weird stuff?
=======================================================================
Part 2A: Trim reads with light quality filter and remove adapters
=======================================================================
*/
process Trimmomatic_SE {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
input:
tuple val(id), path(forward)
output:
tuple val(id), path("${id}_1.fq.gz")
script:
if (params.notrim) {
"""
mv $forward ${id}_1.fq.gz
"""
} else {
"""
trimmomatic SE -threads 1 $forward ${id}_1.fq.gz \
ILLUMINACLIP:${baseDir}/resources/all_adapters.fa:2:30:10: \
LEADING:10 TRAILING:10 SLIDINGWINDOW:4:15 MINLEN:36
"""
}
}
/*
=======================================================================
Part 2B: Downsample reads to increase speed
=======================================================================
*/
process Downsample_SE {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
input:
tuple val(id), path(forward)
output:
tuple val(id), path("${id}_1_cov.fq.gz")
script:
if (params.size > 0) {
"""
seqtk sample -s 11 $forward $params.size | gzip - > ${id}_1_cov.fq.gz
"""
} else {
"""
mv $forward ${id}_1_cov.fq.gz
"""
}
}
/*
=======================================================================
Part 2C: Align reads against the reference
=======================================================================
*/
process SE_reference_alignment {
conda "$baseDir/env_spandx.yaml"
label "spandx_alignment"
tag { "$id" }
input:
path ref_indices
tuple val(id), path(forward)
path reference
path bed_files
output:
tuple val(id), path("${id}.bam"), path("${id}.bam.bai")
tuple val(id), path("${id}.depth.txt")
script:
"""
bwa mem -R '@RG\\tID:${params.org}\\tSM:${id}\\tPL:ILLUMINA' -a \
-t $task.cpus ref ${forward} > ${id}.sam
samtools view -h -b -@ 1 -q 1 -o ${id}.bam_tmp ${id}.sam
samtools sort -@ 1 -o ${id}.bam ${id}.bam_tmp
samtools index ${id}.bam
rm ${id}.sam ${id}.bam_tmp
mosdepth --by ${ref}.bed ${id}.output ${id}.bam
sum_depth=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | awk '{s+=\$1}END{print s}')
total_chromosomes=\$(zcat ${id}.output.regions.bed.gz | awk '{print \$4}' | wc -l)
echo "\$sum_depth \$total_chromosomes" | awk '{print int(\$1/\$2)}' > "${id}.depth.txt"
"""
}
/*
=======================================================================
Part 2D: De-duplicate bams
=======================================================================
*/
process Deduplicate {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
publishDir "./Outputs/bams", mode: 'copy', pattern: "*.dedup*", overwrite: true
if (params.unaligned) {
publishDir "./Outputs/bams", mode: 'copy', pattern: "*.unmapped.bam", overwrite: true
publishDir "./Outputs/unmapped_reads", mode: 'copy', pattern: "*fastq.gz", overwrite: true
}
input:
tuple val(id), path(bam_file), path(bai_file)
output:
tuple val(id), path("${id}.dedup.bam"), path("${id}.dedup.bam.bai"), emit: dedup_bams
if (params.unaligned) {
val(id), file("${id}_unmapped_1_sequence.fastq.gz"), file("${id}_unmapped_2_sequence.fastq.gz")
}
script:
if (params.unaligned) {
"""
gatk --java-options -Xmx${task.memory.toString().replaceAll(/[\sB]/,'')} MarkDuplicates -I "${id}.bam" -O ${id}.dedup.bam --REMOVE_DUPLICATES true \
--METRICS_FILE ${id}.dedup.txt --VALIDATION_STRINGENCY LENIENT --ASSUME_SORT_ORDER coordinate
samtools index ${id}.dedup.bam
samtools view -b -F 2 ${id}.dedup.bam | samtools sort -n - -o ${id}.unmapped.bam
bamToFastq -i ${id}.unmapped.bam -fq ${id}_unmapped_1_sequence.fastq -fq2 ${id}_unmapped_2_sequence.fastq
gzip ${id}_unmapped_1_sequence.fastq
gzip ${id}_unmapped_2_sequence.fastq
"""
} else {
"""
gatk --java-options -Xmx${task.memory.toString().replaceAll(/[\sB]/,'')} MarkDuplicates -I "${id}.bam" -O ${id}.dedup.bam --REMOVE_DUPLICATES true \
--METRICS_FILE ${id}.dedup.txt --VALIDATION_STRINGENCY LENIENT --ASSUME_SORT_ORDER coordinate
samtools index ${id}.dedup.bam
"""
}
}
/*
=======================================================================
Part 2E: Calculate coverage stats
=======================================================================
*/
process ReferenceCoverage {
conda "$baseDir/env_spandx.yaml"
label "spandx_default"
tag { "$id" }
input:
file ref_indices
tuple val(id), path(dedup_bam), path(dedup_bam_bai)
output:
tuple val(id), path("*.bedcov"), emit: bedcov_files
script:
"""
bedtools coverage -sorted -a ${ref_indices[0]} -b ${dedup_bam} > ${id}.bedcov
"""
}
process Merge_bedcov {
conda "$baseDir/env_spandx.yaml"
label "bedcov"
// tag { "$id" }
publishDir "./Outputs/Coverage", mode: 'copy', overwrite: true
input:
file bedcov_files
output:
path "Bedcov_merge.txt"
"""
bash ${baseDir}/bin/Bedcov_merge.sh
"""
}
/*
=======================================================================
Part 2F: Variant identification
=======================================================================
*/
process VariantCallingMixture {
conda "$baseDir/env_spandx.yaml"
label "spandx_gatk"
tag { "$id" }
publishDir "./Outputs/Variants/GVCFs", mode: 'copy', overwrite: true, pattern: '*.gvcf'
input:
file reference
file fai_files
file dict_files
tuple val(id), path(bam_file), path(bai_file)
output:
tuple val(id), path("${id}.raw.snps.indels.mixed.gvcf"), emit: gvcf_files
tuple val(id), path("${id}.raw.snps.indels.mixed.vcf"), path("${id}.raw.snps.indels.mixed.vcf.idx"), emit: vcf_files
script:
if (params.freebayes) {
"""
freebayes --gvcf -f ${reference} ${id}.dedup.bam > ${id}.raw.snps.indels.mixed.gvcf
bcftools view ${id}.raw.snps.indels.mixed.gvcf | awk '{gsub(/<\\*>/, "<NON_REF>"); print}' > converted_output.g.vcf
mv converted_output.g.vcf ${id}.raw.snps.indels.mixed.gvcf
gatk SelectVariants -V ${id}.raw.snps.indels.mixed.gvcf -O ${id}.raw.snps.indels.mixed.vcf --exclude-non-variants
"""
} else {
"""
gatk HaplotypeCaller -ERC GVCF -R ${reference} --I ${id}.dedup.bam -O ${id}.raw.snps.indels.mixed.gvcf
gatk SelectVariants -V ${id}.raw.snps.indels.mixed.gvcf -O ${id}.raw.snps.indels.mixed.vcf --exclude-non-variants
"""
}
}
process VariantCallingMixture_Clair3 {
conda "$baseDir/env_clair3.yaml"
label "spandx_clair3"
tag { "$id" }
publishDir "./Outputs/Variants/GVCFs", mode: 'copy', overwrite: true, pattern: '*.gvcf'
publishDir "./Outputs/Variants/VCFs", mode: 'copy', overwrite: true, pattern: '*.vcf'
input:
file reference
file fai_files
file dict_files
tuple val(id), path(bam_file), path(bai_file)
output:
tuple val(id), path("${id}.raw.snps.indels.mixed.gvcf"), emit: gvcf_files
tuple val(id), path("${id}.raw.snps.indels.mixed.vcf"), path("${id}.raw.snps.indels.mixed.vcf.idx"), emit: vcf_files
tuple val(id), path("${id}.raw.snps.vcf"), path("${id}.raw.snps.vcf.idx"), emit: snps_output
tuple val(id), path("${id}.raw.indels.vcf"), path("${id}.raw.indels.vcf.idx"), emit: indels_output
script:
"""
run_clair3.sh --bam_fn ${id}.dedup.bam --ref_fn ${reference} --threads $task.cpus \
--model_path "${baseDir}"/resources/clair3_models/r941_prom_sup_g5014 --output ./ -p ont --fast_mode \
--gvcf --enable_long_indel --sample_name="${id}" --include_all_ctgs --fast_mode
gunzip *.gz
mv merge_output.vcf "${id}.raw.snps.indels.mixed.vcf"
mv merge_output.gvcf "${id}.raw.snps.indels.mixed.gvcf"
gatk IndexFeatureFile -I "${id}.raw.snps.indels.mixed.vcf"
gatk IndexFeatureFile -I "${id}.raw.snps.indels.mixed.gvcf"
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.mixed.vcf -O ${id}.raw.snps.vcf -select-type SNP
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.mixed.vcf -O ${id}.raw.indels.vcf -select-type INDEL
"""
}
process VariantCalling_Clair3 {
conda "$baseDir/env_clair3.yaml"
label "spandx_clair3"
tag { "$id" }
publishDir "./Outputs/Variants/GVCFs", mode: 'copy', overwrite: true, pattern: '*.gvcf'
publishDir "./Outputs/Variants/VCFs", mode: 'copy', overwrite: true, pattern: '*.vcf'
input:
file reference
file fai_files
file dict_files
tuple val(id), path(bam_file), path(bai_file)
output:
tuple val(id), path("${id}.raw.snps.indels.mixed.gvcf"), emit: gvcf_files
tuple val(id), path("${id}.raw.snps.indels.mixed.vcf"), path("${id}.raw.snps.indels.mixed.vcf.idx"), emit: vcf_files
tuple val(id), path("${id}.raw.snps.vcf"), path("${id}.raw.snps.vcf.idx"), emit: snps_output
tuple val(id), path("${id}.raw.indels.vcf"), path("${id}.raw.indels.vcf.idx"), emit: indels_output
script:
"""
run_clair3.sh --bam_fn ${id}.dedup.bam --ref_fn ${reference} --threads $task.cpus \
--model_path "${baseDir}"/resources/clair3_models/r941_prom_sup_g5014 --output ./ -p ont --fast_mode \
--gvcf --enable_long_indel --sample_name="${id}" --include_all_ctgs --fast_mode
gunzip *.gz
mv merge_output.vcf "${id}.raw.snps.indels.mixed.vcf"
mv merge_output.gvcf "${id}.raw.snps.indels.mixed.gvcf"
gatk IndexFeatureFile -I "${id}.raw.snps.indels.mixed.vcf"
gatk IndexFeatureFile -I "${id}.raw.snps.indels.mixed.gvcf"
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.mixed.vcf -O ${id}.raw.snps.vcf -select-type SNP
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.mixed.vcf -O ${id}.raw.indels.vcf -select-type INDEL
"""
}
process VariantFilterMixture {
conda "$baseDir/env_spandx.yaml"
label "spandx_gatk"
tag { "$id" }
publishDir "./Outputs/Variants/VCFs", mode: 'copy', overwrite: true
input:
file reference
file reference_fai
file reference_dict
tuple val(id), file(variants), file(variants_index)
output:
tuple val(id), path("${id}.PASS.snps.indels.mixed.vcf"), path("${id}.FAIL.snps.indels.mixed.vcf")
script:
"""
gatk VariantFiltration -R ${reference} -O ${id}.snps.indels.filtered.mixed.vcf -V $variants \
-filter "MQ < $params.MQ_SNP" --filter-name "MQFilter" \
-filter "FS > $params.FS_SNP" --filter-name "FSFilter" \
-filter "QUAL < $params.QUAL_SNP" --filter-name "StandardFilters"
header=`grep -n "#CHROM" ${id}.snps.indels.filtered.mixed.vcf | cut -d':' -f 1`
head -n "\$header" ${id}.snps.indels.filtered.mixed.vcf > snp_head
cat ${id}.snps.indels.filtered.mixed.vcf | awk -F'\t' '\$7 == "PASS" {print}' | cat snp_head - > ${id}.PASS.snps.indels.mixed.vcf
cat ${id}.snps.indels.filtered.mixed.vcf | awk -F'\t' '\$7 != "PASS" {print}' | cat snp_head - > ${id}.FAIL.snps.indels.mixed.vcf
"""
}
process AnnotateMixture {
conda "$baseDir/env_spandx.yaml"
label "spandx_snpeff"
tag { "$id" }
publishDir "./Outputs/Variants/Annotated", mode: 'copy', overwrite: true
input:
tuple val(id), path("${id}.PASS.snps.indels.mixed.vcf"), path("${id}.FAIL.snps.indels.mixed.vcf")
output:
tuple val(id), path("${id}.ALL.annotated.mixture.vcf")
//Check to see if there is a databae in the default location then run
script:
"""
snpEff eff -dataDir ${params.snpeff_cache} -c ${params.snpeff_config} -nodownload -no-downstream -no-intergenic -ud 100 -v ${snpeff_database} ${id}.PASS.snps.indels.mixed.vcf > ${id}.ALL.annotated.mixture.vcf
"""
}
// TO DO - needs to be updated with Delly
process PindelProcessing {
conda "$baseDir/env_spandx.yaml"
label "spandx_pindel"
tag { "$id" }
input:
path reference
path reference_fai
tuple val(id), path("${id}.dedup.bam"), path(alignment_index)
output:
path("pindel.out_D.vcf")
path("pindel.out_TD.vcf")
script:
"""
echo -e "${id}.dedup.bam\t250\tB" > pindel.bam.config
pindel -f ${reference} -T $task.cpus -i pindel.bam.config -o pindel.out
rm -f pindel.out_CloseEndMapped pindel.out_INT_final
for f in pindel.out_*; do
pindel2vcf -r ${reference} -R ${reference.baseName} -d ARDaP -p \$f -v \${f}.vcf -e 5 -is 15 -as 50000
if [ "${params.annotate}" == "true" ]; then
snpEff eff -dataDir ${params.snpeff_cache} -no-downstream -no-intergenic -ud 100 -v ${params.snpeff_database} \${f}.vcf > \${f}.vcf.annotated
fi
done
"""
}
//Not a mixture
process VariantCalling {
conda "$baseDir/env_spandx.yaml"
label "spandx_gatk"
tag { "$id" }
publishDir "./Outputs/Variants/GVCFs", mode: 'copy', overwrite: false, pattern: '*.gvcf'
input:
file reference
file fai_files
file dict_files
tuple val(id), path(dedup_bam), path(bai_file)
output:
tuple val(id), path("${id}.raw.snps.vcf"), path("${id}.raw.snps.vcf.idx"), emit: snps_output
tuple val(id), path("${id}.raw.indels.vcf"), path("${id}.raw.indels.vcf.idx"), emit: indels_output
tuple val(id), path("${id}.raw.snps.indels.gvcf"), emit: gvcf_files
script:
if (params.freebayes) {
"""
freebayes --gvcf -f ${reference} ${id}.dedup.bam > ${id}.raw.snps.indels.gvcf
bcftools view ${id}.raw.snps.indels.mixed.gvcf | awk '{gsub(/<\\*>/, "<NON_REF>"); print}' > converted_output.g.vcf
mv converted_output.g.vcf ${id}.raw.snps.indels.mixed.gvcf
gatk SelectVariants -V ${id}.raw.snps.indels.gvcf -O ${id}.raw.snps.indels.vcf --exclude-non-variants
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.vcf -O ${id}.raw.snps.vcf -select-type SNP
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.vcf -O ${id}.raw.indels.vcf -select-type INDEL
"""
} else {
"""
gatk HaplotypeCaller -R ${reference} -ERC GVCF --ploidy 1 --I ${dedup_bam} -O ${id}.raw.snps.indels.gvcf
## gatk SelectVariants -V ${id}.raw.snps.indels.gvcf -O ${id}.raw.snps.indels.vcf --exclude-non-variants
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.gvcf -O ${id}.raw.snps.vcf -select-type SNP --ignore-non-ref-in-types
gatk SelectVariants -R ${reference} -V ${id}.raw.snps.indels.gvcf -O ${id}.raw.indels.vcf -select-type INDEL --ignore-non-ref-in-types
"""
}
}