Skip to content

Commit

Permalink
get missing categories using sri resolver prior to query process
Browse files Browse the repository at this point in the history
  • Loading branch information
marcodarko committed Sep 7, 2021
1 parent 32fc883 commit 2378ec5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
* @private
* @param {object} queryGraph - TRAPI Query Graph Object
*/
_processQueryGraph_2(queryGraph) {
async _processQueryGraph_2(queryGraph) {
try {
let queryGraphHandler = new QueryGraph(queryGraph);
let res = queryGraphHandler.calculateEdges();
let res = await queryGraphHandler.calculateEdges();
this.logs = [...this.logs, ...queryGraphHandler.logs];
return res;
} catch (err) {
Expand Down Expand Up @@ -141,7 +141,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
debug('Start to load metakg.');
const kg = this._loadMetaKG(this.smartapiID, this.team);
debug('MetaKG successfully loaded!');
let queryEdges = this._processQueryGraph_2(this.queryGraph);
let queryEdges = await this._processQueryGraph_2(this.queryGraph);
debug(`(3) All edges created ${JSON.stringify(queryEdges)}`);
const manager = new EdgeManager(queryEdges);
while (manager.getEdgesNotExecuted()) {
Expand Down
63 changes: 54 additions & 9 deletions src/query_graph.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const QNode = require('./query_node');
const QEdge = require('./query_edge');
const QExecEdge = require('./query_execution_edge');
const _ = require('lodash');
const InvalidQueryGraphError = require('./exceptions/invalid_query_graph_error');
const LogEntry = require('./log_entry');
const NewExeEdge = require('./query_execution_edge_2');
const MAX_DEPTH = 3;
const debug = require('debug')('bte:biothings-explorer-trapi:query_graph');
const QNode2 = require('./query_node_2');
const id_resolver = require('biomedical_id_resolver');

module.exports = class QueryGraphHandler {
constructor(queryGraph) {
Expand Down Expand Up @@ -58,13 +58,57 @@ module.exports = class QueryGraphHandler {
return nodes;
}

/**
* @private
*/
async _findNodeCategories(ids) {
if (ids.length == 1) {
let category = await id_resolver.resolveSRI({
unknown: ids
});
debug(`Query node missing categories...Looking for match...`);
if (Object.hasOwnProperty.call(category, ids[0])) {
category = category[ids[0]][0]['semanticType'];
return ["biolink:" + category];
}else{
debug(`No category match found for ${JSON.stringify(ids)}.`);
return [];
}
}else{
debug(`(Error) Can't find category matches of multiple IDs.`);
return [];
}
}

/**
* @private
*/
_storeNodes_2() {
async _storeNodes_2() {
let nodes = {};
for (let node_id in this.queryGraph.nodes) {
nodes[node_id] = new QNode2(node_id, this.queryGraph.nodes[node_id]);
//if node has ID but no categories
if (
(!Object.hasOwnProperty.call(this.queryGraph.nodes[node_id], 'categories') &&
Object.hasOwnProperty.call(this.queryGraph.nodes[node_id], 'ids')) ||
(Object.hasOwnProperty.call(this.queryGraph.nodes[node_id], 'categories') &&
this.queryGraph.nodes[node_id].categories.length == 0 &&
Object.hasOwnProperty.call(this.queryGraph.nodes[node_id], 'ids'))
) {
let category = await this._findNodeCategories(this.queryGraph.nodes[node_id].ids);
this.queryGraph.nodes[node_id].categories = category;
debug(`Node category found. Assigning value: ${JSON.stringify(this.queryGraph.nodes[node_id])}`);
this.logs.push(
new LogEntry(
'DEBUG',
null,
`Assigned missing node ID category: ${JSON.stringify(this.queryGraph.nodes[node_id])}`).getLog(),
);
nodes[node_id] = new QNode2(node_id, this.queryGraph.nodes[node_id]);
}else{
debug(`Creating node...`);
nodes[node_id] = new QNode2(node_id, this.queryGraph.nodes[node_id]);
}

}
this.logs.push(
new LogEntry('DEBUG', null, `BTE identified ${Object.keys(nodes).length} QNodes from your query graph`).getLog(),
Expand All @@ -75,9 +119,9 @@ module.exports = class QueryGraphHandler {
/**
* @private
*/
_storeEdges() {
async _storeEdges() {
if (this.nodes === undefined) {
this.nodes = this._storeNodes();
this.nodes = await this._storeNodes();
}
let edges = {};
for (let edge_id in this.queryGraph.edges) {
Expand All @@ -99,9 +143,9 @@ module.exports = class QueryGraphHandler {
/**
* @private
*/
_storeEdges_2() {
async _storeEdges_2() {
if (this.nodes === undefined) {
this.nodes = this._storeNodes_2();
this.nodes = await this._storeNodes_2();
}
let edges = {};
for (let edge_id in this.queryGraph.edges) {
Expand Down Expand Up @@ -158,11 +202,12 @@ module.exports = class QueryGraphHandler {
/**
*
*/
calculateEdges() {
async calculateEdges() {
this._validate(this.queryGraph);
//populate edge and node info
debug(`(1) Creating edges for manager...`);
if (this.edges === undefined) {
this.edges = this._storeEdges_2();
this.edges = await this._storeEdges_2();
}
let edges = {};
let edge_index = 0;
Expand Down

0 comments on commit 2378ec5

Please sign in to comment.