-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
409 lines (375 loc) · 16.3 KB
/
functions.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
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
import json
import re
import requests
import glob
import sys
import pandas as pd
import numpy as np
from collections import defaultdict
import pubchempy as pcp
from chembl_webresource_client.new_client import new_client
from chemspipy import ChemSpider
from rdkit import Chem
from rdkit.Chem.rdMolDescriptors import CalcMolFormula
from rdkit.Chem import Descriptors
from ete3 import NCBITaxa
NCBI = NCBITaxa()
# Generic functions
def capitalize_first_letter(string):
capitalized_string = string[:1].upper()+string[1:]
return capitalized_string
def ref_nr_to_id(ref_nr):
base_id = "REF00000"
ref_id = re.sub("0{"+str(len(str(ref_nr)))+"}$",str(ref_nr),base_id)
return ref_id
def generate_id(last_id):
if not last_id:
new_id = "FT00001"
else:
last_nr = int(re.sub("FT0*","",last_id))
new_nr = last_nr + 1
new_id = "FT"+str(new_nr).zfill(5)
return new_id
def get_all_compound_names(dict):
all_names = []
main_name = dict["data"]["compound"]["main_name"]
alt_names = dict["data"]["compound"]["alt_names"]
all_names.append(main_name)
for name in alt_names:
if name:
all_names.append(name)
return all_names
# NCBI taxonomy
def name_to_taxid(name):
name_translator = NCBI.get_name_translator([name])
taxid = next(iter(name_translator.values()))[0] #report first taxid found
return taxid
def taxid_to_name(taxid):
taxid_translator = NCBI.get_taxid_translator([taxid])
name = next(iter(taxid_translator.values())) #report first name found
return name
def taxid_to_category(taxid):
lineage = NCBI.get_lineage(taxid)
if 2 in lineage:
category = "bacteria"
elif 4751 in lineage:
category = "fungi"
elif 33090 in lineage:
category = "plants"
else:
category = "other"
return category
# Manually added information
def line_to_FT_dict(line, id, fungi_without_taxid):
infinite_defaultdict = lambda: defaultdict(infinite_defaultdict)
d = infinite_defaultdict()
main_name, alt_names, organism_field, toxicity_field, notes, group = line.rstrip('\n').split('\t')
d["data"]["id"] = id
d["data"]["compound"]["main_name"] = capitalize_first_letter(main_name)
d["data"]["compound"]["alt_names"] = [capitalize_first_letter(name) for name in alt_names.split(', ')]
d["data"]["compound"]["family"] = group.capitalize()
d["data"]["notes"] = notes.split(',')
ref_dict_o = unpack_ref_field(organism_field)
ref_dict_t = unpack_ref_field(toxicity_field)
producer_list = write_producer_list(ref_dict_o, fungi_without_taxid)
toxicity_list = write_toxicity_list(ref_dict_t)
d["data"]["biosynthesis"]["producers"] = producer_list
d["data"]["toxicity"]["experimental_data"] = toxicity_list
return d
def unpack_ref_field(field):
chunks = re.findall("\D*[\d, ]+",field) #each chunk consists of one or multiple values coupled to one or multiple references
ref_dict = defaultdict(set)
for chunk in chunks:
values_str, refs_str = re.findall("\d+[\d, ]*|\D+", chunk) #split values and references
values = values_str.strip(" ,").split(',')
refs = refs_str.strip(" ,").split(',')
values = [value.strip() for value in values]
refs = [ref.strip() for ref in refs]
ref_ids = set()
for value in values:
#check that this contains only letters, hyphens, parentheses and dots. Spaces in between letters are allowed.
assert re.match("^[a-zA-Z-.\(\)]+( *[a-zA-Z-.\(\)]+)*$", value), "\""+value+"\" in field "+field+" is not a valid value"
for ref in refs:
#check that this contains only digits
assert re.match("^\d*$", ref), "\""+ref+"\" in field "+field+" is not a valid ref number"
#convert to ref id
ref_id = ref_nr_to_id(ref)
ref_ids.add(ref_id)
for value in values:
ref_dict[value] |= ref_ids
return ref_dict
def write_producer_list(ref_dict, fungi_without_taxid_list):
producer_list = []
for organism, ref_ids in ref_dict.items():
try:
taxid = name_to_taxid(organism)
organism_name = taxid_to_name(taxid) #Reconvert to name, as the name specified might not correspond to the 'official' name in NCBI Taxonomy
category = taxid_to_category(taxid)
except StopIteration:
print("No taxid found for \""+organism+"\".")
if fungi_without_taxid_list:
if organism in fungi_without_taxid_list:
print("Adding name without taxid")
taxid = "missing"
organism_name = organism
category = "fungi"
else:
sys.exit("Error: Name not in list of allowed names without taxid, aborting.")
else:
sys.exit("Error: No list of allowed names without taxid found. Please add file under --fungi_without_taxid.")
producer_dict = defaultdict(dict)
producer_dict["organism"]["organism_name"] = organism_name
producer_dict["organism"]["taxid"] = str(taxid)
producer_dict["organism"]["category"] = category
producer_dict["ref_id"] = list(ref_ids)
producer_list.append(producer_dict)
return producer_list
def write_toxicity_list(ref_dict):
toxicity_list = []
for tox_label, ref_ids in ref_dict.items():
tox_label = tox_label.lower()
tox_dict = {
"toxicity_type": tox_label,
"ref_id": list(ref_ids)
}
toxicity_list.append(tox_dict)
return toxicity_list
# Compound
## Retrieving chemical info from databases
def get_data_chembl(names, get_mol_info = True):
chembl_id = "missing"
mol_info = None
for name in names:
molecule = new_client.molecule
molecule_data = molecule.filter(pref_name__iexact=name)[0]
if not molecule_data:
molecule_data = molecule.filter(molecule_synonyms__molecule_synonym__iexact=name)[0]
if molecule_data:
chembl_id = molecule_data["molecule_chembl_id"]
if get_mol_info == True:
mol_info = {
"smiles": molecule_data["molecule_structures"]["canonical_smiles"],
"mol_formula": molecule_data["molecule_properties"]["full_molformula"],
"mw_avg": round(float(molecule_data["molecule_properties"]["full_mwt"]),2),
"mw_mono": round(float(molecule_data["molecule_properties"]["mw_monoisotopic"]),4)
}
break
return chembl_id, mol_info
def get_data_npatlas(names, get_mol_info = True):
npatlas_id = "missing"
mol_info = None
for name in names:
url = "https://www.npatlas.org/api/v1/compounds/advancedSearch"
query = {
"operator": "eq",
"attribute": "name",
"value": name
}
headers = {
"Content-Type": "application/json",
}
response = requests.post(url, data=json.dumps(query), headers=headers)
if response.json():
molecule_data = response.json()[0]
npatlas_id = molecule_data["npaid"]
if get_mol_info == True:
mol_info = {
"smiles": molecule_data["smiles"],
"mol_formula": molecule_data["mol_formula"],
"mw_avg": round(float(molecule_data["mol_weight"]),2),
"mw_mono": round(float(molecule_data["exact_mass"]),4)
}
break
return npatlas_id, mol_info
def get_data_chemspider(names, api_key, get_mol_info = True):
chemspider_id = "missing"
mol_info = None
cs = ChemSpider(api_key)
for name in names:
if len(name) >= 3:
query_id = cs.filter_name(name)
results = cs.filter_results(query_id)
if results:
chemspider_id = str(results[0])
if get_mol_info == True:
molecule = cs.get_compound(chemspider_id)
mol_info = {
"smiles": molecule.smiles,
"mol_formula": re.sub("[_/{/}]",'',molecule.molecular_formula),
"mw_avg": round(molecule.molecular_weight,2),
"mw_mono": round(molecule.monoisotopic_mass,4)
}
break
return chemspider_id, mol_info
def get_data_pubchem(names, get_mol_info = True):
pubchem_id = "missing"
mol_info = None
for name in names:
results = pcp.get_compounds(name, "name")
if results:
molecule_data = results[0].to_dict()
pubchem_id = str(molecule_data["cid"])
if get_mol_info == True:
mol_info = {
"smiles": molecule_data["canonical_smiles"],
"mol_formula": molecule_data["molecular_formula"],
"mw_avg": round(float(molecule_data["molecular_weight"]),2),
"mw_mono": round(float(molecule_data["monoisotopic_mass"]),4)
}
break
return pubchem_id, mol_info
def add_compound_data_dbs(FT_dict, compound_names, chemspider_api_key):
db_order = ["chembl","npatlas","chemspider","pubchem"]
get_mol_info = True
id_dict = {}
for database in db_order:
if database == "chemspider":
kwargs = {
"api_key": chemspider_api_key
}
else:
kwargs = {}
id, mol_info = globals()["get_data_" + database](compound_names, get_mol_info = get_mol_info, **kwargs)
if mol_info:
# If molecule info is found in the db, stop trying to retrieve this info from the next dbs
get_mol_info = False
FT_dict["data"]["compound"].update(mol_info)
# Always search for the database ID
id_dict[database] = id
FT_dict["data"]["compound"]["databases"] = id_dict
return FT_dict
## Adding chemical info based on manually annotated SMILES
def add_compound_data_manual(FT_dict, smiles_df):
main_name = FT_dict["data"]["compound"]["main_name"]
try:
smiles = smiles_df.loc[smiles_df["compound_name"].str.lower() == main_name.lower()].values[0][1]
except KeyError:
print("Error: No SMILES provided for compound \""+main_name+"\".")
molecule = Chem.MolFromSmiles(smiles)
mol_info = {
"smiles": smiles,
"mol_formula": CalcMolFormula(molecule),
"mw_avg": round(Descriptors.MolWt(molecule),2),
"mw_mono": round(Descriptors.ExactMolWt(molecule),4)
}
FT_dict["data"]["compound"].update(mol_info)
return FT_dict
# Biosynthesis
## Adding BGC info from MIBiG
def load_mibig(mibig_dir):
mibig_db = []
mibig_entries=glob.glob(mibig_dir+"/*.json")
for entry in mibig_entries:
with open(entry, 'r') as json_file:
bgc_json = json.load(json_file)
mibig_db.append(bgc_json)
return mibig_db
def add_gene_clusters(FT_dict, names, mibig_db):
bgc_list = []
for name in names:
for bgc_json in mibig_db:
compounds = []
for compound_entry in bgc_json["cluster"]["compounds"]:
compound_name = compound_entry["compound"]
compounds.append(compound_name)
if name.casefold() in [compound.casefold() for compound in compounds]:
taxid = bgc_json["cluster"]["ncbi_tax_id"]
organism_name = taxid_to_name(taxid) #get organism name from taxid, as it may have changed since the MIBiG entry was created
organism_category = taxid_to_category(taxid)
bgc_dict = defaultdict(dict)
bgc_dict["mibig_id"] = bgc_json["cluster"]["mibig_accession"]
bgc_dict["mibig_class"] = bgc_json["cluster"]["biosyn_class"]
bgc_dict["organism"]["organism_name"] = organism_name
bgc_dict["organism"]["taxid"] = taxid
bgc_dict["organism"]["category"] = organism_category
bgc_dict["compounds"] = compounds
bgc_dict["status"] = bgc_json["cluster"]["status"]
bgc_dict["completeness"] = bgc_json["cluster"]["loci"]["completeness"].lower()
bgc_dict["minimal"] = bgc_json["cluster"]["minimal"]
bgc_list.append(bgc_dict)
FT_dict["data"]["biosynthesis"]["gene_clusters"] = bgc_list
return FT_dict
# Toxicity
## Adding carcinogenicity classifications by the International Agency for Research on Cancer (IARC)
def load_IARC_file(IARC_file):
IARC_df = pd.read_csv(IARC_file, sep='\t', header=0, encoding='utf-16')
IARC_df = IARC_df.astype(str)
return IARC_df
def retrieve_IARC_classification(IARC_df, names):
for name in names:
#Search for occurrence of entire compound name (cannot be a substring within a word)
pattern = "(?<!\w)" + re.escape(name) + "(?!\w)"
classification = IARC_df[IARC_df["Agent"].str.contains(pattern, case=False, na=False)]
if not classification.empty:
break
#Find base name of the compound e.g. Fumonisin for Fumonisin B2
try:
basename = re.match(".*(?= [A-Z])", name).group()
#Search for occurrence of base name (cannot be preceeded by any letters or followed by a suffix specifying the type (e.g. A, B2, III))
pattern = "(?<!\w)" + re.escape(basename) + "(?!( [A-Z]+(0-9)*))"
classification = IARC_df[IARC_df["Agent"].str.match(pattern, case=False, na=False)]
if not classification.empty:
break
except AttributeError:
continue
return classification
def extract_properties(classification):
classification = classification.drop(["CAS No.", "Additional information"], axis=1)
classification = classification.replace("nan", "missing")
nr_nans = classification.eq("missing").sum(axis=1).tolist()
best_row = classification.values[nr_nans.index(min(nr_nans))]
agent, group, volume, vol_year, eval_year = best_row
return agent, group, volume, vol_year, eval_year
def add_carcinogenicity_data(FT_dict, names, IARC_df):
classification = retrieve_IARC_classification(IARC_df, names)
if not classification.empty:
agent, group, volume, vol_year, eval_year = extract_properties(classification)
IARC_dict = {
"IARC_group": group,
"agent": agent,
"volume": volume,
"vol_year": vol_year,
"eval_year": str(eval_year)
}
else:
IARC_dict = {
"IARC_group": "missing",
"agent": "missing",
"volume": "missing",
"vol_year": "missing",
"eval_year": "missing"
}
FT_dict["data"]["toxicity"]["carcinogenicity"] = IARC_dict
return FT_dict
## Adding links to toxicity databases
def add_comptox_id(FT_dict, names, api_key):
comptox_id = "missing"
for name in names:
url = "https://api-ccte.epa.gov/chemical/search/equal/"+name
headers = {
'accept': 'application/json',
'x-api-key': api_key
}
response=requests.get(url, headers=headers)
if not response.status_code == 400:
molecule_data = response.json()[0]
comptox_id = molecule_data["dtxcid"]
if not comptox_id:
comptox_id = molecule_data["dtxsid"]
break
FT_dict["data"]["toxicity"]["databases"]["comptox"] = comptox_id
return FT_dict
def add_ctd_id(FT_dict, names):
ctd_id = "missing"
for name in names:
url = "https://id.nlm.nih.gov/mesh/lookup/descriptor?label="+name+"&match=exact"
headers = {
'accept': 'application/json',
}
response=requests.get(url, headers=headers)
if response.json():
mesh_data = response.json()[0]
ctd_id = mesh_data["resource"].rsplit('/', 1)[-1]
break
FT_dict["data"]["toxicity"]["databases"]["ctd"] = ctd_id
return FT_dict