Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do pfocr for creative/pathfinder mode at the end #217

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -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,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 @@ -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 {
Expand Down Expand Up @@ -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());

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
Loading