From da574443e5fd186e078d9572f303a031cfb8b8c9 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Sun, 8 Nov 2026 18:35:25 +0100 Subject: [PATCH] ec.source: one format, one parser and a record of tuning parseKcatSource and formatKcatSource read and write [ ()][ | tuned= (prior=)]. The writers build their strings with the formatter and the readers use the parsed origin. setKcatForReactions writes manual. sensitivityTuning and cmaesKcatTuning keep the origin and detail and add the tuning method, with the kcat before the first tuning as prior, and sensitivityTuning no longer writes the old source into ec.notes. A standard row with a tuning status is kept by getStandardKcat and removeStandardKcat. --- src/change_model/applyCustomKcats.m | 4 +- src/change_model/getKcatAcrossIsozymes.m | 2 +- src/change_model/setKcatForReactions.m | 4 +- src/gather_kcats/assignKcatValues.m | 29 +- src/gather_kcats/collectKcats.m | 9 +- src/gather_kcats/formatKcatSource.m | 103 +++++ src/gather_kcats/getStandardKcat.m | 14 +- src/gather_kcats/parseKcatSource.m | 123 ++++++ src/gather_kcats/removeStandardKcat.m | 9 +- src/gather_kcats/reviewKcatAssignment.m | 16 +- src/kcat_tuning/evotune/cmaesKcatTuning.m | 25 +- .../evotune/resolveTunableContext.m | 19 +- src/kcat_tuning/sensitivityTuning.m | 39 +- test/VerificationMatrix.tsv | 9 + test/unit_tests/kcatSourceFormatTests.m | 384 ++++++++++++++++++ 15 files changed, 733 insertions(+), 56 deletions(-) create mode 100644 src/gather_kcats/formatKcatSource.m create mode 100644 src/gather_kcats/parseKcatSource.m create mode 100644 test/unit_tests/kcatSourceFormatTests.m diff --git a/src/change_model/applyCustomKcats.m b/src/change_model/applyCustomKcats.m index 54f9ba67..b849cd69 100644 --- a/src/change_model/applyCustomKcats.m +++ b/src/change_model/applyCustomKcats.m @@ -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) @@ -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 diff --git a/src/change_model/getKcatAcrossIsozymes.m b/src/change_model/getKcatAcrossIsozymes.m index 71dfe485..e2d76435 100644 --- a/src/change_model/getKcatAcrossIsozymes.m +++ b/src/change_model/getKcatAcrossIsozymes.m @@ -51,6 +51,6 @@ newKcat = noKcatVal(newKcat); model.ec.kcat(newKcatIdx) = newKcat; -model.ec.source(newKcatIdx) = {'isozymes'}; +model.ec.source(newKcatIdx) = {formatKcatSource('isozymes')}; end diff --git a/src/change_model/setKcatForReactions.m b/src/change_model/setKcatForReactions.m index a8e8018f..e0fee5c9 100644 --- a/src/change_model/setKcatForReactions.m +++ b/src/change_model/setKcatForReactions.m @@ -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 % -------- @@ -59,5 +59,5 @@ end end ecModel.ec.kcat(rxnsToChange) = kcat; -ecModel.ec.source(rxnsToChange) = {'setKcatForReactions'}; +ecModel.ec.source(rxnsToChange) = {formatKcatSource('manual')}; end diff --git a/src/gather_kcats/assignKcatValues.m b/src/gather_kcats/assignKcatValues.m index 227ca798..cc669b6b 100644 --- a/src/gather_kcats/assignKcatValues.m +++ b/src/gather_kcats/assignKcatValues.m @@ -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 @@ -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 @@ -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 diff --git a/src/gather_kcats/collectKcats.m b/src/gather_kcats/collectKcats.m index 1966d194..ea9976b4 100644 --- a/src/gather_kcats/collectKcats.m +++ b/src/gather_kcats/collectKcats.m @@ -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; @@ -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(:), ... diff --git a/src/gather_kcats/formatKcatSource.m b/src/gather_kcats/formatKcatSource.m new file mode 100644 index 00000000..373dfe0a --- /dev/null +++ b/src/gather_kcats/formatKcatSource.m @@ -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 +% +% [ ()][ | tuned=[+...] (prior=)] +% +% 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 + 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 diff --git a/src/gather_kcats/getStandardKcat.m b/src/gather_kcats/getStandardKcat.m index 962746c9..55a128a5 100644 --- a/src/gather_kcats/getStandardKcat.m +++ b/src/gather_kcats/getStandardKcat.m @@ -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 % ---------- @@ -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 @@ -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) = {''}; @@ -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 = []; diff --git a/src/gather_kcats/parseKcatSource.m b/src/gather_kcats/parseKcatSource.m new file mode 100644 index 00000000..7c8a1a56 --- /dev/null +++ b/src/gather_kcats/parseKcatSource.m @@ -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, +% +% [ ()][ | tuned=[+...] (prior=)] +% +% 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 diff --git a/src/gather_kcats/removeStandardKcat.m b/src/gather_kcats/removeStandardKcat.m index ee222ad8..529b08f4 100644 --- a/src/gather_kcats/removeStandardKcat.m +++ b/src/gather_kcats/removeStandardKcat.m @@ -5,7 +5,9 @@ % they were introduced by getStandardKcat. Also standard kcat values that % were assigned by getStandardKcat if fillZeroKcat was set to true are % removed from model.ec.kcat. Both the model.ec and model.S structures are -% modified. +% modified. A standard kcat value is recognised by the origin 'standard' of +% model.ec.source, compared without regard to letter case; a row with a +% tuning status is kept. % % Parameters % ---------- @@ -48,7 +50,10 @@ % Remove standard kcat values and reapply kcat constraints for those % specific reactions -stdKcatIdx = find(strcmpi(model.ec.source, 'standard')); +% A row with a tuning status was changed on purpose and is kept. +stdSource = parseKcatSource(model.ec.source); +stdKcatIdx = find(strcmpi({stdSource.origin}, 'standard') & cellfun(@isempty, {stdSource.tuned})); +stdKcatIdx = stdKcatIdx(:); if ~isempty(stdKcatIdx) model.ec.source(stdKcatIdx) = {''}; model.ec.kcat(stdKcatIdx) = 0; diff --git a/src/gather_kcats/reviewKcatAssignment.m b/src/gather_kcats/reviewKcatAssignment.m index a5d265af..47360453 100644 --- a/src/gather_kcats/reviewKcatAssignment.m +++ b/src/gather_kcats/reviewKcatAssignment.m @@ -26,8 +26,9 @@ % ---------- % rxnIds, kcats, sources, ecCodes : cell/double % ec.rxns, ec.kcat, ec.source, ec.eccodes (or the equivalent -% subset), all the same length. The source labels 'custom' and -% 'standard' are recognised without regard to letter case. +% subset), all the same length. The origins 'custom' and 'standard' +% of the source (see parseKcatSource) are recognised without regard to +% letter case. % % Name-Value Arguments % -------------------- @@ -109,6 +110,13 @@ lev = leverage(:); end +sourceText = sources(:); +if ~iscell(sourceText) + sourceText = cellstr(sourceText); +end +sourceInfo = parseKcatSource(sourceText); +origins = {sourceInfo.origin}'; + tie = isozymeTieMap(rxnIds,kcats,'sources',sources); reps = unique(tie); memberLists = cell(numel(reps),1); @@ -179,7 +187,7 @@ checks{end+1} = 'magnitude'; %#ok detail{end+1} = sprintf('%g 1/s is outside %g-%g', kcats(i), slow, fast); %#ok end - if strcmpi(sources{i},'custom') && ~isempty(st) && ismember(round(kcats(i),6), st.values) + if strcmpi(origins{i},'custom') && ~isempty(st) && ismember(round(kcats(i),6), st.values) checks{end+1} = 'custom-duplicate'; %#ok detail{end+1} = 'custom value equals a database value for its own EC'; %#ok end @@ -189,7 +197,7 @@ end detail{end+1} = sprintf('%d isozyme copies share this value', numel(idx)); %#ok end - if strcmpi(sources{i},'standard') + if strcmpi(origins{i},'standard') if ~ismember('standard-fallback',include) continue end diff --git a/src/kcat_tuning/evotune/cmaesKcatTuning.m b/src/kcat_tuning/evotune/cmaesKcatTuning.m index ee67e7e0..e22d9b7e 100644 --- a/src/kcat_tuning/evotune/cmaesKcatTuning.m +++ b/src/kcat_tuning/evotune/cmaesKcatTuning.m @@ -46,9 +46,11 @@ % ------- % ecModel : struct % mutated: tunable rows hold the best kcat vector CMA-ES found, with -% kcat constraints already applied. MATLAB structs are copied on -% assignment, so -- unlike geckopy's in-place mutation -- this output -% must be captured by the caller. +% kcat constraints already applied. The source of each changed kcat +% keeps its origin and detail and gains the tuning status 'tuned=cmaes' +% with the kcat before the first tuning as prior (see formatKcatSource). +% MATLAB structs are copied on assignment, so -- unlike geckopy's +% in-place mutation -- this output must be captured by the caller. % result : struct % rxns/oldKcat/newKcat/groups cover the selected tunable set, tied % groups included (their members share one value in newKcat). @@ -187,6 +189,23 @@ meanX,sigma,D,pSigma,pC,Z,ord,w,mu,cs,ds,cc,c1,cmu,muEff,chiN,generation); end +changed = tunableIdx(bestVec ~= kcat0); +for i = changed(:)' + info = parseKcatSource(ecModel.ec.source{i}); + origin = info.origin; + if isempty(origin) + origin = 'unknown'; + end + tuned = info.tuned; + if ~any(strcmp(tuned,'cmaes')) + tuned{end+1} = 'cmaes'; + end + prior = info.prior; + if isnan(prior) + prior = ecModel.ec.kcat(i); + end + ecModel.ec.source{i} = formatKcatSource(origin,info.detail,tuned,prior); +end ecModel.ec.kcat(tunableIdx) = bestVec; ecModel = applyKcatConstraints(ecModel,'updateRxns',ecRxnIdsTunable); diff --git a/src/kcat_tuning/evotune/resolveTunableContext.m b/src/kcat_tuning/evotune/resolveTunableContext.m index abddb708..57781ac8 100644 --- a/src/kcat_tuning/evotune/resolveTunableContext.m +++ b/src/kcat_tuning/evotune/resolveTunableContext.m @@ -5,11 +5,13 @@ % Shared by screenKcatLeverage and cmaesKcatTuning so a screen and the % tuning run it feeds are always looking at exactly the same parameters. % -% A kcat belongs to the group whose params.kcatSources entry equals its -% ec.source, or the part of ec.source before ' (' (case-insensitive), so -% 'brenda (wc=0, origin=1)' is in the group of 'brenda' and -% 'combined (sd=0.10, n=2)' in the group of 'combined', with that group's -% sigma0logSource. +% A kcat belongs to the group whose params.kcatSources entry equals the +% origin of its ec.source or its whole ec.source (case-insensitive, see +% parseKcatSource), so +% 'brenda (wc=0, origin=1)' is in the group of 'brenda', +% 'combined (sd=0.10, n=2)' in the group of 'combined' and +% 'brenda (wc=0, origin=1) | tuned=cmaes (prior=2)' in the group of 'brenda', +% with that group's sigma0logSource. % % Parameters % ---------- @@ -54,14 +56,13 @@ ecRxnIdsTunable = ecModel.ec.rxns(tunableIdx); kcat0 = ecModel.ec.kcat(tunableIdx); sources = ecModel.ec.source(tunableIdx); -% A source with a detail, such as 'combined (sd=0.10, n=2)', is also matched by -% its name before ' ('. -sourceNames = strtrim(regexprep(sources, ' \(.*$', '')); +sourceInfo = parseKcatSource(sources); +sourceNames = {sourceInfo.origin}'; groups = repmat({'unlabelled'}, numel(tunableIdx), 1); sigma0log = params.sigma0logDefault * ones(numel(tunableIdx),1); for i = 1:numel(params.kcatSources) - idx = strcmpi(sources, params.kcatSources{i}) | strcmpi(sourceNames, params.kcatSources{i}); + idx = strcmpi(sourceNames, params.kcatSources{i}) | strcmpi(sources, params.kcatSources{i}); groups(idx) = params.kcatSources(i); sigma0log(idx) = params.sigma0logSource(i); end diff --git a/src/kcat_tuning/sensitivityTuning.m b/src/kcat_tuning/sensitivityTuning.m index 96d1efce..b9b5133e 100644 --- a/src/kcat_tuning/sensitivityTuning.m +++ b/src/kcat_tuning/sensitivityTuning.m @@ -46,9 +46,10 @@ % - source : source label of each kcat, taken from the input model before % tuning. % -% The model.ec.notes field will contain the original kcat value and source, -% unless the kcat has previously been set by sensitivityTuning, in which case -% the notes field remains unchanged. +% model.ec.source of a tuned kcat keeps its origin and detail and gains the +% tuning status, e.g. 'brenda (wc=0, origin=1) | tuned=sensitivity +% (prior=0.05)', where prior is the kcat before the first tuning (see +% formatKcatSource). An origin that is missing is written as 'unknown'. % % Examples % -------- @@ -134,16 +135,8 @@ kcatList = [kcatList, rxnSel]; rxn = m.rxns(rxnSel); targetSubRxn = strcmp(m.ec.rxns, rxn); - if ~strcmp(m.ec.source(targetSubRxn),'sensitivityTuning') - oldNote = m.ec.notes{targetSubRxn}; - newNote = ['preTuneKcat=' num2str(m.ec.kcat(targetSubRxn)) ' | source:' m.ec.source{targetSubRxn}]; - if ~isempty(oldNote) - newNote = [oldNote '; ' newNote]; - end - m.ec.notes{targetSubRxn} = newNote; - end + m = markTuned(m,targetSubRxn); m.ec.kcat(targetSubRxn) = m.ec.kcat(targetSubRxn) .* foldChange; - m.ec.source(targetSubRxn) = {'sensitivityTuning'}; m = applyKcatConstraints(m,targetSubRxn); end @@ -186,6 +179,7 @@ kcatList = [kcatList, sel]; rxn = m.rxns(sel.'); targetSubRxns = strcmp(origRxns, rxn); + m = markTuned(m,targetSubRxns); m.ec.kcat(targetSubRxns) = m.ec.kcat(targetSubRxns) .* foldChange; m = applyKcatConstraints(m,rxn); end @@ -210,3 +204,24 @@ model = m; end + +function m = markTuned(m,rows) +% Add the tuning method sensitivity to the source of the ec rows, with the +% kcat before the first tuning as prior. Called before the kcat changes. +for i = find(rows(:))' + info = parseKcatSource(m.ec.source{i}); + origin = info.origin; + if isempty(origin) + origin = 'unknown'; + end + tuned = info.tuned; + if ~any(strcmp(tuned,'sensitivity')) + tuned{end+1} = 'sensitivity'; + end + prior = info.prior; + if isnan(prior) + prior = m.ec.kcat(i); + end + m.ec.source{i} = formatKcatSource(origin,info.detail,tuned,prior); +end +end diff --git a/test/VerificationMatrix.tsv b/test/VerificationMatrix.tsv index 64affd99..be2fab53 100644 --- a/test/VerificationMatrix.tsv +++ b/test/VerificationMatrix.tsv @@ -115,3 +115,12 @@ tutorials/tutorial_yeast-GEM/code/removeIncorrectPathways.m Not explicitly teste tutorials/tutorial_yeast-GEM/code/scaleBioMass.m Not explicitly tested - The code has existed for a long time tutorials/tutorial_yeast-GEM/code/sumBioMass.m Not explicitly tested - The code has existed for a long time tutorials/tutorial_yeast-GEM/code/sumProtein.m Not explicitly tested - The code has existed for a long time +gather_kcats/formatKcatSource.m tc0701, tc0702, tc0703, tc0706, tc0707, tc0713, tc0714, tc0718 +gather_kcats/parseKcatSource.m tc0704, tc0705, tc0706, tc0709, tc0710, tc0711, tc0712, tc0715, tc0716, tc0717, tc0719, tc0720, tc0721, tc0722 +gather_kcats/assignKcatValues.m tc0707 - the source text of a match +gather_kcats/getStandardKcat.m tc0709 - the tuned standard row is kept +gather_kcats/removeStandardKcat.m tc0710 +gather_kcats/reviewKcatAssignment.m tc0711 - the labels custom and standard are recognised by their origin +change_model/setKcatForReactions.m tc0708 +kcat_tuning/sensitivityTuning.m tc0713, tc0714 - the tuning status of the source +kcat_tuning/evotune/* resolveTunableContext by tc0712; the tuning status that cmaesKcatTuning writes into the source has not been tested by a test case diff --git a/test/unit_tests/kcatSourceFormatTests.m b/test/unit_tests/kcatSourceFormatTests.m new file mode 100644 index 00000000..68894ed5 --- /dev/null +++ b/test/unit_tests/kcatSourceFormatTests.m @@ -0,0 +1,384 @@ +%run this test case with the command +%results = runtests('kcatSourceFormatTests.m') + +function tests = kcatSourceFormatTests + tests = functiontests(localfunctions); +end + +function testFormatKcatSourceExamples_tc0701(testCase) + verifyEqual(testCase, formatKcatSource('custom'), 'custom') + verifyEqual(testCase, formatKcatSource('standard'), 'standard') + verifyEqual(testCase, formatKcatSource('brenda', struct('wc', 0, 'origin', 1)), 'brenda (wc=0, origin=1)') + verifyEqual(testCase, formatKcatSource('okp_catapro', struct('similarity', 42)), 'okp_catapro (similarity=42)') + verifyEqual(testCase, formatKcatSource('combined', struct('sd', sprintf('%.2f', 0.61), 'n', 6)), 'combined (sd=0.61, n=6)') + verifyEqual(testCase, formatKcatSource('homologs', struct('identity', 0.953)), 'homologs (identity=0.953)') + verifyEqual(testCase, formatKcatSource('brenda', struct('wc', 0, 'origin', 2), {'cmaes'}, 6.55), ... + 'brenda (wc=0, origin=2) | tuned=cmaes (prior=6.55)') + verifyEqual(testCase, formatKcatSource('okp_catapro', struct('similarity', 42), {'sensitivity', 'cmaes'}, 5.48), ... + 'okp_catapro (similarity=42) | tuned=sensitivity+cmaes (prior=5.48)') +end + +function testFormatKcatSourceNormalisesOriginAndKeys_tc0702(testCase) + verifyEqual(testCase, formatKcatSource('OKP-CataPro'), 'okp_catapro') + verifyEqual(testCase, formatKcatSource(' DLKcat '), 'dlkcat') + verifyEqual(testCase, formatKcatSource('a - b'), 'a_b') + verifyEqual(testCase, formatKcatSource('_x_'), 'x') + verifyEqual(testCase, formatKcatSource('Brenda', struct('WC', 0, 'Origin', 1)), 'brenda (wc=0, origin=1)') + % an empty origin is the empty text, unless there is more to write + verifyEqual(testCase, formatKcatSource(''), '') + verifyEqual(testCase, formatKcatSource('', struct('wc', 0)), 'unknown (wc=0)') + verifyEqual(testCase, formatKcatSource('', [], {'cmaes'}, 3), 'unknown | tuned=cmaes (prior=3)') +end + +function testFormatKcatSourceDetailValuesAndStatus_tc0703(testCase) + % numbers use %.6g, text is verbatim, NaN and [] leave the pair out and + % the brackets with it + verifyEqual(testCase, formatKcatSource('x', struct('v', 1234567)), 'x (v=1.23457e+06)') + verifyEqual(testCase, formatKcatSource('x', struct('v', 0.1 + 0.2)), 'x (v=0.3)') + verifyEqual(testCase, formatKcatSource('x', struct('v', '0.60')), 'x (v=0.60)') + verifyEqual(testCase, formatKcatSource('x', struct('a', NaN)), 'x') + verifyEqual(testCase, formatKcatSource('x', struct('a', [])), 'x') + verifyEqual(testCase, formatKcatSource('x', struct('a', NaN, 'b', 2)), 'x (b=2)') + % the prior is written when there is a tuning status + verifyEqual(testCase, formatKcatSource('x', [], {}, 5), 'x') + verifyEqual(testCase, formatKcatSource('x', [], {'cmaes'}, NaN), 'x | tuned=cmaes') + verifyEqual(testCase, formatKcatSource('x', [], {'cmaes'}, []), 'x | tuned=cmaes') + verifyEqual(testCase, formatKcatSource('x', [], 'cmaes', 2), 'x | tuned=cmaes (prior=2)') +end + +function testParseKcatSourceExamples_tc0704(testCase) + info = parseKcatSource('brenda (wc=0, origin=1)'); + verifyEqual(testCase, info.origin, 'brenda') + verifyEqual(testCase, info.detail, struct('wc', 0, 'origin', 1)) + verifyEqual(testCase, info.tuned, {}) + verifyTrue(testCase, isnan(info.prior)) + + info = parseKcatSource('combined (sd=0.61, n=6)'); + verifyEqual(testCase, info.origin, 'combined') + verifyEqual(testCase, info.detail, struct('sd', 0.61, 'n', 6)) + + info = parseKcatSource('okp_catapro (similarity=42) | tuned=sensitivity+cmaes (prior=5.48)'); + verifyEqual(testCase, info.origin, 'okp_catapro') + verifyEqual(testCase, info.detail, struct('similarity', 42)) + verifyEqual(testCase, info.tuned, {'sensitivity', 'cmaes'}) + verifyEqual(testCase, info.prior, 5.48) + + info = parseKcatSource('standard | tuned=cmaes'); + verifyEqual(testCase, info.origin, 'standard') + verifyEqual(testCase, info.tuned, {'cmaes'}) + verifyTrue(testCase, isnan(info.prior)) + + % a value that is not a number stays text; whitespace around is stripped + info = parseKcatSource(' x ( a = abc ,b=2,junk, c=NaN ) '); + verifyEqual(testCase, info.origin, 'x') + verifyEqual(testCase, info.detail, struct('a', 'abc', 'b', 2, 'c', 'NaN')) +end + +function testParseKcatSourceLegacyAndNonText_tc0705(testCase) + verifyEqual(testCase, parseKcatSource('brenda').origin, 'brenda') + % an unclosed bracket is part of the origin, with no detail + info = parseKcatSource('brenda (wc=1'); + verifyEqual(testCase, info.origin, 'brenda (wc=1') + verifyEqual(testCase, info.detail, struct()) + % the origin keeps the case it was written in + info = parseKcatSource('OKP-CataPro'); + verifyEqual(testCase, info.origin, 'OKP-CataPro') + verifyEqual(testCase, info.detail, struct()) + verifyEqual(testCase, info.tuned, {}) + verifyEqual(testCase, parseKcatSource('sensitivityTuning').origin, 'sensitivityTuning') + verifyEqual(testCase, parseKcatSource('other (x)').origin, 'other') + verifyEqual(testCase, parseKcatSource('other (x)').detail, struct()) + verifyEqual(testCase, parseKcatSource('no closing (a=1').origin, 'no closing (a=1') + for bad = {'', NaN, [], 3} + info = parseKcatSource(bad{1}); + verifyEqual(testCase, info.origin, '') + verifyEqual(testCase, info.detail, struct()) + verifyEqual(testCase, info.tuned, {}) + verifyTrue(testCase, isnan(info.prior)) + end + verifyEqual(testCase, parseKcatSource("custom").origin, 'custom') + verifyEqual(testCase, parseKcatSource(string(missing)).origin, '') + % a cell array gives a struct array of its shape + info = parseKcatSource({'brenda (wc=0)', ''; 'custom', NaN}); + verifyEqual(testCase, size(info), [2 2]) + verifyEqual(testCase, {info.origin}, {'brenda', 'custom', '', ''}) + verifyEqual(testCase, info(1).detail, struct('wc', 0)) + verifyEqual(testCase, size(parseKcatSource({})), [0 0]) +end + +function testFormatOfParseGivesTheText_tc0706(testCase) + texts = {'brenda (wc=0, origin=1)', 'custom', 'okp_catapro (similarity=42)', 'combined (sd=0.61, n=6)', ... + 'brenda (wc=0, origin=2) | tuned=cmaes (prior=6.55)', ... + 'okp_catapro (similarity=42) | tuned=sensitivity+cmaes (prior=5.48)', ... + 'standard', 'manual', 'isozymes', 'homologs (identity=0.953)', 'standard | tuned=cmaes'}; + for i = 1:numel(texts) + info = parseKcatSource(texts{i}); + verifyEqual(testCase, formatKcatSource(info.origin, info.detail, info.tuned, info.prior), texts{i}) + end + % numbers are written again with %.6g + info = parseKcatSource('combined (sd=0.60, n=6)'); + verifyEqual(testCase, formatKcatSource(info.origin, info.detail, info.tuned, info.prior), 'combined (sd=0.6, n=6)') +end + +function testAssignKcatValuesWritesTheSourceText_tc0707(testCase) + % The source text of a match: the label in lower case snake_case, the + % wildcard level with the origin when both are known, the level alone + % when the origin is not, and no detail when the level is not known. + model = ecFixture(); + rxns = model.ec.rxns(1:5); + kcatList = struct('source', 'x', 'rxns', {rxns}, 'kcats', [5; 6; 7; 8; 9], ... + 'kcatSource', {{'BRENDA'; 'DLKcat'; 'OKP-CataPro'; 'brenda'; 'Brenda'}}, ... + 'wildcardLvl', [0; NaN; NaN; 2; NaN], 'origin', [1; NaN; 3; NaN; 4]); + model = assignKcatValues(model, kcatList); + verifyEqual(testCase, model.ec.source(1:5), {'brenda (wc=0, origin=1)'; 'dlkcat'; 'okp_catapro'; 'brenda (wc=2)'; 'brenda'}) +end + +function testSetKcatForReactionsWritesManualWithoutStatus_tc0708(testCase) + model = ecFixture(); + model.ec.source(:) = {'brenda (wc=0, origin=1) | tuned=cmaes (prior=2)'}; + j = strcmp(model.ec.rxns, 'R3'); + out = setKcatForReactions(model, 'R3', 7); + verifyEqual(testCase, out.ec.source{j}, 'manual') + verifyEqual(testCase, out.ec.kcat(j), 7) + verifyEqual(testCase, out.ec.source(~j), model.ec.source(~j)) +end + +function testGetStandardKcatKeepsATunedStandardRow_tc0709(testCase) + % R3 holds a standard kcat on a real enzyme: it is set to zero and its + % source cleared. R5 holds a tuned standard kcat: it is kept as it is. + % The row added for the reaction without GPR (R1) reads 'standard'. + [model, adapter] = ecFixture(); + model.ec.kcat(:) = 0; + model.ec.kcat(strcmp(model.ec.rxns, 'R3')) = 50; + model.ec.kcat(strcmp(model.ec.rxns, 'R5')) = 999; + model.ec.source(:) = {'manual'}; + model.ec.source{strcmp(model.ec.rxns, 'R3')} = 'standard'; + model.ec.source{strcmp(model.ec.rxns, 'R5')} = 'standard | tuned=cmaes (prior=3)'; + evalc('out = getStandardKcat(model, ''modelAdapter'', adapter, ''threshold'', 1, ''fillZeroKcat'', false);'); + verifyEqual(testCase, out.ec.source{strcmp(out.ec.rxns, 'R3')}, '') + verifyEqual(testCase, out.ec.kcat(strcmp(out.ec.rxns, 'R3')), 0) + verifyEqual(testCase, out.ec.source{strcmp(out.ec.rxns, 'R5')}, 'standard | tuned=cmaes (prior=3)') + verifyEqual(testCase, out.ec.kcat(strcmp(out.ec.rxns, 'R5')), 999) + verifyEqual(testCase, out.ec.source{strcmp(out.ec.rxns, 'R1')}, 'standard') +end + +function testRemoveStandardKcatKeepsATunedStandardRow_tc0710(testCase) + % The standard rows are cleared whatever their letter case; the one with + % a tuning status and the others are kept. + model = ecFixture(); + model.ec.kcat(:) = 10; + model.ec.source(:) = {'brenda'}; + model.ec.source{1} = 'standard'; + model.ec.source{2} = 'Standard'; + model.ec.source{3} = 'standard | tuned=sensitivity (prior=5)'; + out = removeStandardKcat(model); + verifyEqual(testCase, out.ec.source(1:2), {''; ''}) + verifyEqual(testCase, out.ec.kcat(1:2), [0; 0]) + verifyEqual(testCase, out.ec.source(3:end), model.ec.source(3:end)) + verifyEqual(testCase, out.ec.kcat(3:end), model.ec.kcat(3:end)) +end + +function testReviewKcatAssignmentRecognisesOriginsOfLabels_tc0711(testCase) + % Every kcat is outside the magnitude bounds. The standard rows, in any + % letter case and with a tuning status, are left out; the custom row is + % reported. + findings = reviewKcatAssignment({'A'; 'B'; 'C'}, [1e-5; 1e-5; 1e-5], ... + {'Custom (x=1)'; 'Standard'; 'standard | tuned=cmaes (prior=1)'}, {'1.1.1.1'; '1.1.1.2'; '1.1.1.3'}); + verifyEqual(testCase, findings.rxnId, {'A'}) + verifyEqual(testCase, findings.checks, {'magnitude'}) + findings = reviewKcatAssignment({'A'; 'B'}, [1e-5; 1e-5], {'standard (x=1)'; 'brenda'}, {'1.1.1.1'; '1.1.1.2'}, ... + 'include', {'standard-fallback'}); + verifyEqual(testCase, findings.rxnId, {'A'; 'B'}) + verifyEqual(testCase, findings.checks, {'magnitude,standard-fallback'; 'magnitude'}) +end + +function testResolveTunableContextGroupsATunedSourceByItsOrigin_tc0712(testCase) + model = ecFixture(); + model.ec.kcat(1:4) = 10; + model.ec.source(1:4) = {'brenda | tuned=cmaes (prior=2)'; 'Combined (sd=0.10, n=2) | tuned=sensitivity (prior=1)'; ... + 'DLKcat'; 'other | tuned=cmaes (prior=2)'}; + params = struct('kcatSources', {{'brenda', 'combined', 'dlkcat'}}, 'sigma0logSource', [0.2; 0.3; 0.4], ... + 'sigma0logDefault', 0.5, 'tieIsozymes', false); + mask = false(numel(model.ec.rxns), 1); + mask(1:4) = true; + [~, ~, ~, groups, sigma0log] = resolveTunableContext(model, params, mask); + verifyEqual(testCase, groups, {'brenda'; 'combined'; 'dlkcat'; 'unlabelled'}) + verifyEqual(testCase, sigma0log, [0.2; 0.3; 0.4; 0.5]) +end + +function testSensitivityTuningExtendsTheStatusOnce_tc0713(testCase) + % Every kcat the tuning changes gets the origin it had, the method + % sensitivity and the kcat before the first tuning as prior. A second + % tuning adds no second method and keeps the prior. The notes are left + % as they were. + [ecModel, tuneAdapter] = tuningModel(); + ecModel.ec.source(:) = {'brenda (wc=0, origin=1)'}; + cleanup = shadowSolveLP([1 1 50]); %#ok + first = sensitivityTuning(ecModel, 'desiredGrowthRate', 50, ... + 'modelAdapter', tuneAdapter, 'protToIgnore', {'P4'}, 'verbose', false); + changed = find(first.ec.kcat ~= ecModel.ec.kcat); + verifyTrue(testCase, any(strcmp(first.ec.rxns(changed), 'R5'))) + iR5 = strcmp(ecModel.ec.rxns, 'R5'); + verifyEqual(testCase, ecModel.ec.kcat(iR5), 200) + verifyEqual(testCase, first.ec.source{iR5}, 'brenda (wc=0, origin=1) | tuned=sensitivity (prior=200)') + for i = changed' + verifyEqual(testCase, first.ec.source{i}, ... + sprintf('brenda (wc=0, origin=1) | tuned=sensitivity (prior=%.6g)', ecModel.ec.kcat(i))) + end + verifyEqual(testCase, first.ec.source(first.ec.kcat == ecModel.ec.kcat), ecModel.ec.source(first.ec.kcat == ecModel.ec.kcat)) + verifyEqual(testCase, first.ec.notes, ecModel.ec.notes) + + clear cleanup + cleanup2 = shadowSolveLP([1 1 50]); %#ok + second = sensitivityTuning(first, 'desiredGrowthRate', 50, ... + 'modelAdapter', tuneAdapter, 'protToIgnore', {'P4'}, 'verbose', false); + changedFirst = find(first.ec.kcat ~= ecModel.ec.kcat); + changedSecond = find(second.ec.kcat ~= first.ec.kcat); + both = intersect(changedFirst(:), changedSecond(:)); + verifyNotEmpty(testCase, both) + for k = 1:numel(both) + i = both(k); + verifyEqual(testCase, second.ec.source{i}, ... + sprintf('brenda (wc=0, origin=1) | tuned=sensitivity (prior=%.6g)', ecModel.ec.kcat(i))) + end + onlySecond = setdiff(changedSecond(:), changedFirst(:)); + for k = 1:numel(onlySecond) + i = onlySecond(k); + verifyEqual(testCase, second.ec.source{i}, ... + sprintf('brenda (wc=0, origin=1) | tuned=sensitivity (prior=%.6g)', first.ec.kcat(i))) + end +end + +function testSensitivityTuningWritesUnknownForAnEmptyOrigin_tc0714(testCase) + [ecModel, tuneAdapter] = tuningModel(); + ecModel.ec.source(:) = {''}; + cleanup = shadowSolveLP([1 1 50]); %#ok + out = sensitivityTuning(ecModel, 'desiredGrowthRate', 50, ... + 'modelAdapter', tuneAdapter, 'protToIgnore', {'P4'}, 'verbose', false); + changed = find(out.ec.kcat ~= ecModel.ec.kcat); + verifyNotEmpty(testCase, changed) + for i = changed' + verifyEqual(testCase, out.ec.source{i}, sprintf('unknown | tuned=sensitivity (prior=%.6g)', ecModel.ec.kcat(i))) + end +end + +function [ecModel, adapter] = ecFixture() +% The ecTestGEM ecModel with the enzyme data from the model. +adapter = getAdapter(); +ecModel = makeEcModel(getGeckoTestModel(), false, adapter); +ecModel = getECfromGEM(ecModel); +end + +function [ecModel, tuneAdapter] = tuningModel() +% R3 limits growth, its enzyme P4 is ignored by the tests, so tuning raises R5. +adapter = getAdapter(); +model = loadConventionalGEM('modelAdapter', adapter); +for id = {'R2', 'R4'} + idx = strcmp(model.rxns, id{1}); + model.lb(idx) = 0; model.ub(idx) = 0; +end +ecModel = makeEcModel(model, false, adapter); +ecModel = getECfromGEM(ecModel); +kcats = struct('R3', 0.05, 'R5', 200); +for id = fieldnames(kcats)' + j = strcmp(ecModel.ec.rxns, id{1}); + ecModel.ec.kcat(j) = kcats.(id{1}); + ecModel.ec.source{j} = 'manual'; +end +ecModel = applyKcatConstraints(ecModel); +ecModel = setProtPoolSize(ecModel, [], [], [], adapter); +tuneAdapter = adapter; +tuneAdapter.params.bioRxn = 'R5'; +end + +function cleanup = shadowSolveLP(values) +% Puts a solveLP on the path that calls RAVEN's and then reports the growth +% of solve number n as values(min(n, end)). The path is restored when the +% returned object is cleared. +setappdata(0, 'gecko_test_solvelp_real', @solveLP); +setappdata(0, 'gecko_test_solvelp_count', 0); +setappdata(0, 'gecko_test_solvelp_values', values); +stubDir = tempname; +mkdir(stubDir); +fid = fopen(fullfile(stubDir, 'solveLP.m'), 'w'); +fprintf(fid, '%s\n', ... + 'function [res,hs] = solveLP(varargin)', ... + 'n = getappdata(0,''gecko_test_solvelp_count'') + 1;', ... + 'setappdata(0,''gecko_test_solvelp_count'',n);', ... + 'real = getappdata(0,''gecko_test_solvelp_real'');', ... + 'values = getappdata(0,''gecko_test_solvelp_values'');', ... + '[res,hs] = real(varargin{:});', ... + 'res.f = values(min(n,numel(values)));', ... + 'end'); +fclose(fid); +addpath(stubDir, '-begin'); +cleanup = onCleanup(@() removeShadow(stubDir)); +end + +function removeShadow(stubDir) +rmpath(stubDir); +rmdir(stubDir, 's'); +for name = {'real', 'values'} + if isappdata(0, ['gecko_test_solvelp_' name{1}]) + rmappdata(0, ['gecko_test_solvelp_' name{1}]); + end +end +if isappdata(0, 'gecko_test_solvelp_count') + rmappdata(0, 'gecko_test_solvelp_count'); +end +end + +function adapter = getAdapter() +geckoPath = findGECKOroot; +adapter = ModelAdapterManager.getAdapter(fullfile(geckoPath,'test','unit_tests','ecTestGEM','TestGEMAdapter.m')); +end + +function testParseKcatSourceReadsANumberOnlyInPlainDecimalForm_tc0715(testCase) + info = parseKcatSource('x (a=1_0, b=inf, c=1e3, d=.5, e=-2., f=0x10)'); + verifyEqual(testCase, info.detail, struct('a', '1_0', 'b', 'inf', 'c', 1000, 'd', 0.5, 'e', -2, 'f', '0x10')) +end + +function testParseKcatSourceIgnoresAPairWithoutALetterKey_tc0716(testCase) + info = parseKcatSource('x (1a=1, a b=2, ok=3, _c=4)'); + verifyEqual(testCase, info.detail, struct('ok', 3)) +end + +function testParseKcatSourceNeedsOneSpaceBeforeThePrior_tc0717(testCase) + info = parseKcatSource('x | tuned=cmaes(prior=3)'); + verifyEqual(testCase, info.tuned, {'cmaes(prior=3)'}) + verifyTrue(testCase, isnan(info.prior)) +end + +function testFormatKcatSourceLeavesOutUnusablePairsAndPriors_tc0718(testCase) + verifyEqual(testCase, formatKcatSource('x', struct('ok', 3, 'a1', 2)), 'x (ok=3, a1=2)') + verifyEqual(testCase, formatKcatSource('x', struct('a', Inf, 'b', 1)), 'x (b=1)') + verifyEqual(testCase, formatKcatSource('x', struct('a', -Inf)), 'x') + verifyEqual(testCase, formatKcatSource('x', [], {'cmaes'}, Inf), 'x | tuned=cmaes') + verifyEqual(testCase, formatKcatSource('x', [], 'cmaes', 2), 'x | tuned=cmaes (prior=2)') +end + +function testParseKcatSourceNeedsExactlyOneSpaceBeforeThePrior_tc0719(testCase) + info = parseKcatSource('x | tuned=cmaes (prior=3)'); + verifyEqual(testCase, info.tuned, {'cmaes (prior=3)'}) + verifyTrue(testCase, isnan(info.prior)) +end + +function testParseKcatSourceReadsAClosingBracketInThePriorAsAMethod_tc0720(testCase) + info = parseKcatSource('x | tuned=cmaes (prior=1))'); + verifyEqual(testCase, info.tuned, {'cmaes (prior=1))'}) + verifyTrue(testCase, isnan(info.prior)) +end + +function testParseKcatSourceTrimsTheHeadBeforeTheTuningStatus_tc0721(testCase) + info = parseKcatSource('x (a=1) | tuned=cmaes'); + verifyEqual(testCase, info.origin, 'x') + verifyEqual(testCase, info.detail, struct('a', 1)) + verifyEqual(testCase, info.tuned, {'cmaes'}) +end + +function testParseKcatSourceTakesTheFirstPriorMarker_tc0722(testCase) + info = parseKcatSource('x | tuned=cmaes (prior=3) (prior=4)'); + verifyEqual(testCase, info.tuned, {'cmaes (prior=3) (prior=4)'}) + verifyTrue(testCase, isnan(info.prior)) +end +