From cbb8497b7072b25ee762330cb7846806f6089281 Mon Sep 17 00:00:00 2001 From: rjawesome Date: Mon, 16 Sep 2024 11:23:47 -0700 Subject: [PATCH 1/2] do pfocr for creative mode at the end --- src/index.ts | 12 +++++++----- src/inferred_mode/inferred_mode.ts | 22 ++++++---------------- src/results_assembly/pfocr.ts | 21 ++++++++++++++++++++- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8221e1d0..12fccacd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -474,10 +474,10 @@ export default class TRAPIQueryHandler { } async _processQueryGraph(queryGraph: TrapiQueryGraph): Promise { -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 { @@ -782,7 +782,9 @@ return queryEdges; this.bteGraph.notify(); // Attempt to enrich results with PFOCR figures - this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(this.getResponse()))]; + if (!this.options.skipPfocr) { + this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(this.getResponse()))]; + } span3?.finish(); diff --git a/src/inferred_mode/inferred_mode.ts b/src/inferred_mode/inferred_mode.ts index fac5ac52..ec5b938f 100644 --- a/src/inferred_mode/inferred_mode.ts +++ b/src/inferred_mode/inferred_mode.ts @@ -20,6 +20,7 @@ import { TrapiAnalysis } from '@biothings-explorer/types'; import { CompactQualifiers } from '../index'; +import { enrichTrapiResultsWithPfocrFigures } from '../results_assembly/pfocr'; const debug = Debug('bte:biothings-explorer-trapi:inferred-mode'); export interface CombinedResponse { @@ -301,7 +302,6 @@ export default class InferredQueryHandler { [qEdge.subject]: [{ id: result.node_bindings.creativeQuerySubject[0].id, attributes: [] }], [qEdge.object]: [{ id: result.node_bindings.creativeQueryObject[0].id, attributes: [] }], }, - pfocr: result.pfocr?.length ? result.pfocr : undefined, analyses: [ { resource_id: result.analyses[0].resource_id, @@ -473,20 +473,6 @@ export default class InferredQueryHandler { }); }); - // Combine, re-sort, and truncate to 20 any pfocr figures - if (combinedResponse.message.results[resultID].pfocr || translatedResult.pfocr) { - let reSort = false; - if (combinedResponse.message.results[resultID].pfocr && translatedResult.pfocr) reSort = true; - let newFigures = [ - ...(combinedResponse.message.results[resultID].pfocr ?? []), - ...(translatedResult.pfocr ?? []), - ]; - if (reSort) { - newFigures = newFigures.sort((figA, figB) => figB.score - figA.score).slice(0, 20); - } - combinedResponse.message.results[resultID].pfocr = newFigures; - } - const resScore = translatedResult.analyses[0].score; if (typeof combinedResponse.message.results[resultID].analyses[0].score !== 'undefined') { combinedResponse.message.results[resultID].analyses[0].score = resScore @@ -644,7 +630,7 @@ export default class InferredQueryHandler { } if (global.queryInformation != null) global.queryInformation.totalRecords = 0; // Reset between templates - const handler = new TRAPIQueryHandler(this.options, this.path, this.predicatePath, this.includeReasoner); + const handler = new TRAPIQueryHandler({ ...this.options, skipPfocr: true }, this.path, this.predicatePath, this.includeReasoner); try { // make query and combine results/kg/logs/etc handler.setQueryGraph(queryGraph); @@ -725,6 +711,10 @@ export default class InferredQueryHandler { response.message.results = response.message.results.slice(0, this.CREATIVE_LIMIT); response.description = `Query processed successfully, retrieved ${response.message.results.length} results.`; this.pruneKnowledgeGraph(response); + + // add pfocr figures + this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(response, true))]; + // get the final summary log if (successfulQueries) { this.parent diff --git a/src/results_assembly/pfocr.ts b/src/results_assembly/pfocr.ts index 5fab0980..b4819c0b 100644 --- a/src/results_assembly/pfocr.ts +++ b/src/results_assembly/pfocr.ts @@ -176,7 +176,7 @@ function traverseResultForNodes(result: TrapiResult, response: TrapiResponse): S * t: trapiResults.length * f: figures.length */ -export async function enrichTrapiResultsWithPfocrFigures(response: TrapiResponse): Promise { +export async function enrichTrapiResultsWithPfocrFigures(response: TrapiResponse, checkAuxGraphs = false): Promise { // NOTE: This function operates on the actual TRAPI information that will be returned // to the client. Don't mutate what shouldn't be mutated! const results = response.message.results; @@ -192,6 +192,25 @@ export async function enrichTrapiResultsWithPfocrFigures(response: TrapiResponse Object.values(result.node_bindings).forEach((bindings) => bindings.forEach((binding) => nodes.add(response.message.knowledge_graph.nodes[binding.id])), ); + // check aux graphs if applicable + if (checkAuxGraphs) { + for (const edgeId in result.analyses[0].edge_bindings) { + for (const eb of result.analyses[0].edge_bindings[edgeId]) { + const edge = response.message.knowledge_graph.edges[eb.id]; + const supportGraphs = edge.attributes.find((attribute) => attribute.attribute_type_id == 'biolink:support_graphs'); + if (supportGraphs) { + (supportGraphs.value as string[]).forEach((auxGraphID) => + response.message.auxiliary_graphs[auxGraphID].edges.forEach((edgeID) => { + const edge = response.message.knowledge_graph.edges[edgeID]; + nodes.add(response.message.knowledge_graph.nodes[edge.subject]); + nodes.add(response.message.knowledge_graph.nodes[edge.object]); + }), + ); + } + } + } + } + const combo: Set = new Set(); let matchedNodes = 0; [...nodes].forEach((node) => { From 066a273e68da3360e7da8384e8e8d26e0c412d93 Mon Sep 17 00:00:00 2001 From: rjawesome Date: Mon, 16 Sep 2024 16:45:52 -0700 Subject: [PATCH 2/2] pfocr enrichment for pathfinder --- src/inferred_mode/inferred_mode.ts | 4 +++- src/inferred_mode/pathfinder.ts | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/inferred_mode/inferred_mode.ts b/src/inferred_mode/inferred_mode.ts index ec5b938f..0b66eb28 100644 --- a/src/inferred_mode/inferred_mode.ts +++ b/src/inferred_mode/inferred_mode.ts @@ -713,7 +713,9 @@ export default class InferredQueryHandler { this.pruneKnowledgeGraph(response); // add pfocr figures - this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(response, true))]; + if (!this.pathfinder) { + this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(response, true))]; + } // get the final summary log if (successfulQueries) { diff --git a/src/inferred_mode/pathfinder.ts b/src/inferred_mode/pathfinder.ts index 07ecab9a..13d68ec9 100644 --- a/src/inferred_mode/pathfinder.ts +++ b/src/inferred_mode/pathfinder.ts @@ -15,6 +15,7 @@ import Debug from 'debug'; import generateTemplates from './pf_template_generator'; import biolink from '../biolink'; import { removeBioLinkPrefix } from '../utils'; +import { enrichTrapiResultsWithPfocrFigures } from '../results_assembly/pfocr'; const debug = Debug('bte:biothings-explorer-trapi:pathfinder'); interface ResultAuxObject { @@ -116,6 +117,9 @@ export default class PathfinderQueryHandler { this.parse(creativeResponse); this._pruneKg(creativeResponse); + // pfocr + this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(creativeResponse, true))]; + // logs creativeResponse.logs = this.logs.map((log) => log.toJSON());