-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile_subtype
executable file
·244 lines (222 loc) · 7.87 KB
/
Snakefile_subtype
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
import os
# To run locally:
# snakemake --snakefile Snakefile_subtype --keep-going --cores 4 --use-singularity --singularity-prefix ~/.singularity --singularity-args "--home ~"
# To visualise the pipeline:
# snakemake --snakefile Snakefile_subtype --dag | dot -Tsvg > pipeline_subtype.svg
localrules: all
folder = os.path.abspath(config.get("folder", '..'))
data_dir = os.path.join(folder, 'data')
# The files below are the ones provided by the UK Drug Resistance Database
sequence_data = os.path.join(data_dir, 'input', 'sequencetable.csv')
sequence_fa = os.path.join(data_dir, 'input', 'sequences.fasta.xz')
# The alignment will be split N chunks, which will be jpHMM-subtyped in parallel, and the results recombined.
# N should be smaller than the number of sequences in the alignment
N = 10000
rule all:
input:
os.path.join(data_dir, 'metadata', 'metadata.uk.tab'),
os.path.join(data_dir, 'aln', 'aln.uk.fa.xz'),
rule metadata:
'''Reformats input UKHIVDB metadata.'''
input:
data = sequence_data,
output:
data = temp(os.path.join(data_dir, 'metadata', 'metadata.uk.no_subtype.tab'))
params:
mem = 2000,
name = 'metadata',
qos = 'fast'
threads: 1
singularity: "docker://evolbioinfo/python-evol:v3.6"
shell:
"""
python3 {folder}/py/prepare_metadata.py --input_data {input.data} --output_data {output.data}
"""
rule sequences:
'''
Unaligns input sequences.
'''
input:
aln = sequence_fa,
output:
fa = temp(os.path.join(data_dir, 'aln', 'sequences.uk.fa'))
singularity:
"docker://evolbioinfo/goalign:v0.3.6"
params:
mem = 2000,
name = 'unalign',
qos = 'fast',
threads: 1
shell:
"""
goalign unalign -i {input.aln} > {output.fa}
"""
rule xzip:
'''
Xzips the alignments.
'''
input:
aln = os.path.join(data_dir, 'aln', '{aln}.fa'),
output:
aln = os.path.join(data_dir, 'aln', '{aln}.fa.xz'),
params:
mem = 120000,
name = 'xz_{aln}',
qos = 'fast',
threads: 1
shell:
"""
xz -zc {input.aln} > {output.aln}
"""
rule unxzip_LA_aln:
'''
Unxzips an input LA alignment.
'''
input:
fa = os.path.join(data_dir, 'input', 'HIV1_REF_2010_pol_DNA.fa.xz')
output:
fa = temp(os.path.join(data_dir, 'input', 'HIV1_REF_2010_pol_DNA.fa'))
params:
mem = 120000,
name = 'unxz_ref',
qos = 'fast',
partition = 'common,dedicated,bioevo'
threads: 1
shell:
"""
xz -dc {input.fa} > {output.fa}
"""
rule ref_jpHMM:
'''Transforms an official LA alignment into a reference file for jpHMM'''
input:
fa = os.path.join(data_dir, 'input', 'HIV1_REF_2010_pol_DNA.fa')
output:
fa = temp(os.path.join(data_dir, 'input', 'jpHMM_files', 'input', 'HIV1_REF_2010_pol_DNA.fa')),
params:
mem = 2000,
name = 'jphmm_ref',
qos = 'fast',
threads: 1
singularity: "docker://evolbioinfo/jphmm_tools:v0.1.2"
shell:
"""
jphmm_ref --la_alignment {input.fa} --jphmm_ref_alignment {output.fa}
"""
rule aln_length:
'''
Calculates alignment length.
'''
input:
aln = os.path.join(data_dir, '{prefix}.fa.xz'),
output:
length = os.path.join(data_dir, '{prefix}.length'),
singularity:
"docker://evolbioinfo/goalign:v0.3.6"
params:
mem = 2000,
name = 'aln_len',
qos = 'fast',
threads: 1
shell:
"""
goalign stats length -i {input.aln} > {output.length}
"""
rule split_fa:
'''Splits fasta into chunks.'''
input:
fa = os.path.join(data_dir, 'aln', 'sequences.uk.fa'),
output:
fa = temp(expand(os.path.join(data_dir, 'aln', 'split_aln', 'sequences.uk.{n}.fa'), n=range(N)))
params:
mem = 1000,
name = 'split_fa',
fa = os.path.join(data_dir, 'aln', 'split_aln', 'sequences.uk.{}.fa'),
qos = 'fast'
threads: 1
singularity: "docker://evolbioinfo/jphmm_tools:v0.1.4"
shell:
"""
jphmm_split --out_fa_pattern {params.fa} --in_fa {input.fa} --n {N}
"""
rule subtype_jpHMM:
'''Subtypes sequences with jpHMM'''
input:
fa = os.path.join(data_dir, 'aln', 'split_aln', 'sequences.uk.{n}.fa'),
emission_priors = os.path.join(data_dir, 'input', 'jpHMM_files', 'priors', 'emissionPriors_HIV.txt'),
transition_priors = os.path.join(data_dir, 'input', 'jpHMM_files', 'priors', 'transition_priors.txt'),
aln = os.path.join(data_dir, 'input', 'jpHMM_files', 'input', 'HIV1_REF_2010_pol_DNA.fa'),
output:
msa = temp(os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', '{n}', 'alignment_to_msa.txt')),
rec = temp(os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', '{n}', 'recombination.txt'))
params:
mem = 20000,
name = 'subtype_{n}',
output_dir = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', '{n}'),
qos = 'normal'
threads: 1
singularity: "docker://evolbioinfo/jphmm:v03.2015"
shell:
"""
mkdir -p {params.output_dir}
echo "jpHMM -s {input.fa} -a {input.emission_priors} -b {input.transition_priors} -i {input.aln} -v HIV -o {params.output_dir}"
jpHMM -s {input.fa} -a {input.emission_priors} -b {input.transition_priors} -i {input.aln} -v HIV -o {params.output_dir}
"""
rule combine_jpHMM_output:
'''Combines jpHMM output files'''
input:
msa = expand(os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', '{n}', 'alignment_to_msa.txt'), n=range(N)),
rec = expand(os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', '{n}', 'recombination.txt'), n=range(N))
output:
msa = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', 'bk', 'alignment_to_msa.txt'),
rec = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', 'bk', 'recombination.txt')
params:
mem = 1000,
name = 'combine_uk',
dir = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk'),
qos = 'normal'
threads: 1
shell:
"""
cd {params.dir}
cat {params.dir}/*/recombination.txt > {params.dir}/bk/recombination.txt
cat {params.dir}/*/alignment_to_msa.txt > {params.dir}/bk/alignment_to_msa.txt
"""
rule aln_jpHMM:
'''Aligns sequences with jpHMM'''
input:
fa = os.path.join(data_dir, 'aln', 'sequences.uk.fa'),
msa = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', 'bk', 'alignment_to_msa.txt'),
length = os.path.join(data_dir, 'input', 'HIV1_REF_2010_pol_DNA.length'),
output:
aln = temp(os.path.join(data_dir, 'aln', 'aln.uk.fa')),
params:
mem = 10000,
name = 'aln_jpHMM',
qos = 'fast'
threads: 1
singularity: "docker://evolbioinfo/jphmm_tools:v0.1.4"
shell:
"""
jphmm_align --jphmm_msas {input.msa} --in_fas {input.fa} --out_aln {output.aln} \
--aln_len `head -n 1 {input.length}`
"""
rule metadata_jpHMM:
'''Adds jpHMM subtype to metadata.'''
input:
data = os.path.join(data_dir, 'metadata', 'metadata.uk.no_subtype.tab'),
msa = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', 'bk', 'alignment_to_msa.txt'),
rec = os.path.join(data_dir, 'aln', 'jpHMM_output', 'uk', 'bk', 'recombination.txt'),
aln = os.path.join(data_dir, 'input', 'HIV1_REF_2010_pol_DNA.fa'),
output:
data = os.path.join(data_dir, 'metadata', 'metadata.uk.tab')
params:
mem = 2000,
name = 'metadata_subtyped',
qos = 'fast'
threads: 1
singularity: "docker://evolbioinfo/jphmm_tools:v0.1.4"
shell:
"""
python3 {folder}/py/add_jphmm_subtype.py --in_data {input.data} --out_data {output.data} \
--in_msa {input.msa} --in_rec {input.rec} --ref_aln {input.aln}
"""