forked from SysBioChalmers/RAVEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortModel.m
executable file
·181 lines (155 loc) · 6.28 KB
/
sortModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function model=sortModel(model,sortReversible,sortMetName,sortReactionOrder)
% sortModel
% Sorts a model based on metabolite names and compartments
%
% model a model structure
% sortReversible sorts the reversible reactions so the the metabolite
% that is first in lexiographical order is a reactant
% (opt, default true)
% sortMetName sort the metabolite names in the equation, also uses
% compartment abbreviation (opt, default false)
% sortReactionOrder sorts the reaction order within each subsystem so that
% reactions consuming some metabolite comes efter
% reactions producing it. This overrides the
% sortReversible option and reactions are sorted so that
% the production direction matches the consumption
% direction (opt, default false)
%
% model an updated model structure
%
% Usage: model=sortModel(model,sortReversible,sortMetName,sortReactionOrder)
if nargin<2
sortReversible=true;
end
if nargin<3
sortMetName=false;
end
if nargin<4
sortReactionOrder=false;
end
if sortMetName==true
%Assuming that metComps are the indexes. Should be changed at one point
[~, metIndexes]=sort(strcat(model.metNames,'[',model.comps(model.metComps),']'));
model=permuteModel(model,metIndexes,'mets');
end
if sortReversible==true && sortReactionOrder==false
%Get all reversible reactions
revIndexes=find(model.rev);
%Loop through them
for i=1:numel(revIndexes)
%Create a cell array with all the metabolite names
mets=find(model.S(:,revIndexes(i)));
metNames=strcat(model.metNames(mets),model.comps(model.metComps(mets)));
if iscellstr(metNames)
[~, indexes]=sort(metNames);
if model.S(mets(indexes(1)),revIndexes(i))>0
model.S(:,revIndexes(i))=model.S(:,revIndexes(i))*-1;
end
end
end
end
if sortReactionOrder==true
%Check if the model has sub-systems, otherwise throw an error
if ~isfield(model,'subSystems')
EM='The model must contain a subSystems field in order to sort reaction order';
dispEM(EM);
end
subsystemsUnique='';
subsystemsConcatenated='';
for i=1:numel(model.subSystems)
subsystemsConcatenated{i,1}=strjoin(model.subSystems{i,1},';');
if ~isempty(model.subSystems{i,1})
for j=1:numel(model.subSystems{i,1})
subsystemsUnique{numel(subsystemsUnique)+1,1}=model.subSystems{i,1}{1,j};
end
end
end
subsystemsUnique=unique(subsystemsUnique);
for i=1:numel(subsystems)
%Get all reactions for that subsystem
rxns=find(~cellfun(@isempty,regexp(subsystemsConcatenated,subsystemsUnique(i))));
%Temporarily ignore large subsystems because of inefficient
%implementation
if numel(rxns)<2 || numel(rxns)>250
continue;
end
nRxns=numel(rxns);
revRxns=rxns(model.rev(rxns)~=0);
%This variable will hold the current reversibility directions of
%the reversible reactions. 1 means the same direction as in the
%original model and -1 means the opposite direction.
oldRev=ones(numel(revRxns),1);
%The problem could probably be solved analytically but a simple
%random method is implemented here instead. Two reactions are
%chosen randomly and their positions are switched. A score is
%calculated based on the number of metabolites that are produced
%before they are consumed. If the perturbed model has a better or
%equal score than the original the reaction order is switched. If
%no increase in score has been seen after 1000*rxnsInSubsystem then
%the optimization is terminated
rxnOrder=1:nRxns;
oldScore=-inf;
counter=0;
firstIter=true;
while 1==1
counter=counter+1;
if counter==100*nRxns
break;
end
newRxnOrder=rxnOrder;
rev=oldRev;
if firstIter==false
y=randperm(nRxns,2);
%Switch the order
newRxnOrder(y(1))=rxnOrder(y(2));
newRxnOrder(y(2))=rxnOrder(y(1));
%With a 50% chance, also switch the reversibility of one of
%the reactions
if rand()>0.5 && numel(rev)>1
n=randperm(numel(rev),1);
rev(n)=rev(n)*-1;
end
end
firstIter=false;
tempS=model.S;
%Switch the directionalities
for j=1:numel(rev)
if rev(j)==-1
tempS(:,revRxns(j))=tempS(:,revRxns(j)).*-1;
end
end
%Get the metabolites that are involved and when they are
%produced/consumed
s=tempS(:,newRxnOrder);
%Remove mets that aren't used in both directions
s=s(any(s,2),:);
%Add so that all mets are produced and consumed in the end
s=[s ones(size(s,1),1) ones(size(s,1),1)*-1];
%For each metabolite, find the reaction where it's first
%produced and the reaction where it's first consumed
s1=s>0;
r1=arrayfun(@(x) find(s1(x,:),1,'first'),1:size(s1,1));
s2=s<0;
r2=arrayfun(@(x) find(s2(x,:),1,'first'),1:size(s2,1));
score=sum(r1<r2);
if score>=oldScore
if score>oldScore
counter=0;
end
oldScore=score;
oldRev=rev;
rxnOrder=newRxnOrder;
end
end
%Update the model for this subsystem
for j=1:numel(oldRev)
if oldRev(j)==-1
model.S(:,revRxns(j))=model.S(:,revRxns(j)).*-1;
end
end
order=1:numel(model.rxns);
order(rxns)=rxns(rxnOrder);
model=permuteModel(model, order, 'rxns');
end
end
end