-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.nf
1763 lines (1483 loc) · 94.5 KB
/
main.nf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
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
/*
========================================================================================
Human Respiratory Virus Pipeline v1.4
========================================================================================
Github Repo:
Greninger Lab
Author:
Paul RK Cruz <kurtisc@uw.edu>
Michelle J Lin <Mjlin@uw.edu>
Alex L Greninger <agrening@uw.edu>
UW Medicine | Virology
Department of Laboratory Medicine and Pathology
University of Washington
Created: April, 2021
Updated: August 24, 2021
LICENSE: GNU
----------------------------------------------------------------------------------------
Human Respiratory Virus Pipeline was designed to run either single-end or paired end Illumina Next-Generation-Sequencing (NGS) sequence for Human respiratory virus discovery, analysis, and Genbank submission.
PIPELINE OVERVIEW:
- 1. : Trim Reads
-Trimmomatic - sequence read trimming of adaptors and low quality reads.
- 2. : Genome Mapping
-BBMap - align to MultiFasta Reference Virus Genome.
-Samtools - SAM and BAM file processing.
- 3. : Reference Fasta Selection
-Selects the closest reference genome.
- 4. : Sort Bam
-Convert Sam to Bam
-Sort Bam file by coordinates
-Generate Statistics about Bam file
- 5. : Variant Calling
-Calculate the read coverage of positions in the genome
-Detect the single nucleotide polymorphisms (SNPs)
-Filter and report the SNP variants in variant calling format (VCF)
CLI Command to view results: less -S ${base}_final_variants.vcf
- 6. : Consensus
-Consensus generation using variants VCF, mapped reference fasta, and
sorted bam.
- 7. : Final Consensus
-Creates the Final Consensus by editing the fasta header.
- 8. : Summary Report Generation
Generates a run report summary.
- 9. : FastQC
-Sequence read quality control analysis.
Dependencies:
HRV-Docker includes all dependencies. Currently (7/2021), Mapping step requires local dependencies. Please see docker for dependencies required for mapping and viral annotation steps.
Setup Multifasta References:
1. Multifasta references containing Viral genome sequences formatted with accession numbers only.
Current supported virus`s: Rhinovirus, Human Coronavirus, Influenza B, HPIV3, and HPV.
Setup File Paths:
1. BBMAP_PATH
Path to your installation of BBTools --> bbmap.sh
2. trimmomatic_adapters_file_SE
Path to your Trimmomatic single-end file
3. trimmomatic_adapters_file_PE
Path to your Trimmomatic paired-end file
Setup Trimmomatic Parameters:
1. params.trimmomatic_adapters_parameters = "2:30:10:1"
2. params.trimmomatic_window_length = "4"
3. params.trimmomatic_window_value = "20"
4. params.trimmomatic_mininum_length = "75"
EXAMPLE USAGE:
Single-end Illumina Reads
nextflow run ./HRV_Pipeline/main.nf --reads './HRV_Pipeline/example/' --outdir './HRV_Pipeline/example/output/' --withMetadata './HRV_Pipeline/example/example_metadata.csv' --withSerotype --singleEnd -resume
Paired-end Illumina Reads
nextflow run ./HRV_Pipeline/main.nf --reads './HRV_Pipeline/example/' --outdir './HRV_Pipeline/example/output/' --withMetadata './HRV_Pipeline/example/example_metadata.csv' --withSerotype -resume
----------------------------------------------------------------------------------------
*/
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* DISPLAY HELP MSG */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Pipeline version
version = '1.4'
params.helpMsg = false
def helpMsg() {
log.info"""
_______________________________________________________________________________
Human Respiratory Virus Pipeline : Version ${version}
________________________________________________________________________________
Pipeline Usage:
To run the pipeline, enter the following in the command line:
nextflow run FILE_PATH/HRV_Genome_Mapping_Pipeline/main.nf --reads PATH_TO_FASTQ --outdir PATH_TO_OUTPUT_DIR
Valid CLI Arguments:
REQUIRED:
--reads Path to input fastq.gz folder).
--outdir The output directory where the results will be saved
OPTIONAL:
--withMetadata Adds Metadata information to Final Run Report Summary
--withVapid Annotate the resulting consensus fasta for GenBank submission
--ref_rv Overwrite set multi-fasta Rhinovirus reference file
--ref_hcov Overwrite set multi-fasta Human Coronavirus reference file
--ref_hpv Overwrite set multi-fasta HPV reference file
--ref_inflb Overwrite set multi-fasta Influenza B reference file
--ref_hpiv3 Overwrite set multi-fasta HPIV3 reference file
--helpMsg Displays help message in terminal
--singleEnd Specifies that the input fastq files are single end reads
--withFastQC Runs a quality control check on fastq files
""".stripIndent()
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* CONFIGURATION VARIABLES */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Show help msg
if (params.helpMsg){
helpMsg()
exit 0
}
// Make sure outdir path ends with trailing slash
if (!params.outdir.endsWith("/")){
params.outdir = "${params.outdir}/"
}
// Make sure reads path ends with trailing slash
if (!params.reads.endsWith("/")){
params.reads = "${params.outdir}/"
}
params.virus_index = false
params.virus_fasta = false
params.withFastQC = false
params.skipTrim = false
params.reads = false
params.singleEnd = false
params.ADAPTERS = false
params.withMetadata = false
params.withSerotype = false
params.withVapid = false
// Trimmomatic Paths and variables
params.ADAPTERS_SE = file("${baseDir}/adapters/TruSeq2-SE.fa")
params.ADAPTERS_EE = file("${baseDir}/adapters/TruSeq2-PE.fa")
ADAPTERS_SE = file("${baseDir}/adapters/TruSeq2-SE.fa")
ADAPTERS_PE = file("${baseDir}/adapters/TruSeq2-PE.fa")
vapid_rhinovirus_sbt = file("${baseDir}/vapid/rhinovirus.sbt")
params.SETTING = "2:30:10:1:true"
SETTING = "2:30:10:1:true"
params.LEADING = "3"
LEADING = "3"
params.TRAILING = "3"
TRAILING = "3"
params.SWINDOW = "4:20"
SWINDOW = "4:20"
// Script Files
if(params.withMetadata != false) {
METADATA = file(params.withMetadata)
}
if(params.withVapid != false) {
vapid_python = file("${baseDir}/vapid/vapid.py")
vapid_python3 = file("${baseDir}/vapid/vapid3.py")
VAPID_DB_ALL_1 = file("${baseDir}/vapid/all_virus.fasta")
VAPID_DB_ALL_2 = file("${baseDir}/vapid/all_virus.fasta.ndb")
VAPID_DB_ALL_3 = file("${baseDir}/vapid/all_virus.fasta.nhr")
VAPID_DB_ALL_4 = file("${baseDir}/vapid/all_virus.fasta.nin")
VAPID_DB_ALL_5 = file("${baseDir}/vapid/all_virus.fasta.not")
VAPID_DB_ALL_6 = file("${baseDir}/vapid/all_virus.fasta.nsq")
VAPID_DB_ALL_7 = file("${baseDir}/vapid/all_virus.fasta.ntf")
VAPID_DB_ALL_8 = file("${baseDir}/vapid/all_virus.fasta.nto")
tbl2asn = file("${baseDir}/vapid/tbl2asn")
}
if(params.withSerotype != false) {
// Rhinovirus VP1 database files
BLAST_DB_VP1_1 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta")
BLAST_DB_VP1_2 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.ndb")
BLAST_DB_VP1_3 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.nhr")
BLAST_DB_VP1_4 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.nin")
BLAST_DB_VP1_5 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.not")
BLAST_DB_VP1_6 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.nsq")
BLAST_DB_VP1_7 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.ntf")
BLAST_DB_VP1_8 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta.nto")
// HPV database files
BLAST_DB_ALL_1hpv = file("${baseDir}/blast_db/hpv.fasta")
BLAST_DB_ALL_2hpv = file("${baseDir}/blast_db/hpv.fasta.ndb")
BLAST_DB_ALL_3hpv = file("${baseDir}/blast_db/hpv.fasta.nhr")
BLAST_DB_ALL_4hpv = file("${baseDir}/blast_db/hpv.fasta.nin")
BLAST_DB_ALL_5hpv = file("${baseDir}/blast_db/hpv.fasta.nog")
BLAST_DB_ALL_6hpv = file("${baseDir}/blast_db/hpv.fasta.nos")
BLAST_DB_ALL_7hpv = file("${baseDir}/blast_db/hpv.fasta.not")
BLAST_DB_ALL_8hpv = file("${baseDir}/blast_db/hpv.fasta.nsq")
BLAST_DB_ALL_9hpv = file("${baseDir}/blast_db/hpv.fasta.ntf")
BLAST_DB_ALL_10hpv = file("${baseDir}/blast_db/hpv.fasta.nto")
// All BLAST db files for respiratory viruses recognized by this pipeline
BLAST_DB_ALL_1 = file("${baseDir}/blast_db/all_ref.fasta")
BLAST_DB_ALL_2 = file("${baseDir}/blast_db/all_ref.fasta.ndb")
BLAST_DB_ALL_3 = file("${baseDir}/blast_db/all_ref.fasta.nhr")
BLAST_DB_ALL_4 = file("${baseDir}/blast_db/all_ref.fasta.nin")
BLAST_DB_ALL_5 = file("${baseDir}/blast_db/all_ref.fasta.nog")
BLAST_DB_ALL_6 = file("${baseDir}/blast_db/all_ref.fasta.nos")
BLAST_DB_ALL_7 = file("${baseDir}/blast_db/all_ref.fasta.not")
BLAST_DB_ALL_8 = file("${baseDir}/blast_db/all_ref.fasta.nsq")
BLAST_DB_ALL_9 = file("${baseDir}/blast_db/all_ref.fasta.ntf")
BLAST_DB_ALL_10 = file("${baseDir}/blast_db/all_ref.fasta.nto")
}
// File paths
BLAST_DB_VP1 = file("${baseDir}/blast_db/VP1_164_annotated_nospaces.fasta")
BLAST_DB_ALL = file("${baseDir}/blast_db/allref.fasta")
// BBMAP_PATH="/Users/greningerlab/Documents/bbmap/"
BBMAP_PATH="/Users/kurtiscruz/Documents/bbmap/"
params.MINLEN = "35"
MINLEN = "35"
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* VIRAL MULTI-FASTA REFERENCES */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Setup MULTIFASTA Reference parameters.
params.ref_rv = false
params.ref_hcov = false
params.ref_hpv = false
params.ref_inflb = false
params.ref_hpiv3 = false
// Setup MULTIFASTA Reference file paths for OPTIONAL-override of set file paths.
Reference_hpv_14 = file("${baseDir}/hrv_ref/hrv_ref_hpv_14.fa")
// Rhinovirus
if(params.ref_rv != false) {
Reference_rv = file(params.ref_rv)
} else {
Reference_rv=file("${baseDir}/hrv_ref/hrv_ref_rhinovirus.fa")
}
// Human Coronavirus
if(params.ref_hcov != false) {
Reference_hcov = file(params.ref_hcov)
} else {
Reference_hcov=file("${baseDir}/hrv_ref/hrv_ref_hcov.fa")
}
// HPV
if(params.ref_hpv != false) {
Reference_hpv = file(params.ref_hpv)
} else {
Reference_hpv=file("${baseDir}/hrv_ref/hrv_ref_hpv.fa")
}
// Influenza B
if(params.ref_inflb != false) {
Reference_inflb = file(params.ref_inflb)
} else {
Reference_inflb=file("${baseDir}/hrv_ref/hrv_ref_Influenza_B.fa")
}
// HPIV3 - Human Parainfluenza Virus 3
if(params.ref_hpiv3 != false) {
Reference_hpiv3 = file(params.ref_hpiv3)
} else {
Reference_hpiv3=file("${baseDir}/hrv_ref/hrv_ref_hpiv3.fa")
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* SET UP CHANNELS */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Check Nextflow version
nextflow_req_v = '20.10.0'
try {
if( ! nextflow.version.matches(">= $nextflow_req_v") ){
throw GroovyException("> ERROR: The version of Nextflow running on your machine is out dated.\n>Please update to Version $nextflow_req_v")
}
} catch (all) {
log.error"ERROR: This version of Nextflow is out of date.\nPlease update to the latest version of Nextflow."
}
if (! params.reads ) exit 1, "> Error: Fastq files not found. Please specify a valid path with --reads"
// Create channel for input reads.
// Import reads depending on single-end or paired-end
if(params.singleEnd == false) {
// Check for R1s and R2s in input directory
input_read_ch = Channel
.fromFilePairs("${params.reads}*_R{1,2}*.fastq.gz")
// .ifEmpty { error "> Cannot locate paired-end reads in: ${params.reads}.\n> Please enter a valid file path." }
.map { it -> [it[0], it[1][0], it[1][1]]}
} else {
// input: *.gz
input_read_ch = Channel
.fromPath("${params.reads}*.gz")
.ifEmpty { error "> Cannot locate single-end reads in: ${params.reads}.\n> Please enter a valid file path." }
.map { it -> file(it)}
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* WORKFLOW DISPLAY HEADER */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
def hrvheader() {
return """
""".stripIndent()
}
// log files header
// log.info hrvheader()
log.info "_______________________________________________________________________________"
log.info " Human Respiratory Virus Pipeline : v${version}"
log.info "_______________________________________________________________________________"
def summary = [:]
summary['Configuration Profile:'] = workflow.profile
summary['Current directory path:'] = "$PWD"
summary['HRV Pipeline directory path:'] = workflow.projectDir
summary['Input directory path:'] = params.reads
summary['Output directory path:'] = params.outdir
summary['Work directory path:'] = workflow.workDir
summary['Metadata:'] = params.withMetadata ? params.withMetadata : 'False'
summary['Serotyping:'] = params.withSerotype ? 'True' : 'False'
summary['Viral Annotation:'] = params.withVapid ? 'True' : 'False'
summary['Sequence type:'] = params.singleEnd ? 'Single-End' : 'Paired-End'
if(workflow.revision) summary['Pipeline Release'] = workflow.revision
if (params.singleEnd) {
summary['Trimmomatic adapters:'] = params.ADAPTERS_SE
} else {
summary['Trimmomatic adapters:'] = params.ADAPTERS_PE
}
summary["Trimmomatic read length (minimum):"] = params.MINLEN
summary["Trimmomatic Setting:"] = params.SETTING
summary["Trimmomatic Sliding Window:"] = params.SWINDOW
summary["Trimmomatic Leading:"] = params.LEADING
summary["Trimmomatic Trailing:"] = params.TRAILING
log.info summary.collect { k,v -> "${k.padRight(21)}: $v" }.join("\n")
log.info "_______________________________________________________________________________"
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/* */
/* WORKFLOW PROCESSES */
/* */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/*
* STEP 1: Trimming
* Trimming of low quality and short NGS sequences.
*/
if (params.singleEnd) {
process Trimming {
container "docker.io/paulrkcruz/hrv-pipeline:latest"
errorStrategy 'retry'
maxRetries 3
input:
file R1 from input_read_ch
file ADAPTERS_SE
val MINLEN
output:
tuple env(base),file("*.trimmed.fastq.gz"), file("${R1}_num_trimmed.txt"),file("*summary.csv") into Trim_out_SE, Trim_out_SE_FQC
publishDir "${params.outdir}trimmed_fastqs", mode: 'copy',pattern:'*.trimmed.fastq*'
script:
"""
#!/bin/bash
base=`basename ${R1} ".fastq.gz"`
echo \$base
/usr/local/miniconda/bin/trimmomatic SE -threads ${task.cpus} ${R1} \$base.trimmed.fastq.gz \
ILLUMINACLIP:${ADAPTERS_SE}:${SETTING} LEADING:${LEADING} TRAILING:${TRAILING} SLIDINGWINDOW:${SWINDOW} MINLEN:${MINLEN}
num_untrimmed=\$((\$(gunzip -c ${R1} | wc -l)/4))
num_trimmed=\$((\$(gunzip -c \$base'.trimmed.fastq.gz' | wc -l)/4))
printf "\$num_trimmed" >> ${R1}_num_trimmed.txt
percent_trimmed=\$((100-\$((100*num_trimmed/num_untrimmed))))
echo Sample_Name,Raw_Reads,Trimmed_Reads,Percent_Trimmed,Reference_Genome,Reference_Length,Mapped_Reads,Percent_Ref_Coverage,Min_Coverage,Mean_Coverage,Max_Coverage,Bam_Size,Consensus_Length,Percent_N,%_Reads_On_Target, PCR_CT,Method, NCBI_Name, Serotype, Nomenclature, Reference_Name, Reference_Genome, Biosample_name, Biosample_accession, SRA_Accession, Release_date, Bioproject> \$base'_summary.csv'
printf "\$base,\$num_untrimmed,\$num_trimmed,\$percent_trimmed" >> \$base'_summary.csv'
ls -latr
"""
}
}
/*
* STEP 2: Aligning
* Viral identification & mapping.
*/
if (params.singleEnd) {
process Aligning {
// container "docker.io/paulrkcruz/hrv-pipeline:latest"
// errorStrategy 'retry'
// maxRetries 3
// echo true
input:
tuple val(base), file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_summary.csv") from Trim_out_SE
file Reference_rv
file Reference_hcov
file Reference_hpv
file Reference_hpv_14
file Reference_inflb
file Reference_hpiv3
output:
tuple val(base), file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"), file("${base}_summary2.csv"), file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),env(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),env(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"),file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt") into Everything_ch
tuple val(base), file("${base}_map1_histogram.txt"),file("${base}_map2_histogram.txt") into BBmap_map1_hist_ch
tuple val (base), file("*") into Dump_map1_ch
publishDir "${params.outdir}all_ref", mode: 'copy', pattern:'*all_ref.sam*'
publishDir "${params.outdir}all_ref", mode: 'copy', pattern:'*all_ref*'
publishDir "${params.outdir}sam_map2", mode: 'copy', pattern:'*_map2.sam*'
publishDir "${params.outdir}txt_bbmap_map1_stats", mode: 'copy', pattern:'*_map1_bbmap_out.txt*'
publishDir "${params.outdir}txt_bbmap_map1_hist", mode: 'copy', pattern:'*_map2_histogram.txt*'
publishDir "${params.outdir}txt_bbmap_map2_stats", mode: 'copy', pattern:'*_map2_bbmap_out.txt*'
publishDir "${params.outdir}txt_bbmap_map2_hist", mode: 'copy', pattern:'*_map2_histogram.txt*'
publishDir "${params.outdir}txt_indxstats_mapped_refs", mode: 'copy', pattern:'*_idxstats.txt*'
publishDir "${params.outdir}ref_id", mode: 'copy', pattern:'*_most_mapped_ref.txt*'
publishDir "${params.outdir}ref_fasta", mode: 'copy', pattern:'*_mapped_ref_genome.fa*'
publishDir "${params.outdir}ref_size", mode: 'copy', pattern:'*_most_mapped_ref_size.txt*'
publishDir "${params.outdir}ref_fai_index", mode: 'copy', pattern:'*_mapped_ref_genome.fa.fai*'
publishDir "${params.outdir}ref_ids", mode: 'copy', pattern:'*_all_ref_id.txt*'
publishDir "${params.outdir}ref_ids", mode: 'copy', pattern:'*_ids.txt*'
script:
"""
#!/bin/bash
cat ${Reference_hpv} ${Reference_rv} ${Reference_inflb} ${Reference_hcov} ${Reference_hpiv3} > ${base}_all_ref.fa
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_all_ref.sam ref=${base}_all_ref.fa threads=${task.cpus} covstats=${base}_all_ref_bbmap_out.txt covhist=${base}_all_ref_histogram.txt local=true interleaved=false -Xmx10g > ${base}_all_ref_stats.txt 2>&1
samtools view -S -b ${base}_all_ref.sam > ${base}_all_ref.bam
samtools sort -@ 4 ${base}_all_ref.bam > ${base}_all_ref.sorted.bam
samtools index ${base}_all_ref.sorted.bam
samtools idxstats ${base}_all_ref.sorted.bam > ${base}_all_ref_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_all_ref_bbmap_out.txt > ${base}_all_ref_id.txt
all_ref_id=\$(awk '{print \$1}' ${base}_all_ref_id.txt)
grep -B 0 ">" ${Reference_rv} | tr -d ">" > ${base}_rv_ids.txt
grep -B 0 ">" ${Reference_hpv} | tr -d ">" > ${base}_hpv_ids.txt
grep -B 0 ">" ${Reference_inflb} | tr -d ">" > ${base}_inbflb_ids.txt
grep -B 0 ">" ${Reference_hcov} | tr -d ">" > ${base}_hcov_ids.txt
grep -B 0 ">" ${Reference_hpiv3} | tr -d ">" > ${base}_hpiv3.txt
# Rhinovirus`s
if grep -q \$all_ref_id "${base}_rv_ids.txt";
then
echo "< Accession found in Rhinovirus multifasta file. hrv_ref_rhinovirus.fa will be used for mapping."
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${Reference_rv} threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${Reference_rv} \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
# HPV
elif grep -q \$all_ref_id "${base}_hpv_ids.txt";
then
echo "< Accession found in HPV multifasta file. hrv_ref_hpv.fa will be used for mapping."
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${Reference_hpv} threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${Reference_hpv} \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
# Influenza B
elif grep -q \$all_ref_id "${base}_inbflb_ids.txt";
then
echo "< Accession found in Influenza B multifasta file. hrv_ref_Influenza_b.fa will be used for mapping."
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${Reference_inflb} threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${Reference_inflb} \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
# Human Coronavirus
elif grep -q \$all_ref_id "${base}_hcov_ids.txt";
then
echo "Accession found in HCoVs multifasta file. hrv_ref_hcov.fa will be used for mapping."
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${Reference_hcov} threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=20 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${Reference_hcov} \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=20 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
# HPIV3 - Human parainfluenza virus 3
elif grep -q \$all_ref_id "${base}_hpiv3.txt";
then
echo "Accession found in HPIV3 multifasta file. hrv_ref_hpiv3.fa will be used for mapping."
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${Reference_hpiv3} threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${Reference_hpiv3} \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=9 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
else
# Not Rhinovirus, HPV, Inlfuenza B, OR Human Coronavirus - Use Regular Mapping Settings
# MAP 1
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map1.sam ref=${base}_all_ref.fa threads=${task.cpus} covstats=${base}_map1_bbmap_out.txt covhist=${base}_map1_histogram.txt local=true interleaved=false maxindel=20 strictmaxindel -Xmx6g > ${base}_map1_stats.txt 2>&1
samtools view -S -b ${base}_map1.sam > ${base}_map1.bam
samtools sort -@ 4 ${base}_map1.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools idxstats ${base}.sorted.bam > ${base}_idxstats.txt
awk 'NR == 2 || \$5 > max {number = \$1; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref.txt
id=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref.txt)
ref_coverage=\$(awk 'FNR==1{print val,\$2}' ${base}_most_mapped_ref.txt)
samtools faidx ${base}_all_ref.fa \$id > ${base}_mapped_ref_genome.fa
# MAP 2
${BBMAP_PATH}bbmap.sh in=${base}.trimmed.fastq.gz outm=${base}_map2.sam ref=${base}_mapped_ref_genome.fa threads=${task.cpus} covstats=${base}_map2_bbmap_out.txt covhist=${base}_map2_histogram.txt local=true interleaved=false maxindel=20 strictmaxindel -Xmx6g > ${base}_map2_stats.txt 2>&1
head -n 1 ${base}_mapped_ref_genome.fa > ${base}_mapped_ref_genome_edited.fa
grep -v ">" ${base}_mapped_ref_genome.fa | sed 's/U/T/g' >> ${base}_mapped_ref_genome_edited.fa
mv ${base}_mapped_ref_genome_edited.fa ${base}_mapped_ref_genome.fa
samtools faidx ${base}_mapped_ref_genome.fa
awk 'NR == 2 || \$5 > max {number = \$3; max = \$5} END {if (NR) print number, max}' < ${base}_map1_bbmap_out.txt > ${base}_most_mapped_ref_size_out.txt
id_ref_size=\$(awk 'FNR==1{print val,\$1}' ${base}_most_mapped_ref_size_out.txt)
echo \$id_ref_size >> ${base}_most_mapped_ref_size.txt
reads_mapped=\$(cat ${base}_map2_stats.txt | grep "mapped:" | cut -d\$'\\t' -f3)
printf "\$reads_mapped" >> ${base}_num_mapped.txt
cp ${base}_summary.csv ${base}_summary2.csv
printf ",\$id" >> ${base}_summary2.csv
printf ",\$id_ref_size" >> ${base}_summary2.csv
printf ",\$reads_mapped" >> ${base}_summary2.csv
printf ",\$ref_coverage" >> ${base}_summary2.csv
fi
"""
}
}
/*
* STEP 2: Sort_Bam
* Sorting, indexing, and collecting of summary statistics from BAM files.
*/
if (params.singleEnd) {
process Bam_Sorting {
// container "docker.io/paulrkcruz/hrv-pipeline:latest"
// errorStrategy 'retry'
errorStrategy 'ignore'
// maxRetries 3
input:
tuple val(base), file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"), file("${base}_summary2.csv"),file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),val(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),val(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"),file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt") from Everything_ch
output:
tuple val(base), file("${base}.bam") into Aligned_bam_ch, Bam_ch
tuple val(base), file("${base}.sorted.bam"),file("${base}_flagstats.txt"),env(bamsize),file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"),file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),val(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),val(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"), file("${base}_summary.csv"),file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt") into Consensus_ch
publishDir "${params.outdir}bam_map2", mode: 'copy', pattern:'*.sorted.bam*'
publishDir "${params.outdir}txt_bam_flagstats-map2", mode: 'copy', pattern:'*_flagstats.txt*'
script:
"""
#!/bin/bash
samtools view -S -b ${base}_map2.sam > ${base}.bam
samtools sort -@ ${task.cpus} ${base}.bam > ${base}.sorted.bam
samtools index ${base}.sorted.bam
samtools flagstat ${base}.sorted.bam > ${base}_flagstats.txt
bedtools genomecov -d -ibam ${base}.sorted.bam > ${base}_coverage.txt
awk 'NR == 3 || \$3 > max {number = \$3; max = \$1} END {if (NR) print number, max}' < ${base}_coverage.txt > ${base}_min_coverage.txt
awk 'NR == 2 || \$3 > min {number = \$1; min = \$3} END {if (NR) print number, min}' < ${base}_coverage.txt > ${base}_max_coverage.txt
meancoverage=\$(cat ${base}_coverage.txt | awk '{sum+=\$3} END { print sum/NR}')
mincoverage=\$(awk 'FNR==1{print val,\$1}' ${base}_min_coverage.txt)
maxcoverage=\$(awk 'FNR==1{print val,\$2}' ${base}_max_coverage.txt)
bamsize=\$((\$(wc -c ${base}.sorted.bam | awk '{print \$1'})+0))
cp ${base}_summary2.csv ${base}_summary3.csv
printf ",\$mincoverage" >> ${base}_summary3.csv
printf ",\$meancoverage" >> ${base}_summary3.csv
printf ",\$maxcoverage" >> ${base}_summary3.csv
printf ",\$bamsize" >> ${base}_summary3.csv
cp ${base}_summary3.csv ${base}_summary.csv
"""
}
}
/*
* STEP 3: Consensus_Generation
* Consensus fasta generation.
*/
if (params.singleEnd) {
process Consensus_Generation {
// container "docker.io/paulrkcruz/hrv-pipeline:latest"
// errorStrategy 'retry'
// errorStrategy 'ignore'
// maxRetries 3
// echo true
input:
tuple val(base), file("${base}.sorted.bam"),file("${base}_flagstats.txt"),val(bamsize),file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"),file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),val(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),val(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"), file("${base}_summary.csv"),file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt") from Consensus_ch
output:
tuple val(base), file("${base}.sorted.bam"),file("${base}_flagstats.txt"),val(bamsize),file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"),file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),val(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),val(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"), file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt"), file("${base}_final_summary.csv"),file("${base}.consensus_final.fa") into Consensus_Fasta_ch
tuple val(base), file("${base}.mpileup") into Consensus_mpileup_Ch
publishDir "${params.outdir}consensus-final", mode: 'copy', pattern:'*.consensus_final.fa*'
publishDir "${params.outdir}consensus_mpileup", mode: 'copy', pattern:'*.mpileup*'
script:
"""
#!/bin/bash
all_ref_id=\$(awk '{print \$1}' ${base}_all_ref_id.txt)
# Rhinovirus
if grep -q \$all_ref_id "${base}_rv_ids.txt";
then
echo "< Accession found in Rhinovirus multifasta file. hrv_ref_rhinovirus.fa will be used for mapping."
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
# HPV
elif grep -q \$all_ref_id "${base}_hpv_ids.txt";
then
echo "< Accession found in HPV multifasta file. hrv_ref_hpv.fa will be used for mapping."
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
# Influenza B
elif grep -q \$all_ref_id "${base}_inbflb_ids.txt";
then
echo "< Accession found in Influenza B multifasta file. hrv_ref_Influenza_b.fa will be used for mapping."
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
# Human Coronavirus
elif grep -q \$all_ref_id "${base}_hcov_ids.txt";
then
echo "Accession found in HCoVs multifasta file. hrv_ref_hcov.fa will be used for mapping."
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
# HPIV3 - Human parainfluenza virus 3
elif grep -q \$all_ref_id "${base}_hpiv3.txt";
then
echo "Accession found in HPIV3 multifasta file. hrv_ref_hpiv3.fa will be used for mapping."
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
else
samtools mpileup \\
--count-orphans \\
--no-BAQ \\
--max-depth 50000 \\
--fasta-ref ${base}_mapped_ref_genome.fa \\
--min-BQ 15 \\
--output ${base}.mpileup \\
${base}.sorted.bam
cat ${base}.mpileup | ivar consensus -q 15 -t 0.6 -m 3 -n N -p ${base}.consensus_final
bedtools genomecov \\
-bga \\
-ibam ${base}.sorted.bam \\
-g ${base}_mapped_ref_genome.fa \\
| awk '\$4 < 10' | bedtools merge > ${base}.mask.bed
bedtools maskfasta \\
-fi ${base}.consensus_final.fa \\
-bed ${base}.mask.bed \\
-fo ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.masked.consensus/' ${base}.consensus.masked.fa
sed -i 's/>.*/>${base}.ivar.consensus/' ${base}.consensus_final.fa
awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length(\$0)}END{print l}' ${base}.consensus_final.fa > bases.txt
num_bases=\$(awk 'FNR==2{print val,\$1}' bases.txt)
seqkit -is replace -p "^n+|n+\$" -r "" ${base}.consensus_final.fa > ${base}.consensusfinal.fa
sed 's/>.*/>${base}/' ${base}.consensusfinal.fa > ${base}.consensusfinal-renamed-header.fa
grep -v "^>" ${base}.consensusfinal-renamed-header.fa | tr -cd N | wc -c > N.txt
cp ${base}.consensusfinal-renamed-header.fa ${base}.consensus_final.fa
num_ns=\$(awk 'FNR==1{print val,\$1}' N.txt)
echo "\$num_ns/\$num_bases*100" | bc -l > n_percent.txt
percent_n=\$(awk 'FNR==1{print val,\$1}' n_percent.txt)
printf ",\$num_bases" >> ${base}_summary.csv
printf ",\$percent_n" >> ${base}_summary.csv
cp ${base}_summary.csv ${base}_final_summary.csv
fi
"""
}
}
/*
* STEP 4: Aligning_Final
* Final round of mapping.
*/
if (params.singleEnd) {
process Aligning_Final {
// container "docker.io/paulrkcruz/hrv-pipeline:latest"
// errorStrategy 'retry'
errorStrategy 'ignore'
// maxRetries 3
// echo true
input:
tuple val(base), file("${base}.sorted.bam"),file("${base}_flagstats.txt"),val(bamsize),file("${base}_map2.sam"), file("${base}_most_mapped_ref.txt"),file("${base}_most_mapped_ref_size.txt"),file("${base}_most_mapped_ref_size_out.txt"),val(id_ref_size),file("${base}_idxstats.txt"),file("${base}_mapped_ref_genome.fa"),val(id),file("${base}_map1_bbmap_out.txt"),file("${base}_map2_bbmap_out.txt"),file("${base}_map1_stats.txt"),file("${base}_map2_stats.txt"),file("${base}_mapped_ref_genome.fa.fai"),file("${base}.trimmed.fastq.gz"), file("${base}_num_trimmed.txt"), file("${base}_num_mapped.txt"), file("${base}_rv_ids.txt"), file("${base}_hpv_ids.txt"), file("${base}_inbflb_ids.txt"), file("${base}_hcov_ids.txt"), file("${base}_hpiv3.txt"), file("${base}_all_ref_id.txt"), file("${base}_final_summary.csv"),file("${base}.consensus_final.fa") from Consensus_Fasta_ch
output: