annotate gem_extract_exchange.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
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 def read_model(model_location):
b79cf44068bc 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)
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
8 return model
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
9
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
10
b79cf44068bc 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):
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
12 exchange_reactions = model.exchanges
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
13 exchange_reactions_info = []
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
14 for reaction in exchange_reactions:
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
15 exchange_reactions_info.append(
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
16 [
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
17 reaction.id,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
18 reaction.name,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
19 reaction.reaction,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
20 reaction.lower_bound,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
21 reaction.upper_bound
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 txt_object = (
b79cf44068bc 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;"
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
25 "lower_bound;upper_bound\n"
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
26 )
b79cf44068bc 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:
b79cf44068bc 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"
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
29 return txt_object
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
30
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
31
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
32 def __main__():
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
33
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
34 # Parsing arguments
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
35 parser = argparse.ArgumentParser(
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
36 prog="GEM ",
b79cf44068bc 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 "
b79cf44068bc 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.",
b79cf44068bc 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.",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
40 )
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
41 parser.add_argument(
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
42 "-m",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
43 "--cb_model_location",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
44 dest="cb_model_location",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
45 action="store",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
46 type=str,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
47 default=None,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
48 required=True,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
49 help="The model to use."
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
50 )
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
51 parser.add_argument(
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
52 "-output",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
53 "--output",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
54 dest="out_file",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
55 action="store",
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
56 type=str,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
57 default=None,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
58 required=True,
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
59 help="The output file."
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
60 )
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
61 args = parser.parse_args()
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
62
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
63 # Reading model from file
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
64 try:
b79cf44068bc 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)
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
66 except Exception as e:
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
67 raise Exception(
b79cf44068bc 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."
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
69 ) from e
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
70
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
71 # Getting exchange reactions info
b79cf44068bc 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)
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
73
b79cf44068bc 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
b79cf44068bc 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:
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
76 outfile.write(str(answer))
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 if __name__ == "__main__":
b79cf44068bc planemo upload for repository https://github.com/AlmaasLab/elixir-galaxy-tools-systemsbiology commit 3f7bec1264a86e1488ee1315dbac0f44675f5171
iuc
parents:
diff changeset
80 __main__()