From ad3ce727926f1f5b7f1d6bddebe6b5390071f43d Mon Sep 17 00:00:00 2001 From: Kristoffer Kiil Date: Thu, 9 Jan 2025 13:52:11 +0100 Subject: [PATCH] Update aggregation.py Resistance genes are put into a list, only unique elements are retained, and eventually the elements are concatenated to a string --- bifrost/bifrost_listener/aggregation.py | 32 +++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/bifrost/bifrost_listener/aggregation.py b/bifrost/bifrost_listener/aggregation.py index 0c0627dd..b4d839d2 100644 --- a/bifrost/bifrost_listener/aggregation.py +++ b/bifrost/bifrost_listener/aggregation.py @@ -87,20 +87,32 @@ def agg_pipeline(changed_ids=None): "resistance_genes": { "$reduce": { "input": { - "$map": { - "input": {"$objectToArray": "$categories.resistance.report.phenotypes"}, - "as": "phenotype", - "in": { - "$reduce": { - "input": {"$objectToArray": "$$phenotype.v.genes"}, - "initialValue": "", - "in": {"$concat": ["$$value", {"$cond": [{"$eq": ["$$value", ""]}, "", ", "]}, "$$this.k"]} + "$reduce": { + "input": { + "$map": { + "input": {"$objectToArray": "$categories.resistance.report.phenotypes"}, + "as": "phenotype", + "in": { + "$reduce": { + "input": {"$objectToArray": "$$phenotype.v.genes"}, + "initialValue": [], + "in": {"$concatArrays": ["$$value", ["$$this.k"]]} + } + } } - } + }, + "initialValue": [], + "in": {"$setUnion": ["$$value", "$$this"]} } }, "initialValue": "", - "in": {"$concat": ["$$value", {"$cond": [{"$eq": ["$$value", ""]}, "", ", "]}, "$$this"]} + "in": { + "$cond": { + "if": {"$eq": ["$$value",""]}, + "then": "$$this", + "else": {"$concat":["$$value", ", ", "$$this"]} + } + } } }, "amr_profile": "$categories.resistance.summary",