-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplifyDuplicatedGenesFastbox.m
318 lines (268 loc) · 11 KB
/
simplifyDuplicatedGenesFastbox.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
function[model, changed_rules_ON]=simplifyDuplicatedGenesFastbox(model, taggene)
%% %(c) Maria Pires Pacheco 2023 et al -University of Luxembourg
% INPUTS
% model_target cobra model
% S m x n stoichiometric matrix
% lb n x 1 flux lower bound
% ub n x 1 flux upper bound
% rxns n x 1 cell array of reaction abbreviations
% c n x 1
% taggene == 1 if the model is a Recon model and the .1 has to be removed
if isfield(model, 'rxnGeneMat')
if numel(model.genes)~=size(model.rxnGeneMat,2)
disp('simplifyDuplicatedGenes_fastbox was not run')
return
end
end
[model]=removeUnusedGenesFastbox(model,taggene);
[genes,~,~] = unique(model.genes);
if numel(genes) < numel(model.genes)
%find non unique genes
[genes,ia,ic] = unique(model.genes);
for i=1:numel(ia)
dupidx = find(ic == i);
if numel(dupidx)>1
first=min(dupidx);
index=strfind(model.rules,strcat('x(',num2str(first),')'));
I = model.rules(not(cellfun('isempty',index)));
if~isempty(I)
model.rules(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(first),')'), strcat('x(',num2str(i),'*)'));
else
end
if isfield(model,'rules_back')
I = model.rules_back(not(cellfun('isempty',index)));% % %
if~isempty(I)
model.rules_back(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(first),')'), strcat('x(',num2str(i),'*)'));
else
end
end
dupidx=setdiff(dupidx,first);
for ii=1:numel(dupidx)
index=strfind(model.rules,strcat('x(',num2str(dupidx(ii)),')'));
I = model.rules(not(cellfun('isempty',index)));
if ~isempty(I)
model.rules(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(dupidx(ii)),')'), strcat('x(',num2str(i),'*)'));
else
end
if isfield(model,'rules_back')
I = model.rules_back(not(cellfun('isempty',index)));%
if ~isempty(I)
model.rules_back(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(dupidx(ii)),')'), strcat('x(',num2str(i),'*)'));
else
end
end
end
else
first=min(dupidx);
index=strfind(model.rules,strcat('x(',num2str(first),')'));
I = model.rules(not(cellfun('isempty',index)));
if ~isempty(I)
model.rules(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(first),')'), strcat('x(',num2str(i),'*)'));
else
end
if isfield(model,'rules_back')
I = model.rules_back(not(cellfun('isempty',index)));% % %
if ~isempty(I)
model.rules_back(not(cellfun('isempty',index)))=strrep(I, strcat('x(',num2str(first),')'), strcat('x(',num2str(i),'*)'));
else
end
end
end
end
model.rules=strrep(model.rules, '*','');
model.rules=strrep(model.rules, ' ','');
model.genes=genes;
if isfield(model,'rules_back')
model.rules_back=strrep(model.rules_back, '*','');
model.rules_back=strrep(model.rules_back, ' ','');
end
Index=strfind(model.rules, '&') ;
match=(cellfun('isempty',Index) & ~strcmp(model.rules, '')); % find statement only with OR
In=strfind( model.rules, '|');
match2=(not(cellfun('isempty',In)));
match=match & match2;
A=model.rules(match);
newA = cellfun(@(s) strsplit(s, '|'), A, 'UniformOutput', false); %or use regexp
new2=cell(size(newA,1),1);
for i=1:numel(newA)
%Cu = cellfun(@(y) unique(y{:} ), newA, 'UniformOutput',false)
C=unique(newA{i});
AllC = {cat(2, C{:})};
AllC= strrep(AllC, ')x', ') | x');
AllC= strrep(AllC, ')(x', ') | (x');
new2(i)=AllC;
end
%
model.rules(match)=new2;
Index=strfind(model.rules, '&') ;
%
match=not(cellfun('isempty',Index));
In=strfind( model.rules, '|');
match2=((cellfun('isempty',In)));
match=match & match2;
A=model.rules(match);
newA = cellfun(@(s) strsplit(s, '|'), A, 'UniformOutput', false); %or use regexp
new2=cell(size(newA,1),1);
for i=1:numel(newA)
C=unique(newA{i});
AllC = {cat(2, C{:})};
AllC= strrep(AllC, ')x', ') & x');
AllC= strrep(AllC, ')(x', ') & (x');
new2(i)=AllC;
end
model.rules(match)=new2;
if isfield(model, 'rules_back')
Index=strfind(model.rules_back, '&') ;
match=(cellfun('isempty',Index) & ~strcmp(model.rules, '')); % find statement only with OR
In=strfind( model.rules_back, '|');
match2=(not(cellfun('isempty',In)));
match=match & match2;
A=model.rules_back(match);
newA = cellfun(@(s) strsplit(s, '|'), A, 'UniformOutput', false); %or use regexp
new2=cell(size(newA,1),1);
for i=1:numel(newA)
%Cu = cellfun(@(y) unique(y{:} ), newA, 'UniformOutput',false)
C=unique(newA{i});
AllC = {cat(2, C{:})};
AllC= strrep(AllC, ')x', ') | x');
AllC= strrep(AllC, ')(x', ') | (x');
new2(i)=AllC;
end
model.rules_back(match)=new2;
Index=strfind(model.rules, '&') ; %
match=not(cellfun('isempty',Index));
In=strfind( model.rules_back, '|');
match2=((cellfun('isempty',In)));
match=match & match2;
A=model.rules_back(match);
newA = cellfun(@(s) strsplit(s, '|'), A, 'UniformOutput', false); %or use regexp
new2=cell(size(newA,1),1);
for i=1:numel(newA)
C=unique(newA{i});
AllC = {cat(2, C{:})};
AllC= strrep(AllC, ')x', ') & x');
AllC= strrep(AllC, ')(x', ') & (x');
new2(i)=AllC;
end
end%
end
model=buildRxnGeneMat(model);
mixed_rules=find(contains(model.rules, '&') & contains(model.rules, '|'));
OR_rules=find(~contains(model.rules, '&') & contains(model.rules, '|'));
AND_rules=find(contains(model.rules, '&') & ~contains(model.rules, '|'));
changed_rules_ON=zeros(numel(mixed_rules),1);
for i=1:numel(mixed_rules)
%%
[~,g]=find(model.rxnGeneMat(mixed_rules(i),:));
OrGenes=zeros(numel(g),1);
nb_AND = count(model.rules(mixed_rules(i)),'&');
nb_OR= count(model.rules(mixed_rules(i)),'|');
if numel(g)< (nb_OR + nb_AND +1)
for ii=1:numel(g)
x=zeros(numel(model.genes),1);
xm=x;
x(g(ii))=1;
xm(g(ii))=-1;
rr=mixed_rules(i);
mapping=zeros(numel(model.rxns),1);
for k=1:numel(model.rxns(rr))
mapping(rr(k),1)= GPRrulesMapper_rFASTCORMICS(cell2mat(model.rules(rr(k))),x);
end
mapping_m=zeros(numel(model.rxns),1);
for k=1:numel(model.rxns(rr))
mapping_m(rr(k),1)= GPRrulesMapper_rFASTCORMICS(cell2mat(model.rules(rr(k))),xm);
end
if sum(mapping(rr))==1 && sum(mapping_m(rr)==-1)==0
OrGenes(ii)=1;
end
if sum(mapping(rr))==1 && sum(mapping_m(rr)==-1)==1
OrGenes(ii)=2;
end
end
AndGenes=~OrGenes;
AndGenes2=zeros(size(AndGenes));
x=zeros(numel(model.genes),1);
x(g(AndGenes))=1;
xm=x;
AndGenes=find(AndGenes);
for iii=1:numel(AndGenes)
xm(g(AndGenes(iii)))=-1;
mapping=zeros(numel(model.rxns),1);
for k=1:numel(model.rxns(rr))
mapping(rr(k),1)= GPRrulesMapper_rFASTCORMICS(cell2mat(model.rules(rr(k))),x);
end
for k=1:numel(model.rxns(rr))
mapping_m(rr(k),1)= GPRrulesMapper_rFASTCORMICS(cell2mat(model.rules(rr(k))),xm);
end
if sum(mapping(rr))==1 && sum(mapping_m(rr) ~=0)==0
AndGenes2(iii)=1;
end
end
if sum(AndGenes2~=OrGenes)== numel(g)
gAnd=g(AndGenes);
if ~isempty(gAnd)
rules=strcat('(x(', num2str(gAnd(1)),')&x(',num2str(gAnd(2)),'))');
if numel(AndGenes)>2
else
OrGenes=find(OrGenes);
for o=1:numel(OrGenes)
rules=strcat(rules,'| x(', num2str(g(OrGenes(o))),')');
end
model.rules(rr)=cellstr(rules);
end
else
OrGenes=find(OrGenes);
OrGenes=find(OrGenes);
rules=strcat('x(', num2str(g(OrGenes(1))),')|x(',num2str(g(OrGenes(2))),')');
for o=3:numel(OrGenes)
rules=strcat(rules,'| x(', num2str(g(OrGenes(o))),')');
end
model.rules(rr)=cellstr(rules);
model.rules(rr)=cellstr(rules);
changed_rules_ON(mixed_rules(i))=1;
end
else
SuperOR=find(OrGenes==2);
if~isempty(SuperOR)
rules=strcat('x(', num2str(g(SuperOR(1))),')');
model.rules(rr)=cellstr(rules);
changed_rules_ON(mixed_rules(i))=1;
end
end
%GLCNACT2g
end
end
mixed_rules_after=find(contains(model.rules, '&') & contains(model.rules, '|'));
to_check=setdiff(1:numel(model.rxns), mixed_rules_after);
for iv=1:numel(to_check)
[~,g]=find(model.rxnGeneMat(to_check(iv), :));
if numel(unique(g))< numel(g)
'warning';
end
end
for i=1:numel(AND_rules)
g=unique(find(model.rxnGeneMat(AND_rules(i),:)));
r=strcat('x(',num2str(g(1)),')');
if numel(g)>1
for ii=2:numel(g)
r =strcat(r,'&x(',num2str(g(ii)),')');
end
end
model.rules(AND_rules(i))=cellstr(r);
end
for i=1:numel(OR_rules)
g=unique(find(model.rxnGeneMat(OR_rules(i),:)));
r=strcat('x(',num2str(g(1)),')');
if numel(g)>1
for ii=2:numel(g)
r =strcat(r,'|x(',num2str(g(ii)),')') ;
end
end
model.rules(OR_rules(i))=cellstr(r);
end
if isfield(model, 'genes')
model=removeUnusedGenes(model);
end
model = creategrRulesField(model);
model = buildRxnGeneMat(model);
end