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