Skip to content

Commit

Permalink
feat: move biolink methods into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Sep 26, 2024
1 parent 0e577b6 commit 9088f75
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"@commitlint/cli": "^18.2.0",
"@commitlint/config-conventional": "^11.0.0",
"biolink-model": "workspace:../biolink-model",
"@types/debug": "^4.1.10",
"@types/jest": "^29.5.7",
"@types/lodash": "^4.14.200",
Expand Down
74 changes: 74 additions & 0 deletions src/biolink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { BioLink } from 'biolink-model';
import Debug from 'debug';
const debug = Debug('bte:biothings-explorer-trapi:EdgeReverse');

class BioLinkModel {
biolink: BioLink;
constructor() {
this.biolink = new BioLink();
this.biolink.loadSync();
}

reverse(predicate: string) {
if (typeof predicate === 'string') {
if (predicate in this.biolink.slotTree.objects) {
if (this.biolink.slotTree.objects[predicate].symmetric === true) {
return predicate;
}
return this.biolink.slotTree.objects[predicate].inverse;
}
}

return undefined;
}

getAncestorClasses(className: string): string | string[] {
if (className in this.biolink.classTree.objects) {
const ancestors = this.biolink.classTree.getAncestors(className).map((entity) => entity.name);
return [...ancestors, ...[className]];
}
return className;
}

getAncestorPredicates(predicate: string): string | string[] {
if (predicate in this.biolink.slotTree.objects) {
const ancestors = this.biolink.slotTree.getAncestors(predicate).map((entity) => entity.name);
return [...ancestors, ...[predicate]];
}
return predicate;
}

getDescendantClasses(className: string): string | string[] {
if (className in this.biolink.classTree.objects) {
const descendants = this.biolink.classTree.getDescendants(className).map((entity) => entity.name);
return [...descendants, ...[className]];
}
return className;
}

getDescendantPredicates(predicate: string): string[] {
if (predicate in this.biolink.slotTree.objects) {
const descendants = this.biolink.slotTree.getDescendants(predicate).map((entity) => entity.name);
return [...descendants, ...[predicate]];
}
return [predicate];
}

getDescendantQualifiers(qualifier: string): string[] {
try {
const descendants = this.biolink.enumTree.getDescendants(qualifier).map((entity) => entity.name);
return [...descendants, qualifier];
} catch (e) {
console.log('qual error', e);
return [qualifier];
}
}
}

// Freeze an instance to avoid multiple reloads
const biolink = new BioLinkModel();
Object.freeze(biolink);

global.BIOLINK_VERSION = biolink.biolink.biolinkJSON.version;

export { biolink };
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./log_entry";
export * from "./telemetry";
export * from "./misc";
export * from "./redis-client";
export * from "./biolink";
1 change: 1 addition & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ export function cartesian(a: number[][]): number[][] {
}
return o;
}

0 comments on commit 9088f75

Please sign in to comment.