Skip to content

Commit

Permalink
refactor: use TRAPI types from types package
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Apr 11, 2024
1 parent 55811ee commit 1838d58
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 177 deletions.
10 changes: 5 additions & 5 deletions src/cache_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 MetaKG from '@biothings-explorer/smartapi-kg';
import QEdge from './query_edge';
import { QueryHandlerOptions } from '@biothings-explorer/types';

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 === "true") {
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 === "true") {
if (this.cacheEnabled === false || process.env.INTERNAL_DISABLE_REDIS === 'true') {
if (global.parentPort) {
global.parentPort.postMessage({ threadId, cacheDone: true });
}
Expand Down Expand Up @@ -257,8 +257,8 @@ export default class CacheHandler {
resolve();
});
});
if (process.env.QEDGE_CACHE_TIME_S !== "0") {
await redisClient.client.expireTimeout(redisID, process.env.QEDGE_CACHE_TIME_S || 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);
Expand Down
4 changes: 2 additions & 2 deletions src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Debug from 'debug';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import KGNode from './kg_node';
import KGEdge from './kg_edge';
import { Record } from '../../../api-response-transform/built';
import { TrapiAuxiliaryGraph, TrapiResult } from '../types';
import { Record } from '@biothings-explorer/api-response-transform';
import { TrapiAuxiliaryGraph, TrapiResult } from '@biothings-explorer/types';
import KnowledgeGraph from './knowledge_graph';
const debug = Debug('bte:biothings-explorer-trapi:Graph');

Expand Down
6 changes: 3 additions & 3 deletions src/graph/kg_edge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProvenanceChainItem } from '@biothings-explorer/api-response-transform';
import { TrapiAttribute } from '../types';
import { TrapiSource } from '@biothings-explorer/types';
import { TrapiAttribute } from '@biothings-explorer/types';

export interface KGEdgeInfo {
object: string;
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class KGEdge {
});
}

addSource(source: ProvenanceChainItem | ProvenanceChainItem[]): void {
addSource(source: TrapiSource | TrapiSource[]): void {
if (typeof source === 'undefined') {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/kg_node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TrapiAttribute } from '../types';
import { TrapiAttribute } from '@biothings-explorer/types';

export interface KGNodeInfo {
label: string;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/knowledge_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TrapiKGNodes,
TrapiQualifier,
TrapiSource,
} from '../types';
} from '@biothings-explorer/types';
import KGNode from './kg_node';
import KGEdge from './kg_edge';
import { BTEGraphUpdate } from './graph';
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
TrapiQueryGraph,
TrapiResponse,
TrapiResult,
} from './types';
} from '@biothings-explorer/types';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import BTEGraph from './graph/graph';
import QEdge from './query_edge';
Expand Down
23 changes: 13 additions & 10 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import biolink from '../biolink';
import { getTemplates, MatchedTemplate, TemplateLookup } from './template_lookup';
import { scaled_sigmoid, inverse_scaled_sigmoid } from '../results_assembly/score';
import TRAPIQueryHandler from '../index';
import { QueryHandlerOptions } from '@biothings-explorer/types';
import {
CompactQualifiers,
QueryHandlerOptions,
TrapiAuxGraphCollection,
TrapiEdgeBinding,
TrapiKnowledgeGraph,
Expand All @@ -17,7 +16,8 @@ import {
TrapiQueryGraph,
TrapiResponse,
TrapiResult,
} from '../types';
} from '@biothings-explorer/types';
import { CompactQualifiers } from '../index';
const debug = Debug('bte:biothings-explorer-trapi:inferred-mode');

export interface CombinedResponse {
Expand Down Expand Up @@ -385,9 +385,9 @@ export default class InferredQueryHandler {
if (typeof combinedResponse.message.results[resultID].analyses[0].score !== 'undefined') {
combinedResponse.message.results[resultID].analyses[0].score = resScore
? scaled_sigmoid(
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
inverse_scaled_sigmoid(combinedResponse.message.results[resultID].analyses[0].score) +
inverse_scaled_sigmoid(resScore),
)
: combinedResponse.message.results[resultID].analyses[0].score;
} else {
combinedResponse.message.results[resultID].analyses[0].score = resScore;
Expand Down Expand Up @@ -555,9 +555,11 @@ export default class InferredQueryHandler {
const message = [
`Addition of ${creativeLimitHit} results from Template ${i + 1}`,
Object.keys(combinedResponse.message.results).length === this.CREATIVE_LIMIT ? ' meets ' : ' exceeds ',
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${Object.keys(combinedResponse.message.results).length
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${
Object.keys(combinedResponse.message.results).length
} merged). `,
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${subQueries.length - (i + 1)
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${
subQueries.length - (i + 1)
} `,
subQueries.length - (i + 1) === 1 ? `template.` : `templates.`,
].join('');
Expand All @@ -582,8 +584,9 @@ export default class InferredQueryHandler {
const total =
Object.values(mergedResultsCount).reduce((sum, count) => sum + count, 0) +
Object.keys(mergedResultsCount).length;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
const message = `Merging Summary: (${total}) inferred-template results were merged into (${
Object.keys(mergedResultsCount).length
}) final results, reducing result count by (${total - Object.keys(mergedResultsCount).length})`;
debug(message);
combinedResponse.logs.push(new LogEntry('INFO', null, message).getLog());
}
Expand Down
3 changes: 2 additions & 1 deletion src/inferred_mode/template_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { promises as fs } from 'fs';
import path from 'path';
import async from 'async';
import { TrapiQueryGraph, CompactQualifiers } from '../types';
import { TrapiQueryGraph } from '@biothings-explorer/types';
import { CompactQualifiers } from '../types';

export interface TemplateLookup {
subject: string;
Expand Down
2 changes: 1 addition & 1 deletion src/query_edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Record, RecordNode, FrozenRecord } from '@biothings-explorer/api-respon
import QNode from './query_node';
import { QNodeInfo } from './query_node';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import { TrapiAttributeConstraint, TrapiQualifierConstraint } from './types';
import { TrapiAttributeConstraint, TrapiQualifierConstraint } from '@biothings-explorer/types';

const debug = Debug('bte:biothings-explorer-trapi:QEdge');

Expand Down
2 changes: 1 addition & 1 deletion src/query_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import biolink from './biolink';
import { resolveSRI } from 'biomedical_id_resolver';
import _ from 'lodash';
import * as utils from './utils';
import { TrapiQueryGraph } from './types';
import { TrapiQueryGraph } from '@biothings-explorer/types';

const debug = Debug('bte:biothings-explorer-trapi:query_graph');

Expand Down
2 changes: 1 addition & 1 deletion src/results_assembly/pfocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const debug = Debug('bte:biothings-explorer-trapi:pfocr');
import { intersection } from '../utils';
import _ from 'lodash';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import { TrapiResult } from '../types';
import { TrapiResult } from '@biothings-explorer/types';

// the minimum acceptable intersection size between the CURIEs
// in a TRAPI result and in a PFOCR figure.
Expand Down
2 changes: 1 addition & 1 deletion src/results_assembly/query_results.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import { TrapiResult } from '../types';
import { TrapiResult } from '@biothings-explorer/types';
import Debug from 'debug';
import { zip } from 'lodash';
const debug = Debug('bte:biothings-explorer-trapi:QueryResult');
Expand Down
148 changes: 0 additions & 148 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,153 +1,5 @@
import { TrapiLog } from '@biothings-explorer/utils';

export interface TrapiQNode {
ids?: string[];
categories?: string[];
is_set?: boolean;
constraints?: TrapiAttributeConstraint[];
}

export interface TrapiQEdge {
knowledge_type?: string;
predicates?: string[];
subject: string;
object: string;
attribute_constraints?: TrapiAttributeConstraint[];
qualifier_constraints?: TrapiQualifierConstraint[];
}

export interface TrapiQueryGraph {
nodes: {
[QNodeID: string]: TrapiQNode;
};
edges: {
[QEdgeID: string]: TrapiQEdge;
};
}

export interface TrapiSource {
resource_id: string;
resource_role: string;
upstream_resource_ids?: string[];
}

export interface TrapiKGNodes {
[nodeID: string]: TrapiKGNode;
}

export interface TrapiKGEdges {
[edgeID: string]: TrapiKGEdge;
}

export interface TrapiKnowledgeGraph {
nodes: TrapiKGNodes;
edges: TrapiKGEdges;
}

export interface TrapiKGEdge {
predicate: string;
subject: string;
object: string;
attributes?: TrapiAttribute[];
qualifiers?: TrapiQualifier[];
sources: TrapiSource[];
}

export interface TrapiKGNode {
categories: string[];
name: string;
attributes?: TrapiAttribute[];
}

export interface TrapiAttribute {
attribute_type_id: string;
original_attribute_name?: string;
value: string | string[] | number | number[];
value_type_id?: string;
attribute_source?: string | null;
value_url?: string | null;
attributes?: TrapiAttribute;
[additionalProperties: string]: string | string[] | null | TrapiAttribute | number | number[];
}

export interface TrapiQualifier {
qualifier_type_id: string;
qualifier_value: string | string[];
}

export interface TrapiQualifierConstraint {
qualifier_set: TrapiQualifier[];
}

export interface TrapiAttributeConstraint {
id: string;
name: string;
not: boolean;
operator: string;
value: string | string[] | number | number[];
}

export interface TrapiNodeBinding {
id: string;
query_id?: string;
attributes?: TrapiAttribute[];
}

export interface TrapiEdgeBinding {
id: string;
attributes?: TrapiAttribute[];
}

export interface TrapiAnalysis {
resource_id?: string;
score?: number;
edge_bindings: {
[qEdgeID: string]: TrapiEdgeBinding[];
};
support_graphs?: string[];
scoring_method?: string;
attributes?: TrapiAttribute[];
}

export interface TrapiAuxiliaryGraph {
edges: string[];
attributes?: TrapiAttribute[];
}

export interface TrapiPfocrFigure {
figureUrl: string;
pmc: string;
matchedCuries: string[];
score: number;
}

export interface TrapiResult {
node_bindings: {
[qNodeID: string]: TrapiNodeBinding[];
};
analyses: TrapiAnalysis[];
pfocr?: TrapiPfocrFigure[];
}

export interface TrapiAuxGraphCollection {
[supportGraphID: string]: TrapiAuxiliaryGraph;
}

export interface TrapiResponse {
description?: string;
schema_version?: string;
biolink_version?: string;
workflow?: { id: string }[];
message: {
query_graph: TrapiQueryGraph;
knowledge_graph: TrapiKnowledgeGraph;
auxiliary_graphs?: TrapiAuxGraphCollection;
results: TrapiResult[];
};
logs: TrapiLog[];
}


export interface UnavailableAPITracker {
[server: string]: { skip: boolean; skippedQueries: number };
}
Expand Down
2 changes: 1 addition & 1 deletion src/update_nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Debug from 'debug';
import { ResolverInput, SRIResolverOutput } from 'biomedical_id_resolver';
import { Record } from '@biothings-explorer/api-response-transform';
import QEdge from './query_edge';
import { NodeNormalizerResultObj } from '../../api-response-transform/built';
import { NodeNormalizerResultObj } from '@biothings-explorer/api-response-transform';
const debug = Debug('bte:biothings-explorer-trapi:nodeUpdateHandler');

export interface CuriesByCategory {
Expand Down

0 comments on commit 1838d58

Please sign in to comment.