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
52 changes: 45 additions & 7 deletions src/did/+did2/+schema/termIndex.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@
% main): nothing can be checked against it
% count the number of registered terms
% byNode curieKey(node) -> the registered term {node, name, status,
% number_unit, equivalent} (number_unit: [] or the {node, name}
% number_unit, equivalent, synonyms} (number_unit: [] or the {node, name}
% of the unit the term's number takes; equivalent: a cellstr of
% the ids in other vocabularies that mean the same thing, {} when
% the row states none or the registry predates the column --
% [term equivalents recorded], Jess Haley, 2026-10-27)
% byName did2.schema.termNameKey(name) -> the registered term, for every
% term (case, spaces, underscores and hyphens ignored: [term
% names match ignoring spacing], Jess Haley, 2026-10-29)
% byEquivalent curieKey(id) -> the registered term, for each id in a
% row's `equivalent` list (a node a document written before the
% row's node was chosen may still hold, e.g. NCIm:CL1445492 for
% CHEBI:757169 carbogen)
% bySynonym termNameKey(synonym) -> the registered term, for each
% entry in a row's `synonyms` list (spacing ignored, as byName)
% Each registered term also carries `synonyms` (a cellstr, {} when none).
% byEquivalent and bySynonym are for a READER resolving what a user typed
% ([entity search resolves a term through the registry], Jess Haley,
% 2026-11-30; VH-Lab/NDI-matlab#1332). termRegistered and registeredTerm
% do not read them: an equivalent node or a synonym is not the registered
% term, so a writer still refuses or rewrites it as before. A key already
% taken in byNode or byName is not added to byEquivalent or bySynonym.
%
% REGISTRY ([] = the shared schema cache's) is the struct jsondecode makes of
% the file. With REGISTRY empty the index is built once per shared cache.
Expand Down Expand Up @@ -56,7 +69,9 @@

ix = struct('present', false, 'count', 0, ...
'byNode', containers.Map('KeyType', 'char', 'ValueType', 'any'), ...
'byName', containers.Map('KeyType', 'char', 'ValueType', 'any'));
'byName', containers.Map('KeyType', 'char', 'ValueType', 'any'), ...
'byEquivalent', containers.Map('KeyType', 'char', 'ValueType', 'any'), ...
'bySynonym', containers.Map('KeyType', 'char', 'ValueType', 'any'));
if ~isstruct(registry) || ~isscalar(registry) || ~isfield(registry, 'terms')
return;
end
Expand All @@ -65,31 +80,54 @@
terms = num2cell(terms); % one row per cell: rows may differ in keys
end
ix.present = true;
rows = {};
for k = 1:numel(terms)
t = terms{k};
if ~isstruct(t) || ~isscalar(t)
continue;
end
e = struct('node', textOf(t, 'node'), 'name', textOf(t, 'name'), ...
'status', textOf(t, 'status'), 'number_unit', unitOf(t), ...
'equivalent', {equivalentOf(t)});
'equivalent', {listOf(t, 'equivalent')}, 'synonyms', {listOf(t, 'synonyms')});
if ~isempty(e.node)
ix.byNode(did2.schema.curieKey(e.node)) = e;
end
if ~isempty(e.name)
ix.byName(did2.schema.termNameKey(e.name)) = e;
end
rows{end+1} = e; %#ok<AGROW>
end
% the equivalents and synonyms after every row's own node and name, so a row's
% own node or name always wins over another row's equivalent or synonym
for k = 1:numel(rows)
e = rows{k};
for j = 1:numel(e.equivalent)
key = did2.schema.curieKey(e.equivalent{j});
if ~isempty(key) && ~isKey(ix.byNode, key) && ~isKey(ix.byEquivalent, key)
ix.byEquivalent(key) = e;
end
end
for j = 1:numel(e.synonyms)
key = did2.schema.termNameKey(e.synonyms{j});
if ~isempty(key) && ~isKey(ix.byName, key) && ~isKey(ix.bySynonym, key)
ix.bySynonym(key) = e;
end
end
end
ix.count = numel(terms);
end

function c = equivalentOf(s)
% The row's `equivalent` list as a cellstr ({} when none).
function c = listOf(s, f)
% The row's list F (`equivalent`, `synonyms`) as a cellstr ({} when none).
arguments
s struct
f {mustBeText}
end
c = {};
if ~isfield(s, 'equivalent') || isempty(s.equivalent)
if ~isfield(s, f) || isempty(s.(f))
return;
end
v = s.equivalent;
v = s.(f);
if ischar(v) || isstring(v)
c = cellstr(v);
elseif iscell(v)
Expand Down
43 changes: 43 additions & 0 deletions tests/+did2/+unittest/testBuildTermRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ function testANameIsFoundIgnoringCaseSpacesUnderscoresAndHyphens(testCase)
verifyFalse(testCase, did2.schema.termRegistered(term('', 'mid point'), ix));
end

function testEquivalentsAndSynonymsAreIndexedForAReader(testCase)
% [entity search resolves a term through the registry] (Jess Haley,
% 2026-11-30; VH-Lab/NDI-matlab#1332): an `equivalent` node and a `synonyms`
% entry find their registered row, for a reader. A writer's rule is unchanged:
% an equivalent node or a synonym is not the registered term.
ix = did2.schema.termIndex(resolveRegistry());
verifyEqual(testCase, ix.count, 3);
c = ix.byNode(did2.schema.curieKey('CHEBI:757169'));
verifyEqual(testCase, c.equivalent, {'NCIm:CL1445492'});
verifyEqual(testCase, c.synonyms, {'95% O2 / 5% CO2'});
% by its equivalent, prefix by key and case as for a node
verifyTrue(testCase, isKey(ix.byEquivalent, did2.schema.curieKey('ncim:CL1445492')));
verifyEqual(testCase, ix.byEquivalent(did2.schema.curieKey('NCIm:CL1445492')).node, 'CHEBI:757169');
% by its synonym, spacing ignored as for a name
verifyEqual(testCase, ix.bySynonym(did2.schema.termNameKey('95% O2 / 5% co2')).name, 'carbogen');
% control: the equivalent and the synonym are not in byNode or byName
verifyFalse(testCase, isKey(ix.byNode, did2.schema.curieKey('NCIm:CL1445492')));
verifyFalse(testCase, isKey(ix.byName, did2.schema.termNameKey('95% O2 / 5% CO2')));
% control: a row's own node or name wins over another row's equivalent or synonym
verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).name, 'dioxygen');
verifyFalse(testCase, isKey(ix.byEquivalent, did2.schema.curieKey('CHEBI:15379')));
verifyFalse(testCase, isKey(ix.bySynonym, did2.schema.termNameKey('dioxygen')));
% a row whose lists are empty has empty ones (dioxygen; body midpoint's are not)
verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).equivalent, {});
verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).synonyms, {});
verifyEqual(testCase, ix.byName(did2.schema.termNameKey('body midpoint')).equivalent, {'CHEBI:15379'});
% the writer's rule: an equivalent node is not the registered term
verifyFalse(testCase, did2.schema.termRegistered(term('NCIm:CL1445492', 'carbogen'), ix));
verifyTrue(testCase, did2.schema.termRegistered(term('CHEBI:757169', 'carbogen'), ix));
end

function testNoRegistryRegistersNothing(testCase)
ix = did2.schema.termIndex(struct('title', 'not a term registry'));
verifyFalse(testCase, ix.present);
Expand Down Expand Up @@ -325,6 +356,18 @@ function testTheCensusDoesNotListAFitsVariableSymbols(testCase)
'{"node": "", "name": "body midpoint", "status": "staged"}]}']);
end

function r = resolveRegistry()
% carbogen with an equivalent and a synonym; dioxygen, whose node another row
% lists as an equivalent and whose name another row lists as a synonym
r = jsondecode(['{"terms": [' ...
'{"node": "CHEBI:757169", "name": "carbogen", "status": "decided", ' ...
'"equivalent": ["NCIm:CL1445492"], "synonyms": ["95% O2 / 5% CO2"]},' ...
'{"node": "CHEBI:15379", "name": "dioxygen", "status": "decided", ' ...
'"equivalent": [], "synonyms": []},' ...
'{"node": "", "name": "body midpoint", "status": "staged", ' ...
'"equivalent": ["CHEBI:15379"], "synonyms": ["dioxygen"]}]}']);
end

function t = symbol(name)
% a coefficient's symbol as a label ({node '', name}), as the migrators write it
arguments
Expand Down
Loading