forked from likelet/LncPipe
-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.nf
executable file
·1630 lines (1379 loc) · 65 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
/*
* LncPipe was implemented by Dr. Qi Zhao from Sun Yat-sen University Cancer Center, China.
*
*
* LncPipe 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.
*
* See the GNU General Public License for more details.
*
*
*/
/*
* LncPipe: A nextflow-based lncRNA identification and analysis pipeline from RNA sequencing data
*
* Authors:
* Qi Zhao <zhaoqi@sysucc.org.cn>: design and implement the pipeline.
* Yu Sun <sun_yu@mail.nankai.edu.cn>: design and implement the analysis report sections.
* Zhixiang Zuo <zuozhx@sysucc.org.cn>: design the project and perform the testing.
*/
//pre-defined functions for render command
//=======================================================================================
ANSI_RESET = "\u001B[0m";
ANSI_BLACK = "\u001B[30m";
ANSI_RED = "\u001B[31m";
ANSI_GREEN = "\u001B[32m";
ANSI_YELLOW = "\u001B[33m";
ANSI_BLUE = "\u001B[34m";
ANSI_PURPLE = "\u001B[35m";
ANSI_CYAN = "\u001B[36m";
ANSI_WHITE = "\u001B[37m";
def print_red = { str -> ANSI_RED + str + ANSI_RESET }
def print_black = { str -> ANSI_BLACK + str + ANSI_RESET }
def print_green = { str -> ANSI_GREEN + str + ANSI_RESET }
def print_yellow = { str -> ANSI_YELLOW + str + ANSI_RESET }
def print_blue = { str -> ANSI_BLUE + str + ANSI_RESET }
def print_cyan = { str -> ANSI_CYAN + str + ANSI_RESET }
def print_purple = { str -> ANSI_PURPLE + str + ANSI_RESET }
def print_white = { str -> ANSI_WHITE + str + ANSI_RESET }
//Help information
// Nextflow version
version="v0.2.44"
//=======================================================================================
// Nextflow Version check
if( !nextflow.version.matches('0.30+') ) {
println print_yellow("This workflow requires Nextflow version 0.26 or greater -- You are running version ")+ print_red(nextflow.version)
}
//help information
params.help = null
if (params.help) {
log.info ''
log.info print_purple('------------------------------------------------------------------------')
log.info "LncPipe: a Nextflow-based Long non-coding RNA analysis Pipeline v$version"
log.info "LncPipe integrates several NGS processing tools to identify novel long non-coding RNAs from"
log.info "un-processed RNA sequencing data. To run this pipeline, users either need to install required tools manually"
log.info "or use the docker image for LncPipe that comes with all tools pre-installed. (note: docker needs to be installed on your system). More information on usage can be found at https://github.com/likelet/LncPipe ."
log.info "Bugs or new feature requests can be reported by opening issues in our github repository."
log.info print_purple('------------------------------------------------------------------------')
log.info ''
log.info print_yellow('Usage: ')
log.info print_yellow(' The typical command for running the pipeline is as follows (we do not recommend users passing configuration parameters through command line, please modify the config.file instead):\n') +
print_purple(' Nextflow run LncRNAanalysisPipe.nf \n') +
print_yellow(' General arguments: Input and output setting\n') +
print_cyan(' --inputdir <path> ') + print_green('Path to input data(optional), current path default\n') +
print_cyan(' --reads <*_fq.gz> ') + print_green('Filename pattern for pairing raw reads, e.g: *_{1,2}.fastq.gz for paired reads\n') +
print_cyan(' --out_folder <path> ') + print_green('The output directory where the results will be saved(optional), current path is default\n') +
print_cyan(' --aligner <hisat> ') + print_green('Aligner for reads mapping (optional),"hisat"(defalt)/"star"/"tophat"\n') +
print_cyan(' --qctools <fastp> ') + print_green('Tools for assess reads quality, fastp(default)/afterqc/fastqc/none(skip QC step)\n') +
print_cyan(' --detools <edger> ') + print_green('Tools for differential analysis, edger(default)/deseq/noiseq\n') +
print_cyan(' --quant <kallisto> ') + print_green('Tools for estimating abundance of transcript, kallisto(default)/htseq\n') +
'\n' +
print_yellow(' Options: General options for run this pipeline\n') +
print_cyan(' --merged_gtf <gtffile> ') + print_green('Start analysis with assemblies already produced and skip fastqc/alignment step, DEFAOUL NULL\n') +
print_cyan(' --design <file> ') + print_green('A flat file stored the experimental design information ( required when perform differential expression analysis)\n') +
print_cyan(' --singleEnd ') + print_green('Reads type, True for single ended \n') +
print_cyan(' --unstrand ') + print_green('RNA library construction strategy, specified for \'unstranded\' library \n') +
'\n' +
print_yellow(' References: If not specified in the configuration file or you wish to overwrite any of the references.\n') +
print_cyan(' --fasta ') + print_green('Path to Fasta reference(required)\n') +
print_cyan(' --gencode_annotation_gtf ') + print_green('An annotation file from GENCODE database in GTF format (required)\n') +
print_cyan(' --lncipedia_gtf ') + print_green('An annotation file from LNCipedia database in GTF format (required)\n') +
'\n' +
print_yellow(' LncPipeReporter Options: LncPipeReporter setting \n') +
print_cyan(' --lncRep_Output ') + print_green('Specify report file name, \"report.html\" default.\n') +
print_cyan(' --lncRep_theme ') + print_green('Plot theme setting in interactive plot, \"npg\" default.\n') +
print_cyan(' --lncRep_min_expressed_sample ') + print_green('Minimum expressed gene allowed in each sample, 50 default.\n') +
'\n' +
print_yellow(' Other options: Specify the email and \n') +
print_cyan(' --sam_processor ') + print_green('program to process samfile generated by hisat2 if aligner is hisat2. Default \"sambamba\". \n') +
print_cyan(' --mail ') + print_green('email info for reporting status of your LncPipe execution \n') +
log.info '------------------------------------------------------------------------'
log.info print_yellow('Contact information: zhaoqi@sysucc.org.cn')
log.info print_yellow('Copyright (c) 2013-2017, Sun Yat-sen University Cancer Center.')
log.info '------------------------------------------------------------------------'
exit 0
}
//check parameters
/*
allowed_params = ["inputdir","reads","out_folder","aligner","qctools","detools","quant",
"merged_gtf","design","singleEnd","unstrand",
"fasta","gencode_annotation_gtf","lncipedia_gtf",
"lncRep_Output", "lncRep_theme","lncRep_min_expressed_sample",
"sam_processor","mail"]
params.each { entry ->
if (! allowed_params.contains(entry.key)) {
println("The parameter <${entry}.key> is not known");
System.exit(2);
}
}
*/
//default values
params.inputdir = ''
params.outdir = './'
params.multiqc_config = "$baseDir/assets/multiqc_config.yaml" // for generate qc and alignment result
params.merged_gtf = null// dose merged_gtf provided
singleEnd = params.singleEnd ? true : false
skip_combine = params.skip_combine ? true : false
unstrand = params.unstrand ? true : false
params.mail=false
//Checking parameters
log.info print_purple("You are running LncPipe with the following parameters:")
log.info print_purple("Checking parameters ...")
log.info print_yellow("=====================================")
log.info print_yellow("Species: ") + print_green(params.species)
log.info print_yellow("Fastq file extension: ") + print_green(params.reads)
log.info print_yellow("Design file: ") + print_green(params.design)
log.info print_yellow("Single end : ") + print_green(params.singleEnd)
log.info print_yellow("skip annotation process: ") + print_green(params.skip_combine)
log.info print_yellow("Input folder: ") + print_green(params.inputdir)
log.info print_yellow("Output folder: ") + print_green(params.outdir)
log.info print_yellow("Genome sequence location: ") + print_green(params.fasta)
log.info print_yellow("STAR index path: ") + print_green(params.star_index)
log.info print_yellow("HISAT2 index path: ") + print_green(params.hisat2_index)
log.info print_yellow("bowtie/tophat index path: ") + print_green(params.bowtie2_index)
log.info print_yellow("GENCODE annotation location: ") + print_green(params.gencode_annotation_gtf)
log.info print_yellow("lncipedia annotation location: ") + print_green(params.lncipedia_gtf)
log.info print_yellow("=====================================")
log.info "\n"
// run information of system file
//automatic set optimize resource for analysis based on current system resources
// read file
fasta = file(params.fasta)
if (!fasta.exists()) exit 1, "Reference genome not found: ${params.fasta}"
if(params.aligner=='star'){
star_index = file(params.star_index)
if (!star_index.exists()) exit 1, "STAR index not found: ${params.star_index}"
}else if(params.aligner =='hisat'){
hisat2_index = Channel.fromPath("${params.hisat2_index}*")
.ifEmpty { exit 1, "HISAT2 index not found: ${params.hisat2_index}" }
}else if(params.aligner =='tophat'){
bowtie2_index = Channel.fromPath("${params.bowtie2_index}*")
.ifEmpty { exit 1, "bowtie2 index for tophat not found: ${params.bowtie2_index}" }
}
inputdir = params.inputdir
multiqc_config = file(params.multiqc_config)
/*
*Step 1: Prepare Annotations
*/
println print_purple("Combining known annotations from GTFs")
if (params.species=="human") {
gencode_annotation_gtf = file(params.gencode_annotation_gtf)
if (!gencode_annotation_gtf.exists()) exit 1, "GENCODE annotation file not found: ${params.gencode_annotation_gtf}"
lncipedia_gtf = file(params.lncipedia_gtf)
if (!lncipedia_gtf.exists()) exit 1, "lncipedia annotation file not found: ${params.lncipedia_gtf}"
//Prepare annotations
annotation_channel = Channel.from(gencode_annotation_gtf, lncipedia_gtf)
annotation_channel.collectFile { file -> ['lncRNA.gtflist', file.name + '\n'] }
.set { LncRNA_gtflist }
process combine_public_annotation {
storeDir { params.outdir + "/Combined_annotations" }
input:
file lncRNA_gtflistfile from LncRNA_gtflist
file gencode_annotation_gtf
file lncipedia_gtf
output:
file "gencode_protein_coding.gtf" into proteinCodingGTF, proteinCodingGTF_forClass
file "known.lncRNA.gtf" into KnownLncRNAgtf
file "*_mod.gtf" into mod_file_for_rename
shell:
if(params.aligner=='hisat'){//fix the gtf format required by hisat
'''
set -o pipefail
touch filenames.txt
perl -lpe 's/ ([^"]\\S+) ;/ "$1" ;/g' !{gencode_annotation_gtf} > gencode_annotation_gtf_mod.gtf
perl -lpe 's/ ([^"]\\S+) ;/ "$1" ;/g' !{lncipedia_gtf} > lncipedia_mod.gtf
echo gencode_annotation_gtf_mod.gtf >>filenames.txt
echo lncipedia_mod.gtf >>filenames.txt
stringtie --merge -o merged_lncRNA.gtf filenames.txt
cat gencode_annotation_gtf_mod.gtf |grep "protein_coding" > gencode_protein_coding.gtf
gffcompare -r gencode_protein_coding.gtf -p !{task.cpus} merged_lncRNA.gtf
awk '$3 =="u"||$3=="x"{print $5}' gffcmp.merged_lncRNA.gtf.tmap |sort|uniq|perl !{baseDir}/bin/extract_gtf_by_name.pl merged_lncRNA.gtf - > merged.filter.gtf
mv merged.filter.gtf known.lncRNA.gtf
'''
}else {
'''
set -o pipefail
cuffmerge -o merged_lncRNA !{lncRNA_gtflistfile}
cat !{gencode_annotation_gtf} |grep "protein_coding" > gencode_protein_coding.gtf
cuffcompare -o merged_lncRNA -r gencode_protein_coding.gtf -p !{task.cpus} merged_lncRNA/merged.gtf
awk '$3 =="u"||$3=="x"{print $5}' merged_lncRNA/merged_lncRNA.merged.gtf.tmap |sort|uniq|perl !{baseDir}/bin/extract_gtf_by_name.pl merged_lncRNA/merged.gtf - > merged.filter.gtf
mv merged.filter.gtf known.lncRNA.gtf
'''
}
}
}
else {// for mouse or other species, user should provide known_protein_coding and known_lncRNA GTF file for analysis
KnownLncRNAgtf=file(params.known_lncRNA_gtf)
if (!KnownLncRNAgtf.exists()) exit 1, print_red("In non-human mode, known lncRNA GTF annotation file not found: ${params.known_lncRNA_gtf}")
known_coding_gtf=file(params.known_coding_gtf)
if (!known_coding_gtf.exists()) exit 1, print_red("In non-human mode, known protein coding GTF annotation file not found: ${params.known_coding_gtf}")
gencode_annotation_gtf = file(params.gencode_annotation_gtf)
if (!gencode_annotation_gtf.exists()) exit 1, print_red("GENCODE annotation file not found: ${params.gencode_annotation_gtf}")
gencode_annotation_gtf.into{proteinCodingGTF; proteinCodingGTF_forClass}
knownLncRNAgtf.set{knownLncRNAgtf}
}
// whether the merged gtf have already produced.
if (!params.merged_gtf) {
/*
* Step 2: Build read aligner (STAR/tophat/HISAT2) index, if not provided
*/
//star_index if not exist
/*if (params.aligner == 'star' && params.star_index == false && fasta) {
process Make_STARindex {
tag fasta
storeDir { params.outdir + "/STARIndex" }
input:
file fasta from fasta
file gencode_annotation_gtf
output:
file "star_index" into star_index
shell:
star_threads = ava_cpu- 1
"""
mkdir star_index
STAR \
--runMode genomeGenerate \
--runThreadN ${star_threads} \
--sjdbGTFfile $gencode_annotation_gtf \
--sjdbOverhang 149 \
--genomeDir star_index/ \
--genomeFastaFiles $fasta
"""
}
} else if (params.aligner == 'star' && params.star_index == false && !fasta) {
println print_red("No reference fasta sequence loaded! please specify ") + print_red("--fasta") + print_red(" with reference.")
} else if (params.aligner == 'tophat' && params.bowtie2_index == false && !fasta) {
process Make_bowtie2_index {
tag fasta
storeDir { params.outdir + "/bowtie2Index" }
input:
file fasta from fasta
output:
file "genome_bt2.*" into bowtie2_index
shell:
"""
bowtie2-build !{fasta} genome_bt2
"""
}
} else if (params.aligner == 'tophat' && !fasta) {
println print_red("No reference fasta equence loaded! please specify ") + print_red("--fasta") + print_red(" with reference.")
} else if (params.aligner == 'hisat' && !fasta) {
process Make_hisat_index {
tag fasta
storeDir { params.outdir + "/hisatIndex" }
input:
file fasta from fasta
file gencode_annotation_gtf
output:
file "genome_ht2.*" into hisat2_index
shell:
hisat2_index_threads = ava_cpu- 1
"""
#for human genome it will take more than 160GB memory and take really long time (6 more hours), thus we recommand to down pre-build genome from hisat website
extract_splice_sites.py !{gencode_annotation_gtf} >genome_ht2.ss
extract_exons.py !{gencode_annotation_gtf} > genome_ht2.exon
hisat2-build -p !{hisat2_index_threads} --ss genome_ht2.ss --exo genome_ht2.exon !{fasta} genome_ht2
"""
}
} else if (params.aligner == 'tophat' && params.hisat_index == false && !fasta) {
println print_red("No reference fasta sequence loaded! please specify ") + print_red("--fasta") + print_red(" with reference.")
}*/
println print_purple("Analysis from fastq file")
//Match the pairs on two channels
reads = params.inputdir + params.reads
/*
* Step 3: QC (FastQC/AfterQC/Fastp) of raw reads
*/
println print_purple("Perform quality control of raw fastq files ")
if (params.qctools == 'fastqc') {
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Cannot find any reads matching: ${reads}\nNB: Path needs to be enclosed in quotes!\n")
}
.into { reads_for_fastqc; readPairs_for_discovery;readPairs_for_kallisto}
process Run_fastQC {
tag { fastq_tag }
label 'qc'
publishDir pattern: "*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "*.html" into fastqc_for_waiting
shell:
fastq_tag = samplename
'''
fastqc -t !{task.cpus} !{fastq_file[0]} !{fastq_file[1]}
'''
}
}
else if (params.qctools == 'afterqc'){
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Cannot find any reads matching: ${reads}\nPlz check your fasta string in nextflow.config file \n")
}.set { reads_for_fastqc}
process Run_afterQC {
tag { fastq_tag }
label 'qc'
publishDir pattern: "QC/*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "QC/*.html" into fastqc_for_waiting
set val(fastq_tag), file('*.good.fq.gz') into readPairs_for_discovery,readPairs_for_kallisto
shell:
fastq_tag = samplename
if (params.singleEnd) {
'''
after.py -z -1 !{fastq_file[0]} -g ./
'''
} else {
'''
after.py -z -1 !{fastq_file[0]} -2 !{fastq_file[1]} -g ./
'''
}
}
}
else if (params.qctools == 'fastp'){
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Cannot find any reads matching: ${reads}\nPlz check your fasta string in nextflow.config file \n")
}
.set { reads_for_fastqc}
process Run_FastP {
tag { fastq_tag }
label 'qc'
publishDir pattern: "*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "*.html" into fastqc_for_waiting
set val(fastq_tag), file('*qc.fq.gz') into readPairs_for_discovery,readPairs_for_kallisto
shell:
fastq_tag = samplename
if (params.singleEnd) {
'''
fastp -i !{fastq_file[0]} -o !{samplename}.qc.gz -h !{samplename}_fastp.html
'''
} else {
'''
fastp -i !{fastq_file[0]} -I !{fastq_file[1]} -o !{samplename}_1.qc.fq.gz -O !{samplename}_2.qc.fq.gz -h !{samplename}_fastp.html
'''
}
}
}else{
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Cannot find any reads matching: ${reads}\nPlz check your fasta string in nextflow.config file \n")
}
.into{readPairs_for_discovery; readPairs_for_kallisto;fastqc_for_waiting}
}
fastqc_for_waiting = fastqc_for_waiting.first()
/*
* Step 4: Initialize read alignment (STAR/HISAT2/tophat)
*/
if (params.aligner == 'star') {
process fastq_star_alignment_For_discovery {
tag { file_tag }
publishDir pattern: "",
path: { params.outdir + "/Star_alignment" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(pair) from readPairs_for_discovery
file tempfiles from fastqc_for_waiting // just for waiting
file fasta
file star_index
output:
set val(file_tag_new), file("${file_tag_new}Aligned.sortedByCoord.out.bam") into mappedReads,forHtseqMappedReads
file "${file_tag_new}Log.final.out" into alignment_logs
shell:
println print_purple("Start mapping with STAR aligner " + samplename)
file_tag = samplename
file_tag_new = file_tag
if (params.singleEnd) {
println print_purple("Initial reads mapping of " + samplename + " performed by STAR in single-end mode")
"""
STAR --runThreadN !{task.cpus} \
--twopassMode Basic \
--genomeDir !{star_index} \
--readFilesIn !{pair} \
--readFilesCommand zcat \
--outSAMtype BAM SortedByCoordinate \
--chimSegmentMin 20 \
--outFilterIntronMotifs RemoveNoncanonical \
--outFilterMultimapNmax 20 \
--alignIntronMin 20 \
--alignIntronMax 1000000 \
--alignMatesGapMax 1000000 \
--outFilterType BySJout \
--alignSJoverhangMin 8 \
--alignSJDBoverhangMin 1 \
--outFileNamePrefix !{file_tag_new}
"""
} else {
println print_purple("Initial reads mapping of " + samplename + " performed by STAR in paired-end mode")
'''
STAR --runThreadN !{task.cpus} \
--twopassMode Basic --genomeDir !{star_index} \
--readFilesIn !{pair[0]} !{pair[1]} \
--readFilesCommand zcat \
--outSAMtype BAM SortedByCoordinate \
--chimSegmentMin 20 \
--outFilterIntronMotifs RemoveNoncanonical \
--outFilterMultimapNmax 20 \
--alignIntronMin 20 \
--alignIntronMax 1000000 \
--alignMatesGapMax 1000000 \
--outFilterType BySJout \
--alignSJoverhangMin 8 \
--alignSJDBoverhangMin 1 \
--outFileNamePrefix !{file_tag_new}
'''
}
}
}
else if (params.aligner == 'tophat')
{
process fastq_tophat_alignment_For_discovery {
tag { file_tag }
publishDir pattern: "",
path: { params.outdir + "/tophat_alignment" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(pair) from readPairs_for_discovery
file tempfiles from fastqc_for_waiting // just for waiting
file fasta
file bowtie2_index from bowtie2_index.collect()
file gtf from gencode_annotation_gtf
output:
set val(samplename),file("${file_tag_new}_thout/accepted.bam") into mappedReads,forHtseqMappedReads
file "${file_tag_new}_thout/Alignment_summary.txt" into alignment_logs
//align_summary.txt as log file
shell:
println print_purple("Start mapping with tophat2 aligner " + samplename)
file_tag = samplename
file_tag_new = file_tag
index_base = bowtie2_index[0].toString() - ~/.\d.bt2/
strand_str="fr-firststrand"
if(unstrand){
strand_str="fr-unstranded"
}
if (params.singleEnd) {
println print_purple("Initial reads mapping of " + samplename + " performed by Tophat in single-end mode")
'''
tophat -p !{task.cpus} -G !{gtf} -–no-novel-juncs -o !{samplename}_thout --library-type !{strand_str} !{index_base} !{pair}
'''
} else {
println print_purple("Initial reads mapping of " + samplename + " performed by Tophat in paired-end mode")
'''
tophat -p !{task.cpus} -G !{gtf} -–no-novel-juncs -o !{samplename}_thout --library-type !{strand_str} !{index_base} !{pair[0]} !{pair[1]}
'''
}
}
}
else if (params.aligner == 'hisat') {
process fastq_hisat2_alignment_For_discovery {
tag { file_tag }
label 'para'
publishDir pattern: "",
path: { params.outdir + "/hisat_alignment" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(pair) from readPairs_for_discovery
file tempfiles from fastqc_for_waiting // just for waiting
file fasta
file hisat2_id from hisat2_index.collect()
output:
set val(file_tag_new),file("${file_tag_new}.sort.bam") into hisat_mappedReads,forHtseqMappedReads
file "${file_tag_new}.hisat2_summary.txt" into alignment_logs
//align_summary.txt as log file
shell:
println print_purple("Start mapping with hisat2 aligner " + samplename)
file_tag = samplename
file_tag_new = file_tag
index_base = hisat2_id[0].toString() - ~/.\d.ht2/
if(unstrand){
if (params.singleEnd) {
println print_purple("Initial reads mapping of " + samplename + " performed by hisat2 in single-end mode")
'''
mkdir tmp
hisat2 -p !{task.cpus} --dta -x !{index_base} -U !{pair} -S !{file_tag_new}.sam 2>!{file_tag_new}.hisat2_summary.txt
sambamba view -S -f bam -t !{task.cpus} !{file_tag_new}.sam -o temp.bam
sambamba sort -o !{file_tag_new}.sort.bam --tmpdir ./tmp -t !{task.cpus} temp.bam
rm !{file_tag_new}.sam
rm temp.bam
'''
} else {
println print_purple("Initial reads mapping of " + samplename + " performed by hisat2 in paired-end mode")
'''
mkdir tmp
hisat2 -p !{task.cpus} --dta -x !{index_base} -1 !{pair[0]} -2 !{pair[1]} -S !{file_tag_new}.sam 2> !{file_tag_new}.hisat2_summary.txt
sambamba view -S -f bam -t !{hisat2_threads} !{file_tag_new}.sam -o temp.bam
sambamba sort -o !{file_tag_new}.sort.bam --tmpdir ./tmp -t !{task.cpus} temp.bam
rm !{file_tag_new}.sam
'''
}
}else {
if (params.singleEnd) {
println print_purple("Initial reads mapping of " + samplename + " performed by hisat2 in single-end mode")
'''
mkdir tmp
hisat2 -p !{task.cpus} --dta --rna-strandness !{params.hisat_strand} -x !{index_base} -U !{pair} -S !{file_tag_new}.sam 2>!{file_tag_new}.hisat2_summary.txt
sambamba view -S -f bam -t !{hisat2_threads} !{file_tag_new}.sam -o temp.bam
sambamba sort -o !{file_tag_new}.sort.bam --tmpdir ./tmp -t !{hisat2_threads} temp.bam
rm !{file_tag_new}.sam
rm temp.bam
'''
} else {
println print_purple("Initial reads mapping of " + samplename + " performed by hisat2 in paired-end mode")
'''
mkdir tmp
hisat2 -p !{task.cpus} --dta --rna-strandness !{params.hisat_strand} -x !{index_base} -1 !{pair[0]} -2 !{pair[1]} -S !{file_tag_new}.sam 2> !{file_tag_new}.hisat2_summary.txt
sambamba view -S -f bam -t !{task.cpus} !{file_tag_new}.sam -o temp.bam
sambamba sort -o !{file_tag_new}.sort.bam --tmpdir ./tmp -t !{task.cpus} temp.bam
rm !{file_tag_new}.sam
'''
}
}
}
}
/*
* Step 5: Transcript assembly using Stringtie
*/
if(params.aligner == 'hisat'){
process StringTie_assembly {
tag { file_tag }
input:
set val(samplename),file(alignment_bam) from hisat_mappedReads
file fasta
file gencode_annotation_gtf
output:
file "stringtie_${file_tag_new}_transcripts.gtf" into stringTieoutgtf, StringTieOutGtf_fn
shell:
file_tag = samplename
file_tag_new = file_tag
if(unstrand){
'''
#run stringtie
stringtie -p !{task.cpus} -G !{gencode_annotation_gtf} -l stringtie_!{file_tag_new} -o stringtie_!{file_tag_new}_transcripts.gtf !{alignment_bam}
'''
}else{
'''
#run stringtie
stringtie -p !{task.cpus} -G !{gencode_annotation_gtf} --rf -l stringtie_!{file_tag_new} -o stringtie_!{file_tag_new}_transcripts.gtf !{alignment_bam}
'''
}
}
// Create a file 'gtf_filenames' containing the filenames of each post processes cufflinks gtf
stringTieoutgtf.collectFile { file -> ['gtf_filenames.txt', file.name + '\n'] }
.set { GTFfilenames }
/*
* Step 6: Merged GTFs into one
*/
process StringTie_merge_assembled_gtf {
tag { file_tag }
label 'para'
publishDir pattern: "merged.gtf",
path: { params.outdir + "/Merged_assemblies" }, mode: 'copy', overwrite: true
input:
file gtf_filenames from GTFfilenames
file cufflinksgtf_file from StringTieOutGtf_fn.toList() // not used but just send the file in current running folder
file fasta
output:
file "merged.gtf" into mergeTranscripts_forCompare, mergeTranscripts_forExtract, mergeTranscripts_forCodeingProtential
shell:
'''
stringtie --merge -p !{task.cpus} -o merged.gtf !{gtf_filenames}
'''
}
}
else{
process Cufflinks_assembly {
tag { file_tag }
input:
set val(file_tag), file(alignment_bam) from mappedReads
file fasta
file gencode_annotation_gtf
output:
file "Cufout_${file_tag_new}_transcripts.gtf" into cuflinksoutgtf, cuflinksoutgtf_fn
shell:
file_tag_new = file_tag
strand_str="fr-firststrand"
if(unstrand){
strand_str="fr-unstranded"
}
if (params.aligner == 'tophat') {
'''
#run cufflinks
cufflinks -g !{gencode_annotation_gtf} \
-b !{fasta} \
--library-type !{strand_str}\
--max-multiread-fraction 0.25 \
--3-overhang-tolerance 2000 \
-o Cufout_!{file_tag_new} \
-p !{task.cpus} !{alignment_bam}
mv Cufout_!{file_tag_new}/transcripts.gtf Cufout_!{file_tag_new}_transcripts.gtf
'''
} else if (params.aligner == 'star') {
'''
#run cufflinks
cufflinks -g !{gencode_annotation_gtf} \
-b !{fasta} \
--library-type !{strand_str} \
--max-multiread-fraction 0.25 \
--3-overhang-tolerance 2000 \
-o Cufout_!{file_tag_new} \
-p !{task.cpus} !{alignment_bam}
mv Cufout_!{file_tag_new}/transcripts.gtf Cufout_!{file_tag_new}_transcripts.gtf
'''
}
}
// Create a file 'gtf_filenames' containing the filenames of each post processes cufflinks gtf
cuflinksoutgtf.collectFile { file -> ['gtf_filenames.txt', file.name + '\n'] }
.set { GTFfilenames }
/*
* Step 6: Merged GTFs into one
*/
process cuffmerge_assembled_gtf {
tag { file_tag }
label 'para'
publishDir pattern: "CUFFMERGE/merged.gtf",
path: { params.outdir + "/All_assemblies" }, mode: 'copy', overwrite: true
input:
file gtf_filenames from GTFfilenames
file cufflinksgtf_file from cuflinksoutgtf_fn.toList() // not used but just send the file in current running folder
file fasta
output:
file "CUFFMERGE/merged.gtf" into mergeTranscripts_forCompare, mergeTranscripts_forExtract, mergeTranscripts_forCodeingProtential
shell:
'''
mkdir CUFFMERGE
cuffmerge -o CUFFMERGE \
-s !{fasta} \
-p !{task.cpus} \
!{gtf_filenames}
'''
}
}
}
else {
println print_yellow("Raw reads quality check step was skipped due to provided ") + print_green("--merged_gtf") + print_yellow(" option\n")
println print_yellow("Reads mapping step was skipped due to provided ") + print_green("--merged_gtf") + print_yellow(" option\n")
merged_gtf = file(params.merged_gtf)
Channel.fromPath(merged_gtf)
.ifEmpty { exit 1, "Cannot find merged gtf : ${merged_gtf}" }
.into {
mergeTranscripts_forCompare; mergeTranscripts_forExtract; mergeTranscripts_forCodeingProtential
}
// add fastq when do quantification
reads = params.inputdir + params.reads
if (params.qctools == 'fastqc') {
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Fastq file not found, plz check your file path : ${reads}\n")
}
.into { reads_for_fastqc; readPairs_for_discovery;readPairs_for_kallisto}
process Run_fastQC_2 {
tag { fastq_tag }
label 'qc'
publishDir pattern: "*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "*.html" into fastqc_for_waiting
shell:
fastq_tag = samplename
'''
fastqc -t !{task.cpus} !{fastq_file[0]} !{fastq_file[1]}
'''
}
}
else if (params.qctools == 'afterqc'){
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Fastq file not found : ${reads}\nPlz check your reads string in nextflow.config file \n")
}
.set { reads_for_fastqc}
process Run_afterQC_2 {
tag { fastq_tag }
label 'qc'
publishDir pattern: "QC/*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "QC/*.html" into fastqc_for_waiting
set val(fastq_tag), file('*.good.fq.gz') into readPairs_for_discovery,readPairs_for_kallisto
shell:
fastq_tag = samplename
if (params.singleEnd) {
'''
after.py -z -1 !{fastq_file[0]} -g ./
'''
} else {
'''
after.py -z -1 !{fastq_file[0]} -2 !{fastq_file[1]} -g ./
'''
}
}
}
else if (params.qctools == 'fastp'){
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Fastq file not found : ${reads}\nPlz check your reads string in nextflow.config file \n")
}
.set { reads_for_fastqc}
process Run_FastP_2 {
tag { fastq_tag }
label 'qc'
publishDir pattern: "*.html",
path: { params.outdir + "/QC" }, mode: 'copy', overwrite: true
input:
set val(samplename), file(fastq_file) from reads_for_fastqc
output:
file "*.html" into fastqc_for_waiting
set val(fastq_tag), file('*qc.fq.gz') into readPairs_for_discovery,readPairs_for_kallisto
shell:
fastq_tag = samplename
if (params.singleEnd) {
'''
fastp -i !{fastq_file[0]} -o !{samplename}.qc.gz -h !{samplename}_fastp.html
'''
} else {
'''
fastp -i !{fastq_file[0]} -I !{fastq_file[1]} -o !{samplename}_1.qc.fq.gz -O !{samplename}_2.qc.fq.gz -h !{samplename}_fastp.html
'''
}
}
}
else{
Channel.fromFilePairs(reads, size: params.singleEnd ? 1 : 2)
.ifEmpty {
exit 1, print_red("Cannot find any reads matching: ${reads}\nPlz check your reads string in nextflow.config file \n")
}
.into{readPairs_for_discovery; readPairs_for_kallisto;fastqc_for_waiting}
}
fastqc_for_waiting2 = fastqc_for_waiting.first()
}
/*
*Step 7: Compare assembled gtf with known annotations (GENCODE)
*/
process Merge_assembled_gtf_with_GENCODE {
tag { file_tag }
input:
file mergeGtfFile from mergeTranscripts_forCompare
file gencode_annotation_gtf
output:
file "merged_lncRNA.merged.gtf.tmap" into comparedGTF_tmap
shell:
'''
#!/bin/sh
gffcompare -r !{gencode_annotation_gtf} -p !{task.cpus} !{mergeGtfFile} -o merged_lncRNA
'''
}
/*
*Step 8: Filter GTFs to distinguish novel lncRNAs
*/
process Identify_novel_lncRNA_with_criterions {
input:
file comparedTmap from comparedGTF_tmap
file fasta
file mergedGTF from mergeTranscripts_forExtract
output:
file "novel.gtf.tmap" into noveltmap
file "novel.longRNA.fa" into novelLncRnaFasta
file "novel.longRNA.exoncount.txt" into novelLncRnaExonCount
shell:
'''
# filtering novel lncRNA based on cuffmerged trascripts
awk '$3 =="x"||$3=="u"||$3=="i"{print $0}' !{comparedTmap} > novel.gtf.tmap
# excluding length smaller than 200 nt
awk '$10 >200{print}' novel.gtf.tmap > novel.longRNA.gtf.tmap
# extract gtf
awk '{print $5}' novel.longRNA.gtf.tmap |perl !{baseDir}/bin/extract_gtf_by_name.pl !{mergedGTF} - >novel.longRNA.gtf
awk '{if($3=="exon"){print $0}}' novel.longRNA.gtf > novel.longRNA.format.gtf
perl !{baseDir}/bin/get_exoncount.pl novel.longRNA.format.gtf > novel.longRNA.exoncount.txt
# gtf2gff3
#check whether required
# get fasta from gtf
gffread novel.longRNA.gtf -g !{fasta} -w novel.longRNA.fa -W
'''
}
/*
*Step 9: Predict coding potential abilities using CPAT and PLEK (CNCI functionality coming soon!)
*/
novelLncRnaFasta.into { novelLncRnaFasta_for_PLEK; novelLncRnaFasta_for_CPAT; }
process Predict_coding_abilities_by_PLEK {
// as PLEK can not return valid exit status even run smoothly, we manually set the exit status into 0 to promote analysis
validExitStatus 0, 1, 2
input:
file novel_lncRNA_fasta from novelLncRnaFasta_for_PLEK
output:
file "novel.longRNA.PLEK.out" into novel_longRNA_PLEK_result
shell:
'''
PLEK.py -fasta !{novel_lncRNA_fasta} \
-out novel.longRNA.PLEK.out \
-thread !{task.cpus}
exit 0
'''
}
process Predict_coding_abilities_by_CPAT {
input:
file novel_lncRNA_fasta from novelLncRnaFasta_for_CPAT
output:
file "novel.longRNA.CPAT.out" into novel_longRNA_CPAT_result
shell:
if(params.species=="human"){
'''
cpat.py -g !{novel_lncRNA_fasta} \
-x !{baseDir}/bin/cpat_model/Human_Hexamer.tsv \
-d !{baseDir}/bin/cpat_model/Human_logitModel.RData \
-o novel.longRNA.CPAT.out
'''
}else if (params.species=="mouse"){
'''
cpat.py -g !{novel_lncRNA_fasta} \
-x !{baseDir}/bin/cpat_model/Mouse_Hexamer.tsv \
-d !{baseDir}/bin/cpat_model/Mouse_logitModel.RData \
-o novel.longRNA.CPAT.out
'''
}else if (params.species=="zebrafish"){
'''
cpat.py -g !{novel_lncRNA_fasta} \
-x !{baseDir}/bin/cpat_model/zebrafish_Hexamer.tsv \