comparison COBRAxy/src/flux_simulation.py @ 547:73f2f7e2be17 draft

Uploaded
author francesco_lapi
date Tue, 28 Oct 2025 10:44:07 +0000
parents 01147e83f43c
children 5aef7b860706
comparison
equal deleted inserted replaced
546:01147e83f43c 547:73f2f7e2be17
344 if os.path.exists(batch_filename): 344 if os.path.exists(batch_filename):
345 os.remove(batch_filename) 345 os.remove(batch_filename)
346 346
347 347
348 348
349 def model_sampler_with_bounds(model_input_original: cobra.Model, bounds_path: str, cell_name: str) -> List[pd.DataFrame]: 349 def model_sampler_with_bounds(model_path: str, bounds_path: str, cell_name: str) -> List[pd.DataFrame]:
350 """ 350 """
351 MODE 1: Prepares the model with bounds from separate bounds file and performs sampling. 351 MODE 1: Loads model from file, applies bounds from separate bounds file and performs sampling.
352 352
353 Args: 353 Args:
354 model_input_original (cobra.Model): The original COBRA model. 354 model_path (str): Path to the tabular model file.
355 bounds_path (str): Path to the CSV file containing the bounds dataset. 355 bounds_path (str): Path to the CSV file containing the bounds dataset.
356 cell_name (str): Name of the cell, used to generate filenames for output. 356 cell_name (str): Name of the cell, used to generate filenames for output.
357 357
358 Returns: 358 Returns:
359 List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results. 359 List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results.
360 """ 360 """
361 361
362 model_input = model_input_original.copy() 362 model_input = model_utils.build_cobra_model_from_csv(model_path)
363
364 validation = model_utils.validate_model(model_input)
365
366 print("\n=== MODEL VALIDATION ===")
367 for key, value in validation.items():
368 print(f"{key}: {value}")
369
370 model_input.solver.configuration.verbosity = 1
371
363 bounds_df = read_dataset(bounds_path, "bounds dataset") 372 bounds_df = read_dataset(bounds_path, "bounds dataset")
364 373
365 # Apply bounds to model 374 # Apply bounds to model
366 for rxn_index, row in bounds_df.iterrows(): 375 for rxn_index, row in bounds_df.iterrows():
367 try: 376 try:
371 warning(f"Warning: Reaction {rxn_index} not found in model. Skipping.") 380 warning(f"Warning: Reaction {rxn_index} not found in model. Skipping.")
372 381
373 return perform_sampling_and_analysis(model_input, cell_name) 382 return perform_sampling_and_analysis(model_input, cell_name)
374 383
375 384
376 def perform_sampling_and_analysis(model_input: cobra.Model, cell_name: str) -> List[pd.DataFrame]: 385 def perform_sampling_and_analysis(model_path: str, cell_name: str) -> List[pd.DataFrame]:
377 """ 386 """
378 Common function to perform sampling and analysis on a prepared model. 387 Common function to perform sampling and analysis on a prepared model.
379 388
380 Args: 389 Args:
381 model_input (cobra.Model): The prepared COBRA model with bounds applied. 390 model_path (str): Path to the tabular model file. model with bounds applied.
382 cell_name (str): Name of the cell, used to generate filenames for output. 391 cell_name (str): Name of the cell, used to generate filenames for output.
383 392
384 Returns: 393 Returns:
385 List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results. 394 List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results.
386 """ 395 """
387 396
388 returnList = [] 397 returnList = []
398
399 model_input = model_utils.build_cobra_model_from_csv(model_path)
389 400
390 if ARGS.sampling_enabled == "true": 401 if ARGS.sampling_enabled == "true":
391 402
392 if ARGS.algorithm == 'OPTGP': 403 if ARGS.algorithm == 'OPTGP':
393 OPTGP_sampler(model_input, cell_name, ARGS.n_samples, ARGS.thinning, ARGS.n_batches, ARGS.seed) 404 OPTGP_sampler(model_input, cell_name, ARGS.n_samples, ARGS.thinning, ARGS.n_batches, ARGS.seed)
560 571
561 # Load base model 572 # Load base model
562 if not ARGS.model_upload: 573 if not ARGS.model_upload:
563 sys.exit("Error: model_upload is required for Mode 1") 574 sys.exit("Error: model_upload is required for Mode 1")
564 575
565 base_model = model_utils.build_cobra_model_from_csv(ARGS.model_upload)
566
567 validation = model_utils.validate_model(base_model)
568
569 print("\n=== MODEL VALIDATION ===")
570 for key, value in validation.items():
571 print(f"{key}: {value}")
572
573 # Set solver verbosity to 1 to see warning and error messages only.
574 base_model.solver.configuration.verbosity = 1
575
576 # Process each bounds file with the base model 576 # Process each bounds file with the base model
577 results = Parallel(n_jobs=num_processors)( 577 results = Parallel(n_jobs=num_processors)(
578 delayed(model_sampler_with_bounds)(base_model, bounds_file, cell_name) 578 delayed(model_sampler_with_bounds)(ARGS.model_upload, bounds_file, cell_name)
579 for bounds_file, cell_name in zip(ARGS.input_files, ARGS.file_names) 579 for bounds_file, cell_name in zip(ARGS.input_files, ARGS.file_names)
580 ) 580 )
581 581
582 else: 582 else:
583 # MODE 2: Multiple complete models 583 # MODE 2: Multiple complete models
584 print("=== MODE 2: Multiple complete models ===") 584 print("=== MODE 2: Multiple complete models ===")
585 585
586 # Process each complete model file 586 # Process each complete model file
587 results = Parallel(n_jobs=num_processors)( 587 results = Parallel(n_jobs=num_processors)(
588 delayed(perform_sampling_and_analysis)(model_utils.build_cobra_model_from_csv(model_file), cell_name) 588 delayed(perform_sampling_and_analysis)(model_file, cell_name)
589 for model_file, cell_name in zip(ARGS.input_files, ARGS.file_names) 589 for model_file, cell_name in zip(ARGS.input_files, ARGS.file_names)
590 ) 590 )
591 591
592 # Handle sampling outputs (only if sampling was performed) 592 # Handle sampling outputs (only if sampling was performed)
593 if perform_sampling: 593 if perform_sampling: