comparison gem_flux_variability_analysis.py @ 0:d9893d41dd6a 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:33:34 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d9893d41dd6a
1 import argparse
2
3 import cobra
4 import pandas as pd
5
6
7 def __main__():
8 parser = argparse.ArgumentParser(
9 prog="FluxVariabilityAnalysis",
10 description="This program performs flux variability "
11 "analysis on a GEM",
12 epilog="Adding an epilog, but doubt it's needed.",
13 )
14 parser.add_argument(
15 "-m", "--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 "-f",
35 "--fraction",
36 dest="fraction_of_optimum",
37 action="store",
38 type=float,
39 default=None,
40 required=True,
41 help="The fraction of optimum the FVA solutions should come within."
42 )
43 parser.add_argument(
44 "-u",
45 "--uptake_constraints_file",
46 dest="uptake_constraints_file",
47 action="store",
48 type=str,
49 default=None,
50 required=False,
51 help="File containing new uptake constraits."
52 )
53
54 args = parser.parse_args()
55
56 # Validate constraints file first if provided
57 constraints_df = None
58 if args.uptake_constraints_file is not None\
59 and args.uptake_constraints_file != "None":
60 try:
61 constraints_df = pd.read_csv(
62 args.uptake_constraints_file,
63 sep=";",
64 header=0,
65 index_col=False
66 )
67
68 required_columns = ['reaction_id', 'lower_bound', 'upper_bound']
69 missing_columns = [col for col in required_columns if
70 col not in constraints_df.columns]
71
72 if missing_columns:
73 raise ValueError(
74 f"Constraints file is missing required columns: "
75 f"{', '.join(missing_columns)}. "
76 f"Required columns are: {', '.join(required_columns)}"
77 )
78 except FileNotFoundError:
79 raise FileNotFoundError(
80 f"Constraints file not found: {args.uptake_constraints_file}"
81 )
82 except pd.errors.EmptyDataError:
83 raise ValueError("Constraints file is empty")
84 except Exception as e:
85 raise ValueError(f"Error processing constraints file: {str(e)}")
86
87 # Load model
88 cb_model = cobra.io.read_sbml_model(args.cb_model_location)
89
90 # Apply constraints if they were loaded successfully
91 if constraints_df is not None:
92 for index, row in constraints_df.iterrows():
93 cb_model.reactions.get_by_id(
94 row["reaction_id"]).lower_bound = float(row["lower_bound"])
95 cb_model.reactions.get_by_id(
96 row["reaction_id"]).upper_bound = float(row["upper_bound"])
97
98 fraction_of_optimum = args.fraction_of_optimum
99
100 # perform fva
101 fva_result = cobra.flux_analysis.flux_variability_analysis(
102 cb_model,
103 fraction_of_optimum=fraction_of_optimum
104 )
105
106 # add reaction names and ids to the dataframe
107 fva_result["reaction_id"] = fva_result.index
108 fva_result["reaction_name"] = fva_result["reaction_id"].apply(
109 lambda x: cb_model.reactions.get_by_id(x).name
110 )
111
112 # reorder the columns
113 fva_result = fva_result[[
114 "reaction_id", "reaction_name", "minimum", "maximum"
115 ]]
116
117 fva_result.to_csv(args.out_file, sep=";", index=False, header=True)
118
119
120 if __name__ == "__main__":
121 __main__()