1
1
import cobra
2
2
import pandas as pd
3
3
from pathlib import Path
4
+ import libchebipy
4
5
5
6
def map_model_metabolites (model , metanetx_fn ):
6
7
df = pd .read_csv (metanetx_fn , header = None , sep = "\t " , comment = "#" )
@@ -59,9 +60,82 @@ def map_model_metabolites(model, metanetx_fn):
59
60
# print(new_df_list)
60
61
61
62
new_df = pd .DataFrame (new_df_list , columns = ["Met ID" , "MNX 1" , "MNX 2" ])
62
- new_df .to_csv ("metanetx_to_change.csv" , index_label = "index" )
63
+ new_df .to_csv ("../../ComplementaryData/curation/ metanetx_to_change.csv" , index_label = "index" )
63
64
# print(new_df)
64
65
66
+ def map_metabolites_to_chebi (scoGEM , metanetx_fn ):
67
+ df = pd .read_csv (metanetx_fn , header = None , sep = "\t " , comment = "#" )
68
+ df .columns = ["db:id" , "metanetx" , "reason" , "name" ]
69
+
70
+ chebi_df = df [df ["db:id" ].str .contains ("chebi:" )]
71
+ del df
72
+
73
+ new_df_list = []
74
+ for m in model .metabolites :
75
+ try :
76
+ mnx_annot = m .annotation ["metanetx.chemical" ]
77
+ except KeyError :
78
+ print ("No metanetx annotation for {0}, {1}" .format (m .id , ["{0}:{1}" .format (key , value ) for key , value in m .annotation .items ()]))
79
+ continue
80
+
81
+ mnx_annot = as_list (mnx_annot )
82
+
83
+ chebi_ids = []
84
+ for mnx_i in mnx_annot :
85
+ mnx_match = chebi_df .loc [chebi_df ["metanetx" ] == mnx_i ]
86
+ chebi_ids += list (mnx_match ["db:id" ].values )
87
+ chebi_ids = [x .upper () for x in chebi_ids ]
88
+
89
+ parent_chebis = []
90
+ for chebi_id in list (set (chebi_ids )):
91
+ lib_data = libchebipy .ChebiEntity (chebi_id .upper ())
92
+ parent = lib_data .get_parent_id ()
93
+ if parent :
94
+ parent_chebis .append (parent )
95
+ else :
96
+ parent_chebis .append (chebi_id .upper ())
97
+ parent_chebis = list (set (parent_chebis ))
98
+ try :
99
+ current_chebi_list = as_list (m .annotation ["chebi" ])
100
+ except :
101
+ current_chebi_list = [None ]
102
+ in_new_chebis = False
103
+ else :
104
+ in_new_chebis = True
105
+ for current_chebi in current_chebi_list :
106
+ if not current_chebi in chebi_ids :
107
+ in_new_chebis = False
108
+ print ("{2}: {0} is not in the new set {1}" .format (current_chebi , parent_chebis , m .id ))
109
+
110
+ new_df_list .append ([m .id , parent_chebis , current_chebi_list , in_new_chebis ])
111
+ new_df = pd .DataFrame (new_df_list , columns = ["Met ID" , "New chebi annotation" , "Current chebi annotation" , "Old chebi in new (including secondary chebis)" ])
112
+ new_df .to_csv ("../../ComplementaryData/curation/chebi_annotation.csv" , index = False )
113
+
114
+ def as_list (param ):
115
+ if isinstance (param , list ):
116
+ return param
117
+ else :
118
+ return [param ]
119
+
120
+
121
+ # chebi_dict[m.id] = set(chebi_ids)
122
+ # all_chebis += chebi_ids
123
+
124
+ # # Remove duplicates
125
+ # all_chebis = list(set(all_chebis))
126
+ # parent_child_dict = {}
127
+ # for chebi_id in all_chebis:
128
+ # lib_data = libchebipy.ChebiEntity(chebi_id.upper())
129
+ # print(chebi_id.upper(), lib_data)
130
+ # parent = lib_data.get_parent_id()
131
+ # if parent is not None:
132
+ # parent_child_dict[chebi_id.upper()] = parent
133
+
134
+
135
+ # Use libchebipy to find parent IDs
136
+ # a = libchebipy.ChebiEntity
137
+ # a.get_parent_id()
138
+ # a.
65
139
def apply_metanetx_mapping (scoGEM , met_to_metanetx_fn ):
66
140
"""
67
141
Depreceated: moved to fix_issue33_annotation_bugs.py
@@ -86,11 +160,17 @@ def apply_metanetx_mapping(scoGEM, met_to_metanetx_fn):
86
160
m .id , old_anno , m .annotation ["metanetx.chemical" ]))
87
161
88
162
163
+
164
+
89
165
if __name__ == '__main__' :
90
166
repo_path = Path (__file__ ).parent .parent .parent
91
167
metanetx_fn = repo_path / "ComplementaryData" / "curation" / "metanetx_chem_xref.tsv"
92
168
model_fn = repo_path / "ModelFiles" / "xml" / "scoGEM.xml"
93
169
model = cobra .io .read_sbml_model (str (model_fn ))
94
170
# map_model_metabolites(model, metanetx_fn)
95
171
fn = repo_path / "ComplementaryData" / "curation" / "metanetx_to_change.csv"
96
- apply_metanetx_mapping (model , fn )
172
+ if 0 :
173
+ apply_metanetx_mapping (model , fn )
174
+
175
+ if 1 :
176
+ map_metabolites_to_chebi (model , metanetx_fn )
0 commit comments