Skip to content

Commit

Permalink
Merge branch 'max-research-phase' of https://github.com/biothings/bte…
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed May 3, 2024
2 parents b7c41f1 + 1f891cc commit b850df1
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/graph/knowledge_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'biolink:evidence_count'];

interface SpecialAttributeHandlers {
[attribute_type_id: string]: (value: Set<string | number>, 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<string>).map(map_phase);
},
};

export default class KnowledgeGraph {
nodes: {
[nodePrimaryID: string]: TrapiKGNode;
Expand Down Expand Up @@ -117,13 +149,19 @@ 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<string>).reduce((acc, val) => acc + val)
: Array.from(value as Set<string>);

if (key in SPECIAL_ATTRIBUTE_HANDLERS) {
formatted_value = SPECIAL_ATTRIBUTE_HANDLERS[key](value as Set<string | number>, kgEdge);
}

attributes.push({
attribute_type_id: key,
value: // technically works for numbers as well
NON_ARRAY_ATTRIBUTES.includes(key)
? [...(value as Set<string>)].reduce((acc, val) => acc + val)
: Array.from(value as Set<string>),
// technically works for numbers as well
value: formatted_value,
//value_type_id: 'bts:' + key,
});
});
Expand Down

0 comments on commit b850df1

Please sign in to comment.