-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncbi-ab-sample-downloader.py
316 lines (252 loc) · 16.5 KB
/
ncbi-ab-sample-downloader.py
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
# /usr/bin/python3
import os
import time
import re
import sys
from pgd_schema import Aro, Strain, StrainAntibioticSusceptibility, GenomeProjectCoreSample,GenomeProjectSequenceAssembly
from optparse import OptionParser
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
def main():
# Get options from the command line
parser = OptionParser()
parser.add_option("--organism", dest="organism", action="store", type="string", help="Genus, species or strain name",metavar="ORGANISM")
parser.add_option("--assembly", action="store_true", dest="assembly")
parser.add_option("--sra", action="store_true", dest="sra")
parser.add_option("--out", dest="out", action="store", type="string", help="Output path and file name",metavar="OUT", default="out.txt")
parser.add_option("-d", "--database", dest="database", action="store", type="string",help="Name of database you wish to connect to", metavar="DATABASE")
parser.add_option("-H", "--host", dest="host", action="store", type="string", help="Database host", metavar="HOST")
parser.add_option("-u", "--user", dest="user", action="store", type="string", help="User name", metavar="NAME")
parser.add_option("-p", "--password", dest="password", action="store", type="string", help="Password", metavar="PASSWORD")
(options, args) = parser.parse_args()
queries=["antibiogram[filter]"]
if options.organism is None:
print("Please specify a genus, species or strain name for --organism")
sys.exit()
else:
queries.append(options.organism+"[ORGN]")
if options.assembly and options.sra:
parser.error("Error: --assembly and --sra are mutually exclusive. Please provide only one of these options")
if options.sra:
queries.append("biosample sra[filter]")
if options.assembly:
queries.append("biosample assembly[filter]")
query=" AND ".join(queries)
engine = None
if options.database is not None and options.user is not None and options.password is not None and options.host is not None:
engine = create_engine('mysql+pymysql://'+options.user+':'+options.password+'@'+options.host+':3306/'+options.database)
# Test database connection
try:
if engine.connect():
print("Connected to "+options.database+" as "+options.user)
except:
print("Writing to file")
Session = sessionmaker(bind=engine)
# create a new session
session = Session()
# Get BioSample Accessions for samples that have an antibiogram
cmd = "esearch -db BioSample -query \""+ query +"\" | efetch -format docsum | xtract -pattern DocumentSummary -element BioSample@accession Organism -block Attribute -if Attribute@attribute_name -equals strain -def \"N/A\" -element Attribute -block Attribute -if Attribute@attribute_name -equals isolate -def \"N/A\" -element Attribute > biosamples.txt"
print(cmd)
os.system(cmd)
filename=options.out;
filename=filename.replace(" ","_")
f = open(filename, 'w')
header=["sample_accession","organism_name"]
if options.assembly:
header.extend(["assembly_accession","assembly_name","assembly_status","organization","ftp_path"])
if options.sra:
header.extend(["sra_exp_acc|sra_run_acc|instrument"])
header.extend(["antibiotic","phenotype","sign","measurement","units","laboratory_typing_method", "laboratory_typing_platform","vendor","laboratory_typing_method_version_or_reagent"])
f.write("\t".join(header)+"\n")
phenotypes = {}
assemblies = {}
with open('biosamples.txt') as file:
for line in file:
metadata = line.rstrip().split("\t")
biosample=metadata[0]
species=metadata[1]
strain=metadata[2]
try:
isolate=metadata[3]
except:
isolate=""
species=species.replace("missing","")
isolate=isolate.replace("missing","")
if species != "":
organism=species+" strain "+strain
if isolate !="":
organism = species + " isolate " + isolate
if options.assembly:
cmd2 = "esearch -db assembly -query "+biosample+" | efetch -format docsum | xtract -pattern DocumentSummary -def \"N/A\" -element AssemblyAccession AssemblyName AssemblyStatus SubmitterOrganization FtpPath_RefSeq > assembly.txt"
return2=1
# Keep running until you get a successful return code
while (return2!=0):
print(cmd2)
return2=os.system(cmd2)
time.sleep(1)
with open('assembly.txt') as ab_file:
for ab_line in ab_file:
columns = re.split(r'\t', ab_line.rstrip())
assemblies[biosample] = {}
assemblies[biosample]['accession'] = columns[0]
assemblies[biosample]['name'] = columns[1]
assemblies[biosample]['status'] = columns[2]
assemblies[biosample]['organization'] = columns[3]
assemblies[biosample]['refseq_path'] = columns[4]
sra_runs = []
if options.sra:
cmd2 = "esearch -db sra -query " + biosample + " | efetch -format XML | xtract -pattern EXPERIMENT_PACKAGE -tab \"|\" -element EXPERIMENT@accession RUN@accession INSTRUMENT_MODEL > sra.txt"
return2 = 1
# Keep running until you get a successful return code
while (return2 != 0):
print(cmd2)
return2 = os.system(cmd2)
time.sleep(1)
with open('sra.txt') as sra_file:
for sra_line in sra_file:
sra_runs.append(sra_line.rstrip())
# Call no. 3 get the associated antibiogram
cmd3 = "esearch -db BioSample -query " + biosample + " | efetch -format xml | xtract -pattern Row -sep \"\\t\" -tab \"\\n\" -def \"N/A\" -element Cell > antibiogram.txt"
return3=1
while return3 !=0:
try:
return3=os.system(cmd3)
except:
print(cmd3)
time.sleep(1)
with open('antibiogram.txt') as ab_file:
for ab_line in ab_file:
columns = re.split(r'\t', ab_line.rstrip())
phenotypes[biosample] = {}
phenotypes[biosample]['antibiotic'] = columns[0]
phenotypes[biosample]['phenotype'] = columns[1]
phenotypes[biosample]['sign'] = columns[2]
phenotypes[biosample]['measurement'] = columns[3]
phenotypes[biosample]['units'] = columns[4]
phenotypes[biosample]['lab_type_method'] = columns[5]
for value in columns[6:9]:
if value in laboratory_typing_platform:
phenotypes[biosample]['lab_typing_platform'] = value
if value in vendor:
phenotypes[biosample]['vendor'] = value
if value in laboratory_typing_method_version_or_reagent:
phenotypes[biosample]['lab_typing_method_version_or_reagent'] = value
if value in testing_standards:
phenotypes[biosample]['testing_standard'] = value
if 'measurement_units' not in phenotypes[biosample]:
phenotypes[biosample]['measurement_units']=None;
if 'testing_standard' not in phenotypes[biosample]:
phenotypes[biosample]['testing_standard']=None;
if 'lab_typing_method_version_or_reagent' not in phenotypes[biosample]:
phenotypes[biosample]['lab_typing_method_version_or_reagent'] = None;
if 'vendor' not in phenotypes[biosample]:
phenotypes[biosample]['vendor'] = None;
if 'lab_typing_platform' not in phenotypes[biosample]:
phenotypes[biosample]['lab_typing_platform'] = None;
# If a database connection for Pseudomonas Genome Database is in place, insert the new data, otherwise just write it out at the end
if engine is not None:
biosampleRecord = session.query(GenomeProjectCoreSample).filter(
GenomeProjectCoreSample.sample_accession == biosample).first()
if biosampleRecord:
assemblyRecord = session.query(GenomeProjectSequenceAssembly).filter(
GenomeProjectSequenceAssembly.sample_id == biosampleRecord.id).first()
if assemblyRecord:
strain_record = session.query(Strain).filter(Strain.genome_project_sequence_assembly_id == assemblyRecord.id).first()
if strain_record:
strain_ab_record = session.query(StrainAntibioticSusceptibility).filter(
StrainAntibioticSusceptibility.strain_id == strain_record.strain_id,
StrainAntibioticSusceptibility.antibiotic == phenotypes[biosample]['antibiotic'],
StrainAntibioticSusceptibility.phenotype == phenotypes[biosample]['phenotype'],
StrainAntibioticSusceptibility.measurement_sign == phenotypes[biosample]['sign'],
StrainAntibioticSusceptibility.measurement == phenotypes[biosample]['measurement'],
StrainAntibioticSusceptibility.measurement_units == phenotypes[biosample]['units'],
StrainAntibioticSusceptibility.laboratory_typing_method == phenotypes[biosample][
'lab_type_method'],
StrainAntibioticSusceptibility.laboratory_typing_platform == phenotypes[biosample][
'lab_typing_platform'],
StrainAntibioticSusceptibility.vendor == phenotypes[biosample]['vendor'],
StrainAntibioticSusceptibility.laboratory_typing_method_version_or_reagent ==
phenotypes[biosample]['lab_typing_method_version_or_reagent'],
StrainAntibioticSusceptibility.testing_standard == phenotypes[biosample]['testing_standard']).first()
if strain_ab_record is None:
aro_record =session.query(Aro).filter(Aro.term == phenotypes[biosample]['antibiotic']).first()
aro_term = None
if aro_record is not None:
aro_term = aro_record.accession
else:
ab_phenotype=phenotypes[biosample]['antibiotic']
ab_phenotype=ab_phenotype.replace("micin","mycin")
aro_record = session.query(Aro).filter(Aro.term == ab_phenotype).first()
if aro_record:
aro_term = aro_record.accession
strain_ab_record_new = StrainAntibioticSusceptibility(
strain_id=strain_record.strain_id,
antibiotic=phenotypes[biosample]['antibiotic'],
aro_term=aro_term,
phenotype = phenotypes[biosample]['phenotype'],
measurement_sign = phenotypes[biosample]['sign'],
measurement=phenotypes[biosample]['measurement'],
measurement_units =phenotypes[biosample]['units'],
laboratory_typing_method=phenotypes[biosample]['lab_type_method'],
laboratory_typing_platform=phenotypes[biosample]['lab_typing_platform'],
vendor =phenotypes[biosample]['vendor'],
laboratory_typing_method_version_or_reagent=phenotypes[biosample]['lab_typing_method_version_or_reagent'],
testing_standard = phenotypes[biosample]['testing_standard'])
session.add(strain_ab_record_new)
session.commit()
f.write(biosample+"\t")
f.write(organism + "\t")
if options.assembly:
if biosample in assemblies:
f.write(str(assemblies[biosample]['accession'])+"\t")
f.write(str(assemblies[biosample]['name']) + "\t")
f.write(str(assemblies[biosample]['status']) + "\t")
f.write(str(assemblies[biosample]['organization']) + "\t")
f.write(str(assemblies[biosample]['refseq_path']) + "\t")
else:
f.write("\t")
f.write("\t")
f.write("\t")
f.write("\t")
f.write("\t")
if options.sra:
f.write(" ; ".join(sra_runs)+"\t")
f.write(str(phenotypes[biosample]['antibiotic'])+"\t")
f.write(str(phenotypes[biosample]['phenotype']) + "\t")
f.write(str(phenotypes[biosample]['sign']) + "\t")
f.write(str(phenotypes[biosample]['measurement']) + "\t")
f.write(str(phenotypes[biosample]['units']) + "\t")
f.write(str(phenotypes[biosample]['lab_type_method']).replace("None","") + "\t")
f.write(str(phenotypes[biosample]['lab_typing_platform']).replace("None","") + "\t")
f.write(str(phenotypes[biosample]['vendor']).replace("None","") + "\t")
f.write(str(phenotypes[biosample]['lab_typing_method_version_or_reagent']).replace("None","") + "\n")
f.flush()
print(biosample, end="\t")
print(organism, end="\t")
if options.assembly:
if biosample in assemblies:
print(assemblies[biosample]['accession'], end="\t")
print(assemblies[biosample]['name'], end="\t")
print(assemblies[biosample]['status'], end="\t")
print(assemblies[biosample]['organization'], end="\t")
print(assemblies[biosample]['refseq_path'], end="\t")
else:
print("\t\t\t\t\t")
if options.sra:
print(" ; ".join(sra_runs))
print(phenotypes[biosample]['antibiotic'], end="\t")
print(phenotypes[biosample]['phenotype'], end="\t")
print(phenotypes[biosample]['sign'], end="\t")
print(phenotypes[biosample]['measurement'], end="\t")
print(phenotypes[biosample]['units'], end="\t")
print(str(phenotypes[biosample]['lab_type_method']).replace("None",""), end="\t")
print(str(phenotypes[biosample]['lab_typing_platform']).replace("None",""), end="\t")
print(str(phenotypes[biosample]['vendor']).replace("None",""), end="\t")
print(str(phenotypes[biosample]['lab_typing_method_version_or_reagent']).replace("None",""), end="\n")
f.close()
laboratory_typing_platform = ['Microscan', 'Phoenix', 'Sensititre', 'Vitek']
vendor = ['Becton Dickinson', 'Biomérieux', 'Siemens', 'Trek', 'Biom?rieux', 'Biomerieux']
laboratory_typing_method_version_or_reagent = ['96-Well Plate', 'E-Test', 'GM-NEG']
testing_standards = ['BSAC', 'CLSI', 'DIN', 'EUCAST', 'NARMS', 'NCCLS', 'SFM', 'SIR', 'WRG']
if __name__ == '__main__':
main()