Skip to content

Commit

Permalink
Merge branch 'pf-dryrun' of https://github.com/biothings/bte_trapi_qu…
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Sep 17, 2024
2 parents df33afb + 07ed2de commit affe4d5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 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 @@ -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]);

Expand All @@ -67,12 +72,14 @@ describe('Test Pathfinder Template Generator', () => {
"creativeQuerySubject": {
"categories": [
"biolink:Drug"
]
],
"ids": ["subject"]
},
"creativeQueryObject": {
"categories": [
"biolink:Disease"
]
],
"ids": ["object"]
},
"un": {
"categories": [
Expand Down Expand Up @@ -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]);

Expand All @@ -135,12 +143,14 @@ describe('Test Pathfinder Template Generator', () => {
"creativeQuerySubject": {
"categories": [
"biolink:Drug"
]
],
"ids": ["subject"]
},
"creativeQueryObject": {
"categories": [
"biolink:Disease"
]
],
"ids": ["object"]
},
"un": {
"categories": [
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions src/inferred_mode/pathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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
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

0 comments on commit affe4d5

Please sign in to comment.