0001 function [grRules,rxnGeneMat,indexes2check] = standardizeGrRules(model,embedded)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 n = length(model.rxns);
0026 [g,~] = size(model.genes);
0027 rxnGeneMat = sparse(n,g);
0028 grRules = cell(n,1);
0029 genes = model.genes;
0030 if nargin<2
0031 embedded = false;
0032 end
0033
0034 if isfield(model,'grRules')
0035 originalGrRules=model.grRules;
0036 originalGrRules=grRulesPreparation(originalGrRules);
0037
0038 indexes2check = findPotentialErrors(originalGrRules,embedded,model);
0039
0040 for i=1:length(originalGrRules)
0041 originalSTR = originalGrRules{i};
0042 grRules{i,:} = originalSTR;
0043
0044 genesSets = getSimpleGeneSets(originalSTR);
0045 rxnGeneMat = modifyRxnGeneMat(genesSets,genes,rxnGeneMat,i);
0046
0047 if ~ismember(i,indexes2check)
0048 newSTR = [];
0049 if ~isempty(genesSets)
0050
0051 for j=1:length(genesSets)
0052 simpleSet = genesSets{j};
0053
0054 if length(genesSets)>1
0055 if ~isempty(strfind(simpleSet,' and '))
0056 simpleSet = horzcat('(',simpleSet,')');
0057 end
0058 end
0059
0060
0061 if j<length(genesSets)
0062 newSTR = [newSTR, simpleSet, ' or '];
0063
0064 else
0065 newSTR = [newSTR, simpleSet];
0066 end
0067 end
0068
0069 grRules{i} = char(newSTR);
0070 end
0071 end
0072 end
0073 else
0074 error('The model does not have a grRules field.')
0075 end
0076
0077 end
0078
0079
0080
0081 function genesSets = getSimpleGeneSets(originalSTR)
0082 genesSets = [];
0083
0084 if ~isempty(originalSTR)
0085 originalSTR = strtrim(originalSTR);
0086
0087 originalSTR = strrep(originalSTR,'(','');
0088 originalSTR = strrep(originalSTR,')','');
0089
0090 genesSets = transpose(strsplit(originalSTR,' or '));
0091 end
0092 end
0093
0094
0095
0096
0097 function rxnGeneMat = modifyRxnGeneMat(genesSets,modelGenes,rxnGeneMat,i)
0098
0099 if ~isempty(genesSets)
0100 for j=1:length(genesSets)
0101 simpleSet = genesSets{j};
0102
0103
0104 STR = strrep(simpleSet,') and (',' and ');
0105 genes = strsplit(STR,' ');
0106 for k=1:length(genes)
0107 if ~strcmpi(genes(k),' and ')
0108
0109 genePos = find(strcmpi(modelGenes,genes(k)));
0110 if ~isempty(genePos)
0111 rxnGeneMat(i,genePos) = 1;
0112
0113
0114
0115 end
0116 end
0117 end
0118 end
0119 end
0120 end
0121
0122
0123
0124 function indexes2check = findPotentialErrors(grRules,embedded,model)
0125 indxs_l = find(~cellfun(@isempty,strfind(grRules,') and (')));
0126 indxs_l_L = find(~cellfun(@isempty,strfind(grRules,') and')));
0127 indxs_l_R = find(~cellfun(@isempty,strfind(grRules,'and (')));
0128 indexes2check = vertcat(indxs_l,indxs_l_L,indxs_l_R);
0129 indexes2check = unique(indexes2check);
0130
0131 if ~isempty(indexes2check)
0132
0133 if ~embedded
0134 STR = 'Potentially problematic ") AND (", ") AND" or "AND ("relat';
0135 STR = [STR,'ionships found in\n\n'];
0136 for i=1:length(indexes2check)
0137 index = indexes2check(i);
0138 STR = [STR ' - grRule #' model.rxns{index} ': ' grRules{index} '\n'];
0139 end
0140 STR = [STR,'\n This kind of relationships should only be present '];
0141 STR = [STR,'in reactions catalysed by complexes of isoenzymes e'];
0142 STR = [STR,'.g.\n\n - (G1 or G2) and (G3 or G4)\n\n For these c'];
0143 STR = [STR,'ases modify the grRules manually, writing all the po'];
0144 STR = [STR,'ssible combinations e.g.\n\n - (G1 and G3) or (G1 a'];
0145 STR = [STR,'nd G4) or (G2 and G3) or (G2 and G4)\n\n For other c'];
0146 STR = [STR,'ases modify the correspondent grRules avoiding:\n\n '];
0147 STR = [STR,' 1) Overall container brackets, e.g.\n "(G1 a'];
0148 STR = [STR,'nd G2)" should be "G1 and G2"\n\n 2) Single unit en'];
0149 STR = [STR,'zymes enclosed into brackets, e.g.\n "(G1)" s'];
0150 STR = [STR,'hould be "G1"\n\n 3) The use of uppercases for logi'];
0151 STR = [STR,'cal operators, e.g.\n "G1 OR G2" should be "G'];
0152 STR = [STR,'1 or G2"\n\n 4) Unbalanced brackets, e.g.\n '];
0153 STR = [STR,'"((G1 and G2) or G3" should be "(G1 and G2) or G3"\n'];
0154 warning(sprintf(STR))
0155 end
0156 end
0157 end
0158
0159 function grRules = grRulesPreparation(grRules)
0160
0161 grRules=strrep(grRules,' ',' ');
0162 grRules=strrep(grRules,'( ','(');
0163 grRules=strrep(grRules,' )',')');
0164
0165 grRules=strrep(grRules,' AND ',' and ');
0166 grRules=strrep(grRules,' OR ',' or ');
0167 end