forked from guigolab/grape-nf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grape-pipeline.nf
executable file
·808 lines (668 loc) · 23.8 KB
/
grape-pipeline.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
#!/bin/env nextflow
/*
* Copyright (c) 2015, Centre for Genomic Regulation (CRG)
* Emilio Palumbo, Alessandra Breschi and Sarah Djebali.
*
* This file is part of the GRAPE RNAseq pipeline.
*
* The GRAPE RNAseq pipeline is a free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// imports
import groovy.json.JsonSlurper
//Set default values for params
params.addXs = false
params.mappingSortTool = null
params.chunkSize = null
params.dbFile = 'pipeline.db'
params.genomeIndex = null
params.help = false
params.markDuplicates = false
params.removeDuplicates = false
params.maxMismatches = 4
params.maxMultimaps = 10
params.pairedEnd = false
params.readLength = 150
params.readStrand = null
params.rgCenterName = null
params.rgDesc = null
params.rgLibrary = null
params.rgPlatform = null
params.sjOverHang = 100
params.steps = 'mapping,bigwig,contig,quantification'
params.wigRefPrefix = ''
params.inferExpThreshold = 0.8
// Process channels
Channel.empty().into {
fetchInput
fastaIndexGenomes; fastaIndexAnnotations
indexGenomes; indexAnnotations
mappingInput; mappingIndex; mappingAnnotations
txIndexGenomes; txIndexAnnotations
mergeBamInput
markdupInput
inferExpInput; inferExpAnnotations
bamStatsInput; bamStatsAnnotations
bigwigInput; bigwigFastaIndex
contigInput; contigFastaIndex
quantificationInput; quantificationTxIndex
}
// Auxiliary variables
def comprExts = ['gz', 'bz2', 'zip']
def pref = "_m${params.maxMismatches}_n${params.maxMultimaps}"
// Clear pipeline.db file
pdb = file(params.dbFile)
pdb.write('')
// get list of steps from comma-separated strings
pipelineSteps = params.steps.split(',').collect { it.trim() }
//print usage
if (params.help) {
log.info ''
log.info 'G R A P E ~ RNA-seq Pipeline'
log.info '----------------------------'
log.info 'Run the GRAPE RNA-seq pipeline on a set of data.'
log.info ''
log.info 'Usage: '
log.info ' grape-pipeline.nf --index INDEX_FILE --genome GENOME_FILE --annotation ANNOTATION_FILE [OPTION]...'
log.info ''
log.info 'Options:'
log.info ' --help Show this message and exit.'
log.info ' --index INDEX_FILE Index file.'
log.info ' --genome GENOME_FILE Reference genome file(s).'
log.info ' --annotation ANNOTAION_FILE Reference gene annotation file(s).'
log.info ' --steps STEP[,STEP]... The steps to be executed within the pipeline run. Possible values: "mapping", "bigwig", "contig", "quantification". Default: all'
// log.info ' --chunk-size CHUNK_SIZE The number of records to be put in each chunk when splitting the input. Default: no split'
log.info ' --max-mismatches THRESHOLD Set maps with more than THRESHOLD error events to unmapped. Default "4".'
log.info ' --max-multimaps THRESHOLD Set multi-maps with more than THRESHOLD mappings to unmapped. Default "10".'
log.info ' --bam-sort METHOD Specify the method used for sorting the genome BAM file.'
log.info ' --paired-end Treat input data as paired-end.'
log.info ' --add-xs Add the XS field required by Cufflinks/Stringtie to the genome BAM file.'
log.info ''
log.info 'SAM read group options:'
log.info ' --rg-platform PLATFORM Platform/technology used to produce the reads for the BAM @RG tag.'
log.info ' --rg-library LIBRARY Sequencing library name for the BAM @RG tag.'
log.info ' --rg-center-name CENTER_NAME Name of sequencing center that produced the reads for the BAM @RG tag.'
log.info ' --rg-desc DESCRIPTION Description for the BAM @RG tag.'
// log.info ' --loglevel LOGLEVEL Log level (error, warn, info, debug). Default "info".'
log.info ''
exit 1
}
// check mandatory options
if (!params.genomeIndex && !params.genome) {
exit 1, "Reference genome not specified"
}
if ('quantification' in pipelineSteps && !params.annotation) {
exit 1, "Annotation not specified"
}
log.info ""
log.info "G R A P E ~ RNA-seq Pipeline"
log.info ""
log.info "General parameters"
log.info "------------------"
log.info "Index file : ${params.index}"
log.info "Genome : ${params.genome}"
log.info "Annotation : ${params.annotation}"
log.info "Pipeline steps : ${pipelineSteps.join(" ")}"
log.info "Pipeline profile : ${workflow.profile}"
log.info ""
if ('mapping' in pipelineSteps) {
log.info "Mapping parameters"
log.info "------------------"
// log.info "Tool : ${mappingTool}"
log.info "Max mismatches : ${params.maxMismatches}"
log.info "Max multimaps : ${params.maxMultimaps}"
if ( params.rgPlatform ) log.info "Sequencing platform : ${params.rgPlatform}"
if ( params.rgLibrary ) log.info "Sequencing library : ${params.rgLibrary}"
if ( params.rgCenterName ) log.info "Sequencing center : ${params.rgCenterName}"
if ( params.rgDesc ) log.info "@RG Descritpiton : ${params.rgDesc}"
log.info ""
}
if ('bigwig' in pipelineSteps) {
log.info "Bigwig parameters"
log.info "-----------------"
// log.info "Tool : ${bigwigTool}"
log.info "References prefix : ${['','-'].contains(params.wigRefPrefix) ? 'all' : params.wigRefPrefix}"
log.info ""
}
if ('quantification' in pipelineSteps) {
log.info "Quantification parameters"
log.info "-------------------------"
log.info "Tool : ${params.quantificationTool}"
log.info "Mode : ${params.quantificationMode}"
log.info ""
}
log.info "Execution information"
log.info "---------------------"
log.info "Use containers : ${workflow.containerEngine?:false}"
log.info ""
def msg = "Output files db"
log.info "=" * msg.size()
log.info msg + " -> ${pdb}"
log.info "=" * msg.size()
log.info ""
// Get input data
index = params.index ? file(params.index) : System.in
Channel.from(index.readLines())
.filter { it } // get only non-empty lines
.map { line ->
def (sampleId, runId, fileName, format, readId) = line.split()
def fetch = false
if ( fileName.split(',').size() > 1 )
fetch = true
if ( ! fetch )
fileName = resolveFile(fileName, index)
[sampleId, runId, fileName, format, readId, fetch]
}.tap {
inputFilesForFetch
inputFiles
}
(ids, samples, indexLines) = readTsv(index)
log.info "Dataset information"
log.info "-------------------"
log.info "Number of sequenced samples : ${samples}"
log.info "Number of sequencing runs : ${ids}"
log.info "Merging : ${ ids != samples ? 'by sample' : 'none' }"
log.info ""
inputFilesForFetch
.filter { it[5] }
.map { sampleId, runId, fileName, format, readId, fetch ->
[sampleId, runId, fileName, format, readId]
}.set {
fetchInput
}
inputFiles.filter { !it[5] }
.map { sampleId, runId, fileName, format, readId, fetch ->
[sampleId, runId, fileName, format, readId]
}.set {
inputFilesNotToFetch
}
// Get references
genomes = params.genome.split(',').collect { file(it) }
annotations = params.annotation.split(',').collect { file(it) }
if (params.genomeIndex) {
genomeidxs = params.genomeIndex.split(',').collect { file(it) }
}
Channel.from(genomes)
.merge(Channel.from(annotations)) { g,a ->
[ g.simpleName, [g, a] ]
}.transpose()
.into { refsForGenomes; refsForAnnotations }
refsForGenomes.filter {
it[1].name =~ /.fa(.gz)?$/
}.set{ Genomes }
refsForAnnotations.filter {
it[1].name =~ /.gtf(.gz)?$/
}.set{ Annotations }
Genomes.into {
genomesForFastaIndex
genomesForIndex
genomesForTxIndex
}
Annotations.into {
annotationsForFastaIndex
annotationsForIndex
annotationsForMapping
annotationsForTxIndex
annotationsForInferExp
annotationsForBamStats
annotationsForQuantification
}
if ( 'bigwig' in pipelineSteps || 'contig' in pipelineSteps) {
genomesForFastaIndex.set { fastaIndexGenomes }
annotationsForFastaIndex.set { fastaIndexAnnotations }
}
if ( 'mapping' in pipelineSteps ) {
if ( params.genomeIndex ) {
Channel.from(genomeidxs).map {
[ it.simpleName, it ]
}.set { mappingIndex
}
} else {
genomesForIndex.set { indexGenomes }
annotationsForIndex.set { indexAnnotations }
}
annotationsForMapping.set { mappingAnnotations }
annotationsForInferExp.set { inferExpAnnotations }
annotationsForBamStats.set { bamStatsAnnotations }
}
if ( 'quantification' in pipelineSteps && params.quantificationMode == "Transcriptome" ) {
genomesForTxIndex.set { txIndexGenomes }
annotationsForTxIndex.set { txIndexAnnotations }
}
// Processes
process fetch {
tag { outPath.name }
storeDir { outPath.parent }
input:
set sample, id, path, type, view from fetchInput
output:
set sample, id, file("${outPath.name}"), type, view into fetchOutput
script:
def paths = path.split(',')
outPath = workflow.launchDir.resolve(paths[-1])
urls = paths.size() > 1 ? paths[0..-2].join(' ') : ''
"""
for url in $urls; do
if wget \${url}; then
exit 0
fi
done
"""
}
inputFilesNotToFetch.mix(fetchOutput)
.groupTuple(by: [0,1,3], sort: true)
.into {
inputFilesForBams
inputFilesForFastqs
}
inputFilesForFastqs.filter {
it[3] == 'fastq'
}.map {
[it[1], it[0], it[2], fastq(it[2][0]).qualityScore()]
}.set { mappingInput }
inputFilesForBams.filter {
it[3] == 'bam'
}.transpose()
.map { sample, id, path, type, view ->
[id, sample, type, view, path, params.pairedEnd].flatten()
}
.set {
inputBams
}
process fastaIndex {
tag "${species}-${params.fastaIndexTool}-${params.fastaIndexToolVersion}"
input:
set species, file(genome) from fastaIndexGenomes
set species, file(annotation) from fastaIndexAnnotations
output:
set species, file { "${genome.name.replace('.gz','')}.fai" } into fastaIndexOutput
script:
compressed = genome.extension in comprExts ? "-${genome.extension}" : ''
command = "${task.process}/${params.fastaIndexTool}${compressed}"
template(command)
}
fastaIndexOutput.into {
fastaIndexForBigwig
fastaIndexForContig
}
if ( 'bigwig' in pipelineSteps ) {
fastaIndexForBigwig.set { bigwigFastaIndex }
}
if ( 'contig' in pipelineSteps ) {
fastaIndexForContig.set { contigFastaIndex }
}
process index {
label "mapping"
tag "${species}-${params.mappingTool}-${params.mappingToolVersion}"
input:
set species, file(genome) from indexGenomes
set species, file(annotation) from indexAnnotations
output:
set species, file("genomeDir") into indexOutput
script:
cpus = task.cpus
memory = (task.memory ?: 1.GB).toBytes()
sjOverHang = params.sjOverHang
readLength = params.readLength
genomeCompressed = genome.extension in comprExts ? "-genome-${genome.extension}" : ''
annoCompressed = annotation.extension in comprExts ? "-anno-${annotation.extension}" : ''
command = "${task.process}/${params.mappingTool}${genomeCompressed}${annoCompressed}"
template(command)
}
if ( ! params.genomeIndex ) {
indexOutput.set { mappingIndex }
}
process txIndex {
label 'quantification'
tag "${species}-${params.quantificationTool}-${params.quantificationToolVersion}"
input:
set species, file(genome) from txIndexGenomes
set species, file(annotation) from txIndexAnnotations
output:
set species, file('txDir') into txIndexOutput
script:
genomeCompressed = genome.extension in comprExts ? "-genome-${genome.extension}" : ''
annoCompressed = annotation.extension in comprExts ? "-anno-${annotation.extension}" : ''
command = "${task.process}/${params.quantificationTool}${genomeCompressed}${annoCompressed}"
template(command)
}
process mapping {
label "mapping"
tag "${id.replace(':', '_')}-${params.mappingTool}-${params.mappingToolVersion}"
input:
set id, sample, file(reads), qualityOffset from mappingInput
set species, file(annotation) from mappingAnnotations.first()
set species, file(genomeDir) from mappingIndex.first()
output:
set id, sample, type, view, file("*.bam"), pairedEnd into mappingOutput
script:
type = 'bam'
view = 'Alignments'
prefix = "${sample}${pref}"
maxMultimaps = params.maxMultimaps
maxMismatches = params.maxMismatches
// prepare BAM @RG tag information
// def date = new Date().format("yyyy-MM-dd'T'HH:mmZ", TimeZone.getTimeZone("UTC"))
date = ""
readGroupList = []
readGroupList << ["ID", "${id}"]
readGroupList << ["PU", "${id}"]
readGroupList << ["SM", "${sample}"]
if ( date ) readGroupList << ["DT", "${date}"]
if ( params.rgPlatform ) readGroupList << ["PL", "${params.rgPlatform}"]
if ( params.rgLibrary ) readGroupList << ["LB", "${params.rgLibrary}"]
if ( params.rgCenterName ) readGroupList << ["CN", "${params.rgCenterName}"]
if ( params.rgDesc ) readGroupList << ["DS", "${params.rgDesc}"]
(s,t) = params.mappingReadGroupSeparators
readGroup = readGroupList.collect { it.join(s) }.join(t)
fqs = reads.toString().split(" ")
pairedEnd = (fqs.size() == 2)
taskMemory = task.memory ?: 1.GB
totalMemory = (taskMemory.toBytes()*2/3) as int
threadMemory = (totalMemory/task.cpus) as int
cpus = task.cpus
halfCpus = (task.cpus > 1 ? task.cpus / 2 : task.cpus) as int
command = "${task.process}/${params.mappingTool}-${params.mappingToolVersion.split("\\.")[0..1].join(".")}"
switch(params.mappingTool) {
case 'GEM':
command += "-${pairedEnd ? 'Paired-End' : 'Single-End'}"
break
case 'STAR':
command += (params.mappingSortTool ? "-"+params.mappingSortTool : '') + (params.quantificationMode ? "-"+params.quantificationMode : '') + (params.addXs ? "-XS" : '')
break
}
template(command)
}
mappingOutput.flatMap { id, sample, type, view, path, pairedEnd ->
[path].flatten().collect { f ->
[id, sample, type, (f.name =~ /toTranscriptome/ ? 'Transcriptome' : 'Genome') + view, f, pairedEnd]
}
}.mix(inputBams).groupTuple(by: [1, 2, 3, 5]) // group by sample, type, view, pairedEnd (to get unique values for keys)
.into {
bamFilesForSingle
bamFilesForMergeBam
}
bamFilesForSingle.filter {
it[4].size() == 1
}.set {
bamFilesSingle
}
bamFilesForMergeBam.filter {
it[4].size() > 1
}.into {
bamFilesGenomeForMerge
bamFilesTranscriptomeForMerge
}
bamFilesGenomeForMerge.filter {
it[3] =~ /^Genome/
}.set {
mergeBamGenomeInput
}
bamFilesTranscriptomeForMerge.filter {
it[3] =~ /^Transcriptome/
}.transpose()
.set {
bamFilesTranscriptomeMerge
}
process sortBam {
tag "${id}-${params.mergeBamTool}-${params.mergeBamToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd from bamFilesTranscriptomeMerge
output:
set id, sample, type, view, file("${prefix}.bam"), pairedEnd into mergeBamTranscriptomeInput
script:
cpus = task.cpus
taskMemory = task.memory ?: 1.GB
totalMemory = taskMemory.toBytes()
threadMemory = totalMemory/cpus
prefix = "${bam.baseName}_sorted"
command = "${task.process}/${params.mergeBamTool}"
template(command)
}
mergeBamGenomeInput.mix(
mergeBamTranscriptomeInput.groupTuple(by: [1, 2, 3, 5], sort: true)
).set {
mergeBamInput
}
process mergeBam {
tag "${id.replace(':', '_')}-${params.mergeBamTool}-${params.mergeBamToolVersion}"
input:
set id, sample, type, view, file("${sample}_??.bam"), pairedEnd from mergeBamInput
output:
set id, sample, type, view, file("${prefix}.bam"), pairedEnd into mergeBamOutput
script:
cpus = task.cpus
id = id.sort().join(':')
prefix = "${sample}${pref}_to${view.replace('Alignments','')}"
command = "${task.process}/${params.mergeBamTool}"
template(command)
}
bamFilesSingle
.mix(mergeBamOutput)
.map {
it.flatten()
}.into {
bamFilesForGenome
bamFilesForTranscriptome
}
bamFilesForTranscriptome.filter {
it[3] =~ /^Transcriptome/
}.set{
bamFilesToTranscriptome
}
bamFilesForGenome.filter {
it[3] =~ /^Genome/
}.into{
bamFilesForMarkdup
bamFilesToGenome
}
if ( params.markDuplicates || params.removeDuplicates ) {
bamFilesForMarkdup.set { markdupInput }
}
process markdup {
tag "${id.replace(':', '_')}-${params.markdupTool}-${params.markdupToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd from markdupInput
output:
set id, sample, type, view, file("${prefix}.bam"), pairedEnd into markdupOutput
script:
cpus = task.cpus
memory = (task.memory ?: 2.GB).toMega()
prefix = "${bam.baseName}.markdup"
command = "${task.process}/${params.markdupTool}${params.removeDuplicates ? '-remove' : ''}"
template(command)
}
if ( params.markDuplicates || params.removeDuplicates ) {
bamFilesToGenome = markdupOutput
}
bamFilesToGenome.into {
inferExpInput
bamStatsInput
}
process inferExp {
tag "${id.replace(':', '_')}-${params.inferExpTool}-${params.inferExpToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd from inferExpInput
set species, file(annotation) from inferExpAnnotations.first()
output:
// set id, stdout into bamStrand
set id, sample, type, view, file(bam), pairedEnd, stdout into inferExpOutputJSON
script:
prefix = "${annotation.name.split('\\.', 2)[0]}"
command = "${task.process}/${params.inferExpTool}"
threshold = params.inferExpThreshold
template(command)
}
inferExpOutputJSON.map {
j = new JsonSlurper()
d = j.parseText(it[-1])
it[0..-3] + [ d.paired, d.exp ]
}.set {
inferExpOutput
}
inferExpOutput.into {
bigwigInput
contigInput
bamFilesCrossTranscriptome
bamFilesCrossBamStats
quantificationInputGenome
bamFilesToGenome
}
bamFilesToTranscriptome.cross(bamFilesCrossTranscriptome).map { transcriptome, genome ->
transcriptome[0..-2] + genome[-2..-1]
}.into {
bamFilesToTranscriptome
quantificationInputTranscriptome
}
switch(params.quantificationMode) {
case 'Genome':
quantificationInputGenome.set { quantificationInput }
annotationsForQuantification.set { quantificationIndex }
break
case 'Transcriptome':
quantificationInputTranscriptome.set { quantificationInput }
txIndexOutput.set { quantificationIndex }
break
}
process bamStats {
tag "${id.replace(':', '_')}-${params.bamStatsTool}-${params.bamStatsToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd from bamStatsInput
set species, file(annotation) from bamStatsAnnotations.first()
output:
set id, sample, type, views, file('*.json'), pairedEnd into bamStatsOutput
script:
cpus = task.cpus
type = "json"
prefix = "${sample}"
views = "BamStats"
maxBuf = params.bamStatsMaxBuf
logLevel = params.bamStatsLogLevel
command = "${task.process}/${params.bamStatsTool}"
template(command)
}
bamStatsOutput.cross(bamFilesCrossBamStats).map { stats, genome ->
stats + genome[-1]
}.set {
bamStatsFiles
}
process bigwig {
tag "${id.replace(':', '_')}-${params.bigwigTool}-${params.bigwigToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd, readStrand from bigwigInput
set species, file(genomeFai) from bigwigFastaIndex.first()
output:
set id, sample, type, views, file('*.bw'), pairedEnd, readStrand into bigwigOutput
script:
cpus = task.cpus
type = "bigWig"
prefix = "${sample}"
wigRefPrefix = params.wigRefPrefix != "-" ? params.wigRefPrefix : ""
views = params.bigwigViews[readStrand]
command = "${task.process}/${params.bigwigTool}-${readStrand}"
template(command)
}
process contig {
tag "${id.replace(':', '_')}-${params.contigTool}-${params.contigToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd, readStrand from contigInput
set species, file(genomeFai) from contigFastaIndex.first()
output:
set id, sample, type, view, file('*.bed'), pairedEnd, readStrand into contigOutput
script:
cpus = task.cpus
type = 'bed'
view = 'Contigs'
prefix = "${sample}.contigs"
command = "${task.process}/${params.contigTool}-${readStrand}"
template(command)
}
process quantification {
label 'quantification'
tag "${id.replace(':', '_')}-${params.quantificationTool}-${params.quantificationToolVersion}"
input:
set id, sample, type, view, file(bam), pairedEnd, readStrand from quantificationInput
set species, file(quantRef) from quantificationIndex.first()
output:
set id, sample, type, viewTx, file("*isoforms*"), pairedEnd, readStrand into quantificationIsoforms
set id, sample, type, viewGn, file("*genes*"), pairedEnd, readStrand into quantificationGenes
script:
cpus = task.cpus
prefix = "${sample}"
type = params.quantificationFileType
viewTx = "TranscriptQuantifications"
viewGn = "GeneQuantifications"
memory = (task.memory ?: 1.GB).toMega()
command = "${task.process}/${params.quantificationTool}"
if ( params.quantificationTool == 'RSEM') {
command += "-${pairedEnd ? 'Paired-End' : 'Single-End'}"
}
command += "-${readStrand}"
template(command)
}
bigwigOutput.flatMap { id, sample, type, views, files, pairedEnd, readStrand ->
[views, files].transpose().collect { view, f ->
[ id, sample, type, view, f, pairedEnd, readStrand ]
}
}.set {
bigwigFiles
}
bamFilesToGenome.mix(bamFilesToTranscriptome, bamStatsFiles, bigwigFiles, contigOutput, quantificationIsoforms, quantificationGenes)
.collectFile(name: pdb.name, storeDir: pdb.parent, newLine: true) { id, sample, type, view, file, pairedEnd, readStrand ->
[sample, id, file, type, view, pairedEnd ? 'Paired-End' : 'Single-End', readStrand].join("\t")
}
.subscribe {
log.info ""
log.info "-----------------------"
log.info "Pipeline run completed."
log.info "-----------------------"
}
/*
* Given the input index file returns the number of unique samples,
* the number of unique runs, and the lines of the index.
* Params:
* - tsvFile: a file object representing the TSV file
*/
def readTsv(tsvFile) {
def (samples, ids, lines) = [[], [], []]
tsvFile.eachLine { line ->
def (sampleId, runId, fileName, format, readId) = line.split()
samples << sampleId
ids << runId
lines << line
}
[ids.unique().size(), samples.unique().size(), lines]
}
/*
* Given a string path resolve it against the index file location.
* Params:
* - str: a string value represting the file pah to be resolved
* - index: path location against which relative paths need to be resolved
*/
def resolveFile( str, index ) {
if( str.startsWith('/') || str =~ /^[\w\d]*:\// ) {
return file(str)
}
else if( index instanceof Path ) {
return index.parent.resolve(str)
}
else {
return file(str)
}
}
def testResolveFile() {
def index = file('/path/to/index')
assert resolveFile('str', index) == file('/path/to/str')
assert resolveFile('/abs/file', index) == file('/abs/file')
assert resolveFile('s3://abs/file', index) == file('s3://abs/file')
}