Mercurial > repos > iuc > gem_flux_variability_analysis
comparison gem_escher_visualization.py @ 0:dfeabe31d865 draft default tip
planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
author | iuc |
---|---|
date | Fri, 13 Dec 2024 21:34:04 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:dfeabe31d865 |
---|---|
1 import argparse | |
2 | |
3 import cobra | |
4 import pandas as pd | |
5 from escher import Builder | |
6 | |
7 | |
8 def __main__(): | |
9 parser = argparse.ArgumentParser( | |
10 prog="EscherVisualization", | |
11 description="This program visualizes an Escher map", | |
12 epilog="Adding an epilog, but doubt it's needed.", | |
13 ) | |
14 parser.add_argument( | |
15 "-m", | |
16 "--cb_model_location", | |
17 dest="cb_model_location", | |
18 action="store", | |
19 type=str, | |
20 default=None, | |
21 required=False, | |
22 help="The model to use." | |
23 ) | |
24 parser.add_argument( | |
25 "-f", | |
26 "--flux_distribution_location", | |
27 dest="flux_distribution_location", | |
28 action="store", | |
29 type=str, | |
30 default=None, | |
31 required=False, | |
32 help="The flux distribution to visualize." | |
33 ) | |
34 parser.add_argument( | |
35 "-e", | |
36 "--expect_map", | |
37 dest="expect_map", | |
38 action="store", | |
39 type=str, | |
40 default=None, | |
41 required=True, | |
42 help="Is a map expected to be uploaded?" | |
43 ) | |
44 parser.add_argument( | |
45 "-l", | |
46 "--model_to_download", | |
47 dest="model_to_download", | |
48 action="store", | |
49 type=str, | |
50 default=None, | |
51 required=False, | |
52 help="The model to download." | |
53 ) | |
54 parser.add_argument( | |
55 "--map_load_name", | |
56 dest="map_load_name", | |
57 action="store", | |
58 type=str, | |
59 default=None, | |
60 required=False, | |
61 help="The name of the map to use." | |
62 ) | |
63 parser.add_argument( | |
64 "--map_upload_name", | |
65 dest="map_upload_name", | |
66 action="store", | |
67 type=str, | |
68 default=None, | |
69 required=False, | |
70 help="The name of the map to use." | |
71 ) | |
72 parser.add_argument( | |
73 "-u", | |
74 "--uptake_constraints_file", | |
75 dest="uptake_constraints_file", | |
76 action="store", | |
77 type=str, | |
78 default=None, | |
79 required=False, | |
80 help="File containing new uptake constraits." | |
81 ) | |
82 parser.add_argument( | |
83 "-output", | |
84 "--output", | |
85 dest="out_file", | |
86 action="store", | |
87 type=str, | |
88 default=None, | |
89 required=True, | |
90 help="The output file." | |
91 ) | |
92 | |
93 args = parser.parse_args() | |
94 | |
95 if args.expect_map not in ["True", "False"]: | |
96 raise Exception("The expect_map argument must be either True or False.") | |
97 if args.expect_map == "True" and args.map_load_name is None and \ | |
98 args.map_upload_name is None: | |
99 raise Exception( | |
100 "You must specify a map name if a map is expected to be uploaded." | |
101 ) | |
102 | |
103 cb_model = None | |
104 model_name = None | |
105 if args.model_to_download is not None and args.model_to_download != "None": | |
106 if args.cb_model_location is not None \ | |
107 and args.cb_model_location != "None": | |
108 raise Exception( | |
109 "You cannot specify both a model to " | |
110 "download and a model to use." | |
111 ) | |
112 model_name = args.model_to_download | |
113 elif args.cb_model_location is not None\ | |
114 and args.cb_model_location != "None": | |
115 try: | |
116 cb_model = cobra.io.read_sbml_model(args.cb_model_location) | |
117 except Exception as e: | |
118 raise Exception( | |
119 "The model could not be read. " | |
120 "Ensure it is in correct SBML format." | |
121 ) from e | |
122 | |
123 map_name = None | |
124 map_location = None | |
125 if args.map_upload_name is not None and args.map_upload_name != "None": | |
126 if args.map_load_name is not None and args.map_load_name != "None": | |
127 raise Exception( | |
128 "You cannot specify both a map to upload and a map to load." | |
129 ) | |
130 map_location = args.map_upload_name | |
131 elif args.map_load_name is not None and args.map_load_name != "None": | |
132 map_name = args.map_load_name | |
133 | |
134 if args.uptake_constraints_file is not None and \ | |
135 args.uptake_constraints_file != "None": | |
136 if cb_model is None: | |
137 raise Exception( | |
138 "You cannot specify uptake constraints " | |
139 "without uploading a model." | |
140 ) | |
141 else: | |
142 constraints_df = pd.read_csv( | |
143 args.uptake_constraints_file, | |
144 sep=";", | |
145 header=0, | |
146 index_col=False | |
147 ) | |
148 for index, row in constraints_df.iterrows(): | |
149 rxn_id = row["reaction_id"] | |
150 cb_model.reactions.get_by_id(rxn_id).lower_bound = \ | |
151 row["lower_bound"] | |
152 cb_model.reactions.get_by_id(rxn_id).upper_bound = \ | |
153 row["upper_bound"] | |
154 | |
155 flux_dict = None | |
156 if args.flux_distribution_location is not None and \ | |
157 args.flux_distribution_location != "None": | |
158 if cb_model is None: | |
159 raise Exception( | |
160 "You cannot specify a flux distribution " | |
161 "without uploading a model." | |
162 ) | |
163 if args.uptake_constraints_file is not None and \ | |
164 args.uptake_constraints_file != "None": | |
165 raise Exception( | |
166 "You cannot specify both uptake constraints and a flux " | |
167 "distribution." | |
168 ) | |
169 try: | |
170 flux_df = pd.read_csv( | |
171 args.flux_distribution_location, | |
172 sep=";", | |
173 header=0, | |
174 index_col=False | |
175 ) | |
176 flux_dict = { | |
177 key: value for key, value in zip( | |
178 flux_df['reaction_name'], | |
179 flux_df['flux'] | |
180 ) | |
181 } | |
182 except Exception as e: | |
183 raise Exception( | |
184 "The flux distribution file could not be read. " | |
185 "Ensure the file has semicolon-separated " | |
186 "columns and a header row." | |
187 ) from e | |
188 | |
189 if cb_model is not None and flux_dict is None: | |
190 solution = cobra.flux_analysis.pfba(cb_model) | |
191 | |
192 # make a dataframe with the reaction names, reaction ids, and flux | |
193 flux_distribution = pd.DataFrame( | |
194 columns=["reaction_name", "reaction_id", "flux"] | |
195 ) | |
196 flux_distribution["reaction_name"] = [ | |
197 reaction.name for reaction in cb_model.reactions | |
198 ] | |
199 flux_distribution["reaction_id"] = [ | |
200 reaction.id for reaction in cb_model.reactions | |
201 ] | |
202 flux_distribution["flux"] = [ | |
203 solution.fluxes[reaction.id] for reaction in cb_model.reactions | |
204 ] | |
205 flux_dict = { | |
206 key: value for key, value in zip( | |
207 flux_distribution['reaction_name'], | |
208 flux_distribution['flux'] | |
209 ) | |
210 } | |
211 | |
212 builder = Builder() | |
213 if map_name is not None: | |
214 builder.map_name = map_name | |
215 print("Downloading map...") | |
216 if map_location is not None: | |
217 builder.map_json = map_location | |
218 print("Uploading map...") | |
219 if model_name is not None: | |
220 builder.model_name = model_name | |
221 print("Downloading model...") | |
222 if cb_model is not None: | |
223 builder.model = cb_model | |
224 print("Uploading model...") | |
225 | |
226 if flux_dict is not None: | |
227 builder.reaction_data = flux_dict | |
228 | |
229 builder.save_html(args.out_file) | |
230 | |
231 | |
232 if __name__ == "__main__": | |
233 __main__() |