-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_hmdb_metabolites_table_goodmod.py
211 lines (159 loc) · 8.96 KB
/
make_hmdb_metabolites_table_goodmod.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
# Adam Berman
# Princeton University
# 5 May 2017
import os
import xmltodict
import xml.etree.ElementTree as ET
import itertools
# Parse metabolites
all_metabolites = []
# Iterate over all metabolites
# Folder containing all metabolite's xml files (named "hmdb_metabolites") can be found
# at the following link: http://www.hmdb.ca/system/downloads/current/hmdb_metabolites.zip
for filename in os.listdir('/Users/adamberman/Independent_Work_v2/hmdb_metabolites'):
if filename.startswith('HMDB'):
# Parse the metabolite
root = ET.parse('/Users/adamberman/Independent_Work_v2/hmdb_metabolites/' + filename).getroot()
origin = root.find('ontology').find('origins').find('origin')
# Keep only endogenous metabolites
if (origin != None) and (origin.text == 'Endogenous'):
# Collect all genes associated with the given metabolite
gene_names = []
for protein in root.find('protein_associations').findall('protein'):
gene_name = protein.find('gene_name').text
if gene_name != None:
gene_names.append(gene_name.strip())
# Only continue if the metabolite has at least three associated genes
#if (len(gene_names) >= 3):
# Only continue if the metabolite has at least one associated gene:
if gene_names:
# Collect the name and KEGG ID of the metabolite
name = root.find('name').text.strip()
kegg = root.find('kegg_id').text
if kegg == None:
kegg = "None"
else:
kegg = kegg.strip()
# Collect the synonyms of the metabolite
synonyms = []
for synonym in root.find('synonyms').findall('synonym'):
synonyms.append(synonym.text.strip())
# Collect the SMILES identifier of the metabolite
smiles = root.find('smiles').text
if smiles != None:
smiles = smiles.strip()
# InChI instead of SMILES
#if smiles == None:
inchi = root.find('inchi').text
if inchi != None:
inchi = inchi.strip()
inchi_key = root.find('inchikey').text
if inchi_key != None:
inchi_key = inchi_key.strip()
# Add new metabolite to all_metabolites
metabolite = {'name': name, 'synonyms': synonyms, 'kegg': kegg, 'gene_names': gene_names, 'smiles': smiles, 'inchi': inchi, 'inchi_key': inchi_key, 'filename': filename}
all_metabolites.append(metabolite)
print 'Number of endogenous metabolites: '
print len(all_metabolites)
#20,440 out of 61,388 metabolites that are endogenous and have at least one associated gene
#'''
# Simple test version of all_metabolites
'''
all_metabolites = [{'name': 'dog', 'synonyms': ['canine', 'pupper', 'poochy'], 'kegg': 'kegg1', 'gene_names': ['1AA', 'B1B'], 'smiles': 'IOU1K9_1', 'inchi': 'inchi1', 'inchi_key': 'inchi_key1', 'filename': 'Dogger_1.xml'},
{'name': 'hound', 'synonyms': ['dog', 'pupper', 'doggo'], 'kegg': 'kegg2', 'gene_names': ['B2B', 'BBB'], 'smiles': 'IOU1K9_2', 'inchi': 'inchi1', 'inchi_key': 'inchi_key2', 'filename': 'Dogger_2.xml'},
{'name': 'houndy', 'synonyms': ['new', 'newer', 'newest'], 'kegg': 'kegg3', 'gene_names': ['EEE', 'FFF'], 'smiles': 'IOU1K9_3', 'inchi': 'inchi3', 'inchi_key': 'inchi_key3', 'filename': 'Dogger_3.xml'},
{'name': 'New', 'synonyms': ['Title', 'hound', 'poochy'], 'kegg': 'kegg4', 'gene_names': ['GGG', 'HHH'], 'smiles': None, 'inchi': 'inchi4', 'inchi_key': 'inchi_key4', 'filename': 'Dogger_4.xml'},
{'name': 'doggy', 'synonyms': ['canine', 'pupper', 'poochy'], 'kegg': 'kegg1', 'gene_names': ['YYY', 'ZZZ'], 'smiles': 'IOU1K9_1', 'inchi': 'inchi10', 'inchi_key': 'inchi_key1', 'filename': 'Dogger_1.xml'}]
'''
# Remove synonymous metabolites from all_metabolites
smiles_dictionary = {}
for metabolite in all_metabolites:
genes = metabolite.get('gene_names')
smiles = metabolite.get('smiles')
kgg = metabolite.get('kegg')
syn = metabolite.get('synonyms')
name = metabolite.get('name')
inchi = metabolite.get('inchi')
inchi_key = metabolite.get('inchi_key')
name_and_syn = syn + [name]
merged = False
for key, value in smiles_dictionary.iteritems():
# Merge metabolite with existing smiles_dictionary entry if both metabolites have the same set of associated genes
if (set(genes) == set(value.get('gene_names'))):
# Perform merge operation
new_syn = list(set(name_and_syn + value.get('synonyms')))
if key in new_syn:
new_syn.remove(key)
new_gn = list(set(metabolite.get('gene_names') + value.get('gene_names')))
new_gn.sort()
# Create new entry and store it into the dictionary
new_value = {'synonyms': new_syn, 'kegg': value.get('kegg'), 'gene_names': new_gn,
'smiles': value.get('smiles'), 'inchi': value.get('inchi'),
'inchi_key': value.get('inchi_key'), 'filename': value.get('filename')}
smiles_dictionary[key] = new_value
# Denote that merge occured
merged = True
break
# If the metabolite was not merged, create new smiles_dictionary entry for metabolite
if not merged:
smiles_dictionary[name] = {'synonyms': syn, 'kegg': metabolite.get('kegg'),
'gene_names': metabolite.get('gene_names'),
'smiles': metabolite.get('smiles'),
'inchi': metabolite.get('inchi'),
'inchi_key': metabolite.get('inchi_key'),
'filename': metabolite.get('filename')}
print 'Number of endogenous metabolites after merging synonyms: '
print len(smiles_dictionary)
# Print SMILES metabolites into table
print 'Metabolite' + '\t' + 'SMILES' + '\t' + 'KEGG' + '\t' + 'Gene Name'
for key, value in smiles_dictionary.iteritems():
n = key
if n == None:
n = 'None'
s = value.get('smiles')
if s == None:
s = 'None'
k = value.get('kegg')
if k == None:
k = 'None'
gns = value.get('gene_names')
if gns == None:
gns = 'None'
for gn in gns:
print n + '\t' + s + '\t' + k + '\t' + gn
# TEST CODE
'''
# Simple test version of all_metabolites
all_metabolites = [{'name': 'dog', 'synonyms': ['canine', 'pupper', 'poochy'], 'gene_names': ['1AA', 'B1B'], 'smiles': 'IOU1K9_1', 'filename': 'Dogger_1.xml'},
{'name': 'hound', 'synonyms': ['dog', 'pupper', 'doggo'], 'gene_names': ['B2B', 'BBB'], 'smiles': 'IOU1K9_2', 'filename': 'Dogger_2.xml'},
{'name': 'hound', 'synonyms': ['new', 'newer', 'newest'], 'gene_names': ['EEE', 'FFF'], 'smiles': 'IOU1K9_3', 'filename': 'Dogger_3.xml'},
{'name': 'New', 'synonyms': ['Title', 'hound', 'poochy'], 'gene_names': ['GGG', 'HHH'], 'smiles': 'IOU1K9_4', 'filename': 'Dogger_4.xml'}]
'''
'''
# Simple test version of all_inchi_metabolites
all_inchi_metabolites = [{'name': 'dog', 'synonyms': ['canine', 'pupper', 'poochy'], 'gene_names': ['1AA', 'B1B'], 'inchi': 'inchi_1', 'inchi_key': 'inchi_key_1', 'filename': 'Dogger_1.xml'},
{'name': 'hound', 'synonyms': ['dog', 'pupper', 'doggo'], 'gene_names': ['B2B', 'BBB'], 'inchi': 'inchi_2', 'inchi_key': 'inchi_key_2', 'filename': 'Dogger_2.xml'},
{'name': 'hound', 'synonyms': ['new', 'newer', 'newest'], 'gene_names': ['EEE', 'FFF'], 'inchi': 'inchi_3', 'inchi_key': 'inchi_key_3', 'filename': 'Dogger_3.xml'},
{'name': 'New', 'synonyms': ['Title', 'hound', 'poochy'], 'gene_names': ['GGG', 'HHH'], 'inchi': 'inchi_4', 'inchi_key': 'inchi_key_4', 'filename': 'Dogger_4.xml'}]
'''
'''
all_metabolites = [{'name': 'dog', 'synonyms': ['canine', 'pupper', 'poochy'], 'kegg': 'dog_kegg', 'gene_names': ['1AA', 'B1B'], 'smiles': 'IOU1K9_1', 'filename': 'Dogger_1.xml'},
{'name': 'hound', 'synonyms': ['dog', 'pupper', 'doggo'], 'kegg': 'hound_kegg', 'gene_names': ['B2B', 'BBB'], 'smiles': 'IOU1K9_2', 'filename': 'Dogger_2.xml'},
{'name': 'Title', 'synonyms': ['new', 'newer', 'newest'], 'kegg': 'Title_kegg', 'gene_names': ['EEE', 'FFF'], 'smiles': 'IOU1K9_3', 'filename': 'Dogger_3.xml'},
{'name': 'New', 'synonyms': ['Title', 'hound', 'poochy'], 'kegg': 'New_kegg', 'gene_names': ['GGG', 'HHH'], 'smiles': 'IOU1K9_4', 'filename': 'Dogger_4.xml'}]
'''
'''
all_metabolites = [{'name': 'test1', 'synonyms': ['a', 'b', 'c'], 'kegg': 'same_kegg', 'gene_names': ['1AA', 'B1B'], 'smiles': 'IOU1K9_1', 'filename': 'Dogger_1.xml'},
{'name': 'test2', 'synonyms': ['d', 'e', 'f'], 'kegg': 'diff_kegg', 'gene_names': ['B2B', 'BBB'], 'smiles': 'IOU1K9_2', 'filename': 'Dogger_2.xml'},
{'name': 'test3', 'synonyms': ['g', 'h', 'i'], 'kegg': 'same_kegg', 'gene_names': ['EEE', 'FFF'], 'smiles': 'IOU1K9_3', 'filename': 'Dogger_3.xml'},
{'name': 'test4', 'synonyms': ['j', 'k', 'l'], 'kegg': 'same_kegg', 'gene_names': ['GGG', 'HHH'], 'smiles': 'IOU1K9_4', 'filename': 'Dogger_4.xml'}]
'''
# GRAVEYARD
# Merge metabolite with existing smiles_dictionary entry if both metabolites have the same set of associated genes, or either their SMILES or INCHI strings are the same
#if (set(genes) == set(value.get('gene_names'))) or (((inchi != None) and (inchi == value.get('inchi'))) or ((smiles != None) and (smiles == value.get('smiles')))):
#if ((inchi == value.get('inchi')) or (smiles == value.get('smiles'))):
#if ((key in name_and_syn)
# or (not set(value.get('synonyms')).isdisjoint(name_and_syn))
# or ((kgg != "None") and (kgg == value.get('kegg')))
# or (smiles == value.get('smiles'))):
#print '{0:40s} {1:500s} {2:30s}'.format(n, s, gn)