-
Notifications
You must be signed in to change notification settings - Fork 4
/
Snakefile
466 lines (422 loc) · 16.7 KB
/
Snakefile
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
container: "docker://mfansler/scutr-quant:0.5.0"
configfile: "config.yaml"
SQ_VERSION="0.5.0"
import os
import pandas as pd
from snakemake.utils import min_version
from sys import stderr
# robust loading of `load_configfile`
try:
## version >=8.0.0
from snakemake.common.configfile import load_configfile
except:
## version <8.0.0
from snakemake.io import load_configfile
# set minimum Snakemake version
min_version("6.0")
# print to stderr
def message(*args, **kwargs):
print(*args, file=stderr, **kwargs)
message(f"[INFO] scUTRquant v{SQ_VERSION}")
# make sure the tmp directory exists
os.makedirs(config['tmp_dir'], exist_ok=True)
# load target configuration
targets = load_configfile(config['targets_config'])
# check for valid target(s)
target_list = [config['target']] if isinstance(config['target'], str) else config['target']
if not set(target_list).issubset(targets.keys()):
message("[Error] Some target(s) '%s' not found!" % config['target'])
message("Available targets:", *targets.keys(), sep="\n ")
exit(1)
def get_target_file(key):
def f(wcs):
if not targets[wcs.target][key]:
return []
else:
return targets[wcs.target]['path'] + targets[wcs.target][key]
return f
# load sample data
message("[INFO] Loading sample data...")
samples = pd.read_csv(config['sample_file'], index_col='sample_id')
message("[INFO] Loaded %d samples." % len(samples.index))
# conform cell_annots for input
if config['cell_annots'] is None:
config['cell_annots'] = []
wildcard_constraints:
sample_id=config['sample_regex']
# get list of expected outputs
def get_outputs():
outputs = []
if config['include_reports']:
outputs += expand("qc/umi_count/{target}/{sample_id}.umi_count.html",
target=target_list,
sample_id=samples.index.values)
if (not config['output_format']) or ("sce" in config['output_format']):
if config['use_hdf5']:
outputs += expand("data/sce/{target}/{dataset}.{output_type}.{file_type}",
target=target_list,
dataset=config['dataset_name'],
output_type=config['output_type'],
file_type=['se.rds', 'assays.h5'])
else:
outputs += expand("data/sce/{target}/{dataset}.{output_type}.Rds",
target=target_list,
dataset=config['dataset_name'],
output_type=config['output_type'])
if "h5ad" in config['output_format']:
outputs += expand("data/h5ad/{target}/{dataset}.{output_type}.h5ad",
target=target_list,
dataset=config['dataset_name'],
output_type=config['output_type'])
return outputs
rule all:
input: get_outputs()
################################################################################
## Downloading and Preprocessing
################################################################################
## generate downloading rules for target files if a script is provided
## NB: script should generate files *relative* to the script
for target_id, target in targets.items():
if 'download_script' not in target or target['download_script'] is None:
continue
target_path = target['path']
download_script = target_path + target['download_script']
FILE_KEYS = ['gtf', 'kdx', 'merge_tsv',
'tx_annots', 'gene_annots',
'tx_annots_csv', 'gene_annots_csv']
target_files = [target_path + target[k] for k in FILE_KEYS if target[k] is not None]
rule:
name: f"download_{target_id}"
input: f"{download_script}"
output: expand("{target_file}", target_file=target_files)
conda: "envs/downloading.yaml"
shell:
"""
pushd $(dirname {input})
sh $(basename {input})
popd
"""
## Import downloading rules for barcodes
module bxs_workflow:
snakefile: "extdata/bxs/download.smk"
use rule * from bxs_workflow
## Convert merge data for tx output
rule generate_tx_merge:
input:
tsv=get_target_file('merge_tsv')
output:
"data/utrs/{target}/tx_merge.tsv"
shell:
"""
tail -n+2 {input.tsv} | cut -f1,2 > {output}
"""
## Convert merge data for gene output
rule generate_gene_merge:
input:
tsv=get_target_file('merge_tsv')
output:
"data/utrs/{target}/gene_merge.tsv"
shell:
"""
tail -n+2 {input.tsv} | cut -f1,3 > {output}
"""
################################################################################
## kallisto-bustools
################################################################################
def get_file_type(wildcards):
return "--bam" if samples.file_type[wildcards.sample_id] == 'bam' else ""
def get_sequence_files(wildcards):
return samples.files[wildcards.sample_id].split(';')
rule kallisto_bus:
input:
idx=get_target_file('kdx'),
files=get_sequence_files
output:
bus=temp("data/kallisto/{target}/{sample_id}/output.bus"),
ec="data/kallisto/{target}/{sample_id}/matrix.ec",
tx="data/kallisto/{target}/{sample_id}/transcripts.txt"
params:
tech=config['tech'],
strand=config['strand'],
bam=get_file_type
threads: 16
resources:
mem_mb=1000
conda: "envs/kallisto-bustools.yaml"
shell:
"""
outDir=$(dirname {output.bus})
rm -r $outDir
kallisto bus {params.bam} -t {threads} -i {input.idx} \\
-x {params.tech} {params.strand} -o $outDir --verbose {input.files}
"""
rule bustools_sort:
input:
"data/kallisto/{target}/{sample_id}/output.bus"
output:
"data/kallisto/{target}/{sample_id}/output.sorted.bus"
params:
tmpDir=lambda wcs: config['tmp_dir'] + "/bs-utrome-sort" + wcs.sample_id
threads: 4
resources:
mem_mb=2000
conda: "envs/kallisto-bustools.yaml"
shell:
"""
bustools sort -t{threads} -T {params.tmpDir} -o {output} {input}
"""
rule bustools_whitelist:
input:
"data/kallisto/{target}/{sample_id}/output.sorted.bus"
output:
"data/kallisto/{target}/{sample_id}/whitelist.txt"
conda: "envs/kallisto-bustools.yaml"
shell:
"""
bustools whitelist -o {output} {input}
"""
def get_whitelist(wildcards):
if not config['bx_whitelist']:
return "data/kallisto/%s/%s/whitelist.txt" % (wildcards.target, wildcards.sample_id)
else:
return config['bx_whitelist']
rule bustools_correct:
input:
bus="data/kallisto/{target}/{sample_id}/output.sorted.bus",
bxs=get_whitelist
output:
temp("data/kallisto/{target}/{sample_id}/output.corrected.bus")
conda: "envs/kallisto-bustools.yaml"
shell:
"""
bustools correct -w {input.bxs} -o {output} {input.bus}
"""
rule bustools_correct_sort:
input:
"data/kallisto/{target}/{sample_id}/output.corrected.bus"
output:
temp("data/kallisto/{target}/{sample_id}/output.corrected.sorted.bus")
params:
tmpDir=lambda wcs: config['tmp_dir'] + "/bs-utrome-sort" + wcs.target + "-" + wcs.sample_id
threads: 4
resources:
mem_mb=2000
conda: "envs/kallisto-bustools.yaml"
shell:
"""
bustools sort -t{threads} -T {params.tmpDir} -o {output} {input}
"""
def get_input_busfile(wildcards):
if config['correct_bus']:
return "data/kallisto/%s/%s/output.corrected.sorted.bus" % (wildcards.target, wildcards.sample_id)
else:
return "data/kallisto/%s/%s/output.sorted.bus" % (wildcards.target, wildcards.sample_id)
rule bustools_count_txs:
input:
bus=get_input_busfile,
txs="data/kallisto/{target}/{sample_id}/transcripts.txt",
ec="data/kallisto/{target}/{sample_id}/matrix.ec",
merge="data/utrs/{target}/tx_merge.tsv"
output:
mtx="data/kallisto/{target}/{sample_id}/txs.mtx",
txs="data/kallisto/{target}/{sample_id}/txs.genes.txt",
bxs="data/kallisto/{target}/{sample_id}/txs.barcodes.txt"
conda: "envs/kallisto-bustools.yaml"
shell:
"""
filename={output.mtx}
prefix=${{filename%.*}}
bustools count -e {input.ec} -t {input.txs} -g {input.merge} -o $prefix --em --genecounts {input.bus}
"""
rule bustools_count_genes:
input:
bus=get_input_busfile,
txs="data/kallisto/{target}/{sample_id}/transcripts.txt",
ec="data/kallisto/{target}/{sample_id}/matrix.ec",
merge="data/utrs/{target}/gene_merge.tsv"
output:
mtx="data/kallisto/{target}/{sample_id}/genes.mtx",
txs="data/kallisto/{target}/{sample_id}/genes.genes.txt",
bxs="data/kallisto/{target}/{sample_id}/genes.barcodes.txt"
conda: "envs/kallisto-bustools.yaml"
shell:
"""
filename={output.mtx}
prefix=${{filename%.*}}
bustools count -e {input.ec} -t {input.txs} -g {input.merge} -o $prefix --em --genecounts {input.bus}
"""
################################################################################
## Outputs
################################################################################
rule mtxs_to_sce_txs:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/txs.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
txs=expand("data/kallisto/{target}/{sample_id}/txs.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/txs.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
tx_annots=get_target_file('tx_annots'),
cell_annots=config['cell_annots']
output:
sce="data/sce/{target}/%s.txs.Rds" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells'],
tmp_dir=config['tmp_dir'],
use_hdf5=False
resources:
mem_mb=16000
conda: "envs/bioconductor-sce.yaml"
script:
"scripts/mtxs_to_sce_txs.R"
rule mtxs_to_sce_genes:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/genes.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
genes=expand("data/kallisto/{target}/{sample_id}/genes.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/genes.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
gene_annots=get_target_file('gene_annots'),
cell_annots=config['cell_annots']
output:
sce="data/sce/{target}/%s.genes.Rds" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells'],
tmp_dir=config['tmp_dir'],
use_hdf5=False
resources:
mem_mb=16000
conda: "envs/bioconductor-sce.yaml"
script:
"scripts/mtxs_to_sce_genes.R"
rule mtxs_to_sce_h5_txs:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/txs.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
txs=expand("data/kallisto/{target}/{sample_id}/txs.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/txs.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
tx_annots=get_target_file('tx_annots'),
cell_annots=config['cell_annots']
output:
sce="data/sce/{target}/%s.txs.se.rds" % config['dataset_name'],
h5="data/sce/{target}/%s.txs.assays.h5" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells'],
tmp_dir=lambda wcs: config['tmp_dir'] + "/sce-txs-" + wcs.target + "-" + config['dataset_name'],
use_hdf5=True
resources:
mem_mb=8000
threads: 8
conda: "envs/bioconductor-sce.yaml"
script:
"scripts/mtxs_to_sce_txs.R"
rule mtxs_to_sce_h5_genes:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/genes.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
genes=expand("data/kallisto/{target}/{sample_id}/genes.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/genes.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
gene_annots=get_target_file('gene_annots'),
cell_annots=config['cell_annots']
output:
sce="data/sce/{target}/%s.genes.se.rds" % config['dataset_name'],
h5="data/sce/{target}/%s.genes.assays.h5" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells'],
tmp_dir=lambda wcs: config['tmp_dir'] + "/sce-genes-" + wcs.target + "-" + config['dataset_name'],
use_hdf5=True
resources:
mem_mb=8000
threads: 8
conda: "envs/bioconductor-sce.yaml"
script:
"scripts/mtxs_to_sce_genes.R"
rule mtxs_to_h5ad_txs:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/txs.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
txs=expand("data/kallisto/{target}/{sample_id}/txs.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/txs.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
tx_annots=get_target_file('tx_annots_csv'),
cell_annots=config['cell_annots']
output:
h5ad="data/h5ad/{target}/%s.txs.h5ad" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells']
resources:
mem_mb=16000
conda: "envs/anndata.yaml"
script:
"scripts/mtxs_to_h5ad_txs.py"
rule mtxs_to_h5ad_genes:
input:
bxs=expand("data/kallisto/{target}/{sample_id}/genes.barcodes.txt",
sample_id=samples.index.values, allow_missing=True),
genes=expand("data/kallisto/{target}/{sample_id}/genes.genes.txt",
sample_id=samples.index.values, allow_missing=True),
mtxs=expand("data/kallisto/{target}/{sample_id}/genes.mtx",
sample_id=samples.index.values, allow_missing=True),
gtf=get_target_file('gtf'),
gene_annots=get_target_file('gene_annots_csv'),
cell_annots=config['cell_annots']
output:
h5ad="data/h5ad/{target}/%s.genes.h5ad" % config['dataset_name']
params:
genome=lambda wcs: targets[wcs.target]['genome'],
sample_ids=samples.index.values,
min_umis=config['min_umis'],
cell_annots_key=config['cell_annots_key'],
exclude_unannotated_cells=config['exclude_unannotated_cells']
resources:
mem_mb=16000
conda: "envs/anndata.yaml"
script:
"scripts/mtxs_to_h5ad_genes.py"
################################################################################
## Reports
################################################################################
rule report_umis_per_cell:
input:
bxs="data/kallisto/{target}/{sample_id}/txs.barcodes.txt",
txs="data/kallisto/{target}/{sample_id}/txs.genes.txt",
mtx="data/kallisto/{target}/{sample_id}/txs.mtx"
params:
min_umis=config['min_umis'],
sq_version=SQ_VERSION
output:
"qc/umi_count/{target}/{sample_id}.umi_count.html"
conda: "envs/rmd-reporting.yaml"
script:
"scripts/report_umi_counts_per_cell.Rmd"