-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROalign.nf
478 lines (388 loc) · 11.1 KB
/
PROalign.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
#!/usr/bin/env nextflow
/*********************
* parameters section *
**********************/
params.output="$baseDir/results/"
params.UMI_LEN=6 // Length of UMI in basepairs
// Mapq value for filtering multimappers
params.MAPQ=10
//params.FIVEP_UMI=true // Is there a UMI on the 5' end of the read?
//params.THREEP_UMI=true // Is there a UMI on the 3' end of the read?
if (params.FIVEP_UMI & params.THREEP_UMI){
params.umi=true
params.umi_loc="per_read"
}
else if (params.FIVEP_UMI){
params.umi=true
params.umi_loc="read2"
}
else if (params.THREEP_UMI){
params.umi=true
params.umi_loc="read1"
}
else {
exit 1, "Pipeline only makes sense with UMI"
}
// Adaptor sequences to clip. Default = Tru-Seq small RNA
params.ADAPTOR_1="TGGAATTCTCGGGTGCCAAGGAACTCCAGTCAC"
params.ADAPTOR_2="GATCGTCGGACTGTAGAACTCTGAACGTGTAGATCTCGGTGGTCGCCGTATCATT"
params.indexName = "spombe"
params.index = "library://elin.axelsson/index/index_bowtie2_spombe:v2.4.1-release-55"
params.spike_name = "spombe_cerevisiae"
params.index_spike = "library://elin.axelsson/newindex/index_bowtie2_spombe_scerevisiae:v2.4.1-release55"
params.SPIKE_PREFIX = "R64" //This is the prefix you've used on your spike in chromosomes, ie >hg38chr1
params.rdnaName = "spombe_rdna"
params.index_rdna = "library://elin.axelsson/newindex/index_bowtie2_spombe_rdna:v2.4.1_zach"
// channel
files = Channel.fromFilePairs( params.files, size: -1 ).ifEmpty { error "Invalid path: point to path containing fq files using --files flag (Paired-end only)" }
/********************************
*********************************
* START
*********************************
********************************/
/********************************
* Get indices
********************************/
// Spikein
process get_spike_index{
input:
params.index_spike
output:
file params.spike_name into comb
script:
"""
singularity run ${params.index_spike}
"""
}
// experimental
process get_index{
input:
params.index
output:
file params.indexName into experiment
script:
"""
singularity run ${params.index}
"""
}
// rDNA
process get_rdna_index{
input:
params.index_rdna
output:
file params.rdnaName into rdna
script:
"""
singularity run ${params.index_rdna}
"""
}
/********************************
* fastqc
********************************/
files.into{files;qc_files}
process fastqc {
publishDir "$params.output/logs_and_QC/fastqc", mode: 'copy'
input:
set id, file(fq) from qc_files
output:
file "*fastqc.*"
script:
"""
fastqc $fq
"""
}
/********************************
* preprocess reads
********************************/
//Trimming adapters and umi
process trim_fastp {
publishDir "$params.output/logs_and_QC/fastp", mode: 'copy', pattern: '*.html'
publishDir "$params.output/logs_and_QC/fastp", mode: 'copy', pattern: '*.log'
input:
set id, file(reads) from files
output:
set id, file("${id}_R*.fq.gz") into trimmed
file("${id}_fastp.html")
file("${id}_fastp.log") into fastp_log
script:
"""
fastp \
-i ${reads[0]} \
-I ${reads[1]} \
-o ${id}_R1.fq.gz \
-O ${id}_R2.fq.gz \
--adapter_sequence ${params.ADAPTOR_1} \
--adapter_sequence_r2 ${params.ADAPTOR_2} \
--umi \
--umi_loc=${params.umi_loc} \
--umi_len=${params.UMI_LEN} \
--html ${id}_fastp.html \
-c \
--overlap_len_require 15 2> ${id}_fastp.log
"""
}
//rRNA filter
process rRNA {
publishDir "$params.output/logs_and_QC/rRNA", mode: 'copy', pattern: '*.log'
input:
set id, file(reads) from trimmed
file(index) from rdna
output:
set id, file("${id}_1.fastq"), file("${id}_2.fastq") into filtered
file("${id}_rRNA_bowtie.log") into rRNA_bowtie_log
script:
"""
bowtie2 \
-1 ${reads[0]} \
-2 ${reads[1]} \
--fast-local \
--un-conc ${id}.fastq \
--interleaved - \
-x ${index}/${index} 2> ${id}_rRNA_bowtie.log
mv ${id}.1.fastq ${id}_1.fastq
mv ${id}.2.fastq ${id}_2.fastq
"""
}
/********************************
* alignments
********************************/
filtered.into{filtered_sp;filtered}
// spike in
process align_w_spikein {
publishDir "$params.output/logs_and_QC/align", mode: 'copy', pattern: '*.log'
input:
set id, file(read1), file(read2) from filtered_sp
file(index) from comb
output:
set id, file("spike_${id}.sam") into spike
file("${id}_spikeAlign.log") into spike_log
script:
"""
bowtie2 \
--local \
--very-sensitive-local \
--no-unal \
--no-mixed \
--no-discordant \
-x $index/$index \
-1 ${read1} \
-2 ${read2} \
-S spike_${id}.sam 2> ${id}_spikeAlign.log
"""
}
process get_spike_bam {
input:
set id, file(reads) from spike
val params.SPIKE_PREFIX
output:
set id, file("${id}_spike.bam"),file("${id}_spike.bam.bai") into spike_bam
shell:
'''
samtools view -hS -f 2 -q !{params.MAPQ} !{reads} |
perl -n -e 'print $_ if (/^\\@/ || /!{params.SPIKE_PREFIX}/ )' |
samtools view -b |
samtools sort -o !{id}_spike.bam
samtools index !{id}_spike.bam
'''
}
// Aligning to experimental genome
process align {
publishDir "$params.output/logs_and_QC/align", mode: 'copy', pattern: '*.log'
input:
set id, file(read1), file(read2) from filtered
file(index) from experiment
output:
set id, file("${id}.sam") into align
file("${id}_align.log") into align_log
script:
"""
bowtie2 \
--local \
--sensitive-local \
-x $index/$index \
-1 ${read1} \
-2 ${read2} \
-S ${id}.sam 2> ${id}_align.log
"""
}
process filter_align {
input:
set id, file(reads) from align
output:
set id, file("${id}.bam"),file("${id}.bam.bai") into align_bam
script:
"""
samtools view -bS -f 2 -q ${params.MAPQ} ${reads}|
samtools sort -o ${id}.bam
samtools index ${id}.bam
"""
}
/********************************
* dedup based on umi
********************************/
allbam=align_bam.combine(Channel.of('main')).concat(spike_bam.combine(Channel.of('spikein')))
process dedup_umi{
publishDir "$params.output/logs_and_QC/dedup", mode: 'copy', pattern: '*.log'
publishDir "$params.output/bams/dedup", mode: 'copy', pattern: '*main_deDuped.bam'
input:
set id, file(reads), file(index), type from allbam
output:
set id, type, file("*_deDuped.bam") into dedup
file("*.log") into de_log
script:
"""
umi_tools dedup \
-I ${reads} \
--umi-separator=":" \
--paired \
-S ${id}_${type}_deDuped.bam \
> ${id}_${type}_deDup.log
"""
}
/********************************
* index
********************************/
process index {
input:
set id, type, file(reads) from dedup
output:
set id, type, file("${reads}"), file("${reads}.bai") into dedup_i
script:
"""
samtools index ${reads}
"""
}
/********************************
* Unnormalized bw files
********************************/
dedup_i.into{f_dedup;r_dedup}
process bw_fwd {
publishDir "$params.output/bw/unnorm", mode: 'copy', pattern: '*.bw'
input:
set id, type, file(reads), file(bai) from f_dedup
output:
set id, file("*_fwd.bw")
when:
type=="main"
script:
"""
bamCoverage \
--bam ${reads} \
--skipNonCoveredRegions \
--outFileName ${id}_fwd.bw \
--binSize 1 \
--normalizeUsing None \
--Offset 1 \
--samFlagInclude 82
"""
}
process bw_rev {
publishDir "$params.output/bw/unnorm", mode: 'copy', pattern: '*.bw'
input:
set id, type, file(reads), file(bai) from r_dedup
output:
set id, file("*_rev.bw")
when:
type=="main"
script:
"""
bamCoverage \
--bam ${reads} \
--skipNonCoveredRegions \
--outFileName ${id}_rev.bw \
--binSize 1 \
--normalizeUsing None \
--Offset 1 \
--samFlagInclude 98
"""
}
/********************************
* Summary table
********************************/
process table {
publishDir "$params.output/logs_and_QC/info", mode: 'copy', pattern: 'infoTable.tsv'
input:
file de from de_log.collect()
file al from align_log.collect()
file sp from spike_log.collect()
file fa from fastp_log.collect()
file rr from rRNA_bowtie_log.collect()
output:
file "infoTable.tsv"
shell:
'''
touch infoTable.tsv
echo -e Name'\t'RawReads'\t'NonDimerReads'\t'%dimer'\t'insertSize'\t'rRNAreads'\t'%rRNA'\t'passedFilters'\t'\
bowtieConcordant'\t'bowtieMulti'\t'bowtieUnal'\t'bowtieOverallMap%'\t'bowtieConcordant%'\t'\
bowtieMulti%'\t'bowtieUnal%'\t'uniqueMapped'\t'uniqueMappedNondup'\t'%PCRdups'\t'uniqueMappedSpikein'\t'\
uniqueMappedSpikeinNondup'\t'spikeInPCRdups% >> infoTable.tsv
for SAMPLE in $(ls *_align.log | sed 's/_align.log//' )
do
echo ${SAMPLE}
NAME=${SAMPLE}
RAW_READS=$(cat ${SAMPLE}_fastp.log |
grep "total reads:" | head -n 1 |
awk '{print $3}')
TRIMMED_READS=$(cat ${SAMPLE}_fastp.log |
grep "total reads:" | tail -n 1 |
awk '{print $3}')
PER_DIMER=$(awk -v a="${TRIMMED_READS}" -v b="${RAW_READS}" 'BEGIN { print (1-a/b)*100 }')
INSERT_SIZE=$(cat ${SAMPLE}_fastp.log |
grep "Insert size peak" |
awk '{print $8}')
PASSED_FILTERS=$(cat ${SAMPLE}_align.log |
grep "reads; of these:$" |
awk '{print $1}')
RRNA=$(awk -v a="${TRIMMED_READS}" -v b="${PASSED_FILTERS}" 'BEGIN { print a-b }')
PER_RRNA=$(awk -v a="${RRNA}" -v b="${RAW_READS}" 'BEGIN { print (a/b)*100 }')
B_CONC=$(cat ${SAMPLE}_align.log |
grep "aligned concordantly exactly 1 time$" |
awk '{print $1}')
B_MULTI=$(cat ${SAMPLE}_align.log |
grep "aligned concordantly >1 times$" |
awk '{print $1}')
B_UNAL=$(cat ${SAMPLE}_align.log |
grep "aligned concordantly 0 times$" |
awk '{print $1}')
B_OAP=$(cat ${SAMPLE}_align.log |
grep "overall alignment rate$" |
awk '{print $1}')
B_CONC_PER=$(awk -v a="${B_CONC}" -v b="${PASSED_FILTERS}" 'BEGIN { print (a/b)*100}')
B_MULTI_PER=$(awk -v a="${B_MULTI}" -v b="${PASSED_FILTERS}" 'BEGIN { print (a/b)*100 }')
B_UNAL_PER=$(awk -v a="${B_UNAL}" -v b="${PASSED_FILTERS}" 'BEGIN { print (a/b)*100 }')
UNIQ_MAPPED=$(cat ${SAMPLE}_main_deDup.log |
grep "Input Reads:" | awk '{print $10}')
UNIQ_MAPPED_DEDUP=$(cat ${SAMPLE}_main_deDup.log |
grep "Number of reads out:" | awk '{print $8}')
PER_DUPS=$(awk -v a="${UNIQ_MAPPED_DEDUP}" -v b="${UNIQ_MAPPED}" 'BEGIN { print (1-a/b)*100 }')
UNIQ_MAPPED_SPIKE=$(cat ${SAMPLE}_spikein_deDup.log |
grep "Input Reads:" | awk '{print $10}')
UNIQ_MAPPED_DEDUP_SPIKE=$(cat ${SAMPLE}_spikein_deDup.log |
grep "Number of reads out:" | awk '{print $8}')
PER_DUPS_SPIKE=$(awk -v a="${UNIQ_MAPPED_DEDUP_SPIKE}" -v b="${UNIQ_MAPPED_SPIKE}" 'BEGIN { print (1-a/b)*100}')
echo ${UNIQ_MAPPED_DEDUP_SPIKE}
echo ${UNIQ_MAPPED_SPIKE}
echo -e $NAME'\t'\
$RAW_READS'\t'\
$TRIMMED_READS'\t'\
$PER_DIMER'\t'\
$INSERT_SIZE'\t'\
$RRNA'\t'\
$PER_RRNA'\t'\
$PASSED_FILTERS'\t'\
$B_CONC'\t'\
$B_MULTI'\t'\
$B_UNAL'\t'\
$B_OAP'\t'\
$B_CONC_PER'\t'\
$B_MULTI_PER'\t'\
$B_UNAL_PER'\t'\
$UNIQ_MAPPED'\t'\
$UNIQ_MAPPED_DEDUP'\t'\
$PER_DUPS'\t'\
$UNIQ_MAPPED_SPIKE'\t'\
$UNIQ_MAPPED_DEDUP_SPIKE'\t'\
$PER_DUPS_SPIKE >> infoTable.tsv
done
'''
}