Skip to content

Commit

Permalink
feat: use service provider as source where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Aug 10, 2023
1 parent 32eb684 commit 31cb619
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
9 changes: 4 additions & 5 deletions src/batch_edge_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ module.exports = class BatchEdgeQueryHandler {
this.subscribers = [];
this.logs = [];
this.caching = options && options.caching;
this.recordConfig = {};
this.options = options;
if (options && options.recordHashEdgeAttributes) {
this.recordConfig.EDGE_ATTRIBUTES_USED_IN_RECORD_HASH = options.recordHashEdgeAttributes;
this.options.EDGE_ATTRIBUTES_USED_IN_RECORD_HASH = options.recordHashEdgeAttributes;
}
if (options && options.submitter) this.recordConfig.submitter = options.submitter;
this.resolveOutputIDs = resolveOutputIDs;
}

Expand Down Expand Up @@ -45,7 +44,7 @@ module.exports = class BatchEdgeQueryHandler {
* @private
*/
async _queryAPIEdges(APIEdges, unavailableAPIs = {}) {
let executor = new call_api(APIEdges, this.recordConfig);
let executor = new call_api(APIEdges, this.options);
const records = await executor.query(this.resolveOutputIDs, unavailableAPIs);
this.logs = [...this.logs, ...executor.logs];
return records;
Expand Down Expand Up @@ -117,7 +116,7 @@ module.exports = class BatchEdgeQueryHandler {
await this._rmEquivalentDuplicates(qEdges);
debug('Node Update Success');

const cacheHandler = new CacheHandler(this.caching, this.metaKG, this.recordConfig);
const cacheHandler = new CacheHandler(this.caching, this.metaKG, this.options);
const { cachedRecords, nonCachedQEdges } = await cacheHandler.categorizeEdges(qEdges);
this.logs = [...this.logs, ...cacheHandler.logs];
let queryRecords;
Expand Down
1 change: 1 addition & 0 deletions src/edge_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ module.exports = class QueryEdgeManager {
caching: this.options.caching,
submitter: this.options.submitter,
recordHashEdgeAttributes: config.EDGE_ATTRIBUTES_USED_IN_RECORD_HASH,
provenanceUsesServiceProvider: this.options.provenanceUsesServiceProvider,
});
handler.setEdges(qEdge);
return handler;
Expand Down
62 changes: 38 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
constructor(options = {}, smartAPIPath = undefined, predicatesPath = undefined, includeReasoner = true) {
this.logs = [];
this.options = options;
this.options.provenanceUsesServiceProvider = this.options.smartAPIID || this.options.teamName ? true : false;
this.includeReasoner = includeReasoner;
this.resolveOutputIDs =
typeof this.options.enableIDResolution === 'undefined' ? true : this.options.enableIDResolution;
Expand Down Expand Up @@ -145,7 +146,12 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
})[1];
subclassEdge.addSource([
{ resource_id: source, resource_role: 'primary_knowledge_source' },
{ resource_id: 'infores:biothings-explorer', resource_role: 'aggregator_knowledge_source' },
{
resource_id: this.options.provenanceUsesServiceProvider
? 'infores:service-provider-trapi'
: 'infores:biothings-explorer',
resource_role: 'aggregator_knowledge_source',
},
]);
this.bteGraph.edges[subclassEdgeID] = subclassEdge;
nodesToRebind[subject] = { newNode: object, subclassEdgeID };
Expand Down Expand Up @@ -186,7 +192,14 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
object: object,
});
boundEdge.addAdditionalAttributes('biolink:support_graphs', [supportGraphID]);
boundEdge.addSource([{ resource_id: 'infores:biothings-explorer', resource_role: 'primary_knowledge_source' }]);
boundEdge.addSource([
{
resource_id: this.options.provenanceUsesServiceProvider
? 'infores:service-provider-trapi'
: 'infores:biothings-explorer',
resource_role: 'primary_knowledge_source',
},
]);
this.bteGraph.edges[boundEdgeID] = boundEdge;
} else {
this.bteGraph.edges[boundEdgeID].attributes['biolink:support_graphs'].add(supportGraphID);
Expand All @@ -206,7 +219,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
if (!boundIDs.has(binding.id)) {
newBindings.push(binding);
boundIDs.add(binding.id);
};
}
} else if (!boundIDs.has(nodesToRebind[binding.id].newNode)) {
newBindings.push({ id: nodesToRebind[binding.id].newNode });
boundIDs.add(nodesToRebind[binding.id].newNode);
Expand Down Expand Up @@ -245,9 +258,11 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
});

// Prune unused auxGraphs
auxGraphs = Object.fromEntries(Object.entries(auxGraphs).filter(([auxGraphID]) => {
return [...edgesIDsByAuxGraphID[auxGraphID]].some((edgeID => resultBoundEdgesWithAuxGraphs.has(edgeID)))
}))
auxGraphs = Object.fromEntries(
Object.entries(auxGraphs).filter(([auxGraphID]) => {
return [...edgesIDsByAuxGraphID[auxGraphID]].some((edgeID) => resultBoundEdgesWithAuxGraphs.has(edgeID));
}),
);

this.auxGraphs = auxGraphs;
this.finalizedResults = fixedResults;
Expand All @@ -267,20 +282,19 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
const resolvedCuries = await id_resolver.resolveSRI({ unknown: curiesToResolve });
Object.entries(resolvedCuries).forEach(([originalCurie, resolvedEntity]) => {
if (!this.bteGraph.nodes[resolvedEntity.primaryID]) {
const category = resolvedEntity.primaryTypes?.[0] ? `biolink:${resolvedEntity.primaryTypes[0]}` : qNodeIDsByOriginalID.get(originalCurie).categories?.[0];

this.bteGraph.nodes[resolvedEntity.primaryID] = new KGNode(
resolvedEntity.primaryID,
{
primaryCurie: resolvedEntity.primaryID,
qNodeID: qNodeIDsByOriginalID[originalCurie],
equivalentCuries: resolvedEntity.equivalentIDs,
names: resolvedEntity.labelAliases,
category: category ? [category] : ["biolink:NamedThing"],
attributes: resolvedEntity.attributes,
label: resolvedEntity.label,
},
);
const category = resolvedEntity.primaryTypes?.[0]
? `biolink:${resolvedEntity.primaryTypes[0]}`
: qNodeIDsByOriginalID.get(originalCurie).categories?.[0];

this.bteGraph.nodes[resolvedEntity.primaryID] = new KGNode(resolvedEntity.primaryID, {
primaryCurie: resolvedEntity.primaryID,
qNodeID: qNodeIDsByOriginalID[originalCurie],
equivalentCuries: resolvedEntity.equivalentIDs,
names: resolvedEntity.labelAliases,
category: category ? [category] : ['biolink:NamedThing'],
attributes: resolvedEntity.attributes,
label: resolvedEntity.label,
});
}
});
}
Expand Down Expand Up @@ -349,7 +363,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {

_initializeResponse() {
this.knowledgeGraph = new KnowledgeGraph(this.options?.apiList?.include);
this.trapiResultsAssembler = new TrapiResultsAssembler();
this.trapiResultsAssembler = new TrapiResultsAssembler(this.options);
this.bteGraph = new Graph();
this.bteGraph.subscribe(this.knowledgeGraph);
}
Expand Down Expand Up @@ -589,7 +603,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
await this.addQueryNodes();

const span1 = Sentry?.getCurrentHub()?.getScope()?.getTransaction()?.startChild({
description: "loadMetaKG"
description: 'loadMetaKG',
});

debug('Start to load metakg.');
Expand Down Expand Up @@ -637,7 +651,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {

if (this._queryUsesInferredMode()) {
const span2 = Sentry?.getCurrentHub()?.getScope()?.getTransaction()?.startChild({
description: "creativeExecution"
description: 'creativeExecution',
});
await this._handleInferredEdges();
span2?.finish();
Expand All @@ -656,7 +670,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
}

const span3 = Sentry?.getCurrentHub()?.getScope()?.getTransaction()?.startChild({
description: "resultsAssembly"
description: 'resultsAssembly',
});

// update query graph
Expand Down
2 changes: 1 addition & 1 deletion src/inferred_mode/inferred_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ module.exports = class InferredQueryHandler {
subject: resultCreativeSubjectID,
object: resultCreativeObjectID,
predicate: qEdge.predicates[0],
sources: [{ resource_id: 'infores:biothings-explorer', resource_role: 'primary_knowledge_source' }],
sources: [{ resource_id: this.parent.options.provenanceUsesServiceProvider ? 'infores:service-provider-trapi' : 'infores:biothings-explorer', resource_role: 'primary_knowledge_source' }],
attributes: [{ attribute_type_id: 'biolink:support_graphs', value: [] }],
};
}
Expand Down
19 changes: 16 additions & 3 deletions src/results_assembly/query_results.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ module.exports = class TrapiResultsAssembler {
/**
* Create a QueryResult i9nstance.
*/
constructor() {
constructor(options) {
/**
* @property {Result[]} _results - list of query results
* @private
*/
this._results = [];
this.logs = [];
this.options = options;
}

getResults() {
Expand Down Expand Up @@ -374,7 +375,17 @@ module.exports = class TrapiResultsAssembler {
recordHashes: new Set(),
};
solutionRecords.forEach(
({ inputQNodeID, outputQNodeID, inputPrimaryCurie, outputPrimaryCurie, inputUMLS, outputUMLS, isTextMined, qEdgeID, recordHash }) => {
({
inputQNodeID,
outputQNodeID,
inputPrimaryCurie,
outputPrimaryCurie,
inputUMLS,
outputUMLS,
isTextMined,
qEdgeID,
recordHash,
}) => {
consolidatedSolutionRecord.inputPrimaryCuries.add(inputPrimaryCurie);
consolidatedSolutionRecord.outputPrimaryCuries.add(outputPrimaryCurie);
consolidatedSolutionRecord.inputUMLS.add(...inputUMLS);
Expand Down Expand Up @@ -404,7 +415,9 @@ module.exports = class TrapiResultsAssembler {
node_bindings: {},
analyses: [
{
resource_id: `infores:biothings-explorer`,
resource_id: this.options.provenanceUsesServiceProvider
? `infores:service-provider-trapi`
: `infores:biothings-explorer`,
edge_bindings: {},
score: score,
},
Expand Down

0 comments on commit 31cb619

Please sign in to comment.