Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move biolink, caching to utils #5

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions __test__/unittest/biolink.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { biolink } from '../../src/biolink';

describe('Test BioLinkModel class', () => {
test('test reverse with correct predicate', () => {
const res = biolink.reverse('treats');
expect(res).toBe('treated_by');
});

test('test reverse with correct predicate if it contains underscore', () => {
const res = biolink.reverse('treated_by');
expect(res).toBe('treats');
});

test('test reverse with predicate having symmetric equal to true', () => {
const res = biolink.reverse('correlated_with');
expect(res).toBe('correlated_with');
});

test('test predicate with no inverse property and symmetric not equal to true', () => {
const res = biolink.reverse('has_phenotype');
expect(res).toBe('phenotype_of');
});

test('test predicate not exist in biolink model', () => {
const res = biolink.reverse('haha');
expect(res).toBeUndefined();
});

test('if input not string, return undefined', () => {
//@ts-expect-error: Explicitly testing for wrong type
const res = biolink.reverse(['dd']);
expect(res).toBeUndefined();
});

describe('Test getDescendants function', () => {
test('if input is in biolink model, return all its desendants and itself', () => {
const res = biolink.getDescendantClasses('MolecularEntity');
expect(res).toContain('SmallMolecule');
expect(res).toContain('NucleicAcidEntity');
expect(res).toContain('MolecularEntity');
});

test("if input is in biolink model but doesn't have descendants, return itself", () => {
const res = biolink.getDescendantClasses('Gene');
expect(res).toEqual(['Gene']);
});

test('if input is not in biolink, return itself', () => {
const res = biolink.getDescendantClasses('Gene1');
expect(res).toEqual('Gene1');
});
});

describe('Test getDescendantPredicates function', () => {
test('if input is in biolink model, return all its desendants and itself', () => {
const res = biolink.getDescendantPredicates('related_to');
expect(res).toContain('subclass_of');
expect(res).toContain('superclass_of');
expect(res).toContain('related_to');
});

test("if input is in biolink model but doesn't have descendants, return itself", () => {
const res = biolink.getDescendantPredicates('subclass_of');
expect(res).toEqual(['subclass_of']);
});

test('if input is not in biolink, return itself', () => {
const res = biolink.getDescendantPredicates('Gene1');
expect(res).toEqual(['Gene1']);
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"ioredis": "^5.3.2",
"ioredis-mock": "^8.9.0",
"lodash": "^4.17.21",
"redlock": "5.0.0-beta.2"
"redlock": "5.0.0-beta.2",
"biolink-model": "workspace:*"
}
}
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]];

Check warning on line 28 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
}
return className;

Check warning on line 30 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L30

Added line #L30 was not covered by tests
}

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]];

Check warning on line 36 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L35-L36

Added lines #L35 - L36 were not covered by tests
}
return predicate;

Check warning on line 38 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L38

Added line #L38 was not covered by tests
}

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];

Check warning on line 60 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L58-L60

Added lines #L58 - L60 were not covered by tests
} catch (e) {
console.log('qual error', e);
return [qualifier];

Check warning on line 63 in src/biolink.ts

View check run for this annotation

Codecov / codecov/patch

src/biolink.ts#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}
}

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

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

export { biolink };
Loading
Loading