-
Notifications
You must be signed in to change notification settings - Fork 2
/
decompose_groups.py
251 lines (208 loc) · 8.01 KB
/
decompose_groups.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
import pandas as pd
import pdb
import json
from rdkit import Chem
def count_substructures(radius,molecule):
"""Helper function for get the information of molecular signature of a
metabolite. The relaxed signature requires the number of each substructure
to construct a matrix for each molecule.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
molecule : Molecule
a molecule object create by RDkit (e.g. Chem.MolFromInchi(inchi_code)
or Chem.MolToSmiles(smiles_code))
Returns
-------
dict
dictionary of molecular signature for a molecule,
{smiles: molecular_signature}
"""
m = molecule
smi_count = dict()
atomList = [atom for atom in m.GetAtoms()]
for i in range(len(atomList)):
env = Chem.FindAtomEnvironmentOfRadiusN(m,radius,i)
atoms=set()
for bidx in env:
atoms.add(m.GetBondWithIdx(bidx).GetBeginAtomIdx())
atoms.add(m.GetBondWithIdx(bidx).GetEndAtomIdx())
# only one atom is in this environment, such as O in H2O
if len(atoms) == 0:
atoms = {i}
smi = Chem.MolFragmentToSmiles(m,atomsToUse=list(atoms),
bondsToUse=env,canonical=True)
if smi in smi_count:
smi_count[smi] = smi_count[smi] + 1
else:
smi_count[smi] = 1
return smi_count
def decompse_ac(db_smiles,radius=1):
non_decomposable = []
decompose_vector = dict()
for cid in db_smiles:
# print cid
smiles_pH7 = db_smiles[cid]
try:
mol = Chem.MolFromSmiles(smiles_pH7)
mol = Chem.RemoveHs(mol)
# Chem.RemoveStereochemistry(mol)
smi_count = count_substructures(radius,mol)
decompose_vector[cid] = smi_count
except Exception as e:
non_decomposable.append(cid)
with open('./data/decompose_vector_ac.json','w') as fp:
json.dump(decompose_vector,fp)
def get_rxn_rule():
"""calculate reaction rules based on the relaxed molecular signatures.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
Returns
-------
None
All of the reaction rules are saved in files (csv file)
"""
reaction_dict = json.load(open('./data/optstoic_v3_Sji_dict.json'))
molecular_signature = json.load(open('./data/decompose_vector_ac.json'))
molsigna_df = pd.DataFrame.from_dict(molecular_signature).fillna(0)
all_mets = molsigna_df.columns.tolist()
all_mets.append("C00080")
all_mets.append("C00282")
rule_df = pd.DataFrame(index=molsigna_df.index)
for rid, value in list(reaction_dict.items()):
# skip the reactions with missing metabolites
mets = list(value.keys())
flag = False
for met in mets:
if met not in all_mets:
flag = True
break
if flag: continue
rule_df[rid] = 0
for met, stoic in list(value.items()):
if met == "C00080" or met == "C00282":
continue # hydogen is zero
rule_df[rid] += molsigna_df[met] * stoic
rule_df.to_csv("./data/reaction_rule.csv", index=True)
def get_rxn_rule_no_stero():
"""calculate reaction rules based on the relaxed molecular signatures.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
Returns
-------
None
All of the reaction rules are saved in files (csv file)
"""
reaction_dict = json.load(open('./data/optstoic_v3_Sji_dict.json'))
molecular_signature = json.load(open('./data/decompose_vector_ac_nostereo.json'))
molsigna_df = pd.DataFrame.from_dict(molecular_signature).fillna(0)
all_mets = molsigna_df.columns.tolist()
all_mets.append("C00080")
all_mets.append("C00282")
rule_df = pd.DataFrame(index=molsigna_df.index)
for rid, value in list(reaction_dict.items()):
# skip the reactions with missing metabolites
mets = list(value.keys())
flag = False
for met in mets:
if met not in all_mets:
flag = True
break
if flag: continue
rule_df[rid] = 0
for met, stoic in list(value.items()):
if met == "C00080" or met == "C00282":
continue # hydogen is zero
rule_df[rid] += molsigna_df[met] * stoic
rule_df.to_csv("./data/reaction_rule_no_stero.csv", index=True)
def get_rxn_rule_remove_TECRDB_mets():
"""calculate reaction rules based on the relaxed molecular signatures.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
Returns
-------
None
All of the reaction rules are saved in files (csv file)
"""
reaction_dict = json.load(open('./data/optstoic_v3_Sji_dict.json'))
molecular_signature = json.load(open('./data/decompose_vector_ac.json'))
molsigna_df = pd.DataFrame.from_dict(molecular_signature).fillna(0)
all_mets = molsigna_df.columns.tolist()
all_mets.append("C00080")
all_mets.append("C00282")
mets_TECRDB_df = pd.read_csv('./data/TECRBD_mets.txt',header=None)
mets_TECRDB = mets_TECRDB_df[0].tolist()
# pdb.set_trace()
all_mets = list(set(all_mets + mets_TECRDB))
rule_df = pd.DataFrame(index=molsigna_df.index)
for rid, value in list(reaction_dict.items()):
# skip the reactions with missing metabolites
mets = list(value.keys())
flag = False
for met in mets:
if met not in all_mets:
flag = True
break
if flag: continue
rule_df[rid] = 0
for met, stoic in list(value.items()):
if met in mets_TECRDB:
continue # hydogen is zero
rule_df[rid] += molsigna_df[met] * stoic
rule_df.to_csv("./data/reaction_rule_remove_TECRDB_mets.csv", index=True)
def get_rxn_rule_no_stero_remove_TECRDB_mets():
"""calculate reaction rules based on the relaxed molecular signatures.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
Returns
-------
None
All of the reaction rules are saved in files (csv file)
"""
reaction_dict = json.load(open('./data/optstoic_v3_Sji_dict.json'))
molecular_signature = json.load(open('./data/decompose_vector_ac_nostereo.json'))
molsigna_df = pd.DataFrame.from_dict(molecular_signature).fillna(0)
all_mets = molsigna_df.columns.tolist()
all_mets.append("C00080")
all_mets.append("C00282")
mets_TECRDB_df = pd.read_csv('./data/TECRBD_mets.txt',header=None)
mets_TECRDB = mets_TECRDB_df[0].tolist()
# pdb.set_trace()
all_mets = list(set(all_mets + mets_TECRDB))
rule_df = pd.DataFrame(index=molsigna_df.index)
for rid, value in list(reaction_dict.items()):
# skip the reactions with missing metabolites
mets = list(value.keys())
flag = False
for met in mets:
if met not in all_mets:
flag = True
break
if flag: continue
rule_df[rid] = 0
for met, stoic in list(value.items()):
if met in mets_TECRDB:
continue # hydogen is zero
rule_df[rid] += molsigna_df[met] * stoic
rule_df.to_csv("./data/reaction_rule_nostereo_remove_TECRDB_mets.csv", index=True)
if __name__ == '__main__':
# db = pd.read_csv('./data/cache_compounds_20160818.csv',index_col='compound_id')
# db_smiles = db['smiles_pH7'].to_dict()
# decompse_ac(db_smiles)
# get_rxn_rule()
# get_rxn_rule_remove_TECRDB_mets()
get_rxn_rule_no_stero_remove_TECRDB_mets()