annotate marea_2/ras_to_bounds.py @ 111:b5b7e46be22a draft

Uploaded
author luca_milaz
date Sun, 21 Jul 2024 18:53:55 +0000 (6 months ago)
parents d9b29c64efc9
children 79c079fb4e50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
1 import argparse
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
2 import utils.general_utils as utils
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
3 from typing import Optional, List
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
4 import os
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
5 import numpy as np
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
6 import pandas as pd
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
7 import cobra
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
8 from joblib import Parallel, delayed, cpu_count
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
9 import sys
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
10
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
11 ################################# process args ###############################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
12 def process_args(args :List[str]) -> argparse.Namespace:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
13 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
14 Processes command-line arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
15
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
16 Args:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
17 args (list): List of command-line arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
18
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
19 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
20 Namespace: An object containing parsed arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
21 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
22 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
23 description = 'process some value\'s')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
24
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
25 parser.add_argument(
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
26 '-ms', '--model_selector',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
27 type = utils.Model, default = utils.Model.ENGRO2, choices = [utils.Model.ENGRO2, utils.Model.Custom],
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
28 help = 'chose which type of model you want use')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
29
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
30 parser.add_argument("-mo", "--model", type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
31 help = "path to input file with custom rules, if provided")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
32
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
33 parser.add_argument("-mn", "--model_name", type = str, help = "custom mode name")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
34
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
35 parser.add_argument(
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
36 '-mes', '--medium_selector',
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
37 default = "allOpen",
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
38 help = 'chose which type of medium you want use')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
39
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
40 parser.add_argument("-meo", "--medium", type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
41 help = "path to input file with custom medium, if provided")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
42
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
43 parser.add_argument('-ol', '--out_log',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
44 help = "Output log")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
45
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
46 parser.add_argument('-td', '--tool_dir',
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
47 type = str,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
48 required = True,
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
49 help = 'your tool directory')
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
50
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
51 parser.add_argument('-ir', '--input_ras',
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
52 required = False,
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
53 help = 'input ras')
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
54
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
55 parser.add_argument('-rs', '--ras_selector',
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
56 required = True,
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
57 type=bool,
98
10f8540eeec5 Uploaded
luca_milaz
parents: 88
diff changeset
58 help = 'ras selector')
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
59
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
60 ARGS = parser.parse_args()
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
61 return ARGS
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
62
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
63 ########################### warning ###########################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
64 def warning(s :str) -> None:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
65 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
66 Log a warning message to an output log file and print it to the console.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
67
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
68 Args:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
69 s (str): The warning message to be logged and printed.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
70
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
71 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
72 None
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
73 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
74 with open(ARGS.out_log, 'a') as log:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
75 log.write(s + "\n\n")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
76 print(s)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
77
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
78 def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
79 model_new = model.copy()
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
80 rxns_ids = []
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
81 for rxn in model.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
82 rxns_ids.append(rxn.id)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
83 for reaction in medium.keys():
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
84 if(medium[reaction] != None):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
85 model_new.reactions.get_by_id(reaction).lower_bound=-float(medium[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
86 df_FVA = cobra.flux_analysis.flux_variability_analysis(model_new,fraction_of_optimum=0,processes=1).round(8)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
87 for reaction in rxns_ids:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
88 model_new.reactions.get_by_id(reaction).lower_bound=float(df_FVA.loc[reaction,"minimum"])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
89 model_new.reactions.get_by_id(reaction).upper_bound=float(df_FVA.loc[reaction,"maximum"])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
90
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
91 if(ras is not None):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
92 for reaction in rxns_ids:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
93 if reaction in ras.keys():
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
94 lower_bound=model_new.reactions.get_by_id(reaction).lower_bound
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
95 upper_bound=model_new.reactions.get_by_id(reaction).upper_bound
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
96 valMax=float((upper_bound)*ras[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
97 valMin=float((lower_bound)*ras[reaction])
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
98 if upper_bound!=0 and lower_bound==0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
99 model_new.reactions.get_by_id(reaction).upper_bound=valMax
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
100 if upper_bound==0 and lower_bound!=0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
101 model_new.reactions.get_by_id(reaction).lower_bound=valMin
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
102 if upper_bound!=0 and lower_bound!=0:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
103 model_new.reactions.get_by_id(reaction).lower_bound=valMin
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
104 model_new.reactions.get_by_id(reaction).upper_bound=valMax
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
105 rxns = []
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
106 for reaction in model_new.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
107 rxns.append(reaction.id)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
108
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
109 bounds = pd.DataFrame(columns = ["lower_bound", "upper_bound"], index=rxns)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
110
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
111 for reaction in model.reactions:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
112 bounds.loc[reaction.id] = [reaction.lower_bound, reaction.upper_bound]
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
113 return bounds
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
114
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
115
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
116 ############################# main ###########################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
117 def main() -> None:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
118 """
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
119 Initializes everything and sets the program in motion based on the fronted input arguments.
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
120
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
121 Returns:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
122 None
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
123 """
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
124 if not os.path.exists('ras_to_bounds'):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
125 os.makedirs('ras_to_bounds')
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
126
111
b5b7e46be22a Uploaded
luca_milaz
parents: 107
diff changeset
127 global ARGS
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
128
111
b5b7e46be22a Uploaded
luca_milaz
parents: 107
diff changeset
129 ARGS.output_folder = 'ras_to_bounds/'
b5b7e46be22a Uploaded
luca_milaz
parents: 107
diff changeset
130
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
131 ARGS = process_args(sys.argv)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
132
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
133 ARGS.output_types = ARGS.output_type.split(",")
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
134
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
135 boundsPath = utils.FilePath("bounds", ".csv", prefix = ARGS.output_folder)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
136 mediumPath = utils.FilePath("medium", ".csv", prefix = ARGS.output_folder)
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
137
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
138 if(ARGS.ras_selector == True):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
139 ras = pd.read_table(ARGS.input_ras, header=0, sep=r'\s+', index_col = 0).T
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
140 ras.replace("None", None, inplace=True)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
141 ras = ras.astype(float)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
142
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
143 model_type :utils.Model = ARGS.model_selector
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
144 if model_type is utils.Model.Custom:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
145 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
146 else:
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
147 model = model_type.getCOBRAmodel(toolDir=ARGS.tool_dir)
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
148
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
149 if(ARGS.medium_selector == "Custom"):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
150 medium = pd.read_csv(ARGS.input_medium, index_col = 0)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
151 medium = medium.astype(float)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
152 medium = medium['medium'].to_dict()
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
153 else:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
154 df_mediums = pd.read_csv(ARGS.tool_dir + "/local/medium/medium.csv", index_col = 0)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
155 medium = df_mediums[[ARGS.medium_selector]]
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
156 medium = medium[ARGS.medium_selector].to_dict()
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
157
107
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
158 if(ARGS.ras_selector == True):
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
159 bounds = generate_bounds(model, medium, ras = ras)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
160 else:
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
161 bounds = generate_bounds(model, medium)
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
162
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
163 bounds.to_csv(boundsPath.show())
d9b29c64efc9 Uploaded
luca_milaz
parents: 98
diff changeset
164 medium.to_csv(mediumPath.show())
88
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
165 pass
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
166
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
167 ##############################################################################
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
168 if __name__ == "__main__":
ae7c31133059 Uploaded
luca_milaz
parents:
diff changeset
169 main()