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