-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.nf
More file actions
executable file
·402 lines (302 loc) · 9.98 KB
/
main.nf
File metadata and controls
executable file
·402 lines (302 loc) · 9.98 KB
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
#!/usr/bin/env nextflow
def helpMessage() {
log.info"""
================================================================
crispr-nf
================================================================
Pre-processing of CRISPR-Cas9 / shRNA screening data
Usage:
nextflow run ZuberLab/crispr-nf
Options:
--readDir Directory containing the raw reads. Valid input
files are BAM and compressed FASTQ. (Defaults to
'reads')
--library Path to sgRNA / shRNA library file. (Defaults to
'library.txt')
The following columns are required:
- id: unique name of sgRNA / shRNA
- gene: gene targeted by sgRNA / shRNA
- sequence: nucleotide sequence of sgRNA / shRNA
--barcodes Path to file containing barcodes for demultiplexing.
(Defaults to 'barcodes.txt')
The following columns are required:
- lane: name of BAM / FASTQ input file
- sample_name: name of demultiplexed sample
- barcode: nucleotide sequence of sample barcode
--forward_stranded Orientation of reads in BAM / FASTQ files.
(Defaults to true)
--barcode_random_length Number of nucleotides in random barcode
(Defaults to 6)
--barcode_demux_mismatches Number of mismatches allowed during demultiplexing
of barcode. (Defaults to 1)
--barcode_demux_length Number of nucleotides in sample barcode.
(Defaults to 4)
--spacer_length Number of nucleotides in spacer sequence between
barcodes and sgRNA / shRNA sequence. (Defaults to 20)
--padding_base Nucleotide used for padding if sgRNA / shRNA are of
unequal length. Must be one of G, C, T, and A.
(Defaults to G)
--resultsDir Directory to save results to. (Defaults to
'results')
Profiles:
standard local execution with singularity
sge SGE execution with singularity
slurm SLURM execution with singularity
local local execution without singularity
Docker:
zuberlab/crispr-nf:latest
Author:
Jesse J. Lipp (jesse.lipp@imp.ac.at)
""".stripIndent()
}
if (params.help){
helpMessage()
exit 0
}
strandedness = params.forward_stranded ? "forward": "reverse"
log.info " Screen Preprocessing "
log.info "======================"
log.info "readDir : ${params.readDir}"
log.info "resultsDir : ${params.resultsDir}"
log.info "library file : ${params.library}"
log.info "barcode file : ${params.barcodes}"
log.info "demux barcode length : ${params.barcode_demux_length}"
log.info "demux barcode mismatches : ${params.barcode_demux_mismatches}"
log.info "random barcode length : ${params.barcode_random_length}"
log.info "spacer length : ${params.spacer_length}"
log.info "strandedness : ${strandedness}"
log.info "padding base : ${params.padding_base}"
log.info "======================"
Channel
.fromPath( "${params.readDir}/*.bam" )
.map { file -> tuple( file.baseName, file ) }
.set { bamInputFiles }
Channel
.fromPath( "${params.readDir}/*.{fastq,fq}.gz" )
.map { file -> tuple( file.baseName.replaceAll(/\.fastq|\.fq/, ''), file ) }
.set { fastqInputFiles }
Channel
.fromPath(params.barcodes)
.into { processBarcodeFiles; combineBarcodeFiles }
Channel
.fromPath(params.library)
.into { processLibraryFile ; combineLibraryFile }
process process_library {
tag "${library.baseName}"
input:
file(library) from processLibraryFile
output:
file("library.saf") into librarySafFile
file("library.fasta") into libraryFastaFile
script:
padding_base = params.padding_base
"""
process_library.R ${library} ${strandedness} ${padding_base}
"""
}
process process_barcodes {
tag "${barcodes.baseName}"
input:
file(barcodes) from processBarcodeFiles
output:
file("*.txt") into demuxBarcodeFiles
script:
"""
process_barcodes.R ${barcodes}
"""
}
process bowtie_index {
tag "${library_fasta.baseName}"
input:
file(library_fasta) from libraryFastaFile
output:
file("bt2") into bt2Index
script:
"""
mkdir -p bt2
bowtie2-build ${library_fasta} bt2/
"""
}
process bam_to_fastq {
tag { lane }
input:
set val(lane), file(bam) from bamInputFiles
output:
set val(lane), file("${lane}.fastq.gz") into fastqFilesFromBam
script:
"""
samtools fastq ${bam} | gzip -c > ${lane}.fastq.gz
"""
}
fastqFilesFromBam
.mix(fastqInputFiles)
.set { fastqFiles }
process trim_random_barcode {
tag { lane }
input:
set val(lane), file(fastq) from fastqFiles
output:
set val(lane), file("${lane}_trimmed.fastq.gz") into randomBarcodeTrimmedFiles
script:
position = params.forward_stranded ? params.barcode_random_length + 1 : params.barcode_random_length
flag_strandedness = params.forward_stranded ? "-f ${position}" : "-t ${position}"
flag_strandedness = params.barcode_random_length > 0 ? flag_strandedness : '-f 1'
"""
zcat ${fastq} | fastx_trimmer \
${flag_strandedness} \
-Q33 \
-z \
-o ${lane}_trimmed.fastq.gz
"""
}
demuxBarcodeFiles
.flatten()
.map { file -> [file.baseName, file] }
.concat(randomBarcodeTrimmedFiles)
.groupTuple()
.set { demuxFiles }
process demultiplex {
tag "${lane}"
input:
set val(lane), file(files) from demuxFiles
output:
set val(lane), file('*.fq.gz') into splitFiles
script:
flag_strandedness = params.forward_stranded ? '--bol' : '--eol'
num_mismatches = params.barcode_demux_mismatches
"""
zcat ${files[1]} | fastx_barcode_splitter.pl \
--bcfile ${files[0]} \
--prefix ${lane}_ \
--suffix .fq \
--mismatches ${num_mismatches} \
${flag_strandedness}
gzip *.fq
"""
}
def ungroupTuple = {
def result = []
def name = it[0]
it[1].each { result << [name, it] }
return result
}
splitFiles
.flatMap { it -> ungroupTuple(it) }
.filter { it[1].baseName =~ /^(?!.*_unmatched).*$/ }
.map { lane, file -> tuple(lane, file.name.replaceAll(/\.fq\.gz/, ''), file) }
.into { flattenedSplitFiles; fastqcSplitFiles }
process trim_barcode_and_spacer {
tag { id }
input:
set val(lane), val(id), file(fastq) from flattenedSplitFiles
output:
set val(lane), val(id), file("${id}.fastq.gz") into spacerTrimmedFiles
script:
barcode_spacer_length = params.spacer_length + params.barcode_demux_length
position = params.forward_stranded ? barcode_spacer_length + 1 : barcode_spacer_length
flag_strandedness = params.forward_stranded ? "-f ${position}" : "-t ${position}"
"""
zcat ${fastq} | fastx_trimmer \
${flag_strandedness} \
-Q33 \
-z \
-o ${id}.fastq.gz
"""
}
process align {
tag { id }
input:
set val(lane), val(id), file(fastq) from spacerTrimmedFiles
each file(index) from bt2Index
output:
set val(lane), val(id), file("${id}.sam") into alignedFiles
file "${id}.log" into alignResults
script:
"""
bowtie2 \
--threads \$((${task.cpus} - 4)) \
-x ${index}/ \
-L 18 \
--score-min 'C,0,-1' \
-N 0 \
--seed 42 \
<(zcat ${fastq}) 2> ${id}.log > ${id}.sam
"""
}
alignedFiles
.map { lane, id, file -> tuple(lane, file) }
.groupTuple()
.set { groupedAlignedFiles }
process count {
tag { lane }
publishDir path: "${params.resultsDir}/${lane}/counts",
mode: 'copy',
overwrite: 'true'
input:
set val(lane), file(sams) from groupedAlignedFiles
each file(saf) from librarySafFile
output:
file("${lane}.txt") into countedFiles
file("${lane}.txt.summary") into featureCountsResults
script:
"""
featureCounts \
-T ${task.cpus} \
-a ${saf} \
-F SAF \
-o ${lane}.txt \
${sams}
"""
}
process combine_counts {
tag 'all'
publishDir path: "${params.resultsDir}/counts",
mode: 'copy',
overwrite: 'true'
input:
file(counts) from countedFiles.collect()
file(library) from combineLibraryFile
file(barcodes) from combineBarcodeFiles
output:
file("counts_mageck.txt") into combinedMageckFile
script:
"""
combine_counts.R ${library} ${barcodes} ${counts}
"""
}
fastqcSplitFiles
.map { lane, id, file -> tuple(lane, file) }
.groupTuple()
.set { fastqcSplitFiles }
process fastqc {
tag { lane }
input:
set val(lane), file(fastq) from fastqcSplitFiles
output:
file "*_fastqc.{zip,html}" into fastqcResults
script:
"""
fastqc -q ${fastq}
"""
}
process multiqc {
tag 'all'
publishDir "${params.resultsDir}/multiqc", mode: 'copy'
input:
file (fastqc: 'fastqc/*') from fastqcResults.collect()
file (align: 'align/*') from alignResults.collect()
file (featurecounts: 'featureCounts/*') from featureCountsResults.collect()
output:
file "*multiqc_report.html" into multiqc_report
file "*_data"
file '.command.err' into multiqc_stderr
script:
"""
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
multiqc -f -x *.run .
"""
}
workflow.onComplete {
println ( workflow.success ? "COMPLETED!" : "FAILED" )
}