-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.nf
1499 lines (1299 loc) · 56.4 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
/*
========================================================================================
nf-core/coproid
========================================================================================
nf-core/coproid Analysis Pipeline.
#### Homepage / Documentation
https://github.com/nf-core/coproid
----------------------------------------------------------------------------------------
*/
def helpMessage() {
log.info nfcoreHeader()
log.info"""
coproID: Coprolite Identification
Homepage: https://github.com/nf-core/coproid
Author: Maxime Borry <[email protected]>
Version ${workflow.manifest.version}
=========================================
Usage:
The typical command for running the pipeline is as follows:
nextflow run maxibor/coproid -profile docker --genome1 'GRCh37' --genome2 'CanFam3.1' --name1 'Homo_sapiens' --name2 'Canis_familiaris' --reads '*_R{1,2}.fastq.gz'
Mandatory arguments:
--reads Path to input data (must be surrounded with quotes)
--name1 Name of candidate 1. Example: "Homo_sapiens"
--fasta1 Path to human genome fasta file (must be surrounded with quotes). Must be provided if --genome1 is not provided
--genome1 Name of iGenomes reference for Homo_sapiens. Must be provided if --fasta1 is not provided
--name2 Name of candidate 2. Example: "Canis_familiaris"
--fasta2 Path to canidate organism 2 genome fasta file (must be surrounded with quotes). Must be provided if --genome2 is not provided
--genome2 Name of iGenomes reference for candidate organism 2. Must be provided if --fasta2 is not provided
--krakendb Path to MiniKraken2_v2_8GB Database
Settings:
--adna Specified if data is modern (false) or ancient DNA (true). Default = ${params.adna}
--phred Specifies the fastq quality encoding (33 | 64). Defaults to ${params.phred}
--single_end Specified if reads are single-end. Default = ${params.single_end}
--collapse Specifies if AdapterRemoval should merge the paired-end sequences or not (true | false). Default = ${params.collapse}
--identity Identity threshold to retain read alignment. Default = ${params.identity}
--pmdscore Minimum PMDscore to retain read alignment. Default = ${params.pmdscore}
--library DNA preparation library type ( 'classic' | 'UDGhalf'). Default = ${params.library}
--bowtie Bowtie settings for sensivity ('very-fast' | 'very-sensitive'). Default = ${params.bowtie}
--minKraken Minimum number of Kraken hits per Taxonomy ID to report. Default = ${params.minKraken}
--endo1 Proportion of Endogenous DNA in organism 1 target microbiome. Default = ${params.endo1}
--endo2 Proportion of Endogenous DNA in organism 2 target microbiome. Default = ${params.endo1}
--endo3 Proportion of Endogenous DNA in organism 3 target microbiome. Default = ${params.endo1}
--sp_norm Sourcepredict normalization method. One of 'rle', 'gmpr', 'subsample'. Default = ${params.sp_norm}
--sp_embed SourcePredict embedding algorithm. One of 'mds', 'tsne', 'umap'. Default = ${params.sp_embed}
--sp_neighbors Sourcepredict numbers of neighbors for KNN ML. Integer or 'all'. Default = ${params.sp_neighbors}
Options:
--name3 Name of candidate 1. Example: "Sus_scrofa"
--fasta3 Path to canidate organism 3 genome fasta file (must be surrounded with quotes). Must be provided if --genome3 is not provided
--genome3 Name of iGenomes reference for candidate organism 3. Must be provided if --fasta3 is not provided
--index1 Path to Bowtie2 index of genome candidate 1, in the form of "/path/to/bowtie_index/basename"
--index2 Path to Bowtie2 index genome candidate 2 Coprolite maker's genome, in the form of "/path/to/bowtie_index/basename"
--index3 Path to Bowtie2 index genome candidate 3 Coprolite maker's genome, in the form of "/path/to/bowtie_index/basename"
Other options:
--outdir The output directory where the results will be saved. Defaults to ${params.outdir}
--email Set this parameter to your e-mail address to get a summary e-mail with details of the run sent to you when the workflow exits
--maxMultiqcEmailFileSize Theshold size for MultiQC report to be attached in notification email. If file generated by pipeline exceeds the threshold, it will not be attached (Default: 25MB)
-name Name for the pipeline run. If not specified, Nextflow will automatically generate a random mnemonic.
--help --h Shows this help page
AWSBatch options:
--awsqueue [str] The AWSBatch JobQueue that needs to be set when running on AWSBatch
--awsregion [str] The AWS Region for your AWS Batch job to run on
--awscli [str] Path to the AWS CLI tool
""".stripIndent()
}
/****************************
DEFAULT VARIABLE VALUES SETUP
*****************************/
// Default variable configuration
bowtie_setting = ''
collapse_setting = ''
report_template = "$baseDir/templates/coproID_report.ipynb"
coproid_logo = file("$baseDir/assets/img/coproid_logo.png")
// Show help message
if (params.help){
helpMessage()
exit 0
}
// Message for empty run
if ( (!params.reads && !params.readPaths) || !params.name1 || !params.name2 || !params.krakendb || (!params.genome1 && !params.fasta1) || (!params.genome2 && !params.fasta2)){
log.info"""
CoproID was launched with missing mandatory arguments.
Please check your command line and retry.
To get the help menu, please run:
nextflow run maxibor/coproid --help
The complete documentation is available at https://github.com/nf-core/coproid
"""
exit 0
}
/****************************************
SETTING UP REFERENCE GENOMES AND IGENOMES
*****************************************/
// Check if genome(s) exists in the config file
if (params.genomes && params.genome1 && !params.genomes.containsKey(params.genome1)) {
exit 1, "The provided genome '${params.genome1}' is not available in the iGenomes file. Currently the available genomes are ${params.genomes.keySet().join(", ")}"
}
if (params.genomes && params.genome2 && !params.genomes.containsKey(params.genome2)) {
exit 1, "The provided genome '${params.genome2}' is not available in the iGenomes file. Currently the available genomes are ${params.genomes.keySet().join(", ")}"
}
if (params.genomes && params.genome3 && !params.genomes.containsKey(params.genome3)) {
exit 1, "The provided genome '${params.genome3}' is not available in the iGenomes file. Currently the available genomes are ${params.genomes.keySet().join(", ")}"
}
// Genome 1
fasta1 = params.genome1 ? params.genomes[ params.genome1 ].fasta ?: false : false
bt1 = params.genome1 ? params.genomes[ params.genome1 ].bowtie2 ?: false : false
if ( params.fasta1 ){
fasta1 = file(params.fasta1)
} else if (fasta1) {
fasta1 = file(params.genomes[ params.genome1 ].fasta, checkIfExists: true)
}
if( !fasta1.exists() ) exit 1, "Fasta1 file not found: ${params.fasta2}"
if (params.index1) {
bt1 = params.index1
}
if (bt1) {
lastPath = bt1.lastIndexOf(File.separator)
bt1_dir = bt1.substring(0, lastPath+1)
bt1_index = bt1.substring(lastPath+1)
Channel
.fromPath(bt1_dir+"/*.bt2")
.ifEmpty {exit 1, "Cannot find any index matching : ${bt1}\n"}
.collect()
.set {bt1_ch}
} else {
bt1_index = params.name1
}
// Genome 2
fasta2 = params.genome2 ? params.genomes[ params.genome2 ].fasta ?: false : false
bt2 = params.genome2 ? params.genomes[ params.genome2 ].bowtie2 ?: false : false
if ( params.fasta2 ){
fasta2 = file(params.fasta2)
} else if (fasta2) {
fasta2 = file(params.genomes[ params.genome2 ].fasta, checkIfExists: true)
}
if( !fasta2.exists() ) exit 1, "Fasta2 file not found: ${params.fasta2}"
if (params.index2) {
bt2 = params.index2
}
if (bt2) {
lastPath = bt2.lastIndexOf(File.separator)
bt2_dir = bt2.substring(0, lastPath+1)
bt2_index = bt2.substring(lastPath+1)
Channel
.fromPath(bt2_dir+"/*.bt2")
.ifEmpty {exit 1, "Cannot find any index matching : ${params.bt2}\n"}
.collect()
.set {bt2_ch}
} else {
bt2_index = params.name2
}
// Genome 3
if (params.name3) {
fasta3 = params.genome3 ? params.genomes[ params.genome3 ].fasta ?: false : false
bt3 = params.genome3 ? params.genomes[ params.genome3 ].bowtie2 ?: false : false
if ( params.fasta3 ){
fasta3 = file(params.fasta3)
} else if (fasta3) {
fasta3 = file(params.genomes[ params.genome3 ].fasta, checkIfExists: true)
}
if( !fasta3.exists() ) exit 1, "Fasta3 file not found: ${params.fasta3}"
if (params.index3) {
bt3 = params.index3
}
if (bt3) {
lastPath = bt3.lastIndexOf(File.separator)
bt3_dir = bt3.substring(0, lastPath+1)
bt3_index = bt3.substring(lastPath+1)
Channel
.fromPath(bt3_dir+"/*.bt2")
.ifEmpty {exit 1, "Cannot find any index matching : ${params.bt3}\n"}
.collect()
.set {bt3_ch}
} else {
bt3_index = params.name3
}
}
// Has the run name been specified by the user?
// this has the bonus effect of catching both -name and --name
custom_runName = params.name
if (!(workflow.runName ==~ /[a-z]+_[a-z]+/)) {
custom_runName = workflow.runName
}
// Stage config files
ch_multiqc_config = file(params.multiqc_config, checkIfExists: true)
ch_output_docs = file("$baseDir/docs/output.md", checkIfExists: true)
// Report template channel
report_template_ch = file(report_template)
/*************
SETTINGS CHECK
**************/
// Bowtie setting check
if (params.bowtie == 'very-fast'){
bowtie_setting = '--very-fast'
} else if (params.bowtie == 'very-sensitive'){
bowtie_setting = '--very-sensitive -N 1'
} else {
println "Problem with --bowtie. Make sure to choose between 'very-fast' or 'very-sensitive'"
exit(1)
}
// single_end or paired_end Check
if (params.single_end == false) {
paired_end = true
} else if (params.single_end == true) {
paired_end = false
}
//Library setting check
if ((params.library != 'classic' && params.library != 'UDGhalf' ) && (params.h == false || params.help == false) ){
println 'ERROR: You did not specify --library'
exit(1)
}
if (params.library == 'classic'){
library = ''
} else {
library = '--UDGhalf'
}
if( ! nextflow.version.matches(workflow.manifest.nextflowVersion) ){
println "Your version of Nextflow is too old, please use Nextflow "+workflow.manifest.nextflowVersion.toString()
exit(1)
}
// Check sourcepredict parameters
if (params.sp_embed != 'mds' && params.sp_embed != 'tsne' && params.sp_embed != 'umap'){
println "${params.sp_embed} is not a valid method for SourcePredict embedding (--sp_embed)"
println """Available methods are: 'mds', 'tsne', 'umap' """
exit(1)
}
if (params.sp_norm != 'rle' && params.sp_norm != 'gmpr' && params.sp_norm != 'subsample'){
println "${params.sp_norm} is not a valid method for SourcePredict normalization method (--sp_norm)"
println """Available methods are: 'rle', 'gmpr', 'subsample' """
exit(1)
}
/****************
AWSBATCH SETTINGS
*****************/
if( workflow.profile == 'awsbatch') {
// AWSBatch sanity checking
if (!params.awsqueue || !params.awsregion) exit 1, "Specify correct --awsqueue and --awsregion parameters on AWSBatch!"
// Check outdir paths to be S3 buckets if running on AWSBatch
// related: https://github.com/nextflow-io/nextflow/issues/813
if (!params.outdir.startsWith('s3:')) exit 1, "Outdir not on S3 - specify S3 Bucket to run on AWSBatch!"
// Prevent trace files to be stored on S3 since S3 does not support rolling files.
if (workflow.tracedir.startsWith('s3:')) exit 1, "Specify a local tracedir or run without trace! S3 cannot be used for tracefiles."
}
/*********************
READS CHANNEL CREATION
**********************/
// Creating reads channel
if(params.readPaths){
if(params.single_end){
Channel
.from(params.readPaths)
.map { row -> [ row[0], [ file(row[1][0], checkIfExists: true) ] ] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied" }
.into { ch_read_files_fastqc; ch_read_files_trimming }
} else {
Channel
.from(params.readPaths)
.map { row -> [ row[0], [ file(row[1][0], checkIfExists: true), file(row[1][1], checkIfExists: true) ] ] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied" }
.into { ch_read_files_fastqc; ch_read_files_trimming }
}
} else {
Channel
.fromFilePairs(params.reads, size: params.single_end ? 1 : 2)
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.reads}\nNB: Path needs to be enclosed in quotes!\nIf this is single-end data, please specify --single_end on the command line." }
.into { ch_read_files_fastqc; ch_read_files_trimming }
}
/****************
KRAKEN DB CHANNEL
*****************/
if (params.krakendb.endsWith(".tar.gz")){
comp_kraken = file(params.krakendb)
process decomp_kraken {
input:
file(ckdb) from comp_kraken
output:
file("kraken") into krakendb_ch
script:
"""
tar xvzf $ckdb
"""
}
} else {
krakendb_ch = file(params.krakendb)
}
/*******************************
SOURCEPREDICT SOURCES AND LABELS
********************************/
sp_labels = file(params.sp_labels, checkIfExists: true)
sp_sources = file(params.sp_sources, checkIfExists: true)
/*******************
Logging parameters
********************/
log.info nfcoreHeader()
log.info " coproID: Coprolite Identification"
log.info " Homepage / Documentation: https://github.com/nf-core/coproid"
log.info " Author: Maxime Borry <[email protected]>"
log.info "================================================================"
def summary = [:]
if (workflow.revision) summary['Pipeline Release'] = workflow.revision
summary['Run Name'] = custom_runName ?: workflow.runName
if (params.reads) {
summary['Reads'] = params.reads
} else {
summary['Read Path'] = params.readPaths
}
summary['phred quality'] = params.phred
summary['identity threshold'] = params.identity
summary['collapse'] = params.collapse
summary['Ancient DNA'] = params.adna
summary['single_end'] = params.single_end
summary['bowtie setting'] = params.bowtie
if (params.genome1) summary['Genome1'] = params.genome1
if (params.index1) summary["Genome1 BT2 index"] = params.index1
if (params.genome2) summary['Genome2'] = params.genome2
if (params.index2) summary["Genome2 BT2 index"] = params.index2
if (params.genome3) summary['Genome3'] = params.genome3
if (params.index3) summary["Genome3 BT3 index"] = params.index3
summary['Kraken DB'] = params.krakendb
summary['Min Kraken Hits to report Clade'] = params.minKraken
summary['Organism 1'] = params.name1
summary['Organism 2'] = params.name2
if (params.name3) summary['Organism 3'] = params.name3
summary['Sourcepredict sources'] = params.sp_sources
summary['Sourcepredict labels'] = params.sp_labels
summary['PMD Score'] = params.pmdscore
summary['Library type'] = params.library
summary["Result directory"] = params.outdir
summary['Max Resources'] = "$params.max_memory memory, $params.max_cpus cpus, $params.max_time time per job"
if(workflow.containerEngine) summary['Container'] = "$workflow.containerEngine - $workflow.container"
summary['Launch dir'] = workflow.launchDir
summary['Working dir'] = workflow.workDir
summary['Script dir'] = workflow.projectDir
summary['User'] = workflow.userName
if (workflow.profile.contains('awsbatch')) {
summary['AWS Region'] = params.awsregion
summary['AWS Queue'] = params.awsqueue
summary['AWS CLI'] = params.awscli
}
summary['Config Profile'] = workflow.profile
if (params.config_profile_description) summary['Config Description'] = params.config_profile_description
if (params.config_profile_contact) summary['Config Contact'] = params.config_profile_contact
if (params.config_profile_url) summary['Config URL'] = params.config_profile_url
if (params.email || params.email_on_fail) {
summary['E-mail Address'] = params.email
summary['E-mail on failure'] = params.email_on_fail
summary['MultiQC maxsize'] = params.max_multiqc_email_size
}
log.info summary.collect { k,v -> "${k.padRight(25)}: $v" }.join("\n")
log.info "\033[2m----------------------------------------------------\033[0m"
// 0: FASTQC
process fastqc {
tag "$name"
input:
set val(name), file(reads) from ch_read_files_fastqc
output:
file '*_fastqc.{zip,html}' into fastqc_results
script:
"""
fastqc -q $reads
"""
}
// 0.1 Rename reference genome fasta files
process renameGenome1 {
input:
file (genome) from fasta1
output:
file (params.name1+".fa") into (genome1Fasta, genome1Size, genome1damageprofiler)
script:
outname = params.name1+".fa"
"""
mv $genome $outname
"""
}
process renameGenome2 {
input:
file (genome) from fasta2
output:
file (params.name2+".fa") into (genome2Fasta, genome2Size, genome2damageprofiler)
script:
outname = params.name2+".fa"
"""
mv $genome $outname
"""
}
if (params.name3){
process renameGenome3 {
input:
file (genome) from fasta3
output:
file (params.name3+".fa") into (genome3Fasta, genome3Size, genome3damageprofiler)
script:
outname = params.name3+".fa"
"""
mv $genome $outname
"""
}
}
// 1.1: AdapterRemoval: Adapter trimming, quality filtering, and read merging
if (params.collapse == true && params.single_end == false){
process AdapterRemovalCollapse {
tag "$name"
label 'process_low'
input:
set val(name), file(reads) from ch_read_files_trimming
output:
set val(name), file('*.trimmed.fastq') into trimmed_reads_genome1, trimmed_reads_genome2, trimmed_reads_genome3, trimmed_reads_kraken
file("*.settings") into adapter_removal_results
script:
out1 = name+".pair1.discarded.fastq"
out2 = name+".pair2.discarded.fastq"
col_out = name+".trimmed.fastq"
settings = name+".settings"
"""
AdapterRemoval --basename $name \\
--file1 ${reads[0]} \\
--file2 ${reads[1]} \\
--trimns \\
--trimqualities \\
--collapse \\
--minquality 20 \\
--minlength 30 \\
--output1 $out1 \\
--output2 $out2 \\
--outputcollapsed $col_out \\
--threads ${task.cpus} \\
--qualitybase ${params.phred} \\
--settings $settings
"""
}
} else if (params.collapse == false || params.single_end == true) {
process AdapterRemovalNoCollapse {
tag "$name"
label 'process_low'
input:
set val(name), file(reads) from ch_read_files_trimming
output:
set val(name), file('*.trimmed.fastq') into trimmed_reads_genome1, trimmed_reads_genome2, trimmed_reads_genome3
file("*.settings") into adapter_removal_results
script:
out1 = name+".pair1.trimmed.fastq"
out2 = name+".pair2.trimmed.fastq"
se_out = name+".trimmed.fastq"
settings = name+".settings"
if (params.single_end == false) {
"""
AdapterRemoval --basename $name \\
--file1 ${reads[0]} \\
--file2 ${reads[1]} \\
--trimns \\
--trimqualities \\
--minquality 20 \\
--minlength 30 \\
--output1 $out1 \\
--output2 $out2 \\
--threads ${task.cpus} \\
--qualitybase ${params.phred} \\
--settings $settings
"""
} else {
"""
AdapterRemoval --basename $name \\
--file1 ${reads[0]} \\
--trimns \\
--trimqualities \\
--minquality 20 \\
--minlength 30 \\
--output1 $se_out \\
--threads ${task.cpus} \\
--qualitybase ${params.phred} \\
--settings $settings
"""
}
}
} else {
println "Problem with --collapse. If --single_end is set to true, you have to set --collapse to false"
exit(1)
}
if (!params.index1){
// 1.2: Bowtie Indexing of Genome1
process BowtieIndexGenome1 {
tag "${params.name1}"
label 'process_medium'
input:
file(fasta) from genome1Fasta
output:
file("*.bt2") into bt1_ch
script:
"""
bowtie2-build $fasta ${bt1_index}
"""
}
}
// 2.1: Reads alignment on Genome1
process AlignToGenome1 {
tag "$name"
label 'process_medium'
publishDir "${params.outdir}/alignments/${params.name1}", mode: 'copy', pattern: '*.sorted.bam'
input:
set val(name), file(reads) from trimmed_reads_genome1
file(index) from bt1_ch
output:
set val(name), file("*.aligned.sorted.bam") into alignment_genome1, alignment_genome1_pmd
set val(name), file("*.unaligned.sorted.bam") into unaligned_genome1
set val(name), file("*.stats.txt") into align1_multiqc
script:
samfile = "aligned_"+params.name1+".sam"
fstat = name+"_"+params.name1+".stats.txt"
outfile = name+"_"+params.name1+".aligned.sorted.bam"
outfile_unalign = name+"_"+params.name1+".unaligned.sorted.bam"
if (params.collapse == true || params.single_end == true) {
"""
bowtie2 -x $bt1_index -U ${reads[0]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
} else if (params.collapse == false){
"""
bowtie2 -x $bt1_index -1 ${reads[0]} -2 ${reads[1]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
}
}
process bam2fq {
tag "$name"
label 'process_medium'
input:
set val(name), file(bam) from unaligned_genome1
output:
set val(name), file("*.fastq") into unmapped_humans_reads
script:
if (paired_end && params.collapse == false){
out1 = name+"_"+params.name1+".unaligned_R1.fastq"
out2 = name+"_"+params.name1+".unaligned_R2.fastq"
"""
samtools fastq -1 $out1 -2 $out2 -0 /dev/null -s /dev/null -n -F 0x900 $bam
"""
} else {
out = name+"_"+params.name1+".unaligned.fastq"
"""
samtools fastq $bam > $out
"""
}
}
// 1.3: Bowtie Indexing of Genome2
if (!params.index2){
process BowtieIndexGenome2 {
tag "${params.name2}"
label 'process_medium'
input:
file(fasta) from genome2Fasta
output:
file("*.bt2") into bt2_ch
script:
"""
bowtie2-build $fasta ${bt2_index}
"""
}
}
// 1.3: Bowtie Indexing of Genome2
if (params.name3 && !params.index3) {
process BowtieIndexGenome3 {
tag "${params.name2}"
label 'process_medium'
input:
file(fasta) from genome3Fasta
output:
file("*.bt2") into bt3_ch
script:
"""
bowtie2-build $fasta ${bt3_index}
"""
}
}
// 2.2: Reads alignment on Genome2
process AlignToGenome2 {
tag "$name"
label 'process_medium'
publishDir "${params.outdir}/alignments/${params.name2}", mode: 'copy', pattern: '*.sorted.bam'
input:
set val(name), file(reads) from trimmed_reads_genome2
file(index) from bt2_ch
output:
set val(name), file("*.aligned.sorted.bam") into alignment_genome2, alignment_genome2_pmd
set val(name), file("*.unaligned.sorted.bam") into unaligned_genome2
set val(name), file("*.stats.txt") into align2_multiqc
script:
samfile = "aligned_"+params.name2+".sam"
fstat = name+"_"+params.name2+".stats.txt"
outfile = name+"_"+params.name2+".aligned.sorted.bam"
outfile_unalign = name+"_"+params.name2+".unaligned.sorted.bam"
if (params.collapse == true || params.single_end == true) {
"""
bowtie2 -x $bt2_index -U ${reads[0]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
} else if (params.collapse == false){
"""
bowtie2 -x $bt2_index -1 ${reads[0]} -2 ${reads[1]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
}
}
// 2.2: Reads alignment on Genome3
if (params.name3) {
process AlignToGenome3 {
tag "$name"
label 'process_medium'
publishDir "${params.outdir}/alignments/${params.name1}", mode: 'copy', pattern: '*.sorted.bam'
input:
set val(name), file(reads) from trimmed_reads_genome3
file(index) from bt3_ch
output:
set val(name), file("*.aligned.sorted.bam") into alignment_genome3, alignment_genome3_pmd
set val(name), file("*.unaligned.sorted.bam") into unaligned_genome3
set val(name), file("*.stats.txt") into align3_multiqc
script:
samfile = "aligned_"+params.name3+".sam"
fstat = name+"_"+params.name3+".stats.txt"
outfile = name+"_"+params.name3+".aligned.sorted.bam"
outfile_unalign = name+"_"+params.name3+".unaligned.sorted.bam"
if (params.collapse == true || params.single_end == true) {
"""
bowtie2 -x $bt3_index -U ${reads[0]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
} else if (params.collapse == false){
"""
bowtie2 -x $bt3_index -1 ${reads[0]} -2 ${reads[1]} $bowtie_setting --threads ${task.cpus} > $samfile 2> $fstat
samtools view -S -b -F 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile
samtools view -S -b -f 4 -@ ${task.cpus} $samfile | samtools sort -@ ${task.cpus} -o $outfile_unalign
"""
}
}
}
// 3: Checking for read PMD with PMDtools
if (params.adna){
process pmdtoolsgenome1 {
tag "$name"
publishDir "${params.outdir}/pmdtools/${params.name1}", mode: 'copy', pattern: '*.pmd_filtered.bam'
input:
set val(name), file(bam1) from alignment_genome1_pmd
output:
set val(name), file("*.pmd_filtered.bam") into pmd_aligned1
script:
outfile = name+"_"+params.name1+".pmd_filtered.bam"
"""
samtools view -h -F 4 $bam1 | pmdtools -t ${params.pmdscore} --header $library | samtools view -Sb - > $outfile
"""
}
process pmdtoolsgenome2 {
tag "$name"
publishDir "${params.outdir}/pmdtools/${params.name2}", mode: 'copy', pattern: '*.pmd_filtered.bam'
input:
set val(name), file(bam2) from alignment_genome2_pmd
output:
set val(name), file("*.pmd_filtered.bam") into pmd_aligned2
script:
outfile = name+"_"+params.name2+".pmd_filtered.bam"
"""
samtools view -h -F 4 $bam2 | pmdtools -t ${params.pmdscore} --header $library | samtools view -Sb - > $outfile
"""
}
if (params.name3 != ''){
process pmdtoolsgenome3 {
tag "$name"
publishDir "${params.outdir}/pmdtools/${params.name3}", mode: 'copy', pattern: '*.pmd_filtered.bam'
input:
set val(name), file(bam3) from alignment_genome3_pmd
output:
set val(name), file("*.pmd_filtered.bam") into pmd_aligned3
script:
outfile = name+"_"+params.name3+".pmd_filtered.bam"
"""
samtools view -h -F 4 $bam3 | pmdtools -t ${params.pmdscore} --header $library | samtools view -Sb - > $outfile
"""
}
}
}
process kraken2 {
tag "$name"
label 'process_medium'
input:
set val(name), file(reads) from unmapped_humans_reads
file(krakendb) from krakendb_ch
output:
set val(name), file('*.kraken.out') into kraken_out
set val(name), file('*.kreport') into kraken_report
script:
out = name+".kraken.out"
kreport = name+".kreport"
if (paired_end && params.collapse == false){
"""
kraken2 --db ${krakendb} \\
--threads ${task.cpus} \\
--output $out \\
--report $kreport \\
--paired ${reads[0]} ${reads[1]}
"""
} else {
"""
kraken2 --db ${krakendb} \\
--threads ${task.cpus} \\
--output $out \\
--report $kreport ${reads[0]}
"""
}
}
process kraken_parse {
tag "$name"
input:
set val(name), file(kraken_r) from kraken_report
output:
file('*.kraken_parsed.csv') into kraken_parsed
script:
out = name+".kraken_parsed.csv"
"""
kraken_parse.py -c ${params.minKraken} $kraken_r
"""
}
process kraken_merge {
publishDir "${params.outdir}/kraken", mode: 'copy'
input:
file(csv_count) from kraken_parsed.collect()
output:
file('kraken_merged.csv') into kraken_merged
script:
out = "kraken_merged.csv"
"""
merge_kraken_res.py -o $out
"""
}
process sourcepredict {
label 'process_high'
input:
file(otu_table) from kraken_merged
file(sp_sources) from sp_sources
file(sp_labels) from sp_labels
output:
file('*.sourcepredict.csv') into sourcepredict_out
file('*_embedding.csv') into sourcepredict_embed_out
script:
outfile = "prediction.sourcepredict.csv"
embed_out = "sourcepredict_embedding.csv"
"""
sourcepredict -di ${params.sp_dim} \\
-kne ${params.sp_neighbors} \\
-me ${params.sp_embed} \\
-n ${params.sp_norm} \\
-l ${sp_labels} \\
-s ${sp_sources} \\
-t ${task.cpus} \\
-o $outfile \\
-e $embed_out $otu_table
"""
}
// 4: Count aligned bp on each genome and compute ratio
if (params.name3 == ''){
process countBp2genomes{
tag "$name"
label 'process_low'
input:
set val(name), file(abam1), file(abam2), file(bam1), file(bam2) from ( (params.adna ? pmd_aligned1.join(pmd_aligned2) : alignment_genome1_pmd.join(alignment_genome2_pmd)).join(alignment_genome1).join(alignment_genome2))
file(genome1) from genome1Size
file(genome2) from genome2Size
output:
set val(name), file("*.bpc.csv") into bp_count
set val(name), file("*"+params.name1+".ancient.filtered.bam") optional true into ancient_filtered_bam1
set val(name), file("*"+params.name2+".ancient.filtered.bam") optional true into ancient_filtered_bam2
script:
outfile = name+".bpc.csv"
organame1 = params.name1
organame2 = params.name2
obam1 = name+"_"+organame1+".filtered.bam"
obam2 = name+"_"+organame2+".filtered.bam"
if (params.adna) {
aobam1 = name+"_"+organame1+".ancient.filtered.bam"
aobam2 = name+"_"+organame2+".ancient.filtered.bam"
"""
samtools index $bam1
samtools index $bam2
samtools index $abam1
samtools index $abam2
normalizedReadCount -n $name \\
-b1 $bam1 \\
-ab1 $abam1 \\
-b2 $bam2 \\
-ab2 $abam2 \\
-g1 $genome1 \\
-g2 $genome2 \\
-r1 $organame1 \\
-r2 $organame2 \\
-i ${params.identity} \\
-o $outfile \\
-ob1 $obam1 \\
-aob1 $aobam1 \\
-ob2 $obam2 \\
-aob2 $aobam2 \\
-ed1 ${params.endo1} \\
-ed2 ${params.endo2} \\
-p ${task.cpus}
"""
}
else {
"""
samtools index $bam1
samtools index $bam2
normalizedReadCount -n $name \\
-b1 $bam1 \\
-b2 $bam2 \\
-g1 $genome1 \\
-g2 $genome2 \\
-r1 $organame1 \\
-r2 $organame2 \\
-i ${params.identity} \\
-o $outfile \\
-ob1 $obam1 \\
-ob2 $obam2 \\
-ed1 ${params.endo1} \\
-ed2 ${params.endo2} \\
-p ${task.cpus}
"""
}
}
} else {
process countBp3genomes{
tag "$name"
label 'process_low'
echo true
input:
set val(name), file(abam1), file(abam2), file(abam3), file(bam1), file(bam2), file(bam3) from ( (params.adna ? pmd_aligned1.join(pmd_aligned2).join(pmd_aligned3) : alignment_genome1_pmd.join(alignment_genome2_pmd).join(alignment_genome3_pmd)).join(alignment_genome1).join(alignment_genome2).join(alignment_genome3))
file(genome1) from genome1Size
file(genome2) from genome2Size
file(genome3) from genome3Size
output:
set val(name), file("*.bpc.csv") into bp_count
set val(name), file("*"+params.name1+".ancient.filtered.bam") optional true into ancient_filtered_bam1
set val(name), file("*"+params.name2+".ancient.filtered.bam") optional true into ancient_filtered_bam2
set val(name), file("*"+params.name3+".ancient.filtered.bam") optional true into ancient_filtered_bam3
script:
outfile = name+".bpc.csv"
organame1 = params.name1
organame2 = params.name2
organame3 = params.name3
obam1 = name+"_"+organame1+".filtered.bam"
obam2 = name+"_"+organame2+".filtered.bam"
obam3 = name+"_"+organame3+".filtered.bam"
if (params.adna) {
aobam1 = name+"_"+organame1+".ancient.filtered.bam"
aobam2 = name+"_"+organame2+".ancient.filtered.bam"
aobam3 = name+"_"+organame3+".ancient.filtered.bam"
"""
samtools index $bam1