annotate gem_extract_exchange.py @ 0:927af80d5e38 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:42 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
1 import argparse
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
2
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
3 import cobra
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
4
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
5
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
6 def read_model(model_location):
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
7 model = cobra.io.read_sbml_model(model_location)
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
8 return model
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
9
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
10
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
11 def get_exchange_reactions_info(model):
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
12 exchange_reactions = model.exchanges
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
13 exchange_reactions_info = []
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
14 for reaction in exchange_reactions:
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
15 exchange_reactions_info.append(
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
16 [
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
17 reaction.id,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
18 reaction.name,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
19 reaction.reaction,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
20 reaction.lower_bound,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
21 reaction.upper_bound
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
22 ])
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
23 txt_object = (
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
24 "reaction_id;reaction_name;reaction_stoichiometry;"
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
25 "lower_bound;upper_bound\n"
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
26 )
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
27 for reaction in exchange_reactions_info:
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
28 txt_object += ";".join([str(x) for x in reaction]) + "\n"
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
29 return txt_object
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
30
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
31
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
32 def __main__():
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
33
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
34 # Parsing arguments
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
35 parser = argparse.ArgumentParser(
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
36 prog="GEM ",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
37 description="This program retrieves the exchange fluxes "
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
38 "of a GEM model to be used in Galaxy.",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
39 epilog="Adding an epilog, but doubt it's needed.",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
40 )
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
41 parser.add_argument(
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
42 "-m",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
43 "--cb_model_location",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
44 dest="cb_model_location",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
45 action="store",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
46 type=str,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
47 default=None,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
48 required=True,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
49 help="The model to use."
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
50 )
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
51 parser.add_argument(
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
52 "-output",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
53 "--output",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
54 dest="out_file",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
55 action="store",
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
56 type=str,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
57 default=None,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
58 required=True,
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
59 help="The output file."
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
60 )
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
61 args = parser.parse_args()
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
62
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
63 # Reading model from file
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
64 try:
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
65 cb_model = read_model(args.cb_model_location)
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
66 except Exception as e:
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
67 raise Exception(
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
68 "The model could not be read. Ensure it is in correct SBML format."
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
69 ) from e
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
70
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
71 # Getting exchange reactions info
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
72 answer = get_exchange_reactions_info(cb_model)
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
73
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
74 # Writing exchange reactions info to file
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
75 with open(args.out_file, "w") as outfile:
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
76 outfile.write(str(answer))
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
77
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
78
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
79 if __name__ == "__main__":
927af80d5e38 planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
80 __main__()