Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_simulation.py @ 519:8f65bed8d4cc draft
Uploaded
| author | luca_milaz |
|---|---|
| date | Thu, 09 Oct 2025 10:08:20 +0000 |
| parents | c66aa96336d3 |
| children | dd159f41b94c |
comparison
equal
deleted
inserted
replaced
| 518:c3c1d3b3941f | 519:8f65bed8d4cc |
|---|---|
| 164 with open(ARGS.out_log, 'a') as log: | 164 with open(ARGS.out_log, 'a') as log: |
| 165 log.write(s + "\n\n") | 165 log.write(s + "\n\n") |
| 166 print(s) | 166 print(s) |
| 167 | 167 |
| 168 | 168 |
| 169 def write_to_file(dataset: pd.DataFrame, name: str, path: str, keep_index:bool=False)->None: | 169 def write_to_file(dataset: pd.DataFrame, path: str, keep_index:bool=False, name:str=None)->None: |
| 170 """ | 170 """ |
| 171 Write a DataFrame to a TSV file under path with a given base name. | 171 Write a DataFrame to a TSV file under path with a given base name. |
| 172 | 172 |
| 173 Args: | 173 Args: |
| 174 dataset: The DataFrame to write. | 174 dataset: The DataFrame to write. |
| 175 name: Base file name (without extension). | 175 name: Base file name (without extension). If None, 'path' is treated as the full file path. |
| 176 path: Directory path where the file will be saved. | 176 path: Directory path where the file will be saved. |
| 177 keep_index: Whether to keep the DataFrame index in the file. | 177 keep_index: Whether to keep the DataFrame index in the file. |
| 178 | 178 |
| 179 Returns: | 179 Returns: |
| 180 None | 180 None |
| 181 """ | 181 """ |
| 182 dataset.index.name = 'Reactions' | 182 dataset.index.name = 'Reactions' |
| 183 dataset.to_csv(os.path.join(path, name + ".csv"), sep = '\t', index = keep_index) | 183 if name: |
| 184 dataset.to_csv(os.path.join(path, name + ".csv"), sep = '\t', index = keep_index) | |
| 185 else: | |
| 186 dataset.to_csv(path, sep = '\t', index = keep_index) | |
| 184 | 187 |
| 185 ############################ dataset input #################################### | 188 ############################ dataset input #################################### |
| 186 def read_dataset(data :str, name :str) -> pd.DataFrame: | 189 def read_dataset(data :str, name :str) -> pd.DataFrame: |
| 187 """ | 190 """ |
| 188 Read a dataset from a CSV file and return it as a pandas DataFrame. | 191 Read a dataset from a CSV file and return it as a pandas DataFrame. |
| 252 | 255 |
| 253 # Convert back to DataFrame with proper column names | 256 # Convert back to DataFrame with proper column names |
| 254 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) | 257 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) |
| 255 | 258 |
| 256 # Save the final merged result as CSV | 259 # Save the final merged result as CSV |
| 257 write_to_file(samplesTotal.T, model_name, ARGS.output_path, True) | 260 write_to_file(samplesTotal.T, ARGS.output_path, True, name=model_name) |
| 258 | 261 |
| 259 # Clean up temporary numpy files | 262 # Clean up temporary numpy files |
| 260 for i in range(n_batches): | 263 for i in range(n_batches): |
| 261 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_OPTGP.npy" | 264 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_OPTGP.npy" |
| 262 if os.path.exists(batch_filename): | 265 if os.path.exists(batch_filename): |
| 329 | 332 |
| 330 # Convert back to DataFrame with proper column namesq | 333 # Convert back to DataFrame with proper column namesq |
| 331 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) | 334 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) |
| 332 | 335 |
| 333 # Save the final merged result as CSV | 336 # Save the final merged result as CSV |
| 334 write_to_file(samplesTotal.T, model_name, ARGS.output_path, True) | 337 write_to_file(samplesTotal.T, ARGS.output_path, True, name=model_name) |
| 335 | 338 |
| 336 # Clean up temporary numpy files | 339 # Clean up temporary numpy files |
| 337 for i in range(n_batches): | 340 for i in range(n_batches): |
| 338 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_CBS.npy" | 341 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_CBS.npy" |
| 339 if os.path.exists(batch_filename): | 342 if os.path.exists(batch_filename): |
| 589 all_quantiles = pd.concat([result[2] for result in results], ignore_index=False) | 592 all_quantiles = pd.concat([result[2] for result in results], ignore_index=False) |
| 590 | 593 |
| 591 if "mean" in ARGS.output_types: | 594 if "mean" in ARGS.output_types: |
| 592 all_mean = all_mean.fillna(0.0) | 595 all_mean = all_mean.fillna(0.0) |
| 593 all_mean = all_mean.sort_index() | 596 all_mean = all_mean.sort_index() |
| 594 write_to_file(all_mean.T, "mean", ARGS.out_mean, True) | 597 write_to_file(all_mean.T, ARGS.out_mean, True) |
| 595 | 598 |
| 596 if "median" in ARGS.output_types: | 599 if "median" in ARGS.output_types: |
| 597 all_median = all_median.fillna(0.0) | 600 all_median = all_median.fillna(0.0) |
| 598 all_median = all_median.sort_index() | 601 all_median = all_median.sort_index() |
| 599 write_to_file(all_median.T, "median", ARGS.out_median, True) | 602 write_to_file(all_median.T, ARGS.out_median, True) |
| 600 | 603 |
| 601 if "quantiles" in ARGS.output_types: | 604 if "quantiles" in ARGS.output_types: |
| 602 all_quantiles = all_quantiles.fillna(0.0) | 605 all_quantiles = all_quantiles.fillna(0.0) |
| 603 all_quantiles = all_quantiles.sort_index() | 606 all_quantiles = all_quantiles.sort_index() |
| 604 write_to_file(all_quantiles.T, "quantiles", ARGS.out_quantiles, True) | 607 write_to_file(all_quantiles.T, ARGS.out_quantiles, True) |
| 605 else: | 608 else: |
| 606 print("=== SAMPLING SKIPPED (n_samples = 0 or sampling disabled) ===") | 609 print("=== SAMPLING SKIPPED (n_samples = 0 or sampling disabled) ===") |
| 607 | 610 |
| 608 # Handle optimization analysis outputs (always available) | 611 # Handle optimization analysis outputs (always available) |
| 609 print("=== PROCESSING OPTIMIZATION RESULTS ===") | 612 print("=== PROCESSING OPTIMIZATION RESULTS ===") |
| 614 index_result = 3 if perform_sampling else 0 | 617 index_result = 3 if perform_sampling else 0 |
| 615 | 618 |
| 616 if "pFBA" in ARGS.output_type_analysis: | 619 if "pFBA" in ARGS.output_type_analysis: |
| 617 all_pFBA = pd.concat([result[index_result] for result in results], ignore_index=False) | 620 all_pFBA = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 618 all_pFBA = all_pFBA.sort_index() | 621 all_pFBA = all_pFBA.sort_index() |
| 619 write_to_file(all_pFBA.T, "pFBA", ARGS.out_pfba, True) | 622 write_to_file(all_pFBA.T, ARGS.out_pfba, True) |
| 620 index_result += 1 | 623 index_result += 1 |
| 621 | 624 |
| 622 if "FVA" in ARGS.output_type_analysis: | 625 if "FVA" in ARGS.output_type_analysis: |
| 623 all_FVA = pd.concat([result[index_result] for result in results], ignore_index=False) | 626 all_FVA = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 624 all_FVA = all_FVA.sort_index() | 627 all_FVA = all_FVA.sort_index() |
| 625 write_to_file(all_FVA.T, "FVA", ARGS.out_fva, True) | 628 write_to_file(all_FVA.T, ARGS.out_fva, True) |
| 626 index_result += 1 | 629 index_result += 1 |
| 627 | 630 |
| 628 if "sensitivity" in ARGS.output_type_analysis: | 631 if "sensitivity" in ARGS.output_type_analysis: |
| 629 all_sensitivity = pd.concat([result[index_result] for result in results], ignore_index=False) | 632 all_sensitivity = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 630 all_sensitivity = all_sensitivity.sort_index() | 633 all_sensitivity = all_sensitivity.sort_index() |
| 631 write_to_file(all_sensitivity.T, "sensitivity", ARGS.out_sensitivity, True) | 634 write_to_file(all_sensitivity.T, ARGS.out_sensitivity, True) |
| 632 | 635 |
| 633 return | 636 return |
| 634 | 637 |
| 635 ############################################################################## | 638 ############################################################################## |
| 636 if __name__ == "__main__": | 639 if __name__ == "__main__": |
