Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optEnvelope #2330

Merged
merged 28 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
46b2dc7
Merge pull request #2323 from opencobra/develop
rmtfleming Sep 26, 2024
f99b1cc
Merge pull request #5 from rmtfleming/b2024Feb1b
rmtfleming Sep 29, 2024
ef80589
Merge pull request #2326 from rmtfleming/master
rmtfleming Sep 29, 2024
8d413ad
Added optEnvelope to cobraToolbox
Kretaks Oct 2, 2024
427f6d4
added test for optEnvelope
Kretaks Oct 2, 2024
01beb32
Update milpOEReinserts.m
Kretaks Oct 3, 2024
af0b42f
Update optEnvelope.m
Kretaks Oct 3, 2024
e0d9789
Update seperateTransposeJoinOE.m
Kretaks Oct 3, 2024
56b4c59
Update sequentialOEReinserts.m
Kretaks Oct 3, 2024
f74dcea
Update optEnvelope.m
Kretaks Oct 7, 2024
38050e1
Update addEnv.m
Kretaks Oct 7, 2024
3f4467d
Update optEnvelope.m
Kretaks Oct 8, 2024
1cae338
Update milpOEReinserts.m
Kretaks Oct 8, 2024
17d2d47
Update milpOEReinserts.m
Kretaks Oct 8, 2024
58548fb
Update minActiveRxns.m
Kretaks Oct 8, 2024
2c857e6
Update minActiveRxns.m
Kretaks Oct 8, 2024
d7c948c
Update milpOEReinserts.m
Kretaks Oct 8, 2024
04bf8c2
Update minActiveRxns.m
Kretaks Oct 8, 2024
205a499
Update sequentialOEReinserts.m
Kretaks Oct 8, 2024
a31a300
Update testOptEnvelope.m
Kretaks Oct 22, 2024
4ab937a
Update sequentialOEReinserts.m
Kretaks Oct 22, 2024
3c627b4
Update addEnv.m
Kretaks Oct 28, 2024
a8a9ca7
Update milpOEReinserts.m
Kretaks Oct 28, 2024
2009d0c
Update minActiveRxns.m
Kretaks Oct 28, 2024
b8c37ce
Update optEnvelope.m
Kretaks Oct 28, 2024
678ad4b
Update sequentialOEReinserts.m
Kretaks Oct 28, 2024
1c412c1
Update optEnvelope.m documentation USAGE section
farid-zare Oct 29, 2024
6cdc246
Update addEnv.m documentation USAGE section
farid-zare Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/design/optEnvelope/addEnv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
function line = addEnv(origModel, biomass, desiredProduct, varargin)
% addEnv adds envelope to figure
% Algorithm is able to knock out genes as well as reactions to produce
% production envelope
%
%INPUT
% origModel COBRA model structure
% biomass Reaction name of biomass
% desiredProduct Reaction name of desired product
% KnockOuts (opt) List of knockouts for production envelope (default: {})
% colour (opt) Short name for colour of line to plot (default: 'r' (red))
% prodMol (opt) Molar mass of target product for yield plot
% subUptake (opt) Uptake of substrate for yield plot
% molarSum (opt) Molar mass of substrate for yield plot
%
%OUTPUT
% line Line data for plot function
%
%NOTES
% Sometimes last point of envelope drops to zero (might be rounding error)
% but this function connects last points of lines so the graph creates
% continuous line.
% This algorithm only adds graph. It does not change labels.
%
% Created by Kristaps Berzins 31/10/2022
% Modified by Kristaps Berzins 30/09/2024

parser = inputParser();
parser.addRequired('model', @(x) isstruct(x) && isfield(x, 'S') && isfield(origModel, 'rxns')...
&& isfield(origModel, 'mets') && isfield(origModel, 'lb') && isfield(origModel, 'ub') && isfield(origModel, 'b')...
&& isfield(origModel, 'c'))
parser.addRequired('biomass', @(x) any(validatestring(x, origModel.rxns)))
parser.addRequired('desiredProduct', @(x) any(validatestring(x, origModel.rxns)))
parser.addOptional('KnockOuts', {}, @(x) iscell(x) && ismatrix(x))
parser.addOptional('colour', 'r', @(x) any(validatecolor(x)))
parser.addOptional('prodMol', [], @(x) isnumeric(x))
parser.addOptional('subUptake', 10, @(x) isnumeric(x))
parser.addOptional('molarSum', 180, @(x) isnumeric(x))

parser.parse(origModel, biomass, desiredProduct, varargin{:});
origModel = parser.Results.model;
biomass = parser.Results.biomass;
desiredProduct = parser.Results.desiredProduct;
KnockOuts = parser.Results.KnockOuts;
colour = parser.Results.colour;
prodMol = parser.Results.prodMol;
subUptake = parser.Results.subUptake;
molarSum = parser.Results.molarSum;

if isempty(prodMol)
prodMolIs = false;
else
prodMolIs = true;
end

model = origModel;

if any(ismember(model.rxns, KnockOuts))
rxns = ismember(model.rxns, KnockOuts);
model.ub(rxns) = 0;
model.lb(rxns) = 0;
elseif any(ismember(model.genes, KnockOuts))
model = buildRxnGeneMat(model);
[model, ~, ~] = deleteModelGenes(model, KnockOuts);
%elseif %Enzymes
end

solMin = optimizeCbModel(model, 'min');
solMax = optimizeCbModel(model, 'max');
controlFlux1 = linspace(solMin.f, solMax.f, 100)';
if nnz(controlFlux1) == 0
return;
end
model = changeObjective(model, desiredProduct);

for i = 1:numel(controlFlux1)
model = changeRxnBounds(model, biomass, controlFlux1(i), 'b');
s = optimizeCbModel(model, 'min'); Min1(i, 1) = s.f;
if s.stat == 0
model = changeRxnBounds(model, biomass, controlFlux1(i) - 0.0001 * controlFlux1(i), 'b');
s = optimizeCbModel(model, 'min'); Min1(i, 1) = s.f;
s = optimizeCbModel(model, 'max'); Max1(i, 1) = s.f;
end
s = optimizeCbModel(model, 'max'); Max1(i, 1) = s.f;
if s.stat == 0
model = changeRxnBounds(model, biomass, controlFlux1(i) - 0.0001 * controlFlux1(i), 'b');
s= optimizeCbModel(model,'min');Min1(i,1)=s.f;
s= optimizeCbModel(model,'max');Max1(i,1)=s.f;
end
end

if prodMolIs
controlFlux1 = controlFlux1 / subUptake * 1000 / molarSum;
Max1 = Max1 / molarSum * prodMol / subUptake;
Min1 = Min1 / molarSum * prodMol / subUptake;
end

hold on
line = plot(controlFlux1, Max1, 'color', colour, 'LineWidth', 2);
plot(controlFlux1, Min1, 'color', colour, 'LineWidth', 2)
hold off

152 changes: 152 additions & 0 deletions src/design/optEnvelope/milpOEReinserts.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
function [knockouts] = milpOEReinserts(model, data, K, toDel, minP, numKO, timeLimit, printLevel)

if nargin < 7
timeLimit = inf;
end
if nargin < 8
printLevel = 0;
end
%%
switch toDel
case 0
rxns = findRxnIDs(model, data.mainActive);
rxns = ismember(model.rxns,[model.rxns(K);model.rxns(rxns)]);
reactions = model.rxns(~rxns);

% MILP

[r,c] = size(model.S);

model.C_chemical=zeros(c,1);
model.C_chemical(minP.proID)=1;

yInd=[];
notYInd=[];
reactionIDs = findRxnIDs(model, reactions);

for i=1:c
if find(reactionIDs == i)
yInd(end+1)=i;
continue;
else
notYInd(end+1)=i;
end
end

notYInd=sort(notYInd)';
yInd=sort(yInd)';

ySize=size(yInd,1);
I = eye(c);
A=[model.S;-model.S;I(notYInd,:);-I(notYInd,:); I(yInd,:); -I(yInd,:)];

[aSizeRow, vSize]=size(A);
reinserts=zeros(c,1);
reinserts(yInd)=1;

Ay1=diag(reinserts); %ub
Ay1=Ay1*diag(model.ub);

Ay2=diag(reinserts); %lb
Ay2=Ay2*diag(model.lb);

z1=find(Ay1);
z2=find(Ay2);
zSize=size([z1;z2],1);

Ay= [zeros(2*r+2*(vSize-ySize),ySize);
-Ay1(yInd,yInd);
Ay2(yInd,yInd);
];

B = [zeros(2*r,1);
model.ub(notYInd);
-model.lb(notYInd);
zeros(2*(ySize),1);
];

C=model.c;

[A_w,Ay_w ,B_w,C_w, ~, ~, wSize] = seperateTransposeJoinOE(-A,-Ay,-B,-C,ySize,1,c,1000,zSize); %max(model.ub) 1000

awSizeRow=size(A_w,1);

Cjoined=[model.C_chemical; zeros(wSize+zSize,1); zeros(ySize,1)];

A2=-[
-C', -C_w';
A, sparse(aSizeRow, wSize+zSize);
sparse(awSizeRow, vSize), A_w];

Ay2=-[
zeros(1, ySize);
Ay;
Ay_w];

C2=Cjoined(1:vSize+wSize+zSize,:);

B2=-[
0;
B;
B_w;
];

z3=find(Ay2);
zSizeOptKnock2=size(z3,1);

[A2_w,Ay2_w,B2_w,C2_w,lb2_w,ub2_w,uSize]=seperateTransposeJoinOE(A2,Ay2,B2,C2,ySize,1,vSize+wSize+zSize,1000,zSizeOptKnock2); %max(model.ub) 1000

[A2_wRow, A2_wCol]=size(A2_w);
[ARow, ACol]=size(A);

A3=[
A2_w, sparse(A2_wRow,ACol), Ay2_w; %dual constraints
zeros(1,uSize+zSizeOptKnock2+vSize), -ones(1,ySize); %y sum constraints
sparse(ARow,uSize+zSizeOptKnock2), A, Ay; %feasibility conatraint
];

B3=[
B2_w;
numKO-ySize;
B;
];

C3=[C2_w;
zeros(ACol,1);
zeros(ySize,1);
];

tmpL=lb2_w(1:uSize+zSizeOptKnock2);
tmpH=ub2_w(1:uSize+zSizeOptKnock2);
ysUpperBound=ones(ySize,1);
lb3=[tmpL; model.lb; zeros(ySize,1)];
ub3=[tmpH; model.ub; ysUpperBound];
intVars=A2_wCol+ACol+1:A2_wCol+ACol+ySize;

MILP.c = C3;
MILP.osense = -1; % max
MILP.A = sparse(A3);
MILP.b = B3;
MILP.lb = lb3; MILP.ub = ub3;
MILP.x0 = [];
MILP.vartype = char(ones(1,length(C3)).*double('C'));
MILP.vartype(intVars) = 'B';
MILP.csense = char(ones(1,length(B3)).*double('L'));

milpSol = solveCobraMILP(MILP, 'timeLimit', timeLimit, 'printLevel', printLevel);

%% add only found reactions

rxns = reactions(milpSol.int == 1);
knockouts = reactions(milpSol.int ~= 1);
numel(rxns)
numel(knockouts)
%debug
[irrev,~,~,~] = convertToIrreversible(model);
addEnv(irrev, irrev.rxns(minP.bioID), irrev.rxns(minP.proID), knockouts, 'r');
print('debug');
case 1
%Genes
case 2
%Enzymes
end
Loading