Skip to content

Commit

Permalink
Merge pull request #12 from biothings/fix_issue_185
Browse files Browse the repository at this point in the history
feat: post query filter by predicates
  • Loading branch information
newgene authored Jun 4, 2021
2 parents d29463e + 774e4f9 commit d97cd02
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 19 additions & 1 deletion src/batch_edge_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const QEdge2BTEEdgeHandler = require('./qedge2bteedge');
const NodesUpdateHandler = require('./update_nodes');
const debug = require('debug')('biothings-explorer-trapi:batch_edge_query');
const CacheHandler = require('./cache_handler');
const utils = require('./utils');

module.exports = class BatchEdgeQueryHandler {
constructor(kg, resolveOutputIDs = true) {
Expand Down Expand Up @@ -30,6 +31,7 @@ module.exports = class BatchEdgeQueryHandler {
* @private
*/
_expandBTEEdges(bteEdges) {
debug(`BTE EDGE ${JSON.stringify(this.qEdges)}`)
return bteEdges;
}

Expand All @@ -47,7 +49,23 @@ module.exports = class BatchEdgeQueryHandler {
* @private
*/
async _postQueryFilter(response) {
return response;
try {
const filtered = response.filter(item => {
let edge_predicate = item['$edge_metadata']['predicate']
let predicate_filters = item['$edge_metadata']['trapi_qEdge_obj']['qEdge']['predicate']
//remove prefix from filter list to match predicate name format
predicate_filters = predicate_filters.map(item => utils.removeBioLinkPrefix(item))
//compare edge predicate to filter list
if (predicate_filters.includes(edge_predicate)) {
return item
}
});
debug(`Filtered results from ${response.length} down to ${filtered.length} results`);
return filtered
} catch (error) {
debug(`Failed to filter ${response.length} results due to ${error}`);
return response
}
}

async query(qEdges) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
const kg = this._loadMetaKG(this.smartapiID, this.team);
debug('metakg successfully loaded');
let queryPaths = this._processQueryGraph(this.queryGraph);
debug(`query paths constructed: ${queryPaths}`);
debug(`query paths constructed: ${JSON.stringify(queryPaths)}`);
const handlers = this._createBatchEdgeQueryHandlers(queryPaths, kg);
debug(`Query depth is ${Object.keys(handlers).length}`);
for (let i = 0; i < Object.keys(handlers).length; i++) {
Expand Down
6 changes: 6 additions & 0 deletions src/query_edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = class QEdge {
this.predicate = info.predicates;
this.subject = info.subject;
this.object = info.object;
this.expanded_predicates = []
this.init()
}

init() {
this.expanded_predicates = this.getPredicate()
}

getID() {
Expand Down
2 changes: 2 additions & 0 deletions src/query_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash');
const InvalidQueryGraphError = require('./exceptions/invalid_query_graph_error');
const LogEntry = require('./log_entry');
const MAX_DEPTH = 3;
const debug = require('debug')('biothings-explorer-trapi:query_graph');

module.exports = class QueryGraphHandler {
constructor(queryGraph) {
Expand Down Expand Up @@ -106,6 +107,7 @@ module.exports = class QueryGraphHandler {
`BTE identified your query graph as a ${Object.keys(paths).length}-depth query graph`,
).getLog(),
);
debug(`ALL PATHS ${JSON.stringify(paths)}`)
return paths;
}

Expand Down

0 comments on commit d97cd02

Please sign in to comment.