forked from SysBioChalmers/RAVEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyToComps.m
executable file
·87 lines (80 loc) · 3.09 KB
/
copyToComps.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
function model=copyToComps(model,toComps,rxns,deleteOriginal,compNames,compOutside)
% copyToComps
% Copies reactions to new compartment(s)
%
% model a model structure
% toComps cell array of compartment ids. If there is no match
% to model.comps then it is added as a new compartment
% (see below for details)
% rxns either a cell array of reaction IDs, a logical vector
% with the same number of elements as reactions in the model,
% or a vector of indexes to remove (opt, default
% model.rxns)
% deleteOriginal true if the original reactions should be removed
% (making it move the reactions instead) (opt, default
% false)
% compNames cell array of compartment names. This is used if new
% compartments should be added (opt, default toComps)
% compOutside cell array of the id (as in comps) for the compartment
% surrounding each of the compartments. This is used if
% new compartments should be added (opt, default all {''})
%
% model an updated model structure
%
% NOTE: New reactions and metabolites will be named as "id_toComps(i)".
%
% Usage: model=copyToComps(model,toComps,rxns,deleteOriginal,compNames,compOutside)
if nargin<3
rxns=model.rxns;
end
if nargin<4
deleteOriginal=false;
end
if nargin<5
compNames=toComps;
end
if nargin<6
compOutside=cell(numel(toComps),1);
compOutside(:)={''};
end
originalID=model.id;
originalName=model.name;
rxns=getIndexes(model,rxns,'rxns');
for i=1:numel(toComps)
%Check if the compartment exists, otherwise add it
[I J]=ismember(toComps(i),model.comps);
if I==false
model.comps=[model.comps;toComps(i)];
model.compNames=[model.compNames;compNames(i)];
if isfield(model,'compOutside')
model.compOutside=[model.compOutside;compOutside(i)];
end
if isfield(model,'compMiriams')
model.compMiriams=[model.compMiriams;cell(1,1)];
end
J=numel(model.comps);
end
%Copy the reactions by making a model structure with only them, then
%change the localization, and finally merge with the original model
modelToAdd=model;
modelToAdd=removeReactions(modelToAdd,setdiff(1:numel(model.rxns),rxns),true,true);
modelToAdd.rxns=strcat(modelToAdd.rxns,'_',toComps(i));
modelToAdd.mets=strcat(modelToAdd.mets,'_',toComps(i));
modelToAdd.comps=modelToAdd.comps(J);
modelToAdd.compNames=modelToAdd.compNames(J);
if isfield(modelToAdd,'compOutside')
modelToAdd.compOutside=modelToAdd.compOutside(J);
end
if isfield(modelToAdd,'compMiriams')
modelToAdd.compMiriams=modelToAdd.compMiriams(J);
end
modelToAdd.metComps=ones(numel(modelToAdd.mets),1);
%Merge the models
model=mergeModels({model;modelToAdd},'metNames');
end
if deleteOriginal==true
model=removeReactions(model,rxns,true,true,true); %Also delete unused compartments
end
model.id=originalID;
model.name=originalName;
end