Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_simulation.py @ 161:9159e12b03fa draft
Uploaded
author | francesco_lapi |
---|---|
date | Tue, 12 Nov 2024 17:11:17 +0000 |
parents | e1b0ddc770a9 |
children | b5a26d1c4fdc |
comparison
equal
deleted
inserted
replaced
160:e1b0ddc770a9 | 161:9159e12b03fa |
---|---|
112 print(s) | 112 print(s) |
113 | 113 |
114 | 114 |
115 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: | 115 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: |
116 dataset.index.name = 'Reactions' | 116 dataset.index.name = 'Reactions' |
117 print(ARGS.output_path + name + ".csv") | 117 print(ARGS.output_path + "/" + name + ".csv") |
118 dataset.to_csv(ARGS.output_path + name + ".csv", sep = '\t', index = keep_index) | 118 dataset.to_csv(ARGS.output_path + "/" + name + ".csv", sep = '\t', index = keep_index) |
119 | 119 |
120 ############################ dataset input #################################### | 120 ############################ dataset input #################################### |
121 def read_dataset(data :str, name :str) -> pd.DataFrame: | 121 def read_dataset(data :str, name :str) -> pd.DataFrame: |
122 """ | 122 """ |
123 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. |
160 """ | 160 """ |
161 | 161 |
162 for i in range(0, n_batches): | 162 for i in range(0, n_batches): |
163 optgp = OptGPSampler(model, thinning, seed) | 163 optgp = OptGPSampler(model, thinning, seed) |
164 samples = optgp.sample(n_samples) | 164 samples = optgp.sample(n_samples) |
165 samples.to_csv(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv', index=False) | 165 samples.to_csv(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_OPTGP.csv', index=False) |
166 seed+=1 | 166 seed+=1 |
167 samplesTotal = pd.DataFrame() | 167 samplesTotal = pd.DataFrame() |
168 for i in range(0, n_batches): | 168 for i in range(0, n_batches): |
169 samples_batch = pd.read_csv(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv') | 169 samples_batch = pd.read_csv(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_OPTGP.csv') |
170 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 170 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) |
171 | 171 |
172 write_to_file(samplesTotal.T, model_name, True) | 172 write_to_file(samplesTotal.T, model_name, True) |
173 | 173 |
174 for i in range(0, n_batches): | 174 for i in range(0, n_batches): |
175 os.remove(ARGS.output_path + model_name + '_'+ str(i)+'_OPTGP.csv') | 175 os.remove(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_OPTGP.csv') |
176 pass | 176 pass |
177 | 177 |
178 | 178 |
179 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: |
180 """ | 180 """ |
203 utils.logWarning( | 203 utils.logWarning( |
204 "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), |
205 ARGS.out_log) | 205 ARGS.out_log) |
206 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], |
207 samples) | 207 samples) |
208 utils.logWarning(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log) | 208 utils.logWarning(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log) |
209 samples.to_csv(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv', index=False) | 209 samples.to_csv(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_CBS.csv', index=False) |
210 | 210 |
211 samplesTotal = pd.DataFrame() | 211 samplesTotal = pd.DataFrame() |
212 for i in range(0, n_batches): | 212 for i in range(0, n_batches): |
213 samples_batch = pd.read_csv(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv') | 213 samples_batch = pd.read_csv(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_CBS.csv') |
214 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 214 samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) |
215 | 215 |
216 write_to_file(samplesTotal.T, model_name, True) | 216 write_to_file(samplesTotal.T, model_name, True) |
217 | 217 |
218 for i in range(0, n_batches): | 218 for i in range(0, n_batches): |
219 os.remove(ARGS.output_path + model_name + '_'+ str(i)+'_CBS.csv') | 219 os.remove(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_CBS.csv') |
220 pass | 220 pass |
221 | 221 |
222 | 222 |
223 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]: |
224 """ | 224 """ |
248 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) |
249 | 249 |
250 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) |
251 | 251 |
252 if("fluxes" not in ARGS.output_types): | 252 if("fluxes" not in ARGS.output_types): |
253 os.remove(ARGS.output_path + name + '.csv') | 253 os.remove(ARGS.output_path + "/" + name + '.csv') |
254 | 254 |
255 returnList = [] | 255 returnList = [] |
256 returnList.append(df_mean) | 256 returnList.append(df_mean) |
257 returnList.append(df_median) | 257 returnList.append(df_median) |
258 returnList.append(df_quantiles) | 258 returnList.append(df_quantiles) |
282 | 282 |
283 df_mean = pd.DataFrame() | 283 df_mean = pd.DataFrame() |
284 df_median= pd.DataFrame() | 284 df_median= pd.DataFrame() |
285 df_quantiles= pd.DataFrame() | 285 df_quantiles= pd.DataFrame() |
286 | 286 |
287 df_samples = pd.read_csv(ARGS.output_path + 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 |
288 df_samples = df_samples.round(8) | 288 df_samples = df_samples.round(8) |
289 | 289 |
290 for output_type in output_types: | 290 for output_type in output_types: |
291 if(output_type == "mean"): | 291 if(output_type == "mean"): |
292 df_mean = df_samples.mean() | 292 df_mean = df_samples.mean() |