Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_simulation.py @ 147:3fca9b568faf draft
Uploaded
author | bimib |
---|---|
date | Wed, 06 Nov 2024 13:57:24 +0000 |
parents | accda943dfb9 |
children | ebd2065dbdc2 |
comparison
equal
deleted
inserted
replaced
146:88cf4543e210 | 147:3fca9b568faf |
---|---|
9 from joblib import Parallel, delayed, cpu_count | 9 from joblib import Parallel, delayed, cpu_count |
10 from cobra.sampling import OptGPSampler | 10 from cobra.sampling import OptGPSampler |
11 import sys | 11 import sys |
12 | 12 |
13 ################################# process args ############################### | 13 ################################# process args ############################### |
14 def process_args(args :List[str]) -> argparse.Namespace: | 14 def process_args(args :List[str] = None) -> argparse.Namespace: |
15 """ | 15 """ |
16 Processes command-line arguments. | 16 Processes command-line arguments. |
17 | 17 |
18 Args: | 18 Args: |
19 args (list): List of command-line arguments. | 19 args (list): List of command-line arguments. |
86 parser.add_argument('-ota', '--output_type_analysis', | 86 parser.add_argument('-ota', '--output_type_analysis', |
87 type = str, | 87 type = str, |
88 required = False, | 88 required = False, |
89 help = 'output type analysis') | 89 help = 'output type analysis') |
90 | 90 |
91 ARGS = parser.parse_args() | 91 parser.add_argument( |
92 '-idop', '--output_path', | |
93 type = str, | |
94 default='result', | |
95 help = 'output path for maps') | |
96 | |
97 ARGS = parser.parse_args(args) | |
92 return ARGS | 98 return ARGS |
93 | 99 |
94 ########################### warning ########################################### | 100 ########################### warning ########################################### |
95 def warning(s :str) -> None: | 101 def warning(s :str) -> None: |
96 """ | 102 """ |
107 print(s) | 113 print(s) |
108 | 114 |
109 | 115 |
110 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: | 116 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: |
111 dataset.index.name = 'Reactions' | 117 dataset.index.name = 'Reactions' |
112 dataset.to_csv(ARGS.output_folder + name + ".csv", sep = '\t', index = keep_index) | 118 dataset.to_csv(ARGS.output_path + name + ".csv", sep = '\t', index = keep_index) |
113 | 119 |
114 ############################ dataset input #################################### | 120 ############################ dataset input #################################### |
115 def read_dataset(data :str, name :str) -> pd.DataFrame: | 121 def read_dataset(data :str, name :str) -> pd.DataFrame: |
116 """ | 122 """ |
117 Read a dataset from a CSV file and return it as a pandas DataFrame. | 123 Read a dataset from a CSV file and return it as a pandas DataFrame. |
154 """ | 160 """ |
155 | 161 |
156 for i in range(0, n_batches): | 162 for i in range(0, n_batches): |
157 optgp = OptGPSampler(model, thinning, seed) | 163 optgp = OptGPSampler(model, thinning, seed) |
158 samples = optgp.sample(n_samples) | 164 samples = optgp.sample(n_samples) |
159 samples.to_csv(ARGS.output_folder + model_name + '_'+ str(i)+'_OPTGP.csv', index=False) | 165 samples.to_csv(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv', index=False) |
160 seed+=1 | 166 seed+=1 |
161 samplesTotal = pd.DataFrame() | 167 samplesTotal = pd.DataFrame() |
162 for i in range(0, n_batches): | 168 for i in range(0, n_batches): |
163 samples_batch = pd.read_csv(ARGS.output_folder + model_name + '_'+ str(i)+'_OPTGP.csv') | 169 samples_batch = pd.read_csv(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv') |
164 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 170 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) |
165 | 171 |
166 write_to_file(samplesTotal.T, model_name, True) | 172 write_to_file(samplesTotal.T, model_name, True) |
167 | 173 |
168 for i in range(0, n_batches): | 174 for i in range(0, n_batches): |
169 os.remove(ARGS.output_folder + model_name + '_'+ str(i)+'_OPTGP.csv') | 175 os.remove(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv') |
170 pass | 176 pass |
171 | 177 |
172 | 178 |
173 def CBS_sampler(model:cobra.Model, model_name:str, n_samples:int=1000, n_batches:int=1, seed:int=0)-> None: | 179 def CBS_sampler(model:cobra.Model, model_name:str, n_samples:int=1000, n_batches:int=1, seed:int=0)-> None: |
174 """ | 180 """ |
197 utils.logWarning( | 203 utils.logWarning( |
198 "Warning: GLPK solver has failed for " + model_name + ". Trying with COBRA interface. Error:" + str(e), | 204 "Warning: GLPK solver has failed for " + model_name + ". Trying with COBRA interface. Error:" + str(e), |
199 ARGS.out_log) | 205 ARGS.out_log) |
200 CBS_backend.randomObjectiveFunctionSampling_cobrapy(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], | 206 CBS_backend.randomObjectiveFunctionSampling_cobrapy(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], |
201 samples) | 207 samples) |
202 utils.logWarning(ARGS.output_folder + model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log) | 208 utils.logWarning(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log) |
203 samples.to_csv(ARGS.output_folder + model_name + '_'+ str(i)+'_CBS.csv', index=False) | 209 samples.to_csv(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv', index=False) |
204 | 210 |
205 samplesTotal = pd.DataFrame() | 211 samplesTotal = pd.DataFrame() |
206 for i in range(0, n_batches): | 212 for i in range(0, n_batches): |
207 samples_batch = pd.read_csv(ARGS.output_folder + model_name + '_'+ str(i)+'_CBS.csv') | 213 samples_batch = pd.read_csv(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv') |
208 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 214 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) |
209 | 215 |
210 write_to_file(samplesTotal.T, model_name, True) | 216 write_to_file(samplesTotal.T, model_name, True) |
211 | 217 |
212 for i in range(0, n_batches): | 218 for i in range(0, n_batches): |
213 os.remove(ARGS.output_folder + model_name + '_'+ str(i)+'_CBS.csv') | 219 os.remove(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv') |
214 pass | 220 pass |
215 | 221 |
216 | 222 |
217 def model_sampler(model_input_original:cobra.Model, bounds_path:str, cell_name:str)-> List[pd.DataFrame]: | 223 def model_sampler(model_input_original:cobra.Model, bounds_path:str, cell_name:str)-> List[pd.DataFrame]: |
218 """ | 224 """ |
242 CBS_sampler(model_input, name, ARGS.n_samples, ARGS.n_batches, ARGS.seed) | 248 CBS_sampler(model_input, name, ARGS.n_samples, ARGS.n_batches, ARGS.seed) |
243 | 249 |
244 df_mean, df_median, df_quantiles = fluxes_statistics(name, ARGS.output_types) | 250 df_mean, df_median, df_quantiles = fluxes_statistics(name, ARGS.output_types) |
245 | 251 |
246 if("fluxes" not in ARGS.output_types): | 252 if("fluxes" not in ARGS.output_types): |
247 os.remove(ARGS.output_folder + name + '.csv') | 253 os.remove(ARGS.output_path + name + '.csv') |
248 | 254 |
249 returnList = [] | 255 returnList = [] |
250 returnList.append(df_mean) | 256 returnList.append(df_mean) |
251 returnList.append(df_median) | 257 returnList.append(df_median) |
252 returnList.append(df_quantiles) | 258 returnList.append(df_quantiles) |
276 | 282 |
277 df_mean = pd.DataFrame() | 283 df_mean = pd.DataFrame() |
278 df_median= pd.DataFrame() | 284 df_median= pd.DataFrame() |
279 df_quantiles= pd.DataFrame() | 285 df_quantiles= pd.DataFrame() |
280 | 286 |
281 df_samples = pd.read_csv(ARGS.output_folder + model_name + '.csv', sep = '\t', index_col = 0).T | 287 df_samples = pd.read_csv(ARGS.output_path + model_name + '.csv', sep = '\t', index_col = 0).T |
282 df_samples = df_samples.round(8) | 288 df_samples = df_samples.round(8) |
283 | 289 |
284 for output_type in output_types: | 290 for output_type in output_types: |
285 if(output_type == "mean"): | 291 if(output_type == "mean"): |
286 df_mean = df_samples.mean() | 292 df_mean = df_samples.mean() |
361 df_sensitivity.loc[model_name] = newRow | 367 df_sensitivity.loc[model_name] = newRow |
362 df_sensitivity = df_sensitivity.astype(float).round(6) | 368 df_sensitivity = df_sensitivity.astype(float).round(6) |
363 return df_pFBA, df_FVA, df_sensitivity | 369 return df_pFBA, df_FVA, df_sensitivity |
364 | 370 |
365 ############################# main ########################################### | 371 ############################# main ########################################### |
366 def main() -> None: | 372 def main(args :List[str] = None) -> None: |
367 """ | 373 """ |
368 Initializes everything and sets the program in motion based on the fronted input arguments. | 374 Initializes everything and sets the program in motion based on the fronted input arguments. |
369 | 375 |
370 Returns: | 376 Returns: |
371 None | 377 None |
374 os.makedirs('flux_simulation/') | 380 os.makedirs('flux_simulation/') |
375 | 381 |
376 num_processors = cpu_count() | 382 num_processors = cpu_count() |
377 | 383 |
378 global ARGS | 384 global ARGS |
379 ARGS = process_args(sys.argv) | 385 ARGS = process_args(args) |
380 | 386 |
381 ARGS.output_folder = 'flux_simulation/' | |
382 | |
383 | 387 |
384 model_type :utils.Model = ARGS.model_selector | 388 model_type :utils.Model = ARGS.model_selector |
385 if model_type is utils.Model.Custom: | 389 if model_type is utils.Model.Custom: |
386 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext) | 390 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext) |
387 else: | 391 else: |