forked from cortes-ciriano-lab/SComatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
380 lines (288 loc) · 11.2 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
#!/usr/bin/env nextflow
// Declare syntax version
nextflow.enable.dsl=2
params.csv = null
params.ref = null
params.bam = null
params.meta = null
params.sample_id = null
params.outdir = "./output"
log.info """\
S C O M A T I C - N F P I P E L I N E
===================================
input_csv : ${params.csv}
reference : ${params.ref}
outdir : ${params.outdir}
bam : ${params.bam}
metadata : ${params.meta}
sample_id : ${params.sample_id}
"""
.stripIndent()
process SPLITBAM {
tag "Sample: $sample_id"
publishDir params.outdir, mode:'copy'
input:
tuple val(sample_id), path(bam), path(bai), path(meta)
output:
tuple val(sample_id), path("${sample_id}/Step1_BamCellTypes/*.bam"), path("${sample_id}/Step1_BamCellTypes/*.bai"), path("${sample_id}/Step1_BamCellTypes/")
script:
"""
outdir="${sample_id}/Step1_BamCellTypes"
mkdir -p \${outdir}
python /scomatic/scripts/SplitBam/SplitBamCellTypes.py \
--bam $bam \
--meta $meta \
--id $sample_id \
--n_trim 5 \
--max_nM 5 \
--max_NH 1 \
--outdir \${outdir}
"""
}
process BASECOUNTS_SPLIT {
label 'multicore'
publishDir path: params.outdir, mode:'copy', overwrite: true
tag "Sample: $sample_id; BAM: $bam"
input:
path ref
tuple val(sample_id), path(bam), path(bai)
output:
tuple val(sample_id), path ("${sample_id}/Step2_BaseCellCounts/*.tsv"), optional: true
script:
"""
outdir="${sample_id}/Step2_BaseCellCounts"
mkdir -p \${outdir}
cell_type=\$(basename "$bam" | sed "s/${sample_id}.//" | sed "s/.bam//")
echo \$cell_type
# Temp folder
temp="${sample_id}/Step2_BaseCellCounts/temp_\${cell_type}"
mkdir -p \$temp
# Hack to ensure output file is emitted
touch "\${outdir}/${sample_id}.\$cell_type.tsv"
# Command line to submit to cluster
python /scomatic/scripts/BaseCellCounter/BaseCellCounter.py --bam $bam \
--ref $ref \
--chrom all \
--out_folder "\${outdir}" \
--min_bq 30 \
--tmp_dir \$temp \
--nprocs $task.cpus
rm -rf \$temp
"""
}
process MERGECOUNTS {
label 'multicore'
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id"
input:
tuple val(sample_id), path(tsvs)
output:
tuple val(sample_id), path("${sample_id}/Step3_BaseCellCountsMerged/")
script:
"""
output_dir3=${sample_id}/Step3_BaseCellCountsMerged
mkdir -p \$output_dir3
# Create a tempdir with each sample tsvs
mkdir -p temp/
cp ${tsvs} temp/
python /scomatic/scripts/MergeCounts/MergeBaseCellCounts.py --tsv_folder temp/ \
--outfile \${output_dir3}/${sample_id}.BaseCellCounts.AllCellTypes.tsv
rm -rf temp/
"""
}
process VARIANTCALLING {
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id"
input:
path ref
tuple val(sample_id), path(outdir3)
output:
tuple val(sample_id), path("${sample_id}/Step4_VariantCalling/")
script:
"""
# Step 4.1
output_dir4=${sample_id}/Step4_VariantCalling
mkdir -p \$output_dir4
# check chr prefix before variant calling
if [ \$(grep -c -m 20 "^chr" ${outdir3}/${sample_id}.BaseCellCounts.AllCellTypes.tsv) -gt 3 ]
then
echo "Chr prefix detected"
repeat_bed="/scomatic/bed_files_of_interest/UCSC.k100_umap.without.repeatmasker.bed"
editing=/scomatic/RNAediting/AllEditingSites.hg38.txt
PON=/scomatic/PoNs/PoN.scRNAseq.hg38.tsv
else
echo "No chr prefix detected"
repeat_bed="/scomatic/bed_files_of_interest/UCSC.k100_umap.without.repeatmasker_nochr.bed"
editing=/scomatic/RNAediting/AllEditingSites.hg38_nochr.txt
PON=/scomatic/PoNs/PoN.scRNAseq.hg38_nochr.tsv
fi
python /scomatic/scripts/BaseCellCalling/BaseCellCalling.step1.py \
--infile ${outdir3}/${sample_id}.BaseCellCounts.AllCellTypes.tsv \
--outfile \${output_dir4}/${sample_id} \
--ref $ref
# Step 4.2
python /scomatic/scripts/BaseCellCalling/BaseCellCalling.step2.py \
--infile \${output_dir4}/${sample_id}.calling.step1.tsv \
--outfile \${output_dir4}/${sample_id} \
--editing \$editing \
--pon \$PON
bedtools intersect -header -a \${output_dir4}/${sample_id}.calling.step2.tsv \
-b \${repeat_bed} | awk '\$1 ~ /^#/ || \$6 == "PASS"' > \${output_dir4}/${sample_id}.calling.step2.pass.tsv
"""
}
process CALLABLESITES {
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id"
input:
tuple val(sample_id), path(outdir4)
output:
tuple val(sample_id), path("${sample_id}/Step5_CellTypeCallableSites/")
script:
"""
# Computing the number of callable sites per cell type
output_dir5=${sample_id}/Step5_CellTypeCallableSites
mkdir -p \$output_dir5
python /scomatic/scripts/GetCallableSites/GetAllCallableSites.py --infile ${outdir4}/${sample_id}.calling.step1.tsv \
--outfile \$output_dir5/${sample_id} \
--max_cov 150 --min_cell_types 2
"""
}
process CALLABLE_PERCT {
label 'multicore'
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id; BAM: $bam"
input:
path ref
tuple val(sample_id), path(bam), path(bai), path(outdir1), path(outdir4)
output:
path("${sample_id}/Step6_UniqueCellCallableSites/*.tsv"), optional: true
script:
"""
STEP4_1=$outdir4/${sample_id}.calling.step1.tsv
output_dir6=${sample_id}/Step6_UniqueCellCallableSites
mkdir -p \$output_dir6
cell_type=\$(basename "$bam" | sed "s/${sample_id}.//" | sed "s/.bam//")
echo \$cell_type
temp=\$output_dir6/temp_\${cell_type}
mkdir -p \$temp
python /scomatic/scripts/SitesPerCell/SitesPerCell.py --bam $bam \
--infile $outdir4/${sample_id}.calling.step1.tsv \
--ref $ref \
--out_folder \$output_dir6 --tmp_dir \$temp --nprocs $task.cpus
echo
"""
}
process GENOTYPE_CELLS {
label 'multicore'
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id; BAM: $bam"
input:
path ref
tuple val(sample_id), path(bam), path(bai), path(outdir1), path(outdir4), path(meta)
output:
path("${sample_id}/Step7_SingleCellAlleles/*.tsv"), optional: true
script:
"""
STEP4_2_pass=${outdir4}/${sample_id}.calling.step2.pass.tsv
output_dir7=${sample_id}/Step7_SingleCellAlleles
mkdir -p \$output_dir7
cell_type=\$(basename "$bam" | sed "s/${sample_id}.//" | sed "s/.bam//")
echo \$cell_type
temp=\$output_dir7/temp_\${cell_type}
mkdir -p \$temp
python /scomatic/scripts/SingleCellGenotype/SingleCellGenotype.py --bam $bam \
--infile \${STEP4_2_pass} \
--nprocs $task.cpus \
--meta $meta \
--outfile \${output_dir7}/\${cell_type}.single_cell_genotype.tsv \
--tmp_dir \$temp \
--ref $ref
rm -rf \$temp
"""
}
process TRINUC_BACKGROUND {
publishDir path: params.outdir, mode:'copy'
tag "Sample: $sample_id"
input:
tuple val(sample_id), path(tsvs)
output:
path("${sample_id}/Step8_TrinucBackground/*.tsv")
script:
"""
output_dir8=${sample_id}/Step8_TrinucBackground/
mkdir -p \$output_dir8
tsv_in="\$output_dir8/in.tsv"
touch \$tsv_in
# Create the input tsv list
for tsv in ${tsvs}; do
if [[ -s "\$tsv" ]]; then
echo -e "\$tsv" >> "\$tsv_in"
fi
done
tsv_out="\$output_dir8/${sample_id}_tnb.tsv"
python /scomatic/scripts/TrinucleotideBackground/TrinucleotideContextBackground.py --in_tsv \$tsv_in \
--out_file \$tsv_out
"""
}
workflow {
if (params.csv) {
input_ch = Channel.fromPath(params.csv, checkIfExists: true).
splitCsv(header:true).
map(row -> tuple "${row.sample_id}", "${row.bam}", "${row.bai}", "${row.metadata}")
} else {
input_ch = Channel.from(params.sample_id).
merge(Channel.fromPath(params.bam, checkIfExists: true)).
merge(Channel.fromPath(params.bai, checkIfExists: true)).
merge(Channel.fromPath(params.meta, checkIfExists: true))
}
splitbam_outch = SPLITBAM(input_ch)
// Issue with scatter gather groupTuple blocking patterns:
// example discussion: https://github.com/nextflow-io/nextflow/issues/796
// Or https://groups.google.com/g/nextflow/c/fScdmB_w_Yw
// Solution: https://labs.epi2me.io/two-years-of-nextflow/ and https://gist.github.com/cjw85/d334352e49ddd2e8bf2bd8e3891f3fe5
// Make an auxiliary chanel counting the number of cell types per sample
// The groupKey is decorated by the number of BAMs for each cell type for each sample
// This should allow for non-blocked parallelized operations to proceed downstream
ct_counts = splitbam_outch
.map { sample_id, bams, bais, dir -> tuple(sample_id, groupKey(sample_id, bams.size()))}
// Split the per sample split sample bams intro multiple input channels to permit parallelization
splitbam_outch
.map { sample_id, bams, bais, dir -> tuple(sample_id, bams, bais) }
.transpose()
.set { basecounts_inch }
basecounts_outch = BASECOUNTS_SPLIT(params.ref, basecounts_inch)
// Merge the output channel indexed on the sized key and then group
basecounts_outch
.combine(ct_counts, by: 0)
.map{samp, tsv, key -> tuple(key, tsv)}
.groupTuple(by: 0)
.set{grouped_ch}
mergecounts_outch = MERGECOUNTS(grouped_ch)
variantcalling_outch = VARIANTCALLING(params.ref, MERGECOUNTS.out)
callablesites_outch = CALLABLESITES(VARIANTCALLING.out)
// Because the split bam channel lacks the decorated (keyed) sample id, we have to bring this back in thru joining
// This might not actually matter at this point...
splitbam_outch
.join(ct_counts)
.map{ samp, bams, bais, dir, key -> tuple(key, bams, bais, dir) }
.set{splitbam_keyed}
splitbam_keyed
.join(variantcalling_outch)
.transpose()
.set{ callable_inch }
CALLABLE_PERCT(params.ref, callable_inch)
// Same with the original input channel -- create a keyed version
input_ch
.join(ct_counts)
.map{samp, bam, bai, meta, key -> tuple(key, bam, bai, meta)}
.set{input_ch_keyed}
gt_inch = splitbam_keyed.join(variantcalling_outch).join(input_ch_keyed.map {
sample_id, bam, bai, meta ->
tuple(sample_id, meta)
}).transpose()
GENOTYPE_CELLS(params.ref, gt_inch)
TRINUC_BACKGROUND(grouped_ch)
}
workflow.onComplete {
log.info(workflow.success ? "Success!" : "Something has gone awry...")
}