Skip to content

Commit

Permalink
Merge pull request #220 from biothings/attribute-array
Browse files Browse the repository at this point in the history
store edge attributes as arrays, convert to set later if needed
  • Loading branch information
colleenXu authored Oct 20, 2024
2 parents 3a3c6de + 3ee617f commit 71432a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/graph/kg_edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class KGEdge {
[qualifier_type_id: string]: string | string[];
};
attributes: {
[attribute_type_id: string]: Set<string> | TrapiAttribute[];
[attribute_type_id: string]: string[] | TrapiAttribute[];
'edge-attributes'?: TrapiAttribute[];
};
constructor(id: string, info: KGEdgeInfo) {
Expand Down Expand Up @@ -126,13 +126,11 @@ export default class KGEdge {
}

if (!(name in this.attributes)) {
this.attributes[name] = new Set();
this.attributes[name] = [];
}
if (!Array.isArray(value)) {
value = [value];
}
(value as string[]).map((item) => {
(this.attributes[name] as Set<string>).add(item);
});
(this.attributes[name] as string[]).push(...(value as string[]));
}
}
18 changes: 13 additions & 5 deletions src/graph/knowledge_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { toArray, 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'];
const NON_ARRAY_ATTRIBUTES = ['biolink:knowledge_level', 'biolink:agent_type'];
const SUM_ATTRIBUTES = ['biolink:evidence_count'];

interface SpecialAttributeHandlers {
[attribute_type_id: string]: (value: Set<string | number>, kgEdge: KGEdge) => TrapiAttribute['value'];
Expand Down Expand Up @@ -149,12 +150,19 @@ export default class KnowledgeGraph {
Object.entries(kgEdge.attributes).forEach(([key, value]) => {
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>);
let formatted_value: TrapiAttribute['value'];
if (SUM_ATTRIBUTES.includes(key)) {
// for sums we don't want to remove duplicates
formatted_value = (value as string[]).reduce((acc, val) => acc + val);
} else if (NON_ARRAY_ATTRIBUTES.includes(key)) {
// for non array attributes we want to remove duplicates (ie. same string for knowledge_level multiple times)
formatted_value = Array.from(new Set(value as string[])).reduce((acc, val) => acc + val);
} else {
formatted_value = Array.from(new Set(value as string[]));
}

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

attributes.push({
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default class TRAPIQueryHandler {
]);
this.bteGraph.edges[boundEdgeID] = boundEdge;
} else {
(this.bteGraph.edges[boundEdgeID].attributes['biolink:support_graphs'] as Set<string>).add(supportGraphID);
this.bteGraph.edges[boundEdgeID].addAdditionalAttributes('biolink:support_graphs', supportGraphID);
}
if (!edgesToRebind[edgeID]) edgesToRebind[edgeID] = {};
if (!edgesToRebind[edgeID][subject]) edgesToRebind[edgeID][subject] = {};
Expand Down

0 comments on commit 71432a2

Please sign in to comment.