diff options
| author | Pjotr <wrk@bosa.lan> | 2026-10-07 10:42:51 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-07 10:42:51 +0000 |
| commit | e912c87deaf220fd5fce1299d669e8fa992b5338 (patch) | |
| tree | 5a065b3178770291c8f9fda5c88e35d31d47243f /test/python | |
| parent | 89c881605a5c94be75f27c2c82cb04cf8bb3b9a8 (diff) | |
| download | genenetwork4-e912c87deaf220fd5fce1299d669e8fa992b5338.tar.gz | |
* src/routes/search.rs (search): add lod, locus, and sort/order.
Search hits gain `lod` (lrs / 4.61) and `locus` (the peak marker name, looked up in PublishXRef in one batched query; null when the database is unavailable). `sort=lrs|lod` with `order=asc|desc` ranks hits: gn3 is asked for its first 1000 matches, those are ranked, then `limit` applies. * src/db.rs (LRS_PER_LOD, lod, get_publish_loci): add the shared LRS-to-LOD constant and the locus lookup; TraitInfo and ProbeSetInfo gain `lod`, computed in SQL from the same constant. * tests/search.rs, test/python/test_search.py: add the "highest LOD, locus marker and additive effect for serotonin in BXD male midbrain" scenario over a recorded production fixture, plus sort/order tests. * tests/group_trait_info.rs, tests/dataset_probeset_info.rs and their Python mirrors: assert lod (and locus for trait 17985). * README.md (Endpoints): document sort, order, lod and locus. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/test_dataset_probeset_info.py | 1 | ||||
| -rw-r--r-- | test/python/test_group_trait_info.py | 15 | ||||
| -rw-r--r-- | test/python/test_search.py | 93 |
3 files changed, 107 insertions, 2 deletions
diff --git a/test/python/test_dataset_probeset_info.py b/test/python/test_dataset_probeset_info.py index c073f3a..04241ba 100644 --- a/test/python/test_dataset_probeset_info.py +++ b/test/python/test_dataset_probeset_info.py @@ -56,6 +56,7 @@ class DatasetProbesetInfoTest(ServerTestCase): self.assertEqual(info["name"], "100001_at") self.assertEqual(info["symbol"], "Cd3g") self.assertIsInstance(info["mean"], float) + self.assertAlmostEqual(info["lod"], info["lrs"] / 4.61) def test_dataset_probeset_info_without_a_token_returns_401(self): if not database_reachable(): diff --git a/test/python/test_group_trait_info.py b/test/python/test_group_trait_info.py index d5a0761..1e4fab8 100644 --- a/test/python/test_group_trait_info.py +++ b/test/python/test_group_trait_info.py @@ -36,6 +36,21 @@ class GroupTraitInfoTest(ServerTestCase): finally: conn.close() + def test_group_trait_info_reports_lod_and_locus(self): + # The "highest LOD ... serotonin in BXD male midbrain" trait: see + # test_search.py. `lod` is `lrs` / 4.61. + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/traits/17985") + self.assertEqual(response.status, 200) + + info = json.loads(body) + self.assertEqual(info["locus"], "rsm10000012461") + self.assertAlmostEqual(info["lod"], info["lrs"] / 4.61) + self.assertAlmostEqual(info["lod"], 2.603, places=3) + self.assertAlmostEqual(info["additive"], -1.3041, places=4) + def test_group_trait_info_bxd_returns_phenotype_and_publication(self): if not database_reachable(): self.skipTest("no database on 127.0.0.1:3306") diff --git a/test/python/test_search.py b/test/python/test_search.py index 18cc54c..bfd1d38 100644 --- a/test/python/test_search.py +++ b/test/python/test_search.py @@ -18,11 +18,20 @@ import unittest import urllib.parse from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token -from server_test_case import HOST, PORT, ServerTestCase, wait_for_port +from server_test_case import ( + HOST, + PORT, + ServerTestCase, + database_reachable, + wait_for_port, +) FIXTURE = os.path.join( os.path.dirname(__file__), "..", "data", "search_alcohol_phenotype.json" ) +SEROTONIN_FIXTURE = os.path.join( + os.path.dirname(__file__), "..", "data", "search_serotonin_midbrain_bxd.json" +) class Gn3StubHandler(http.server.BaseHTTPRequestHandler): @@ -30,7 +39,17 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) - if "boom" in query.get("query", [""])[0]: + if "serotonin" in query.get("query", [""])[0]: + # gn3 has no sort of its own: a sorted /search must ask for a + # wide page (1000), not just `limit`, to rank across it. + if query.get("per_page") != ["1000"]: + body = b"per_page must be 1000" + self.send_response(http.client.INTERNAL_SERVER_ERROR) + else: + with open(SEROTONIN_FIXTURE, "rb") as fixture: + body = fixture.read() + self.send_response(http.client.OK) + elif "boom" in query.get("query", [""])[0]: body = b'{"error": "gn3 exploded"}' self.send_response(http.client.INTERNAL_SERVER_ERROR) elif "&&" in query.get("query", [""])[0]: @@ -129,6 +148,76 @@ class SearchTest(ServerTestCase): self.assertEqual(status, http.client.BAD_REQUEST) self.assertIn("phenotype", body["detail"]) + def test_search_validates_sort_and_order(self): + self.require_auth() + + status, body = self.get("/search?q=alcohol&sort=mean") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertIn("lrs", body["detail"]) + + status, body = self.get("/search?q=alcohol&sort=lod&order=sideways") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertIn("asc", body["detail"]) + + def test_highest_lod_for_serotonin_in_bxd_male_midbrain(self): + # "What is the highest LOD score, locus marker, and additive + # effect for serotonin in BXD male midbrain?" -- mirrors scenario 7 + # of tests/search.rs. Males only: " males [" matches "adult males [" + # but not "adult females [" or "adult males and females [". + self.require_auth() + + status, body = self.get( + "/search?q=serotonin%20AND%20midbrain%20AND%20group:BXD" + "&sort=lod&limit=100" + ) + self.assertEqual(status, http.client.OK) + hits = body["data"] + self.assertEqual(len(hits), 100) + lods = [hit["lod"] for hit in hits] + self.assertEqual(lods, sorted(lods, reverse=True)) + for hit in hits: + self.assertAlmostEqual(hit["lod"], hit["lrs"] / 4.61) + + male_midbrain = [ + hit + for hit in hits + if "Midbrain" in hit["description"] and " males [" in hit["description"] + ] + serotonin = next( + hit + for hit in male_midbrain + if "5-hydroxytryptamine (5-HT, serotonin) level" in hit["description"] + ) + self.assertEqual(serotonin["name"], "17985") + self.assertAlmostEqual(serotonin["lod"], 2.603, places=3) + self.assertAlmostEqual(serotonin["additive"], -1.3041, places=4) + self.assertEqual(serotonin["geno_chr"], "16") + + # The serotonin transporter (SERT) in the same tissue and sex + # has the higher LOD: "serotonin" is ambiguous between the two. + top = male_midbrain[0] + self.assertEqual(top["name"], "17994") + self.assertAlmostEqual(top["lod"], 3.577, places=3) + self.assertAlmostEqual(top["additive"], 233.99, places=2) + + # The peak marker's name comes from PublishXRef (needs a live + # MariaDB); without one `locus` is null and the above still holds. + if database_reachable(): + self.assertEqual(serotonin["locus"], "rsm10000012461") + self.assertEqual(top["locus"], "rsm10000013922") + + def test_search_orders_ascending(self): + self.require_auth() + + with open(SEROTONIN_FIXTURE) as fixture: + min_lrs = min(hit["lrs"] for hit in json.load(fixture)) + status, body = self.get("/search?q=serotonin&sort=lrs&order=asc&limit=3") + self.assertEqual(status, http.client.OK) + lrs = [hit["lrs"] for hit in body["data"]] + self.assertEqual(len(lrs), 3) + self.assertEqual(lrs[0], min_lrs) + self.assertEqual(lrs, sorted(lrs)) + def test_search_forwards_gn3_parse_errors(self): self.require_auth() |
