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