Mercurial > repos > bimib > cobraxy
comparison COBRAxy/ras_to_bounds_beta.py @ 428:8cd0c70b0084 draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Wed, 10 Sep 2025 13:21:41 +0000 |
| parents | 0877682fff48 |
| children | 0485c4b1943d |
comparison
equal
deleted
inserted
replaced
| 427:4a385fdb9e58 | 428:8cd0c70b0084 |
|---|---|
| 12 from joblib import Parallel, delayed, cpu_count | 12 from joblib import Parallel, delayed, cpu_count |
| 13 import utils.rule_parsing as rulesUtils | 13 import utils.rule_parsing as rulesUtils |
| 14 import utils.reaction_parsing as reactionUtils | 14 import utils.reaction_parsing as reactionUtils |
| 15 import utils.model_utils as modelUtils | 15 import utils.model_utils as modelUtils |
| 16 | 16 |
| 17 # , medium | 17 # ras_selector $cond_ras.ras_choice |
| 18 # | |
| 18 | 19 |
| 19 ################################# process args ############################### | 20 ################################# process args ############################### |
| 20 def process_args(args :List[str] = None) -> argparse.Namespace: | 21 def process_args(args :List[str] = None) -> argparse.Namespace: |
| 21 """ | 22 """ |
| 22 Processes command-line arguments. | 23 Processes command-line arguments. |
| 48 help = 'input ras') | 49 help = 'input ras') |
| 49 | 50 |
| 50 parser.add_argument('-rn', '--name', | 51 parser.add_argument('-rn', '--name', |
| 51 type=str, | 52 type=str, |
| 52 help = 'ras class names') | 53 help = 'ras class names') |
| 53 | |
| 54 parser.add_argument('-rs', '--ras_selector', | |
| 55 required = True, | |
| 56 type=utils.Bool("using_RAS"), | |
| 57 help = 'ras selector') | |
| 58 | 54 |
| 59 parser.add_argument('-cc', '--cell_class', | 55 parser.add_argument('-cc', '--cell_class', |
| 60 type = str, | 56 type = str, |
| 61 help = 'output of cell class') | 57 help = 'output of cell class') |
| 62 parser.add_argument( | 58 parser.add_argument( |
| 302 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)( | 298 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)( |
| 303 cellName, ras_row, model, rxns_ids, output_folder, | 299 cellName, ras_row, model, rxns_ids, output_folder, |
| 304 save_models, save_models_path, save_models_format | 300 save_models, save_models_path, save_models_format |
| 305 ) for cellName, ras_row in ras.iterrows()) | 301 ) for cellName, ras_row in ras.iterrows()) |
| 306 else: | 302 else: |
| 307 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 303 raise ValueError("RAS DataFrame is None. Cannot generate bounds without RAS data.") |
| 308 newBounds = apply_ras_bounds(bounds, pd.Series([1]*len(rxns_ids), index=rxns_ids)) | |
| 309 newBounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) | |
| 310 | |
| 311 # Save model if requested | |
| 312 if save_models: | |
| 313 modified_model = apply_bounds_to_model(model, newBounds) | |
| 314 save_model(modified_model, "model_with_bounds", save_models_path, save_models_format) | |
| 315 | |
| 316 pass | 304 pass |
| 317 | 305 |
| 318 ############################# main ########################################### | 306 ############################# main ########################################### |
| 319 def main(args:List[str] = None) -> None: | 307 def main(args:List[str] = None) -> None: |
| 320 """ | 308 """ |
| 327 os.makedirs('ras_to_bounds') | 315 os.makedirs('ras_to_bounds') |
| 328 | 316 |
| 329 global ARGS | 317 global ARGS |
| 330 ARGS = process_args(args) | 318 ARGS = process_args(args) |
| 331 | 319 |
| 332 if(ARGS.ras_selector == True): | 320 |
| 333 ras_file_list = ARGS.input_ras.split(",") | 321 ras_file_list = ARGS.input_ras.split(",") |
| 334 ras_file_names = ARGS.name.split(",") | 322 ras_file_names = ARGS.name.split(",") |
| 335 if len(ras_file_names) != len(set(ras_file_names)): | 323 if len(ras_file_names) != len(set(ras_file_names)): |
| 336 error_message = "Duplicated file names in the uploaded RAS matrices." | 324 error_message = "Duplicated file names in the uploaded RAS matrices." |
| 337 warning(error_message) | 325 warning(error_message) |
| 338 raise ValueError(error_message) | 326 raise ValueError(error_message) |
| 339 pass | 327 pass |
| 340 ras_class_names = [] | 328 ras_class_names = [] |
| 341 for file in ras_file_names: | 329 for file in ras_file_names: |
| 342 ras_class_names.append(file.rsplit(".", 1)[0]) | 330 ras_class_names.append(file.rsplit(".", 1)[0]) |
| 343 ras_list = [] | 331 ras_list = [] |
| 344 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"]) | 332 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"]) |
| 345 for ras_matrix, ras_class_name in zip(ras_file_list, ras_class_names): | 333 for ras_matrix, ras_class_name in zip(ras_file_list, ras_class_names): |
| 346 ras = read_dataset(ras_matrix, "ras dataset") | 334 ras = read_dataset(ras_matrix, "ras dataset") |
| 347 ras.replace("None", None, inplace=True) | 335 ras.replace("None", None, inplace=True) |
| 348 ras.set_index("Reactions", drop=True, inplace=True) | 336 ras.set_index("Reactions", drop=True, inplace=True) |
| 349 ras = ras.T | 337 ras = ras.T |
| 350 ras = ras.astype(float) | 338 ras = ras.astype(float) |
| 351 if(len(ras_file_list)>1): | 339 if(len(ras_file_list)>1): |
| 352 #append class name to patient id (dataframe index) | 340 #append class name to patient id (dataframe index) |
| 353 ras.index = [f"{idx}_{ras_class_name}" for idx in ras.index] | 341 ras.index = [f"{idx}_{ras_class_name}" for idx in ras.index] |
| 354 else: | 342 else: |
| 355 ras.index = [f"{idx}" for idx in ras.index] | 343 ras.index = [f"{idx}" for idx in ras.index] |
| 356 ras_list.append(ras) | 344 ras_list.append(ras) |
| 357 for patient_id in ras.index: | 345 for patient_id in ras.index: |
| 358 class_assignments.loc[class_assignments.shape[0]] = [patient_id, ras_class_name] | 346 class_assignments.loc[class_assignments.shape[0]] = [patient_id, ras_class_name] |
| 359 | 347 |
| 360 | 348 |
| 361 # Concatenate all ras DataFrames into a single DataFrame | 349 # Concatenate all ras DataFrames into a single DataFrame |
| 362 ras_combined = pd.concat(ras_list, axis=0) | 350 ras_combined = pd.concat(ras_list, axis=0) |
| 363 # Normalize the RAS values by max RAS | 351 # Normalize the RAS values by max RAS |
| 364 ras_combined = ras_combined.div(ras_combined.max(axis=0)) | 352 ras_combined = ras_combined.div(ras_combined.max(axis=0)) |
| 370 | 358 |
| 371 print("\n=== VALIDAZIONE MODELLO ===") | 359 print("\n=== VALIDAZIONE MODELLO ===") |
| 372 for key, value in validation.items(): | 360 for key, value in validation.items(): |
| 373 print(f"{key}: {value}") | 361 print(f"{key}: {value}") |
| 374 | 362 |
| 375 if(ARGS.ras_selector == True): | 363 |
| 376 generate_bounds_model(model, ras=ras_combined, output_folder=ARGS.output_path, | 364 generate_bounds_model(model, ras=ras_combined, output_folder=ARGS.output_path, |
| 377 save_models=ARGS.save_models, save_models_path=ARGS.save_models_path, | 365 save_models=ARGS.save_models, save_models_path=ARGS.save_models_path, |
| 378 save_models_format=ARGS.save_models_format) | 366 save_models_format=ARGS.save_models_format) |
| 379 class_assignments.to_csv(ARGS.cell_class, sep='\t', index=False) | 367 class_assignments.to_csv(ARGS.cell_class, sep='\t', index=False) |
| 380 else: | 368 |
| 381 generate_bounds_model(model, output_folder=ARGS.output_path, | |
| 382 save_models=ARGS.save_models, save_models_path=ARGS.save_models_path, | |
| 383 save_models_format=ARGS.save_models_format) | |
| 384 | 369 |
| 385 pass | 370 pass |
| 386 | 371 |
| 387 ############################################################################## | 372 ############################################################################## |
| 388 if __name__ == "__main__": | 373 if __name__ == "__main__": |
