Skip to content

Commit

Permalink
Merge pull request #182 from biothings/intersection-speedup
Browse files Browse the repository at this point in the history
Speed up intersectWithExpandedCuries
  • Loading branch information
tokebe authored Mar 7, 2024
2 parents c2d24d0 + 0572022 commit 559de80
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/query_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,31 @@ export default class QNode {
return [...combined];
}


intersectWithExpandedCuries(newCuries: ExpandedCuries): void {
const keep: { [mainID: string]: string[] } = {};

const existingSet = new Set();
for (const key in this.expanded_curie) {
for (const curie of this.expanded_curie[key]) {
existingSet.add(curie.toLowerCase());
}
}

// If a new entity has any alias intersection with an existing entity, keep it
Object.entries(newCuries).forEach(([newMainID, currentAliases]) => {
const someIntersection = Object.values(this.expanded_curie).some((existingAliases) => {
return currentAliases.some((currentAlias) =>
existingAliases.some((existingAlias) => currentAlias.toLowerCase() === existingAlias.toLowerCase()),
);
});
for (const [newMainID, currentAliases] of Object.entries(newCuries)) {
let someIntersection = false;
for (const curie of currentAliases) {
if (existingSet.has(curie.toLowerCase())) {
someIntersection = true;
break;
}
}

if (someIntersection) {
if (!keep[newMainID]) keep[newMainID] = currentAliases;
}
});
}

//save expanded curies (main + aliases)
this.expanded_curie = keep;
Expand Down

0 comments on commit 559de80

Please sign in to comment.