about summary refs log tree commit diff
path: root/src/db.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-08 13:37:57 +0000
committerPjotr <wrk@bosa.lan>2026-10-08 13:37:57 +0000
commit2edc685bb7c56d5f9f28da09378f300bfe81021d (patch)
tree1120d7589a1143ef29a4f5d2e2a47437e8699552 /src/db.rs
parent208eeac395079f46fcbe07b4185524e017c554ef (diff)
downloadgenenetwork4-2edc685bb7c56d5f9f28da09378f300bfe81021d.tar.gz
* src/db.rs, src/routes: add mapping_method, tissue, /mapping-methods, /accessions.
Answers four more questions of aisearch's full_benchmark.csv from
tables the API did not expose.

* src/db.rs (Group, GroupDetail), src/routes/groups.rs: groups gain
`mapping_method`, the name of the MappingMethod row their
mapping_method_id points to (qtlreaper, Rqtl, ...), or null.
* src/db.rs (Dataset): datasets gain `tissue`, the Tissue their
ProbeFreeze points to (e.g. "Brain mRNA").
* src/routes/mapping_methods.rs: GET /mapping-methods lists the
MappingMethod table, including tools no group uses (PLINK, FastMap).
* src/routes/accessions.rs: GET /accessions/{accession} resolves a
GeneNetwork accession number (GN602, gn602 or 602) via InfoFiles to the
dataset's name, title, category (publish/probeset) and group.
* docs/genenetwork_datatypes.md: MappingMethodId is a key into the
MappingMethod table (the authoritative reading); GN2's code-based reading
is noted as superseded.

* tests/benchmark.rs, test/data/benchmark_search.json: add the 60 new
full-benchmark rows that pass (full rows 0..114; search rows run against
recorded production gn3, the rest need the database), and full rows 33
(mapping tools), 34 (Rqtl for outbred sets), 35 (GN602) and 44 (only
GTEx v8 has TPM datasets). The untested rows are listed in the file's
header comment with the reason for each.
* tests, test/python: tests for the new fields and endpoints; both
endpoints added to the OpenAPI and next-link crawl tests.
* README.md, RELEASE_NOTES.md: document the new fields and endpoints.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs139
1 files changed, 126 insertions, 13 deletions
diff --git a/src/db.rs b/src/db.rs
index c6723ff..ba22811 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -181,6 +181,10 @@ pub struct Group {
     pub family: Option<String>,
     /// The species' short name, e.g. "mouse" (as listed by /species).
     pub species: Option<String>,
+    /// The mapping tool offered for the group, e.g. "qtlreaper" or
+    /// "Rqtl" (the MappingMethod row `mapping_method_id` points to; see
+    /// get_mapping_methods).
+    pub mapping_method: Option<String>,
 }
 
 /// Mirrors GeneNetwork2's wqflask.api.router.get_groups_list, plus the
@@ -192,8 +196,10 @@ pub fn get_all_groups() -> Result<Vec<Group>, mysql::Error> {
         "SELECT InbredSet.InbredSetId, InbredSet.SpeciesId, InbredSet.InbredSetName, \
                 InbredSet.Name, InbredSet.FullName, InbredSet.public, \
                 InbredSet.MappingMethodId, InbredSet.GeneticType, InbredSet.Family, \
-                Species.Name \
-         FROM InbredSet LEFT JOIN Species ON Species.Id = InbredSet.SpeciesId \
+                Species.Name, MappingMethod.Name \
+         FROM InbredSet \
+         LEFT JOIN Species ON Species.Id = InbredSet.SpeciesId \
+         LEFT JOIN MappingMethod ON MappingMethod.Id = InbredSet.MappingMethodId \
          ORDER BY InbredSet.Name ASC",
         |(
             id,
@@ -206,6 +212,7 @@ pub fn get_all_groups() -> Result<Vec<Group>, mysql::Error> {
             genetic_type,
             family,
             species,
+            mapping_method,
         )| {
             Group {
                 id,
@@ -218,11 +225,93 @@ pub fn get_all_groups() -> Result<Vec<Group>, mysql::Error> {
                 genetic_type,
                 family,
                 species,
+                mapping_method,
             }
         },
     )
 }
 
+/// One mapping tool GeneNetwork offers (a MappingMethod row), e.g.
+/// qtlreaper, Rqtl, HappyR, PLINK, FastMap. A group points at one of
+/// these through InbredSet.MappingMethodId (see `Group::mapping_method`);
+/// the table also lists tools no group uses.
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct MappingMethod {
+    pub id: u16,
+    pub name: String,
+}
+
+pub fn get_mapping_methods() -> Result<Vec<MappingMethod>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    conn.query_map(
+        "SELECT Id, Name FROM MappingMethod ORDER BY Id",
+        |(id, name)| MappingMethod { id, name },
+    )
+}
+
+/// A GeneNetwork accession number (the "GN602" of a dataset's page): an
+/// InfoFiles row, resolved to the dataset it describes.
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct Accession {
+    /// The number, without the "GN" prefix.
+    pub accession: u32,
+    /// The dataset's name (InfoFiles.InfoPageName), e.g. "BXDPublish".
+    pub name: String,
+    /// The page title, e.g. "BXD Published Phenotypes".
+    pub title: Option<String>,
+    /// "publish" (a phenotype dataset) or "probeset" (an expression
+    /// dataset); None when the name matches neither.
+    pub category: Option<String>,
+    /// The short name of the group the dataset belongs to.
+    pub group: Option<String>,
+}
+
+/// Resolve an accession number: its InfoFiles row, then the dataset of
+/// that name (PublishFreeze, else ProbeSetFreeze) for its category and
+/// group. None when no InfoFiles row has the accession.
+pub fn get_accession(accession: u32) -> Result<Option<Accession>, mysql::Error> {
+    let pool = pool()?;
+    let mut conn = pool.get_conn()?;
+    let row: Option<(String, Option<String>)> = conn.exec_first(
+        "SELECT InfoPageName, InfoPageTitle FROM InfoFiles \
+         WHERE GN_AccesionId = :accession LIMIT 1",
+        params! { "accession" => accession },
+    )?;
+    let Some((name, title)) = row else {
+        return Ok(None);
+    };
+    let publish: Option<String> = conn.exec_first(
+        "SELECT InbredSet.Name FROM PublishFreeze \
+         JOIN InbredSet ON InbredSet.Id = PublishFreeze.InbredSetId \
+         WHERE PublishFreeze.Name = :name LIMIT 1",
+        params! { "name" => &name },
+    )?;
+    let (category, group) = match publish {
+        Some(group) => (Some("publish".to_string()), Some(group)),
+        None => {
+            let probeset: Option<String> = conn.exec_first(
+                "SELECT InbredSet.Name FROM ProbeSetFreeze \
+                 JOIN ProbeFreeze ON ProbeFreeze.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId \
+                 JOIN InbredSet ON InbredSet.Id = ProbeFreeze.InbredSetId \
+                 WHERE ProbeSetFreeze.Name = :name LIMIT 1",
+                params! { "name" => &name },
+            )?;
+            match probeset {
+                Some(group) => (Some("probeset".to_string()), Some(group)),
+                None => (None, None),
+            }
+        }
+    };
+    Ok(Some(Accession {
+        accession,
+        name,
+        title,
+        category,
+        group,
+    }))
+}
+
 /// One full row of the InbredSet table. InbredSet has 14 columns, more
 /// than mysql_common's FromRow is implemented for tuples (up to 12), so
 /// this derives FromRow instead of being built from a tuple like
@@ -252,6 +341,9 @@ pub struct GroupDetail {
     #[mysql(rename = "InbredSetCode")]
     pub group_code: Option<String>,
     pub description: Option<String>,
+    /// The mapping tool's name (the MappingMethod row `mapping_method_id`
+    /// points to), e.g. "qtlreaper".
+    pub mapping_method: Option<String>,
 }
 
 /// Fetch one group by InbredSetName, Name, FullName or numeric Id,
@@ -263,11 +355,15 @@ pub fn get_group_by_name(name: &str) -> Result<Option<GroupDetail>, mysql::Error
     let pool = pool()?;
     let mut conn = pool.get_conn()?;
     conn.exec_first(
-        "SELECT Id, InbredSetId, InbredSetName, Name, SpeciesId, FullName, \
-                public, MappingMethodId, GeneticType, Family, \
-                FamilyOrder, MenuOrderId, InbredSetCode, Description \
-         FROM InbredSet WHERE InbredSetName = :name OR Name = :name \
-                OR FullName = :name OR CAST(Id AS CHAR) = :name",
+        "SELECT InbredSet.Id, InbredSet.InbredSetId, InbredSet.InbredSetName, InbredSet.Name, \
+                InbredSet.SpeciesId, InbredSet.FullName, InbredSet.public, \
+                InbredSet.MappingMethodId, InbredSet.GeneticType, InbredSet.Family, \
+                InbredSet.FamilyOrder, InbredSet.MenuOrderId, InbredSet.InbredSetCode, \
+                InbredSet.Description, MappingMethod.Name AS MappingMethod \
+         FROM InbredSet \
+         LEFT JOIN MappingMethod ON MappingMethod.Id = InbredSet.MappingMethodId \
+         WHERE InbredSet.InbredSetName = :name OR InbredSet.Name = :name \
+                OR InbredSet.FullName = :name OR CAST(InbredSet.Id AS CHAR) = :name",
         mysql::params! { "name" => name },
     )
 }
@@ -564,6 +660,9 @@ pub struct Dataset {
     pub public: u8,
     pub confidentiality: u8,
     pub data_scale: String,
+    /// What the dataset was measured in, e.g. "Retina mRNA" (the Tissue
+    /// row its ProbeFreeze points to).
+    pub tissue: Option<String>,
 }
 
 /// Fetch the expression datasets belonging to a group. Mirrors
@@ -577,9 +676,10 @@ pub fn get_group_datasets(group_id: u16) -> Result<Vec<Dataset>, mysql::Error> {
         "SELECT ProbeSetFreeze.Id, ProbeSetFreeze.ProbeFreezeId, ProbeSetFreeze.Name, \
                 ProbeSetFreeze.Name2, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, \
                 CAST(ProbeSetFreeze.CreateTime AS CHAR), ProbeSetFreeze.public, \
-                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale \
+                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale, Tissue.Name \
          FROM ProbeSetFreeze \
          JOIN ProbeFreeze ON ProbeFreeze.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId \
+         LEFT JOIN Tissue ON Tissue.Id = ProbeFreeze.TissueId \
          WHERE ProbeFreeze.InbredSetId = :group_id \
          ORDER BY ProbeSetFreeze.Name",
         mysql::params! { "group_id" => group_id },
@@ -594,6 +694,7 @@ pub fn get_group_datasets(group_id: u16) -> Result<Vec<Dataset>, mysql::Error> {
             public,
             confidentiality,
             data_scale,
+            tissue,
         )| {
             Dataset {
                 id,
@@ -606,6 +707,7 @@ pub fn get_group_datasets(group_id: u16) -> Result<Vec<Dataset>, mysql::Error> {
                 public,
                 confidentiality,
                 data_scale,
+                tissue,
             }
         },
     )
@@ -721,10 +823,11 @@ pub fn get_species_datasets(species_id: u16) -> Result<Vec<Dataset>, mysql::Erro
         "SELECT ProbeSetFreeze.Id, ProbeSetFreeze.ProbeFreezeId, ProbeSetFreeze.Name, \
                 ProbeSetFreeze.Name2, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, \
                 CAST(ProbeSetFreeze.CreateTime AS CHAR), ProbeSetFreeze.public, \
-                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale \
+                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale, Tissue.Name \
          FROM ProbeSetFreeze \
          JOIN ProbeFreeze ON ProbeFreeze.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId \
          JOIN InbredSet ON InbredSet.Id = ProbeFreeze.InbredSetId \
+         LEFT JOIN Tissue ON Tissue.Id = ProbeFreeze.TissueId \
          WHERE InbredSet.SpeciesId = :species_id \
          ORDER BY ProbeSetFreeze.Name",
         mysql::params! { "species_id" => species_id },
@@ -739,6 +842,7 @@ pub fn get_species_datasets(species_id: u16) -> Result<Vec<Dataset>, mysql::Erro
             public,
             confidentiality,
             data_scale,
+            tissue,
         )| {
             Dataset {
                 id,
@@ -751,6 +855,7 @@ pub fn get_species_datasets(species_id: u16) -> Result<Vec<Dataset>, mysql::Erro
                 public,
                 confidentiality,
                 data_scale,
+                tissue,
             }
         },
     )
@@ -768,6 +873,7 @@ type DatasetRow = (
     u8,
     u8,
     String,
+    Option<String>,
 );
 
 /// Fetch one dataset by Name, Name2, FullName, ShortName or numeric
@@ -777,11 +883,16 @@ 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 \
+        "SELECT ProbeSetFreeze.Id, ProbeSetFreeze.ProbeFreezeId, ProbeSetFreeze.Name, \
+                ProbeSetFreeze.Name2, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, \
+                CAST(ProbeSetFreeze.CreateTime AS CHAR), ProbeSetFreeze.public, \
+                ProbeSetFreeze.confidentiality, ProbeSetFreeze.DataScale, Tissue.Name \
          FROM ProbeSetFreeze \
-         WHERE Name = :name OR Name2 = :name OR FullName = :name \
-                OR ShortName = :name OR CAST(Id AS CHAR) = :name",
+         LEFT JOIN ProbeFreeze ON ProbeFreeze.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId \
+         LEFT JOIN Tissue ON Tissue.Id = ProbeFreeze.TissueId \
+         WHERE ProbeSetFreeze.Name = :name OR ProbeSetFreeze.Name2 = :name \
+                OR ProbeSetFreeze.FullName = :name OR ProbeSetFreeze.ShortName = :name \
+                OR CAST(ProbeSetFreeze.Id AS CHAR) = :name",
         mysql::params! { "name" => name },
     )
     .map(|row: Option<DatasetRow>| {
@@ -797,6 +908,7 @@ pub fn get_dataset_by_name(name: &str) -> Result<Option<Dataset>, mysql::Error>
                 public,
                 confidentiality,
                 data_scale,
+                tissue,
             )| {
                 Dataset {
                     id,
@@ -809,6 +921,7 @@ pub fn get_dataset_by_name(name: &str) -> Result<Option<Dataset>, mysql::Error>
                     public,
                     confidentiality,
                     data_scale,
+                    tissue,
                 }
             },
         )