-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
1035 lines (859 loc) · 32.7 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
/*
Copyright Institut Curie 2020
This software is a computer program whose purpose is to analyze high-throughput sequencing data.
You can use, modify and/ or redistribute the software under the terms of license (see the LICENSE file for more details).
The software is distributed in the hope that it will be useful, but "AS IS" WITHOUT ANY WARRANTY OF ANY KIND.
Users are therefore encouraged to test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data.
The fact that you are presently reading this means that you have had knowledge of the license and that you accept its terms.
This script is based on the nf-core guidelines. See https://nf-co.re/ for more information
*/
/*
========================================================================================
SmartSeq3
========================================================================================
#### Homepage / Documentation
https://gitlab.curie.fr/sc-platform/smartseq3
----------------------------------------------------------------------------------------
*/
// File with text to display when a developement version is used
devMessageFile = file("$baseDir/assets/devMessage.txt")
def helpMessage() {
if ("${workflow.manifest.version}" =~ /dev/ ){
devMess = file("$baseDir/assets/devMessage.txt")
log.info devMessageFile.text
}
log.info """
SmartSeq3 v${workflow.manifest.version}
======================================================================
Usage:
nextflow run main.nf --reads '*_R{1,2}.fastq.gz' --genome 'hg19' -profile conda
nextflow run main.nf --samplePlan samplePlan --genome 'hg19' -profile conda
Mandatory arguments:
--reads [file] Path to input data (must be surrounded with quotes)
--samplePlan [file] Path to sample plan input file (cannot be used with --reads)
--genome [str] Name of genome reference
-profile [str] Configuration profile to use. test / conda / multiconda / path / multipath / singularity / docker / cluster (see below)
Inputs:
--starIndex [dir] Index for STAR aligner
--singleEnd [bool] Specifies that the input is single-end reads
Skip options: All are false by default
--skipSoftVersion [bool] Do not report software version. Default is false.
--skipMultiQC [bool] Skips MultiQC. Default is false.
--skipGeneCov [bool] Skips calculating genebody coverage. Default is false.
--skipSatCurves [bool] Skips saturation curves. Default is false.
Genomes: If not specified in the configuration file or if you wish to overwrite any of the references given by the --genome field
--genomeAnnotationPath [file] Path to genome annotation folder
Other options:
--outDir [file] The output directory where the results will be saved
-name [str] Name for the pipeline run. If not specified, Nextflow will automatically generate a random mnemonic
=======================================================
Available Profiles
-profile test Set up the test dataset
-profile conda Build a single conda for with all tools used by the different processes before running the pipeline
-profile multiconda Build a new conda environment for each tools used by the different processes before running the pipeline
-profile path Use the path defined in the configuration for all tools
-profile multipath Use the paths defined in the configuration for each tool
-profile docker Use the Docker containers for each process
-profile singularity Use the singularity images for each process
-profile cluster Run the workflow on the cluster, instead of locally
""".stripIndent()
}
/**********************************
* SET UP CONFIGURATION VARIABLES *
**********************************/
// Show help message
if (params.help){
helpMessage()
exit 0
}
// Configurable reference genomes
if (params.genomes && params.genome && !params.genomes.containsKey(params.genome)) {
exit 1, "The provided genome '${params.genome}' is not available in the genomes.config file. Currently the available genomes are ${params.genomes.keySet().join(", ")}"
}
//Stage config files
Channel
.fromPath(params.multiqcConfig, checkIfExists: true)
.set{chMultiqcConfig}
chOutputDocs = file("$baseDir/docs/output.md", checkIfExists: true)
chOutputDocsImages = file("$baseDir/docs/images/", checkIfExists: true)
//Has the run name been specified by the user?
//This has the bonus effect of catching both -name and --name
customRunName = params.name
if( !(workflow.runName ==~ /[a-z]+_[a-z]+/) ){
customRunName = workflow.runName
}
/************
* CHANNELS *
************/
// Validate inputs
if ((params.reads && params.samplePlan) || (params.readPaths && params.samplePlan)){
exit 1, "Input reads must be defined using either '--reads' or '--samplePlan' parameters. Please choose one way."
}
if ( params.metadata ){
Channel
.fromPath( params.metadata )
.ifEmpty { exit 1, "Metadata file not found: ${params.metadata}" }
.set { chMetadata }
}else{
chMetadata=Channel.empty()
}
// Configurable reference genomes
genomeRef = params.genome
params.starIndex = genomeRef ? params.genomes[ genomeRef ].starIndex ?: false : false
if (params.starIndex){
Channel
.fromPath(params.starIndex, checkIfExists: true)
.ifEmpty {exit 1, "STAR index file not found: ${params.starIndex}"}
.set { chStar }
} else {
exit 1, "STAR index file not found: ${params.starIndex}"
}
params.gtf = genomeRef ? params.genomes[ genomeRef ].gtf ?: false : false
if (params.gtf) {
Channel
.fromPath(params.gtf, checkIfExists: true)
.into { chGtfSTAR; chGtfFC }
}else {
exit 1, "GTF annotation file not not found: ${params.gtf}"
}
params.bed12 = genomeRef ? params.genomes[ genomeRef ].bed12 ?: false : false
if (params.bed12) {
Channel
.fromPath(params.bed12)
.ifEmpty { exit 1, "BED12 annotation file not found: ${params.bed12}" }
.set { chBedGeneCov }
}else {
exit 1, "GTF annotation file not not found: ${params.bed12}"
}
// Create a channel for input read files
if(params.samplePlan){
if(params.singleEnd){
Channel
.from(file("${params.samplePlan}"))
.splitCsv(header: false)
.map{ row -> [ row[0], [file(row[2])]] }
.into { rawReadsFastqc; chMergeReadsFastq }
}else{
Channel
.from(file("${params.samplePlan}"))
.splitCsv(header: false)
.map{ row -> [ row[0], [file(row[2]), file(row[3])]] }
.into { rawReadsFastqc ; chMergeReadsFastq}
}
params.reads=false
}
else if(params.readPaths){
if(params.singleEnd){
Channel
.from(params.readPaths)
.map { row -> [ row[0], [file(row[1][0])]] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied." }
.into { rawReadsFastqc ; chMergeReadsFastq}
} else {
Channel
.from(params.readPaths)
.map { row -> [ row[0], [file(row[1][0]), file(row[1][1])]] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied." }
.into { rawReadsFastqc ; chMergeReadsFastq}
}
} else {
Channel
.fromFilePairs( params.reads, size: params.singleEnd ? 1 : 2 )
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.reads}\nNB: Path needs to be enclosed in quotes!\nNB: Path requires at least one * wildcard!\nIf this is single-end data, please specify --singleEnd on the command line." }
.into { rawReadsFastqc ; chMergeReadsFastq}
}
// Make sample plan if not available
if (params.samplePlan){
Channel
.fromPath(params.samplePlan)
.into {chSplan; chSplanCheck}
}else if(params.readPaths){
if (params.singleEnd){
Channel
.from(params.readPaths)
.collectFile() {
item -> ["sample_plan.csv", item[0] + ',' + item[0] + ',' + item[1][0] + '\n']
}
.into{ chSplan; chSplanCheck }
}else{
Channel
.from(params.readPaths)
.collectFile() {
item -> ["sample_plan.csv", item[0] + ',' + item[0] + ',' + item[1][0] + ',' + item[1][1] + '\n']
}
.into{ chSplan; chSplanCheck }
}
} else if(params.bamPaths){
Channel
.from(params.bamPaths)
.collectFile() {
item -> ["sample_plan.csv", item[0] + ',' + item[0] + ',' + item[1][0] + '\n']
}
.into{ chSplan; chSplanCheck }
params.aligner = false
} else {
if (params.singleEnd){
Channel
.fromFilePairs( params.reads, size: 1 )
.collectFile() {
item -> ["sample_plan.csv", item[0] + ',' + item[0] + ',' + item[1][0] + '\n']
}
.into { chSplan; chSplanCheck }
}else{
Channel
.fromFilePairs( params.reads, size: 2 )
.collectFile() {
item -> ["sample_plan.csv", item[0] + ',' + item[0] + ',' + item[1][0] + ',' + item[1][1] + '\n']
}
.into { chSplan; chSplanCheck }
}
}
/*******************
* Header log info *
*******************/
if ("${workflow.manifest.version}" =~ /dev/ ){
log.info devMessageFile.text
}
log.info """=======================================================
smartSeq3 v${workflow.manifest.version}
======================================================="""
def summary = [:]
summary['Pipeline Name'] = 'SmartSeq3'
summary['Pipeline Version'] = workflow.manifest.version
summary['Run Name'] = customRunName ?: workflow.runName
summary['Command Line'] = workflow.commandLine
if (params.samplePlan) {
summary['SamplePlan'] = params.samplePlan
}else{
summary['Reads'] = params.reads
}
summary['Genome'] = params.genome
summary['Annotation'] = params.genomeAnnotationPath
summary['Max Memory'] = params.maxMemory
summary['Max CPUs'] = params.maxCpus
summary['Max Time'] = params.maxTime
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Working dir'] = workflow.workDir
summary['Output dir'] = params.outDir
summary['Config Profile'] = workflow.profile
log.info summary.collect { k,v -> "${k.padRight(15)}: $v" }.join("\n")
log.info "========================================="
/*
* Reads Mapping
*/
process getTaggedSeq{
tag "${prefix}"
label 'seqkit'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/getTaggedSeq", mode: 'copy'
input:
set val(prefix), file(reads) from rawReadsFastqc
output:
set val(prefix), file("*_tagged.R1.fastq.gz"), file("*_tagged.R2.fastq.gz") into chTaggedFastq
set val(prefix), file("*_taggedReadIDs.txt") into chTaggedIDs
script:
"""
# Get tag sequences in R1 == umi sequences
seqkit grep -j ${task.cpus} --by-seq --pattern "TGCGCAATG" ${reads[0]} -o ${prefix}_tagged.R1.fastq.gz
#exctract ids
seqkit seq -j ${task.cpus} -n -i ${prefix}_tagged.R1.fastq.gz -o ${prefix}_taggedReadIDs.txt
# create R2
seqkit grep -j ${task.cpus} -f ${prefix}_taggedReadIDs.txt ${reads[1]} -o ${prefix}_tagged.R2.fastq.gz
"""
}
process umiExtraction {
tag "${prefix}"
label 'umiTools'
label 'highCpu'
label 'highMem'
publishDir "${params.outDir}/umiExtraction", mode: 'copy'
input:
set val(prefix), file(taggedR1), file(taggedR2) from chTaggedFastq
output:
set val(prefix), file("*_UMIsExtracted.R1.fastq.gz"), file("*_UMIsExtracted.R2.fastq.gz") into chUmiExtracted
set val(prefix), file("*_umiExtract.log") into chUmiExtractedLog
file("v_umi_tools.txt") into chUmiToolsVersion
script:
"""
# Extract sequences that have tag+UMI+GGG and add UMI to read names (NB: other sequences are deleted)
# If no umi is find, the reads is leave without changement
umi_tools extract --extract-method=regex --bc-pattern='(?P<discard_1>.*ATTGCGCAATG)(?P<umi_1>.{$params.umi_size})(?P<discard_2>GGG).*' \\
--stdin=${taggedR1} --stdout=${prefix}_UMIsExtracted.R1.fastq.gz \\
--read2-in=${taggedR2} --read2-out=${prefix}_UMIsExtracted.R2.fastq.gz \\
--log=${prefix}_umiExtract.log
umi_tools --version &> v_umi_tools.txt
"""
}
process mergeReads {
tag "${prefix}"
label 'seqkit'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/mergeReads", mode: 'copy'
input:
set val(prefix), file(reads), file(umiReads_R1), file(umiReads_R2), file(taggedReadIDs) from chMergeReadsFastq.join(chUmiExtracted).join(chTaggedIDs)
output:
set val(prefix), file("*_totReads.R1.fastq.gz"), file("*_totReads.R2.fastq.gz") into chMergeReads
set val(prefix), file("*_umisReadsIDs.txt") into chUmiReadsIDs
set val(prefix), file("*_pUMIs.txt") into chCountSummaryExtUMI
set val(prefix), file("*_totReads.txt") into chTotReads
file("v_seqkit.txt") into chSeqkitVersion
script:
"""
# Get UMI read IDs (with UMIs in names for separateReads process)
seqkit seq -j ${task.cpus} -n -i ${umiReads_R1} > ${prefix}_umisReadsIDs.txt
# Extract non UMI reads
seqkit grep -j ${task.cpus} -v -f ${taggedReadIDs} ${reads[0]} -o ${prefix}_nonUMIs.R1.fastq
seqkit grep -j ${task.cpus} -v -f ${taggedReadIDs} ${reads[1]} -o ${prefix}_nonUMIs.R2.fastq
# Merge non umi reads + correct umi reads (with umi sequence in read names) (reads without the exact pattern: tag+UMI+GGG are through out)
cat <(gzip -cd ${umiReads_R1}) > ${prefix}_totReads.R1.fastq
cat ${prefix}_nonUMIs.R1.fastq >> ${prefix}_totReads.R1.fastq
cat <(gzip -cd ${umiReads_R2}) > ${prefix}_totReads.R2.fastq
cat ${prefix}_nonUMIs.R2.fastq >> ${prefix}_totReads.R2.fastq
## Save % of correct UMIs reads (do not take into account all tagged sequences but only tag+UMI+GGG)
nb_lines=`wc -l < <(gzip -cd ${reads[0]})`
nb_totreads=\$(( \$nb_lines / 4 ))
nb_umis=`wc -l < ${prefix}_umisReadsIDs.txt`
echo "percentUMI:\$(( \$nb_umis * 100 / \$nb_totreads ))" > ${prefix}_pUMIs.txt
echo "totReads: \$nb_totreads" > ${prefix}_totReads.txt
seqkit --help | grep Version > v_seqkit.txt
gzip ${prefix}_totReads.R2.fastq ${prefix}_totReads.R1.fastq
rm *.fastq
"""
}
process trimReads{
tag "${prefix}"
label 'cutadapt'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/trimReads", mode: 'copy'
input:
set val(prefix), file(totReadsR1), file(totReadsR2) from chMergeReads
output:
set val(prefix), file("*_trimmed.R1.fastq.gz"), file("*_trimmed.R2.fastq.gz") into chTrimmedReads
set val(prefix), file("*_trimmed.log") into chtrimmedReadsLog
file("v_cutadapt.txt") into chCutadaptVersion
script:
"""
# delete linker + polyA queue
cutadapt -G GCATACGAT{30} --minimum-length=15 --cores=${task.cpus} -o ${prefix}_trimmed.R1.fastq.gz -p ${prefix}_trimmed.R2.fastq.gz ${totReadsR1} ${totReadsR2} > ${prefix}_trimmed.log
cutadapt --version &> v_cutadapt.txt
"""
}
// From nf-core
// Function that checks the alignment rate of the STAR output
// and returns true if the alignment passed and otherwise false
skippedPoorAlignment = []
def checkStarLog(logs) {
def percentAligned = 0;
logs.eachLine { line ->
if ((matcher = line =~ /Uniquely mapped reads %\s*\|\s*([\d\.]+)%/)) {
percentAligned = matcher[0][1]
}else if ((matcher = line =~ /Uniquely mapped reads number\s*\|\s*([\d\.]+)/)) {
numAligned = matcher[0][1]
}
}
logname = logs.getBaseName() - 'Log.final'
if(percentAligned.toFloat() <= '2'.toFloat() || numAligned.toInteger() <= 6000.toInteger() ){
log.info "#################### VERY POOR ALIGNMENT RATE OR TOO LOW NUMBER OF READS! IGNORING FOR FURTHER DOWNSTREAM ANALYSIS! ($logname) >> ${percentAligned}% <<"
skippedPoorAlignment << logname
return false
} else {
log.info " Passed alignment > star ($logname) >> ${percentAligned}% <<"
return true
}
}
// Update input channel
chStarRawReads = Channel.empty()
chStarRawReads = chTrimmedReads
process readAlignment {
tag "${prefix}"
label 'star'
label 'extraCpu'
label 'extraMem'
publishDir "${params.outDir}/readAlignment", mode: 'copy'
input :
file genomeIndex from chStar.collect()
file genomeGtf from chGtfSTAR.collect()
set val(prefix), file(trimmedR1) , file(trimmedR2) from chStarRawReads
output :
set file("${prefix}Log.final.out"), file("${prefix}Aligned.sortedByCoord.out.bam") into chAlignBam
file "*.out" into chAlignmentLogs
file("v_star.txt") into chStarVersion
script:
"""
STAR \
--genomeDir $genomeIndex \
--readFilesIn <(gzip -cd ${trimmedR1} ${trimmedR2}) \
--runThreadN ${task.cpus} \
--outFilterMultimapNmax 1 \
--outFileNamePrefix ${prefix} \
--outSAMtype BAM SortedByCoordinate \
--clip3pAdapterSeq CTGTCTCTTATACACATCT \
--limitSjdbInsertNsj 2000000 \
--sjdbGTFfile $genomeGtf --outFilterIntronMotifs RemoveNoncanonicalUnannotated
# outFilterMultimapNmax = max nb of loci the read is allowed to map to. If more, the read is concidered "map to too many loci".
# clip3pAdapterSeq = cut 3' remaining illumina adaptater (~1-2%)
# limitSjdbInsertNsj = max number of junctions to be insterted to the genome (those known (annotated) + those not annot. but found in many reads).
# Default is 1 000 000. By increasing it, more new junctions can be discovered.
# outFilterIntronMotifs = delete non annotated (not in genomeGtf) + non-canonical junctions.
# Non-canonical but annot. or canonical but not annot. will be kept.
# NB: Canonical <=> juctions describe as having GT/AG, GC/AG or AT/AC (donor/acceptor) dinucleotide combination.
# Non-canonical are all other dinucleotide combinations.
STAR --version &> v_star.txt
"""
}
// Filter removes all 'aligned' channels that fail the check
chAlignBam
.filter { logs, bams -> checkStarLog(logs) }
.map { logs, bams -> bams }
.dump (tag:'starbams')
.set { chAlignBamCheck }
process readAssignment {
tag "${prefix}"
label 'featureCounts'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/readAssignment", mode: 'copy'
input :
file(alignedBam) from chAlignBamCheck
file(genome) from chGtfFC.collect()
output :
set val(prefix), file("*featureCounts.bam") into chAssignBam
file "*.summary" into chAssignmentLogs
file("v_featurecounts.txt") into chFCversion
script:
prefix = alignedBam[0].toString() - ~/(Aligned.sortedByCoord.out)?(.bam)?$/
"""
featureCounts -p \
-a ${genome} \
-o ${prefix}_counts \
-T ${task.cpus} \
-R BAM \
-g gene_name \
${alignedBam}
featureCounts -v &> v_featurecounts.txt
"""
}
process sortAndIndexBam {
tag "${prefix}"
label 'samtools'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/sortBam", mode: 'copy'
input:
set val(prefix), file(assignBam) from chAssignBam
output:
set val(prefix), file("*_Sorted.{bam,bam.bai}") into chSortedBAMBigWig, chSortedBAMSepReads, chSortedBAMSaturationCurve
file("v_samtools.txt") into chSamtoolsVersion
script :
"""
samtools sort -@ ${task.cpus} ${assignBam} -o ${prefix}_Sorted.bam
samtools index ${prefix}_Sorted.bam
samtools --version &> v_samtools.txt
"""
}
/* Saturation Curves*/
process saturationCurves {
tag "${prefix}"
label 'preseq'
label 'extraCpu'
label 'extraMem'
publishDir "${params.outDir}/saturationCurves", mode: 'copy'
when:
!params.skipSatCurves
input:
set val(prefix), file(sortBam) from chSortedBAMSaturationCurve
output:
set val(prefix), file ("*curve.txt") into preseq_results
file("v_preseq.txt") into chPreseqVersion
script:
"""
preseq lc_extrap -v -B ${sortBam[0]} -o ${prefix}.extrap_curve.txt -e 200e+06
# -e, -extrap = Max extrapolation. Here extrapolate until 200 000 000 reads
# -D, -defects = estimates the complexity curve without checking for instabilities in the curve.
preseq &> v_preseq.txt
"""
}
process separateReads {
tag "${prefix}"
label 'samtools'
label 'highCpu'
label 'highMem'
publishDir "${params.outDir}/separateReads", mode: 'copy'
input :
set val(prefix), file(sortedBam), file(umisReadsIDs) from chSortedBAMSepReads.join(chUmiReadsIDs)
output:
set val("${prefix}_umi"), file("*_assignedUMIs.{bam,bam.bai}") into chUmiBam, chUmiBamCountMtx
set val("${prefix}_NonUmi"), file("*_assignedNonUMIs.{bam,bam.bai}") into chNonUmiBam
script:
"""
# Separate umi and non umi reads
samtools view ${sortedBam[0]} > ${prefix}assignedAll.sam
# save header and extract umi reads
samtools view -H ${sortedBam[0]} > ${prefix}_assignedUMIs.sam
nbLines=\$(wc -l < ${umisReadsIDs})
if((\$nbLines!=0))
then
fgrep -f ${umisReadsIDs} ${prefix}assignedAll.sam >> ${prefix}_assignedUMIs.sam
fi
# sam to bam
samtools view -bh ${prefix}_assignedUMIs.sam > ${prefix}_umi_assignedUMIs.bam
# save header and extract non umi reads
samtools view -H ${sortedBam[0]} > ${prefix}_assignedNonUMIs.sam
# get reads that do not match umi read IDs
if((\$nbLines!=0))
then
fgrep -v -f ${umisReadsIDs} ${prefix}assignedAll.sam >> ${prefix}_assignedNonUMIs.sam
else
cat ${prefix}assignedAll.sam >> ${prefix}_assignedNonUMIs.sam
fi
cat
# sam to bam
samtools view -bh ${prefix}_assignedNonUMIs.sam > ${prefix}_NonUmi_assignedNonUMIs.bam
# index
samtools index ${prefix}_umi_assignedUMIs.bam
samtools index ${prefix}_NonUmi_assignedNonUMIs.bam
rm *.sam
"""
}
process countMatrices {
tag "${prefix}"
label 'umiTools'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/countMatrices", mode: 'copy'
input:
set val(prefix), file(umiBam) from chUmiBamCountMtx
output:
set val(prefix), file("*_Counts.tsv.gz") into chMatrices, chMatrices_dist, chMatrices_counts
set val(prefix), file("*_UmiCounts.log") into chMatricesLog
script:
"""
# Count UMIs per gene per cell
umi_tools count --method=cluster --per-gene --gene-tag=XT --assigned-status-tag=XS -I ${umiBam[0]} -S ${prefix}_Counts.tsv.gz > ${prefix}_UmiCounts.log
"""
}
process bigWig {
tag "${prefix}"
label 'deeptools'
label 'extraCpu'
label 'extraMem'
publishDir "${params.outDir}/bigWig", mode: 'copy'
input:
set val(prefix), file(bam) from chSortedBAMBigWig
output:
set val(prefix), file("*_coverage.bw") into chBigWig
set val(prefix), file("*_coverage.log") into chBigWigLog
file("v_deeptools.txt") into chBamCoverageVersion
script:
"""
## Create bigWig files
bamCoverage --normalizeUsing CPM -b ${bam[0]} -of bigwig -o ${prefix}_coverage.bw --numberOfProcessors=${task.cpus} > ${prefix}_coverage.log
bamCoverage --version &> v_deeptools.txt
"""
}
/*
* Gene body Coverage
*/
process genebodyCoverage {
tag "${prefix}"
label 'rseqc'
label 'extraCpu'
label 'extraMem'
publishDir "${params.outDir}/genebody_coverage" , mode: 'copy',
saveAs: {filename ->
if (filename.indexOf("geneBodyCoverage.curves.pdf") > 0) "geneBodyCoverage/$filename"
else if (filename.indexOf("geneBodyCoverage.r") > 0) "geneBodyCoverage/rscripts/$filename"
else if (filename.indexOf("geneBodyCoverage.txt") > 0) "geneBodyCoverage/data/$filename"
else if (filename.indexOf("log.txt") > -1) false
else filename
}
when:
!params.skipGeneCov
input:
file bed12 from chBedGeneCov.collect()
set val(prefix), file(bm) from chUmiBam.concat(chNonUmiBam)
output:
file "*.{txt,pdf,r}" into chGeneCov_res
file ("v_rseqc") into chRseqcVersion
script:
"""
geneBody_coverage.py \\
-i ${bm[0]} \\
-o ${prefix}.rseqc \\
-r $bed12
mv log.txt ${prefix}.rseqc.log.txt
geneBody_coverage.py --version &> v_rseqc
"""
}
/*
* Cell Viability
*/
process umiPerGeneDist{
tag "${prefix}"
label 'R'
label 'lowCpu'
label 'lowMem'
publishDir "${params.outDir}/umiPerGeneDist", mode: 'copy'
input:
set val(prefix), file(matrix) from chMatrices_dist
output:
set val(prefix), file ("*_HistUMIperGene_mqc.csv") into chUMIperGene
script:
"""
# Get matrix one by one
umiPerGene_dist.r ${matrix} ${prefix}
"""
}
// si MiSeq ~ 20 cells
process countUMIGenePerCell{
tag "${prefix}"
label 'R'
label 'lowCpu'
label 'lowMem'
publishDir "${params.outDir}/countUMIGenePerCell", mode: 'copy'
input:
file(matrices) from chMatrices_counts.collect()
output:
file ("nbGenePerCell.csv") into chGenePerCell
file ("nbUMIPerCell.csv") into chUmiPerCell
script:
"""
umiGenePerCell.r
"""
}
// Si NovaSeq (~1500 cells): 1 histogram de distribution du nb d'umis par cell
// == matrice de 2 columns, 1st avec nb cells, 2nd avec nb UMIs per cell
// TODO
process cellAnalysis{
tag "${prefix}"
label 'R'
label 'highCpu'
label 'highMem'
publishDir "${params.outDir}/cellAnalysis", mode: 'copy'
input:
file (matrices) from chMatrices.collect()
output:
file ("10Xoutput/") into ch10X
file ("10Xoutput.zip") into ch10Xzip
file ("resume.txt") into chResume
file ("RatioPerCell.csv") into chUmiGeneRatio
file ("MtGenePerCell.csv") into chMT
file ("v_R.txt") into chRversion
script:
"""
cellViability.r 10Xoutput/
zip 10Xoutput.zip 10Xoutput/*
R --version &> v_R.txt
"""
}
// Gene-based saturation
process geneSaturation {
label 'R'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/gene_saturation" , mode: 'copy'
//when:
//!params.skip_qc && !params.skip_saturation
input:
file ("10Xoutput/*") from ch10X
output:
file "*gcurve.txt" into genesat_results
script:
"""
gene_saturation.r "10Xoutput/" counts.gcurve.txt
"""
}
/*
* MultiQC
*/
process getSoftwareVersions{
label 'python'
label 'lowCpu'
label 'lowMem'
publishDir path: "${params.outDir}/softwareVersions", mode: "copy"
when:
!params.skipSoftVersions
input:
file("v_umi_tools.txt") from chUmiToolsVersion.first().ifEmpty([])
file("v_seqkit.txt") from chSeqkitVersion.first().ifEmpty([])
file("v_cutadapt.txt") from chCutadaptVersion.first().ifEmpty([])
file("v_star.txt") from chStarVersion.first().ifEmpty([])
file("v_featurecounts.txt") from chFCversion.first().ifEmpty([])
file("v_samtools.txt") from chSamtoolsVersion.first().ifEmpty([])
file("v_deeptools.txt") from chBamCoverageVersion.first().ifEmpty([])
file("v_R.txt") from chRversion.ifEmpty([])
file("v_rseqc") from chRseqcVersion.first().ifEmpty([])
file("v_preseq.txt") from chPreseqVersion.first().ifEmpty([])
output:
file 'software_versions_mqc.yaml' into softwareVersionsYaml
script:
"""
echo $workflow.manifest.version &> v_pipeline.txt
echo $workflow.nextflow.version &> v_nextflow.txt
scrape_software_versions.py &> software_versions_mqc.yaml
"""
}
process workflowSummaryMqc {
label 'onlyLinux'
when:
!params.skipMultiQC
output:
file 'workflow_summary_mqc.yaml' into workflowSummaryYaml
exec:
def yaml_file = task.workDir.resolve('workflow_summary_mqc.yaml')
yaml_file.text = """
id: 'summary'
description: " - this information is collected when the pipeline is started."
section_name: 'Workflow Summary'
section_href: 'https://gitlab.curie.fr/sc-platform/smartseq3'
plot_type: 'html'
data: |
<dl class=\"dl-horizontal\">
${summary.collect { k,v -> " <dt>$k</dt><dd><samp>${v ?: '<span style=\"color:#999999;\">N/A</a>'}</samp></dd>" }.join("\n")}
</dl>
""".stripIndent()
}
if (skippedPoorAlignment.size() > 0){
Channel.fromList(skippedPoorAlignment)
.flatMap{ it -> it + ": Poor alignment rate. Sample removed from the analysis !!!! <br>"}
.collectFile(name: 'warnings.txt', newLine: true)
.set{chWarn}
}else{
chWarn = Channel.empty()
}
process multiqc {
label 'multiqc'
label 'medCpu'
label 'medMem'
publishDir "${params.outDir}/MultiQC", mode: 'copy'
when:
!params.skipMultiQC
input:
file splan from chSplan.collect()
file multiqcConfig from chMultiqcConfig
file metadata from chMetadata.ifEmpty([])
file ('software_versions/*') from softwareVersionsYaml.collect().ifEmpty([])
file ('workflowSummary/*') from workflowSummaryYaml.collect()
file ('workflowSummary/*') from chWarn.collect().ifEmpty([])
//Modules
//file ('trimming/*') from chtrimmedReadsLog.collect().ifEmpty([])
file ('star/*') from chAlignmentLogs.collect().ifEmpty([])
file ('FC/*') from chAssignmentLogs.collect().ifEmpty([])
file ('coverage/*') from chGeneCov_res.collect().ifEmpty([])
file ('preseq/*') from preseq_results.collect().ifEmpty([])
//LOGS
file ('umiExtract/*') from chUmiExtractedLog.collect()
file('pUMIs/*') from chCountSummaryExtUMI.collect()
file('totReads/*') from chTotReads.collect()
file ('bigwig/*') from chBigWigLog.collect()
file (resume) from chResume // general stats
//PLOTS
file ("umiPerGene/*") from chUMIperGene.collect() // linegraph == histogram
file ("nbUMI/*") from chUmiPerCell.collect() // bargraph
file ("nbGene/*") from chGenePerCell.collect() // bargraph
file ("ratio/*") from chUmiGeneRatio.collect() // UmiGenePerCell_mqc.csv
file ("mt/*") from chMT.collect() // MtGenePerCell_mqc.csv
file ('genesat/*') from genesat_results.collect().ifEmpty([])
output:
file splan
file "*report.html" into multiqc_report
file "*_data"
script:
rtitle = customRunName ? "--title \"$customRunName\"" : ''
rfilename = customRunName ? "--filename " + customRunName + "_report" : "--filename report"
metadataOpts = params.metadata ? "--metadata ${metadata}" : ""
modules_list = "-m custom_content -m samtools -m star -m featureCounts -m deeptools -m preseq -m rseqc"
//warn=skippedPoorAlignment.size() > 0 ? "--warn workflowSummary/warnings.txt" : ""
"""
stat2mqc.sh ${splan}
#mean_calculation.r
mqc_header.py --splan ${splan} --name "SmartSeq3 scRNA-seq" --version ${workflow.manifest.version} > multiqc-config-header.yaml
multiqc . -f $rtitle $rfilename -c multiqc-config-header.yaml -c $multiqcConfig $modules_list
"""
}
/****************
* Sub-routines *
****************/
process outputDocumentation {
label 'python'
label 'lowCpu'
label 'lowMem'
publishDir "${params.outDir}/summary", mode: 'copy'
input:
file output_docs from chOutputDocs
file images from chOutputDocsImages
output:
file "results_description.html"
script:
"""
markdown_to_html.py $output_docs -o results_description.html
"""
}
workflow.onComplete {
// pipelineReport.html
def reportFields = [:]
reportFields['version'] = workflow.manifest.version
reportFields['runName'] = customRunName ?: workflow.runName
reportFields['success'] = workflow.success
reportFields['dateComplete'] = workflow.complete
reportFields['duration'] = workflow.duration
reportFields['exitStatus'] = workflow.exitStatus
reportFields['errorMessage'] = (workflow.errorMessage ?: 'None')
reportFields['errorReport'] = (workflow.errorReport ?: 'None')
reportFields['commandLine'] = workflow.commandLine
reportFields['projectDir'] = workflow.projectDir
reportFields['summary'] = summary
reportFields['summary']['Date Started'] = workflow.start
reportFields['summary']['Date Completed'] = workflow.complete
reportFields['summary']['Pipeline script file path'] = workflow.scriptFile
reportFields['summary']['Pipeline script hash ID'] = workflow.scriptId
if(workflow.repository) reportFields['summary']['Pipeline repository Git URL'] = workflow.repository
if(workflow.commitId) reportFields['summary']['Pipeline repository Git Commit'] = workflow.commitId
if(workflow.revision) reportFields['summary']['Pipeline Git branch/tag'] = workflow.revision
reportFields['skippedPoorAlignment'] = skippedPoorAlignment
// Render the TXT template
def engine = new groovy.text.GStringTemplateEngine()
def tf = new File("$baseDir/assets/onCompleteTemplate.txt")
def txtTemplate = engine.createTemplate(tf).make(reportFields)
def reportTxt = txtTemplate.toString()
// Render the HTML template
def hf = new File("$baseDir/assets/onCompleteTemplate.html")
def htmlTemplate = engine.createTemplate(hf).make(reportFields)
def reportHtml = htmlTemplate.toString()