0001 function result = cba_economic_state(N, external, cba_constraints, cba_options)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 [nm,nr] = size(N);
0028
0029 epsilon = 10^-3;
0030
0031 cba_constraints.v_min(find(cba_constraints.v_sign==1)) = epsilon;
0032 cba_constraints.v_max(find(cba_constraints.v_sign==-1)) = -epsilon;
0033
0034 if isempty(cba_constraints.dmu_min), cba_constraints.dmu_min = -10*ones(nr,1); end
0035 if isempty(cba_constraints.dmu_max), cba_constraints.dmu_max = 10*ones(nr,1); end
0036 if isempty(cba_constraints.y_min), cba_constraints.y_min = -10*ones(nr,1); end
0037 if isempty(cba_constraints.y_max), cba_constraints.y_max = 10*ones(nr,1); end
0038 if isempty(cba_constraints.w_min), cba_constraints.w_min = -10*ones(nm,1); end
0039 if isempty(cba_constraints.w_max), cba_constraints.w_max = 10*ones(nm,1); end
0040
0041 zv = cba_constraints.zv;
0042 ind_v_fix = find(isfinite(cba_constraints.v_fix));
0043 ind_extsigns = find(isfinite(cba_constraints.ext_sign));
0044 n_int = sum(external==0);
0045 N_int = N(find(external==0),:);
0046 Ktot = analyse_N(N);
0047 Kint = analyse_N(N(find(external==0),:));
0048 nKint = size(Kint,2);
0049 nKtot = size(Ktot,2);
0050 my_eye = eye(nr);
0051 regularisation_weight = 0;
0052
0053
0054
0055
0056
0057
0058 ind_v = 1:nr;
0059 ind_dmu = nr + (1:nr);
0060 ind_y = 2 * nr + (1:nr);
0061 ind_w = 2 * nr + (1:nm);
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 switch cba_options.cba_conditions,
0098
0099 case 'y',
0100
0101 display('Optimisation with rate values y');
0102
0103
0104
0105
0106
0107
0108
0109 Aeq = double([my_eye(ind_v_fix,:), zeros(length(ind_v_fix),2*nr) ; ...
0110 N_int, zeros(n_int,2*nr) ; ...
0111 zeros(nKtot,nr), Ktot', zeros(nKtot,nr) ; ...
0112 zeros(nKint,2*nr), Kint' ]);
0113
0114 beq = double([cba_constraints.v_fix(ind_v_fix);...
0115 zeros(n_int,1) ;...
0116 zeros(nKtot,1) ;...
0117 Kint' * zv ]);
0118
0119 xmin = [cba_constraints.v_min; cba_constraints.dmu_min; cba_constraints.y_min];
0120 xmax = [cba_constraints.v_max; cba_constraints.dmu_max; cba_constraints.y_max];
0121 zvfull = [cba_constraints.zv ; zeros(nr,1); zeros(nr,1)];
0122
0123
0124
0125
0126
0127 A = [-zv', zeros(1,2*nr) ; ...
0128 double([-diag(cba_constraints.ext_sign(ind_extsigns)) * N(ind_extsigns,:), ...
0129 zeros(length(ind_extsigns),2*nr)])];
0130
0131 b = [0; zeros(length(ind_extsigns),1)];
0132
0133 case 'w',
0134
0135 display('Optimisation with metabolite values w');
0136
0137
0138
0139
0140
0141
0142 Aeq = double([my_eye(ind_v_fix,:), zeros(length(ind_v_fix),nr+nm) ; ...
0143 N_int, zeros(n_int,nr+nm) ; ...
0144 zeros(nKtot,nr), Ktot', zeros(nKtot,nm)]);
0145
0146 beq = double([cba_constraints.v_fix(ind_v_fix);...
0147 zeros(n_int,1) ;...
0148 zeros(nKtot,1) ]);
0149
0150 xmin = [cba_constraints.v_min; cba_constraints.dmu_min; cba_constraints.w_min];
0151 xmax = [cba_constraints.v_max; cba_constraints.dmu_max; cba_constraints.w_max];
0152 zvfull = [cba_constraints.zv; zeros(nr,1); zeros(nm,1)];
0153
0154
0155
0156
0157
0158
0159 A = [zv', zeros(1,nr+nm) ; ...
0160 double([-diag(cba_constraints.ext_sign(ind_extsigns)) * N(ind_extsigns,:), ...
0161 zeros(length(ind_extsigns),nr+nm)])];
0162
0163 b = [0; zeros(length(ind_extsigns),1)];
0164
0165
0166
0167
0168
0169 A = [-zv', zeros(1,nr+nm) ; ...
0170 double([-diag(cba_constraints.ext_sign(ind_extsigns)) * N(ind_extsigns,:), ...
0171 zeros(length(ind_extsigns),nr+nm)])];
0172
0173 b = [0; zeros(length(ind_extsigns),1)];
0174
0175 end
0176
0177
0178
0179
0180
0181 ppp.Ntrans = full(N');
0182 ppp.ind_v = ind_v;
0183 ppp.ind_dmu = ind_dmu;
0184 ppp.ind_y = ind_y;
0185 ppp.ind_w = ind_w;
0186 ppp.zv = zv;
0187
0188 opt = optimset('MaxFunEvals',10000);
0189
0190 switch cba_options.objective
0191
0192 case 'fba',
0193
0194 switch cba_options.cba_conditions
0195 case 'y',
0196 x0 = rand(3*nr,1)-.5;
0197 weights = regularisation_weight * ones(size(x0));
0198 ppp.epsilon = -0.1;
0199 [xtry,negvalue,exitflag] = fmincon(@(xx)cbaf1(xx,zvfull,weights), x0, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2(xx,ppp), opt);
0200
0201 ppp.epsilon = -0.001;
0202 [x,negvalue,exitflag] = fmincon(@(x)cbaf1(x,zvfull,weights), xtry, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2(xx,ppp), opt);
0203 case 'w',
0204 x0 = rand(2*nr+nm,1)-.5;
0205 weights = regularisation_weight * ones(size(x0));
0206 ppp.epsilon = -0.1;
0207 [xtry,negvalue,exitflag] = fmincon(@(xx)cbaf1(xx,zvfull,weights), x0, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2w(xx,ppp), opt);
0208
0209 ppp.epsilon = -0.001;
0210 [x,negvalue,exitflag] = fmincon(@(x)cbaf1(x,zvfull,weights), xtry, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2w(xx,ppp), opt);
0211 end
0212
0213 case 'fit',
0214
0215 v_mean = cba_constraints.v_mean;
0216 v_std = cba_constraints.v_std;
0217 switch cba_options.cba_conditions
0218 case 'y',
0219 x0 = rand(3*nr,1)-.5;
0220 ppp.epsilon = -0.1;
0221 [xtry,value,exitflag] = fmincon(@(xx)cbaf3(xx,v_mean,v_std), x0, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2(xx,ppp), opt);
0222
0223 ppp.epsilon = -0.001;
0224 [x,value,exitflag] = fmincon(@(x)cbaf3(x,v_mean,v_std), xtry, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2(xx,ppp), opt);
0225 case 'w',
0226 x0 = rand(2*nr+nm,1)-.5;
0227 weights = regularisation_weight * ones(size(x0));
0228 ppp.epsilon = -0.1;
0229 [xtry,value,exitflag] = fmincon(@(xx)cbaf3(xx,v_mean,v_std), x0, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2w(xx,ppp), opt)
0230
0231 ppp.epsilon = -0.001;
0232 [x,value,exitflag] = fmincon(@(x)cbaf3(x,v_mean,v_std), xtry, A, b, Aeq, beq, xmin, xmax, @(xx)cbaf2w(xx,ppp), opt)
0233 end
0234
0235 end
0236
0237
0238
0239
0240 v = nan * ones(nr,1);
0241 dmu = nan * ones(nr,1);
0242 y = nan * ones(nr,1);
0243 w = nan * ones(nm,1);
0244 value = nan;
0245
0246 if exitflag<=0,
0247 display('CBA failed');
0248 else,
0249 v = x(ind_v);
0250 dmu = x(ind_dmu);
0251 switch cba_options.objective
0252 case 'fba',
0253 value = -negvalue;
0254 end
0255 switch cba_options.cba_conditions,
0256 case 'y', y = x(ind_y);
0257 case 'w', w = x(ind_w);
0258 end
0259
0260 if value < 0, warning('Objective function is negative'); end
0261 if sum(v.*dmu > 0), warning('Solution violates EBA constraint'); end
0262 switch cba_options.cba_conditions,
0263 case 'y',
0264 if sum(v.*y < 0), warning('Solution violates CBA constraint'); end
0265 case 'w',
0266 if sum(v.*[N'*w+zv] < 0), warning('Solution violates CBA constraint'); end
0267 end
0268
0269 end
0270
0271 result.value = value;
0272 result.v = v;
0273 result.dmu = dmu;
0274
0275 switch cba_options.cba_conditions,
0276 case 'y', result.y = y;
0277 case 'w', result.w = w;
0278 end