Skip to content

Commit 71432a2

Browse files
authored
Merge pull request #220 from biothings/attribute-array
store edge attributes as arrays, convert to set later if needed
2 parents 3a3c6de + 3ee617f commit 71432a2

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/graph/kg_edge.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class KGEdge {
2929
[qualifier_type_id: string]: string | string[];
3030
};
3131
attributes: {
32-
[attribute_type_id: string]: Set<string> | TrapiAttribute[];
32+
[attribute_type_id: string]: string[] | TrapiAttribute[];
3333
'edge-attributes'?: TrapiAttribute[];
3434
};
3535
constructor(id: string, info: KGEdgeInfo) {
@@ -126,13 +126,11 @@ export default class KGEdge {
126126
}
127127

128128
if (!(name in this.attributes)) {
129-
this.attributes[name] = new Set();
129+
this.attributes[name] = [];
130130
}
131131
if (!Array.isArray(value)) {
132132
value = [value];
133133
}
134-
(value as string[]).map((item) => {
135-
(this.attributes[name] as Set<string>).add(item);
136-
});
134+
(this.attributes[name] as string[]).push(...(value as string[]));
137135
}
138136
}

src/graph/knowledge_graph.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { toArray, Telemetry } from '@biothings-explorer/utils';
1717

1818
const debug = Debug('bte:biothings-explorer-trapi:KnowledgeGraph');
1919

20-
const NON_ARRAY_ATTRIBUTES = ['biolink:knowledge_level', 'biolink:agent_type', 'biolink:evidence_count'];
20+
const NON_ARRAY_ATTRIBUTES = ['biolink:knowledge_level', 'biolink:agent_type'];
21+
const SUM_ATTRIBUTES = ['biolink:evidence_count'];
2122

2223
interface SpecialAttributeHandlers {
2324
[attribute_type_id: string]: (value: Set<string | number>, kgEdge: KGEdge) => TrapiAttribute['value'];
@@ -149,12 +150,19 @@ export default class KnowledgeGraph {
149150
Object.entries(kgEdge.attributes).forEach(([key, value]) => {
150151
if (key === 'edge-attributes') return;
151152

152-
let formatted_value: TrapiAttribute['value'] = NON_ARRAY_ATTRIBUTES.includes(key)
153-
? Array.from(value as Set<string>).reduce((acc, val) => acc + val)
154-
: Array.from(value as Set<string>);
153+
let formatted_value: TrapiAttribute['value'];
154+
if (SUM_ATTRIBUTES.includes(key)) {
155+
// for sums we don't want to remove duplicates
156+
formatted_value = (value as string[]).reduce((acc, val) => acc + val);
157+
} else if (NON_ARRAY_ATTRIBUTES.includes(key)) {
158+
// for non array attributes we want to remove duplicates (ie. same string for knowledge_level multiple times)
159+
formatted_value = Array.from(new Set(value as string[])).reduce((acc, val) => acc + val);
160+
} else {
161+
formatted_value = Array.from(new Set(value as string[]));
162+
}
155163

156164
if (key in SPECIAL_ATTRIBUTE_HANDLERS) {
157-
formatted_value = SPECIAL_ATTRIBUTE_HANDLERS[key](value as Set<string | number>, kgEdge);
165+
formatted_value = SPECIAL_ATTRIBUTE_HANDLERS[key](new Set(value as string[]), kgEdge);
158166
}
159167

160168
attributes.push({

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export default class TRAPIQueryHandler {
278278
]);
279279
this.bteGraph.edges[boundEdgeID] = boundEdge;
280280
} else {
281-
(this.bteGraph.edges[boundEdgeID].attributes['biolink:support_graphs'] as Set<string>).add(supportGraphID);
281+
this.bteGraph.edges[boundEdgeID].addAdditionalAttributes('biolink:support_graphs', supportGraphID);
282282
}
283283
if (!edgesToRebind[edgeID]) edgesToRebind[edgeID] = {};
284284
if (!edgesToRebind[edgeID][subject]) edgesToRebind[edgeID][subject] = {};

0 commit comments

Comments
 (0)