| 410 | 1 import argparse | 
|  | 2 import utils.general_utils as utils | 
|  | 3 from typing import Optional, List | 
|  | 4 import os | 
|  | 5 import numpy as np | 
|  | 6 import pandas as pd | 
|  | 7 import cobra | 
|  | 8 import utils.CBS_backend as CBS_backend | 
|  | 9 from joblib import Parallel, delayed, cpu_count | 
|  | 10 from cobra.sampling import OptGPSampler | 
|  | 11 import sys | 
| 419 | 12 import utils.model_utils as model_utils | 
| 410 | 13 | 
|  | 14 | 
|  | 15 ################################# process args ############################### | 
|  | 16 def process_args(args :List[str] = None) -> argparse.Namespace: | 
|  | 17     """ | 
|  | 18     Processes command-line arguments. | 
|  | 19 | 
|  | 20     Args: | 
|  | 21         args (list): List of command-line arguments. | 
|  | 22 | 
|  | 23     Returns: | 
|  | 24         Namespace: An object containing parsed arguments. | 
|  | 25     """ | 
|  | 26     parser = argparse.ArgumentParser(usage = '%(prog)s [options]', | 
|  | 27                                      description = 'process some value\'s') | 
|  | 28 | 
|  | 29     parser.add_argument("-mo", "--model_upload", type = str, | 
|  | 30         help = "path to input file with custom rules, if provided") | 
|  | 31 | 
| 419 | 32     parser.add_argument("-mab", "--model_and_bounds", type = str, | 
|  | 33         choices = ['True', 'False'], | 
|  | 34         required = True, | 
|  | 35         help = "upload mode: True for model+bounds, False for complete models") | 
|  | 36 | 
|  | 37 | 
| 410 | 38     parser.add_argument('-ol', '--out_log', | 
|  | 39                         help = "Output log") | 
|  | 40 | 
|  | 41     parser.add_argument('-td', '--tool_dir', | 
|  | 42                         type = str, | 
|  | 43                         required = True, | 
|  | 44                         help = 'your tool directory') | 
|  | 45 | 
|  | 46     parser.add_argument('-in', '--input', | 
| 419 | 47                     required = True, | 
|  | 48                     type=str, | 
|  | 49                     help = 'input bounds files or complete model files') | 
| 410 | 50 | 
| 419 | 51     parser.add_argument('-ni', '--name', | 
| 410 | 52                         required = True, | 
|  | 53                         type=str, | 
|  | 54                         help = 'cell names') | 
|  | 55 | 
|  | 56     parser.add_argument('-a', '--algorithm', | 
|  | 57                         type = str, | 
|  | 58                         choices = ['OPTGP', 'CBS'], | 
|  | 59                         required = True, | 
|  | 60                         help = 'choose sampling algorithm') | 
|  | 61 | 
|  | 62     parser.add_argument('-th', '--thinning', | 
|  | 63                         type = int, | 
|  | 64                         default= 100, | 
|  | 65                         required=False, | 
|  | 66                         help = 'choose thinning') | 
|  | 67 | 
|  | 68     parser.add_argument('-ns', '--n_samples', | 
|  | 69                         type = int, | 
|  | 70                         required = True, | 
|  | 71                         help = 'choose how many samples') | 
|  | 72 | 
|  | 73     parser.add_argument('-sd', '--seed', | 
|  | 74                         type = int, | 
|  | 75                         required = True, | 
|  | 76                         help = 'seed') | 
|  | 77 | 
|  | 78     parser.add_argument('-nb', '--n_batches', | 
|  | 79                         type = int, | 
|  | 80                         required = True, | 
|  | 81                         help = 'choose how many batches') | 
|  | 82 | 
|  | 83     parser.add_argument('-ot', '--output_type', | 
|  | 84                         type = str, | 
|  | 85                         required = True, | 
|  | 86                         help = 'output type') | 
|  | 87 | 
|  | 88     parser.add_argument('-ota', '--output_type_analysis', | 
|  | 89                         type = str, | 
|  | 90                         required = False, | 
|  | 91                         help = 'output type analysis') | 
|  | 92 | 
|  | 93     parser.add_argument('-idop', '--output_path', | 
|  | 94                         type = str, | 
|  | 95                         default='flux_simulation', | 
|  | 96                         help = 'output path for maps') | 
|  | 97 | 
|  | 98     ARGS = parser.parse_args(args) | 
|  | 99     return ARGS | 
|  | 100 | 
|  | 101 ########################### warning ########################################### | 
|  | 102 def warning(s :str) -> None: | 
|  | 103     """ | 
|  | 104     Log a warning message to an output log file and print it to the console. | 
|  | 105 | 
|  | 106     Args: | 
|  | 107         s (str): The warning message to be logged and printed. | 
|  | 108 | 
|  | 109     Returns: | 
|  | 110       None | 
|  | 111     """ | 
|  | 112     with open(ARGS.out_log, 'a') as log: | 
|  | 113         log.write(s + "\n\n") | 
|  | 114     print(s) | 
|  | 115 | 
|  | 116 | 
|  | 117 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None: | 
|  | 118     dataset.index.name = 'Reactions' | 
|  | 119     dataset.to_csv(ARGS.output_path + "/" + name + ".csv", sep = '\t', index = keep_index) | 
|  | 120 | 
|  | 121 ############################ dataset input #################################### | 
|  | 122 def read_dataset(data :str, name :str) -> pd.DataFrame: | 
|  | 123     """ | 
|  | 124     Read a dataset from a CSV file and return it as a pandas DataFrame. | 
|  | 125 | 
|  | 126     Args: | 
|  | 127         data (str): Path to the CSV file containing the dataset. | 
|  | 128         name (str): Name of the dataset, used in error messages. | 
|  | 129 | 
|  | 130     Returns: | 
|  | 131         pandas.DataFrame: DataFrame containing the dataset. | 
|  | 132 | 
|  | 133     Raises: | 
|  | 134         pd.errors.EmptyDataError: If the CSV file is empty. | 
|  | 135         sys.exit: If the CSV file has the wrong format, the execution is aborted. | 
|  | 136     """ | 
|  | 137     try: | 
|  | 138         dataset = pd.read_csv(data, sep = '\t', header = 0, index_col=0, engine='python') | 
|  | 139     except pd.errors.EmptyDataError: | 
|  | 140         sys.exit('Execution aborted: wrong format of ' + name + '\n') | 
|  | 141     if len(dataset.columns) < 2: | 
|  | 142         sys.exit('Execution aborted: wrong format of ' + name + '\n') | 
|  | 143     return dataset | 
|  | 144 | 
|  | 145 | 
|  | 146 | 
|  | 147 def OPTGP_sampler(model:cobra.Model, model_name:str, n_samples:int=1000, thinning:int=100, n_batches:int=1, seed:int=0)-> None: | 
|  | 148     """ | 
|  | 149     Samples from the OPTGP (Optimal Global Perturbation) algorithm and saves the results to CSV files. | 
|  | 150 | 
|  | 151     Args: | 
|  | 152         model (cobra.Model): The COBRA model to sample from. | 
|  | 153         model_name (str): The name of the model, used in naming output files. | 
|  | 154         n_samples (int, optional): Number of samples per batch. Default is 1000. | 
|  | 155         thinning (int, optional): Thinning parameter for the sampler. Default is 100. | 
|  | 156         n_batches (int, optional): Number of batches to run. Default is 1. | 
|  | 157         seed (int, optional): Random seed for reproducibility. Default is 0. | 
|  | 158 | 
|  | 159     Returns: | 
|  | 160         None | 
|  | 161     """ | 
|  | 162 | 
|  | 163     for i in range(0, n_batches): | 
|  | 164         optgp = OptGPSampler(model, thinning, seed) | 
|  | 165         samples = optgp.sample(n_samples) | 
|  | 166         samples.to_csv(ARGS.output_path + "/" +  model_name + '_'+ str(i)+'_OPTGP.csv', index=False) | 
|  | 167         seed+=1 | 
|  | 168     samplesTotal = pd.DataFrame() | 
|  | 169     for i in range(0, n_batches): | 
|  | 170         samples_batch = pd.read_csv(ARGS.output_path + "/"  +  model_name + '_'+ str(i)+'_OPTGP.csv') | 
|  | 171         samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 
|  | 172 | 
|  | 173     write_to_file(samplesTotal.T, model_name, True) | 
|  | 174 | 
|  | 175     for i in range(0, n_batches): | 
|  | 176         os.remove(ARGS.output_path + "/" +   model_name + '_'+ str(i)+'_OPTGP.csv') | 
|  | 177     pass | 
|  | 178 | 
|  | 179 | 
|  | 180 def CBS_sampler(model:cobra.Model, model_name:str, n_samples:int=1000, n_batches:int=1, seed:int=0)-> None: | 
|  | 181     """ | 
|  | 182     Samples using the CBS (Constraint-based Sampling) algorithm and saves the results to CSV files. | 
|  | 183 | 
|  | 184     Args: | 
|  | 185         model (cobra.Model): The COBRA model to sample from. | 
|  | 186         model_name (str): The name of the model, used in naming output files. | 
|  | 187         n_samples (int, optional): Number of samples per batch. Default is 1000. | 
|  | 188         n_batches (int, optional): Number of batches to run. Default is 1. | 
|  | 189         seed (int, optional): Random seed for reproducibility. Default is 0. | 
|  | 190 | 
|  | 191     Returns: | 
|  | 192         None | 
|  | 193     """ | 
|  | 194 | 
|  | 195     df_FVA = cobra.flux_analysis.flux_variability_analysis(model,fraction_of_optimum=0).round(6) | 
|  | 196 | 
|  | 197     df_coefficients = CBS_backend.randomObjectiveFunction(model, n_samples*n_batches, df_FVA, seed=seed) | 
|  | 198 | 
|  | 199     for i in range(0, n_batches): | 
|  | 200         samples = pd.DataFrame(columns =[reaction.id for reaction in model.reactions], index = range(n_samples)) | 
|  | 201         try: | 
|  | 202             CBS_backend.randomObjectiveFunctionSampling(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], samples) | 
|  | 203         except Exception as e: | 
|  | 204             utils.logWarning( | 
|  | 205             "Warning: GLPK solver has failed for " + model_name + ". Trying with COBRA interface. Error:" + str(e), | 
|  | 206             ARGS.out_log) | 
|  | 207             CBS_backend.randomObjectiveFunctionSampling_cobrapy(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], | 
|  | 208                                                     samples) | 
|  | 209         utils.logWarning(ARGS.output_path + "/" +  model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log) | 
|  | 210         samples.to_csv(ARGS.output_path + "/" +  model_name + '_'+ str(i)+'_CBS.csv', index=False) | 
|  | 211 | 
|  | 212     samplesTotal = pd.DataFrame() | 
|  | 213     for i in range(0, n_batches): | 
|  | 214         samples_batch = pd.read_csv(ARGS.output_path + "/"  +  model_name + '_'+ str(i)+'_CBS.csv') | 
|  | 215         samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True) | 
|  | 216 | 
|  | 217     write_to_file(samplesTotal.T, model_name, True) | 
|  | 218 | 
|  | 219     for i in range(0, n_batches): | 
|  | 220         os.remove(ARGS.output_path + "/" + model_name + '_'+ str(i)+'_CBS.csv') | 
|  | 221     pass | 
|  | 222 | 
|  | 223 | 
| 419 | 224 | 
|  | 225 def model_sampler_with_bounds(model_input_original: cobra.Model, bounds_path: str, cell_name: str) -> List[pd.DataFrame]: | 
| 410 | 226     """ | 
| 419 | 227     MODE 1: Prepares the model with bounds from separate bounds file and performs sampling. | 
| 410 | 228 | 
|  | 229     Args: | 
|  | 230         model_input_original (cobra.Model): The original COBRA model. | 
|  | 231         bounds_path (str): Path to the CSV file containing the bounds dataset. | 
|  | 232         cell_name (str): Name of the cell, used to generate filenames for output. | 
|  | 233 | 
|  | 234     Returns: | 
|  | 235         List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results. | 
|  | 236     """ | 
|  | 237 | 
|  | 238     model_input = model_input_original.copy() | 
|  | 239     bounds_df = read_dataset(bounds_path, "bounds dataset") | 
| 419 | 240 | 
|  | 241     # Apply bounds to model | 
| 410 | 242     for rxn_index, row in bounds_df.iterrows(): | 
| 419 | 243         try: | 
|  | 244             model_input.reactions.get_by_id(rxn_index).lower_bound = row.lower_bound | 
|  | 245             model_input.reactions.get_by_id(rxn_index).upper_bound = row.upper_bound | 
|  | 246         except KeyError: | 
|  | 247             warning(f"Warning: Reaction {rxn_index} not found in model. Skipping.") | 
| 410 | 248 | 
| 419 | 249     return perform_sampling_and_analysis(model_input, cell_name) | 
|  | 250 | 
|  | 251 | 
|  | 252 def perform_sampling_and_analysis(model_input: cobra.Model, cell_name: str) -> List[pd.DataFrame]: | 
|  | 253     """ | 
|  | 254     Common function to perform sampling and analysis on a prepared model. | 
|  | 255 | 
|  | 256     Args: | 
|  | 257         model_input (cobra.Model): The prepared COBRA model with bounds applied. | 
|  | 258         cell_name (str): Name of the cell, used to generate filenames for output. | 
|  | 259 | 
|  | 260     Returns: | 
|  | 261         List[pd.DataFrame]: A list of DataFrames containing statistics and analysis results. | 
|  | 262     """ | 
| 410 | 263 | 
|  | 264     if ARGS.algorithm == 'OPTGP': | 
|  | 265         OPTGP_sampler(model_input, cell_name, ARGS.n_samples, ARGS.thinning, ARGS.n_batches, ARGS.seed) | 
|  | 266     elif ARGS.algorithm == 'CBS': | 
| 419 | 267         CBS_sampler(model_input, cell_name, ARGS.n_samples, ARGS.n_batches, ARGS.seed) | 
| 410 | 268 | 
|  | 269     df_mean, df_median, df_quantiles = fluxes_statistics(cell_name, ARGS.output_types) | 
|  | 270 | 
|  | 271     if("fluxes" not in ARGS.output_types): | 
| 419 | 272         os.remove(ARGS.output_path + "/" + cell_name + '.csv') | 
| 410 | 273 | 
| 419 | 274     returnList = [df_mean, df_median, df_quantiles] | 
| 410 | 275 | 
|  | 276     df_pFBA, df_FVA, df_sensitivity = fluxes_analysis(model_input, cell_name, ARGS.output_type_analysis) | 
|  | 277 | 
|  | 278     if("pFBA" in ARGS.output_type_analysis): | 
|  | 279         returnList.append(df_pFBA) | 
|  | 280     if("FVA" in ARGS.output_type_analysis): | 
|  | 281         returnList.append(df_FVA) | 
|  | 282     if("sensitivity" in ARGS.output_type_analysis): | 
|  | 283         returnList.append(df_sensitivity) | 
|  | 284 | 
|  | 285     return returnList | 
|  | 286 | 
|  | 287 def fluxes_statistics(model_name: str,  output_types:List)-> List[pd.DataFrame]: | 
|  | 288     """ | 
|  | 289     Computes statistics (mean, median, quantiles) for the fluxes. | 
|  | 290 | 
|  | 291     Args: | 
|  | 292         model_name (str): Name of the model, used in filename for input. | 
|  | 293         output_types (List[str]): Types of statistics to compute (mean, median, quantiles). | 
|  | 294 | 
|  | 295     Returns: | 
|  | 296         List[pd.DataFrame]: List of DataFrames containing mean, median, and quantiles statistics. | 
|  | 297     """ | 
|  | 298 | 
|  | 299     df_mean = pd.DataFrame() | 
|  | 300     df_median= pd.DataFrame() | 
|  | 301     df_quantiles= pd.DataFrame() | 
|  | 302 | 
|  | 303     df_samples = pd.read_csv(ARGS.output_path + "/"  +  model_name + '.csv', sep = '\t', index_col = 0).T | 
|  | 304     df_samples = df_samples.round(8) | 
|  | 305 | 
|  | 306     for output_type in output_types: | 
|  | 307         if(output_type == "mean"): | 
|  | 308             df_mean = df_samples.mean() | 
|  | 309             df_mean = df_mean.to_frame().T | 
|  | 310             df_mean = df_mean.reset_index(drop=True) | 
|  | 311             df_mean.index = [model_name] | 
|  | 312         elif(output_type == "median"): | 
|  | 313             df_median = df_samples.median() | 
|  | 314             df_median = df_median.to_frame().T | 
|  | 315             df_median = df_median.reset_index(drop=True) | 
|  | 316             df_median.index = [model_name] | 
|  | 317         elif(output_type == "quantiles"): | 
|  | 318             newRow = [] | 
|  | 319             cols = [] | 
|  | 320             for rxn in df_samples.columns: | 
|  | 321                 quantiles = df_samples[rxn].quantile([0.25, 0.50, 0.75]) | 
|  | 322                 newRow.append(quantiles[0.25]) | 
|  | 323                 cols.append(rxn + "_q1") | 
|  | 324                 newRow.append(quantiles[0.5]) | 
|  | 325                 cols.append(rxn + "_q2") | 
|  | 326                 newRow.append(quantiles[0.75]) | 
|  | 327                 cols.append(rxn + "_q3") | 
|  | 328             df_quantiles = pd.DataFrame(columns=cols) | 
|  | 329             df_quantiles.loc[0] = newRow | 
|  | 330             df_quantiles = df_quantiles.reset_index(drop=True) | 
|  | 331             df_quantiles.index = [model_name] | 
|  | 332 | 
|  | 333     return df_mean, df_median, df_quantiles | 
|  | 334 | 
|  | 335 def fluxes_analysis(model:cobra.Model,  model_name:str, output_types:List)-> List[pd.DataFrame]: | 
|  | 336     """ | 
|  | 337     Performs flux analysis including pFBA, FVA, and sensitivity analysis. | 
|  | 338 | 
|  | 339     Args: | 
|  | 340         model (cobra.Model): The COBRA model to analyze. | 
|  | 341         model_name (str): Name of the model, used in filenames for output. | 
|  | 342         output_types (List[str]): Types of analysis to perform (pFBA, FVA, sensitivity). | 
|  | 343 | 
|  | 344     Returns: | 
|  | 345         List[pd.DataFrame]: List of DataFrames containing pFBA, FVA, and sensitivity analysis results. | 
|  | 346     """ | 
|  | 347 | 
|  | 348     df_pFBA = pd.DataFrame() | 
|  | 349     df_FVA= pd.DataFrame() | 
|  | 350     df_sensitivity= pd.DataFrame() | 
|  | 351 | 
|  | 352     for output_type in output_types: | 
|  | 353         if(output_type == "pFBA"): | 
|  | 354             model.objective = "Biomass" | 
|  | 355             solution = cobra.flux_analysis.pfba(model) | 
|  | 356             fluxes = solution.fluxes | 
| 419 | 357             df_pFBA.loc[0,[rxn.id for rxn in model.reactions]] = fluxes.tolist() | 
| 410 | 358             df_pFBA = df_pFBA.reset_index(drop=True) | 
|  | 359             df_pFBA.index = [model_name] | 
|  | 360             df_pFBA = df_pFBA.astype(float).round(6) | 
|  | 361         elif(output_type == "FVA"): | 
|  | 362             fva = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8) | 
|  | 363             columns = [] | 
|  | 364             for rxn in fva.index.to_list(): | 
|  | 365                 columns.append(rxn + "_min") | 
|  | 366                 columns.append(rxn + "_max") | 
|  | 367             df_FVA= pd.DataFrame(columns = columns) | 
|  | 368             for index_rxn, row in fva.iterrows(): | 
|  | 369                 df_FVA.loc[0, index_rxn+ "_min"] = fva.loc[index_rxn, "minimum"] | 
|  | 370                 df_FVA.loc[0, index_rxn+ "_max"] = fva.loc[index_rxn, "maximum"] | 
|  | 371             df_FVA = df_FVA.reset_index(drop=True) | 
|  | 372             df_FVA.index = [model_name] | 
|  | 373             df_FVA = df_FVA.astype(float).round(6) | 
|  | 374         elif(output_type == "sensitivity"): | 
|  | 375             model.objective = "Biomass" | 
|  | 376             solution_original = model.optimize().objective_value | 
|  | 377             reactions = model.reactions | 
|  | 378             single = cobra.flux_analysis.single_reaction_deletion(model) | 
|  | 379             newRow = [] | 
|  | 380             df_sensitivity = pd.DataFrame(columns = [rxn.id for rxn in reactions], index = [model_name]) | 
|  | 381             for rxn in reactions: | 
|  | 382                 newRow.append(single.knockout[rxn.id].growth.values[0]/solution_original) | 
|  | 383             df_sensitivity.loc[model_name] = newRow | 
|  | 384             df_sensitivity = df_sensitivity.astype(float).round(6) | 
|  | 385     return df_pFBA, df_FVA, df_sensitivity | 
|  | 386 | 
|  | 387 ############################# main ########################################### | 
|  | 388 def main(args :List[str] = None) -> None: | 
|  | 389     """ | 
|  | 390     Initializes everything and sets the program in motion based on the fronted input arguments. | 
|  | 391 | 
|  | 392     Returns: | 
|  | 393         None | 
|  | 394     """ | 
|  | 395 | 
| 419 | 396     num_processors = max(1, cpu_count() - 1) | 
| 410 | 397 | 
|  | 398     global ARGS | 
|  | 399     ARGS = process_args(args) | 
|  | 400 | 
|  | 401     if not os.path.exists(ARGS.output_path): | 
|  | 402         os.makedirs(ARGS.output_path) | 
| 419 | 403 | 
|  | 404     #ARGS.bounds = ARGS.input.split(",") | 
|  | 405     #ARGS.bounds_name = ARGS.name.split(",") | 
|  | 406     #ARGS.output_types = ARGS.output_type.split(",") | 
|  | 407     #ARGS.output_type_analysis = ARGS.output_type_analysis.split(",") | 
|  | 408 | 
|  | 409     # --- Normalize inputs (the tool may pass comma-separated --input and either --name or --names) --- | 
| 421 | 410     ARGS.input_files = ARGS.input.split(",") if ARGS.input else [] | 
| 419 | 411     ARGS.file_names = ARGS.name.split(",") | 
|  | 412     # output types (required) -> list | 
| 421 | 413     ARGS.output_types = ARGS.output_type.split(",") if ARGS.output_type else [] | 
| 419 | 414     # optional analysis output types -> list or empty | 
| 421 | 415     ARGS.output_type_analysis = ARGS.output_type_analysis.split(",") if ARGS.output_type_analysis else [] | 
| 419 | 416 | 
| 421 | 417     print("=== INPUT FILES ===") | 
| 422 | 418     print(f"{ARGS.input_files}") | 
|  | 419     print(f"{ARGS.file_names}") | 
|  | 420     print(f"{ARGS.output_type}") | 
|  | 421     print(f"{ARGS.output_types}") | 
|  | 422     print(f"{ARGS.output_type_analysis}") | 
| 410 | 423 | 
| 419 | 424     if ARGS.model_and_bounds == "True": | 
|  | 425         # MODE 1: Model + bounds (separate files) | 
|  | 426         print("=== MODE 1: Model + Bounds (separate files) ===") | 
|  | 427 | 
|  | 428         # Load base model | 
|  | 429         if not ARGS.model_upload: | 
|  | 430             sys.exit("Error: model_upload is required for Mode 1") | 
| 410 | 431 | 
| 419 | 432         base_model = model_utils.build_cobra_model_from_csv(ARGS.model_upload) | 
| 410 | 433 | 
| 419 | 434         validation = model_utils.validate_model(base_model) | 
|  | 435 | 
|  | 436         print("\n=== VALIDAZIONE MODELLO ===") | 
|  | 437         for key, value in validation.items(): | 
|  | 438             print(f"{key}: {value}") | 
|  | 439 | 
|  | 440         #Set solver verbosity to 1 to see warning and error messages only. | 
|  | 441         base_model.solver.configuration.verbosity = 1 | 
| 410 | 442 | 
| 419 | 443                 # Process each bounds file with the base model | 
|  | 444         results = Parallel(n_jobs=num_processors)( | 
|  | 445             delayed(model_sampler_with_bounds)(base_model, bounds_file, cell_name) | 
|  | 446             for bounds_file, cell_name in zip(ARGS.input_files, ARGS.file_names) | 
|  | 447         ) | 
| 410 | 448 | 
| 419 | 449     else: | 
|  | 450         # MODE 2: Multiple complete models | 
|  | 451         print("=== MODE 2: Multiple complete models ===") | 
|  | 452 | 
|  | 453         # Process each complete model file | 
|  | 454         results = Parallel(n_jobs=num_processors)( | 
|  | 455             delayed(perform_sampling_and_analysis)(model_utils.build_cobra_model_from_csv(model_file), cell_name) | 
|  | 456             for model_file, cell_name in zip(ARGS.input_files, ARGS.file_names) | 
|  | 457         ) | 
| 410 | 458 | 
|  | 459 | 
|  | 460     all_mean = pd.concat([result[0] for result in results], ignore_index=False) | 
|  | 461     all_median = pd.concat([result[1] for result in results], ignore_index=False) | 
|  | 462     all_quantiles = pd.concat([result[2] for result in results], ignore_index=False) | 
|  | 463 | 
|  | 464     if("mean" in ARGS.output_types): | 
|  | 465         all_mean = all_mean.fillna(0.0) | 
|  | 466         all_mean = all_mean.sort_index() | 
|  | 467         write_to_file(all_mean.T, "mean", True) | 
|  | 468 | 
|  | 469     if("median" in ARGS.output_types): | 
|  | 470         all_median = all_median.fillna(0.0) | 
|  | 471         all_median = all_median.sort_index() | 
|  | 472         write_to_file(all_median.T, "median", True) | 
|  | 473 | 
|  | 474     if("quantiles" in ARGS.output_types): | 
|  | 475         all_quantiles = all_quantiles.fillna(0.0) | 
|  | 476         all_quantiles = all_quantiles.sort_index() | 
|  | 477         write_to_file(all_quantiles.T, "quantiles", True) | 
|  | 478 | 
|  | 479     index_result = 3 | 
|  | 480     if("pFBA" in ARGS.output_type_analysis): | 
|  | 481         all_pFBA = pd.concat([result[index_result] for result in results], ignore_index=False) | 
|  | 482         all_pFBA = all_pFBA.sort_index() | 
|  | 483         write_to_file(all_pFBA.T, "pFBA", True) | 
|  | 484         index_result+=1 | 
|  | 485     if("FVA" in ARGS.output_type_analysis): | 
|  | 486         all_FVA= pd.concat([result[index_result] for result in results], ignore_index=False) | 
|  | 487         all_FVA = all_FVA.sort_index() | 
|  | 488         write_to_file(all_FVA.T, "FVA", True) | 
|  | 489         index_result+=1 | 
|  | 490     if("sensitivity" in ARGS.output_type_analysis): | 
|  | 491         all_sensitivity = pd.concat([result[index_result] for result in results], ignore_index=False) | 
|  | 492         all_sensitivity = all_sensitivity.sort_index() | 
|  | 493         write_to_file(all_sensitivity.T, "sensitivity", True) | 
|  | 494 | 
|  | 495     pass | 
|  | 496 | 
|  | 497 ############################################################################## | 
|  | 498 if __name__ == "__main__": | 
|  | 499     main() |