annotate COBRAxy/flux_simulation.py @ 162:b5a26d1c4fdc draft

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