annotate marea_2/ras_to_bounds.py @ 120:f44f7a7c6a47 draft

Uploaded
author luca_milaz
date Sun, 21 Jul 2024 19:28:48 +0000
parents 6e62587904d6
children cd1943c86a4f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
1 import argparse
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
2 import utils.general_utils as utils
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
3 from typing import Optional, List
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
4 import os
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
5 import numpy as np
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
6 import pandas as pd
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
7 import cobra
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
8 import sys
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
9
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
10 ################################# process args ###############################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
11 def process_args(args :List[str]) -> argparse.Namespace:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
12 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
13 Processes command-line arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
14
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
15 Args:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
16 args (list): List of command-line arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
17
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
18 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
19 Namespace: An object containing parsed arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
20 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
21 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
22 description = 'process some value\'s')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
23
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
24 parser.add_argument(
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
25 '-ms', '--model_selector',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
26 type = utils.Model, default = utils.Model.ENGRO2, choices = [utils.Model.ENGRO2, utils.Model.Custom],
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
27 help = 'chose which type of model you want use')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
28
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
29 parser.add_argument("-mo", "--model", type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
30 help = "path to input file with custom rules, if provided")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
31
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
32 parser.add_argument("-mn", "--model_name", type = str, help = "custom mode name")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
33
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
34 parser.add_argument(
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
35 '-mes', '--medium_selector',
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
36 default = "allOpen",
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
37 help = 'chose which type of medium you want use')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
38
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
39 parser.add_argument("-meo", "--medium", type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
40 help = "path to input file with custom medium, if provided")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
41
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
42 parser.add_argument('-ol', '--out_log',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
43 help = "Output log")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
44
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
45 parser.add_argument('-td', '--tool_dir',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
46 type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
47 required = True,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
48 help = 'your tool directory')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
49
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
50 parser.add_argument('-ir', '--input_ras',
120
f44f7a7c6a47 Uploaded
luca_milaz
parents: 118
diff changeset
51 type=str,
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
52 required = False,
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
53 help = 'input ras')
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
54
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
55 parser.add_argument('-rs', '--ras_selector',
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
56 required = True,
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
57 type=bool,
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
58 help = 'ras selector')
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
59
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
60 ARGS = parser.parse_args()
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
61 return ARGS
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
62
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
63 ########################### warning ###########################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
64 def warning(s :str) -> None:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
65 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
66 Log a warning message to an output log file and print it to the console.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
67
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
68 Args:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
69 s (str): The warning message to be logged and printed.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
70
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
71 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
72 None
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
73 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
74 with open(ARGS.out_log, 'a') as log:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
75 log.write(s + "\n\n")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
76 print(s)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
77
115
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
78 ############################ dataset input ####################################
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
79 def read_dataset(data :str, name :str) -> pd.DataFrame:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
80 """
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
81 Read a dataset from a CSV file and return it as a pandas DataFrame.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
82
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
83 Args:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
84 data (str): Path to the CSV file containing the dataset.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
85 name (str): Name of the dataset, used in error messages.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
86
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
87 Returns:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
88 pandas.DataFrame: DataFrame containing the dataset.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
89
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
90 Raises:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
91 pd.errors.EmptyDataError: If the CSV file is empty.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
92 sys.exit: If the CSV file has the wrong format, the execution is aborted.
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
93 """
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
94 try:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
95 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
96 except pd.errors.EmptyDataError:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
97 sys.exit('Execution aborted: wrong format of ' + name + '\n')
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
98 if len(dataset.columns) < 2:
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
99 sys.exit('Execution aborted: wrong format of ' + name + '\n')
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
100 return dataset
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
101
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
102 def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
103 model_new = model.copy()
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
104 rxns_ids = []
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
105 for rxn in model.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
106 rxns_ids.append(rxn.id)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
107 for reaction in medium.keys():
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
108 if(medium[reaction] != None):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
109 model_new.reactions.get_by_id(reaction).lower_bound=-float(medium[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
110 df_FVA = cobra.flux_analysis.flux_variability_analysis(model_new,fraction_of_optimum=0,processes=1).round(8)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
111 for reaction in rxns_ids:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
112 model_new.reactions.get_by_id(reaction).lower_bound=float(df_FVA.loc[reaction,"minimum"])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
113 model_new.reactions.get_by_id(reaction).upper_bound=float(df_FVA.loc[reaction,"maximum"])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
114
116
902921535134 Uploaded
luca_milaz
parents: 115
diff changeset
115 if(ras is not None):#iterate over cells
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
116 for reaction in rxns_ids:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
117 if reaction in ras.keys():
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
118 lower_bound=model_new.reactions.get_by_id(reaction).lower_bound
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
119 upper_bound=model_new.reactions.get_by_id(reaction).upper_bound
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
120 valMax=float((upper_bound)*ras[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
121 valMin=float((lower_bound)*ras[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
122 if upper_bound!=0 and lower_bound==0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
123 model_new.reactions.get_by_id(reaction).upper_bound=valMax
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
124 if upper_bound==0 and lower_bound!=0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
125 model_new.reactions.get_by_id(reaction).lower_bound=valMin
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
126 if upper_bound!=0 and lower_bound!=0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
127 model_new.reactions.get_by_id(reaction).lower_bound=valMin
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
128 model_new.reactions.get_by_id(reaction).upper_bound=valMax
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
129 rxns = []
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
130 for reaction in model_new.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
131 rxns.append(reaction.id)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
132
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
133 bounds = pd.DataFrame(columns = ["lower_bound", "upper_bound"], index=rxns)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
134
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
135 for reaction in model.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
136 bounds.loc[reaction.id] = [reaction.lower_bound, reaction.upper_bound]
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
137 return bounds
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
138
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
139
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
140 ############################# main ###########################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
141 def main() -> None:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
142 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
143 Initializes everything and sets the program in motion based on the fronted input arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
144
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
145 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
146 None
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
147 """
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
148 if not os.path.exists('ras_to_bounds'):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
149 os.makedirs('ras_to_bounds')
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
150
111
b5b7e46be22a Uploaded
luca_milaz
parents: 107
diff changeset
151 global ARGS
113
93d2985a5189 Uploaded
luca_milaz
parents: 112
diff changeset
152 ARGS = process_args(sys.argv)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
153
111
b5b7e46be22a Uploaded
luca_milaz
parents: 107
diff changeset
154 ARGS.output_folder = 'ras_to_bounds/'
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
155
117
1061a1cbca8c Uploaded
luca_milaz
parents: 116
diff changeset
156 utils.logWarning(
1061a1cbca8c Uploaded
luca_milaz
parents: 116
diff changeset
157 ARGS.input_ras,
1061a1cbca8c Uploaded
luca_milaz
parents: 116
diff changeset
158 ARGS.out_log)
116
902921535134 Uploaded
luca_milaz
parents: 115
diff changeset
159
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
160 boundsPath = utils.FilePath("bounds", ".csv", prefix = ARGS.output_folder)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
161 mediumPath = utils.FilePath("medium", ".csv", prefix = ARGS.output_folder)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
162
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
163 if(ARGS.ras_selector == True):
115
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
164 ras = read_dataset(ARGS.input_ras, "ras dataset")
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
165 ras.replace("None", None, inplace=True)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
166 ras = ras.astype(float)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
167
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
168 model_type :utils.Model = ARGS.model_selector
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
169 if model_type is utils.Model.Custom:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
170 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
171 else:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
172 model = model_type.getCOBRAmodel(toolDir=ARGS.tool_dir)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
173
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
174 if(ARGS.medium_selector == "Custom"):
115
5971a04cc59a Uploaded
luca_milaz
parents: 113
diff changeset
175 medium = read_dataset(ARGS.input_medium, "medium dataset")
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
176 medium = medium.astype(float)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
177 medium = medium['medium'].to_dict()
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
178 else:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
179 df_mediums = pd.read_csv(ARGS.tool_dir + "/local/medium/medium.csv", index_col = 0)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
180 medium = df_mediums[[ARGS.medium_selector]]
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
181 medium = medium[ARGS.medium_selector].to_dict()
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
182
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
183 if(ARGS.ras_selector == True):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
184 bounds = generate_bounds(model, medium, ras = ras)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
185 else:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
186 bounds = generate_bounds(model, medium)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
187
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
188 bounds.to_csv(boundsPath.show())
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
189 medium.to_csv(mediumPath.show())
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
190 pass
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
191
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
192 ##############################################################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
193 if __name__ == "__main__":
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
194 main()