Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…raph_handler into ctkp-reverse
  • Loading branch information
tokebe committed Sep 27, 2024
2 parents 8ade72b + 6ebae13 commit 842691b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 33 deletions.
39 changes: 26 additions & 13 deletions __test__/unittest/pf_template_generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import generateTemplates from '../../src/inferred_mode/pf_template_generator';
describe('Test Pathfinder Template Generator', () => {
test('Should generate correct templates', async () => {
const sub = {
categories: ['biolink:Drug']
categories: ['biolink:Drug'],
ids: ['subject']
};
const un = {
categories: ['biolink:Gene']
};
const obj = {
categories: ['biolink:Disease']
categories: ['biolink:Disease'],
ids: ['object']
};

const templatesWithUnCat = await generateTemplates(sub, un, obj);
Expand All @@ -21,12 +23,14 @@ describe('Test Pathfinder Template Generator', () => {
"creativeQuerySubject": {
"categories": [
"biolink:Drug"
]
],
"ids": ["subject"]
},
"creativeQueryObject": {
"categories": [
"biolink:Disease"
]
],
"ids": ["object"]
},
"un": {
"categories": [
Expand Down Expand Up @@ -54,7 +58,8 @@ describe('Test Pathfinder Template Generator', () => {
"biolink:contributes_to"
]
}
}
},
"log": "(subject) -(regulates,regulated_by,affects,affected_by,interacts_with,associated_with)-> (Gene) -(gene_associated_with_condition,biomarker_for,affects,contributes_to)-> (object)",
});
expect(templatesWithoutUnCat[0]).toEqual(templatesWithUnCat[0]);

Expand All @@ -64,12 +69,14 @@ describe('Test Pathfinder Template Generator', () => {
"creativeQuerySubject": {
"categories": [
"biolink:Drug"
]
],
"ids": ["subject"]
},
"creativeQueryObject": {
"categories": [
"biolink:Disease"
]
],
"ids": ["object"]
},
"un": {
"categories": [
Expand Down Expand Up @@ -112,7 +119,8 @@ describe('Test Pathfinder Template Generator', () => {
"biolink:interacts_with"
]
}
}
},
"log": "(subject) -(regulates,regulated_by,affects,affected_by,interacts_with,associated_with)-> (Gene) -(related_to_at_instance_level,affects,contributes_to,participates_in,regulates,regulated_by,affected_by,interacts_with,correlated_with)-> (AnatomicalEntity,BiologicalProcessOrActivity,ChemicalEntity) -(related_to_at_instance_level,affects,affected_by,occurs_in)-> (object)"
});
expect(templatesWithoutUnCat[1]).toEqual(templatesWithUnCat[1]);

Expand All @@ -122,12 +130,14 @@ describe('Test Pathfinder Template Generator', () => {
"creativeQuerySubject": {
"categories": [
"biolink:Drug"
]
],
"ids": ["subject"]
},
"creativeQueryObject": {
"categories": [
"biolink:Disease"
]
],
"ids": ["object"]
},
"un": {
"categories": [
Expand Down Expand Up @@ -172,20 +182,23 @@ describe('Test Pathfinder Template Generator', () => {
"biolink:contributes_to"
]
}
}
},
"log": "(subject) -(regulates,regulated_by,affects,affected_by,interacts_with,associated_with)-> (Gene) -(regulates,regulated_by,affects,affected_by,interacts_with)-> (Gene) -(gene_associated_with_condition,biomarker_for,affects,contributes_to)-> (object)"
});
expect(templatesWithoutUnCat[2]).toEqual(templatesWithUnCat[2]);
});

test('template with no predicates should not have predicate property', async () => {
const sub = {
categories: ['biolink:Drug']
categories: ['biolink:Drug'],
ids: ['subject']
};
const un = {
categories: ['biolink:Dummy']
};
const obj = {
categories: ['biolink:Drug']
categories: ['biolink:Drug'],
ids: ['object']
};

const templates = await generateTemplates(sub, un, obj);
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,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))];
}

// get the final summary log
if (successfulQueries) {
this.parent
Expand Down
29 changes: 29 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 * as utils from '@biothings-explorer/utils';
import { LogEntry, StampedLog, Telemetry, removeBioLinkPrefix } from '@biothings-explorer/utils';
import Debug from 'debug';
import generateTemplates from './pf_template_generator';
import { enrichTrapiResultsWithPfocrFigures } from '../results_assembly/pfocr';
const debug = Debug('bte:biothings-explorer-trapi:pathfinder');

interface ResultAuxObject {
Expand Down Expand Up @@ -87,6 +88,31 @@ export default class PathfinderQueryHandler {
debug(logMessage);
this.logs.push(new LogEntry('INFO', null, logMessage).getLog());

// log all the templates
const templateNames = ['A', 'B', 'C'];
for (let i = 0; i < 3; i++) {
let logMessage = `Pathfinder Template ${templateNames[i]}: ${templates[i].log}`;
debug(logMessage);
this.logs.push(new LogEntry('INFO', null, logMessage).getLog());
}

// handle dry run scenario
if (this.options.dryrun_pathfinder) {
return {
description: `Pathfinder Dry Run completed successfully. No results received. ${templates.length} templates generated.`,
schema_version: global.SCHEMA_VERSION,
biolink_version: global.BIOLINK_VERSION,
workflow: [{ id: this.options.smartAPIID || this.options.teamName ? 'lookup' : 'lookup_and_score' }],
message: {
query_graph: this.parent.originalQueryGraph,
knowledge_graph: this.parent.knowledgeGraph.kg,
auxiliary_graphs: {},
results: [],
},
logs: this.logs.map((log) => log.toJSON()),
};
}

// remove unpinned node & all edges involving unpinned node for now
delete this.queryGraph.nodes[this.unpinnedNodeId];

Expand Down Expand Up @@ -116,6 +142,9 @@ export default class PathfinderQueryHandler {
this.parse(creativeResponse);
this._pruneKg(creativeResponse);

// pfocr
this.logs = [...this.logs, ...(await enrichTrapiResultsWithPfocrFigures(creativeResponse))];

// logs
creativeResponse.logs = this.logs.map((log) => log.toJSON());

Expand Down
19 changes: 16 additions & 3 deletions src/inferred_mode/pf_template_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface PredicateTable {
[category1: string]: { [category2: string]: { predicate: string }[] };
}

interface AnnotatedQueryGraph extends TrapiQueryGraph {
log: string;
}

let categoryTable: CategoryTable;
let predicateTable: PredicateTable;
let loadTablesPromise: Promise<void>;
Expand Down Expand Up @@ -44,7 +48,7 @@ async function loadTables() {
tablesLoaded = true;
}

export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, obj: TrapiQNode): Promise<TrapiQueryGraph[]> {
export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, obj: TrapiQNode): Promise<(AnnotatedQueryGraph & {log: string})[]> {
// load tables
if (!tablesLoaded) {
if (!loadTablesPromise) {
Expand All @@ -70,6 +74,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode,
object: 'creativeQueryObject',
predicates: new Set<string>(),
}
},
generateLog: function() {
return `(${sub.ids.join(',')}) -(${[...this.edges.sub_un.predicates].join(',')})-> (${Array.from(this.nodes.un.categories).join(',')}) -(${[...this.edges.un_obj.predicates].join(',')})-> (${obj.ids.join(',')})`.replace(/biolink:/g, '');
}
};
const templateB = {
Expand All @@ -95,6 +102,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode,
object: 'creativeQueryObject',
predicates: new Set<string>()
}
},
generateLog: function() {
return `(${sub.ids.join(',')}) -(${[...this.edges.sub_un.predicates].join(',')})-> (${Array.from(this.nodes.un.categories).join(',')}) -(${[...this.edges.un_b.predicates].join(',')})-> (${Array.from(this.nodes.nb.categories).join(',')}) -(${[...this.edges.b_obj.predicates].join(',')})-> (${obj.ids.join(',')})`.replace(/biolink:/g, '');
}
};
const templateC = {
Expand All @@ -120,6 +130,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode,
object: 'creativeQueryObject',
predicates: new Set<string>()
}
},
generateLog: function() {
return `(${sub.ids.join(',')}) -(${[...this.edges.sub_c.predicates].join(',')})-> (${Array.from(this.nodes.nc.categories).join(',')}) -(${[...this.edges.c_un.predicates].join(',')})-> (${Array.from(this.nodes.un.categories).join(',')}) -(${[...this.edges.un_obj.predicates].join(',')})-> (${obj.ids.join(',')})`.replace(/biolink:/g, '');
}
};
for (const subCat of sub.categories) {
Expand Down Expand Up @@ -154,9 +167,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode,
}
}

const queryGraphs: TrapiQueryGraph[] = [];
const queryGraphs: AnnotatedQueryGraph[] = [];
for (const template of [templateA, templateB, templateC]) {
const queryGraph: TrapiQueryGraph = { nodes: {}, edges: {} };
const queryGraph: AnnotatedQueryGraph = { nodes: {}, edges: {}, log: template.generateLog()};
for (const node in template.nodes) {
queryGraph.nodes[node] = {
...template.nodes[node],
Expand Down
1 change: 1 addition & 0 deletions src/results_assembly/pfocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ 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])),
);

const combo: Set<string> = new Set();
let matchedNodes = 0;
[...nodes].forEach((node) => {
Expand Down

0 comments on commit 842691b

Please sign in to comment.