Skip to content

Commit

Permalink
Merge pull request #208 from biothings/subclassing-fix
Browse files Browse the repository at this point in the history
fix subclassing, self graphs
  • Loading branch information
tokebe authored Aug 22, 2024
2 parents f29d19d + 05d7b46 commit f4ae7fa
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 105 deletions.
48 changes: 36 additions & 12 deletions src/edge_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import QEdge from './query_edge';
import MetaKG from '@biothings-explorer/smartapi-kg';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import { Record } from '@biothings-explorer/api-response-transform';
import { UnavailableAPITracker } from './types';
import { SubclassEdges, UnavailableAPITracker } from './types';
import { RecordsByQEdgeID } from './results_assembly/query_results';
import path from 'path';
import { promises as fs } from 'fs';
Expand All @@ -22,7 +22,8 @@ export default class QueryEdgeManager {
private _records: Record[];
options: QueryHandlerOptions;
private _organizedRecords: RecordsByQEdgeID;
constructor(edges: QEdge[], metaKG: MetaKG, options: QueryHandlerOptions) {
private _subclassEdges: SubclassEdges;
constructor(edges: QEdge[], metaKG: MetaKG, subclassEdges: SubclassEdges, options: QueryHandlerOptions) {
// flatten list of all edges available
this._qEdges = _.flatten(edges);
this._metaKG = metaKG;
Expand All @@ -31,6 +32,7 @@ export default class QueryEdgeManager {
//organized by edge with refs to connected edges
this._organizedRecords = {};
this.options = options;
this._subclassEdges = subclassEdges;
this.init();
}

Expand Down Expand Up @@ -108,7 +110,7 @@ export default class QueryEdgeManager {
}
debug(
`(5) Sending next edge '${nextQEdge.getID()}' ` +
`WITH entity count...(${nextQEdge.subject.entity_count || nextQEdge.object.entity_count})`,
`WITH entity count...(${nextQEdge.subject.entity_count || nextQEdge.object.entity_count})`,
);
return this.preSendOffCheck(nextQEdge);
}
Expand All @@ -117,18 +119,19 @@ export default class QueryEdgeManager {
this._qEdges.forEach((qEdge) => {
debug(
`'${qEdge.getID()}'` +
` : (${qEdge.subject.entity_count || 0}) ` +
`${qEdge.reverse ? '<--' : '-->'}` +
` (${qEdge.object.entity_count || 0})`,
` : (${qEdge.subject.entity_count || 0}) ` +
`${qEdge.reverse ? '<--' : '-->'}` +
` (${qEdge.object.entity_count || 0})`,
);
});
}

_logSkippedQueries(unavailableAPIs: UnavailableAPITracker): void {
Object.entries(unavailableAPIs).forEach(([api, { skippedQueries }]) => {
if (skippedQueries > 0) {
const skipMessage = `${skippedQueries} additional quer${skippedQueries > 1 ? 'ies' : 'y'} to ${api} ${skippedQueries > 1 ? 'were' : 'was'
} skipped as the API was unavailable.`;
const skipMessage = `${skippedQueries} additional quer${skippedQueries > 1 ? 'ies' : 'y'} to ${api} ${
skippedQueries > 1 ? 'were' : 'was'
} skipped as the API was unavailable.`;
debug(skipMessage);
this.logs.push(new LogEntry('WARNING', null, skipMessage).getLog());
}
Expand Down Expand Up @@ -194,7 +197,7 @@ export default class QueryEdgeManager {
const objectCuries = qEdge.object.curie;
debug(
`'${qEdge.getID()}' Reversed[${qEdge.reverse}] (${JSON.stringify(subjectCuries.length || 0)})` +
`--(${JSON.stringify(objectCuries.length || 0)}) entities / (${records.length}) records.`,
`--(${JSON.stringify(objectCuries.length || 0)}) entities / (${records.length}) records.`,
);
// debug(`IDS SUB ${JSON.stringify(sub_count)}`)
// debug(`IDS OBJ ${JSON.stringify(obj_count)}`)
Expand All @@ -203,8 +206,28 @@ export default class QueryEdgeManager {

records.forEach((record) => {
// check against original, primaryID, and equivalent ids
const subjectIDs = [record.subject.original, record.subject.curie, ...record.subject.equivalentCuries];
const objectIDs = [record.object.original, record.object.curie, ...record.object.equivalentCuries];
let subjectIDs = [record.subject.original, record.subject.curie, ...record.subject.equivalentCuries];
let objectIDs = [record.object.original, record.object.curie, ...record.object.equivalentCuries];

// check if IDs will be resolved to a parent
subjectIDs = [
...subjectIDs,
...subjectIDs.reduce((set, subjectID) => {
Object.entries(this._subclassEdges[subjectID] ?? {}).forEach(([id, { qNodes }]) => {
if (qNodes.includes(qEdge.reverse ? qEdge.object.id : qEdge.subject.id)) set.add(id);
});
return set;
}, new Set<string>()),
];
objectIDs = [
...objectIDs,
...objectIDs.reduce((set, objectID) => {
Object.entries(this._subclassEdges[objectID] ?? {}).forEach(([id, { qNodes }]) => {
if (qNodes.includes(qEdge.reverse ? qEdge.subject.id : qEdge.object.id)) set.add(id);
});
return set;
}, new Set<string>()),
];

// there must be at least a minimal intersection
const subjectMatch = subjectIDs.some((curie) => execSubjectCuries.includes(curie));
Expand Down Expand Up @@ -393,7 +416,8 @@ export default class QueryEdgeManager {
new LogEntry(
'INFO',
null,
`Executing ${currentQEdge.getID()}${currentQEdge.isReversed() ? ' (reversed)' : ''}: ${currentQEdge.subject.id
`Executing ${currentQEdge.getID()}${currentQEdge.isReversed() ? ' (reversed)' : ''}: ${
currentQEdge.subject.id
} ${currentQEdge.isReversed() ? '<--' : '-->'} ${currentQEdge.object.id}`,
).getLog(),
);
Expand Down
Loading

0 comments on commit f4ae7fa

Please sign in to comment.