0001 function [S, mets, badRxns, reversible]=constructS(equations,mets,rxns)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 equations=convertCharArray(equations);
0026 switch nargin
0027 case 2
0028 mets=convertCharArray(mets);
0029 case 3
0030 rxns=convertCharArray(rxns);
0031 end
0032
0033 badRxns=false(numel(equations),1);
0034
0035
0036 I=cellfun(@numel,equations);
0037 I=find(I<4,1);
0038 if any(I)
0039 if isempty(rxns)
0040 EM=['The following equation does not have reversibility data: ' equations{I} ];
0041 dispEM(EM);
0042 else
0043 EM=['The reaction ' rxns{I} ' does not have reversibility data'];
0044 dispEM(EM);
0045 end
0046 end
0047
0048
0049 equations=strtrim(equations);
0050 equations=fixEquations(equations);
0051
0052 if nargin<2
0053 mets=parseRxnEqu(equations);
0054 end
0055 if nargin<3
0056 rxns=[];
0057 end
0058
0059
0060 reversible=cellfun(@any,strfind(equations,' <=> '));
0061
0062
0063 equations=strrep(equations,' => ',' <=> ');
0064
0065
0066
0067 equations=strrep(equations,' + ', '€');
0068
0069
0070 S=zeros(numel(mets),numel(equations));
0071
0072
0073 metsToS = cell(100000,1);
0074 rxnsToS = zeros(100000,1);
0075 coefToS = zeros(100000,1);
0076 newEntry = 1;
0077
0078 for i=1:numel(equations)
0079
0080 arrowIndex=strfind(equations{i},' <=> ');
0081
0082 if numel(arrowIndex)~=1
0083 if isempty(rxns)
0084 EM=['The following equation does not have reversibility data: ' equations{i} ];
0085 dispEM(EM);
0086 else
0087 EM=['The reaction ' rxns{i} ' does not have reversibility data'];
0088 dispEM(EM);
0089 end
0090 end
0091
0092 reactants=regexp(equations{i}(1:arrowIndex-1),'€','split');
0093 products=regexp(equations{i}(arrowIndex+5:end),'€','split');
0094
0095
0096
0097 reactants(cellfun(@isempty,reactants))=[];
0098 products(cellfun(@isempty,products))=[];
0099
0100
0101
0102 multiplyWith=[ones(numel(reactants),1)*-1; ones(numel(products),1)];
0103
0104 metabolites=strtrim([reactants products]);
0105
0106
0107
0108 for j=1:numel(metabolites)
0109 space=strfind(metabolites{j},' ');
0110
0111 if isempty(space)
0112
0113 coeff=1;
0114 name=metabolites{j};
0115 else
0116 coeff=str2double(metabolites{j}(1:space(1)));
0117
0118
0119 if isnan(coeff)
0120 coeff=1;
0121 name=strtrim(metabolites{j});
0122 else
0123 name=strtrim(metabolites{j}(space(1)+1:end));
0124 end
0125 end
0126
0127
0128 metsToS{newEntry}=name;
0129 rxnsToS(newEntry)=i;
0130 coefToS(newEntry)=coeff*multiplyWith(j);
0131 newEntry=newEntry+1;
0132 end
0133 end
0134
0135 metsToS(newEntry:end)=[];
0136 rxnsToS(newEntry:end)=[];
0137 coefToS(newEntry:end)=[];
0138
0139
0140 [metsPresent,metsLoc]=ismember(metsToS,mets);
0141
0142
0143 [~,I]=unique([rxnsToS,metsLoc],'rows','stable');
0144 x=1:length(rxnsToS);
0145 x(I)=[];
0146 x=unique(rxnsToS(x));
0147 badRxns(x)=true;
0148
0149 if any(~metsPresent)
0150 if isempty(rxns)
0151 error(['Could not find the following metabolites in the metabolite list: ',...
0152 strjoin(unique(metsToS(~metsPresent)),', ')],'')
0153 else
0154 missingMet = find(~metsPresent);
0155 missingMet = strcat(metsToS(missingMet),' (reaction:',rxns(rxnsToS(missingMet)),')\n');
0156 missingMet = strjoin(missingMet,'');
0157 error(['Could not find the following metabolites (reaction indicated) in the metabolite list: \n' ...
0158 missingMet '%s'],'');
0159 end
0160 end
0161 linearIndices=sub2ind(size(S),metsLoc,rxnsToS);
0162 S(linearIndices)=coefToS;
0163 S=sparse(S);
0164 end
0165
0166 function equ=fixEquations(equ)
0167
0168
0169
0170 equ=equ(:);
0171 for i=1:numel(equ)
0172 if strcmp(equ{i}(1:2),'=>') || strcmp(equ{i}(1:3),'<=>')
0173 equ{i}=[' ' equ{i}];
0174 else
0175 if strcmp(equ{i}(end-1:end),'=>') || strcmp(equ{i}(end-2:end),'<=>')
0176 equ{i}=[equ{i} ' '];
0177 end
0178 end
0179 end
0180 end