Skip to content

Commit

Permalink
Reapply "Merge branch 'creative-pfocr-fix' of https://github.com/biot…
Browse files Browse the repository at this point in the history
…hings/bte_trapi_query_graph_handler into dev"

This reverts commit 1f1a8ba.
  • Loading branch information
tokebe committed Sep 18, 2024
1 parent 1f1a8ba commit cb803ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,9 @@ export default class TRAPIQueryHandler {
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
24 changes: 8 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 @@ -311,7 +312,6 @@ export default class InferredQueryHandler {
[qEdge.subject]: [subjectBinding],
[qEdge.object]: [objectBinding],
},
pfocr: result.pfocr?.length ? result.pfocr : undefined,
analyses: [
{
resource_id: result.analyses[0].resource_id,
Expand Down Expand Up @@ -483,20 +483,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 @@ -654,7 +640,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 @@ -735,6 +721,12 @@ 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
if (!this.pathfinder) {
this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(response, true))];
}

// get the final summary log
if (successfulQueries) {
this.parent
Expand Down
4 changes: 4 additions & 0 deletions src/inferred_mode/pathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 {
Expand Down Expand Up @@ -142,6 +143,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());

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 cb803ce

Please sign in to comment.