-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
407 lines (363 loc) · 10.8 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
#!/usr/bin/env python3
from pathlib import Path
from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider
import csv
import os
#############
# FUNCTIONS #
#############
def busco_input(wildcards):
if wildcards.guppy == 'ref':
return(raw_ref)
else:
return('output/051_oriented/{guppy}.{flye_mode}/contigs.fa')
###########
# GLOBALS #
###########
# NCBI reference genome
HTTP = HTTPRemoteProvider()
remote_ref_url = (
'https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/003/254/395/'
'GCF_003254395.2_Amel_HAv3.1/GCF_003254395.2_Amel_HAv3.1_genomic.fna.gz')
local_ref = 'output/GCF_003254395.2_Amel_HAv3.1_genomic.fna.gz'
raw_ref = 'output/GCF_003254395.2_Amel_HAv3.1_genomic.fna'
# remote_ref = HTTP.remote(
# ('https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/149/405/'
# 'GCF_000149405.2_ASM14940v2/GCF_000149405.2_ASM14940v2_assembly_structure/'
# 'Primary_Assembly/assembled_chromosomes/FASTA/chr17.fna.gz'),
# keep_local=True)
# raw_ref = 'data/GCF_000149405.2_chr17.fna'
# BUSCO lineage
busco_lineage = 'hymenoptera_odb10'
lineage_url = ('https://busco-data.ezlab.org/v5/data/lineages/'
'hymenoptera_odb10.2020-08-05.tar.gz')
lineage_archive = f'output/080_busco/{busco_lineage}.tar.gz'
# busco_lineage = 'eukaryota_odb10'
# lineage_archive = HTTP.remote(
# ('https://busco-data.ezlab.org/v5/data/lineages/'
# 'eukaryota_odb10.2020-09-10.tar.gz'),
# keep_local=True)
lineage_path = f'output/080_busco/{busco_lineage}'
versions_manifest = 'data/versions_to_run.csv'
# Containers
biopython = 'docker://quay.io/biocontainers/biopython:1.78'
bcftools = 'docker://quay.io/biocontainers/bcftools:1.15.1--h0ea216a_0'
busco = 'docker://quay.io/biocontainers/busco:5.3.2--pyhdfd78af_0'
flye = 'docker://quay.io/biocontainers/flye:2.9--py39h6935b12_1'
minimap = 'docker://quay.io/biocontainers/minimap2:2.24--h7132678_1'
mummer = 'docker://quay.io/biocontainers/mummer:3.23--pl5321h1b792b2_13'
quast = 'docker://quay.io/biocontainers/quast:5.0.2--py36pl5321hcac48a8_7'
ragtag = 'docker://quay.io/biocontainers/ragtag:2.1.0--pyhb7b1952_0'
samtools = 'docker://quay.io/biocontainers/samtools:1.15.1--h1170115_0'
########
# MAIN #
########
guppy_versions = {}
with open(versions_manifest, 'rt') as f:
reader = csv.DictReader(row for row in f if not row.startswith('#'))
for row in reader:
guppy_versions[row['name']] = row['container']
versions_to_run = sorted(set(guppy_versions.keys()))
#########
# RULES #
#########
subworkflow process_reads:
snakefile: 'process_reads.Snakefile'
wildcard_constraints:
guppy = '|'.join(versions_to_run) + '|ref'
rule target:
input:
# expand('output/060_dnadiff/{guppy}.{flye_mode}/contigs.snps',
# guppy=versions_to_run,
# flye_mode=['nano-raw', 'nano-hq']),
expand('output/065_minimap-snps/{guppy}.{flye_mode}/out.snps.vcf',
guppy=versions_to_run,
flye_mode=['nano-raw', 'nano-hq']),
expand('output/080_busco/{guppy}.{flye_mode}/run_{busco_lineage}/full_table.tsv',
busco_lineage=busco_lineage,
guppy=versions_to_run,
flye_mode=['nano-raw', 'nano-hq']),
f'output/080_busco/ref.ref/run_{busco_lineage}/full_table.tsv',
'output/tmp/070_quast/report.txt'
# compare genomes
# using busco
rule busco:
input:
fasta = busco_input,
lineage = lineage_path
output:
f'output/080_busco/{{guppy}}.{{flye_mode}}/run_{busco_lineage}/full_table.tsv',
log:
Path(('output/logs/'
'busco.{guppy}.{flye_mode}.log')).resolve()
benchmark:
Path('output/benchmarks/busco.{guppy}.{flye_mode}.log.tsv').resolve()
params:
wd = 'output/080_busco',
fasta = lambda wildcards, input:
Path(input.fasta).resolve(),
lineage = lambda wildcards, input:
Path(input.lineage).resolve()
threads:
workflow.cores
resources:
time = 60,
mem_mb = 10000
singularity:
busco
shell:
'mkdir -p {params.wd} ; '
'cd {params.wd} || exit 1 ; '
'busco '
'--offline '
'--force '
'--in {params.fasta} '
'--out {wildcards.guppy}.{wildcards.flye_mode} '
'--lineage_dataset {params.lineage} '
'--cpu {threads} '
'--mode genome '
'&> {log}'
# using quast
rule quast:
input:
genomes = expand('output/051_oriented/{guppy}.{flye_mode}/contigs.fa',
guppy=versions_to_run,
flye_mode=['nano-raw', 'nano-hq']),
ref = local_ref
output:
'output/tmp/070_quast/report.txt'
params:
outdir = 'output/tmp/070_quast'
threads:
workflow.cores
resources:
time = 1440,
mem_mb = 50000
log:
'output/logs/quast.log'
benchmark:
'output/benchmarks/quast.tsv'
container:
quast
shell:
'quast '
'-o {params.outdir} '
'-r {input.ref} '
'-t {threads} '
'-L '
'--eukaryote '
'--k-mer-stats '
'{input} '
'&> {log}'
# needs to be replaced with minimap2
# docker://quay.io/biocontainers/minimap2:2.24--h7132678_1 has paftools
# see https://github.com/lh3/minimap2/blob/fe35e679e95d936698e9e937acc48983f16253d6/cookbook.md#calling-variants-from-assembly-to-reference-alignment
rule snps_only:
input:
'output/065_minimap-snps/{guppy}.{flye_mode}/out.vcf'
output:
'output/065_minimap-snps/{guppy}.{flye_mode}/out.snps.vcf'
log:
'output/logs/snps_only.{guppy}.{flye_mode}.log'
container:
bcftools
shell:
'bcftools view '
'--types snps '
'{input} '
'> {output} '
'2> {log}'
rule paftools_snps:
input:
ref = raw_ref,
paf = 'output/tmp/065_minimap-snps/{guppy}.{flye_mode}/aln.sorted.paf'
output:
'output/065_minimap-snps/{guppy}.{flye_mode}/out.vcf'
log:
paftools = 'output/logs/minimap_snps.{guppy}.{flye_mode}.paftools.log',
resources:
time = 10,
container:
minimap
shell:
'paftools.js '
'call '
'-f {input.ref} '
'<( cat {input.paf} ) '
'>> {output} '
'2> {log}'
rule minimap_sort:
input:
'output/tmp/065_minimap-snps/{guppy}.{flye_mode}/aln.paf'
output:
pipe('output/tmp/065_minimap-snps/{guppy}.{flye_mode}/aln.sorted.paf')
threads:
1
resources:
time = 10
container:
minimap
shell:
'sort -k6,6 -k8,8n <( cat {input} ) >> {output} '
rule minimap:
input:
ref = raw_ref,
contigs = 'output/051_oriented/{guppy}.{flye_mode}/contigs.fa',
output:
pipe('output/tmp/065_minimap-snps/{guppy}.{flye_mode}/aln.paf')
log:
'output/logs/minimap_snps.{guppy}.{flye_mode}.minimap.log',
benchmark:
'output/benchmarks/minimap_snps.{guppy}.{flye_mode}.minimap.tsv'
threads:
min(12, workflow.cores - 2)
resources:
time = 10,
mem_mb = 4000 * 4
container:
minimap
shell:
'minimap2 '
'-t {threads} '
'-cx asm5 '
'--cs '
'{input.ref} '
'{input.contigs} '
'>> {output} '
'2> {log} '
# dnadiff uses Olin Silander's method (https://github.com/osilander/bonito_benchmarks)
rule dnadiff:
input:
ref = raw_ref,
contigs = 'output/051_oriented/{guppy}.{flye_mode}/contigs.fa',
output:
'output/060_dnadiff/{guppy}.{flye_mode}/contigs.snps'
params:
prefix = "output/060_dnadiff/{guppy}.{flye_mode}/contigs",
log:
'output/logs/dnadiff.{guppy}.{flye_mode}.log'
resources:
time = 2880,
mem_mb = 50000
container:
mummer
shell:
'dnadiff '
'{input.ref} '
'{input.contigs} '
'-p {params.prefix} '
'&> {log}'
# extract genomic contigs.
# n.b. this removes unplaced contigs!!!
rule orient_scaffolds:
input:
fa = 'output/040_flye/{guppy}.{flye_mode}/assembly.fasta',
agp = 'output/050_ragtag/{guppy}.{flye_mode}/ragtag.scaffold.agp'
output:
fa = 'output/051_oriented/{guppy}.{flye_mode}/contigs.fa'
log:
'output/logs/orient_scaffolds.{guppy}.{flye_mode}.log'
container:
biopython
script:
'src/orient_scaffolds.py'
rule ragtag:
input:
ref = local_ref,
query = 'output/040_flye/{guppy}.{flye_mode}/assembly.fasta'
output:
'output/050_ragtag/{guppy}.{flye_mode}/ragtag.scaffold.fasta',
'output/050_ragtag/{guppy}.{flye_mode}/ragtag.scaffold.agp'
params:
wd = 'output/050_ragtag/{guppy}.{flye_mode}'
log:
'output/logs/ragtag.{guppy}.{flye_mode}.log'
benchmark:
'output/benchmarks/ragtag.{guppy}.{flye_mode}.tsv'
threads:
min(workflow.cores, 64)
resources:
time = 120,
mem_mb = 50000
container:
ragtag
shell:
'ragtag.py scaffold '
'-o {params.wd} '
'-w '
# '-r -g 101 ' # only add gaps 101 Ns or longer DOESN'T WORK
'-t {threads} '
'{input.ref} '
'{input.query} '
'&> {log}'
# run assembly
rule flye:
input:
fq = process_reads('output/035_processed_reads/{guppy}.fastq.gz')
output:
'output/040_flye/{guppy}.{flye_mode}/assembly.fasta'
params:
outdir = 'output/040_flye/{guppy}.{flye_mode}',
mode = '{flye_mode}'
threads:
min(128, workflow.cores)
resources:
time = 120,
mem_mb = 128000
log:
'output/logs/flye.{guppy}.{flye_mode}.log'
benchmark:
'output/benchmarks/flye.{guppy}.{flye_mode}.tsv'
container:
flye
shell:
'flye '
# '--resume '
'--{wildcards.flye_mode} '
'{input.fq} '
'--out-dir {params.outdir} '
'--threads {threads} '
'&>> {log}'
# GENERIC
rule busco_expand:
input:
lineage_archive
output:
directory(lineage_path)
singularity:
busco
shell:
'mkdir -p {output} && '
'tar -zxf '
'{input} '
'-C {output} '
'--strip-components 1 '
rule download_lineage:
input:
HTTP.remote(
lineage_url,
keep_local=True)
output:
lineage_archive
shell:
'mv {input} {output}'
rule raw_ref:
input:
local_ref
output:
fa = raw_ref,
fai = f'{raw_ref}.fai'
singularity:
samtools
shell:
'gunzip -c {input} > {output.fa} ; '
'samtools faidx {output.fa}'
rule download_ref:
input:
HTTP.remote(
remote_ref_url,
keep_local=True)
output:
local_ref
shell:
'mv {input} {output}'