annotate cobraxy-9688ad27287b/COBRAxy/flux_simulation.py @ 91:f4f93df8c221 draft

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