about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-11-15 03:34:34 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-11-15 03:34:34 +0000
commit4f2117ce6bbd065059775eaebd98b00135159847 (patch)
tree50336d87d91d0c178c14cd3e272d8bf4762844dd /src
parentc8ce261f2bf499ab3260ff5ce186c801491b9fea (diff)
downloadgenenetwork4-4f2117ce6bbd065059775eaebd98b00135159847.tar.gz
correlation: phenotype traits and datasets, the common case
Either side of a correlation may now be a Publish (phenotype) dataset, not
just a ProbeSet one: a pheno trait correlated against an expression
dataset is what GN2's mapping page is usually used for, and returning 404
for BXDPublish was the wrong answer.

A publish reference trait is addressed by its numeric PublishXRef.Id --
that is how GN2 addresses phenotype traits, and a name there is refused
with a message saying so rather than a confusing "no such trait".  The
bulk fetch gains the publish equivalent, keyed on the freeze's
InbredSetId, so the same individuals line up on both sides.

Measured live: BXDPublish/10001 (34 observations) against HC_M2_0606_P,
top 5, HTTP 200 in 5.0 s; 1440079_at at r -0.757 (p 3.2e-6) leads.
Diffstat (limited to 'src')
-rw-r--r--src/db.rs37
-rw-r--r--src/routes/correlation.rs87
2 files changed, 101 insertions, 23 deletions
diff --git a/src/db.rs b/src/db.rs
index 7464125..91734f7 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1594,6 +1594,43 @@ pub fn get_dataset_values_for_individuals(
     .map_err(DbError::from)
 }
 
+/// The same bulk fetch for a Publish (phenotype) dataset.
+///
+/// `PublishXRef.Id` is the trait's handle within that dataset -- GN2 addresses
+/// phenotype traits by it ("1427571_at" style names belong to the expression
+/// sets) -- and the values live in `PublishData`, keyed by the same InbredSetId
+/// as the reference trait's group.  Returned as (trait id, individual, value)
+/// so the caller can group it exactly like the probeset case.
+pub fn get_publish_values_for_individuals(
+    inbred_set_id: u16,
+    individuals: &[String],
+) -> Result<Vec<(String, String, Option<f64>)>, DbError> {
+    if individuals.is_empty() {
+        return Ok(Vec::new());
+    }
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    let placeholders = std::iter::repeat_n("?", individuals.len())
+        .collect::<Vec<_>>()
+        .join(",");
+    let query = format!(
+        "SELECT CAST(PublishXRef.Id AS CHAR), Strain.Name, PublishData.value \
+         FROM PublishData, Strain, PublishXRef \
+         WHERE PublishXRef.InbredSetId = ? AND PublishXRef.DataId = PublishData.Id \
+                AND PublishData.StrainId = Strain.Id \
+                AND Strain.Name IN ({placeholders}) \
+         ORDER BY PublishXRef.Id, Strain.Name"
+    );
+    let mut params = mysql::Params::Positional(vec![mysql::Value::from(inbred_set_id)]);
+    if let mysql::Params::Positional(values) = &mut params {
+        values.extend(individuals.iter().map(|name| mysql::Value::from(name.as_str())));
+    }
+    conn.exec_map(&query, params, |(trait_id, individual, value)| {
+        (trait_id, individual, value)
+    })
+    .map_err(DbError::from)
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/src/routes/correlation.rs b/src/routes/correlation.rs
index 36faedb..00442a9 100644
--- a/src/routes/correlation.rs
+++ b/src/routes/correlation.rs
@@ -163,23 +163,17 @@ impl Failure {
 }
 
 /// The blocking half: resolve both datasets, fetch the values, correlate.
+///
+/// Either side may be a ProbeSet (expression) dataset or a Publish (phenotype)
+/// one: GN2's `corr_dataset` menu mixes them, so a pheno trait correlated
+/// against an expression dataset -- the common case -- has to work.
 fn compute(
     request: &CorrelationRequest,
     method: Method,
     top_n: usize,
 ) -> Result<(String, Vec<correlation::Hit>), Failure> {
-    let reference_dataset = db::get_dataset_by_name(&request.dataset)
-        .map_err(|error| Failure::Database(error.to_string()))?
-        .ok_or_else(|| Failure::NotFound(format!("no dataset named '{}'", request.dataset)))?;
-
-    let values = db::get_dataset_probeset_values(reference_dataset.id, &request.trait_name)
-        .map_err(|error| Failure::Database(error.to_string()))?;
-    let reference = correlation::Reference::from_pairs(
-        values
-            .into_iter()
-            .map(|value| (value.name, value.value)),
-    );
-    if reference.is_empty() {
+    let reference = resolve_reference(request)?;
+    if reference.values.is_empty() {
         return Err(Failure::NoValues(format!(
             "'{}' has no values in '{}', so there is nothing to correlate",
             request.trait_name, request.dataset
@@ -194,17 +188,7 @@ fn compute(
         )));
     }
 
-    let target = db::get_dataset_by_name(&request.target_dataset)
-        .map_err(|error| Failure::Database(error.to_string()))?
-        .ok_or_else(|| {
-            Failure::NotFound(format!(
-                "no dataset named '{}'",
-                request.target_dataset
-            ))
-        })?;
-
-    let rows = db::get_dataset_values_for_individuals(target.id, &reference.names)
-        .map_err(|error| Failure::Database(error.to_string()))?;
+    let rows = resolve_target_rows(&request.target_dataset, &reference.names)?;
     let hits = correlation::correlate_many(&reference, method, rows, top_n);
     Ok((
         format!(
@@ -216,3 +200,60 @@ fn compute(
         hits,
     ))
 }
+
+/// The reference trait's values, from whichever kind of dataset holds it.
+fn resolve_reference(request: &CorrelationRequest) -> Result<correlation::Reference, Failure> {
+    if let Some(dataset) =
+        db::get_dataset_by_name(&request.dataset).map_err(db_failure)?
+    {
+        let values = db::get_dataset_probeset_values(dataset.id, &request.trait_name)
+            .map_err(db_failure)?;
+        return Ok(correlation::Reference::from_pairs(
+            values.into_iter().map(|value| (value.name, value.value)),
+        ));
+    }
+    if let Some(publish) =
+        db::get_publish_freeze_by_name(&request.dataset).map_err(db_failure)?
+    {
+        // A publish trait is addressed by its numeric id within the set.
+        let trait_id: u32 = request.trait_name.trim().parse().map_err(|_| {
+            Failure::NoValues(format!(
+                "'{}' is a phenotype dataset, so its traits are addressed by id (a number), \
+                 not by name '{}'",
+                request.dataset, request.trait_name
+            ))
+        })?;
+        let values = db::get_group_trait_values(publish.inbred_set_id, trait_id)
+            .map_err(db_failure)?;
+        let values: Vec<(String, Option<f64>)> = values
+            .into_iter()
+            .map(|value| (value.name, value.value))
+            .collect();
+        return Ok(correlation::Reference::from_pairs(values));
+    }
+    Err(Failure::NotFound(format!(
+        "no dataset named '{}'",
+        request.dataset
+    )))
+}
+
+/// Every candidate trait's values in the target dataset, restricted to the
+/// reference's individuals.
+fn resolve_target_rows(
+    dataset: &str,
+    individuals: &[String],
+) -> Result<Vec<(String, String, Option<f64>)>, Failure> {
+    if let Some(target) = db::get_dataset_by_name(dataset).map_err(db_failure)? {
+        return db::get_dataset_values_for_individuals(target.id, individuals)
+            .map_err(db_failure);
+    }
+    if let Some(publish) = db::get_publish_freeze_by_name(dataset).map_err(db_failure)? {
+        return db::get_publish_values_for_individuals(publish.inbred_set_id, individuals)
+            .map_err(db_failure);
+    }
+    Err(Failure::NotFound(format!("no dataset named '{dataset}'")))
+}
+
+fn db_failure(error: db::DbError) -> Failure {
+    Failure::Database(error.to_string())
+}