forked from nf-core/smrnaseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
1074 lines (926 loc) · 35.9 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/smrnaseq
========================================================================================
nf-core/smrnaseq Analysis Pipeline.
#### Homepage / Documentation
https://github.com/nf-core/smrnaseq
----------------------------------------------------------------------------------------
*/
log.info Headers.nf_core(workflow, params.monochrome_logs)
////////////////////////////////////////////////////
/* -- PRINT HELP -- */
////////////////////////////////////////////////////+
def json_schema = "$projectDir/nextflow_schema.json"
if (params.help) {
def command = "nextflow run nf-core/smrnaseq --input '*_R{1,2}.fastq.gz' -profile docker"
log.info NfcoreSchema.params_help(workflow, params, json_schema, command)
exit 0
}
////////////////////////////////////////////////////
/* -- VALIDATE PARAMETERS -- */
////////////////////////////////////////////////////+
if (params.validate_params) {
NfcoreSchema.validateParameters(params, json_schema, log)
}
////////////////////////////////////////////////////
/* -- Collect configuration parameters -- */
////////////////////////////////////////////////////
// Check if genome exists in the config file
if (params.genomes && params.genome && !params.genomes.containsKey(params.genome)) {
exit 1, "The provided genome '${params.genome}' is not available in the iGenomes file. Currently the available genomes are ${params.genomes.keySet().join(', ')}"
}
// Genome options
params.bt_index = params.genome ? params.genomes[ params.genome ].bowtie ?: false : false
params.mirtrace_species = params.genome ? params.genomes[ params.genome ].mirtrace_species ?: false : false
params.fasta = params.genome ? params.genomes[ params.genome ].fasta ?: false : false
params.mirna_gtf = params.mirtrace_species ? "ftp://mirbase.org/pub/mirbase/CURRENT/genomes/${params.mirtrace_species}.gff3" : false
// Define regular variables so that they can be overwritten
clip_r1 = params.clip_r1
three_prime_clip_r1 = params.three_prime_clip_r1
three_prime_adapter = params.three_prime_adapter
protocol = params.protocol
// Presets
if (params.protocol == "illumina"){
clip_r1 = 0
three_prime_clip_r1 = 0
three_prime_adapter = "TGGAATTCTCGGGTGCCAAGG"
} else if (params.protocol == "nextflex"){
clip_r1 = 4
three_prime_clip_r1 = 4
three_prime_adapter = "TGGAATTCTCGGGTGCCAAGG"
} else if (params.protocol == "qiaseq"){
clip_r1 = 0
three_prime_clip_r1 = 0
three_prime_adapter = "AACTGTAGGCACCATCAAT"
} else if (params.protocol == "cats"){
clip_r1 = 3
three_prime_clip_r1 = 0
// three_prime_adapter = "GATCGGAAGAGCACACGTCTG"
three_prime_adapter = "AAAAAAAA"
} else {
//custom protocol
clip_r1 = params.clip_r1
three_prime_clip_r1 = params.three_prime_clip_r1
three_prime_adapter = params.three_prime_adapter
protocol = params.protocol
}
// mirtrace protocol defaults to 'params.protocol' if not set
if (!params.mirtrace_protocol){
mirtrace_protocol = protocol
} else {
mirtrace_protocol = params.mirtrace_protocol
}
if (params.mirna_gtf) {
mirna_gtf = file(params.mirna_gtf, checkIfExists: true)
} else {
mirna_gtf = false
}
// Validate inputs
if (params.skip_mirdeep){
if (params.mature) { mature = file(params.mature, checkIfExists: true) } else { exit 1, "Mature file not found: ${params.mature}" }
if (params.hairpin) { hairpin = file(params.hairpin, checkIfExists: true) } else { exit 1, "Hairpin file not found: ${params.hairpin}" }
indices_mirdeep2 = Channel.empty()
fasta = Channel.empty()
} else {
if (params.references_parsed){
fasta = file("$params.references_parsed/genome.fa", checkIfExists: true)
hairpin = file("$params.references_parsed/hairpin.fa", checkIfExists: true)
mature = file("$params.references_parsed/mature.fa", checkIfExists: true)
indices_mirdeep2 = Channel.fromPath("$params.references_parsed/genome.*.ebwt", checkIfExists: true).ifEmpty { exit 1, "Reference parsed genome indices not found: ${references_parsed}"}
} else {
if (params.mature) { reference_mature = file(params.mature, checkIfExists: true) } else { exit 1, "Mature miRNA fasta file not found: ${params.mature}" }
if (params.hairpin) { reference_hairpin = file(params.hairpin, checkIfExists: true) } else { exit 1, "Hairpin miRNA fasta file not found: ${params.hairpin}" }
if (params.fasta) { reference_genome = file(params.fasta, checkIfExists: true) } else { exit 1, "Reference genome Fasta file not found: ${params.fasta}" }
}
}
if( params.bt_index ){
bt_indices = Channel.fromPath("${params.bt_index}*", checkIfExists: true).ifEmpty { exit 1, "Bowtie1 index directory not found: ${bt_dir}" }
} else if( params.bt_indices ){
bt_indices = Channel.from(params.bt_indices).map{ file(it) }.toList()
} else {
log.info "No Bowtie 1 index supplied - host reference genome analysis will be skipped."
}
if( !params.mirtrace_species ){
exit 1, "Reference species for miRTrace is not defined."
}
// Check AWS batch settings
if (workflow.profile.contains('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 (params.tracedir.startsWith('s3:')) exit 1, 'Specify a local tracedir or run without trace! S3 cannot be used for tracefiles.'
}
// Stage config files
ch_multiqc_config = file("$projectDir/assets/multiqc_config.yaml", checkIfExists: true)
ch_multiqc_custom_config = params.multiqc_config ? Channel.fromPath(params.multiqc_config, checkIfExists: true) : Channel.empty()
ch_output_docs = file("$projectDir/docs/output.md", checkIfExists: true)
ch_output_docs_images = file("$projectDir/docs/images/", checkIfExists: true)
/*
* Create a channel for input read files
*/
if(params.input_paths){
Channel
.from(params.input_paths)
.map { file(it) }
.ifEmpty { exit 1, "params.input_paths was empty - no input files supplied" }
.into { raw_reads_fastqc; raw_reads_trimgalore; raw_reads_mirtrace }
} else {
Channel
.fromPath( params.input )
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.input}" }
.into { raw_reads_fastqc; raw_reads_trimgalore; raw_reads_mirtrace }
}
////////////////////////////////////////////////////
/* -- PRINT PARAMETER SUMMARY -- */
////////////////////////////////////////////////////
log.info NfcoreSchema.params_summary_log(workflow, params, json_schema)
// Header log info
def summary = [:]
if(workflow.revision) summary['Pipeline Release'] = workflow.revision
summary['Run Name'] = workflow.runName
summary['Input'] = params.input
summary['Genome'] = params.genome
summary['Min Trimmed Length'] = params.min_length
summary["Trim 5' R1"] = clip_r1
summary["Trim 3' R1"] = three_prime_clip_r1
summary['miRBase mature'] = params.mature
summary['miRBase hairpin'] = params.hairpin
summary['Reference Genome'] = params.fasta
if(params.bt_index) summary['Bowtie Index for Ref'] = params.bt_index
summary['Save Reference'] = params.save_reference ? 'Yes' : 'No'
summary['Protocol'] = params.protocol
summary['Mirtrace Protocol'] = mirtrace_protocol
summary['miRTrace species'] = params.mirtrace_species
summary["3' adapter"] = three_prime_adapter
summary['Output dir'] = params.outdir
summary['Launch dir'] = workflow.launchDir
summary['Working dir'] = workflow.workDir
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Script dir'] = workflow.projectDir
summary['Config Profile'] = (workflow.profile == 'standard' ? 'UPPMAX' : workflow.profile)
summary['Fasta Ref'] = params.fasta
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['Output dir'] = params.outdir
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 Profile Description'] = params.config_profile_description
if (params.config_profile_contact) summary['Config Profile Contact'] = params.config_profile_contact
if (params.config_profile_url) summary['Config Profile URL'] = params.config_profile_url
summary['Config Files'] = workflow.configFiles.join(', ')
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
}
// Check the hostnames against configured profiles
checkHostname()
Channel.from(summary.collect{ [it.key, it.value] })
.map { k,v -> "<dt>$k</dt><dd><samp>${v ?: '<span style=\"color:#999999;\">N/A</a>'}</samp></dd>" }
.reduce { a, b -> return [a, b].join("\n ") }
.map { x -> """
id: 'nf-core-smrnaseq-summary'
description: " - this information is collected when the pipeline is started."
section_name: 'nf-core/smrnaseq Workflow Summary'
section_href: 'https://github.com/nf-core/smrnaseq'
plot_type: 'html'
data: |
<dl class=\"dl-horizontal\">
$x
</dl>
""".stripIndent() }
.set { ch_workflow_summary }
/*
* Parse software version numbers
*/
process get_software_versions {
publishDir "${params.outdir}/pipeline_info", mode: params.publish_dir_mode,
saveAs: {filename ->
if (filename.indexOf(".csv") > 0) filename
else null
}
output:
file 'software_versions_mqc.yaml' into software_versions_yaml
file "software_versions.csv"
script:
java_mem = ''
if(task.memory){
tmem = task.memory.toBytes()
java_mem = "-Xms${tmem} -Xmx${tmem}"
}
"""
export mirtracejar=\$(dirname \$(which mirtrace))
echo $workflow.manifest.version > v_pipeline.txt
echo $workflow.nextflow.version > v_nextflow.txt
echo \$(R --version 2>&1) > v_R.txt
fastqc --version > v_fastqc.txt
trim_galore --version > v_trim_galore.txt
bowtie --version > v_bowtie.txt
samtools --version > v_samtools.txt
htseq-count -h > v_htseq.txt
fasta_formatter -h > v_fastx.txt
java $java_mem -jar \$mirtracejar/mirtrace.jar --mirtrace-wrapper-name mirtrace --version > v_mirtrace.txt
multiqc --version > v_multiqc.txt
miRDeep2.pl -h > v_mirdeep2.txt
scrape_software_versions.py > software_versions_mqc.yaml
"""
}
if (!params.references_parsed && !params.skip_mirdeep){
/*
* PREPROCESSING - Parse genome.fa and build Bowtie index for the reference genome
*/
process bowtie_indices {
label 'process_medium'
publishDir path: { params.save_reference ? "${params.outdir}/references_parsed" : params.outdir },
saveAs: { params.save_reference ? it : null }, mode: 'copy'
input:
file refgenome from reference_genome
file mature from reference_mature
file hairpin from reference_hairpin
output:
file 'genome.edited.fa' into fasta
file 'genome.*.ebwt' into indices_mirdeep2
file 'hairpin.fa' into hairpin
file 'mature.fa' into mature
script:
"""
# Uncompress FASTA reference files if necessary
MATURE="$mature"
HAIRPIN="$hairpin"
if [ \${MATURE: -3} == ".gz" ]; then
gunzip -f \$MATURE
MATURE=\${MATURE%%.gz}
fi
if [ \${HAIRPIN: -3} == ".gz" ]; then
gunzip -f \$HAIRPIN
HAIRPIN=\${HAIRPIN%%.gz}
fi
# Remove any special base characters from reference genome FASTA file
sed '/^[^>]/s/[^ATGCatgc]/N/g' $refgenome > genome.edited.fa
# Remove spaces from miRBase FASTA files
sed -i 's, ,_,g' \$HAIRPIN
sed -i 's, ,_,g' \$MATURE
# Build bowtie index
bowtie-build genome.edited.fa genome --threads ${task.cpus}
"""
}
}
/*
* PREPROCESSING - Build Bowtie index for mature and hairpin
*/
process make_bowtie_index {
label 'process_medium'
publishDir path: { params.save_reference ? "${params.outdir}/bowtie/reference" : params.outdir },
saveAs: { params.save_reference ? it : null }, mode: 'copy'
input:
file mature from mature
file hairpin from hairpin
output:
file 'mature_idx.*' into mature_index_bowtie
file 'hairpin_idx.*' into hairpin_index_bowtie, hairpin_index_bowtie_2
file 'hairpin_idx.fa' into hairpin_mirtop
script:
"""
seqkit grep -r --pattern \".*${params.mirtrace_species}-.*\" $mature > mature_sps.fa
seqkit seq --rna2dna mature_sps.fa > mature_igenome.fa
fasta_formatter -w 0 -i mature_igenome.fa -o mature_idx.fa
# fasta_nucleotide_changer -d -i mature_igenome.fa -o mature_idx.fa
bowtie-build mature_idx.fa mature_idx --threads ${task.cpus}
seqkit grep -r --pattern \".*${params.mirtrace_species}-.*\" $hairpin > hairpin_sps.fa
seqkit seq --rna2dna hairpin_sps.fa > hairpin_igenome.fa
# fasta_nucleotide_changer -d -i hairpin_igenome.fa -o hairpin_idx.fa
fasta_formatter -w 0 -i hairpin_igenome.fa -o hairpin_idx.fa
bowtie-build hairpin_idx.fa hairpin_idx --threads ${task.cpus}
"""
}
/*
* STEP 1 - FastQC
*/
process fastqc {
label 'process_low'
tag "$reads"
publishDir "${params.outdir}/fastqc", mode: params.publish_dir_mode,
saveAs: { filename ->
filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"
}
when:
!params.skip_qc && !params.skip_fastqc
input:
file reads from raw_reads_fastqc
output:
file '*_fastqc.{zip,html}' into fastqc_results
script:
"""
fastqc --quiet --threads $task.cpus $reads
"""
}
/*
* STEP 2 - Trim Galore!
*/
process trim_galore {
label 'process_low'
tag "$reads"
publishDir "${params.outdir}/trim_galore", mode: 'copy'
input:
file reads from raw_reads_trimgalore
output:
file '*.gz' into trimmed_reads_bowtie, trimmed_reads_collapse, trimmed_reads_bowtie_ref, trimmed_reads_insertsize, trimmed_zipped_reads_mirdeep2
file '*trimming_report.txt' into trimgalore_results
file "*_fastqc.{zip,html}" into trimgalore_fastqc_reports
script:
tg_length = "--length ${params.min_length}"
c_r1 = clip_r1 > 0 ? "--clip_r1 ${clip_r1}" : ''
tpc_r1 = three_prime_clip_r1 > 0 ? "--three_prime_clip_r1 ${three_prime_clip_r1}" : ''
tpa = (protocol == "qiaseq" | protocol == "cats") ? "--adapter ${three_prime_adapter}" : '--small_rna'
"""
trim_galore --adapter ${three_prime_adapter} $tg_length $c_r1 $tpc_r1 --max_length ${params.trim_galore_max_length} --gzip $reads --fastqc
"""
}
/*
* STEP 2.1 - Insertsize
*/
process insertsize {
label 'process_low'
tag "$reads"
publishDir "${params.outdir}/trim_galore/insertsize", mode: 'copy'
input:
file reads from trimmed_reads_insertsize
output:
file '*.insertsize' into insertsize_results
script:
prefix = reads.toString() - ~/(.R1)?(_R1)?(_trimmed)?(\.fq)?(\.fastq)?(\.gz)?$/
"""
awk 'NR%4 == 2 {lengths[length(\$0)]++} END {for (l in lengths) {print l, lengths[l]}}' <(zcat $reads) >${prefix}.insertsize
"""
}
/*
* STEP 2.2 - Collapse
*/
process collapse {
label 'process_medium'
tag "$reads"
input:
file reads from trimmed_reads_collapse
output:
file 'final/*.fastq' into collapsed_fasta
script:
prefix = reads.toString() - '_trimmed.fq.gz'
"""
seqcluster collapse -f $reads -m 1 --min_size 15 -o collapsed
mkdir final
mv collapsed/${prefix}_trimmed_trimmed.fastq final/${prefix}.fastq
"""
}
/*
* STEP 3 - Bowtie miRBase mature miRNA
*/
process bowtie_miRBase_mature {
label 'process_medium'
tag "$reads"
publishDir "${params.outdir}/bowtie/miRBase_mature", mode: params.publish_dir_mode, pattern: '*.mature_unmapped.fq.gz'
input:
file reads from trimmed_reads_bowtie
file index from mature_index_bowtie
output:
file '*.mature.bam' into miRBase_mature_bam
file '*.mature_unmapped.fq.gz' into mature_unmapped_reads
script:
index_base = index.toString().tokenize(' ')[0].tokenize('.')[0]
prefix = reads.toString() - ~/(.R1)?(_R1)?(_trimmed)?(\.fq)?(\.fastq)?(\.gz)?$/
seq_center = params.seq_center ? "--sam-RG ID:${prefix} --sam-RG 'CN:${params.seq_center}'" : ''
"""
bowtie \\
$index_base \\
-q <(zcat $reads) \\
-p ${task.cpus} \\
-t \\
-k 50 \\
--best \\
--strata \\
-e 99999 \\
--chunkmbs 2048 \\
--un ${prefix}.mature_unmapped.fq \\
-S $seq_center \\
| samtools view -bS - > ${prefix}.mature.bam
gzip ${prefix}.mature_unmapped.fq
"""
}
/*
* STEP 4 - Bowtie against miRBase hairpin
*/
process bowtie_miRBase_hairpin {
label 'process_medium'
tag "$reads"
publishDir "${params.outdir}/bowtie/miRBase_hairpin", mode: params.publish_dir_mode, pattern: '*.hairpin_unmapped.fq.gz'
input:
file reads from mature_unmapped_reads
file index from hairpin_index_bowtie
output:
file '*.hairpin.bam' into miRBase_hairpin_bam, miRBase_hairpin_bam_mirtop
file '*.hairpin_unmapped.fq.gz' into hairpin_unmapped_reads
script:
index_base = index.toString().tokenize(' ')[0].tokenize('.')[0]
prefix = reads.toString() - '.mature_unmapped.fq.gz'
seq_center = params.seq_center ? "--sam-RG ID:${prefix} --sam-RG 'CN:${params.seq_center}'" : ''
"""
bowtie \\
$index_base \\
-p ${task.cpus} \\
-t \\
-a \\
--best \\
--strata \\
-e 99999 \\
--chunkmbs 2048 \\
-q <(zcat $reads) \\
--un ${prefix}.hairpin_unmapped.fq \\
-S $seq_center \\
| samtools view -bS - > ${prefix}.hairpin.bam
gzip ${prefix}.hairpin_unmapped.fq
"""
}
/*
* STEP 4.1 - Bowtie against miRBase hairpin with collapsed reads
*/
process bowtie_miRBase_hairpin_collapsed {
label 'process_medium'
tag "$reads"
input:
file reads from collapsed_fasta
file index from hairpin_index_bowtie_2
output:
file '*.bam' into miRBase_hairpin_collapse_bam
script:
index_base = index.toString().tokenize(' ')[0].tokenize('.')[0]
prefix = reads.baseName
seq_center = params.seq_center ? "--sam-RG ID:${prefix} --sam-RG 'CN:${params.seq_center}'" : ''
"""
bowtie \\
$index_base \\
-p ${task.cpus} \\
-t \\
-k 50 \\
-a \\
--best \\
--strata \\
-e 99999 \\
--chunkmbs 2048 \\
-q <(cat $reads) \\
-S $seq_center \\
| samtools view -bS - > ${prefix}.bam
"""
}
/*
* STEP 5.1 - Post-alignment processing for miRBase mature and hairpin
*/
def wrap_mature_and_hairpin = { file ->
if ( file.contains("mature") ) return "miRBase_mature/$file"
if ( file.contains("hairpin") ) return "miRBase_hairpin/$file"
}
process mirna_post_alignment {
label 'process_medium'
tag "$input"
publishDir "${params.outdir}/bowtie", mode: params.publish_dir_mode, saveAs: wrap_mature_and_hairpin
input:
file input from miRBase_mature_bam.mix(miRBase_hairpin_bam)
output:
file "${input.baseName}.stats" into miRBase_counts
file "*.{flagstat,idxstats,stats}" into ch_sort_bam_flagstat_mqc
file "${input.baseName}.sorted.bam" into miRBase_bam
file "${input.baseName}.sorted.bam.bai" into miRBase_bai
script:
"""
samtools sort ${input.baseName}.bam -o ${input.baseName}.sorted.bam
samtools index ${input.baseName}.sorted.bam
samtools idxstats ${input.baseName}.sorted.bam > ${input.baseName}.stats
samtools flagstat ${input.baseName}.sorted.bam > ${input.baseName}.sorted.bam.flagstat
samtools stats ${input.baseName}.sorted.bam > ${input.baseName}.sorted.bam.stats
"""
}
/*
* STEP 5.2 - edgeR miRBase feature counts processing
*/
process edgeR_mirna {
label 'process_low'
label 'process_ignore'
publishDir "${params.outdir}/edgeR", mode: params.publish_dir_mode, saveAs: wrap_mature_and_hairpin
input:
file input_files from miRBase_counts.toSortedList()
output:
file '*.{txt,pdf,csv}' into edgeR_miRBase_results
script:
"""
edgeR_miRBase.r $input_files
"""
}
/*
* STEP 5.3 - miRNA format conversion to mirGFF3
*/
process mirtop_bam_hairpin {
label 'process_medium'
tag "$input"
publishDir "${params.outdir}", mode: 'copy'
when:
mirna_gtf
input:
file input from miRBase_hairpin_collapse_bam.collect()
file hairpin from hairpin_mirtop
file gtf from mirna_gtf
output:
file "mirtop/mirtop.gff" into mirtop_gff
file "mirtop/mirtop.tsv" into mirtop_tsv
file "mirtop/mirna.tsv" into mirna_tsv
file "mirtop/mirtop_rawData.tsv" into isomir_tsv
script:
"""
mirtop gff --hairpin $hairpin --gtf $gtf -o mirtop --sps $params.mirtrace_species $input
mirtop counts --hairpin $hairpin --gtf $gtf -o mirtop --sps $params.mirtrace_species --add-extra --gff mirtop/mirtop.gff
mirtop export --format isomir --hairpin $hairpin --gtf $gtf --sps $params.mirtrace_species -o mirtop mirtop/mirtop.gff
collapse_mirtop.r mirtop/mirtop.tsv
"""
}
/*
* STEP 6.1 and 6.2 IF A GENOME SPECIFIED ONLY!
*/
if( params.bt_index ) {
/*
* STEP 6.1 - Bowtie 1 against reference genome
*/
process bowtie_ref {
label 'process_high'
tag "$reads"
publishDir "${params.outdir}/bowtie_ref", mode: 'copy'
input:
file reads from trimmed_reads_bowtie_ref
file indices from bt_indices.collect()
output:
file '*.genome.bam' into bowtie_bam, bowtie_bam_for_unmapped
script:
index_base = indices[0].toString() - ~/.rev.\d.ebwt?/ - ~/.\d.ebwt?/
prefix = reads.toString() - ~/(.R1)?(_R1)?(_trimmed)?(\.fq)?(\.fastq)?(\.gz)?$/
seq_center = params.seq_center ? "--sam-RG ID:${prefix} --sam-RG 'CN:${params.seq_center}'" : ''
"""
bowtie \\
$index_base \\
-q <(zcat $reads) \\
-p ${task.cpus} \\
-t \\
-k 50 \\
--best \\
--strata \\
-e 99999 \\
--chunkmbs 2048 \\
-S $seq_center \\
| samtools view -bS - > ${prefix}.genome.bam
"""
}
process genome_post_alignment {
label 'process_low'
tag "$input"
publishDir "${params.outdir}/bowtie_ref", mode: 'copy'
input:
file input from bowtie_bam
output:
file "*.{flagstat,idxstats,stats}" into ch_genome_bam_flagstat_mqc
script:
"""
samtools sort ${input.baseName}.bam -o ${input.baseName}.sorted.bam
samtools index ${input.baseName}.sorted.bam
samtools idxstats ${input.baseName}.sorted.bam > ${input.baseName}.stats
samtools flagstat ${input.baseName}.sorted.bam > ${input.baseName}.sorted.bam.flagstat
samtools stats ${input.baseName}.sorted.bam > ${input.baseName}.sorted.bam.stats
"""
}
/*
* STEP 6.2 - Statistics about unmapped reads against ref genome
*/
process bowtie_unmapped {
label 'process_ignore'
label 'process_medium'
tag "${input_files[0].baseName}"
publishDir "${params.outdir}/bowtie_ref/unmapped", mode: 'copy'
input:
file input_files from bowtie_bam_for_unmapped.toSortedList()
output:
file 'unmapped_refgenome.txt' into bowtie_unmapped
script:
"""
for i in $input_files
do
printf "\${i}\t"
samtools view -c -f0x4 \${i}
done > unmapped_refgenome.txt
"""
}
} else{
ch_genome_bam_flagstat_mqc = Channel.empty()
}
/*
* STEP 7.0 - unzip trim galore reads
*/
process uncompress_trimmed_reads{
label 'process_low'
when:
!params.skip_mirdeep
input:
path reads from trimmed_zipped_reads_mirdeep2
output:
path "$unzip" into trimmed_reads_mirdeep2
script:
unzip = reads.toString() - '.gz'
"""
pigz -f -d -p $task.cpus $reads
"""
}
/*
* STEP 7.1 - miRDeep2 mapper
*/
process mapper_mirdeep2 {
label 'process_medium'
tag "$reads"
publishDir "${params.outdir}/mirdeep2/mapper", mode: 'copy'
when:
!params.skip_mirdeep
input:
file reads from trimmed_reads_mirdeep2
file indices from indices_mirdeep2.collect()
output:
file '*_collapsed.fa' into mirdeep_reads_collapsed
file '*reads_vs_refdb.arf' into reads_vs_refdb
script:
index_base = indices.toString().tokenize(' ')[0].tokenize('.')[0]
"""
mapper.pl \\
$reads \\
-e \\
-h \\
-i \\
-j \\
-m \\
-p $index_base \\
-s ${reads.baseName}_collapsed.fa \\
-t ${reads.baseName}_reads_vs_refdb.arf \\
-o 4
"""
}
/*
* STEP 7.2 - miRDeep2 novel mirnas
*/
process mirdeep2 {
label 'process_medium'
publishDir "${params.outdir}/mirdeep2/mirdeep", mode: 'copy'
when:
!params.skip_mirdeep
input:
file refgenome from fasta
file reads_collapsed from mirdeep_reads_collapsed
file reads_vs_refdb from reads_vs_refdb
file mature from mature
file hairpin from hairpin
output:
file 'result*.{bed,csv,html}'
script:
"""
perl -ane 's/[ybkmrsw]/N/ig;print;' $hairpin > hairpin_ok.fa
sed 's/ .*//' $refgenome | awk '\$1 ~ /^>/ {gsub(/_/,"",\$1); print; next} {print}' > genome_nowhitespace.fa
miRDeep2.pl \\
$reads_collapsed \\
genome_nowhitespace.fa \\
$reads_vs_refdb \\
$mature \\
none \\
hairpin_ok.fa \\
-d \\
-z _${reads_collapsed.simpleName}
"""
}
/*
* STEP 8 - miRTrace
*/
process mirtrace {
label 'process_medium'
tag "$reads"
publishDir "${params.outdir}/miRTrace", mode: 'copy'
input:
file reads from raw_reads_mirtrace.collect()
output:
file '*mirtrace' into mirtrace_results
script:
primer = (mirtrace_protocol=="cats") ? " " : " --adapter $three_prime_adapter "
java_mem = ''
if(task.memory){
tmem = task.memory.toBytes()
java_mem = "-Xms${tmem} -Xmx${tmem}"
}
"""
export mirtracejar=\$(dirname \$(which mirtrace))
for i in $reads
do
path=\$(realpath \${i})
prefix=\$(echo \${i} | sed -e 's/.gz//' -e 's/.fastq//' -e 's/.fq//' -e 's/_val_1//' -e 's/_trimmed//' -e 's/_R1//' -e 's/.R1//')
echo \$path","\$prefix
done > mirtrace_config
java $java_mem -jar \$mirtracejar/mirtrace.jar --mirtrace-wrapper-name mirtrace qc \\
--species $params.mirtrace_species \\
$primer \\
--protocol $mirtrace_protocol \\
--config mirtrace_config \\
--write-fasta \\
--output-dir mirtrace \\
--force
"""
}
/*
* STEP 9 - MultiQC
*/
process multiqc {
publishDir "${params.outdir}/MultiQC", mode: params.publish_dir_mode
when:
!params.skip_qc && !params.skip_multiqc
input:
file (multiqc_config) from ch_multiqc_config
file (mqc_custom_config) from ch_multiqc_custom_config.collect().ifEmpty([])
file ('fastqc/*') from fastqc_results.collect()
file ('trim_galore/*') from trimgalore_results.collect()
file ('mirtrace/*') from mirtrace_results.collect()
file ('samtools/*') from ch_sort_bam_flagstat_mqc.collect()
file ('samtools_genome/*') from ch_genome_bam_flagstat_mqc.collect().ifEmpty([])
file ('software_versions/*') from software_versions_yaml.collect()
file workflow_summary from ch_workflow_summary.collectFile(name: "workflow_summary_mqc.yaml")
output:
file "*multiqc_report.html" into ch_multiqc_report
file "*_data"
script:
rtitle = ''
rfilename = ''
if (!(workflow.runName ==~ /[a-z]+_[a-z]+/)) {
rtitle = "--title \"${workflow.runName}\""
rfilename = "--filename " + workflow.runName.replaceAll('\\W','_').replaceAll('_+','_') + "_multiqc_report"
}
custom_config_file = params.multiqc_config ? "--config $mqc_custom_config" : ''
"""
multiqc . -f $rtitle $rfilename $custom_config_file -m bowtie1 -m samtools -m cutadapt -m fastqc -m custom_content
"""
}
/*
* STEP 10 - Output Description HTML
*/
process output_documentation {
publishDir "${params.outdir}/pipeline_info", mode: params.publish_dir_mode
input:
file output_docs from ch_output_docs
file images from ch_output_docs_images
output:
file 'results_description.html'
script:
"""
markdown_to_html.py $output_docs -o results_description.html
"""
}
/*
* Completion e-mail notification
*/
workflow.onComplete {
// Set up the e-mail variables
def subject = "[nf-core/smrnaseq] Successful: $workflow.runName"
if (!workflow.success) {
subject = "[nf-core/smrnaseq] FAILED: $workflow.runName"
}
def email_fields = [:]
email_fields['version'] = workflow.manifest.version
email_fields['runName'] = workflow.runName
email_fields['success'] = workflow.success
email_fields['dateComplete'] = workflow.complete
email_fields['duration'] = workflow.duration
email_fields['exitStatus'] = workflow.exitStatus
email_fields['errorMessage'] = (workflow.errorMessage ?: 'None')
email_fields['errorReport'] = (workflow.errorReport ?: 'None')
email_fields['commandLine'] = workflow.commandLine
email_fields['projectDir'] = workflow.projectDir
email_fields['summary'] = summary
email_fields['summary']['Date Started'] = workflow.start
email_fields['summary']['Date Completed'] = workflow.complete
email_fields['summary']['Pipeline script file path'] = workflow.scriptFile
email_fields['summary']['Pipeline script hash ID'] = workflow.scriptId
if (workflow.repository) email_fields['summary']['Pipeline repository Git URL'] = workflow.repository
if (workflow.commitId) email_fields['summary']['Pipeline repository Git Commit'] = workflow.commitId
if (workflow.revision) email_fields['summary']['Pipeline Git branch/tag'] = workflow.revision
email_fields['summary']['Nextflow Version'] = workflow.nextflow.version
email_fields['summary']['Nextflow Build'] = workflow.nextflow.build
email_fields['summary']['Nextflow Compile Timestamp'] = workflow.nextflow.timestamp
// On success try attach the multiqc report
def mqc_report = null
try {
if (workflow.success) {
mqc_report = ch_multiqc_report.getVal()
if (mqc_report.getClass() == ArrayList) {
log.warn "[nf-core/smrnaseq] Found multiple reports from process 'multiqc', will use only one"
mqc_report = mqc_report[0]
}
}
} catch (all) {
log.warn "[nf-core/smrnaseq] Could not attach MultiQC report to summary email"
}
// Check if we are only sending emails on failure
email_address = params.email
if (!params.email && params.email_on_fail && !workflow.success) {
email_address = params.email_on_fail
}
// Render the TXT template
def engine = new groovy.text.GStringTemplateEngine()
def tf = new File("$projectDir/assets/email_template.txt")
def txt_template = engine.createTemplate(tf).make(email_fields)
def email_txt = txt_template.toString()
// Render the HTML template
def hf = new File("$projectDir/assets/email_template.html")
def html_template = engine.createTemplate(hf).make(email_fields)
def email_html = html_template.toString()
// Render the sendmail template
def smail_fields = [ email: email_address, subject: subject, email_txt: email_txt, email_html: email_html, projectDir: "$projectDir", mqcFile: mqc_report, mqcMaxSize: params.max_multiqc_email_size.toBytes() ]
def sf = new File("$projectDir/assets/sendmail_template.txt")
def sendmail_template = engine.createTemplate(sf).make(smail_fields)
def sendmail_html = sendmail_template.toString()
// Send the HTML e-mail
if (email_address) {
try {