0001 function reducedModel = removeGenes(model,genesToRemove,removeUnusedMets,removeBlockedRxns,standardizeRules)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if nargin<3
0021 removeUnusedMets = false;
0022 end
0023 if nargin<4
0024 removeBlockedRxns = false;
0025 end
0026 if nargin<5
0027 standardizeRules = true;
0028 end
0029
0030 if standardizeRules
0031 [grRules,rxnGeneMat,toCheck] = standardizeGrRules(model,true);
0032 model.grRules = grRules;
0033 model.rxnGeneMat = rxnGeneMat;
0034 else
0035 toCheck = [];
0036 rxnGeneMat = model.rxnGeneMat;
0037 end
0038 reducedModel = model;
0039
0040 if ~(islogical(genesToRemove) || isnumeric(genesToRemove))
0041 genesToRemove=convertCharArray(genesToRemove);
0042 genesToRemove=genesToRemove(ismember(genesToRemove,model.genes));
0043 end
0044 if ~isempty(genesToRemove)
0045 indexesToRemove = getIndexes(model,genesToRemove,'genes');
0046 if ~isempty(indexesToRemove)
0047
0048 reducedModel.rxnGeneMat(:,indexesToRemove) = 0;
0049 genes = model.genes(indexesToRemove);
0050
0051 if ~isempty(toCheck)
0052 for i=1:numel(toCheck)
0053 index = toCheck(i);
0054 gIdxs = find(rxnGeneMat(index,:));
0055 g2check = model.genes(gIdxs);
0056 if any(ismember(g2check,genes))
0057 warning(['Conflicting grRule #' num2str(index) ' (' model.rxns{index} ') contains at least one of the genes to be removed, this grRule will be bypassed in order to avoid logical errors'])
0058 end
0059 end
0060 end
0061 canCarryFlux = true(size(model.rxns));
0062
0063 for i = 1:length(genes)
0064
0065 geneRxns = find(rxnGeneMat(:,indexesToRemove(i)));
0066 if ~isempty(geneRxns)
0067 for j = 1:numel(geneRxns)
0068 index = geneRxns(j);
0069 grRule = reducedModel.grRules{index};
0070 ruleGenes = reducedModel.genes(logical(rxnGeneMat(index,:)));
0071 if ~ismember(index,toCheck) && canCarryFlux(index) && ~isempty(grRule)
0072
0073 canCarryFlux(index) = canRxnCarryFlux(ruleGenes,grRule,genes{i});
0074
0075 grRule = removeGeneFromRule(grRule,genes{i});
0076 reducedModel.grRules{index} = grRule;
0077 end
0078 end
0079 end
0080 end
0081
0082 if removeBlockedRxns
0083 rxnsToRemove = reducedModel.rxns(~canCarryFlux);
0084 reducedModel = removeReactions(reducedModel,rxnsToRemove,removeUnusedMets,true);
0085 else
0086 reducedModel = removeReactions(reducedModel,[],removeUnusedMets,true);
0087 reducedModel.lb(~canCarryFlux) = 0;
0088 reducedModel.ub(~canCarryFlux) = 0;
0089 end
0090 end
0091 end
0092
0093 if standardizeRules
0094 [grRules,rxnGeneMat] = standardizeGrRules(reducedModel,true);
0095 reducedModel.grRules = grRules;
0096 reducedModel.rxnGeneMat = rxnGeneMat;
0097 end
0098 end
0099
0100 function canIt = canRxnCarryFlux(ruleGenes,geneRule,geneToRemove)
0101
0102
0103
0104 geneRule = [' ', geneRule, ' '];
0105 for i = 1:length(ruleGenes)
0106 if strcmp(ruleGenes{i},geneToRemove)
0107 geneRule = strrep(geneRule,[' ' ruleGenes{i} ' '],' false ');
0108 geneRule = strrep(geneRule,['(' ruleGenes{i} ' '],'(false ');
0109 geneRule = strrep(geneRule,[' ' ruleGenes{i} ')'],' false)');
0110 else
0111 geneRule = strrep(geneRule,[' ' ruleGenes{i} ' '],' true ');
0112 geneRule = strrep(geneRule,['(' ruleGenes{i} ' '],'(true ');
0113 geneRule = strrep(geneRule,[' ' ruleGenes{i} ')'],' true)');
0114 end
0115 end
0116 geneRule = strtrim(geneRule);
0117 geneRule = strrep(geneRule,'and','&&');
0118 geneRule = strrep(geneRule,'or','||');
0119 canIt = eval(geneRule);
0120 end
0121
0122 function geneRule = removeGeneFromRule(geneRule,geneToRemove)
0123
0124
0125 geneSets = strsplit(geneRule,' or ');
0126 hasGene = ~cellfun(@isempty,strfind(geneSets,geneToRemove));
0127 geneSets = geneSets(~hasGene);
0128 geneRule = strjoin(geneSets,' or ');
0129 end