annotate marea_2/ras_to_bounds.py @ 208:d69e18da32d3 draft

Uploaded
author francesco_lapi
date Wed, 31 Jul 2024 15:46:05 +0000
parents 8aefca7975d9
children 7a098cb82865
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
179
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
1 import argparse
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
2 import utils.general_utils as utils
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
3 from typing import Optional, List
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
4 import os
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
5 import numpy as np
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
6 import pandas as pd
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
7 import cobra
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
8 import sys
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
9 import csv
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
10 from joblib import Parallel, delayed, cpu_count
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
11
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
12 ################################# process args ###############################
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
13 def process_args(args :List[str]) -> argparse.Namespace:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
14 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
15 Processes command-line arguments.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
16
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
17 Args:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
18 args (list): List of command-line arguments.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
19
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
20 Returns:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
21 Namespace: An object containing parsed arguments.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
22 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
23 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
24 description = 'process some value\'s')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
25
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
26 parser.add_argument(
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
27 '-ms', '--model_selector',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
28 type = utils.Model, default = utils.Model.ENGRO2, choices = [utils.Model.ENGRO2, utils.Model.Custom],
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
29 help = 'chose which type of model you want use')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
30
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
31 parser.add_argument("-mo", "--model", type = str,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
32 help = "path to input file with custom rules, if provided")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
33
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
34 parser.add_argument("-mn", "--model_name", type = str, help = "custom mode name")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
35
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
36 parser.add_argument(
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
37 '-mes', '--medium_selector',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
38 default = "allOpen",
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
39 help = 'chose which type of medium you want use')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
40
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
41 parser.add_argument("-meo", "--medium", type = str,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
42 help = "path to input file with custom medium, if provided")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
43
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
44 parser.add_argument('-ol', '--out_log',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
45 help = "Output log")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
46
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
47 parser.add_argument('-td', '--tool_dir',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
48 type = str,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
49 required = True,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
50 help = 'your tool directory')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
51
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
52 parser.add_argument('-ir', '--input_ras',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
53 type=str,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
54 required = False,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
55 help = 'input ras')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
56
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
57 parser.add_argument('-rs', '--ras_selector',
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
58 required = True,
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
59 type=utils.Bool("using_RAS"),
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
60 help = 'ras selector')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
61
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
62 ARGS = parser.parse_args()
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
63 return ARGS
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
64
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
65 ########################### warning ###########################################
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
66 def warning(s :str) -> None:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
67 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
68 Log a warning message to an output log file and print it to the console.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
69
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
70 Args:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
71 s (str): The warning message to be logged and printed.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
72
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
73 Returns:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
74 None
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
75 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
76 with open(ARGS.out_log, 'a') as log:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
77 log.write(s + "\n\n")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
78 print(s)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
79
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
80 ############################ dataset input ####################################
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
81 def read_dataset(data :str, name :str) -> pd.DataFrame:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
82 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
83 Read a dataset from a CSV file and return it as a pandas DataFrame.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
84
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
85 Args:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
86 data (str): Path to the CSV file containing the dataset.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
87 name (str): Name of the dataset, used in error messages.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
88
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
89 Returns:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
90 pandas.DataFrame: DataFrame containing the dataset.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
91
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
92 Raises:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
93 pd.errors.EmptyDataError: If the CSV file is empty.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
94 sys.exit: If the CSV file has the wrong format, the execution is aborted.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
95 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
96 try:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
97 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
98 except pd.errors.EmptyDataError:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
99 sys.exit('Execution aborted: wrong format of ' + name + '\n')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
100 if len(dataset.columns) < 2:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
101 sys.exit('Execution aborted: wrong format of ' + name + '\n')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
102 return dataset
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
103
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
104
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
105 def apply_ras_bounds(model, ras_row, rxns_ids):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
106 for reaction in rxns_ids:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
107 if reaction in ras_row.index and pd.notna(ras_row[reaction]):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
108 rxn = model.reactions.get_by_id(reaction)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
109 scaling_factor = ras_row[reaction]
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
110 rxn.lower_bound *= scaling_factor
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
111 rxn.upper_bound *= scaling_factor
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
112
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
113 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
114 model_new = model.copy()
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
115 apply_ras_bounds(model_new, ras_row, rxns_ids)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
116 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
181
8aefca7975d9 Uploaded
luca_milaz
parents: 179
diff changeset
117 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True)
179
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
118
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
119 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
120 rxns_ids = [rxn.id for rxn in model.reactions]
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
121
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
122 # Set medium conditions
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
123 for reaction, value in medium.items():
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
124 if value is not None:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
125 model.reactions.get_by_id(reaction).lower_bound = -float(value)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
126
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
127 # Perform Flux Variability Analysis (FVA)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
128 df_FVA = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
129
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
130 # Set FVA bounds
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
131 for reaction in rxns_ids:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
132 rxn = model.reactions.get_by_id(reaction)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
133 rxn.lower_bound = float(df_FVA.loc[reaction, "minimum"])
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
134 rxn.upper_bound = float(df_FVA.loc[reaction, "maximum"])
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
135
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
136 if ras is not None:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
137 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows())
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
138 else:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
139 model_new = model.copy()
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
140 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
141 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
181
8aefca7975d9 Uploaded
luca_milaz
parents: 179
diff changeset
142 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True)
179
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
143
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
144
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
145 ############################# main ###########################################
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
146 def main() -> None:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
147 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
148 Initializes everything and sets the program in motion based on the fronted input arguments.
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
149
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
150 Returns:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
151 None
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
152 """
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
153 if not os.path.exists('ras_to_bounds'):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
154 os.makedirs('ras_to_bounds')
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
155
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
156
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
157 global ARGS
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
158 ARGS = process_args(sys.argv)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
159
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
160 ARGS.output_folder = 'ras_to_bounds/'
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
161
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
162 if(ARGS.ras_selector == True):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
163 ras = read_dataset(ARGS.input_ras, "ras dataset")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
164 ras.replace("None", None, inplace=True)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
165 ras.set_index("Reactions", drop=True, inplace=True)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
166 ras = ras.T
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
167 ras = ras.astype(float)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
168
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
169 model_type :utils.Model = ARGS.model_selector
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
170 if model_type is utils.Model.Custom:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
171 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
172 else:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
173 model = model_type.getCOBRAmodel(toolDir=ARGS.tool_dir)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
174
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
175 if(ARGS.medium_selector == "Custom"):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
176 medium = read_dataset(ARGS.medium, "medium dataset")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
177 medium.set_index(medium.columns[0], inplace=True)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
178 medium = medium.astype(float)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
179 medium = medium[medium.columns[0]].to_dict()
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
180 else:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
181 df_mediums = pd.read_csv(ARGS.tool_dir + "/local/medium/medium.csv", index_col = 0)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
182 ARGS.medium_selector = ARGS.medium_selector.replace("_", " ")
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
183 medium = df_mediums[[ARGS.medium_selector]]
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
184 medium = medium[ARGS.medium_selector].to_dict()
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
185
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
186 if(ARGS.ras_selector == True):
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
187 generate_bounds(model, medium, ras = ras, output_folder=ARGS.output_folder)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
188 else:
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
189 generate_bounds(model, medium, output_folder=ARGS.output_folder)
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
190
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
191 pass
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
192
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
193 ##############################################################################
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
194 if __name__ == "__main__":
11645e643642 Uploaded
francesco_lapi
parents:
diff changeset
195 main()