Skip to content

Commit 3291315

Browse files
authored
Add top-level descriptions response field (#20)
1 parent 865d48c commit 3291315

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

src/nodenorm/handlers/normalized_nodes.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ async def get_normalized_nodes(
201201

202202
async def create_normalized_node(
203203
aggregate_node: NormalizedNode,
204-
include_descriptions: bool = True,
204+
include_descriptions: bool = False,
205205
include_individual_types: bool = False,
206206
conflations: dict = None,
207207
) -> dict:
@@ -254,16 +254,19 @@ async def create_normalized_node(
254254
else:
255255
normal_node = {"id": {"identifier": aggregate_node.canonical_identifier}}
256256

257-
# if descriptions are enabled, look for the first available description and use that
257+
# if descriptions are enabled, collect all available descriptions and use the first as the preferred one
258258
if include_descriptions:
259-
descriptions = list(
260-
map(
261-
lambda x: x[0],
262-
filter(lambda x: len(x) > 0, [eid["d"] for eid in aggregate_node.identifiers if "d" in eid]),
263-
)
259+
descriptions = unique_list(
260+
[
261+
description
262+
for identifier in aggregate_node.identifiers
263+
for description in identifier.get("d", [])
264+
if description
265+
]
264266
)
265267
if len(descriptions) > 0:
266268
normal_node["id"]["description"] = descriptions[0]
269+
normal_node["descriptions"] = descriptions
267270

268271
# now need to reformat the identifier keys. It could be cleaner but we have to worry about if there is a label
269272
normal_node["equivalent_identifiers"] = []

tests/test_normalized_nodes_lookup.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def load_normalized_nodes_module():
4141
normalized_nodes = load_normalized_nodes_module()
4242
_lookup_curie_metadata = normalized_nodes._lookup_curie_metadata
4343
_lookup_equivalent_identifiers = normalized_nodes._lookup_equivalent_identifiers
44+
create_normalized_node = normalized_nodes.create_normalized_node
45+
NormalizedNode = normalized_nodes.NormalizedNode
4446

4547

4648
class FakeAsyncElasticsearch:
@@ -78,6 +80,50 @@ def no_hit_response():
7880
return {"hits": {"total": {"value": 0}, "hits": []}}
7981

8082

83+
@pytest.mark.asyncio
84+
async def test_create_normalized_node_aggregates_descriptions_when_requested():
85+
node = NormalizedNode(
86+
curie="NCIT:C34373",
87+
canonical_identifier="MONDO:0004976",
88+
preferred_label="amyotrophic lateral sclerosis",
89+
information_content=74.9,
90+
identifiers=[
91+
{"i": "MONDO:0004976", "l": "amyotrophic lateral sclerosis", "d": ["first description"]},
92+
{"i": "NCIT:C34373", "l": "Amyotrophic Lateral Sclerosis", "d": ["second description"]},
93+
{"i": "UMLS:C0002736", "l": "Amyotrophic Lateral Sclerosis", "d": ["first description", ""]},
94+
{"i": "MESH:D000690", "l": "Amyotrophic Lateral Sclerosis"},
95+
],
96+
types=["biolink:Disease"],
97+
taxa=[],
98+
)
99+
100+
response = await create_normalized_node(node, include_descriptions=True)
101+
102+
assert response["id"]["description"] == "first description"
103+
assert response["descriptions"] == ["first description", "second description"]
104+
assert response["equivalent_identifiers"][0]["description"] == "first description"
105+
assert response["equivalent_identifiers"][1]["description"] == "second description"
106+
107+
108+
@pytest.mark.asyncio
109+
async def test_create_normalized_node_hides_descriptions_by_default():
110+
node = NormalizedNode(
111+
curie="NCIT:C34373",
112+
canonical_identifier="MONDO:0004976",
113+
preferred_label="amyotrophic lateral sclerosis",
114+
information_content=74.9,
115+
identifiers=[{"i": "MONDO:0004976", "l": "amyotrophic lateral sclerosis", "d": ["first description"]}],
116+
types=["biolink:Disease"],
117+
taxa=[],
118+
)
119+
120+
response = await create_normalized_node(node)
121+
122+
assert "description" not in response["id"]
123+
assert "descriptions" not in response
124+
assert "description" not in response["equivalent_identifiers"][0]
125+
126+
81127
@pytest.mark.asyncio
82128
async def test_lookup_equivalent_identifiers_uses_shared_msearch_index():
83129
namespace = fake_namespace([[hit_response("CHEBI:17310"), no_hit_response()]])

0 commit comments

Comments
 (0)