-
Notifications
You must be signed in to change notification settings - Fork 5
/
DEN-IM.nf
1543 lines (1155 loc) · 58.4 KB
/
DEN-IM.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
import Helper
import CollectInitialMetadata
// Pipeline version
if (workflow.commitId){
version = "3.0 $workflow.revision"
} else {
version = "3.0 (local version)"
}
params.help = false
if (params.help){
Help.print_help(params)
exit 0
}
def infoMap = [:]
if (params.containsKey("fastq")){
infoMap.put("fastq", file(params.fastq).size())
}
if (params.containsKey("fasta")){
if (file(params.fasta) instanceof LinkedList){
infoMap.put("fasta", file(params.fasta).size())
} else {
infoMap.put("fasta", 1)
}
}
if (params.containsKey("accessions")){
// checks if params.accessions is different from null
if (params.accessions) {
BufferedReader reader = new BufferedReader(new FileReader(params.accessions));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
infoMap.put("accessions", lines)
}
}
Help.start_info(infoMap, "$workflow.start", "$workflow.profile", version)
CollectInitialMetadata.print_metadata(workflow)
// Placeholder for main input channels
if (params.fastq instanceof Boolean){exit 1, "'fastq' must be a path pattern. Provide value:'$params.fastq'"}
if (!params.fastq){ exit 1, "'fastq' parameter missing"}
IN_fastq_raw = Channel.fromFilePairs(params.fastq, size: -1).ifEmpty { exit 1, "No fastq files provided with pattern:'${params.fastq}'" }
// Placeholder for secondary input channels
// Placeholder for extra input channels
// Placeholder to fork the raw input channel
IN_fastq_raw.set{ integrity_coverage_in_1_0 }
IN_genome_size_1_1 = Channel.value(params.genomeSize)
.map{it -> it.toString().isNumber() ? it : exit(1, "The genomeSize parameter must be a number or a float. Provided value: '${params.genomeSize_}'")}
IN_min_coverage_1_1 = Channel.value(params.minCoverage)
.map{it -> it.toString().isNumber() ? it : exit(1, "The minCoverage parameter must be a number or a float. Provided value: '${params.minCoverage_}'")}
process integrity_coverage_1_1 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_1 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_1 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_1 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId integrity_coverage_1_1 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
// This process can only use a single CPU
cpus 1
input:
set sample_id, file(fastq_pair) from integrity_coverage_in_1_0
val gsize from IN_genome_size_1_1
val cov from IN_min_coverage_1_1
// This channel is for the custom options of the integrity_coverage.py
// script. See the script's documentation for more information.
val opts from Channel.value('')
output:
set sample_id,
file(fastq_pair),
file('*_encoding'),
file('*_phred'),
file('*_coverage'),
file('*_max_len') into MAIN_integrity_1_1
file('*_report') optional true into LOG_report_coverage1_1_1
set sample_id, val("1_1_integrity_coverage"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_integrity_coverage_1_1
set sample_id, val("integrity_coverage_1_1"), val("1_1"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_integrity_coverage_1_1
file ".versions"
script:
template "integrity_coverage.py"
}
// TRIAGE OF CORRUPTED SAMPLES
LOG_corrupted_1_1 = Channel.create()
MAIN_PreCoverageCheck_1_1 = Channel.create()
// Corrupted samples have the 2nd value with 'corrupt'
MAIN_integrity_1_1.choice(LOG_corrupted_1_1, MAIN_PreCoverageCheck_1_1) {
a -> a[2].text == "corrupt" ? 0 : 1
}
// TRIAGE OF LOW COVERAGE SAMPLES
integrity_coverage_out_1_0 = Channel.create()
SIDE_phred_1_1 = Channel.create()
SIDE_max_len_1_1 = Channel.create()
MAIN_PreCoverageCheck_1_1
// Low coverage samples have the 4th value of the Channel with 'fail'
.filter{ it[4].text != "fail" }
// For the channel to proceed with FastQ in 'sample_good' and the
// Phred scores for each sample in 'SIDE_phred'
.separate(integrity_coverage_out_1_0, SIDE_phred_1_1, SIDE_max_len_1_1){
a -> [ [a[0], a[1]], [a[0], a[3].text], [a[0], a[5].text] ]
}
/** REPORT_COVERAGE - PLUG-IN
This process will report the expected coverage for each non-corrupted sample
and write the results to 'reports/coverage/estimated_coverage_initial.csv'
*/
process report_coverage_1_1 {
// This process can only use a single CPU
cpus 1
publishDir 'reports/coverage_1_1/'
input:
file(report) from LOG_report_coverage1_1_1.filter{ it.text != "corrupt" }.collect()
output:
file 'estimated_coverage_initial.csv'
"""
echo Sample,Estimated coverage,Status >> estimated_coverage_initial.csv
cat $report >> estimated_coverage_initial.csv
"""
}
/** REPORT_CORRUPT - PLUG-IN
This process will report the corrupted samples and write the results to
'reports/corrupted/corrupted_samples.txt'
*/
process report_corrupt_1_1 {
// This process can only use a single CPU
cpus 1
publishDir 'reports/corrupted_1_1/'
input:
val sample_id from LOG_corrupted_1_1.collect{it[0]}
output:
file 'corrupted_samples.txt'
"""
echo ${sample_id.join(",")} | tr "," "\n" >> corrupted_samples.txt
"""
}
SIDE_phred_1_1.set{ SIDE_phred_1_2 }
// Check sliding window parameter
if ( params.trimSlidingWindow.toString().split(":").size() != 2 ){
exit 1, "'trimSlidingWindow' parameter must contain two values separated by a ':'. Provided value: '${params.trimSlidingWindow}'"
}
if ( !params.trimLeading.toString().isNumber() ){
exit 1, "'trimLeading' parameter must be a number. Provide value: '${params.trimLeading}'"
}
if ( !params.trimTrailing.toString().isNumber() ){
exit 1, "'trimTrailing' parameter must be a number. Provide value: '${params.trimTrailing}'"
}
if ( !params.trimMinLength.toString().isNumber() ){
exit 1, "'trimMinLength' parameter must be a number. Provide value: '${params.trimMinLength}'"
}
IN_trimmomatic_opts_1_2 = Channel.value([params.trimSlidingWindow,params.trimLeading,params.trimTrailing,params.trimMinLength])
IN_adapters_1_2 = Channel.value(params.adapters)
clear = params.clearInput ? "true" : "false"
checkpointClear_1_2 = Channel.value(clear)
process fastqc_1_2 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_2 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId fastqc_trimmomatic_1_2 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
publishDir "reports/fastqc_1_2/", pattern: "*.html"
input:
set sample_id, file(fastq_pair) from integrity_coverage_out_1_0
val ad from Channel.value('None')
output:
set sample_id, file(fastq_pair), file('pair_*') into MAIN_fastqc_out_1_2
file "*html"
set sample_id, val("1_2_fastqc"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_fastqc_1_2
set sample_id, val("fastqc_1_2"), val("1_2"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_fastqc_1_2
file ".versions"
script:
template "fastqc.py"
}
/** FASTQC_REPORT - MAIN
This process will parse the result files from a FastQC analyses and output
the optimal_trim information for Trimmomatic
*/
process fastqc_report_1_2 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_2 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId fastqc_trimmomatic_1_2 \"$params.platformSpecies\" false"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
// This process can only use a single CPU
cpus 1
publishDir 'reports/fastqc_1_2/run_1/', pattern: '*summary.txt', mode: 'copy'
input:
set sample_id, file(fastq_pair), file(results) from MAIN_fastqc_out_1_2
val opts from Channel.value("--ignore-tests")
output:
set sample_id, file(fastq_pair), 'optimal_trim', ".status" into _MAIN_fastqc_trim_1_2
file '*_trim_report' into LOG_trim_1_2
file "*_status_report" into LOG_fastqc_report_1_2
file "${sample_id}_*_summary.txt" optional true
set sample_id, val("1_2_fastqc_report"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_fastqc_report_1_2
set sample_id, val("fastqc_report_1_2"), val("1_2"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_fastqc_report_1_2
file ".versions"
script:
template "fastqc_report.py"
}
MAIN_fastqc_trim_1_2 = Channel.create()
_MAIN_fastqc_trim_1_2
.filter{ it[3].text == "pass" }
.map{ [it[0], it[1], file(it[2]).text] }
.into(MAIN_fastqc_trim_1_2)
/** TRIM_REPORT - PLUG-IN
This will collect the optimal trim points assessed by the fastqc_report
process and write the results of all samples in a single csv file
*/
process trim_report_1_2 {
publishDir 'reports/fastqc_1_2/', mode: 'copy'
input:
file trim from LOG_trim_1_2.collect()
output:
file "FastQC_trim_report.csv"
"""
echo Sample,Trim begin, Trim end >> FastQC_trim_report.csv
cat $trim >> FastQC_trim_report.csv
"""
}
process compile_fastqc_status_1_2 {
publishDir 'reports/fastqc_1_2/', mode: 'copy'
input:
file rep from LOG_fastqc_report_1_2.collect()
output:
file 'FastQC_1run_report.csv'
"""
echo Sample, Failed? >> FastQC_1run_report.csv
cat $rep >> FastQC_1run_report.csv
"""
}
/** TRIMMOMATIC - MAIN
This process will execute trimmomatic. Currently, the main channel requires
information on the trim_range and phred score.
*/
process trimmomatic_1_2 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_2 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_2 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId fastqc_trimmomatic_1_2 \"$params.platformSpecies\" false"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
publishDir "results/trimmomatic_1_2", pattern: "*.gz"
input:
set sample_id, file(fastq_pair), trim_range, phred from MAIN_fastqc_trim_1_2.join(SIDE_phred_1_2)
val opts from IN_trimmomatic_opts_1_2
val ad from IN_adapters_1_2
val clear from checkpointClear_1_2
output:
set sample_id, "*trim.fastq.gz" into fastqc_trimmomatic_out_1_1
file 'trimmomatic_report.csv'
set sample_id, val("1_2_trimmomatic"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_trimmomatic_1_2
set sample_id, val("trimmomatic_1_2"), val("1_2"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_trimmomatic_1_2
file ".versions"
script:
template "trimmomatic.py"
}
IN_adapter_1_3 = Channel.value(params.pattern)
clear = params.clearInput ? "true" : "false"
checkpointClear_1_3 = Channel.value(clear)
process filter_poly_1_3 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_3 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_3 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_3 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId filter_poly_1_3 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
echo true
errorStrategy { task.exitStatus == 120 ? 'ignore' : 'retry' }
input:
set sample_id, file(fastq_pair) from fastqc_trimmomatic_out_1_1
val adapter from IN_adapter_1_3
val clear from checkpointClear_1_3
output:
set sample_id , file("${sample_id}_filtered*") into filter_poly_out_1_2
set sample_id, val("1_3_filter_poly"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_filter_poly_1_3
set sample_id, val("filter_poly_1_3"), val("1_3"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_filter_poly_1_3
file ".versions"
script:
"""
a=(${fastq_pair})
if ((\${#a[@]} > 1));
then
gunzip -c ${fastq_pair[0]} > ${sample_id}_1.fq
gunzip -c ${fastq_pair[1]} > ${sample_id}_2.fq
else
gunzip -c ${fastq_pair[0]} > ${sample_id}.fq
fi
for seqfile in *.fq;
do if [ ! -s \$seqfile ]
then
echo \$seqfile is empty
echo 'No data left after polymorphic sequence filtering' > .fail
exit 120
fi
done
if ((\${#a[@]} > 1));
then
prinseq-lite.pl --fastq ${sample_id}_1.fq --fastq2 ${sample_id}_2.fq --custom_params "${adapter}" -out_format 3 -out_good ${sample_id}_filtered
else
prinseq-lite.pl --fastq ${sample_id}.fq --custom_params "${adapter}" -out_format 3 -out_good ${sample_id}_filtered
fi
if ls *_singletons* 1> /dev/null 2>&1; then
rm *_singletons*
fi
gzip ${sample_id}_filtered*
if [ "$clear" = "true" ];
then
work_regex=".*/work/.{2}/.{30}/.*"
file_source1=\$(readlink -f \$(pwd)/${fastq_pair[0]})
file_source2=\$(readlink -f \$(pwd)/${fastq_pair[1]})
if [[ "\$file_source1" =~ \$work_regex ]]; then
rm \$file_source1 \$file_source2
fi
fi
"""
}
// Check for the presence of absence of both index and fasta reference
if (params.index == null && params.reference == null){
exit 1, "An index or a reference fasta file must be provided."
} else if (params.index != null && params.reference != null){
exit 1, "Provide only an index OR a reference fasta file."
}
clear = params.clearInput ? "true" : "false"
checkpointClear_1_4 = Channel.value(clear)
if (params.reference){
reference_in_1_4 = Channel.fromPath(params.reference)
.map{it -> file(it).exists() ? [it.toString().tokenize('/').last().tokenize('.')[0..-2].join('.') ,it] : null}
process bowtie_build_1_4 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_4 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId bowtie_1_4 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { build_id }
storeDir 'bowtie_index/'
maxForks 1
input:
set build_id, file(fasta) from reference_in_1_4
output:
val build_id into bowtieIndexId_1_4
file "${build_id}*.bt2" into bowtieIndex_1_4
script:
"""
# checking if reference file is empty. Moved here due to allow reference file to be inside the container.
if [ ! -f "$fasta" ]
then
echo "Error: ${fasta} file not found."
exit 1
fi
bowtie2-build ${fasta} $build_id > ${build_id}_bowtie2_build.log
"""
}
} else {
bowtieIndexId_1_4 = Channel.value(params.index.split("/").last())
bowtieIndex_1_4 = Channel.fromPath("${params.index}*.bt2").collect().toList()
}
process bowtie_1_4 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_4 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId bowtie_1_4 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
publishDir 'results/mapping/bowtie_1_4/'
input:
set sample_id, file(fastq_pair) from filter_poly_out_1_2
each index from bowtieIndexId_1_4
each file(index_files) from bowtieIndex_1_4
output:
set sample_id , file("pair_info.txt"), file("*.bam") into bowtie_out_1_3
set sample_id, file("*_bowtie2.log") into into_json_1_4
set sample_id, val("1_4_bowtie"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_bowtie_1_4
set sample_id, val("bowtie_1_4"), val("1_4"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_bowtie_1_4
file ".versions"
script:
"""
{
a=(${fastq_pair})
if ((\${#a[@]} > 1));
then
echo "True" > pair_info.txt
bowtie2 -x $index -1 ${fastq_pair[0]} -2 ${fastq_pair[1]} -p $task.cpus 1> ${sample_id}.bam 2> ${sample_id}_bowtie2.log
if [ "$clear" = "true" ];
then
work_regex=".*/work/.{2}/.{30}/.*"
file_source1=\$(readlink -f \$(pwd)/${fastq_pair[0]})
file_source2=\$(readlink -f \$(pwd)/${fastq_pair[1]})
if [[ "\$file_source1" =~ \$work_regex ]]; then
rm \$file_source1 \$file_source2
fi
fi
echo pass > .status
else
echo "False" > pair_info.txt
bowtie2 -x $index -U ${fastq_pair[0]} -p $task.cpus 1> ${sample_id}.bam 2> ${sample_id}_bowtie2.log
if [ "$clear" = "true" ];
then
work_regex=".*/work/.{2}/.{30}/.*"
file_source1=\$(readlink -f \$(pwd)/${fastq_pair[0]})
if [[ "\$file_source1" =~ \$work_regex ]]; then
rm \$file_source1
fi
fi
echo pass > .status
fi
} || {
echo fail > .status
}
"""
}
process report_bowtie_1_4 {
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_4 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_4 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId bowtie_1_4 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
input:
set sample_id, file(bowtie_log) from into_json_1_4
output:
set sample_id, val("1_4_report_bowtie"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_report_bowtie_1_4
set sample_id, val("report_bowtie_1_4"), val("1_4"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_report_bowtie_1_4
file ".versions"
script:
template "process_mapping.py"
}
process retrieve_mapped_1_5 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_5 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_5 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_5 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId retrieve_mapped_1_5 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
publishDir 'results/mapping/retrieve_mapped_1_5/'
input:
set sample_id, file(is_pair), file(bam) from bowtie_out_1_3
output:
set sample_id , file("*_mapped*") into OUT_retrieve_mapped_1_4
set sample_id, val("1_5_retrieve_mapped"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_retrieve_mapped_1_5
set sample_id, val("retrieve_mapped_1_5"), val("1_5"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_retrieve_mapped_1_5
file ".versions"
script:
"""
if [[ \$(cat ${is_pair}) == "True" ]];
then
samtools view -buh -F 12 -o ${sample_id}_samtools.bam -@ $task.cpus ${bam}
rm ${bam}
samtools fastq -1 ${sample_id}_mapped_1.fq -2 ${sample_id}_mapped_2.fq ${sample_id}_samtools.bam
rm ${sample_id}_samtools.bam
else
samtools view -buh -F 4 -o ${sample_id}_samtools.bam -@ $task.cpus ${bam}
rm ${bam}
samtools fastq ${sample_id}_samtools.bam > ${sample_id}_mapped.fq
rm ${sample_id}_samtools.bam
fi
"""
}
process renamePE_1_4 {
tag { sample_id }
publishDir 'results/mapping/retrieve_mapped_{{ pid }}/'
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_5 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_5 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_5 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId renamePE_1_5 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
input:
set sample_id, file(fastq_pair) from OUT_retrieve_mapped_1_4
output:
set sample_id , file("*.headersRenamed*") into retrieve_mapped_out_1_4
set sample_id, val("1_5_renamePE"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_renamePE_1_5
set sample_id, val("renamePE_1_5"), val("1_5"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_renamePE_1_5
file ".versions"
script:
template "renamePE_samtoolsFASTQ.py"
}
IN_genome_size_1_6 = Channel.value(params.genomeSize)
.map{it -> it.toString().isNumber() ? it : exit (1, "The genomeSize parameter must be a number or a float. Provided value: '${params.genomeSize}'")}
IN_min_coverage_1_6 = Channel.value(params.minCoverage)
.map{it -> it.toString().isNumber() ? it : exit (1, "The minCoverage parameter must be a number or a float. Provided value: '${params.minCoverage}'")}
process integrity_coverage2_1_6 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_6 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_6 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_6 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId check_coverage_1_6 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
cpus 1
input:
set sample_id, file(fastq_pair) from retrieve_mapped_out_1_4
val gsize from IN_genome_size_1_6
val cov from IN_min_coverage_1_6
// Use -e option for skipping encoding guess
val opts from Channel.value('-e')
output:
set sample_id,
file(fastq_pair),
file('*_coverage'),
file('*_max_len') optional true into MAIN_integrity_1_6
file('*_report') into LOG_report_coverage_1_6
set sample_id, val("1_6_check_coverage"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_check_coverage_1_6
set sample_id, val("check_coverage_1_6"), val("1_6"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_check_coverage_1_6
file ".versions"
script:
template "integrity_coverage.py"
}
_check_coverage_out_1_5 = Channel.create()
SIDE_max_len_1_6 = Channel.create()
MAIN_integrity_1_6
.filter{ it[2].text != "fail" }
.separate(_check_coverage_out_1_5, SIDE_max_len_1_6){
a -> [ [a[0], a[1]], [a[0], a[3].text]]
}
process report_coverage2_1_6 {
// This process can only use a single CPU
cpus 1
publishDir 'reports/coverage_1_6/'
input:
file(report) from LOG_report_coverage_1_6.filter{ it.text != "corrupt" }.collect()
output:
file 'estimated_coverage_second.csv'
"""
echo Sample,Estimated coverage,Status >> estimated_coverage_second.csv
cat $report >> estimated_coverage_second.csv
"""
}
_check_coverage_out_1_5.into{ check_coverage_out_1_5;_LAST_fastq_1_8;_LAST_fastq_1_11 }
SIDE_max_len_1_6.set{ SIDE_max_len_1_7 }
//MAIN INPUT - FASTQ FILES
spades_in = Channel.create()
megahit_in = Channel.create()
check_coverage_out_1_5.into{ spades_in; megahit_in }
//EXPECTED GENOME SIZE
if ( !params.minimumContigSize.toString().isNumber() ){
exit 1, "'minimumContigSize' parameter must be a number. Provided value: '${params.minimumContigSize}'"
}
//SPADES OPTIONS
if ( !params.spadesMinCoverage.toString().isNumber() ){
exit 1, "'spadesMinCoverage' parameter must be a number. Provided value: '${params.spadesMinCoverage}'"
}
if ( !params.spadesMinKmerCoverage.toString().isNumber()){
exit 1, "'spadesMinKmerCoverage' parameter must be a number. Provided value: '${params.spadesMinKmerCoverage}'"
}
if ( params.spadesKmers.toString().split(" ").size() <= 1 ){
if (params.spadesKmers.toString() != 'auto'){
exit 1, "'spadesKmers' parameter must be a sequence of space separated numbers or 'auto'. Provided value: ${params.spadesKmers}"
}
}
clear = params.clearInput ? "true" : "false"
checkpointClearSpades_1_7 = Channel.value(clear)
checkpointClearMegahit_1_7 = Channel.value(clear)
//MEGAHIT OPTIONS
if ( params.megahitKmers.toString().split(" ").size() <= 1 ){
if (params.megahitKmers.toString() != 'auto'){
exit 1, "'megahitKmers' parameter must be a sequence of space separated numbers or 'auto'. Provided value: ${params.megahitKmers}"
}
}
//SPADES INPUT CHANNELS
IN_spades_opts_1_7 = Channel.value([params.spadesMinCoverage,params.spadesMinKmerCoverage])
IN_spades_kmers_1_7 = Channel.value(params.spadesKmers)
//MEGAGIT INPUT CHANNELS
IN_megahit_kmers_1_7 = Channel.value(params.megahitKmers)
SIDE_max_len_spades = Channel.create()
SIDE_max_len_megahit = Channel.create()
SIDE_max_len_1_7.into{SIDE_max_len_spades ; SIDE_max_len_megahit}
disableRR_1_7 = "false"
process va_spades_1_7 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_7 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId viral_assembly_1_7 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
validExitStatus 0,1
tag { sample_id }
publishDir 'results/assembly/spades_1_7/', pattern: '*_spades*.fasta', mode: 'copy'
input:
set sample_id, file(fastq_pair), max_len from spades_in.join(SIDE_max_len_spades)
val opts from IN_spades_opts_1_7
val kmers from IN_spades_kmers_1_7
val clear from checkpointClearSpades_1_7
val disable_rr from disableRR_1_7
output:
set sample_id, file({task.exitStatus == 1 ? ".exitcode" : '*_spades*.fasta'}) into assembly_spades
set sample_id, val("1_7_va_spades"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_va_spades_1_7
set sample_id, val("va_spades_1_7"), val("1_7"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_va_spades_1_7
file ".versions"
script:
template "spades.py"
}
class VerifyCompletness {
public static boolean contigs(String filename, int threshold){
BufferedReader reader = new BufferedReader(new FileReader(filename));
boolean result = processContigs(reader, threshold);
reader.close()
return result;
}
private static boolean processContigs(BufferedReader reader, int threshold){
String line;
int lineThreshold = 0;
List splittedLine
while ((line = reader.readLine()) != null) {
if (line.startsWith('>')) {
splittedLine = line.split('_')
lineThreshold = splittedLine[3].toInteger()
if(lineThreshold >= threshold) {
return true;
}
}
}
return false;
}
}
megahit = Channel.create()
good_assembly = Channel.create()
assembly_spades.choice(good_assembly, megahit){a -> a[1].toString() == "null" ? false : VerifyCompletness.contigs(a[1].toString(), params.minimumContigSize.toInteger()) == true ? 0 : 1}
process va_megahit_1_7 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_7 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId viral_assembly_1_7 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
publishDir 'results/assembly/megahit_1_7/', pattern: '*_megahit*.fasta', mode: 'copy'
input:
set sample_id, file(fastq_pair), max_len from megahit_in.join(megahit).map{ ot -> [ot[0], ot[1]] }.join(SIDE_max_len_megahit)
val kmers from IN_megahit_kmers_1_7
val clear from checkpointClearSpades_1_7
output:
set sample_id, file('*megahit*.fasta') into megahit_assembly
set sample_id, val("1_7_va_megahit"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_va_megahit_1_7
set sample_id, val("va_megahit_1_7"), val("1_7"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_va_megahit_1_7
file ".versions"
script:
template "megahit.py"
}
good_assembly.mix(megahit_assembly).into{ to_report_1_7 ; viral_assembly_out_1_6 }
orf_size = Channel.value(params.minimumContigSize)
process report_viral_assembly_1_7 {
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_7 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_7 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId viral_assembly_1_7 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
input:
set sample_id, file(assembly) from to_report_1_7
val min_size from orf_size
output:
set sample_id, val("1_7_report_viral_assembly"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_report_viral_assembly_1_7
set sample_id, val("report_viral_assembly_1_7"), val("1_7"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_report_viral_assembly_1_7
file ".versions"
script:
template "process_viral_assembly.py"
}
if ( !params.minAssemblyCoverage.toString().isNumber() ){
if (params.minAssemblyCoverage.toString() != 'auto'){
exit 1, "'minAssemblyCoverage' parameter must be a number or 'auto'. Provided value: ${params.minAssemblyCoverage}"
}
}
if ( !params.AMaxContigs.toString().isNumber() ){
exit 1, "'AMaxContigs' parameter must be a number. Provide value: '${params.AMaxContigs}'"
}
IN_assembly_mapping_opts_1_8 = Channel.value([params.minAssemblyCoverage,params.AMaxContigs])
IN_genome_size_1_8 = Channel.value(params.genomeSize)
process assembly_mapping_1_8 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_8 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_8 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_8 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId assembly_mapping_1_8 \"$params.platformSpecies\" true"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
input:
set sample_id, file(assembly), file(fastq) from viral_assembly_out_1_6.join(_LAST_fastq_1_8)
output:
set sample_id, file(assembly), file(fastq), 'coverages.tsv', 'coverage_per_bp.tsv', 'sorted.bam', 'sorted.bam.bai' into MAIN_am_out_1_8
set sample_id, file("coverage_per_bp.tsv") optional true into SIDE_BpCoverage_1_8
set sample_id, val("1_8_assembly_mapping"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_assembly_mapping_1_8
set sample_id, val("assembly_mapping_1_8"), val("1_8"), file(".report.json"), file(".versions"), file(".command.trace") into REPORT_assembly_mapping_1_8
file ".versions"
script:
"""
{
a=(${fastq})
echo [DEBUG] BUILDING BOWTIE INDEX FOR ASSEMBLY: $assembly >> .command.log 2>&1
bowtie2-build --threads ${task.cpus} $assembly genome_index >> .command.log 2>&1
if ((\${#a[@]} > 1));
then
echo [DEBUG] MAPPING READS FROM $fastq >> .command.log 2>&1
bowtie2 -q --very-sensitive-local --threads ${task.cpus} -x genome_index -1 ${fastq[0]} -2 ${fastq[1]} -S mapping.sam >> .command.log 2>&1
else
echo [DEBUG] MAPPING READS FROM $fastq >> .command.log 2>&1
bowtie2 -q --very-sensitive-local --threads ${task.cpus} -x genome_index -U ${fastq[0]} -S mapping.sam >> .command.log 2>&1
fi
echo [DEBUG] CONVERTING AND SORTING SAM TO BAM >> .command.log 2>&1
samtools sort -o sorted.bam -O bam -@ ${task.cpus} mapping.sam && rm *.sam >> .command.log 2>&1
echo [DEBUG] CREATING BAM INDEX >> .command.log 2>&1
samtools index sorted.bam >> .command.log 2>&1
echo [DEBUG] ESTIMATING READ DEPTH >> .command.log 2>&1
parallel -j ${task.cpus} samtools depth -ar {} sorted.bam \\> {}.tab ::: \$(grep ">" $assembly | cut -c 2- | tr " " "_")
# Insert 0 coverage count in empty files. See Issue #2
echo [DEBUG] REMOVING EMPTY FILES >> .command.log 2>&1
find . -size 0 -print0 | xargs -0 -I{} sh -c 'echo -e 0"\t"0"\t"0 > "{}"'
echo [DEBUG] COMPILING COVERAGE REPORT >> .command.log 2>&1
parallel -j ${task.cpus} echo -n {.} '"\t"' '&&' cut -f3 {} '|' paste -sd+ '|' bc >> coverages.tsv ::: *.tab
cat *.tab > coverage_per_bp.tsv
rm *.tab
if [ -f "coverages.tsv" ]
then
echo pass > .status
else
echo fail > .status
fi
echo -n "" > .report.json
echo -n "" > .versions
} || {
echo fail > .status
}
"""
}
/** PROCESS_ASSEMBLY_MAPPING - MAIN
Processes the results from the assembly_mapping process and filters the
assembly contigs based on coverage and length thresholds.
*/
process process_assembly_mapping_1_8 {
// Send POST request to platform
if ( params.platformHTTP != null ) {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; export PATH; set_dotfiles.sh; startup_POST.sh $params.projectId $params.pipelineId 1_8 $params.platformHTTP"
afterScript "final_POST.sh $params.projectId $params.pipelineId 1_8 $params.platformHTTP; report_POST.sh $params.projectId $params.pipelineId 1_8 $params.sampleName $params.reportHTTP $params.currentUserName $params.currentUserId assembly_mapping_1_8 \"$params.platformSpecies\" false"
} else {
beforeScript "PATH=${workflow.projectDir}/bin:\$PATH; set_dotfiles.sh"
}
tag { sample_id }
// This process can only use a single CPU
cpus 1
input:
set sample_id, file(assembly), file(fastq), file(coverage), file(coverage_bp), file(bam_file), file(bam_index) from MAIN_am_out_1_8
val opts from IN_assembly_mapping_opts_1_8
val gsize from IN_genome_size_1_8
output:
set sample_id, file(fastq), '*_filt.fasta', 'filtered.bam', 'filtered.bam.bai' into assembly_mapping_out_1_7
set sample_id, val("1_8_process_am"), file(".status"), file(".warning"), file(".fail"), file(".command.log") into STATUS_process_am_1_8