-
Notifications
You must be signed in to change notification settings - Fork 6
/
nf-RNA_Seq_Preprocess.nf
executable file
·902 lines (692 loc) · 29.5 KB
/
nf-RNA_Seq_Preprocess.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
#!/usr/bin/env nextflow
//############################################################################################################################
//
// Josh Campbell
// 1/20/2017
// Peforms alignment and preprocessing of paired-end RNA sequencing data specifically for variant calling.
// This pipline may not be suitable for expression estimation as it marks duplicates
// For all samples derived from the same individual, an indel co-cleaning step will be performed on all bams jointly
//
// GATK tutorials this pipeline is based on:
// Mapping: https://software.broadinstitute.org/gatk/guide/article?id=3891
// Marking Duplicates: http://gatkforums.broadinstitute.org/gatk/discussion/6747/how-to-mark-duplicates-with-markduplicates-or-markduplicateswithmatecigar
// Realignment around indels: http://gatkforums.broadinstitute.org/gatk/discussion/2800/howto-perform-local-realignment-around-indels
// Base Quality Score Recalibration: http://gatkforums.broadinstitute.org/gatk/discussion/2801/howto-recalibrate-base-quality-scores-run-bqsr
//
//############################################################################################################################
// Set up global variables for requried parameters:
inputFile = file(params.infile)
inputFileHeader = params.infile_header
demoFile = file(params.demofile)
// Set up global variables for parameters with preset defaults:
REF = file(params.ref_dir)
REF_FASTA = file(params.ref_fasta)
GATK = file(params.gatk_jar)
PICARD = file(params.picard_jar)
GOLD1 = file(params.gold_indels1)
GOLD2 = file(params.gold_indels2)
OUTDIR = file(params.output_dir)
DBSNP = file(params.dbsnp)
READ_LENGTH = params.read_length
GENE_GTF = file(params.gene_gtf)
GENE_BED = file(params.gene_bed)
RSEM_REF = file(params.rsem_ref)
OVERHANG = READ_LENGTH - 1
STRANDED = params.stranded
PREFIX = params.prefix
CREATE_SE = params.create_SE_Rscript
inferAncestry = params.inferAncestry
RSEM_FORWARD_PROB = 0.5
if(STRANDED == true) {
RSEM_FORWARD_PROB = 0
}
logParams(params, "nextflow_parameters.txt")
VERSION = "1.0"
// Header log info
log.info ""
log.info "========================================="
log.info "GATK Best Practices for RNA-Seq Preprocessing v${VERSION}"
log.info "Nextflow Version: $workflow.nextflow.version"
log.info "Command Line: $workflow.commandLine"
log.info "========================================="
log.info ""
//#############################################################################################################
//#############################################################################################################
//
// Main
//
//#############################################################################################################
//#############################################################################################################
// ------------------------------------------------------------------------------------------------------------
//
// Send FASTQ files to two processes from input file: FastQC and FastqToSam
//
// ------------------------------------------------------------------------------------------------------------
Channel.from(inputFile)
.splitCsv(sep: '\t', header: inputFileHeader)
.into { readPairsFastQC; readPairsFastqToSTAR_1Pass; readPairsFastqToSTAR_2Pass }
// ------------------------------------------------------------------------------------------------------------
//
// Run STAR 2-pass to align reads to genome
//
// ------------------------------------------------------------------------------------------------------------
process runSTAR_1pass {
tag "${indivID}|${sampleID}|${libraryID}|${rgID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/Libraries/${libraryID}/${rgID}/STAR_1Pass/"
input:
set indivID, sampleID, libraryID, rgID, platform_unit, platform, platform_model, run_date, center, fastqR1, fastqR2 from readPairsFastqToSTAR_1Pass
output:
file(outfile_sj) into runSTAR_1PassOutput
script:
outfile_prefix = sampleID + "_" + libraryID + "_" + rgID + ".1pass."
outfile_bam = outfile_prefix + "Aligned.out.bam"
outfile_sj = outfile_prefix + "SJ.out.tab"
"""
module load star/2.5.2b
STAR --genomeDir ${REF} \
--readFilesIn ${fastqR1} ${fastqR2} \
--runThreadN 12 \
--outFileNamePrefix ${outfile_prefix} \
--outSAMtype BAM Unsorted \
--outFilterMultimapNmax 20 \
--outFilterType BySJout \
--readFilesCommand zcat
rm -vf ${outfile_bam}
"""
}
process runSTAR_GenomeGenerate {
tag "Generating STAR genome reference with Splice Junctions"
publishDir "${OUTDIR}/Output/STAR_Genome"
input:
val sjdb_files from runSTAR_1PassOutput.flatten().toSortedList()
output:
set file('Genome'), file('SA'), file('SAindex'), file("*.txt"), file("*.out"), file("*.tab") into runSTAR_GenomeGenerateOutput
script:
"""
module load star/2.5.2b
STAR --runMode genomeGenerate \
--genomeDir ./ \
--genomeFastaFiles ${REF_FASTA} \
--sjdbFileChrStartEnd ${sjdb_files.join(' ')} \
--sjdbGTFfile ${GENE_GTF} \
--sjdbOverhang ${OVERHANG} \
--runThreadN 12 \
--limitSjdbInsertNsj 5000000
"""
}
process runSTAR_2pass {
tag "${indivID}|${sampleID}|${libraryID}|${rgID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/Libraries/${libraryID}/${rgID}/STAR_2Pass/"
input:
set indivID, sampleID, libraryID, rgID, platform_unit, platform, platform_model, run_date, center, fastqR1, fastqR2 from readPairsFastqToSTAR_2Pass
set genomeFile, other_files from runSTAR_GenomeGenerateOutput.first()
output:
set indivID, sampleID, libraryID, rgID, platform_unit, platform, platform_model, run_date, center, file(outfile_bam) into runSTAR_2PassOutput
set indivID, sampleID, file(outfile_tbam) into runSTAR_2PassOutput_For_RSEM
file(outfile_log) into runSTARMultiQCOutput
script:
outfile_prefix = sampleID
outfile_bam = outfile_prefix + "Aligned.sortedByCoord.out.bam"
outfile_tbam = outfile_prefix + "Aligned.toTranscriptome.out.bam"
outfile_log = outfile_prefix + "Log.final.out"
genomeDir = genomeFile.getParent()
"""
module load star/2.5.2b
STAR --genomeDir ${genomeDir} \
--readFilesIn ${fastqR1} ${fastqR2} \
--runThreadN 12 \
--outFileNamePrefix ${outfile_prefix} \
--outSAMtype BAM SortedByCoordinate \
--quantMode TranscriptomeSAM \
--outFilterMultimapNmax 20 \
--outFilterType BySJout \
--outSAMunmapped Within \
--readFilesCommand zcat
"""
}
process runRSEM {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/RSEM"
input:
set indivID, sampleID, tbam from runSTAR_2PassOutput_For_RSEM
output:
file outfile_plot into runRSEMOutput
file genes_file into genesFileForSE
file isoforms_file into isoformsFileForSE
script:
outfile_plot_prefix = sampleID + "_RSEM"
outfile_plot = sampleID + "_RSEM.pdf"
genes_file = sampleID + ".genes.results"
isoforms_file = sampleID + ".isoforms.results"
"""
module load rsem/1.3.0
rsem-calculate-expression \
--calc-ci --estimate-rspd --no-bam-output --bam \
--paired-end \
--forward-prob ${RSEM_FORWARD_PROB} \
-p 12 \
$tbam \
${RSEM_REF} \
${sampleID}
rsem-plot-model ${sampleID} ${outfile_plot}
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Add read group information and sort
//
// ------------------------------------------------------------------------------------------------------------
process runAddReadGroupInfo {
tag "${indivID}|${sampleID}|${libraryID}|${rgID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}"
input:
set indivID, sampleID, libraryID, rgID, platform_unit, platform, platform_model, run_date, center, bam from runSTAR_2PassOutput
output:
set indivID, sampleID, file(outfile_bam),file(outfile_bai),file(outfile_bambai) into runAddReadGroupInfoOutput, runAddReadGroupInfoOutput_For_RSeQC
script:
outfile_bam = sampleID + ".bam"
outfile_bai = sampleID + ".bai"
outfile_bambai = sampleID + ".bam.bai"
"""
module load java/1.8.0_66
module load samtools
java -Xmx5G -XX:ParallelGCThreads=1 -Djava.io.tmpdir=tmp/ -jar ${PICARD} AddOrReplaceReadGroups \
I=${bam} \
O=${outfile_bam} \
SO=coordinate \
RGID=${rgID} \
RGLB=${libraryID} \
RGPL=${platform} \
RGPU=${platform_unit} \
RGSM=${sampleID} \
RGDT=${run_date} \
RGCN=${center} \
RGPM=${platform_model} \
CREATE_INDEX=true
samtools index ${outfile_bam}
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Run Picard MarkDuplicates
// Requires a lot of memory
// Need to set "ParallelGCThreads" otherwise it will "grab" extra available threads without asking (and potentially be terminated by SGE)
//
// ------------------------------------------------------------------------------------------------------------
process runMarkDuplicates {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/MarkDuplicates"
input:
set indivID, sampleID, bam from runAddReadGroupInfoOutput
output:
set indivID, sampleID, file(outfile_bam), file(outfile_bai) into runMarkDuplicatesOutput
set file(outfile_bam), file(outfile_bai) into runMarkDuplicatesOutput_for_RSeQC
file(outfile_metrics) into runMarkDuplicatesOutput_for_MultiQC
script:
outfile_bam = sampleID + ".dedup.bam"
outfile_bai = sampleID + ".dedup.bai"
outfile_metrics = sampleID + "_duplicate_metrics.txt"
"""
module load java/1.8.0_66
java -Xmx25G -XX:ParallelGCThreads=5 -Djava.io.tmpdir=tmp/ -jar ${PICARD} MarkDuplicates \
INPUT=${bam} \
OUTPUT=${outfile_bam} \
METRICS_FILE=${outfile_metrics} \
CREATE_INDEX=true \
TMP_DIR=tmp
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Split reads aligning to introns into sepearate reads for downstream analysis
//
// ------------------------------------------------------------------------------------------------------------
process runSplitNCigarReads {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/SplitNCigarReads"
input:
set indivID, sampleID, bam, bai from runMarkDuplicatesOutput
output:
set indivID, sampleID, file(outfile_bam), file(outfile_bai) into runSplitNCigarReadsOutput
script:
outfile_bam = sampleID + ".splitNreads.bam"
outfile_bai = sampleID + ".splitNreads.bai"
"""
module load java/1.8.0_66
java -Xmx15g -XX:ParallelGCThreads=5 -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T SplitNCigarReads \
-R ${REF_FASTA} \
-I ${bam} \
-o ${outfile_bam} \
-rf ReassignOneMappingQuality \
-RMQF 255 \
-RMQT 60 \
-U ALLOW_N_CIGAR_READS
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Combine samples from the same Individual (e.g. tumor/normal pair) to send to runRealignerTargetCreator
//
// ------------------------------------------------------------------------------------------------------------
runSplitNCigarReadsOutput_grouped_by_sample = runSplitNCigarReadsOutput.groupTuple(by: [0])
// ------------------------------------------------------------------------------------------------------------
//
// Perform realignment around indels
// 1) Identify regions for realignement
// 2) Perform realignment
//
// ------------------------------------------------------------------------------------------------------------
process runRealignerTargetCreator {
tag "${indivID}"
publishDir "${OUTDIR}/${indivID}/Processing/RealignerTargetCreator/"
input:
set indivID, sampleID, dedup_bam_list from runSplitNCigarReadsOutput_grouped_by_sample
output:
set indivID, dedup_bam_list, file(target_file) into runRealignerTargetCreatorOutput
script:
target_file = indivID + "_target_intervals.list"
"""
module load java/1.8.0_66
java -Xmx15g -XX:ParallelGCThreads=5 -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T RealignerTargetCreator \
-R ${REF_FASTA} \
-I ${dedup_bam_list.join(" -I ")} \
-known ${GOLD1} \
-known ${GOLD2} \
-o ${target_file}
"""
}
process runIndelRealigner {
tag "${indivID}"
publishDir "${OUTDIR}/${indivID}/Processing/IndelRealigner/"
input:
set indivID, dedup_bam_list, target_file from runRealignerTargetCreatorOutput
output:
set indivID, file('*.realign.bam') into runIndelRealignerOutput mode flatten
set indivID, file('*.bai') into runIndelRealignerOutputbai mode flatten
script:
"""
module load java/1.8.0_66
java -Xmx25g -XX:ParallelGCThreads=5 -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T IndelRealigner \
-R ${REF_FASTA} \
-I ${dedup_bam_list.join(" -I ")} \
-targetIntervals ${target_file} \
-known ${GOLD1} \
-known ${GOLD2} \
-nWayOut ".realign.bam"
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Perform base quality score recalibration (BQSR) including
// 1) Generate a recalibration table
// 2) Generate a new table after applying recalibration
// 3) Compare differences between recalibration tables
// 4) Apply recalibration
//
// ------------------------------------------------------------------------------------------------------------
// First we need to recapture the SampleID from the filename
runIndelRealignerOutput_split = runIndelRealignerOutput.map { indivID, file -> tuple(indivID, file.baseName.replaceAll(".splitNreads.realign", ""), file) }
process runBaseRecalibrator {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/BaseRecalibrator/"
input:
set indivID, sampleID, realign_bam from runIndelRealignerOutput_split
output:
set indivID, sampleID, realign_bam, file(recal_table) into runBaseRecalibratorOutput
script:
recal_table = sampleID + "_recal_table.txt"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=2 -Xmx25g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T BaseRecalibrator \
-R ${REF_FASTA} \
-I ${realign_bam} \
-knownSites ${GOLD1} \
-knownSites ${GOLD2} \
-knownSites ${DBSNP} \
-o ${recal_table}
"""
}
process runPrintReads {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/PrintReads"
input:
set indivID, sampleID, realign_bam, recal_table from runBaseRecalibratorOutput
output:
set indivID, sampleID, file(outfile_bam), file(outfile_bai) into runPrintReadsOutput
set indivID, sampleID, realign_bam, recal_table into runPrintReadsOutput_for_PostRecal
script:
outfile_bam = sampleID + ".clean.bam"
outfile_bai = sampleID + ".clean.bai"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=1 -Xmx25g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T PrintReads \
-R ${REF_FASTA} \
-I ${realign_bam} \
-BQSR ${recal_table} \
-o ${outfile_bam}
"""
}
process runBaseRecalibratorPostRecal {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/BaseRecalibratorPostRecal/"
input:
set indivID, sampleID, realign_bam, recal_table from runPrintReadsOutput_for_PostRecal
output:
set indivID, sampleID, recal_table, file(post_recal_table) into runBaseRecalibratorPostRecalOutput_Analyze
script:
post_recal_table = sampleID + "_post_recal_table.txt"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=1 -Xmx5g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T BaseRecalibrator \
-R ${REF_FASTA} \
-I ${realign_bam} \
-knownSites ${GOLD1} \
-knownSites ${GOLD2} \
-knownSites ${DBSNP} \
-BQSR ${recal_table} \
-o ${post_recal_table}
"""
}
process runAnalyzeCovariates {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/AnalyzeCovariates/"
input:
set indivID, sampleID, recal_table, post_recal_table from runBaseRecalibratorPostRecalOutput_Analyze
output:
set indivID, sampleID, recal_plots into runAnalyzeCovariatesOutput
script:
recal_plots = sampleID + "_recal_plots.pdf"
"""
module load java/1.8.0_66
module load R/3.3.2
java -XX:ParallelGCThreads=1 -Xmx5g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T AnalyzeCovariates \
-R ${REF_FASTA} \
-before ${recal_table} \
-after ${post_recal_table} \
-plots ${recal_plots}
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Call Variants for each sample separately
// 1) Run HaplotypeCaller
// 2) Filter variants
// 3) Combine into single VCF file
//
// ------------------------------------------------------------------------------------------------------------
process runHTC {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/HTC"
input:
set indivID, sampleID, bam, bai from runPrintReadsOutput
output:
set indivID, sampleID, file(outfile) into runHTC_Output
script:
outfile = sampleID + ".raw.vcf"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=1 -Xmx5g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T HaplotypeCaller \
-R ${REF_FASTA} \
-I ${bam} \
-dontUseSoftClippedBases \
-stand_call_conf 20.0 \
--dbsnp ${DBSNP} \
-L ${GENE_BED} \
-o ${outfile}
"""
}
process runFilterVariants {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/HTC"
input:
set indivID, sampleID, vcf from runHTC_Output
output:
file outfile into runFilterVariantsOutput
script:
outfile = sampleID + ".filter.vcf"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=1 -Xmx5g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T VariantFiltration \
-R ${REF_FASTA} \
-V ${vcf} \
-window 35 -cluster 3 \
-filterName FS -filter "FS > 30.0" \
-filterName QD -filter "QD < 2.0" \
-o ${outfile}
"""
}
process runCombineVariants {
tag "Combining VCFs"
publishDir "${OUTDIR}/Output/Variants"
input:
val vcf from runFilterVariantsOutput.flatten().toSortedList()
output:
file outfile into runCombineVariantsforinferAncestry
script:
outfile = PREFIX + ".filter.vcf"
"""
module load java/1.8.0_66
java -XX:ParallelGCThreads=1 -Xmx5g -Djava.io.tmpdir=tmp/ -jar ${GATK} \
-T CombineVariants \
-R ${REF_FASTA} \
-V ${vcf.join(" -V ")} \
-o ${outfile}
"""
}
//------------------------------------------------------------------------------------------------------------
//
// Using SNPRelate, infer ancestry based on SNPs. Outputs PDF of PC1 v PC2, text file of pc vals x sample
//
//------------------------------------------------------------------------------------------------------------
process inferAncestry {
tag "inferringAncestry"
publishDir "${OUTDIR}/Output/ancestryPCA"
input:
val vcfFile from runCombineVariantsforinferAncestry
output:
file("*pca.txt") into inferFileForrunCreateSE
file("*pca.pdf") into inferAncestryOutput
script:
outfile_prefix = PREFIX +"_ancestryInference"
gz_suffix = ".gz"
"""
module load R/3.2.3
module load tabix
Rscript ${inferAncestry} ${vcfFile} ${outfile_prefix}
bgzip ${vcfFile}
tabix ${vcfFile}${gz_suffix}
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Perform QC:
// 1) Run FASTQC to assess read quality
// 2) MultiQC on STAR 1st pass and 2nd pass output
//
// ------------------------------------------------------------------------------------------------------------
process runFastQC {
tag "${indivID}|${sampleID}|${libraryID}|${rgID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/Processing/Libraries/${libraryID}/${rgID}/FastQC/"
input:
set indivID, sampleID, libraryID, rgID, platform_unit, platform, platform_model, run_date, center, fastqR1, fastqR2 from readPairsFastQC
output:
set file("*.zip"), file("*.html") into FastQCOutput
script:
"""
module load fastqc/0.11.3
fastqc -t 1 -o . ${fastqR1} ${fastqR2}
"""
}
process runRSeQC {
tag "${indivID}|${sampleID}"
publishDir "${OUTDIR}/${indivID}/${sampleID}/RSeQC/"
input:
set indivID, sampleID, bam from runAddReadGroupInfoOutput_For_RSeQC
output:
file("${sampleID}*") into rseqc_results
file("*.summary.txt") into rseqc_tin_forSE
script:
outfile1 = sampleID + ".bam_stat.txt"
outfile2 = sampleID + ".inferred_experiment.txt"
outfile3 = sampleID + ".read_distribution.txt"
outfile4 = sampleID + ".summary.txt"
outfile5 = sampleID + ".junction_annotation.txt"
"""
module load python/2.7.12
module load rseqc/2.6.4
bam_stat.py -i ${bam} > ${outfile1}
geneBody_coverage.py -i ${bam} -r ${GENE_BED} -o ${sampleID}
junction_annotation.py -i ${bam} -r ${GENE_BED} -o ${sampleID} 2> ${outfile5}
junction_saturation.py -i ${bam} -r ${GENE_BED} -o ${sampleID}
tin.py -i ${bam} -r ${GENE_BED} > ${outfile4}
inner_distance.py -i ${bam} -r ${GENE_BED} -o ${sampleID}
clipping_profile.py -i ${bam} -s "PE" -o ${sampleID}
infer_experiment.py -i ${bam} -r ${GENE_BED} > ${outfile2}
insertion_profile.py -s "PE" -i ${bam} -o ${sampleID}
deletion_profile.py -i ${bam} -l ${READ_LENGTH} -o ${sampleID}
read_distribution.py -i ${bam} -r ${GENE_BED} > ${outfile3}
read_GC.py -i ${bam} -o ${sampleID}
read_duplication.py -i ${bam} -o ${sampleID}
read_NVC.py -i ${bam} -o ${sampleID}
read_quality.py -i ${bam} -o ${sampleID}
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Plot results with multiqc
//
// ------------------------------------------------------------------------------------------------------------
process runMultiQCFastq {
tag "Generating fastq level summary and QC plots"
publishDir "${OUTDIR}/Output/QC/Fastq"
input:
val fastqc_files from FastQCOutput.flatten().toSortedList()
output:
file("fastq_multiqc.html") into runMultiQCFastqOutput
file("fastq_multiqc_data/multiqc_fastqc.txt") into runMultiQCFastqOutputForSE
file("fastq_multiqc_input_files.txt") into runMultiQCFastqOutputFile
script:
"""
module load python/2.7.12
module load multiqc/0.9
echo -e "${fastqc_files.join('\n')}" > fastq_multiqc_input_files.txt
multiqc -n fastq_multiqc --file-list fastq_multiqc_input_files.txt
"""
}
process runMultiQCLibrary {
tag "Generating library level summary and QC plots"
publishDir "${OUTDIR}/Output/QC/Library"
input:
val duplicate_files from runMarkDuplicatesOutput_for_MultiQC.flatten().toSortedList()
output:
file("library_multiqc.html") into runMultiQCLibraryOutput
file("library_multiqc_data/multiqc_picard_dups.txt") into runMultiQCPicardOutputForSE
file("library_multiqc_data/multiqc_general_stats.txt") into runMultiQCGeneralStatsForSE
file("library_multiqc_input_files.txt") into runMultiQCLibraryOutputFile
script:
"""
module load python/2.7.12
module load multiqc/0.9
echo -e "${duplicate_files.join('\n')}" > library_multiqc_input_files.txt
multiqc -n library_multiqc --file-list library_multiqc_input_files.txt
"""
}
process runMultiQCSample {
tag "Generating sample level summary and QC plots"
publishDir "${OUTDIR}/Output/QC/Sample"
input:
val rseqc_files from rseqc_results.flatten().toSortedList()
val star_files from runSTARMultiQCOutput.flatten().toSortedList()
output:
file("sample_multiqc.html") into runMultiQCSampleOutput
file("sample_multiqc_data/multiqc_rseqc_bam_stat.txt") into rseqc_bam_stat_resultsforSE
file("sample_multiqc_data/multiqc_rseqc_infer_experiment.txt") into rseqc_inferred_experiment_resultsforSE
file("sample_multiqc_data/multiqc_rseqc_read_distribution.txt") into rseqc_read_distribution_resultsforSE
file("sample_multiqc_data/multiqc_rseqc_junction_annotation.txt") into rseqc_junction_annotation_resultsforSE
file("sample_multiqc_input_files.txt") into runMultiQCSampleOutputFile
file("sample_multiqc_data/multiqc_star.txt") into rseqc_star_resultsforSE
script:
"""
module load python/2.7.12
module load multiqc/0.9
echo -e "${rseqc_files.join('\n')}" > sample_multiqc_input_files.txt
echo -e "${star_files.join('\n')}" >> sample_multiqc_input_files.txt
multiqc -n sample_multiqc --file-list sample_multiqc_input_files.txt
"""
}
// ------------------------------------------------------------------------------------------------------------
//
// Combine results into SummarizedExperiment object
//
// ------------------------------------------------------------------------------------------------------------
process runCreateSE {
tag "Combining results into SummarizedExperiment object"
publishDir "${OUTDIR}/Output/Expression"
input:
val rseqc_bam_stat_files from rseqc_bam_stat_resultsforSE.flatten().toSortedList()
val fastqc_files from runMultiQCFastqOutputForSE.flatten().toSortedList()
val rseqc_inferred_experiment_files from rseqc_inferred_experiment_resultsforSE.flatten().toSortedList()
val rseqc_read_distribution_files from rseqc_read_distribution_resultsforSE.flatten().toSortedList()
val rseqc_junction_annotation_files from rseqc_junction_annotation_resultsforSE.flatten().toSortedList()
val mark_duplicates_files from runMultiQCPicardOutputForSE.flatten().toSortedList()
val multiqc_files from runMultiQCGeneralStatsForSE.flatten().toSortedList()
val star_files from rseqc_star_resultsforSE.flatten().toSortedList()
val tin_files from rseqc_tin_forSE.flatten().toSortedList()
val genes_files from genesFileForSE.flatten().toSortedList()
val isoforms_files from isoformsFileForSE.flatten().toSortedList()
val infer_files from inferFileForrunCreateSE.flatten().toSortedList()
output:
set file(gene_file), file(iso_file) into runCreateSEOutput
script:
gene_file = PREFIX + "_Gene_Expression.rds"
iso_file = PREFIX + "_Isoform_Expression.rds"
"""
module load R/3.3.2
echo -e "${rseqc_bam_stat_files.join('\n')}" > rseqc_bam_stat.txt
echo -e "${fastqc_files.join('\n')}" > fastqc_files.txt
echo -e "${rseqc_inferred_experiment_files.join('\n')}" > rseqc_inferred_experiment.txt
echo -e "${rseqc_read_distribution_files.join('\n')}" > rseqc_read_distribution.txt
echo -e "${rseqc_junction_annotation_files.join('\n')}" > rseqc_junction_annotation.txt
echo -e "${mark_duplicates_files.join('\n')}" > mark_duplicates.txt
echo -e "${multiqc_files.join('\n')}" > multiqc_files.txt
echo -e "${star_files.join('\n')}" > star_files.txt
echo -e "${tin_files.join('\n')}" > tin_files.txt
echo -e "${genes_files.join('\n')}" > genes_results_files.txt
echo -e "${isoforms_files.join('\n')}" > isoforms_results_files.txt
echo -e "${infer_files.join('\n')}" > infer_files.txt
${CREATE_SE} -a genes_results_files.txt -b isoforms_results_files.txt -c ${demoFile} -d ${inputFile} -e fastqc_files.txt -f mark_duplicates.txt -g rseqc_bam_stat.txt -z multiqc_files.txt -i rseqc_inferred_experiment.txt -x rseqc_junction_annotation.txt -k rseqc_read_distribution.txt -n ${GENE_GTF} -m infer_files.txt -o ${PREFIX} -v star_files.txt -w tin_files.txt
"""
}
workflow.onComplete {
log.info ""
log.info "========================================="
log.info "Duration: $workflow.duration"
log.info "========================================="
}
//#############################################################################################################
//
// FUNCTIONS
//
//#############################################################################################################
//#############################################################################################################
// ------------------------------------------------------------------------------------------------------------
//
// Read input file and save it into list of lists
//
// ------------------------------------------------------------------------------------------------------------
def logParams(p, n) {
File file = new File(n)
file.write "Parameter:\tValue\n"
for(s in p) {
file << "${s.key}:\t${s.value}\n"
}
}