Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle max_research_phase #192

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];

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
Loading