Skip to content

Commit

Permalink
chore: move biolink methods to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Sep 26, 2024
1 parent 9bc677d commit ae2fcfc
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 175 deletions.
74 changes: 0 additions & 74 deletions src/biolink.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/graph/knowledge_graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { toArray } from '../utils';
import Debug from 'debug';
import {
TrapiAttribute,
Expand All @@ -14,7 +13,7 @@ import KGNode from './kg_node';
import KGEdge from './kg_edge';
import { BTEGraphUpdate } from './graph';
import { APIDefinition } from '@biothings-explorer/types';
import { Telemetry } from '@biothings-explorer/utils';
import { toArray, Telemetry } from '@biothings-explorer/utils';

const debug = Debug('bte:biothings-explorer-trapi:KnowledgeGraph');

Expand Down
36 changes: 24 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export default class TRAPIQueryHandler {
subject,
object,
});
const source = ontologyKnowledgeSourceMapping[this.subclassEdges[expanded][original].source] ?? 'error-not-provided';
const source =
ontologyKnowledgeSourceMapping[this.subclassEdges[expanded][original].source] ?? 'error-not-provided';
subclassEdge.addAdditionalAttributes('biolink:knowledge_level', 'knowledge_assertion');
subclassEdge.addAdditionalAttributes('biolink:agent_type', 'manual_agent');
subclassEdge.addSource([
Expand Down Expand Up @@ -430,7 +431,9 @@ export default class TRAPIQueryHandler {
const descendantsByCurie: { [curie: string]: { [descendants: string]: string } } = getDescendants(
queryGraph.nodes[nodeId].ids,
);
let expanded = Object.values(descendantsByCurie).map(descendants => Object.keys(descendants)).flat()
let expanded = Object.values(descendantsByCurie)
.map((descendants) => Object.keys(descendants))
.flat();

expanded = _.uniq([...queryGraph.nodes[nodeId].ids, ...expanded]);

Expand All @@ -442,10 +445,11 @@ export default class TRAPIQueryHandler {

if (foundExpandedIds) {
Object.entries(descendantsByCurie).forEach(([curie, descendants]) => {
Object.entries(descendants).forEach(([ descendant, source ]) => {
Object.entries(descendants).forEach(([descendant, source]) => {
if (queryGraph.nodes[nodeId].ids.includes(descendant)) return;
if (!this.subclassEdges[descendant]) this.subclassEdges[descendant] = {};
if (!this.subclassEdges[descendant][curie]) this.subclassEdges[descendant][curie] = { source, qNodes: [] };
if (!this.subclassEdges[descendant][curie])
this.subclassEdges[descendant][curie] = { source, qNodes: [] };
this.subclassEdges[descendant][curie].qNodes.push(nodeId);
});
});
Expand Down Expand Up @@ -474,10 +478,10 @@ export default class TRAPIQueryHandler {
}

async _processQueryGraph(queryGraph: TrapiQueryGraph): Promise<QEdge[]> {
const queryGraphHandler = new QueryGraph(queryGraph, this.options.schema, this._queryIsPathfinder());
const queryEdges = await queryGraphHandler.calculateEdges();
this.logs = [...this.logs, ...queryGraphHandler.logs];
return queryEdges;
const queryGraphHandler = new QueryGraph(queryGraph, this.options.schema, this._queryIsPathfinder());
const queryEdges = await queryGraphHandler.calculateEdges();
this.logs = [...this.logs, ...queryGraphHandler.logs];
return queryEdges;
}

async _edgesSupported(qEdges: QEdge[], metaKG: MetaKG): Promise<boolean> {
Expand Down Expand Up @@ -563,9 +567,17 @@ return queryEdges;
}

_queryIsPathfinder(): boolean {
const inferredEdgeCount = Object.values(this.queryGraph.edges).reduce((i, edge) => i + (edge.knowledge_type === 'inferred' ? 1 : 0), 0);
const inferredEdgeCount = Object.values(this.queryGraph.edges).reduce(
(i, edge) => i + (edge.knowledge_type === 'inferred' ? 1 : 0),
0,
);
const pinnedNodes = Object.values(this.queryGraph.nodes).reduce((i, node) => i + (node.ids?.length > 0 ? 1 : 0), 0);
return inferredEdgeCount === 3 && pinnedNodes == 2 && Object.keys(this.queryGraph.edges).length === 3 && Object.keys(this.queryGraph.nodes).length === 3;
return (
inferredEdgeCount === 3 &&
pinnedNodes == 2 &&
Object.keys(this.queryGraph.edges).length === 3 &&
Object.keys(this.queryGraph.nodes).length === 3
);
}

_queryUsesInferredMode(): boolean {
Expand All @@ -585,7 +597,7 @@ return queryEdges;
const pathfinderResponse = await pathfinderHandler.query();

if (pathfinderResponse) {
this.getResponse = () => pathfinderResponse;
this.getResponse = () => pathfinderResponse;
}
}

Expand All @@ -603,7 +615,7 @@ return queryEdges;
this.options,
this.path,
this.predicatePath,
this.includeReasoner
this.includeReasoner,
);
const inferredQueryResponse = await inferredQueryHandler.query();
if (inferredQueryResponse) {
Expand Down
15 changes: 7 additions & 8 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Debug from 'debug';
import * as utils from '@biothings-explorer/utils';
import { LogEntry, StampedLog, Telemetry } from '@biothings-explorer/utils';
import * as utils from '../utils';
import async from 'async';
import biolink from '../biolink';
import { getTemplates, MatchedTemplate, TemplateLookup } from './template_lookup';
import { scaled_sigmoid, inverse_scaled_sigmoid } from '../results_assembly/score';
import TRAPIQueryHandler from '../index';
Expand Down Expand Up @@ -164,13 +163,13 @@ export default class InferredQueryHandler {
async findTemplates(qEdge: TrapiQEdge, qSubject: TrapiQNode, qObject: TrapiQNode): Promise<MatchedTemplate[]> {
debug('Looking up query Templates');
const expandedSubject = qSubject.categories.reduce((arr: string[], subjectCategory: string) => {
return utils.getUnique([...arr, ...biolink.getDescendantClasses(utils.removeBioLinkPrefix(subjectCategory))]);
return utils.getUnique([...arr, ...utils.biolink.getDescendantClasses(utils.removeBioLinkPrefix(subjectCategory))]);
}, [] as string[]);
const expandedPredicates = qEdge.predicates.reduce((arr: string[], predicate: string) => {
return utils.getUnique([...arr, ...biolink.getDescendantPredicates(utils.removeBioLinkPrefix(predicate))]);
return utils.getUnique([...arr, ...utils.biolink.getDescendantPredicates(utils.removeBioLinkPrefix(predicate))]);
}, [] as string[]);
const expandedObject = qObject.categories.reduce((arr: string[], objectCategory: string) => {
return utils.getUnique([...arr, ...biolink.getDescendantClasses(utils.removeBioLinkPrefix(objectCategory))]);
return utils.getUnique([...arr, ...utils.biolink.getDescendantClasses(utils.removeBioLinkPrefix(objectCategory))]);
}, [] as string[]);
const qualifierConstraints = (qEdge.qualifier_constraints || []).map((qualifierSetObj) => {
return Object.fromEntries(
Expand Down Expand Up @@ -355,7 +354,7 @@ export default class InferredQueryHandler {
// Predicate matches or is descendant
const predicateMatch =
qEdge.predicates?.some((predicate) => {
const descendantMatch = biolink
const descendantMatch = utils.biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(predicate))
.includes(utils.removeBioLinkPrefix(boundEdge.predicate));
return predicate === boundEdge.predicate || descendantMatch;
Expand All @@ -372,10 +371,10 @@ export default class InferredQueryHandler {
let valueMatch: boolean;
try {
const descendants = queryQualifier.qualifier_value.includes('biolink:')
? biolink.getDescendantPredicates(
? utils.biolink.getDescendantPredicates(
utils.removeBioLinkPrefix(queryQualifier.qualifier_value as string),
)
: biolink.getDescendantQualifiers(
: utils.biolink.getDescendantQualifiers(
utils.removeBioLinkPrefix(queryQualifier.qualifier_value as string),
);
valueMatch =
Expand Down
9 changes: 4 additions & 5 deletions src/inferred_mode/pathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {
} from '@biothings-explorer/types';
import InferredQueryHandler from './inferred_mode';
import { scaled_sigmoid, inverse_scaled_sigmoid } from '../results_assembly/score';
import { LogEntry, StampedLog, Telemetry } from '@biothings-explorer/utils';
import * as utils from '@biothings-explorer/utils';
import { LogEntry, StampedLog, Telemetry, removeBioLinkPrefix } from '@biothings-explorer/utils';
import Debug from 'debug';
import generateTemplates from './pf_template_generator';
import biolink from '../biolink';
import { removeBioLinkPrefix } from '../utils';
const debug = Debug('bte:biothings-explorer-trapi:pathfinder');

interface ResultAuxObject {
Expand Down Expand Up @@ -163,7 +162,7 @@ export default class PathfinderQueryHandler {
const label = [this.mainEdge.subject, this.unpinnedNodeId, this.mainEdge.object];
const ancestorsToInject = new Set();
node.categories.forEach((category) => {
const ancestors = biolink.getAncestorClasses(removeBioLinkPrefix(category));
const ancestors = utils.biolink.getAncestorClasses(removeBioLinkPrefix(category));
if (!Array.isArray(ancestors)) return;
ancestors.forEach((ancestor) => {
if (FALLBACK_ANCESTORS.includes(ancestor)) {
Expand Down Expand Up @@ -242,7 +241,7 @@ export default class PathfinderQueryHandler {
if (this.unpinnedNode.categories && !this.unpinnedNode.categories.includes('biolink:NamedThing')) {
acceptableTypes = new Set<string>();
for (const category of this.unpinnedNode.categories) {
for (const desc of biolink.getDescendantClasses(removeBioLinkPrefix(category))) {
for (const desc of utils.biolink.getDescendantClasses(removeBioLinkPrefix(category))) {
acceptableTypes.add('biolink:' + desc);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/inferred_mode/pf_template_generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import fs from "fs/promises";
import yaml2json from "js-yaml";
import biolink from "../biolink";
import * as utils from "@biothings-explorer/utils";
import { TrapiQNode, TrapiQueryGraph } from "@biothings-explorer/types";

interface CategoryTable {
Expand All @@ -27,8 +27,8 @@ async function loadTables() {
for (const table of [categoryTable, predicateTable]) {
for (const category1 in table) {
for (const category2 in table[category1]) {
for (const descendant1 of biolink.getDescendantClasses(category1)) {
for (const descendant2 of biolink.getDescendantClasses(category2)) {
for (const descendant1 of utils.biolink.getDescendantClasses(category1)) {
for (const descendant2 of utils.biolink.getDescendantClasses(category2)) {
if (table?.['biolink:'+descendant1]?.['biolink:'+descendant2] !== undefined) continue;

if (!('biolink:'+descendant1 in table)) {
Expand Down Expand Up @@ -175,4 +175,4 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode,
queryGraphs.push(queryGraph);
}
return queryGraphs;
}
}
15 changes: 7 additions & 8 deletions src/query_edge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import helper from './helper';
import Debug from 'debug';
import * as utils from './utils';
import biolink from './biolink';
import { Record, RecordNode, FrozenRecord } from '@biothings-explorer/api-response-transform';
import QNode from './query_node';
import { QNodeInfo } from './query_node';
import * as utils from '@biothings-explorer/utils';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import { TrapiAttributeConstraint, TrapiQualifierConstraint } from '@biothings-explorer/types';

Expand Down Expand Up @@ -129,7 +128,7 @@ export default class QEdge {
}

expandPredicates(predicates: string[]): string[] {
return Array.from(new Set(predicates.reduce((acc, cur) => [...acc, ...biolink.getDescendantPredicates(cur)], [])));
return Array.from(new Set(predicates.reduce((acc, cur) => [...acc, ...utils.biolink.getDescendantPredicates(cur)], [])));
}

getPredicate(): string[] {
Expand All @@ -141,7 +140,7 @@ export default class QEdge {
debug(`Expanded edges: ${expandedPredicates}`);
return expandedPredicates
.map((predicate) => {
return this.isReversed() === true ? biolink.reverse(predicate) : predicate;
return this.isReversed() === true ? utils.biolink.reverse(predicate) : predicate;
})
.filter((item) => !(typeof item === 'undefined'));
}
Expand All @@ -154,21 +153,21 @@ export default class QEdge {
? Array.isArray(qualifier_value)
? Array.from(
qualifier_value.reduce((set: Set<string>, predicate: string) => {
biolink
utils.biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(predicate))
.forEach((item) => set.add(`biolink:${utils.removeBioLinkPrefix(item)}`));
return set;
}, new Set()),
)
: Array.from(
new Set(
biolink
utils.biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(qualifier_value))
.map((item) => `biolink:${utils.removeBioLinkPrefix(item)}`),
),
)
: Array.from(
new Set(biolink.getDescendantQualifiers(utils.removeBioLinkPrefix(qualifier_value as string))),
new Set(utils.biolink.getDescendantQualifiers(utils.removeBioLinkPrefix(qualifier_value as string))),
);

return {
Expand Down Expand Up @@ -575,6 +574,6 @@ export default class QEdge {
}

getReversedPredicate(predicate: string): string {
return predicate ? biolink.reverse(predicate) : undefined;
return predicate ? utils.biolink.reverse(predicate) : undefined;
}
}
5 changes: 2 additions & 3 deletions src/query_graph.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import QEdge from './query_edge';
import InvalidQueryGraphError from './exceptions/invalid_query_graph_error';
import * as utils from '@biothings-explorer/utils';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import Debug from 'debug';
import QNode from './query_node';
import biolink from './biolink';
import { resolveSRI } from 'biomedical_id_resolver';
import _ from 'lodash';
import * as utils from './utils';
import { TrapiQueryGraph } from '@biothings-explorer/types';
import NotImplementedError from './exceptions/not_implemented_error';

Expand Down Expand Up @@ -226,7 +225,7 @@ export default class QueryGraph {
} else {
try {
let finalCategories: string[] = [];
const tree = biolink.biolink.classTree.objects;
const tree = utils.biolink.biolink.classTree.objects;

// get array of all unique categories for all curies
const allCategories = [
Expand Down
Loading

0 comments on commit ae2fcfc

Please sign in to comment.