-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSConstruct
370 lines (331 loc) · 10.5 KB
/
SConstruct
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
"""
Download and curate the NCBI 16S rRNA sequences
TODO: download records updated before last download
"""
import configparser
import os
import sys
# requirements installed in the virtualenv
from SCons.Script import ARGUMENTS, Environment, Help, Variables
venv = os.environ.get('VIRTUAL_ENV')
if not venv:
sys.exit('--> an active virtualenv is required'.format(venv))
if not os.path.exists('settings.conf'):
sys.exit("Can't find settings.conf")
conf = configparser.SafeConfigParser()
conf.read('settings.conf')
settings = conf['settings']
vrs = Variables(None, ARGUMENTS)
vrs.Add('api_key', 'ncbi api_key for downloading data', settings['api_key'])
vrs.Add('email', 'email address for ncbi', settings['email'])
vrs.Add('nreq', ('Number of concurrent http requests to ncbi'), 8)
vrs.Add('out', default='output')
vrs.Add('retry', 'ncbi retry milliseconds', '60000')
vrs.Add('sort_by', 'sequence sorting', settings['sort_by'])
environment_variables = dict(
os.environ,
PATH=':'.join([
'bin',
os.path.join(venv, 'bin'),
'/usr/local/bin',
'/usr/bin',
'/bin']),
)
env = Environment(
ENV=environment_variables,
variables=vrs,
shell='bash',
taxit=(
'{singularity} exec '
'--bind $$(readlink -f $$(pwd)) '
'--pwd $$(readlink -f $$(pwd)) '
'{taxtastic} taxit'.format(**settings)),
eutils=(
'{singularity} exec '
'--bind $$(readlink -f $$(pwd)) '
'--pwd $$(readlink -f $$(pwd)) '
'{eutils}'.format(**settings))
)
env.Decider('MD5-timestamp')
Help(vrs.GenerateHelpText(env))
if False: # run_full_pipeline=True|False
taxdb = env.Command(
target='$out/taxonomy/ncbi_taxonomy.db',
source=None,
action='$taxit new_database sqlite:///$out')
taxtable = env.Command(
target='$out/taxonomy/taxtable.csv',
source=taxdb,
action=['$taxit get_descendants $SOURCE 543 ' # Enterobacteriaceae
'| named.py --taxids /dev/stdin sqlite:///$SOURCE '
'| $taxit taxtable '
'--out $TARGET '
'--tax-id-file '
'/dev/stdin $SOURCE'])
esearch = env.Command(
target='$out/ncbi/accessions.txt',
source=None,
action=['$eutils esearch '
'-db nucleotide '
'-query "Enterobacteriaceae[Organism] AND dnaj[All Fields]" '
'| mefetch -vv -api-key $api_key '
'-email $email '
'-format acc '
'-log $out/ncbi.log '
'-max-retry -1 '
'-proc $nproc'])
# TODO: incorporate ignore.txt
feature_table = env.Command(
target='$out/ncbi/feature_table.txt',
source=esearch,
action=['mefetch -vv '
'-api-key $api_key '
'-db nucleotide '
'-email $email '
'-format ft '
'-id $SOURCE '
'-log $out/ncbi.log '
'-max-retry -1 '
'-mode text '
'-out $TARGET '
'-proc $nproc '
'-retmax 1 '
'-retry $retry'])
coordinates = env.Command(
target='$out/ncbi/coordinates.csv',
source=feature_table,
action=('ftract '
'-feature gene:gene:"dnaj" '
'-feature CDS:product:"molecular chaperone DnaJ" '
'-feature CDS:product:"chaperone protein DnaJ" '
'-feature CDS:product:"DnaJ protein" '
'-feature CDS:product:"DnaJ domain-containing protein" '
'-feature CDS:product:"dnaJ domain protein" '
'-feature CDS:product:"chaperone dnaJ" '
'-feature CDS:product:"DnaJ" '
'-feature CDS:product:"dnaJ central domain protein" '
'--full-format '
'--out $TARGET '
'$SOURCE'))
genbank = env.Command(
target='$out/ncbi/records.gb',
source=coordinates,
action=('mefetch -vv '
'-id $SOURCE '
'-api-key $api_key '
'-csv '
'-db nucleotide '
'-email $email '
'-format gbwithparts '
'-log $out/ncbi.log '
'-max-retry -1 '
'-mode text '
'-out $TARGET'
'-proc $nproc '
'-retmax 1 '
'-retry $retry'))
'''
TODO: use time.date here
'''
ncbi_fa, ncbi_info, _, _, _ = env.Command(
target=[
'$out/ncbi/seqs.fasta',
'$out/ncbi/info.csv',
'$out/ncbi/pubmed_info.csv',
'$out/ncbi/references.csv',
'$out/ncbi/refseq_info.fasta'],
source=genbank,
action='extract_genbank.py $SOURCE 07-Mar-2019 $TARGETS')
types = env.Command(
target='$out/ncbi/types.txt',
source=None,
action=['$eutils esearch '
'-db nucleotide '
'-query "Enterobacteriaceae[Organism] AND '
'dnaj[All Fields] AND sequence_from_type[Filter]" '
'| mefetch -vv -api-key $api_key '
'-email $email '
'-format acc '
'-log $out/ncbi.log '
'-max-retry -1 '
'-proc $nproc'])
else:
ncbi_fa = '$out/ncbi/seqs.fasta'
ncbi_info = '$out/ncbi/seq_info.csv'
taxtable = '$out/taxtable.csv'
types = '$out/ncbi/types.txt'
'''
drop duplicate records and refseq originals
'''
dedup_fa, dedup_info = env.Command(
target=['$out/ncbi/dedup/seqs.fasta',
'$out/ncbi/dedup/dedup.csv'],
source=[ncbi_fa, ncbi_info],
action='drop_duplicates.py $SOURCES $TARGETS')
'''
add is_type column
'''
is_type = env.Command(
target='$out/ncbi/dedup/seq_info.csv',
source=[dedup_info, types],
action='is_type.py --out $TARGET $SOURCES')
'''
Select just species named rows. Genus column is added
for selecting Escherichia and Shigella species later
'''
named_info = env.Command(
target='$out/ncbi/dedup/named/seq_info.csv',
source=[is_type, taxtable],
action=['merge.py $SOURCES '
'--how inner ' # only species named rows
'--right-columns tax_id,species,genus '
'--out $TARGET'])
named_fa = env.Command(
target='$out/ncbi/dedup/named/seqs.fasta',
source=[dedup_fa, named_info],
action='partition_seqs.py --out $TARGET $SOURCES')
'''
get seqs for muscle alignment
'''
type_fa = env.Command(
target='$out/profile/seqs.fasta',
source=[named_info, dedup_fa],
action=['csvgrep.py --columns is_type True ${SOURCES[0]} | '
'partition_seqs.py --out $TARGET ${SOURCES[1]} /dev/stdin'])
'''
create muscle alignment
'''
muscle = env.Command(
target='$out/profile/muscle.fasta',
source=type_fa,
action='muscle -in $SOURCE -out $TARGET')
'''
initial hmmer profile
'''
rough_profile = env.Command(
target='$out/profile/rough.hmm',
source=muscle,
action='hmmbuild --dna --cpu 14 $TARGET $SOURCE')
'''
get type hits to first profile
'''
type_hits = env.Command(
target='$out/profile/hmm_rough.tsv',
source=[rough_profile, type_fa],
action='hmmsearch --cpu 14 --tblout $TARGET $SOURCES > /dev/null')
'''
LR134137_3722540_3722971 is last seqname per muscle and type_hits
'''
hmmer_info = env.Command(
target='$out/profile/filtered/seq_info.csv',
source=[named_info, type_hits],
action=['hmmer.py '
'--last-in LR134137_3722540_3722971 '
'--out $TARGET $SOURCES'])
'''
get seqs for new dnaj profile
'''
hmmer_seqs = env.Command(
target='$out/profile/filtered/seqs.fasta',
source=[type_fa, hmmer_info],
action='partition_seqs.py --out $TARGET $SOURCES')
'''
make another msa file this time using hmmer
'''
hmmer_msa = env.Command(
target='$out/profile/filtered/msa.fasta',
source=[rough_profile, hmmer_seqs],
action='hmmalign --outformat afa --dna -o $TARGET $SOURCES')
'''
dnaj hmmer final profile
'''
profile = env.Command(
target='$out/profile/filtered/dnaj.hmm',
source=hmmer_msa,
action='hmmbuild --dna --cpu 14 $TARGET $SOURCE')
'''
make another msa file for --last-in decision
'''
env.Command(
target='$out/types_msa.fasta',
source=[profile, type_fa],
action='hmmalign --outformat afa --dna -o $TARGET $SOURCES')
'''
hits against dnaj profile
'''
hits = env.Command(
target='$out/hmmer.tsv',
source=[profile, named_fa],
action='hmmsearch --cpu 14 --tblout $TARGET $SOURCES > /dev/null')
'''
LR134137_3722540_3722971 is last type strain in profile model
'''
info = env.Command(
target='$out/seq_info.csv',
source=[named_info, hits],
action=['hmmer.py '
'--last-in LR134137_3722540_3722971 '
'--out $TARGET $SOURCES'])
fa = env.Command(
target='$out/seqs.fasta',
source=[named_fa, info],
action='partition_seqs.py --out $TARGET $SOURCES')
one_fa, species_info = env.Command(
target=['$out/one/seqs.fasta', '$out/one/seq_info.csv'],
source=[fa, info],
action='extract_one.py --sort-by $sort_by $SOURCES $TARGETS')
'''
alignment
'''
one_msa = env.Command(
target='$out/one/msa.fasta',
source=[profile, one_fa],
action='hmmalign --dna --outformat afa -o $TARGET $SOURCES')
'''
candidatus info
'''
candidatus_info = env.Command(
target='$out/one/candidatus/seq_info.csv',
source=species_info,
action=['csvgrep.py '
'--columns organism '
'--ignore-case '
'--out $TARGET '
'"candidatus" $SOURCE'])
'''
candidatus info
'''
candidatus_fa = env.Command(
target='$out/one/candidatus/seqs.fasta',
source=[one_fa, candidatus_info],
action='partition_seqs.py --out $TARGET $SOURCES')
'''
no candidatus info
'''
no_candidatus_info = env.Command(
target='$out/one/no_candidatus/seq_info.csv',
source=species_info,
action=['csvgrep.py '
'--columns organism '
'--ignore-case '
'--invert-match '
'--out $TARGET '
'"candidatus" $SOURCE'])
'''
no candidatus fa
'''
no_candidatus_fa = env.Command(
target='$out/one/no_candidatus/seqs.fasta',
source=[one_fa, no_candidatus_info],
action='partition_seqs.py --out $TARGET $SOURCES')
'''
output all Escherichia (561) and Shigella (620) seqs into own dir
'''
genus_info = env.Command(
target='$out/genus/seq_info.csv',
source=info,
action='csvgrep.py --out $TARGET --columns genus "561|620" $SOURCE')
genus_fa = env.Command(
target='$out/genus/seqs.fasta',
source=[fa, genus_info],
action='partition_seqs.py --out $TARGET $SOURCES')