Mercurial > repos > iuc > gem_escher_visualization
comparison gem_flux_distribution.py @ 0:b79cf44068bc 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:32:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b79cf44068bc |
---|---|
1 import argparse | |
2 | |
3 import cobra | |
4 import pandas as pd | |
5 | |
6 | |
7 def __main__(): | |
8 parser = argparse.ArgumentParser( | |
9 prog="FluxDistribution", | |
10 description="This program calculates the flux distribution of a GEM", | |
11 epilog="Adding an epilog, but doubt it's needed.", | |
12 ) | |
13 parser.add_argument( | |
14 "-m", | |
15 "--cb_model_location", | |
16 dest="cb_model_location", | |
17 action="store", | |
18 type=str, | |
19 default=None, | |
20 required=True, | |
21 help="The model to use." | |
22 ) | |
23 parser.add_argument( | |
24 "-output", | |
25 "--output", | |
26 dest="out_file", | |
27 action="store", | |
28 type=str, | |
29 default=None, | |
30 required=True, | |
31 help="The output file." | |
32 ) | |
33 parser.add_argument( | |
34 "-u", | |
35 "--uptake_constraints_file", | |
36 dest="uptake_constraints_file", | |
37 action="store", | |
38 type=str, | |
39 default=None, | |
40 required=False, | |
41 help="File containing new uptake constraits." | |
42 ) | |
43 | |
44 args = parser.parse_args() | |
45 | |
46 try: | |
47 cb_model = cobra.io.read_sbml_model(args.cb_model_location) | |
48 except Exception as e: | |
49 raise Exception( | |
50 "The model could not be read. Ensure " | |
51 "it is in correct SBML format." | |
52 ) from e | |
53 | |
54 if args.uptake_constraints_file is not None\ | |
55 and args.uptake_constraints_file != "None": | |
56 constraints_df = pd.read_csv( | |
57 args.uptake_constraints_file, | |
58 sep=";", | |
59 header=0, | |
60 index_col=False | |
61 ) | |
62 for _, row in constraints_df.iterrows(): | |
63 cb_model.reactions.get_by_id( | |
64 row["reaction_id"] | |
65 ).lower_bound = row["lower_bound"] | |
66 cb_model.reactions.get_by_id( | |
67 row["reaction_id"] | |
68 ).upper_bound = row["upper_bound"] | |
69 | |
70 # do pFBA | |
71 solution = cobra.flux_analysis.pfba(cb_model) | |
72 | |
73 # make a dataframe with the reaction names, | |
74 # reaction ids, and flux distribution | |
75 flux_distribution = pd.DataFrame( | |
76 columns=["reaction_name", "reaction_id", "flux"] | |
77 ) | |
78 | |
79 flux_distribution["reaction_name"] = \ | |
80 [reaction.name for reaction in cb_model.reactions] | |
81 flux_distribution["reaction_id"] = \ | |
82 [reaction.id for reaction in cb_model.reactions] | |
83 flux_distribution["flux"] = \ | |
84 [solution.fluxes[reaction.id] for reaction in cb_model.reactions] | |
85 | |
86 flux_distribution.to_csv(args.out_file, sep=";", index=False) | |
87 | |
88 | |
89 if __name__ == "__main__": | |
90 __main__() |