Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_simulation.py @ 515:ded8a0d6cb6d draft
Uploaded
| author | luca_milaz |
|---|---|
| date | Thu, 09 Oct 2025 09:32:09 +0000 |
| parents | 97eea560a10f |
| children | c66aa96336d3 |
comparison
equal
deleted
inserted
replaced
| 514:e24998921824 | 515:ded8a0d6cb6d |
|---|---|
| 109 | 109 |
| 110 parser.add_argument('-ota', '--output_type_analysis', | 110 parser.add_argument('-ota', '--output_type_analysis', |
| 111 type=str, | 111 type=str, |
| 112 required=False, | 112 required=False, |
| 113 help='output type analysis (optimization methods)') | 113 help='output type analysis (optimization methods)') |
| 114 | 114 |
| 115 parser.add_argument('-idop', '--output_path', | 115 parser.add_argument('-idop', '--output_path', |
| 116 type=str, | 116 type=str, |
| 117 default='flux_simulation', | 117 default='flux_simulation/', |
| 118 help='output path for maps') | 118 help = 'output path for fluxes') |
| 119 | 119 |
| 120 parser.add_argument('-otm', '--out_mean', | |
| 121 type = str, | |
| 122 help = 'output of mean of fluxes') | |
| 123 | |
| 124 parser.add_argument('-otmd', '--out_median', | |
| 125 type = str, | |
| 126 help = 'output of median of fluxes') | |
| 127 | |
| 128 parser.add_argument('-otq', '--out_quantiles', | |
| 129 type = str, | |
| 130 help = 'output of quantiles of fluxes') | |
| 131 | |
| 132 parser.add_argument('-otf', '--out_fluxes', | |
| 133 type = str, | |
| 134 help = 'output of fluxes') | |
| 135 parser.add_argument('-otfva', '--out_fva', | |
| 136 type = str, | |
| 137 help = 'output of FVA results') | |
| 138 parser.add_argument('-otp', '--out_pfba', | |
| 139 type = str, | |
| 140 help = 'output of pFBA results') | |
| 141 parser.add_argument('-ots', '--out_sensitivity', | |
| 142 type = str, | |
| 143 help = 'output of sensitivity results') | |
| 120 ARGS = parser.parse_args(args) | 144 ARGS = parser.parse_args(args) |
| 121 return ARGS | 145 return ARGS |
| 122 ########################### warning ########################################### | 146 ########################### warning ########################################### |
| 123 def warning(s :str) -> None: | 147 def warning(s :str) -> None: |
| 124 """ | 148 """ |
| 133 with open(ARGS.out_log, 'a') as log: | 157 with open(ARGS.out_log, 'a') as log: |
| 134 log.write(s + "\n\n") | 158 log.write(s + "\n\n") |
| 135 print(s) | 159 print(s) |
| 136 | 160 |
| 137 | 161 |
| 138 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: | 162 def write_to_file(dataset: pd.DataFrame, name: str, path: str, keep_index:bool=False)->None: |
| 139 """ | 163 """ |
| 140 Write a DataFrame to a TSV file under ARGS.output_path with a given base name. | 164 Write a DataFrame to a TSV file under path with a given base name. |
| 141 | 165 |
| 142 Args: | 166 Args: |
| 143 dataset: The DataFrame to write. | 167 dataset: The DataFrame to write. |
| 144 name: Base file name (without extension). | 168 name: Base file name (without extension). |
| 169 path: Directory path where the file will be saved. | |
| 145 keep_index: Whether to keep the DataFrame index in the file. | 170 keep_index: Whether to keep the DataFrame index in the file. |
| 146 | 171 |
| 147 Returns: | 172 Returns: |
| 148 None | 173 None |
| 149 """ | 174 """ |
| 150 dataset.index.name = 'Reactions' | 175 dataset.index.name = 'Reactions' |
| 151 dataset.to_csv(ARGS.output_path + "/" + name + ".csv", sep = '\t', index = keep_index) | 176 dataset.to_csv(os.path.join(path, name + ".csv"), sep = '\t', index = keep_index) |
| 152 | 177 |
| 153 ############################ dataset input #################################### | 178 ############################ dataset input #################################### |
| 154 def read_dataset(data :str, name :str) -> pd.DataFrame: | 179 def read_dataset(data :str, name :str) -> pd.DataFrame: |
| 155 """ | 180 """ |
| 156 Read a dataset from a CSV file and return it as a pandas DataFrame. | 181 Read a dataset from a CSV file and return it as a pandas DataFrame. |
| 220 | 245 |
| 221 # Convert back to DataFrame with proper column names | 246 # Convert back to DataFrame with proper column names |
| 222 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) | 247 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) |
| 223 | 248 |
| 224 # Save the final merged result as CSV | 249 # Save the final merged result as CSV |
| 225 write_to_file(samplesTotal.T, model_name, True) | 250 write_to_file(samplesTotal.T, model_name, ARGS.output_path, True) |
| 226 | 251 |
| 227 # Clean up temporary numpy files | 252 # Clean up temporary numpy files |
| 228 for i in range(n_batches): | 253 for i in range(n_batches): |
| 229 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_OPTGP.npy" | 254 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_OPTGP.npy" |
| 230 if os.path.exists(batch_filename): | 255 if os.path.exists(batch_filename): |
| 297 | 322 |
| 298 # Convert back to DataFrame with proper column namesq | 323 # Convert back to DataFrame with proper column namesq |
| 299 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) | 324 samplesTotal = pd.DataFrame(samplesTotal_array, columns=reaction_ids) |
| 300 | 325 |
| 301 # Save the final merged result as CSV | 326 # Save the final merged result as CSV |
| 302 write_to_file(samplesTotal.T, model_name, True) | 327 write_to_file(samplesTotal.T, model_name, ARGS.output_path, True) |
| 303 | 328 |
| 304 # Clean up temporary numpy files | 329 # Clean up temporary numpy files |
| 305 for i in range(n_batches): | 330 for i in range(n_batches): |
| 306 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_CBS.npy" | 331 batch_filename = f"{ARGS.output_path}/{model_name}_{i}_CBS.npy" |
| 307 if os.path.exists(batch_filename): | 332 if os.path.exists(batch_filename): |
| 486 num_processors = max(1, cpu_count() - 1) | 511 num_processors = max(1, cpu_count() - 1) |
| 487 | 512 |
| 488 global ARGS | 513 global ARGS |
| 489 ARGS = process_args(args) | 514 ARGS = process_args(args) |
| 490 | 515 |
| 491 if not os.path.exists(ARGS.output_path): | 516 if not os.path.exists('flux_simulation'): |
| 492 os.makedirs(ARGS.output_path) | 517 os.makedirs('flux_simulation') |
| 493 | 518 |
| 494 # --- Normalize inputs (the tool may pass comma-separated --input and either --name or --names) --- | 519 # --- Normalize inputs (the tool may pass comma-separated --input and either --name or --names) --- |
| 495 ARGS.input_files = ARGS.input.split(",") if ARGS.input else [] | 520 ARGS.input_files = ARGS.input.split(",") if ARGS.input else [] |
| 496 ARGS.file_names = ARGS.name.split(",") | 521 ARGS.file_names = ARGS.name.split(",") |
| 497 # output types (required) -> list | 522 # output types (required) -> list |
| 557 all_quantiles = pd.concat([result[2] for result in results], ignore_index=False) | 582 all_quantiles = pd.concat([result[2] for result in results], ignore_index=False) |
| 558 | 583 |
| 559 if "mean" in ARGS.output_types: | 584 if "mean" in ARGS.output_types: |
| 560 all_mean = all_mean.fillna(0.0) | 585 all_mean = all_mean.fillna(0.0) |
| 561 all_mean = all_mean.sort_index() | 586 all_mean = all_mean.sort_index() |
| 562 write_to_file(all_mean.T, "mean", True) | 587 write_to_file(all_mean.T, "mean", ARGS.out_mean, True) |
| 563 | 588 |
| 564 if "median" in ARGS.output_types: | 589 if "median" in ARGS.output_types: |
| 565 all_median = all_median.fillna(0.0) | 590 all_median = all_median.fillna(0.0) |
| 566 all_median = all_median.sort_index() | 591 all_median = all_median.sort_index() |
| 567 write_to_file(all_median.T, "median", True) | 592 write_to_file(all_median.T, "median", ARGS.out_median, True) |
| 568 | 593 |
| 569 if "quantiles" in ARGS.output_types: | 594 if "quantiles" in ARGS.output_types: |
| 570 all_quantiles = all_quantiles.fillna(0.0) | 595 all_quantiles = all_quantiles.fillna(0.0) |
| 571 all_quantiles = all_quantiles.sort_index() | 596 all_quantiles = all_quantiles.sort_index() |
| 572 write_to_file(all_quantiles.T, "quantiles", True) | 597 write_to_file(all_quantiles.T, "quantiles", ARGS.out_quantiles, True) |
| 573 else: | 598 else: |
| 574 print("=== SAMPLING SKIPPED (n_samples = 0 or sampling disabled) ===") | 599 print("=== SAMPLING SKIPPED (n_samples = 0 or sampling disabled) ===") |
| 575 | 600 |
| 576 # Handle optimization analysis outputs (always available) | 601 # Handle optimization analysis outputs (always available) |
| 577 print("=== PROCESSING OPTIMIZATION RESULTS ===") | 602 print("=== PROCESSING OPTIMIZATION RESULTS ===") |
| 582 index_result = 3 if perform_sampling else 0 | 607 index_result = 3 if perform_sampling else 0 |
| 583 | 608 |
| 584 if "pFBA" in ARGS.output_type_analysis: | 609 if "pFBA" in ARGS.output_type_analysis: |
| 585 all_pFBA = pd.concat([result[index_result] for result in results], ignore_index=False) | 610 all_pFBA = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 586 all_pFBA = all_pFBA.sort_index() | 611 all_pFBA = all_pFBA.sort_index() |
| 587 write_to_file(all_pFBA.T, "pFBA", True) | 612 write_to_file(all_pFBA.T, "pFBA", ARGS.out_pfba, True) |
| 588 index_result += 1 | 613 index_result += 1 |
| 589 | 614 |
| 590 if "FVA" in ARGS.output_type_analysis: | 615 if "FVA" in ARGS.output_type_analysis: |
| 591 all_FVA = pd.concat([result[index_result] for result in results], ignore_index=False) | 616 all_FVA = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 592 all_FVA = all_FVA.sort_index() | 617 all_FVA = all_FVA.sort_index() |
| 593 write_to_file(all_FVA.T, "FVA", True) | 618 write_to_file(all_FVA.T, "FVA", ARGS.out_fva, True) |
| 594 index_result += 1 | 619 index_result += 1 |
| 595 | 620 |
| 596 if "sensitivity" in ARGS.output_type_analysis: | 621 if "sensitivity" in ARGS.output_type_analysis: |
| 597 all_sensitivity = pd.concat([result[index_result] for result in results], ignore_index=False) | 622 all_sensitivity = pd.concat([result[index_result] for result in results], ignore_index=False) |
| 598 all_sensitivity = all_sensitivity.sort_index() | 623 all_sensitivity = all_sensitivity.sort_index() |
| 599 write_to_file(all_sensitivity.T, "sensitivity", True) | 624 write_to_file(all_sensitivity.T, "sensitivity", ARGS.out_sensitivity, True) |
| 600 | 625 |
| 601 return | 626 return |
| 602 | 627 |
| 603 ############################################################################## | 628 ############################################################################## |
| 604 if __name__ == "__main__": | 629 if __name__ == "__main__": |
