Skip to content

Commit

Permalink
fix: preserve qualifier biolink prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Aug 9, 2024
1 parent 9fad32d commit 6a8d1e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
35 changes: 21 additions & 14 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,32 @@ export default class InferredQueryHandler {
// Direct edge answers stand on their own, not as an inferred edge.
const boundEdgeID = Object.values(result.analyses[0].edge_bindings)[0][0].id;
const boundEdge = combinedResponse.message.knowledge_graph.edges[boundEdgeID];
const specialHandling = [
Object.keys(result.node_bindings).length === 2, // Direct edge
// Predicate matches or is descendant
const oneHop = Object.keys(result.node_bindings).length === 2; // Direct edge
// Predicate matches or is descendant
const predicateMatch =
qEdge.predicates?.some(
(predicate) =>
predicate === boundEdge.predicate ||
biolink.getDescendantPredicates(predicate).includes(boundEdge.predicate),
) ?? false,
// All query qualifiers (if any) are accounted for (more is fine)
) ?? false;
// All query qualifiers (if any) are accounted for (more is fine)
const qualifierMatch =
qEdge.qualifier_constraints?.some(({ qualifier_set }) => {
return qualifier_set.every((queryQualifier) =>
boundEdge.qualifiers?.some(
(qualifier) =>
queryQualifier.qualifier_type_id === qualifier.qualifier_type_id &&
queryQualifier.qualifier_value === qualifier.qualifier_value,
) ?? false,
);
}) ?? false,
].every((test) => test);
return qualifier_set.every((queryQualifier) => {
return (
boundEdge.qualifiers?.some((qualifier) => {
const typeMatch = queryQualifier.qualifier_type_id === qualifier.qualifier_type_id;
const valueMatch =
queryQualifier.qualifier_value === qualifier.qualifier_value ||
biolink
.getDescendantQualifiers(queryQualifier.qualifier_value as string)
.includes(qualifier.qualifier_value as string);
return typeMatch && valueMatch;
}) ?? false
);
});
}) ?? false;
const specialHandling = oneHop && predicateMatch && qualifierMatch;
if (specialHandling) {
translatedResult.analyses[0].edge_bindings = { [qEdgeID]: [{ id: boundEdgeID, attributes: [] }] };
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/inferred_mode/template_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ export async function getTemplates(lookups: TemplateLookup[]): Promise<MatchedTe
const matchingTemplatePaths: PathMatch[] = templateGroups.reduce((matches: PathMatch[], group: TemplateGroup) => {
let matchingQualifers: CompactQualifiers;
const lookupMatch = lookups.some((lookup) => {
const match = (
const match =
group.subject.includes(lookup.subject) &&
group.object.includes(lookup.object) &&
group.predicate.includes(lookup.predicate) &&
Object.entries(lookup.qualifiers || {}).every(([qualifierType, qualifierValue]) => {
return (group.qualifiers || {})[qualifierType] && group.qualifiers[qualifierType] === qualifierValue;
})
);
return (
(group.qualifiers || {})[qualifierType.replace('biolink:', '')] &&
group.qualifiers[qualifierType.replace('biolink:', '')] === qualifierValue.replace('biolink:', '')
);
});
if (match) matchingQualifers = lookup.qualifiers;
return match;
});

if (lookupMatch) {
group.templates.forEach((template) => {
if (!matches.find(t => t.path === templatePaths[template])) {
if (!matches.find((t) => t.path === templatePaths[template])) {
matches.push({ path: templatePaths[template], qualifiers: matchingQualifers });
}
});
Expand Down

0 comments on commit 6a8d1e5

Please sign in to comment.