Skip to content

Commit

Permalink
move edge filtering earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Jun 11, 2024
1 parent 7aa6bd6 commit b2d1389
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
36 changes: 33 additions & 3 deletions src/edge_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { UnavailableAPITracker } from './types';
import { RecordsByQEdgeID } from './results_assembly/query_results';
import path from 'path';
import { promises as fs } from 'fs';
import KnowledgeGraph from './graph/knowledge_graph';
import BTEGraph from './graph/graph';

export default class QueryEdgeManager {
private _qEdges: QEdge[];
Expand Down Expand Up @@ -211,7 +213,7 @@ export default class QueryEdgeManager {
subjectIDs.some((curie) => execSubjectCuries.includes(curie)) || execSubjectCuries.length === 0;
const objectMatch = objectIDs.some((curie) => execObjectCuries.includes(curie)) || execObjectCuries.length === 0;

//if both ends match then keep record
// if both ends match then keep record

// Don't keep self-edges
const selfEdge = [...subjectIDs].some((curie) => objectIDs.includes(curie));
Expand All @@ -230,6 +232,32 @@ export default class QueryEdgeManager {
return keep;
}

_constrainEdgeRecords(qEdge: QEdge, records: Record[]) {
const keep: Record[] = [];
const bte = new BTEGraph();
const kg = new KnowledgeGraph();
bte.update(records);
kg.update(bte);
records.forEach(record => {
const edge = kg.kg.edges[record.recordHash];
const sub = qEdge.reverse ? kg.kg.nodes[edge.object] : kg.kg.nodes[edge.subject];
const obj = qEdge.reverse ? kg.kg.nodes[edge.subject] : kg.kg.nodes[edge.object];
if (qEdge.meetsConstraints(edge, sub, obj)) {
keep.push(record);
}
});

debug(`'${qEdge.getID()}' dropped (${records.length - keep.length}) records based on edge/node constraints.`);
this.logs.push(
new LogEntry(
'DEBUG',
null,
`'${qEdge.getID()}' kept (${keep.length}) / dropped (${records.length - keep.length}) records (based on node/edge constraints).`,
).getLog(),
);
return keep;
}

collectRecords(): boolean {
//go through edges and collect records organized by edge
let recordsByQEdgeID: RecordsByQEdgeID = {};
Expand Down Expand Up @@ -296,8 +324,10 @@ export default class QueryEdgeManager {

updateEdgeRecords(currentQEdge: QEdge): void {
//1. filter edge records based on current status
const filteredRecords = this._filterEdgeRecords(currentQEdge);
//2.trigger node update / entity update based on new status
let filteredRecords = this._filterEdgeRecords(currentQEdge);
//2. make sure node/edge constraints are met
filteredRecords = this._constrainEdgeRecords(currentQEdge, filteredRecords);
//3. trigger node update / entity update based on new status
currentQEdge.storeRecords(filteredRecords);
}

Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,6 @@ export default class TRAPIQueryHandler {
//update query results
await this.trapiResultsAssembler.update(
manager.getOrganizedRecords(),
this.queryGraphHandler,
this.knowledgeGraph.kg,
!(this.options.smartAPIID || this.options.teamName)
);
this.logs = [...this.logs, ...this.trapiResultsAssembler.logs];
Expand Down
1 change: 0 additions & 1 deletion src/query_edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ export default class QEdge {


storeRecords(records: Record[]): void {
debug((new Error()).stack)
debug(`(6) Storing records...`);
// store new records in current edge
this.records = records;
Expand Down
10 changes: 2 additions & 8 deletions src/results_assembly/query_results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ export default class TrapiResultsAssembler {
_getQueryGraphSolutions(
recordsByQEdgeID: RecordsByQEdgeID,
qEdgeID: string,
queryGraph: QueryGraph,
kg: TrapiKnowledgeGraph,
edgeCount: number,
queryGraphSolutions: QueryGraphSolutionEdge[][],
queryGraphSolution: QueryGraphSolutionEdge[],
Expand Down Expand Up @@ -192,7 +190,7 @@ export default class TrapiResultsAssembler {

records
.filter((record) => {
return [getMatchingPrimaryCurie(record), undefined].indexOf(primaryCurieToMatch) > -1 && queryGraph.edges[qEdgeID].meetsConstraints(kg.edges[record.recordHash], kg.nodes[kg.edges[record.recordHash].subject], kg.nodes[kg.edges[record.recordHash].object]);
return [getMatchingPrimaryCurie(record), undefined].indexOf(primaryCurieToMatch) > -1;
})
.forEach((record, i) => {
// primaryCurie example: 'NCBIGene:1234'
Expand Down Expand Up @@ -225,8 +223,6 @@ export default class TrapiResultsAssembler {
this._getQueryGraphSolutions(
recordsByQEdgeID,
connectedQEdgeID,
queryGraph,
kg,
edgeCount,
queryGraphSolutions,
queryGraphSolution,
Expand Down Expand Up @@ -277,7 +273,7 @@ export default class TrapiResultsAssembler {
* can safely assume every call to update contains all the records.
*
*/
async update(recordsByQEdgeID: RecordsByQEdgeID, queryGraph: QueryGraph, kg: TrapiKnowledgeGraph, shouldScore = true): Promise<void> {
async update(recordsByQEdgeID: RecordsByQEdgeID, shouldScore = true): Promise<void> {
debug(`Updating query results now!`);

let scoreCombos: ScoreCombos;
Expand Down Expand Up @@ -322,8 +318,6 @@ export default class TrapiResultsAssembler {
this._getQueryGraphSolutions(
recordsByQEdgeID,
initialQEdgeID,
queryGraph,
kg,
qEdgeCount,
queryGraphSolutions,
[], // first queryGraphSolution
Expand Down

0 comments on commit b2d1389

Please sign in to comment.