forked from lillamy2012/rna_seq_kallisto1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rna_seq1.nf
561 lines (443 loc) · 14.2 KB
/
rna_seq1.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
#!/usr/bin/env nextflow
/**************
* Parameters
**************/
params.type = "bam"
params.files = "../bams/*.bam" //"../fastq/*_{1,2}.fastq"
params.fragment_len = '180'
params.fragment_sd = '20'
params.bootstrap = '100'
params.seqtype = 'SR' // 'PR'
params.strand = 'rf-stranded'// "fr-stranded" NULL
params.output = "results/"
params.bam_out = "result_bams"
params.info = 'info.tab' // name, type, condition
params.anno_set = "tair10"// "araport_genes" // "tair10_TE"
params.contrast = "contrasts.tab" //'NO_FILE' // SET to N0_FILE to skip DE analyisis
params.pvalue = 0.1
params.filter = 0 // no filter
params.binsize = 10
/***************
* annotation set selection
***************/
if(params.anno_set == "tair10"){
params.fasta_dna = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa")
params.gtf = file("/lustre/scratch/projects/berger_common/backup_berger_common/gtf/Arabidopsis_thaliana.TAIR10.35.gtf")
params.fasta = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Arabidopsis_thaliana.TAIR10.cdna.all.fa")
params.starDir = "star_tair10"
params.kallistoDir = "tair10_transcripts.idx"
params.normtosize = '119146348'
params.txdb="tair10"
}
if(params.anno_set == "tair10_TE"){
params.fasta_dna = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa")
params.gtf = file("/lustre/scratch/projects/berger_common/backup_berger_common/gtf/TAIR10_TE.gtf")
params.fasta = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/TAIR10_TEfamilies.fa")
params.starDir = "star_tair10TE"
params.kallistoDir = "tair10_TE.idx"
params.normtosize = '119146348'
params.txdb=file("/lustre/scratch/projects/berger_common/backup_berger_common/tair10_TE.txdb")
}
if(params.anno_set == "araport_genes"){
params.fasta_dna = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa")
params.gtf = file("/lustre/scratch/projects/berger_common/backup_berger_common/gtf/Araport11_GFF3_genes_transposons.201606.gtf")
params.fasta = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Araport11_genes.201606.cdna.fasta.gz")
params.starDir = "star_araport"
params.kallistoDir = "araport_genes.idx"
params.normtosize = '119146348'
params.txdb=file("/lustre/scratch/projects/berger_common/backup_berger_common/araport11.txdb")
}
if(params.anno_set == "tair10_genes_TE"){
params.fasta_dna = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa")
params.gtf = file("/lustre/scratch/projects/berger_common/backup_berger_common/gtf/TAIR10.cdna.all.with.TEfamilies.gtf")
params.fasta = file("/lustre/scratch/projects/berger_common/backup_berger_common/fasta/TAIR10.cdna.all.with.TEfamilies.fa")
params.starDir = "star_tair10_TE"
params.kallistoDir = "tair10_TE_genes.idx"
params.normtosize = '119146348'
params.txdb= file("/lustre/scratch/projects/berger_common/backup_berger_common/tair10_gene_TE.txdb")
}
/**************************
* pipeline info and documentation
***************************/
report = file("report/deseq2.Rmd") // not sure why I set this here
log.info "RNA-SEQ N F ~ version 0.1"
log.info "====================================="
log.info "files : ${params.files}" // want to add mdsums
log.info "fragment length : ${params.fragment_len}"
log.info "fragment sd : ${params.fragment_sd}"
log.info "bootstrap : ${params.bootstrap}"
log.info "seq type : ${params.seqtype}"
log.info "strandness : ${params.strand}"
log.info "output : ${params.output}"
log.info "sample info : ${params.info}"
log.info "annotations : ${params.anno_set}"
log.info "contrasts : ${params.contrast}"
log.info "p-value : ${params.pvalue}"
log.info "counts filter :${params.filter}"
log.info "norm. size : ${params.normtosize}"
log.info "binsize : ${params.binsize}"
log.info "txdb : ${params.txdb}"
log.info "fasta dna : ${params.fasta_dna}"
log.info "fasta : ${params.fasta}"
log.info "\n"
new File('param.txt').delete()
def file1 = new File('param.txt')
file1 << "RNA-SEQ N F ~ version 0.1 \n"
file1 << "===================================== \n"
file1 << "files : ${params.files} \n"
file1 << "fragment length : ${params.fragment_len} \n"
file1 << "fragment sd : ${params.fragment_sd} \n"
file1 << "bootstrap : ${params.bootstrap} \n"
file1 << "seq type : ${params.seqtype} \n"
file1 << "strandness : ${params.strand} \n"
file1 << "output : ${params.output} \n"
file1 << "sample info : ${params.info} \n"
file1 << "annotations : ${params.anno_set} \n"
file1 << "contrasts : ${params.contrast} \n"
file1 << "p-value : ${params.pvalue} \n"
file1 << "counts filter :${params.filter} \n"
file1 << "norm. size : ${params.normtosize} \n"
file1 << "binsize : ${params.binsize} \n"
file1 << "txdb : ${params.txdb} \n"
file1 << "fasta dna : ${params.fasta_dna} \n"
file1 << "fasta : ${params.fasta} \n"
mypar = file('param.txt')
mod = file('nextflow.config') //modules loaded
/*********************************************
**********************************************
ANALYSIS START
**********************************************
*********************************************/
/*
* Input parameters validation
*/
design = file(params.info)
contrasts = file(params.contrast)
fasta = file(params.fasta)
gtf = file(params.gtf)
/*
* validate input files
*/
if( !design.exists() ) exit 1, "Missing sample info file: ${design}"
/***********************
* Channel for bam/fastq files
***********************/
if(params.type=="bam"){
files = Channel
.fromPath(params.files)
.map { file -> [ id:file.baseName,file:file] }
}
if(params.type=="fastq"){
// paired or single, but single have to be names _1.fq
files =
Channel
.fromFilePairs( params.files, size: -1 )
.ifEmpty { error "Cannot find any reads matching: ${params.files}" }
}
files.into { files; files2}
/***********************
* Keeping track of moduls
************************/
process track {
input:
file mod
output:
file "modules.txt" into modules
script:
"""
grep "module" ${mod} | tr -d ' ' | tr -d \"\t" | sed 's/module=//g' | tr -d \\' | awk 'BEGIN{RS=":"} {print}' | sort | uniq > modules.txt
"""
}
/**********************
* SORT BAM
**********************/
process sortBam {
tag "sort: $id"
input:
set id, file(bam) from files
output:
set id, file("${id}.sort.bam") into bam_sorted
when:
params.type=="bam"
script:
"""
samtools sort -n $bam -o ${id}.sort.bam
"""
}
/*********************
* BAM TO FASTQ
*********************/
process generateFastq {
tag "bam : $name, type:$params.seqtype"
input:
set name, file(bam) from bam_sorted
output:
set name, file('*.fastq') into fastqs
script:
if (params.seqtype=='SR'){
"""
bedtools bamtofastq -i ${bam} -fq ${name}_1.fastq
"""
}
else {
"""
bedtools bamtofastq -i ${bam} -fq ${name}_1.fastq -fq2 ${name}_2.fastq
"""
}
}
/***********************
* COPY CHANNEL
***********************/
if(params.type=="fastq"){
fastqs = fastqs.mix(files2)
}
fastqs.into { fastqs_kallisto; fastqs_star }
/***********************
* KALLISTO INDEX IF NEEDED
************************/
process kallistoIndex {
tag "dir: $params.kallistoDir"
storeDir '/lustre/scratch/projects/berger_common/backup_berger_common'
input:
file fasta
output:
file "${params.kallistoDir}" into transcriptome_index
script:
"""
kallisto index -i ${params.kallistoDir} ${fasta}
"""
}
/*************************
* KALLIST QUANT
*************************/
process quantKallisto {
tag "fq: $name "
publishDir "$params.output/kallisto_output" , mode: 'copy'
input:
file index from transcriptome_index
set name, file(fq) from fastqs_kallisto
output:
file "kallisto_${name}" into kallisto_dirs
script:
def single = fq instanceof Path
if( single && params.strand ==null) {
"""
mkdir kallisto_${name}
kallisto quant -i ${index} -o kallisto_${name} --single -l ${params.fragment_len} -s ${params.fragment_sd} -b ${params.bootstrap} ${fq}
"""
}
else if( single ){
"""
mkdir kallisto_${name}
kallisto quant -i ${index} -o kallisto_${name} --${params.strand} --single -l ${params.fragment_len} -s ${params.fragment_sd} -b ${params.bootstrap} ${fq}
"""
}
else if (params.strand ==null) {
"""
mkdir kallisto_${name}
kallisto quant -i ${index} -o kallisto_${name} -b ${params.bootstrap} ${fq}
"""
}
else {
"""
mkdir kallisto_${name}
kallisto quant -i ${index} -o kallisto_${name} -b ${params.bootstrap} --${params.strand} ${fq}
"""
}
}
/*****************************
* COMBINE KALLISTO OUTPUT
*****************************/
kallisto_dirs.into{kallisto_dirs; kallisto_dirs_deseq2}
process kallistoCountMatrix {
tag "anno: ${params.anno_set}"
publishDir "$params.output/kallisto_data" , mode: 'copy'
input:
file 'kallisto/*' from kallisto_dirs.collect()
output:
file 'kallisto_counts.tab'
script:
"""
singularity exec /lustre/scratch/projects/berger_common/singularity_images/rna_seq1.simg Rscript $baseDir/bin/sumkallisto.R kallisto ${params.txdb} ${design}
"""
}
/****************************
* STAR INDEX IF NEEDED
****************************/
process STARindex {
tag "dir: $params.starDir"
storeDir '/lustre/scratch/projects/berger_common/backup_berger_common/'
input:
file gtf
output:
file "${params.starDir}" into star_index
script:
"""
mkdir -p ${params.starDir}
STAR --runThreadN 4 --runMode genomeGenerate --genomeDir ${params.starDir} --genomeFastaFiles ${params.fasta_dna} --sjdbGTFfile ${params.gtf}
"""
}
/***************************
* STAR ALIGN
***************************/
process STAR {
tag "star: $name"
publishDir "$params.bam_out", mode: 'copy', pattern: 'star_*/*Aligned.sortedByCoord.out.bam'
input:
file index from star_index
set name, file(fq) from fastqs_star
output:
set name, file("star_${name}/${name}Aligned.sortedByCoord.out.bam") into sort_bam
file("star_${name}/${name}Log.final.out") into final_log
file "star_${name}/${name}ReadsPerGene.out.tab" into starcount
script:
"""
mkdir -p star_${name}
STAR --genomeDir $index --outFileNamePrefix ./star_${name}/${name} --readFilesIn $fq --runThreadN 4 --quantMode GeneCounts --outSAMtype BAM SortedByCoordinate
"""
}
process STAR_log {
publishDir "$params.output/star_data" , mode: 'copy'
input:
file 'logs/*' from final_log.collect()
output:
file "star_stats.tab" into stats
script:
"""
bash star_stats.sh
"""
}
/***************************
* COMBINE STAR COUNTS
**************************/
process starCountMatrix {
tag "strand: ${params.strand}"
publishDir "$params.output/star_data" , mode: 'copy'
input:
file 'star/*' from starcount.collect()
output:
file 'star_counts.tab'
script:
"""
singularity exec /lustre/scratch/projects/berger_common/singularity_images/rna_seq1.simg Rscript $baseDir/bin/sumstar.R star ${params.strand} ${design}
"""
}
/*****************************
* BAM 2 BW
****************************/
process bam2bw {
publishDir "$params.output/bam_bw", mode: 'copy'
tag "bw: $name"
input:
set name, file(bam) from sort_bam
output:
file("${name}.bw")
script:
"""
export TMPDIR=\$(pwd)
samtools index ${bam}
bamCoverage -b ${bam} -o ${name}.bw --normalizeTo1x ${params.normtosize} --binSize=${params.binsize}
"""
}
/*****************************
* DESeq2
*****************************/
process deseq2 {
publishDir "$params.output/deseq", mode: 'copy'
input:
file 'kallisto/*' from kallisto_dirs_deseq2.collect()
file design
file cont from contrasts
output:
file 'pairs.png' into pair
file 'dds.Rdata' into dds
file 'pca.png' into pca
file 'maplot*' optional true into maplots
file 'contrast_*' optional true into results
file 'sessionInfo_deseq2.txt' into seinfo
file 'barplot*' optional true into barplots
file 'Rarguments.txt' into argument
file 'table.csv' optional true into table
script:
def contrast_file = cont.name != 'NO_FILE' ? "$cont" : 'NULL'
"""
singularity exec /lustre/scratch/projects/berger_common/singularity_images/rna_seq1.simg Rscript $baseDir/bin/deseq2.R kallisto ${design} ${contrast_file} ${params.pvalue} ${params.txdb} ${params.filter} $workflow.sessionId
"""
}
/*******************************
*report {
*******************************/
// if no contrast was used then not all input from DESeq2
maplots=maplots.ifEmpty('NULL')
results=results.ifEmpty('NULL')
barplots=barplots.ifEmpty('NULL')
table=table.ifEmpty('NULL')
process report {
publishDir "$params.output/report", mode: 'copy'
input:
file table from table
file cont from contrasts
file stats from stats
file mods from modules
file pairplot from pair
file pcaplot from pca
file 'lists/*' from results.collect()
file 'maplots/*' from maplots.collect()
file session from seinfo
file mypar
file 'barplots/*' from barplots.collect()
file args from argument
output:
file 'report.html'
file 'deseq_contrast.Rmd'
file 'report.Rmd'
script:
def contrast_logic = cont.name != 'NO_FILE' ? 'TRUE' : 'FALSE'
"""
cp -L $baseDir/report/deseq_contrast.Rmd .
cp -L $baseDir/report/report.Rmd .
singularity exec /lustre/scratch/projects/berger_common/singularity_images/rna_seq1.simg Rscript $baseDir/bin/createReport.R ${design} ${params.pvalue} ${contrast_logic} $workflow.sessionId
"""
}
/********************************
process config
********************************/
// should run even with resume
process config {
publishDir "$params.output/nextflow", mode: 'copy'
input:
output:
file 'nextflow.config'
file 'rna_seq1.nf'
script:
"""
cp $baseDir/nextflow.config .
cp $baseDir/rna_seq1.nf .
"""
}
/********************************
process script
********************************/
// should run even with resume
process script {
publishDir "$params.output/used_script", mode: 'copy'
input:
output:
file 'deseq2.R'
file 'createReport.R'
file 'star_stats.sh'
file 'sumkallisto.R'
file 'sumstar.R'
script:
"""
cp $baseDir/bin/deseq2.R .
cp $baseDir/bin/createReport.R .
cp $baseDir/bin/star_stats.sh .
cp $baseDir/bin/sumkallisto.R .
cp $baseDir/bin/sumstar.R .
"""
}
workflow.onComplete {
new File('param.txt').delete() // cleaning up
println ( workflow.success ? "Done!" : "Oops .. something went wrong" )
}