Skip to content

Commit

Permalink
Merge pull request #111 from geneontology/exception_handler
Browse files Browse the repository at this point in the history
exception handler for MGI:MGI: - the saga continues
  • Loading branch information
sierra-moxon authored Nov 26, 2024
2 parents 7952def + e4fd771 commit 6de8bc4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/routers/ribbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,32 @@ async def get_ribbon_results(
fields = "bioentity,bioentity_label,taxon,taxon_label"
data = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.BIOENTITY, q, qf, fields, fq, False)

# Create a new list to store updated entities
updated_subjects = []

for entity in subjects:
# Add details from the `data` list
for entity_detail in data:
subject_id = entity_detail["bioentity"]
if entity["id"] == subject_id:
entity["label"] = entity_detail["bioentity_label"]
entity["taxon_id"] = entity_detail["taxon"]
entity["taxon_label"] = entity_detail["taxon_label"]
if entity.get("id").startswith("MGI:MGI:"):
entity_new = entity
subjects.remove(entity)
old_id = entity_new.get("id")

# Check for and fix "MGI:MGI:" prefix in `id`
if entity.get("id", "").startswith("MGI:MGI:"):
# Create a new dictionary to avoid modifying the original entity
entity_new = entity.copy()
old_id = entity_new["id"]
new_id = old_id.replace("MGI:MGI:", "MGI:")
entity_new["id"] = new_id
subjects.append(entity_new)
updated_subjects.append(entity_new)
else:
# Add the entity as-is if no changes are needed
updated_subjects.append(entity)

# Replace the original list with the updated list
subjects = updated_subjects

# map the entity back to their original IDs
for entity in subjects:
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_pathway_widget_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def test_get_gocams_by_geneproduct_id(self):
self.assertGreater(len(response.json()), 0)
self.assertEqual(response.status_code, 200)

def test_get_gocams_by_invalid_geneproduct_id(self):
"""
Test getting Gene Ontology models associated with a gene product by its ID.
:param id: The identifier of the gene product. (parametrized)
"""
for gene_id in gene_not_found_ids:
response = test_client.get(f"/api/gp/{gene_id}/models")
self.assertEqual(response.status_code, 404)

def test_get_gocams_by_geneproduct_not_found(self):
for gene_id in gene_not_found_ids:
response = test_client.get(f"/api/gp/{gene_id}/models")
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_ribbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
test_client = TestClient(app)

gene_ids = ["ZFIN:ZDB-GENE-980526-388", "ZFIN:ZDB-GENE-990415-8", "ZFIN:ZDB-GENE-990415-72"]
ortho_gene_ids = ["WB:WBGene00002147", "HGNC:3449", "HGNC:16942",
"MGI:1930134", "MGI:1349436", "RGD:1559716",
"RGD:1308743", "Xenbase:XB-GENE-4594134", "ZFIN:ZDB-GENE-050522-431",
"ZFIN:ZDB-GENE-090312-15", "FB:FBgn0261984", "SGD:S000001121"]
go_ids = ["GO:0008150"]
subsets = ["goslim_agr"]
shared_ancestors = [("GO:0006259", "GO:0046483")]
Expand Down Expand Up @@ -114,6 +118,9 @@ def test_mgi_ribbon(self):
"""Test MGI ribbon annotation returns."""
data = {"subset": "goslim_agr", "subject": ["MGI:1917258"]}
response = test_client.get("/api/ontology/ribbon/", params=data)
for subject in response.json().get("subjects"):
print(subject.get("id"))
self.assertFalse(subject.get("id").startswith("MGI:MGI:"))
self.assertTrue(len(response.json().get("subjects")) > 0)
for subject in response.json().get("subjects"):
self.assertTrue(subject.get("label") == "Ace2")
Expand Down Expand Up @@ -184,6 +191,16 @@ def test_term_by_subset_endpoint(self):
response = test_client.get(f"/api/ontology/subset/{id}")
self.assertEqual(response.status_code, 200)

def test_ribbon_with_many_subjects(self):
"""Test the ontology ribbon with many subjects."""
data = {"subset": "goslim_agr", "subject": ortho_gene_ids,
"exclude_PB":True, "exclude_IBA":False, "cross_aspect":False}
response = test_client.get("/api/ontology/ribbon/", params=data)
data = response.json()
for subject in data.get("subjects"):
print(subject.get("id"))
self.assertFalse(subject.get("id").startswith("MGI:MGI:"))
self.assertEqual(response.status_code, 200)

if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions tests/unit/test_slimmer_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def test_slimmer_endpoint_mgimgi(self):
"slim": ["GO:0005575"],
}
response = test_client.get(endpoint, params=data)
for subject in response.json().get("subjects"):
print(subject)
self.assertEqual(response.status_code, 200)
self.assertGreater(len(response.json()), 0)

Expand Down

0 comments on commit 6de8bc4

Please sign in to comment.