-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_main.py
281 lines (247 loc) · 13.3 KB
/
old_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#-*- coding:utf-8 -*-
'''
Author: wangruoyu, [email protected]
Date: 2023-04-20 06:01:56
LastEditors: wangruoyu
LastEditTime: 2023-04-20 06:49:23
Description: file content
FilePath: /mqc/start.py
'''
import argparse
import os, sys
from pathlib import Path
import time
import csv
from multiprocessing.pool import Pool
import fcntl
from mqc.config import Config
from mqc.utils import *
from mqc.control.preprocessing_control import Preprocess
from mqc.control.model_control import ModelPreprocess
from mqc.control.initial_control import InitialPreprocess
from mqc.control.nadh_control import Nadhs
from mqc.control.atp_control import Atps
from mqc.control.net_control import Nets
from mqc.control.yield_control import Yields
from mqc.control.biomass_control import Biomasses
from mqc.control.quantitative_control import Quantitative
from mqc.control.yield_control2 import Yields2
from mqc.control.check_control import Check
from mqc.control.rules_control import Rules
# parser = argparse.ArgumentParser()
# parser.add_argument('--file', type=str, default='', help='model file directory')
# parser.add_argument('-o','--outputdir', type=str, default='./', help='result file directory')
# parser.add_argument('--types', type=str, default='', help='model control type')
# parser.add_argument('--rxns_job', type=list, default='', help='List of user-modified reactions')
# args = parser.parse_args()
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]
# print('FILE:',FILE,'ROOT:', ROOT)
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
class ModelCheck():
"""
Obtain the total output information of the model quality control.
"""
def __init__(self, model_file:str,output_dir:str):
"""
define output dictionary.
"""
# input model
self.model_file = model_file
self.output_dir = self.create_outputdir(output_dir)
self.cfg = Config(self.output_dir)
self.model_control_info = {}
self.model_check_info = {}
self.model_check_info['boundary_information'] = {}
self.all_data = []
self.model_check_info["check_reducing_equivalents_production"] = {}
self.model_check_info["check_energy_production"] = {}
self.model_check_info["check_metabolite_production"] = {}
self.model_check_info["check_metabolite_yield"] = {}
self.model_check_info["check_biomass_production"] = {}
self.model_control_info['boundary_information'] = {}
self.model_control_info["check_reducing_equivalents_production"] = {}
self.model_control_info["check_energy_production"] = {}
self.model_control_info["check_metabolite_production"] = {}
self.model_control_info["check_metabolite_yield"] = {}
self.model_control_info["check_biomass_production"] = {}
# self.model_control_info["quantitative_comparison_before_and_after_correction"] = {}
def create_outputdir(self,output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
return output_dir
def model_check(self):
""""""
t1 = time.time()
headers = ['model', 'reducing_power', 'energy', 'metabolite', 'yield', 'biomass']
controler = Preprocess(self.model_file,self.cfg)
if not controler.model:
model_control_info = change_model_control_info(self.model_file, self.model_control_info)
model_control_infos = write_result2(model_control_info,self.cfg)
t2 = time.time()
print("Total time: ", t2-t1)
return model_control_infos
model_pre_process = ModelPreprocess(self.cfg)
model_pre_process.get_model_info(controler)
checks = Check(self.cfg)
all_data = checks.check_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, self.model_check_info)
print(all_data)
# # 创建一个数据框(DataFrame)
# df = pd.DataFrame([all_data], columns=headers)
# # 尝试读取现有文件
# try:
# existing_df = pd.read_excel('/home/dengxiao/mqc/tmp/web_CARVEME_COMMEN2/output/output5.xlsx', sheet_name='Sheet1')
# # 将新的数据追加到现有数据框
# existing_df = existing_df.append(df, ignore_index=True)
# except FileNotFoundError:
# # 如果文件不存在,直接使用新数据框
# existing_df = df
# # 将数据框写入Excel文件
# existing_df.to_excel('/home/dengxiao/mqc/tmp/web_CARVEME_COMMEN2/output/output5.xlsx', index=False, header=True, sheet_name='Sheet1')
del self.model_check_info['boundary_information']
model_control_infos = write_result(self.model_check_info,self.cfg)
t2 = time.time()
print("Total time: ", t2-t1)
return model_control_infos, all_data
def model_check2(self):
"""
The overall process of model quality control.
"""
t1 = time.time()
model_control_infos, final_model = '', ''
self.model_check_info['boundary_information'] = {}
controler = Preprocess(self.model_file,self.cfg)
if not controler.model:
model_control_info = change_model_control_info(self.model_file, self.model_control_info)
model_control_infos = write_result2(model_control_info,self.cfg)
final_model = self.model_file
t2 = time.time()
print("Total time: ", t2-t1)
return model_control_infos
model_pre_process = ModelPreprocess(self.cfg)
model_pre_process.get_model_info(controler)
model_pre_process.model_info["model_file"] = self.model_file
try:
initial_pre_process = InitialPreprocess(self.cfg)
initial_pre_process.initial_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, self.model_check_info)
# quantitative = Quantitative(self.cfg)
# quantitative.get_initial(model_pre_process.model_info, controler.check_model, self.model_control_info)
# model_control_infos = write_result(self.model_control_info,self.cfg)
# final_model = write_final_model(controler.model,self.cfg)
# write_model_info(model_pre_process.model_info,self.cfg)
# t2 = time.time()
# print("Total time: ", t2-t1)
# return model_control_infos, final_model
# try:
nadhs = Nadhs(self.cfg)
nadhs.nadh_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, self.model_check_info, controler)
# write_model_info(model_pre_process.model_info,self.cfg)
atps = Atps(self.cfg)
atps.atp_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, self.model_check_info, controler)
# write_result3(self.model_control_info,self.cfg)
# write_model_info(model_pre_process.model_info,self.cfg)
nets = Nets(self.cfg)
nets.net_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, self.model_check_info, controler)
# write_result(self.model_control_info,self.cfg)
# write_model_info(model_pre_process.model_info,self.cfg)
yields = Yields(self.cfg)
yield_one = yields.yield_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, controler, self.model_check_info)
if yield_one == 1:
return_restoration(model_pre_process.model_info, controler.model, controler.check_model, Biomasses(self.cfg))
yield_two = yields.yield_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, controler, self.model_check_info)
# self.model_control_info["check_metabolite_yield"]["model_revision"].extend(model_pre_process.model_info["yield_revision"])
# write_result(self.model_control_info,self.cfg)
# write_model_info(model_pre_process.model_info,self.cfg)
# final_model = write_final_model(controler.model,self.cfg)
# if final_model.endswith(".json"):
# controler.model = cobra.io.load_json_model(final_model)
# else:
# controler.model = cobra.io.read_sbml_model(final_model)
biomasses = Biomasses(self.cfg)
biomasses.biomass_control(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info, controler, self.model_check_info)
# write_model_info(model_pre_process.model_info,self.cfg)
# write_result(self.model_control_info,self.cfg)
# with open("result.json", "w") as f:
# json.dump(self.model_control_info, f, ensure_ascii=False)
convert_nan_to_null(controler.model)
convert_nan_to_null(controler.check_model)
# get_final_fluxes(model_pre_process.model_info, controler.model, controler.check_model, self.model_control_info)
# comparison_quantitative(self.model_control_info, model_pre_process.model_info)
convert_list_to_string(self.model_control_info, self.model_check_info)
# print(controler.model.reactions.get_by_id('FDR').check_mass_balance(),'xxxxxxxxxxxxxx')
# final_model = f"/home/dengxiao/mqc/tmp/bigg/test.xml"
# cobra.io.write_sbml_model(controler.model,final_model)
final_model = write_final_model(controler.model,self.cfg, self.model_file)
# check_model = write_check_model(controler.check_model,self.cfg, self.model_file)
except RuntimeError as e:
final_model = write_final_model(controler.model,self.cfg, self.model_file)
# check_model = write_check_model(controler.check_model,self.cfg, self.model_file)
model_control_infos = write_result2(self.model_control_info,self.cfg)
print(repr(e),'.............')
# raise
except Exception as e:
model_control_infos = write_result2(self.model_control_info,self.cfg)
final_model = write_final_model(controler.model,self.cfg, self.model_file)
# check_model = write_check_model(controler.check_model,self.cfg, self.model_file)
print(repr(e),'.............')
raise
print('0000000000000000000000000000000000000000000000000000000000')
control_analysis = model_pre_process.model_info["control_analysis"]
headers=['model','NADH','NADPH','FADH2','FMNH2','Q8H2','MQL8','DMMQL8','ATP','CTP','GTP','UTP','ITP','metabolite','yield','biomass','carbon source supply','restricted metabolites']
if len(control_analysis) < 16:
for i in range(16-len(control_analysis)):
control_analysis.append("")
control_analysis.append(model_pre_process.model_info["carbon_source_boundary"])
control_analysis.append(model_pre_process.model_info["limiting_metabolites"])
print(model_pre_process.model_info["control_analysis"])
# 创建一个数据框(DataFrame)
df = pd.DataFrame([control_analysis],columns=headers)
# 尝试读取现有文件
try:
existing_df = pd.read_excel('/home/dengxiao/mqc/tmp/control定量分析.xlsx', sheet_name='Sheet1')
# 将新的数据追加到现有数据框
existing_df = existing_df.append(df, ignore_index=True)
except FileNotFoundError:
# 如果文件不存在,直接使用新数据框
existing_df = df
# 将数据框写入Excel文件
existing_df.to_excel('/home/dengxiao/mqc/tmp/control定量分析.xlsx', index=False, header=True, sheet_name='Sheet1')
# modelInfo = write_model_info(model_pre_process.model_info,self.cfg)
# model_check_infos = write_result(self.model_check_info,self.cfg)
# if not model_control_infos:
model_control_infos = write_result3(self.model_control_info,self.cfg)
t2 = time.time()
print(controler.model.slim_optimize(),'...............................................................')
print("Total time: ", t2-t1)
return model_control_infos, final_model
def run(a,b):
""""""
# modelCheck = ModelCheck(args.file,args.outputdir)
# a=f'/home/dengxiao/mqc/mqc/local_test_data/CARVEME_COMMEN/{file}'
# b=f"tmp/new_CARVEME_COMMEN/{file.split('.')[0]}"
modelCheck = ModelCheck(a,b)
modelCheck.model_check()
modelCheck.model_check2()
if __name__ == '__main__':
""""""
# parser = argparse.ArgumentParser(description="MQC Program")
# parser.add_argument(type=str, default='', help='model file')
# parser.add_argument(type=str, default='./', help='result file directory')
# args = parser.parse_args()
# 解析命令行参数
if len(sys.argv) != 3:
print("Usage: mqc <model_path> <output_path>")
sys.exit(1)
model_path = sys.argv[1]
output_path = sys.argv[2]
if not os.path.exists(model_path):
print('模型文件不存在请检查')
exit(0)
if not os.path.exists(output_path):
# 如果不存在,创建文件夹
os.makedirs(output_path)
print(f"The folder {output_path} has been created.")
else:
print(f"The folder {output_path} already exists.")
run(model_path, output_path)