about summary refs log tree commit diff
path: root/src/db.rs
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-03 16:14:49 +0000
committerPjotr <wrk@bosa.lan>2026-10-04 06:31:16 +0000
commite7191463db66fcc3c2618ccdf004e798e1d3ac6a (patch)
tree2e76f71985ee8b6a49c49c1c17606fffbceb056f /src/db.rs
parent0980c21ff71bf4c8fa73d3193460f7b7f69d9f6b (diff)
downloadgenenetwork4-e7191463db66fcc3c2618ccdf004e798e1d3ac6a.tar.gz
Add ProbeSetXRef endpoints: datasets, probesets, probeset values.
The expression (ProbeSet) counterpart to the PublishXRef endpoints:
ProbeSetXRef has no single Id column of its own (its primary key is
DataId; the natural key is the pair (ProbeSetFreezeId, ProbeSetId)),
so a trait here is addressed by dataset name plus ProbeSet.Name
instead of a single numeric trait id.

* src/db.rs (Dataset): New struct, one ProbeSetFreeze row.
(get_group_datasets): New function. Joins ProbeSetFreeze and
ProbeFreeze by group Id, matching GeneNetwork2's
wqflask.api.router.get_datasets_for_group.
(get_dataset_by_name): New function. Matches Name, Name2, FullName,
ShortName or numeric Id, matching
wqflask.api.router.get_dataset_info's ProbeSet-branch matching logic.
A dataset name is globally unique on its own, unlike a group's
traits, so this isn't scoped by group.
(ProbeSetInfo): New struct, a probe's ProbeSetXRef mapping stats plus
its ProbeSet gene annotation.
(get_dataset_probeset_info): New function, matching
wqflask.api.router.get_trait_info's ProbeSet branch.
(ProbeSetValue): New struct.
(get_dataset_probeset_values): New function, matching
wqflask.api.router.trait_sample_data's ProbeSet branch.
* src/lib.rs (DatasetProbesetLookup): New enum, the expression-dataset
equivalent of GroupTraitLookup -- distinguishes a missing dataset from
a missing probeset in an existing dataset.
(group_datasets): New handler for GET /groups/{group}/datasets.
(dataset_by_name): New handler for GET /datasets/{dataset}.
(get_dataset_probeset_info_result, dataset_probeset_info): New
function and handler for GET /datasets/{dataset}/probesets/{probeset}.
(get_dataset_probeset_values_result, dataset_probeset_values): New
function and handler for
GET /datasets/{dataset}/probesets/{probeset}/values.
(app): Register all four.
* tests/group_datasets.rs, tests/dataset_by_name.rs,
tests/dataset_probeset_info.rs, tests/dataset_probeset_values.rs,
test/python/test_group_datasets.py, test/python/test_dataset_by_name.py,
test/python/test_dataset_probeset_info.py,
test/python/test_dataset_probeset_values.py: New files. Br_U_0803_M
(dataset) / 100001_at (probeset, gene symbol Cd3g) in BXD are real rows
used as fixtures.
* tests/openapi.rs, test/python/test_openapi.py: Check the four new
paths are in the generated spec.
* README.md: Document the four new endpoints and list the new test
files.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs184
1 files changed, 184 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs
index 64a2c4c..223bcde 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -365,3 +365,187 @@ pub fn get_group_trait_info(group_id: u16, trait_id: u32) -> Result<Option<Trait
         mysql::params! { "group_id" => group_id, "trait_id" => trait_id },
     )
 }
+
+/// One expression (ProbeSet) dataset: a ProbeSetFreeze row, matching
+/// the shape of GeneNetwork2's GET /api/v_pre1/datasets/<group_name>
+/// response (field names there: Id, ProbeFreezeId, AvgID,
+/// Short_Abbreviation, Long_Abbreviation, FullName, ShortName,
+/// CreateTime, public, confidentiality, DataScale -- renamed here the
+/// same way GroupDetail renames away from "inbred_set": Name/Name2 are
+/// clearer than GN2's legacy "Short_Abbreviation"/"Long_Abbreviation").
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct Dataset {
+    pub id: u16,
+    pub probe_freeze_id: u16,
+    pub name: String,
+    pub name2: Option<String>,
+    pub full_name: String,
+    pub short_name: String,
+    pub create_time: String,
+    pub public: u8,
+    pub confidentiality: u8,
+    pub data_scale: String,
+}
+
+/// Fetch the expression datasets belonging to a group. Mirrors
+/// GeneNetwork2's wqflask.api.router.get_datasets_for_group (without
+/// its species-scoped variant, which this API doesn't need: a group
+/// name alone already picks one species).
+pub fn get_group_datasets(group_id: u16) -> Result<Vec<Dataset>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    conn.exec_map(
+        "SELECT ProbeSetFreeze.Id, ProbeSetFreeze.ProbeFreezeId, ProbeSetFreeze.Name, \
+                ProbeSetFreeze.Name2, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, \
+                CAST(ProbeSetFreeze.CreateTime AS CHAR), ProbeSetFreeze.public, \
+                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale \
+         FROM ProbeSetFreeze \
+         JOIN ProbeFreeze ON ProbeFreeze.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId \
+         WHERE ProbeFreeze.InbredSetId = :group_id \
+         ORDER BY ProbeSetFreeze.Name",
+        mysql::params! { "group_id" => group_id },
+        |(id, probe_freeze_id, name, name2, full_name, short_name, create_time, public, confidentiality, data_scale)| {
+            Dataset {
+                id,
+                probe_freeze_id,
+                name,
+                name2,
+                full_name,
+                short_name,
+                create_time,
+                public,
+                confidentiality,
+                data_scale,
+            }
+        },
+    )
+}
+
+/// Fetch one dataset by Name, Name2, FullName, ShortName or numeric
+/// Id, matching GeneNetwork2's wqflask.api.router.get_dataset_info's
+/// matching logic for its ProbeSet (mRNA expression) branch.
+pub fn get_dataset_by_name(name: &str) -> Result<Option<Dataset>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    conn.exec_first(
+        "SELECT Id, ProbeFreezeId, Name, Name2, FullName, ShortName, \
+                CAST(CreateTime AS CHAR), public, confidentiality, DataScale \
+         FROM ProbeSetFreeze \
+         WHERE Name = :name OR Name2 = :name OR FullName = :name \
+                OR ShortName = :name OR CAST(Id AS CHAR) = :name",
+        mysql::params! { "name" => name },
+    )
+    .map(|row: Option<(u16, u16, String, Option<String>, String, String, String, u8, u8, String)>| {
+        row.map(
+            |(id, probe_freeze_id, name, name2, full_name, short_name, create_time, public, confidentiality, data_scale)| {
+                Dataset {
+                    id,
+                    probe_freeze_id,
+                    name,
+                    name2,
+                    full_name,
+                    short_name,
+                    create_time,
+                    public,
+                    confidentiality,
+                    data_scale,
+                }
+            },
+        )
+    })
+}
+
+/// One expression (ProbeSet) trait's metadata: its ProbeSetXRef
+/// mapping stats plus its ProbeSet gene annotation. The expression
+/// equivalent of TraitInfo (for Publish/phenotype traits) above, but
+/// addressed differently: ProbeSetXRef has no single Id column of its
+/// own (its primary key is DataId; the natural key is the pair
+/// (ProbeSetFreezeId, ProbeSetId)), so a trait here is identified by
+/// dataset name plus ProbeSet.Name rather than a single numeric id.
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct ProbeSetInfo {
+    pub probeset_id: u32,
+    pub name: String,
+    pub symbol: Option<String>,
+    pub description: Option<String>,
+    pub chr: Option<String>,
+    pub mb: Option<f64>,
+    pub mean: Option<f64>,
+    pub locus: Option<String>,
+    pub lrs: Option<f64>,
+    pub p_value: Option<f64>,
+    pub additive: Option<f64>,
+}
+
+/// Fetch one expression trait's metadata (see ProbeSetInfo) within a
+/// dataset, matching GeneNetwork2's wqflask.api.router.get_trait_info's
+/// ProbeSet branch (which additionally takes a Mb/Chr pair from
+/// ProbeSet, as done here, though GN2 doesn't join ProbeSetXRef's
+/// pValue).
+pub fn get_dataset_probeset_info(
+    dataset_id: u16,
+    probeset_name: &str,
+) -> Result<Option<ProbeSetInfo>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    conn.exec_first(
+        "SELECT ProbeSet.Id, ProbeSet.Name, ProbeSet.Symbol, ProbeSet.description, \
+                ProbeSet.Chr, ProbeSet.Mb, ProbeSetXRef.mean, ProbeSetXRef.Locus, \
+                ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.additive \
+         FROM ProbeSet \
+         JOIN ProbeSetXRef ON ProbeSetXRef.ProbeSetId = ProbeSet.Id \
+         WHERE ProbeSetXRef.ProbeSetFreezeId = :dataset_id AND ProbeSet.Name = :probeset_name",
+        mysql::params! { "dataset_id" => dataset_id, "probeset_name" => probeset_name },
+    )
+    .map(
+        |row: Option<(u32, String, Option<String>, Option<String>, Option<String>, Option<f64>, Option<f64>, Option<String>, Option<f64>, Option<f64>, Option<f64>)>| {
+            row.map(
+                |(probeset_id, name, symbol, description, chr, mb, mean, locus, lrs, p_value, additive)| {
+                    ProbeSetInfo {
+                        probeset_id,
+                        name,
+                        symbol,
+                        description,
+                        chr,
+                        mb,
+                        mean,
+                        locus,
+                        lrs,
+                        p_value,
+                        additive,
+                    }
+                },
+            )
+        },
+    )
+}
+
+/// One strain's value for an expression (ProbeSet) trait, matching
+/// GeneNetwork2's wqflask.api.router.trait_sample_data (the ProbeSet
+/// branch, trimmed to just name and value); the expression equivalent
+/// of TraitValue (for Publish/phenotype traits) above.
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct ProbeSetValue {
+    pub name: String,
+    pub value: Option<f64>,
+}
+
+/// Fetch the per-strain values of one expression trait within a
+/// dataset, addressed the same way as get_dataset_probeset_info.
+pub fn get_dataset_probeset_values(
+    dataset_id: u16,
+    probeset_name: &str,
+) -> Result<Vec<ProbeSetValue>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    conn.exec_map(
+        "SELECT Strain.Name, ProbeSetData.value \
+         FROM ProbeSetData, Strain, ProbeSetXRef, ProbeSet \
+         WHERE ProbeSetXRef.ProbeSetFreezeId = :dataset_id AND ProbeSet.Name = :probeset_name \
+                AND ProbeSetXRef.ProbeSetId = ProbeSet.Id \
+                AND ProbeSetXRef.DataId = ProbeSetData.Id AND ProbeSetData.StrainId = Strain.Id \
+         ORDER BY Strain.Name",
+        mysql::params! { "dataset_id" => dataset_id, "probeset_name" => probeset_name },
+        |(name, value)| ProbeSetValue { name, value },
+    )
+}