0001 function [reducedModel,origRxnIds,groupIds,reversedRxns]=mergeLinear(model, noMergeRxns)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 reducedModel=model;
0023
0024
0025 reducedModel.genes={};
0026 reducedModel.rxnGeneMat=sparse(numel(reducedModel.rxns),0);
0027 reducedModel.grRules(:)={''};
0028
0029 if isfield(reducedModel,'geneShortNames')
0030 reducedModel.geneShortNames={};
0031 end
0032 if isfield(reducedModel,'proteins')
0033 reducedModel.proteins={};
0034 end
0035 if isfield(reducedModel,'geneMiriams')
0036 reducedModel.geneMiriams={};
0037 end
0038 if isfield(reducedModel,'geneComps')
0039 reducedModel.geneComps=[];
0040 end
0041
0042 nextGroupId = 1;
0043 origRxnIds = reducedModel.rxns;
0044 groupIds = zeros(numel(reducedModel.rxns),1);
0045 reversedRxns = false(numel(reducedModel.rxns),1);
0046
0047
0048 while 1
0049
0050
0051
0052 bannedIndexes=getIndexes(reducedModel,noMergeRxns,'rxns');
0053
0054
0055
0056 twoNonZero = find(sum(reducedModel.S ~= 0, 2) == 2);
0057
0058 mergedSome=false;
0059
0060
0061
0062 for i=1:numel(twoNonZero)
0063 involvedRxns=find(reducedModel.S(twoNonZero(i),:));
0064
0065
0066
0067
0068
0069 pos = sum(reducedModel.S(twoNonZero(i),involvedRxns).' > 0 | reducedModel.rev(involvedRxns) ~= 0);
0070 neg = sum(reducedModel.S(twoNonZero(i),involvedRxns).' < 0 | reducedModel.rev(involvedRxns) ~= 0);
0071
0072
0073
0074
0075 if numel(involvedRxns)==2 && isempty(intersect(bannedIndexes,involvedRxns)) && pos >= 1 && neg >= 1
0076
0077
0078 if reducedModel.rev(involvedRxns(1)) && (~reducedModel.rev(involvedRxns(2))) && ...
0079 (reducedModel.S(twoNonZero(i),involvedRxns(1)) > 0) && (reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0)
0080 involvedRxns = flip(involvedRxns);
0081 end
0082
0083
0084 if reducedModel.S(twoNonZero(i),involvedRxns(1)) < 0
0085
0086
0087 if reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0
0088 involvedRxns = flip(involvedRxns);
0089 else
0090
0091 if reducedModel.rev(involvedRxns(1)) == 1
0092 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(1), groupIds, reversedRxns);
0093 else
0094 if reducedModel.rev(involvedRxns(2)) == 1
0095 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(2), groupIds, reversedRxns);
0096 involvedRxns = flip(involvedRxns);
0097 else
0098 error('We should never end up here!');
0099 end
0100 end
0101 end
0102 end
0103
0104 if reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0
0105 if reducedModel.rev(involvedRxns(2)) == 1
0106 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(2), groupIds, reversedRxns);
0107 else
0108 error('We should never end up here!');
0109 end
0110 end
0111
0112
0113
0114 stoichRatio=abs(reducedModel.S(twoNonZero(i),involvedRxns(1))/reducedModel.S(twoNonZero(i),involvedRxns(2)));
0115
0116
0117 reducedModel.S(:,involvedRxns(1))=reducedModel.S(:,involvedRxns(1))+reducedModel.S(:,involvedRxns(2))*stoichRatio;
0118
0119
0120 reducedModel.S(:,involvedRxns(2))=0;
0121
0122
0123
0124 reducedModel.S(twoNonZero(i),involvedRxns(1))=0;
0125
0126
0127
0128
0129
0130
0131
0132
0133 lb1=reducedModel.lb(involvedRxns(1));
0134 lb2=reducedModel.lb(involvedRxns(2));
0135 ub1=reducedModel.ub(involvedRxns(1));
0136 ub2=reducedModel.ub(involvedRxns(2));
0137
0138 if lb2~=-inf
0139 reducedModel.lb(involvedRxns(1))=max(lb1,lb2/stoichRatio);
0140 end
0141 if ub2~=inf
0142 reducedModel.ub(involvedRxns(1))=min(ub1,ub2/stoichRatio);
0143 end
0144
0145
0146 reducedModel.rev(involvedRxns(1)) = reducedModel.rev(involvedRxns(1))*reducedModel.rev(involvedRxns(2));
0147
0148
0149
0150 reducedModel.c(involvedRxns(1))=reducedModel.c(involvedRxns(1))+reducedModel.c(involvedRxns(2))*stoichRatio;
0151
0152
0153 rxnInd1 = find(strcmp(origRxnIds, reducedModel.rxns(involvedRxns(1))));
0154 rxnInd2 = find(strcmp(origRxnIds, reducedModel.rxns(involvedRxns(2))));
0155 grpId = max(groupIds(rxnInd1),groupIds(rxnInd2));
0156 if grpId == 0
0157 grpId = nextGroupId;
0158 nextGroupId = nextGroupId + 1;
0159 end
0160
0161 if groupIds(rxnInd1) ~= grpId
0162 if groupIds(rxnInd1) == 0
0163
0164 groupIds(rxnInd1) = grpId;
0165 else
0166
0167 groupIds(groupIds == groupIds(rxnInd1)) = grpId;
0168 end
0169 end
0170 if groupIds(rxnInd2) ~= grpId
0171 if groupIds(rxnInd2) == 0
0172
0173 groupIds(rxnInd2) = grpId;
0174 else
0175
0176 groupIds(groupIds == groupIds(rxnInd2)) = grpId;
0177 end
0178 end
0179
0180
0181 mergedSome=true;
0182 end
0183 end
0184
0185
0186 if mergedSome==false
0187 break;
0188 end
0189
0190
0191 I=find(sum(reducedModel.S~=0,1)==0);
0192
0193
0194 reducedModel=removeReactions(reducedModel,I);
0195
0196
0197 notInUse=sum(reducedModel.S~=0,2)==0;
0198 reducedModel=removeMets(reducedModel,notInUse);
0199 end
0200
0201 function [model1,reversedRxns1] = flipRxn(model1, rxnInd, groupIds1, reversedRxns1)
0202 model1.S(:,rxnInd) = model1.S(:,rxnInd)*-1;
0203
0204 ub = model1.ub(rxnInd);
0205 model1.ub(rxnInd) = -model1.lb(rxnInd);
0206 model1.lb(rxnInd) = -ub;
0207
0208 model1.c(rxnInd) = -model1.c(rxnInd);
0209
0210
0211
0212
0213 rxnIndices = rxnInd;
0214 if groupIds1(rxnInd) > 0
0215 rxnIndices = find(groupIds1 == groupIds1(rxnInd));
0216 end
0217 reversedRxns1(rxnIndices) = ~reversedRxns1(rxnIndices);
0218 end
0219 end