Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/change_model/applyCustomKcats.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
model.ec.kcat(rxnIdxs) = customKcats.kcat(i);

% Add note mentioning manual kcat change
model.ec.source(rxnIdxs) = {'custom'};
model.ec.source(rxnIdxs) = {formatKcatSource('custom')};
if isfield(customKcats,'notes')
matchedIdxs = find(rxnIdxs);
for j = 1:numel(matchedIdxs)
Expand Down Expand Up @@ -202,7 +202,7 @@
model.ec.kcat(rxnIdxs(j)) = customKcats.kcat(i);

% Add note mentioning manual kcat change
model.ec.source{rxnIdxs(j),1} = 'custom';
model.ec.source{rxnIdxs(j),1} = formatKcatSource('custom');
if isfield(customKcats,'notes')
model.ec.notes{rxnIdxs(j), 1} = appendNote(model.ec.notes{rxnIdxs(j), 1}, customKcats.notes{i});
end
Expand Down
2 changes: 1 addition & 1 deletion src/change_model/getKcatAcrossIsozymes.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
newKcat = noKcatVal(newKcat);

model.ec.kcat(newKcatIdx) = newKcat;
model.ec.source(newKcatIdx) = {'isozymes'};
model.ec.source(newKcatIdx) = {formatKcatSource('isozymes')};
end

4 changes: 2 additions & 2 deletions src/change_model/setKcatForReactions.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
% ecModel where selected kcat values in ecModel.ec.kcat are changed, but
% not yet applied to the S-matrix (will require to run
% applyKcatConstraints). ecModel.ec.source for the changed reactions
% will read 'setKcatForReactions'.
% will read 'manual', without any tuning status.
%
% Examples
% --------
Expand Down Expand Up @@ -59,5 +59,5 @@
end
end
ecModel.ec.kcat(rxnsToChange) = kcat;
ecModel.ec.source(rxnsToChange) = {'setKcatForReactions'};
ecModel.ec.source(rxnsToChange) = {formatKcatSource('manual')};
end
29 changes: 15 additions & 14 deletions src/gather_kcats/assignKcatValues.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
% The source is written in lower case snake_case, with the wildcard
% level and origin of the entry in brackets when it has them
% (kcatList.wildcardLvl and kcatList.origin), for example
% 'brenda (wc=0, origin=1)'. Functions that compare model.ec.source with
% a source name use the part before ' (' (see resolveTunableContext).
% 'brenda (wc=0, origin=1)' (see formatKcatSource). Functions that compare
% model.ec.source with a source name use its origin (see parseKcatSource
% and resolveTunableContext).
% overwrite : char
% whether existing kcat values should be overwritten. Options: 'true',
% 'false', 'ifHigher'. The last option will overwrite only if the new
Expand Down Expand Up @@ -130,7 +131,7 @@
otherwise
error('Invalid criteria specified')
end
selectedSource{i} = formatKcatSource(kcatList.kcatSource{idxMatch(j)}, ...
selectedSource{i} = matchSource(kcatList.kcatSource{idxMatch(j)}, ...
wildcardLvl(idxMatch(j)), origin(idxMatch(j)));
end

Expand Down Expand Up @@ -158,16 +159,16 @@
rxnIdx = idxInModelUnique;
end

function text = formatKcatSource(source, wildcardLvl, origin)
% The provenance text of model.ec.source: the source label in lower case
% snake_case, followed by the wildcard level and origin of a fuzzy BRENDA
% match in brackets, e.g. 'brenda (wc=0, origin=1)'.
text = lower(strtrim(char(source)));
text = regexprep(text, '[^0-9a-z]+', '_');
text = regexprep(text, '^_+|_+$', '');
if ~isnan(wildcardLvl) && ~isnan(origin)
text = sprintf('%s (wc=%d, origin=%d)', text, fix(wildcardLvl), fix(origin));
elseif ~isnan(wildcardLvl)
text = sprintf('%s (wc=%d)', text, fix(wildcardLvl));
function text = matchSource(source, wildcardLvl, origin)
% The provenance text of model.ec.source: the source label followed by the
% wildcard level and origin of a fuzzy BRENDA match, as formatKcatSource
% writes them.
detail = struct();
if ~isnan(wildcardLvl)
detail.wc = fix(wildcardLvl) + 0;
if ~isnan(origin)
detail.origin = fix(origin) + 0;
end
end
text = formatKcatSource(char(source), detail);
end
9 changes: 7 additions & 2 deletions src/gather_kcats/collectKcats.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@
end
[~, idx] = ismember(c.rxns, ecModel.ec.rxns);
ecModel.ec.kcat(idx) = c.kcats;
ecModel.ec.source(idx) = cellstr(compose('combined (sd=%.2f, n=%d)', c.sd, c.n));
for k = 1:numel(idx)
ecModel.ec.source{idx(k)} = formatKcatSource('combined', ...
struct('sd', sprintf('%.2f', c.sd(k)), 'n', c.n(k)));
end
stage(idx) = {sprintf('%d', s)};
candidates(idx) = c.n;
spread(idx) = c.spread;
Expand Down Expand Up @@ -263,7 +266,9 @@
candidates(end+1:n) = 0;
spread(end+1:n) = NaN;
sd(end+1:n) = NaN;
stage(strcmp(ecModel.ec.source, 'standard') & ecModel.ec.kcat > 0) = {'standard'};
sourceInfo = parseKcatSource(ecModel.ec.source);
isStandard = strcmp({sourceInfo.origin}', 'standard') & cellfun(@isempty, {sourceInfo.tuned}');
stage(isStandard & ecModel.ec.kcat > 0) = {'standard'};
end

decisions = table(ecModel.ec.rxns(:), stage(:), ecModel.ec.source(:), ecModel.ec.kcat(:), ...
Expand Down
103 changes: 103 additions & 0 deletions src/gather_kcats/formatKcatSource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
function text = formatKcatSource(origin, detail, tuned, prior)
% formatKcatSource Write the provenance text of a kcat in model.ec.source.
%
% The text has the form
%
% <origin>[ (<detail>)][ | tuned=<method>[+<method>...] (prior=<value>)]
%
% for instance 'brenda (wc=0, origin=1)', 'combined (sd=0.61, n=6)' or
% 'brenda (wc=0, origin=2) | tuned=cmaes (prior=6.55)'. The origin and the
% detail keys are written in lower case, with every run of characters other
% than letters and digits replaced by one underscore, so 'OKP-CataPro' gives
% 'okp_catapro'. parseKcatSource reads the text back.
%
% Parameters
% ----------
% origin : char
% where the kcat comes from, e.g. 'brenda', 'custom' or 'standard'. An
% empty origin gives '' when there is no detail and no tuning status,
% and 'unknown' otherwise.
% detail : struct
% scalar struct whose fields are the detail keys, in output order, e.g.
% struct('wc', 0, 'origin', 1). A number is written with '%.6g' and
% text as it is, so a fixed number of decimals is given as text, e.g.
% sprintf('%.2f', sd). A field whose value is NaN, Inf or [] is left out.
% A field is also left out when its name does not start with a letter
% after the normalisation. The brackets are left out when no field is
% left (default []).
% tuned : cell or char
% the tuning methods that changed the kcat, in the order of application,
% e.g. {'sensitivity','cmaes'} (default {}).
% prior : double
% the kcat before the first tuning, written when tuned is not empty and
% the prior is a finite number; NaN, Inf or [] writes none (default []).
%
% Returns
% -------
% text : char
% the provenance text.
%
% Examples
% --------
% formatKcatSource('standard')
% formatKcatSource('brenda', struct('wc', 0, 'origin', 1))
% formatKcatSource('combined', struct('sd', sprintf('%.2f', 0.61), 'n', 6))
% formatKcatSource('brenda', [], {'cmaes'}, 6.55)
%
% See also
% --------
% parseKcatSource, assignKcatValues, sensitivityTuning
if nargin < 2
detail = [];
end
if nargin < 3 || isempty(tuned)
tuned = {};
end
if nargin < 4
prior = [];
end
tuned = cellstr(tuned);

pairs = {};
if isstruct(detail)
keys = fieldnames(detail);
for i = 1:numel(keys)
value = detail.(keys{i});
key = normaliseToken(keys{i});
if isempty(regexp(key, '^[a-z][a-z0-9_]*$', 'once'))
continue
end
if isnumeric(value) || islogical(value)
if isempty(value) || ~isfinite(value(1))
continue
end
value = sprintf('%.6g', value(1));
elseif isstring(value)
value = char(value);
end
pairs{end+1} = [key '=' value]; %#ok<AGROW>
end
end

text = normaliseToken(origin);
if isempty(text) && (~isempty(pairs) || ~isempty(tuned))
text = 'unknown';
end
if ~isempty(pairs)
text = [text ' (' strjoin(pairs, ', ') ')'];
end
if ~isempty(tuned)
text = [text ' | tuned=' strjoin(tuned(:)', '+')];
if ~isempty(prior) && isfinite(prior)
text = [text sprintf(' (prior=%.6g)', prior)];
end
end
end

function token = normaliseToken(token)
% Lower case, every run of characters outside [0-9a-z] one underscore, no
% leading or trailing underscore.
token = lower(strtrim(char(token)));
token = regexprep(token, '[^0-9a-z]+', '_');
token = regexprep(token, '^_+|_+$', '');
end
14 changes: 9 additions & 5 deletions src/gather_kcats/getStandardKcat.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
% linked to the "standard" pseudoenzyme, but will use the enzyme that they
% had already been associated with.
%
% Any pre-existing standard kcat assignments (identified by 'standard'
% entries in model.ec.source) are removed when applying this function.
% Any pre-existing standard kcat assignments (the entries of model.ec.source
% whose origin is 'standard' and that have no tuning status) are removed when
% applying this function.
%
% Parameters
% ----------
Expand Down Expand Up @@ -259,7 +260,10 @@
stdMetIdx = find(strcmpi(model.ec.enzymes, 'standard'));

% Remove previous standard kcat assignment
oldStandardEnz = find(strcmp(model.ec.source,'standard'));
% A row with a tuning status was changed on purpose and is kept.
oldSource = parseKcatSource(model.ec.source);
oldStandardEnz = find(strcmp({oldSource.origin}, 'standard') & cellfun(@isempty, {oldSource.tuned}));
oldStandardEnz = oldStandardEnz(:);
if ~isempty(oldStandardEnz)
oldStandardProt = logical(model.ec.rxnEnzMat(oldStandardEnz,stdMetIdx));
% If annotated with real enzyme => set kcat to zero
Expand Down Expand Up @@ -303,7 +307,7 @@
model.ec.kcat(end+1) = standardKcat;
end

model.ec.source(end+1) = {'standard'};
model.ec.source(end+1) = {formatKcatSource('standard')};
model.ec.notes(end+1) = {''};
model.ec.eccodes(end+1) = {''};

Expand All @@ -316,7 +320,7 @@
if fillZeroKcat
zeroKcat = model.ec.kcat == 0 | isnan(model.ec.kcat);
model.ec.kcat(zeroKcat) = standardKcat;
model.ec.source(zeroKcat) = {'standard'};
model.ec.source(zeroKcat) = {formatKcatSource('standard')};
rxnsNoKcat = model.ec.rxns(zeroKcat);
else
rxnsNoKcat = [];
Expand Down
123 changes: 123 additions & 0 deletions src/gather_kcats/parseKcatSource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
function info = parseKcatSource(source)
% parseKcatSource Read the provenance text of a kcat in model.ec.source.
%
% Reads the form written by formatKcatSource,
%
% <origin>[ (<detail>)][ | tuned=<method>[+<method>...] (prior=<value>)]
%
% A text that does not follow the form is read as an origin without detail
% and without tuning status, so a text that GECKO wrote in the form of an
% earlier release stays readable. A detail pair is ignored when its key does
% not start with a letter, a value is a number when it is in plain decimal
% form (for example 0.61, -2, .5 or 1e3) and text otherwise, and the prior
% needs exactly one space before '(prior='.
%
% Parameters
% ----------
% source : char, string or cell
% one provenance text, or a cell array of them, e.g. model.ec.source.
% Missing, NaN and other non-text input gives an empty origin.
%
% Returns
% -------
% info : struct
% scalar struct for a char or string, and a struct array of the size of
% the cell array for a cell array, with the fields
%
% - origin : the origin as written, in the case it was written in.
% - detail : scalar struct of the detail pairs; a value is a double when
% it parses as a number and char otherwise.
% - tuned : 1xN cell of the tuning methods, {} when none.
% - prior : the kcat before the first tuning, NaN when none.
%
% Examples
% --------
% info = parseKcatSource('brenda (wc=0, origin=2) | tuned=cmaes (prior=6.55)');
% info.origin % 'brenda'
% info.detail.wc % 0
% info.tuned % {'cmaes'}
% info.prior % 6.55
%
% See also
% --------
% formatKcatSource, getStandardKcat, resolveTunableContext
if iscell(source)
info = repmat(emptyInfo(), size(source));
for i = 1:numel(source)
info(i) = parseText(source{i});
end
else
info = parseText(source);
end
end

function info = emptyInfo()
info = struct('origin', '', 'detail', struct(), 'tuned', {{}}, 'prior', NaN);
end

function info = parseText(text)
info = emptyInfo();
if isstring(text) && isscalar(text) && ~ismissing(text)
text = char(text);
end
if ~ischar(text) || (~isempty(text) && ~isrow(text))
return
end
text = strtrim(text);

tail = '';
cut = strfind(text, ' | tuned=');
if ~isempty(cut)
tail = text(cut(1)+9:end);
text = strtrim(text(1:cut(1)-1));
end

open = strfind(text, ' (');
if ~isempty(open) && endsWith(text, ')')
info.origin = strtrim(text(1:open(1)-1));
parts = strsplit(text(open(1)+2:end-1), ',');
for i = 1:numel(parts)
eq = find(parts{i} == '=', 1);
if isempty(eq)
continue
end
key = strtrim(parts{i}(1:eq-1));
if isempty(regexp(key, '^[A-Za-z][A-Za-z0-9_]*$', 'once'))
continue
end
info.detail.(key) = parseValue(strtrim(parts{i}(eq+1:end)));
end
else
info.origin = strtrim(text);
end

tail = strtrim(tail);
if ~isempty(tail)
at = strfind(tail, ' (prior=');
if ~isempty(at)
names = tail(1:at(1)-1);
rest = tail(at(1)+8:end);
if endsWith(rest, ')') && ~any(rest(1:end-1) == ')') && ~(~isempty(names) && isspace(names(end)))
tail = names;
prior = parseValue(strtrim(rest(1:end-1)));
if isnumeric(prior)
info.prior = prior;
end
end
end
methods = strtrim(strsplit(tail, '+'));
methods(cellfun(@isempty, methods)) = [];
if ~isempty(methods)
info.tuned = methods;
end
end
end

function value = parseValue(text)
% A number when the text is a plain decimal number, the text otherwise.
if ~isempty(regexp(text, '^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$', 'once'))
value = str2double(text);
else
value = text;
end
end
Loading
Loading