-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFQ.nf
493 lines (400 loc) · 14.8 KB
/
getFQ.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
#!/usr/bin/env nextflow
// Kevin Brick : Rev 1.0 : 06-03-18
// Kevin Brick : Rev 1.7 : 05-22-20
// Get FASTQ script
// set some default params
params.help=""
if (params.help) {
log.info " "
log.info "=========================================================================="
log.info "getFQ PIPELINE (Version 1.8) "
log.info "=========================================================================="
log.info " "
log.info "USAGE: "
log.info " "
log.info "The pipeline is run using a parent perl script: \\"
log.info "/data/RDCO/code/pipelines/pipeIt --pipe getfq \\"
log.info " "
log.info "/data/RDCO/code/pipelines/pipeIt --h for help \\ "
log.info " "
log.info "------------------------------------------------------------------------- "
log.info "nextflow run $NXF_PIPEDIR/getFQ.nf \\"
log.info " --bam <bam file>"
log.info " --sra <sra names (or comma separated list: align as one file)>"
log.info " --fq1 <Read1 fastq file>"
log.info " --fq2 <Read2 fastq file>"
log.info " --obj <Sample name regex for RDCO object store>"
log.info " --genome <string> \\"
log.info " --threads <number of threads: default = 6> \\"
log.info " --outName <string default=bam file stem> \\"
log.info " --sample_name <string: default = outName> \\"
log.info " --outdir <string: default = outName> \\"
log.info " --outdir_tmp <string>/tmp \\"
log.info " --merge Merge fastqs if multiple FQs are passed (i.e. SRA / obj store) <logical: default = true> \\"
log.info " --withFQC Generate fastQC & fastqScreen stats <logical: default = true> \\"
log.info " --allowMultiSample Allow object store regex to \"Get\" multiple samples <logical: default = true> \\"
log.info " -with-trace -with-timeline"
log.info " "
log.info "HELP: nextflow run /data/RDCO/code/pipelines/getFQ.nf --help"
log.info " "
log.info "================================================================================================================="
log.info "Required Arguments:"
log.info " "
log.info " --bam STRING BAM FILE" OR
log.info " --sra STRING SRA ACCESSION(s) ; for multiple, separate with commas" OR
log.info " --obj STRING Sample name regex to search for in object storage" OR
log.info " --fq1 STRING READ1 FASTQ FILE"
log.info " --fq2 STRING READ2 FASTQ FILE"
log.info " --genome STRING FASTA FILE (GENOME REFERENCE)"
log.info " "
log.info "Output and Tempory directory Arguments"
log.info " "
log.info " --outName STRING Output file prefix"
log.info " --outdir DIR Output directory"
log.info " --outdir_tmp DIR Tempory directory"
log.info " "
log.info "Options"
log.info " "
log.info " --merge true/false Merge fastqs if multiple FQs are passed (i.e. SRA / obj store) "
log.info " --withFQC true/false Run fastQC & fastqScreen "
log.info " --allowMultiSample true/false Allow object store regex to \"Get\" multiple samples "
log.info "================================================================================================================"
exit 1
}
// Define arg defaults
//number of threads
params.threads = 6
//sample information
params.bam = ""
params.fq1 = ""
params.fq2 = ""
params.sra = ""
params.type = "sr"
params.name = ""
params.gzipoutput = "false"
params.merge = true
params.withFQC = true
params.allowMultiSample = true //KB Nov 3 2019: allow multiple retrieve by default
params.splitSz = 10000000
if (params.name){
outName = "${params.name}.${params.genome}"
}else{
if (params.outName){
outName = params.outName
}else{
outName = ""
}
}
params.tmpName = "${params.name}.tmpFile.${params.genome}"
params.obj=""
params.sample_name=""
params.mem="16G"
params.debugmode=""
//output and tempory directories
params.outdir = "./${outName}"
params.outdir_tmp = "/tmp"
if (params.bam){
params.bai = "${params.bam}.bai"
}
//genomeAndindex
params.genome_fasta = "$NXF_GENOMES/${params.genome}/BWAIndex/version0.7.10/genome.fa"
params.genome_faIdx = "$NXF_GENOMES/${params.genome}/BWAIndex/version0.7.10/genome.fa.fai"
def inputType
if (params.sra){inputType = 'sra'}
if (params.obj){inputType = 'obj'}
if (params.bam){inputType = 'bam'}
if (params.fq1){inputType = 'fastq'}
//log.info
log.info "===================================================================="
log.info "GET FQ PIPELINE : Get initial fastq for pipes"
log.info "===================================================================="
log.info "ref genome : ${params.genome}"
log.info "genome fasta : ${params.genome_fasta}"
log.info "bam : ${params.bam}"
log.info "sra : ${params.sra}"
log.info "obj : ${params.obj}"
log.info "fq1 : ${params.fq1}"
log.info "fq2 : ${params.fq2}"
log.info "name : ${params.outName}"
log.info "source type : ${inputType}"
log.info "outdir : ${params.outdir}"
log.info "temp_dir : ${params.outdir_tmp}"
log.info "threads : ${params.threads}"
log.info "mem : ${params.mem}"
log.info "merge : ${params.merge}"
log.info "withFQC : ${params.withFQC}"
log.info "allowMultiSample : ${params.allowMultiSample}"
// This switch deals with multiple input type to generate a fastq from
// either sra records, fq.gz files from the object store, a bam file,
// or directly from fastq/fastq.gz files
//
// PE/SR need not be specified ... it will be autodetected (or @ least it should be !!)
switch (inputType) {
case 'sra':
process getSRAfiles {
tag { params.sra }
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*expttype"
input:
//val sraNum from sraChannel
output:
file '*.R1.fastq' optional true into initFQ1
file '*.R2.fastq' optional true into initFQ2
file '*.fastq' into allFQ, allFQ2
file 'is*.expttype' into exptType
script:
//def randName = new Random().with {(1..30).collect {(('a'..'z')).join()[ nextInt((('a'..'z')).join().length())]}.join()}
// Sept 6 2019: KB
// Modified to use fasterq-dump instead (fastq-dump is being deprecated)
// Also modified to parse SRA line and download all SRA files (must be comma delimited)
// #fastq-dump ${params.sra} --split-files
// #mv ${params.sra}_1.fastq ${params.sra}.R1.fastq
// #mv ${params.sra}_2.fastq ${params.sra}.R2.fastq
"""
echo 1 >isSR.expttype
sra=\$(echo "${params.sra}" | tr "," "\\n")
for s in \$sra
do
fasterq-dump \$s --split-files -o \$s
if [ -f \$s"_2.fastq" ] ; then
mv \$s"_1.fastq" \$s".R1.fastq"
mv \$s"_2.fastq" \$s".R2.fastq"
rm -f isSR.expttype
echo 2 >isPE.expttype
else
mv \$s \$s".R1.fastq"
echo 1 >isSR.expttype
fi
done
"""
}
println('Otherside')
break
case 'obj':
println('Retreiving from object storage ... ')
process getFromObjectStore {
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*expttype"
input:
output:
file '*.R1.fastq' optional true into initFQ1
file '*.R2.fastq' optional true into initFQ2
file '*.fastq' into allFQ, allFQ2
file 'is*.expttype' into exptType
script:
def randName = new Random().with {(1..30).collect {(('a'..'z')).join()[ nextInt((('a'..'z')).join().length())]}.join()}
if (params.allowMultiSample){
"""
perl \$NXF_PIPEDIR/lsObj --v RDCO --p ${params.obj} --get --f
gunzip *fastq.gz
if [ `ls *.R2.*` ]; then
echo 2 >isPE.expttype
else
echo 1 >isSR.expttype
fi
"""
}else{
"""
perl \$NXF_PIPEDIR/lsObj --v RDCO --p ${params.obj} --get
gunzip *fastq.gz
if [ `ls *.R2.*` ]; then
echo 2 >isPE.expttype
else
echo 1 >isSR.expttype
fi
"""
}
}
break
case 'bam':
println('BAM input detected ... ')
inBAM = file(params.bam)
process bamToFastq {
tag { bam }
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*expttype"
input:
file bam from inBAM
output:
file '*.R1.fastq' into initFQ1
file '*.R2.fastq' optional true into initFQ2
file '*.fastq' into allFQ, allFQ2
file 'is*.expttype' into exptType
script:
"""
n=`samtools view -h ${bam} |head -n 100000 |samtools view -f 1 -S /dev/stdin |wc -l`
if [ \$n -eq 0 ]; then
echo 1 >isSR.expttype
java -jar \$PICARDJAR SortSam \
I=${bam} \
O=querynameSort.bam \
SO=queryname \
TMP_DIR=/lscratch/\$SLURM_JOBID \
VALIDATION_STRINGENCY=LENIENT 2>b2fq.err
java -jar \$PICARDJAR SamToFastq I=querynameSort.bam \
F=nxf.R1.fastq \
TMP_DIR=/lscratch/\$SLURM_JOBID \
VALIDATION_STRINGENCY=LENIENT
else
echo 2 >isPE.expttype
java -jar \$PICARDJAR FixMateInformation \
I=${bam} \
O=fixMate.bam \
SORT_ORDER=queryname \
TMP_DIR=/lscratch/\$SLURM_JOBID \
VALIDATION_STRINGENCY=LENIENT
java -jar \$PICARDJAR SortSam \
I=fixMate.bam \
O=querynameSort.bam \
SO=queryname \
TMP_DIR=/lscratch/\$SLURM_JOBID \
VALIDATION_STRINGENCY=LENIENT 2>b2fq.err
java -jar \$PICARDJAR SamToFastq I=querynameSort.bam \
F=nxf.R1.fastq F2=nxf.R2.fastq \
TMP_DIR=/lscratch/\$SLURM_JOBID \
VALIDATION_STRINGENCY=LENIENT
fi
"""
}
break
case 'fastq':
println('FASTQ input(s) detected ... ')
if (params.fq2){
inFQ1 = file(params.fq1)
inFQ2 = file(params.fq2)
process initFQtoFQ_PE {
tag { inFQ1 }
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*expttype"
input:
file inFQ1
file inFQ2
output:
file 'nxf.R1.fastq' into initFQ1
file 'nxf.R2.fastq' into initFQ2
file 'nxf.R*.fastq' into allFQ, allFQ2
file 'is*.expttype' into exptType
script:
if (inFQ1 =~ /.gz$/)
"""
gunzip --stdout $inFQ1 >nxf.R1.fastq
gunzip --stdout $inFQ2 >nxf.R2.fastq
echo 2 >isPE.expttype
"""
else
"""
ln -s $inFQ1 nxf.R1.fastq
ln -s $inFQ2 nxf.R2.fastq
echo 2 >isPE.expttype
"""
}
}else{
inFQ1 = file(params.fq1)
process initFQtoFQ_SR{
tag { inFQ1 }
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*expttype"
input:
file inFQ1
output:
file '*.R1.fastq' into initFQ1
file '*.fastq' into allFQ, allFQ2
file 'is*.expttype' into exptType
script:
if (inFQ1 =~ /.gz$/)
"""
gunzip --stdout $inFQ1 >nxf.R1.fastq
echo 1 >isSR.expttype
"""
else
"""
cp $inFQ1 nxf.R1.fastq
echo 1 >isSR.expttype
"""
}
}
break
}
if (params.withFQC){
process fastQCall {
tag{$fq}
//publishDir params.outdir, mode: 'copy', overwrite: false
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*zip"
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*html"
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*png"
publishDir params.outdir, mode: 'copy', overwrite: false, pattern: "*txt"
input:
each file(fq) from allFQ
output:
file '*zip' into fqZip
file '*html' into fqHtml
file '*png' into fqPng
file '*txt' into fqTxt
file '*fastqc*' into fastQC
file '*_screen*' into fastQScreen
script:
"""
head -n 10000000 ${fq} >${fq}.subset.fastq
fastqc -t ${params.threads} ${fq}.subset.fastq
mv ${fq}.subset_fastqc.html ${fq}c_report.html
mv ${fq}.subset_fastqc.zip ${fq}c_report.zip
\$NXF_PIPEDIR/fastq_screen_v0.11.4/fastq_screen --threads ${params.threads} --force \
--aligner bwa ${fq} \
--conf \$NXF_GENOMES/fastq_screen.conf
"""
}
}
if (!outName){outName='merge'}
println(params.merge)
if (params.merge){
process mergeFQ {
publishDir params.outdir, mode: 'move', overwrite: false
input:
file fq from allFQ2.collect()
output:
file '*.R?.fastq*' into fastqFinal
script:
// Modified to work better with huge files
// Now, we make soft links if at all possible instead of copying stuff
// This also speeds things up quite a bit
"""
R1files=\$(ls *R1*fastq 2> /dev/null | wc -l)
R2files=\$(ls *R2*fastq 2> /dev/null | wc -l)
if [ "\$R2files" != "0" ]
then
if [ "\$R2files" != "1" ]
then
cat *R2*fastq >mergeR2.fastq
rm *.R2.fastq
else
fqR2=`ls *R2*fastq`
ln -s \$fqR2 mergeR2.fastq
fi
fastq-sort --id mergeR2.fastq >${outName}.R2.fastq
rm mergeR2.fastq
fi
if [ "\$R1files" != "1" ]
then
cat *.R1.fastq >mergeR1.fastq
rm *.R1.fastq
else
fqR1=`ls *R1*fastq`
ln -s \$fqR1 mergeR1.fastq
fi
fastq-sort --id mergeR1.fastq >${outName}.R1.fastq
rm mergeR1.fastq
if [ "${params.gzipoutput}" == "true" ]
then
gzip ${outName}.R1.fastq
gzip ${outName}.R2.fastq || true
fi
"""
}
}else{
process omitFQ {
publishDir params.outdir, mode: 'move', overwrite: false
input:
file fq from allFQ2.collect()
output:
file '*.R?.fastq*' into fastqFinal
script:
"""
echo "AAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaagggh FAIL!!!! You forgot to merge !!!)"
"""
}
}