-
Notifications
You must be signed in to change notification settings - Fork 2
/
dolmn1b_makeFBAmodels_core.m
269 lines (238 loc) · 11.8 KB
/
dolmn1b_makeFBAmodels_core.m
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
clear variables
close all
clc
%% Load Data
if ~isdir('DOLMN_Parsed/Core/'); mkdir('DOLMN_Parsed/Core/'); end
saveDataName = 'Ecoli_Core';
model_title = 'E. coli Core';
% Load Metabolic Model
load('Ecoli_core_newS.mat')
% Medium
[~,exchRxns_idx] = identifyExchRxns(Ecoli);
[exchMets_idx,~] = identifyExchMets(Ecoli,exchRxns_idx);
medium = Ecoli.mets(exchMets_idx);
medium_rxns = Ecoli.rxns(exchRxns_idx);
% ATPM Reaction
atpm_name = 'ATPM';
% Transport Reaction Constraints
trsptCon1 = [7:12, 25];
trsptCon2 = [9:16, 25];
% Biomass Flux Lower Bound
bio_lb = 0.1;
%% Make FBA Models
% Each Transport Constraint in 1 & 2 Models
trsptCon = intersect(trsptCon1,trsptCon2);
for trsptCon_num = trsptCon
disp(['T_trspt=' int2str(trsptCon_num)])
% 1 Model
disp(' 1 Model')
data1 = load(fullfile('DOLMN_Output','Core',['CVX_core_K1_Nt' int2str(trsptCon_num) '.mat']));
% Data
data1.trspt_con = trsptCon_num.*ones(size(data1.sparse_con));
% Check for Infeasible Solutions
[~,nan_idx] = find(isnan(data1.biomass)); nan_idx = unique(nan_idx);
[~,bio_idx] = find(data1.biomass==0); bio_idx = min(bio_idx);
if nan_idx == bio_idx-1
data1.biomass(:,nan_idx) = 0;
data1.flux_norm(:,nan_idx) = 0;
for model_num = 1:numel(data1.model)
data1.model{model_num}.biomass(nan_idx) = 0;
data1.model{model_num}.flux(:,nan_idx) = 0;
data1.model{model_num}.int(:,nan_idx) = 0;
end
elseif ~isempty(nan_idx)
error('Error in data: Infeasible solution is not at boundary')
end
% Check for Numerical Problems
[~,num_idx] = find(data1.biomass > 1); num_idx = unique(num_idx);
if ~isempty(num_idx)
error('Error in data: Numerical problem')
end
% Biomass Flux Lower Bound
data1.bio_lb = bio_lb;
% Process
[models1,base_model1,model_flux1] = algorithm2models(data1,Ecoli,[model_title ' 1 Model'],medium_rxns,atpm_name);
% Save Data
save(fullfile('DOLMN_Parsed','Core',[saveDataName '_1Model_TrsptCon_' int2str(trsptCon_num) '.mat']), ...
'models1','base_model1','model_flux1')
% 2 Models
disp(' 2 Models')
data2 = load(fullfile('DOLMN_Output','Core',['CVX_core_K2_Nt' int2str(trsptCon_num) '.mat']));
% Data
data2.trspt_con = trsptCon_num.*ones(size(data2.sparse_con));
% Check for Infeasible Solutions
[~,nan_idx] = find(isnan(data2.biomass)); nan_idx = unique(nan_idx);
[~,bio_idx] = find(data2.biomass==0); bio_idx = min(bio_idx);
if nan_idx == bio_idx-1
data2.biomass(:,nan_idx) = 0;
data2.flux_norm(:,nan_idx) = 0;
for model_num = 1:numel(data2.model)
data2.model{model_num}.biomass(nan_idx) = 0;
data2.model{model_num}.flux(:,nan_idx) = 0;
data2.model{model_num}.int(:,nan_idx) = 0;
end
elseif ~isempty(nan_idx)
error('Error in data: Infeasible solution is not at boundary')
end
% Check for Numerical Problems
[~,num_idx] = find(data2.biomass > 1); num_idx = unique(num_idx);
if ~isempty(num_idx)
error('Error in data: Numerical problem')
end
% Biomass Flux Lower Bound
data2.bio_lb = bio_lb;
% Process
[models2,base_model2,model_flux2] = algorithm2models(data2,Ecoli,[model_title ' 2 Models'],medium_rxns,atpm_name);
% Save Data
save(fullfile('DOLMN_Parsed','Core',[saveDataName '_2Models_TrsptCon_' int2str(trsptCon_num) '.mat']), ...
'models2','base_model2','model_flux2')
clear data1* data2* models* base_model* model_flux* opt_status*
close all
disp('=========================')
end
% Each Transport Constraint in 1 Model
trsptCon1only = setdiff(trsptCon1,trsptCon);
for trsptCon_num = trsptCon1only
disp(['T_trspt=' int2str(trsptCon_num)])
% 1 Model
disp(' 1 Model')
data1 = load(fullfile('DOLMN_Output','Core',['CVX_core_K1_Nt' int2str(trsptCon_num) '.mat']));
% Data
data1.trspt_con = trsptCon_num.*ones(size(data1.sparse_con));
% Check for Infeasible Solutions
[~,nan_idx] = find(isnan(data1.biomass)); nan_idx = unique(nan_idx);
[~,bio_idx] = find(data1.biomass==0); bio_idx = min(bio_idx);
if nan_idx == bio_idx-1
data1.biomass(:,nan_idx) = 0;
data1.flux_norm(:,nan_idx) = 0;
for model_num = 1:numel(data1.model)
data1.model{model_num}.biomass(nan_idx) = 0;
data1.model{model_num}.flux(:,nan_idx) = 0;
data1.model{model_num}.int(:,nan_idx) = 0;
end
elseif ~isempty(nan_idx)
error('Error in data: Infeasible solution is not at boundary')
end
% Check for Numerical Problems
[~,num_idx] = find(data1.biomass > 1); num_idx = unique(num_idx);
if ~isempty(num_idx)
error('Error in data: Numerical problem')
end
% Biomass Flux Lower Bound
data1.bio_lb = bio_lb;
% Process
[models1,base_model1,model_flux1] = algorithm2models(data1,Ecoli,[model_title ' 1 Model'],medium_rxns,atpm_name);
% Save Data
save(fullfile('DOLMN_Parsed','Core',[saveDataName '_1Model_TrsptCon_' int2str(trsptCon_num) '.mat']), ...
'models1','base_model1','model_flux1')
clear data1* models* base_model* model_flux* opt_status*
close all
disp('=========================')
end
% Each Transport Constraint in 2 Models
trsptCon2only = setdiff(trsptCon2,trsptCon);
for trsptCon_num = trsptCon2only
disp(['T_trspt=' int2str(trsptCon_num)])
% 2 Models
disp(' 2 Models')
data2 = load(fullfile('DOLMN_Output','Core',['CVX_core_K2_Nt' int2str(trsptCon_num) '.mat']));
% Data
data2.trspt_con = trsptCon_num.*ones(size(data2.sparse_con));
% Check for Infeasible Solutions
[~,nan_idx] = find(isnan(data2.biomass)); nan_idx = unique(nan_idx);
[~,bio_idx] = find(data2.biomass==0); bio_idx = min(bio_idx);
if nan_idx == bio_idx-1
data2.biomass(:,nan_idx) = 0;
data2.flux_norm(:,nan_idx) = 0;
for model_num = 1:numel(data2.model)
data2.model{model_num}.biomass(nan_idx) = 0;
data2.model{model_num}.flux(:,nan_idx) = 0;
data2.model{model_num}.int(:,nan_idx) = 0;
end
elseif ~isempty(nan_idx)
error('Error in data: Infeasible solution is not at boundary')
end
% Check for Numerical Problems
[~,num_idx] = find(data2.biomass > 1); num_idx = unique(num_idx);
if ~isempty(num_idx)
error('Error in data: Numerical problem')
end
% Biomass Flux Lower Bound
data2.bio_lb = bio_lb;
% Process
[models2,base_model2,model_flux2] = algorithm2models(data2,Ecoli,[model_title ' 2 Models'],medium_rxns,atpm_name);
% Save Data
save(fullfile('DOLMN_Parsed','Core',[saveDataName '_2Models_TrsptCon_' int2str(trsptCon_num) '.mat']), ...
'models2','base_model2','model_flux2')
clear data2* models* base_model* model_flux* opt_status*
close all
disp('=========================')
end
%% Combine Data
% 1 Model
model1 = cell(size(trsptCon1)); % Pre-Allocate
for trsptCon_num = 1:numel(trsptCon1)
% Load Data
load(fullfile('DOLMN_Parsed','Core',[saveDataName '_1Model_TrsptCon_' int2str(trsptCon1(trsptCon_num)) '.mat']),'model_flux1')
% Save to New Struct
model1{trsptCon_num} = model_flux1; clear model_flux1
end
% 2 Models
model2 = cell(size(trsptCon2)); % Pre-Allocate
for trsptCon_num = 1:numel(trsptCon2)
% Load Data
load(fullfile('DOLMN_Parsed','Core',[saveDataName '_2Models_TrsptCon_' int2str(trsptCon2(trsptCon_num)) '.mat']),'model_flux2')
% Save to New Struct
model2{trsptCon_num} = model_flux2; clear model_flux2
end
%% Pad because added a column whenever biomass didn't reach zero
% 1 Model
num_intlCon1 = arrayfun(@(x) numel(model1{x}{1}.intl_con), 1:numel(model1));
for ii = find(num_intlCon1 < max(num_intlCon1))
model1{ii}{1}.intl_con = [model1{ii}{1}.intl_con(1)-1, model1{ii}{1}.intl_con];
model1{ii}{1}.trspt_con = [model1{ii}{1}.trspt_con(1), model1{ii}{1}.trspt_con];
model1{ii}{1}.biomass = [0, model1{ii}{1}.biomass];
model1{ii}{1}.flux = [zeros(numel(model1{ii}{1}.rxns),1), model1{ii}{1}.flux];
model1{ii}{1}.int = [zeros(numel(model1{ii}{1}.rxns),1), model1{ii}{1}.int];
model1{ii}{1}.totalExchFlux = [zeros(numel(model1{ii}{1}.exch_idx),1), model1{ii}{1}.totalExchFlux];
model1{ii}{1}.warningFlag.BinaryVariableFlux = [0, model1{ii}{1}.warningFlag.BinaryVariableFlux];
model1{ii}{1}.warningFlag.CalcExchFlux = [0, model1{ii}{1}.warningFlag.CalcExchFlux];
model1{ii}{1}.warningFlag.FBA_sol = [0, model1{ii}{1}.warningFlag.FBA_sol];
model1{ii}{1}.warningFlag.sol = [0, model1{ii}{1}.warningFlag.sol];
model1{ii}{1}.warningFlag.Sv_viol = [0, model1{ii}{1}.warningFlag.Sv_viol];
model1{ii}{1}.warningFlag.Sv = [zeros(size(model1{1}{1}.mets)), model1{ii}{1}.warningFlag.Sv];
model1{ii}{1}.warningFlag.bounds_viol = [0, model1{ii}{1}.warningFlag.bounds_viol];
model1{ii}{1}.warningFlag.intl_bounds = [zeros(size(model1{1}{1}.intl_idx)), model1{ii}{1}.warningFlag.intl_bounds];
end
% 2 Models
num_intlCon2 = arrayfun(@(x) numel(model2{x}{1}.intl_con), 1:numel(model2));
for ii = find(num_intlCon2 < max(num_intlCon2))
for model_num = 1:2
model2{ii}{model_num}.intl_con = [model2{ii}{model_num}.intl_con(1)-1, model2{ii}{model_num}.intl_con];
model2{ii}{model_num}.trspt_con = [model2{ii}{model_num}.trspt_con(1), model2{ii}{model_num}.trspt_con];
model2{ii}{model_num}.biomass = [0, model2{ii}{model_num}.biomass];
model2{ii}{model_num}.flux = [zeros(numel(model2{ii}{model_num}.rxns),1), model2{ii}{model_num}.flux];
model2{ii}{model_num}.int = [zeros(numel(model2{ii}{model_num}.rxns),1), model2{ii}{model_num}.int];
model2{ii}{model_num}.totalExchFlux = [zeros(numel(model2{ii}{model_num}.exch_idx),1), model2{ii}{model_num}.totalExchFlux];
model2{ii}{model_num}.exchMets.fluxType12 = [zeros(numel(model2{ii}{model_num}.exch_idx),1), model2{ii}{model_num}.exchMets.fluxType12];
model2{ii}{model_num}.exchMets.model1_to_model2 = [zeros(numel(model2{ii}{model_num}.exch_idx),1), model2{ii}{model_num}.exchMets.model1_to_model2];
model2{ii}{model_num}.exchMets.model2_to_model1 = [zeros(numel(model2{ii}{model_num}.exch_idx),1), model2{ii}{model_num}.exchMets.model2_to_model1];
model2{ii}{model_num}.warningFlag.BinaryVariableFlux = [0, model2{ii}{model_num}.warningFlag.BinaryVariableFlux];
model2{ii}{model_num}.warningFlag.CalcExchFlux = [0, model2{ii}{model_num}.warningFlag.CalcExchFlux];
model2{ii}{model_num}.warningFlag.FBA_sol = [0, model2{ii}{model_num}.warningFlag.FBA_sol];
model2{ii}{model_num}.warningFlag.sol = [0, model2{ii}{model_num}.warningFlag.sol];
model2{ii}{model_num}.warningFlag.Sv_viol = [0, model2{ii}{model_num}.warningFlag.Sv_viol];
model2{ii}{model_num}.warningFlag.Sv = [zeros(size(model2{1}{1}.mets)), model2{ii}{model_num}.warningFlag.Sv];
model2{ii}{model_num}.warningFlag.bounds_viol = [0, model2{ii}{model_num}.warningFlag.bounds_viol];
model2{ii}{model_num}.warningFlag.intl_bounds = [zeros(size(model2{1}{1}.intl_idx)), model2{ii}{model_num}.warningFlag.intl_bounds];
end
end
%% Save Data
% Sparsity Constraints
intlCon1 = model1{end}{1}.intl_con; % intracellular sparsity constraints
trsptCon1 = arrayfun(@(x) model1{x}{1}.trspt_con(1), 1:numel(model1)); % transport sparsity constraints
intlCon2 = model2{end}{1}.intl_con; % intracellular sparsity constraints
trsptCon2 = arrayfun(@(x) model2{x}{1}.trspt_con(1), 1:numel(model2)); % transport sparsity constraints
% Save
save(fullfile('DOLMN_Parsed','Core',[saveDataName '.mat']), ...
'model1','model2','trsptCon1','trsptCon2','intlCon1','intlCon2')