From 62c6492d9b76d1765c4844dd2ec6cb06d4843f2d Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Thu, 2 May 2024 13:17:38 -0400 Subject: [PATCH 1/2] feat: handle max_research_phase --- src/graph/knowledge_graph.ts | 50 ++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/graph/knowledge_graph.ts b/src/graph/knowledge_graph.ts index 58b621cb..a05239b4 100644 --- a/src/graph/knowledge_graph.ts +++ b/src/graph/knowledge_graph.ts @@ -14,11 +14,43 @@ import KGNode from './kg_node'; import KGEdge from './kg_edge'; import { BTEGraphUpdate } from './graph'; import { APIDefinition } from '@biothings-explorer/types'; +import { Telemetry } from '@biothings-explorer/utils'; const debug = Debug('bte:biothings-explorer-trapi:KnowledgeGraph'); const NON_ARRAY_ATTRIBUTES = ['biolink:knowledge_level', 'biolink:agent_type']; +interface SpecialAttributeHandlers { + [attribute_type_id: string]: (value: Set, kgEdge: KGEdge) => TrapiAttribute['value']; +} + +const SPECIAL_ATTRIBUTE_HANDLERS: SpecialAttributeHandlers = { + 'biolink:max_research_phase': (value, kgEdge) => { + // Special handling for max research phase + const phase_map = { + '-1.0': 'not_provided', + '0.5': 'pre_clinical_research_phase', + '1.0': 'clinical_trial_phase_1', + '2.0': 'clinical_trial_phase_2', + '3.0': 'clinical_trial_phase_3', + '4.0': 'clinical_trial_phase_4', + }; + function map_phase(val: string) { + let new_val = phase_map[val]; + if (typeof new_val !== 'undefined') return new_val; + + const source = Object.values(kgEdge.sources).find((src) => typeof src.primary_knowledge_source !== 'undefined') + .primary_knowledge_source.resource_id; + const err = new Error( + `Unrecognized research phase (${val}) from ${source} ${kgEdge.subject} > ${kgEdge.predicate} > ${kgEdge.object}`, + ); + Telemetry.captureException(err); + return 'not_provided'; + } + return Array.from(value as Set).map(map_phase); + }, +}; + export default class KnowledgeGraph { nodes: { [nodePrimaryID: string]: TrapiKGNode; @@ -117,13 +149,21 @@ export default class KnowledgeGraph { Object.entries(kgEdge.attributes).forEach(([key, value]) => { if (key === 'edge-attributes') return; - // if (key == 'edge-attributes') return; + + let formatted_value: TrapiAttribute['value'] = NON_ARRAY_ATTRIBUTES.includes(key) + ? Array.from(value as Set).reduce((acc, val) => acc + val) + : Array.from(value as Set); + + if (key in SPECIAL_ATTRIBUTE_HANDLERS) { + formatted_value = SPECIAL_ATTRIBUTE_HANDLERS[key](value as Set, kgEdge); + } + attributes.push({ attribute_type_id: key, - value: // technically works for numbers as well - NON_ARRAY_ATTRIBUTES.includes(key) - ? [...(value as Set)].reduce((acc, val) => acc + val) - : Array.from(value as Set), + // technically works for numbers as well + value: NON_ARRAY_ATTRIBUTES.includes(key) + ? [...(value as Set)].reduce((acc, val) => acc + val) + : Array.from(value as Set), //value_type_id: 'bts:' + key, }); }); From 1f891ccea1f6e812ce797796991d10a94d029681 Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Thu, 2 May 2024 13:29:26 -0400 Subject: [PATCH 2/2] fix: use formatted_value --- src/graph/knowledge_graph.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/graph/knowledge_graph.ts b/src/graph/knowledge_graph.ts index a05239b4..e4bfc9e1 100644 --- a/src/graph/knowledge_graph.ts +++ b/src/graph/knowledge_graph.ts @@ -161,9 +161,7 @@ export default class KnowledgeGraph { attributes.push({ attribute_type_id: key, // technically works for numbers as well - value: NON_ARRAY_ATTRIBUTES.includes(key) - ? [...(value as Set)].reduce((acc, val) => acc + val) - : Array.from(value as Set), + value: formatted_value, //value_type_id: 'bts:' + key, }); });