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