forked from opencobra/cobratoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometricFBA.m
237 lines (211 loc) · 6.47 KB
/
geometricFBA.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
function flux = geometricFBA(model,varargin)
%geometricFBA finds a unique optimal FBA solution that is (in some sense)
%central to the range of possible fluxes; as described in
% K Smallbone, E Simeonidis (2009). Flux balance analysis:
% A geometric perspective. J Theor Biol 258: 311-315
% http://dx.doi.org/10.1016/j.jtbi.2009.01.027
%
% flux = geometricFBA(model)
%
%INPUT
% model COBRA model structure
%
%OPTIONAL INPUTS
% Optional parameters can be entered as parameter name followed by
% parameter value: i.e. ...,'epsilon',1e-9)
% printLevel [default: 1] printing level
% = 0 silent
% = 1 show algorithm progress and times
% epsilon [default: 1e-6] convergence tolerance of algorithm,
% defined in more detail in paper above
% flexRel [default: 0] flexibility to flux bounds
% try e.g. 1e-3 if the algorithm has convergence problems
%
%OUTPUT
% flux unique centered flux
%
%kieran smallbone, 5 May 2010
%
% This script is made available under the Creative Commons
% Attribution-Share Alike 3.0 Unported Licence (see
% www.creativecommons.org).
param = struct('epsilon',1e-6,'flexRel',0,'printLevel',1);
field = fieldnames(param);
if mod(nargin,2) ~= 1 % require odd number of inputs
error('incorrect number of input parameters')
else
for k = 1:2:(nargin-1)
param.(field{strcmp(varargin{k},field)}) = varargin{k+1};
end
end
param.flexTol = param.flexRel * param.epsilon; % absolute flexibility
% determine optimum
FBAsolution = optimizeCbModel(model);
ind = find(model.c);
if length(ind) == 1
model.lb(ind) = FBAsolution.f;
end
A = model.S;
b = model.b;
L = model.lb;
U = model.ub;
% ensure column vectors
b = b(:); L = L(:); U = U(:);
% Remove negligible elements
J = any(A,2); A = A(J,:); b = b(J);
% presolve
v = nan(size(L));
J = (U-L < param.epsilon);
v(J) = (L(J)+U(J))/2;
J = find(isnan(v));
if param.printLevel
fprintf('%s\t%g\n\n%s\t@%s\n','# reactions:',length(v),'iteration #0',datestr(now,16));
end
L0 = L; U0 = U;
for k = J(:)'
f = zeros(length(v),1); f(k) = -1;
[dummy,opt,conv] = easyLP(f,A,b,L0,U0);
if conv
vL = max(-opt,L(k));
else
vL = L(k);
end
[dummy,opt,conv] = easyLP(-f,A,b,L0,U0);
if conv
vU = min(opt,U(k));
else vU = U(k);
end
if abs(vL) < param.epsilon
vL = 0;
end
if abs(vU) < param.epsilon
vU = 0;
end
vM = (vL + vU)/2;
if abs(vM) < param.epsilon
vM = 0;
end
if abs(vU - vL) < param.epsilon
vL = (1-sign(vM)* param.flexTol)*vM;
vU = (1+sign(vM)* param.flexTol)*vM;
end
L(k) = vL; U(k) = vU;
end
v = nan(size(L));
J = (U-L < param.epsilon);
v(J) = (L(J)+U(J))/2; v = v.*(abs(v) > param.epsilon);
if param.printLevel
fprintf('%s\t\t%g\n%s\t\t%g\n\n','fixed:',sum(J),'@ zero:',sum(v==0));
end
% iterate
J = find(U-L >= param.epsilon);
n = 1;
mu = [];
Z = [];
while ~isempty(J)
if param.printLevel
fprintf('%s #%g\t@%s\n','iteration',n,datestr(now,16));
end
if n == 1
M = zeros(size(L));
else
M = (L+U)/2;
end
mu(:,n) = M; %#ok<AGROW>
allL = L; allU = U; allA = A; allB = b;
[a1,a2] = size(A);
% build new matrices
for k = 1:(n-1)
[b1,b2] = size(allA);
f = sparse(b2+2*a2,1); f((b2+1):end) = -1;
opt = -Z(k);
allA = [allA,sparse(b1,2*a2);
speye(a2,a2),sparse(a2,b2-a2),-speye(a2),speye(a2);
f(:)']; %#ok<AGROW>
allB = [allB;mu(:,k);opt]; %#ok<AGROW>
allL = [allL;zeros(2*a2,1)]; %#ok<AGROW>
allU = [allU;inf*ones(2*a2,1)]; %#ok<AGROW>
end
[b1,b2] = size(allA);
f = zeros(b2+2*a2,1); f((b2+1):end) = -1;
allA = [allA,sparse(b1,2*a2);
speye(a2,a2),sparse(a2,b2-a2),-speye(a2),speye(a2)]; %#ok<AGROW>
allB = [allB;M]; %#ok<AGROW>
allL = [allL;zeros(2*a2,1)]; %#ok<AGROW>
allU = [allU;inf*ones(2*a2,1)]; %#ok<AGROW>
[v,opt,conv] = easyLP(f,allA,allB,allL,allU);
if ~conv, disp('error: no convergence'); flux = (L+U)/2; return; end
opt = ceil(-opt/eps)*eps;
Z(n) = opt; %#ok<AGROW>
allA = [allA; sparse(f(:)')]; %#ok<AGROW>
allB = [allB; -opt]; %#ok<AGROW>
for k = J(:)'
f = zeros(length(allL),1); f(k) = -1;
[dummy,opt,conv] = easyLP(f,allA,allB,allL,allU);
if conv
vL = max(-opt,L(k));
else
vL = L(k);
end
[dummy,opt,conv] = easyLP(-f,allA,allB,allL,allU);
if conv
vU = min(opt,U(k));
else
vU = U(k);
end
if abs(vL) < param.epsilon
vL = 0;
end
if abs(vU) < param.epsilon
vU = 0;
end
vM = (vL + vU)/2;
if abs(vM) < param.epsilon
vM = 0;
end
if abs(vU - vL) < param.epsilon
vL = (1-sign(vM)* param.flexTol)*vM;
vU = (1+sign(vM)* param.flexTol)*vM;
end
L(k) = vL;
U(k) = vU;
end
v = nan(size(L));
J = (U-L < param.epsilon);
v(J) = (L(J)+U(J))/2; v = v.*(abs(v) > param.epsilon);
if param.printLevel
fprintf('%s\t\t%g\n%s\t\t%g\n\n','fixed:',sum(J),'@ zero:',sum(v==0));
end
n = n+1;
J = find(U-L >= param.epsilon);
flux = v;
end
function [v,fOpt,conv] = easyLP(c,A,b,lb,ub)
%easyLP
%
% solves the linear programming problem:
% max c'x subject to
% A x = b
% lb <= x <= ub.
%
% Usage: [v,fOpt,conv] = easyLP(c,A,b,lb,ub)
%
% c objective coefficient vector
% A LHS matrix
% b RHS vector
% lb lower bound
% ub upper bound
%
% v solution vector
% fOpt objective value
% conv convergence of algorithm [0/1]
%
% the function is a wrapper for the "solveCobraLP" script.
%
%kieran smallbone, 5 may 2010
csense(1:length(b)) = 'E';
model = struct('A',A,'b',b,'c',full(c),'lb',lb,'ub',ub,'osense',-1,'csense',csense);
solution = solveCobraLP(model);
v = solution.full;
fOpt = solution.obj;
conv = solution.stat == 1;