0001 function model=sortModel(model,sortReversible,sortMetName,sortReactionOrder)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 if nargin<2
0023 sortReversible=true;
0024 end
0025 if nargin<3
0026 sortMetName=false;
0027 end
0028 if nargin<4
0029 sortReactionOrder=false;
0030 end
0031
0032 if sortMetName==true
0033
0034 [~, metIndexes]=sort(strcat(model.metNames,'[',model.comps(model.metComps),']'));
0035 model=permuteModel(model,metIndexes,'mets');
0036 end
0037
0038 if sortReversible==true && sortReactionOrder==false
0039
0040 revIndexes=find(model.rev);
0041
0042
0043 for i=1:numel(revIndexes)
0044
0045 mets=find(model.S(:,revIndexes(i)));
0046 metNames=strcat(model.metNames(mets),model.comps(model.metComps(mets)));
0047
0048 if iscellstr(metNames)
0049 [~, indexes]=sort(metNames);
0050
0051 if model.S(mets(indexes(1)),revIndexes(i))>0
0052 model.S(:,revIndexes(i))=model.S(:,revIndexes(i))*-1;
0053 end
0054 end
0055 end
0056 end
0057
0058 if sortReactionOrder==true
0059
0060 if ~isfield(model,'subSystems')
0061 EM='The model must contain a subSystems field in order to sort reaction order';
0062 dispEM(EM);
0063 end
0064
0065 subsystemsUnique='';
0066 subsystemsConcatenated='';
0067 for i=1:numel(model.subSystems)
0068 subsystemsConcatenated{i,1}=strjoin(model.subSystems{i,1},';');
0069 if ~isempty(model.subSystems{i,1})
0070 for j=1:numel(model.subSystems{i,1})
0071 subsystemsUnique{numel(subsystemsUnique)+1,1}=model.subSystems{i,1}{1,j};
0072 end
0073 end
0074 end
0075 subsystemsUnique=unique(subsystemsUnique);
0076 for i=1:numel(subsystemsUnique)
0077
0078 rxns=find(~cellfun(@isempty,regexp(subsystemsConcatenated,subsystemsUnique(i))));
0079
0080
0081
0082 if numel(rxns)<2 || numel(rxns)>250
0083 continue;
0084 end
0085
0086 nRxns=numel(rxns);
0087 revRxns=rxns(model.rev(rxns)~=0);
0088
0089
0090
0091
0092 oldRev=ones(numel(revRxns),1);
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 rxnOrder=1:nRxns;
0104 oldScore=-inf;
0105 counter=0;
0106 firstIter=true;
0107 while 1==1
0108 counter=counter+1;
0109 if counter==100*nRxns
0110 break;
0111 end
0112
0113 newRxnOrder=rxnOrder;
0114 rev=oldRev;
0115
0116 if firstIter==false
0117 y=randperm(nRxns,2);
0118
0119
0120 newRxnOrder(y(1))=rxnOrder(y(2));
0121 newRxnOrder(y(2))=rxnOrder(y(1));
0122
0123
0124
0125 if rand()>0.5 && numel(rev)>1
0126 n=randperm(numel(rev),1);
0127 rev(n)=rev(n)*-1;
0128 end
0129 end
0130 firstIter=false;
0131
0132 tempS=model.S;
0133
0134
0135 for j=1:numel(rev)
0136 if rev(j)==-1
0137 tempS(:,revRxns(j))=tempS(:,revRxns(j)).*-1;
0138 end
0139 end
0140
0141
0142
0143 s=tempS(:,newRxnOrder);
0144
0145
0146 s=s(any(s,2),:);
0147
0148
0149 s=[s ones(size(s,1),1) ones(size(s,1),1)*-1];
0150
0151
0152
0153 s1=s>0;
0154 r1=arrayfun(@(x) find(s1(x,:),1,'first'),1:size(s1,1));
0155 s2=s<0;
0156 r2=arrayfun(@(x) find(s2(x,:),1,'first'),1:size(s2,1));
0157
0158 score=sum(r1<r2);
0159
0160 if score>=oldScore
0161 if score>oldScore
0162 counter=0;
0163 end
0164 oldScore=score;
0165 oldRev=rev;
0166 rxnOrder=newRxnOrder;
0167 end
0168 end
0169
0170
0171 for j=1:numel(oldRev)
0172 if oldRev(j)==-1
0173 model.S(:,revRxns(j))=model.S(:,revRxns(j)).*-1;
0174 end
0175 end
0176 order=1:numel(model.rxns);
0177 order(rxns)=rxns(rxnOrder);
0178 model=permuteModel(model, order, 'rxns');
0179 end
0180 end
0181 end