From ec0816e39675da9fae506cb3d7f8d6998b349091 Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Fri, 5 Aug 2022 13:49:52 -0700 Subject: [PATCH] use canonical direction --- src/biolink.js | 13 +++++++++++++ src/graph/graph.js | 13 +++++++++++++ src/index.js | 1 + 3 files changed, 27 insertions(+) diff --git a/src/biolink.js b/src/biolink.js index c1471177..c734e174 100644 --- a/src/biolink.js +++ b/src/biolink.js @@ -13,6 +13,19 @@ class BioLinkModel { return BioLinkModel.instance; } + isCanonical(predicate) { + if (typeof predicate === 'string') { + if (predicate in this.biolink.slotTree.objects) { + console.log("slttree") + console.log(this.biolink.slotTree.objects[predicate]) + if (this.biolink.slotTree.objects[predicate].canonical_predicate !== true) { + return false; + } + } + } + return true; + } + reverse(predicate) { if (typeof predicate === 'string') { if (predicate in this.biolink.slotTree.objects) { diff --git a/src/graph/graph.js b/src/graph/graph.js index c5916066..60279a66 100644 --- a/src/graph/graph.js +++ b/src/graph/graph.js @@ -1,6 +1,7 @@ const kg_edge = require('./kg_edge'); const kg_node = require('./kg_node'); const debug = require('debug')('bte:biothings-explorer-trapi:Graph'); +const biolink = require('../biolink') module.exports = class BTEGraph { constructor() { @@ -91,6 +92,18 @@ module.exports = class BTEGraph { debug(`pruned ${nodesToDelete.length} nodes and ${edgesToDelete.length} edges from BTEGraph.`); } + useCanonicalDirection() { + Object.keys(this.edges).map(edgeID => { + const predicate = this.edges[edgeID].predicate.substring(8) + if (!biolink.isCanonical(predicate)) { + let temp = this.edges[edgeID].subject + this.edges[edgeID].subject = this.edges[edgeID].object + this.edges[edgeID].object = temp + this.edges[edgeID].predicate = 'biolink:' + biolink.reverse(predicate) + } + }) + } + /** * Register subscribers * @param {object} subscriber diff --git a/src/index.js b/src/index.js index ae01ee7e..f56ef9bb 100644 --- a/src/index.js +++ b/src/index.js @@ -769,6 +769,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler { this.logs = [...this.logs, ...this.trapiResultsAssembler.logs]; // prune bteGraph this.bteGraph.prune(this.trapiResultsAssembler.getResults()); + this.bteGraph.useCanonicalDirection() this.bteGraph.notify(); // finishing logs this.createSummaryLog(this.logs).forEach((log) => this.logs.push(log));