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 (optional, default model.rxns) deleteOriginal true if the original reactions should be removed (making it move the reactions instead) (optional, default false) compNames cell array of compartment names. This is used if new compartments should be added (optional, 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 (optional, 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)
0001 function model=copyToComps(model,toComps,rxns,deleteOriginal,compNames,compOutside) 0002 % copyToComps 0003 % Copies reactions to new compartment(s) 0004 % 0005 % model a model structure 0006 % toComps cell array of compartment ids. If there is no match 0007 % to model.comps then it is added as a new compartment 0008 % (see below for details) 0009 % rxns either a cell array of reaction IDs, a logical vector 0010 % with the same number of elements as reactions in the model, 0011 % or a vector of indexes to remove (optional, default 0012 % model.rxns) 0013 % deleteOriginal true if the original reactions should be removed 0014 % (making it move the reactions instead) (optional, default 0015 % false) 0016 % compNames cell array of compartment names. This is used if new 0017 % compartments should be added (optional, default toComps) 0018 % compOutside cell array of the id (as in comps) for the compartment 0019 % surrounding each of the compartments. This is used if 0020 % new compartments should be added (optional, default all {''}) 0021 % 0022 % model an updated model structure 0023 % 0024 % NOTE: New reactions and metabolites will be named as "id_toComps(i)". 0025 % 0026 % Usage: model=copyToComps(model,toComps,rxns,deleteOriginal,compNames,compOutside) 0027 0028 if nargin<3 0029 rxns=model.rxns; 0030 elseif ~islogical(rxns) && ~isnumeric(rxns) 0031 rxns=convertCharArray(rxns); 0032 end 0033 if nargin<4 0034 deleteOriginal=false; 0035 end 0036 if nargin<5 0037 compNames=toComps; 0038 else 0039 compNames=convertCharArray(compNames); 0040 end 0041 if nargin<6 0042 compOutside=cell(numel(toComps),1); 0043 compOutside(:)={''}; 0044 else 0045 compOutside=convertCharArray(compOutside); 0046 end 0047 0048 originalID=model.id; 0049 originalName=model.name; 0050 0051 rxns=getIndexes(model,rxns,'rxns'); 0052 0053 for i=1:numel(toComps) 0054 %Check if the compartment exists, otherwise add it 0055 [I,J]=ismember(toComps(i),model.comps); 0056 if I==false 0057 model.comps=[model.comps;toComps(i)]; 0058 model.compNames=[model.compNames;compNames(i)]; 0059 if isfield(model,'compOutside') 0060 model.compOutside=[model.compOutside;compOutside(i)]; 0061 end 0062 if isfield(model,'compMiriams') 0063 model.compMiriams=[model.compMiriams;cell(1,1)]; 0064 end 0065 J=numel(model.comps); 0066 end 0067 %Copy the reactions by making a model structure with only them, then 0068 %change the localization, and finally merge with the original model 0069 modelToAdd=model; 0070 modelToAdd=removeReactions(modelToAdd,setdiff(1:numel(model.rxns),rxns),true,true); 0071 modelToAdd.rxns=strcat(modelToAdd.rxns,'_',toComps(i)); 0072 modelToAdd.mets=strcat(modelToAdd.mets,'_',toComps(i)); 0073 modelToAdd.comps=modelToAdd.comps(J); 0074 modelToAdd.compNames=modelToAdd.compNames(J); 0075 if isfield(modelToAdd,'compOutside') 0076 modelToAdd.compOutside=modelToAdd.compOutside(J); 0077 end 0078 if isfield(modelToAdd,'compMiriams') 0079 modelToAdd.compMiriams=modelToAdd.compMiriams(J); 0080 end 0081 modelToAdd.metComps=ones(numel(modelToAdd.mets),1); 0082 0083 %Merge the models 0084 model=mergeModels({model;modelToAdd},'metNames'); 0085 end 0086 0087 model=rmfield(model,'rxnFrom'); 0088 model=rmfield(model,'metFrom'); 0089 model=rmfield(model,'geneFrom'); 0090 0091 if deleteOriginal==true 0092 model=removeReactions(model,rxns,true,true,true); %Also delete unused compartments 0093 end 0094 0095 model.id=originalID; 0096 model.name=originalName; 0097 end