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 pos = sum(reducedModel.S(twoNonZero(i),involvedRxns).' > 0 | reducedModel.rev(involvedRxns));
0066 neg = sum(reducedModel.S(twoNonZero(i),involvedRxns).' < 0 | reducedModel.rev(involvedRxns));
0067
0068
0069
0070
0071 if numel(involvedRxns)==2 && isempty(intersect(bannedIndexes,involvedRxns)) && pos >= 1 && neg >= 1
0072
0073
0074 if reducedModel.rev(involvedRxns(1)) && (~reducedModel.rev(involvedRxns(2))) && ...
0075 (reducedModel.S(twoNonZero(i),involvedRxns(1)) > 0) && (reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0)
0076 involvedRxns = flip(involvedRxns);
0077 end
0078
0079
0080 if reducedModel.S(twoNonZero(i),involvedRxns(1)) < 0
0081
0082
0083 if reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0
0084 involvedRxns = flip(involvedRxns);
0085 else
0086
0087 if reducedModel.rev(involvedRxns(1)) == 1
0088 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(1), groupIds, reversedRxns);
0089 else
0090 if reducedModel.rev(involvedRxns(2)) == 1
0091 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(2), groupIds, reversedRxns);
0092 involvedRxns = flip(involvedRxns);
0093 else
0094 error('We should never end up here!');
0095 end
0096 end
0097 end
0098 end
0099
0100 if reducedModel.S(twoNonZero(i),involvedRxns(2)) > 0
0101 if reducedModel.rev(involvedRxns(2)) == 1
0102 [reducedModel,reversedRxns] = flipRxn(reducedModel, involvedRxns(2), groupIds, reversedRxns);
0103 else
0104 error('We should never end up here!');
0105 end
0106 end
0107
0108
0109
0110 stoichRatio=abs(reducedModel.S(twoNonZero(i),involvedRxns(1))/reducedModel.S(twoNonZero(i),involvedRxns(2)));
0111
0112
0113 reducedModel.S(:,involvedRxns(1))=reducedModel.S(:,involvedRxns(1))+reducedModel.S(:,involvedRxns(2))*stoichRatio;
0114
0115
0116 reducedModel.S(:,involvedRxns(2))=0;
0117
0118
0119
0120 reducedModel.S(twoNonZero(i),involvedRxns(1))=0;
0121
0122
0123
0124
0125
0126
0127
0128
0129 lb1=reducedModel.lb(involvedRxns(1));
0130 lb2=reducedModel.lb(involvedRxns(2));
0131 ub1=reducedModel.ub(involvedRxns(1));
0132 ub2=reducedModel.ub(involvedRxns(2));
0133
0134 if lb2~=-inf
0135 reducedModel.lb(involvedRxns(1))=max(lb1,lb2/stoichRatio);
0136 end
0137 if ub2~=inf
0138 reducedModel.ub(involvedRxns(1))=min(ub1,ub2/stoichRatio);
0139 end
0140
0141
0142 reducedModel.rev(involvedRxns(1)) = reducedModel.rev(involvedRxns(1))*reducedModel.rev(involvedRxns(2));
0143
0144
0145
0146 reducedModel.c(involvedRxns(1))=reducedModel.c(involvedRxns(1))+reducedModel.c(involvedRxns(2))*stoichRatio;
0147
0148
0149 rxnInd1 = find(strcmp(origRxnIds, reducedModel.rxns(involvedRxns(1))));
0150 rxnInd2 = find(strcmp(origRxnIds, reducedModel.rxns(involvedRxns(2))));
0151 grpId = max(groupIds(rxnInd1),groupIds(rxnInd2));
0152 if grpId == 0
0153 grpId = nextGroupId;
0154 nextGroupId = nextGroupId + 1;
0155 end
0156
0157 if groupIds(rxnInd1) ~= grpId
0158 if groupIds(rxnInd1) == 0
0159
0160 groupIds(rxnInd1) = grpId;
0161 else
0162
0163 groupIds(groupIds == groupIds(rxnInd1)) = grpId;
0164 end
0165 end
0166 if groupIds(rxnInd2) ~= grpId
0167 if groupIds(rxnInd2) == 0
0168
0169 groupIds(rxnInd2) = grpId;
0170 else
0171
0172 groupIds(groupIds == groupIds(rxnInd2)) = grpId;
0173 end
0174 end
0175
0176
0177 mergedSome=true;
0178 end
0179 end
0180
0181
0182 if mergedSome==false
0183 break;
0184 end
0185
0186
0187 I=find(sum(reducedModel.S~=0,1)==0);
0188
0189
0190 reducedModel=removeReactions(reducedModel,I);
0191
0192
0193 notInUse=sum(reducedModel.S~=0,2)==0;
0194 reducedModel=removeMets(reducedModel,notInUse);
0195 end
0196
0197 function [model1,reversedRxns1] = flipRxn(model1, rxnInd, groupIds1, reversedRxns1)
0198 model1.S(:,rxnInd) = model1.S(:,rxnInd)*-1;
0199
0200 ub = model1.ub(rxnInd);
0201 model1.ub(rxnInd) = -model1.lb(rxnInd);
0202 model1.lb(rxnInd) = -ub;
0203
0204 model1.c(rxnInd) = -model1.c(rxnInd);
0205
0206
0207
0208
0209 rxnIndices = rxnInd;
0210 if groupIds1(rxnInd) > 0
0211 rxnIndices = find(groupIds1 == groupIds1(rxnInd));
0212 end
0213 reversedRxns1(rxnIndices) = ~reversedRxns1(rxnIndices);
0214 end
0215 end