Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…raph_handler into kg-refactor
  • Loading branch information
rjawesome committed Apr 22, 2024
2 parents fa889e4 + dc48864 commit b0b14d0
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 708 deletions.
19 changes: 0 additions & 19 deletions __test__/integration/BatchEdgeQueryHandler.test.ts

This file was deleted.

9 changes: 5 additions & 4 deletions __test__/integration/TRAPIQueryHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios';
import axios, { AxiosStatic } from 'axios';
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<AxiosStatic>;

import TRAPIQueryHandler from '../../src/index';
import path from 'path';
Expand All @@ -26,7 +27,7 @@ describe('Testing TRAPIQueryHandler Module', () => {
};
describe('Testing query function', () => {
test('test with one query edge', async () => {
(axios.get as jest.Mock).mockResolvedValue({
(mockedAxios.get as jest.Mock).mockResolvedValue({
data: {
message: {
query_graph: {
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('Testing TRAPIQueryHandler Module', () => {
workflow: [{ id: 'lookup' }],
},
});
(axios.post as jest.Mock).mockResolvedValue({
(mockedAxios.post as jest.Mock).mockResolvedValue({
data: {
'MONDO:0005737': {
id: { identifier: 'MONDO:0005737', label: 'Ebola hemorrhagic fever' },
Expand Down Expand Up @@ -139,6 +140,6 @@ describe('Testing TRAPIQueryHandler Module', () => {
queryHandler.setQueryGraph(OneHopQuery);
await queryHandler.query();
expect(queryHandler.knowledgeGraph.kg).toHaveProperty('nodes');
}, 15000);
}, 30000);
});
});
57 changes: 0 additions & 57 deletions __test__/unittest/redisClient.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion data/templates/Drug-treats-Disease/Chem-treats-DoP.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"eA": {
"subject": "creativeQuerySubject",
"object": "creativeQueryObject",
"predicates": ["biolink:treats"]
"predicates": ["biolink:treats_or_applied_or_studied_to_treat"]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eA": {
"subject": "creativeQuerySubject",
"object": "nA",
"predicates": ["biolink:treats"]
"predicates": ["biolink:treats_or_applied_or_studied_to_treat"]
},
"eB": {
"subject": "creativeQueryObject",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@
"@biothings-explorer/node-expansion": "workspace:../node-expansion",
"@biothings-explorer/smartapi-kg": "workspace:../smartapi-kg",
"@biothings-explorer/utils": "workspace:../utils",
"@sentry/node": "^7.74.1",
"async": "^3.2.4",
"@biothings-explorer/types": "workspace:../types",
"biolink-model": "workspace:../biolink-model",
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
"@sentry/node": "^7.74.1",
"async": "^3.2.4",
"chi-square-p-value": "^1.0.5",
"debug": "^4.3.4",
"ioredis": "^5.3.2",
Expand Down
42 changes: 7 additions & 35 deletions src/batch_edge_query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import call_api, { RedisClient } from '@biothings-explorer/call-apis';
import call_api from '@biothings-explorer/call-apis';
import { redisClient } from '@biothings-explorer/utils';
import QEdge2APIEdgeHandler, { APIEdge } from './qedge2apiedge';
import NodesUpdateHandler from './update_nodes';
import Debug from 'debug';
Expand All @@ -7,26 +8,25 @@ import CacheHandler from './cache_handler';
import { threadId } from 'worker_threads';
import MetaKG from '@biothings-explorer/smartapi-kg';
import { StampedLog } from '@biothings-explorer/utils';
import { QueryHandlerOptions, redisClient } from '.';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import QEdge from './query_edge';
import { UnavailableAPITracker } from './types';
import { Record } from '@biothings-explorer/api-response-transform';

export interface BatchEdgeQueryOptions extends QueryHandlerOptions {
recordHashEdgeAttributes: string[];
caching: boolean;
}

export default class BatchEdgeQueryHandler {
metaKG: MetaKG;
subscribers: any[];
logs: StampedLog[];
caching: boolean;
options: QueryHandlerOptions;
resolveOutputIDs: boolean;
qEdges: QEdge | QEdge[];
constructor(metaKG: MetaKG, resolveOutputIDs = true, options?: BatchEdgeQueryOptions) {
this.metaKG = metaKG;
this.subscribers = [];
this.logs = [];
this.caching = options && options.caching;
this.options = options;
Expand Down Expand Up @@ -62,7 +62,7 @@ export default class BatchEdgeQueryHandler {
* @private
*/
async _queryAPIEdges(APIEdges: APIEdge[], unavailableAPIs: UnavailableAPITracker = {}): Promise<Record[]> {
const executor = new call_api(APIEdges, this.options, redisClient as RedisClient);
const executor = new call_api(APIEdges, this.options, redisClient);
const records: Record[] = await executor.query(this.resolveOutputIDs, unavailableAPIs);
this.logs = [...this.logs, ...executor.logs];
return records;
Expand Down Expand Up @@ -100,7 +100,7 @@ export default class BatchEdgeQueryHandler {
const equivalentAlreadyIncluded = qEdge
.getInputNode()
.getEquivalentIDs()
[curie].equivalentIDs.some((equivalentCurie) => reducedCuries.includes(equivalentCurie));
[curie].equivalentIDs.some((equivalentCurie) => reducedCuries.includes(equivalentCurie));
if (!equivalentAlreadyIncluded) {
reducedCuries.push(curie);
} else {
Expand All @@ -112,8 +112,7 @@ export default class BatchEdgeQueryHandler {
strippedCuries.push(...nodeStrippedCuries);
if (nodeStrippedCuries.length > 0) {
debug(
`stripped (${nodeStrippedCuries.length}) duplicate equivalent curies from ${
node.id
`stripped (${nodeStrippedCuries.length}) duplicate equivalent curies from ${node.id
}: ${nodeStrippedCuries.join(',')}`,
);
}
Expand Down Expand Up @@ -173,31 +172,4 @@ export default class BatchEdgeQueryHandler {
debug('Update nodes completed!');
return queryRecords;
}

/**
* Register subscribers
* @param {object} subscriber
*/
subscribe(subscriber): void {
this.subscribers.push(subscriber);
}

/**
* Unsubscribe a listener
* @param {object} subscriber
*/
unsubscribe(subscriber): void {
this.subscribers = this.subscribers.filter((fn) => {
if (fn != subscriber) return fn;
});
}

/**
* Nofity all listeners
*/
notify(res): void {
this.subscribers.map((subscriber) => {
subscriber.update(res);
});
}
}
14 changes: 8 additions & 6 deletions src/cache_handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { redisClient } from './redis-client';
import { redisClient } from '@biothings-explorer/utils';
import Debug from 'debug';
const debug = Debug('bte:biothings-explorer-trapi:cache_handler');
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
Expand All @@ -9,9 +9,9 @@ import chunker from 'stream-chunker';
import { Readable, Transform } from 'stream';
import { Record, RecordPackage } from '@biothings-explorer/api-response-transform';
import { threadId } from 'worker_threads';
import MetaKG from '../../smartapi-kg/built';
import { QueryHandlerOptions } from '.';
import MetaKG from '@biothings-explorer/smartapi-kg';
import QEdge from './query_edge';
import { QueryHandlerOptions } from '@biothings-explorer/types';

export interface RecordPacksByQedgeMetaKGHash {
[QEdgeHash: string]: RecordPackage;
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class CacheHandler {
}

async categorizeEdges(qEdges: QEdge[]): Promise<{ cachedRecords: Record[]; nonCachedQEdges: QEdge[] }> {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS) {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS === 'true') {
return {
cachedRecords: [],
nonCachedQEdges: qEdges,
Expand Down Expand Up @@ -210,7 +210,7 @@ export default class CacheHandler {
}

async cacheEdges(queryRecords: Record[]): Promise<void> {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS) {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS === 'true') {
if (global.parentPort) {
global.parentPort.postMessage({ threadId, cacheDone: true });
}
Expand Down Expand Up @@ -257,7 +257,9 @@ export default class CacheHandler {
resolve();
});
});
await redisClient.client.expireTimeout(redisID, process.env.REDIS_KEY_EXPIRE_TIME || 1800);
if (process.env.QEDGE_CACHE_TIME_S !== '0') {
await redisClient.client.expireTimeout(redisID, process.env.QEDGE_CACHE_TIME_S || 1800);
}
} catch (error) {
failedHashes.push(hash);
debug(
Expand Down
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const EDGE_ATTRIBUTES_USED_IN_RECORD_HASH = [
//"NCIT:C61594",
// Multiomics BigGIM Drug-Response 2023-05-31: may later change?
'biolink:context_qualifier',
'biolink:primary_knowledge_source', // added 2024-01-17
// commenting it out since I haven't tested if it works for this KP or
// if it'll cause bugs when processing other KPs.
// It is only needed to differentiate records from some operations
Expand Down
Loading

0 comments on commit b0b14d0

Please sign in to comment.