Skip to content

Commit

Permalink
do pfocr for creative mode at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Sep 16, 2024
1 parent 6010ce6 commit cbb8497
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,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 @@ -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();

Expand Down
22 changes: 6 additions & 16 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/results_assembly/pfocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function traverseResultForNodes(result: TrapiResult, response: TrapiResponse): S
* t: trapiResults.length
* f: figures.length
*/
export async function enrichTrapiResultsWithPfocrFigures(response: TrapiResponse): Promise<StampedLog[]> {
export async function enrichTrapiResultsWithPfocrFigures(response: TrapiResponse, checkAuxGraphs = false): Promise<StampedLog[]> {
// 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;
Expand All @@ -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<string> = new Set();
let matchedNodes = 0;
[...nodes].forEach((node) => {
Expand Down

0 comments on commit cbb8497

Please sign in to comment.