From 66ba6695ede887bbff3b4f6da32fe6041d3c15dc Mon Sep 17 00:00:00 2001 From: rjawesome Date: Sat, 7 Sep 2024 18:01:28 -0700 Subject: [PATCH 1/3] dryrun pathfinder logs --- src/inferred_mode/pathfinder.ts | 25 ++++++++++++++++++++++ src/inferred_mode/pf_template_generator.ts | 19 +++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/inferred_mode/pathfinder.ts b/src/inferred_mode/pathfinder.ts index 07ecab9a..b339c10d 100644 --- a/src/inferred_mode/pathfinder.ts +++ b/src/inferred_mode/pathfinder.ts @@ -87,6 +87,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]; diff --git a/src/inferred_mode/pf_template_generator.ts b/src/inferred_mode/pf_template_generator.ts index dfbb5f6f..743b881b 100644 --- a/src/inferred_mode/pf_template_generator.ts +++ b/src/inferred_mode/pf_template_generator.ts @@ -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; @@ -44,7 +48,7 @@ async function loadTables() { tablesLoaded = true; } -export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, obj: TrapiQNode): Promise { +export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, obj: TrapiQNode): Promise<(AnnotatedQueryGraph & {log: string})[]> { // load tables if (!tablesLoaded) { if (!loadTablesPromise) { @@ -70,6 +74,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, object: 'creativeQueryObject', predicates: new Set(), } + }, + generateLog: function() { + return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); } }; const templateB = { @@ -95,6 +102,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, object: 'creativeQueryObject', predicates: new Set() } + }, + generateLog: function() { + return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_b.predicates].join(',')}--> Nb (${Array.from(this.nodes.nb.categories).join(',')}) --${[...this.edges.b_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); } }; const templateC = { @@ -120,6 +130,9 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, object: 'creativeQueryObject', predicates: new Set() } + }, + generateLog: function() { + return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_c.predicates].join(',')}--> Nc (${Array.from(this.nodes.nc.categories).join(',')}) --${[...this.edges.c_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); } }; for (const subCat of sub.categories) { @@ -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], From ea8072903495a1fa7e1ba2643270d6421d1b6bd0 Mon Sep 17 00:00:00 2001 From: rjawesome Date: Mon, 9 Sep 2024 15:17:27 -0700 Subject: [PATCH 2/3] change formatting of pf logs --- src/inferred_mode/pf_template_generator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/inferred_mode/pf_template_generator.ts b/src/inferred_mode/pf_template_generator.ts index 743b881b..fda83e2f 100644 --- a/src/inferred_mode/pf_template_generator.ts +++ b/src/inferred_mode/pf_template_generator.ts @@ -76,7 +76,7 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, } }, generateLog: function() { - return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); + 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 = { @@ -104,7 +104,7 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, } }, generateLog: function() { - return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_b.predicates].join(',')}--> Nb (${Array.from(this.nodes.nb.categories).join(',')}) --${[...this.edges.b_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); + 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 = { @@ -132,7 +132,7 @@ export default async function generateTemplates(sub: TrapiQNode, un: TrapiQNode, } }, generateLog: function() { - return `Sub (${sub.categories.join(',')}) --${[...this.edges.sub_c.predicates].join(',')}--> Nc (${Array.from(this.nodes.nc.categories).join(',')}) --${[...this.edges.c_un.predicates].join(',')}--> Un (${Array.from(this.nodes.un.categories).join(',')}) --${[...this.edges.un_obj.predicates].join(',')}--> Obj (${obj.categories.join(',')})`.replace(/biolink:/g, ''); + 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) { From 07ed2de792e9c2ef28f677cfdd7a507ff4c2c105 Mon Sep 17 00:00:00 2001 From: rjawesome Date: Mon, 9 Sep 2024 16:12:08 -0700 Subject: [PATCH 3/3] fix pf template gen test --- .../unittest/pf_template_generator.test.ts | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/__test__/unittest/pf_template_generator.test.ts b/__test__/unittest/pf_template_generator.test.ts index 80aff452..0cc20bec 100644 --- a/__test__/unittest/pf_template_generator.test.ts +++ b/__test__/unittest/pf_template_generator.test.ts @@ -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); @@ -21,12 +23,14 @@ describe('Test Pathfinder Template Generator', () => { "creativeQuerySubject": { "categories": [ "biolink:Drug" - ] + ], + "ids": ["subject"] }, "creativeQueryObject": { "categories": [ "biolink:Disease" - ] + ], + "ids": ["object"] }, "un": { "categories": [ @@ -57,7 +61,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]); @@ -67,12 +72,14 @@ describe('Test Pathfinder Template Generator', () => { "creativeQuerySubject": { "categories": [ "biolink:Drug" - ] + ], + "ids": ["subject"] }, "creativeQueryObject": { "categories": [ "biolink:Disease" - ] + ], + "ids": ["object"] }, "un": { "categories": [ @@ -125,7 +132,8 @@ describe('Test Pathfinder Template Generator', () => { "biolink:occurs_in" ] } - } + }, + "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]); @@ -135,12 +143,14 @@ describe('Test Pathfinder Template Generator', () => { "creativeQuerySubject": { "categories": [ "biolink:Drug" - ] + ], + "ids": ["subject"] }, "creativeQueryObject": { "categories": [ "biolink:Disease" - ] + ], + "ids": ["object"] }, "un": { "categories": [ @@ -187,20 +197,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);