mergeCompartments Merge all compartments in a model model a model structure keepUnconstrained keep metabolites that are unconstrained in a 'unconstrained' compartment. If these are merged the exchange reactions will most often be deleted (opt, default false) deleteRxnsWithOneMet delete reactions with only one metabolite. These reactions come from reactions such as A[c] + B[c] => A[m]. In some models hydrogen is balanced around each membrane with reactions like this (opt, default false) model a model with all reactions located to one compartment deletedRxns reactions that were deleted because of only having one metabolite after merging Merges all compartments into one 's' compartment (for 'System'). This can be useful for example to ensure that there are metabolic capabilities to synthesize all metabolites. NOTE: If the metabolite IDs reflect the compartment that they are in the IDs may no longer be representative. Usage: model=mergeCompartments(model,keepUnconstrained,deleteRxnsWithOneMet) Rasmus Agren, 2013-02-06
0001 function [model deletedRxns]=mergeCompartments(model,keepUnconstrained,deleteRxnsWithOneMet) 0002 % mergeCompartments 0003 % Merge all compartments in a model 0004 % 0005 % model a model structure 0006 % keepUnconstrained keep metabolites that are unconstrained in a 0007 % 'unconstrained' compartment. If these are merged the 0008 % exchange reactions will most often be deleted (opt, 0009 % default false) 0010 % deleteRxnsWithOneMet delete reactions with only one metabolite. These 0011 % reactions come from reactions such as A[c] + B[c] 0012 % => A[m]. In some models hydrogen is balanced around 0013 % each membrane with reactions like this (opt, 0014 % default false) 0015 % 0016 % model a model with all reactions located to one compartment 0017 % deletedRxns reactions that were deleted because of only 0018 % having one metabolite after merging 0019 % 0020 % Merges all compartments into one 's' compartment (for 'System'). This can 0021 % be useful for example to ensure that there are metabolic capabilities to 0022 % synthesize all metabolites. 0023 % 0024 % NOTE: If the metabolite IDs reflect the compartment that they are in 0025 % the IDs may no longer be representative. 0026 % 0027 % Usage: model=mergeCompartments(model,keepUnconstrained,deleteRxnsWithOneMet) 0028 % 0029 % Rasmus Agren, 2013-02-06 0030 % 0031 0032 if nargin<2 0033 keepUnconstrained=false; 0034 end 0035 if nargin<3 0036 deleteRxnsWithOneMet=false; 0037 end 0038 0039 if ~isfield(model,'unconstrained') 0040 keepUnconstrained=false; 0041 end 0042 0043 %Keep track of which reactions only contained one metabolite before the 0044 %merging as they are probable exchange reactions. 0045 if deleteRxnsWithOneMet==true 0046 reservedRxns=model.rxns(sum(model.S~=0)==1); 0047 if ~isempty(reservedRxns) && isfield(model,'unconstrained') 0048 %If there is no unconstrained field these reactions are probably 0049 %exchange reactions and shall be kept. If not then print a warning 0050 fprintf('WARNING: There are reactions with only one metabolite. Cannot determine whether they are exchange reactions since there is no unconstrained field.\n'); 0051 end 0052 end 0053 0054 %Loop through each metabolite, and if it's not unconstrained then change 0055 %the S matrix to use the metabolite with the lowest index in model.comps 0056 %instead 0057 uNames=unique(model.metNames); 0058 for i=1:numel(uNames) 0059 %Find all metabolites with this name.. 0060 I=ismember(model.metNames,uNames(i)); 0061 0062 %Find the first of those that is not unconstrained. This is the one 0063 %that the other "un-unconstrained" should be changed to. 0064 if keepUnconstrained==true 0065 mergeTo=find(I & model.unconstrained==false,1); 0066 0067 %This could happen if there is only one metabolite and it's 0068 %unconstrained 0069 if isempty(mergeTo) 0070 continue; 0071 end 0072 else 0073 mergeTo=find(I,1); 0074 end 0075 I(mergeTo)=false; %Don't do anything for itself 0076 I=find(I); 0077 0078 %Go through each of the metabolites with this name and update them to 0079 %mergeTo 0080 for j=1:numel(I) 0081 if keepUnconstrained==true && model.unconstrained(I(j))==true 0082 continue; 0083 end 0084 %Update the S matrix 0085 model.S(mergeTo,:)=model.S(mergeTo,:)+model.S(I(j),:); 0086 model.S(I(j),:)=0; 0087 end 0088 end 0089 0090 %Remove all metabolites that are no longer used (with a bit of a trick) 0091 model=removeRxns(model,{},true); 0092 0093 %Update all mets so that they are in compartment "s" with id "1" 0094 model.compNames={'System'}; 0095 model.comps={'s'}; 0096 model.compOutside={''}; 0097 model.compMiriams={[]}; 0098 model.metComps=ones(numel(model.mets),1); 0099 0100 %Add exchange mets to another compartment "b" with id "2" 0101 if keepUnconstrained==true 0102 model.compNames=[model.compNames; {'Unconstrained'}]; 0103 model.comps=[model.comps; {'b'}]; 0104 model.compOutside=[model.compOutside; {'s'}]; 0105 model.compMiriams=[model.compMiriams; {[]}]; 0106 model.metComps(model.unconstrained~=0)=2; 0107 end 0108 0109 %Transport reactions on the form A <=> B will have been deleted by the 0110 %merging. Remove those reactions 0111 model=removeMets(model,{},false,true,true); 0112 0113 if deleteRxnsWithOneMet==true 0114 I=model.rxns(sum(model.S~=0)==1); 0115 0116 %Delete the reactions that contain only one metabolite after the 0117 %merging but not before 0118 deletedRxns=setdiff(I,reservedRxns); 0119 model=removeRxns(model,deletedRxns,true,true); 0120 else 0121 deletedRxns={}; 0122 end 0123 0124 %And then finally merge the identical reactions 0125 model=contractModel(model); 0126 end