about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-03 14:49:11 +0000
committerPjotr <wrk@bosa.lan>2026-10-03 14:49:11 +0000
commit5ecc6b70e8a6ce2ba91f7808295a7d7672481d58 (patch)
tree2e7a704d7692098588fbf5fde5b9aa4fc018e8e0 /src
parent9e5655e21a3ec486b1d52d342156de0edc06df76 (diff)
downloadgenenetwork4-5ecc6b70e8a6ce2ba91f7808295a7d7672481d58.tar.gz
Add GET /groups/{group}/strains, mirroring gn-guile's strain-id-names.
* src/db.rs (Strain): New struct.
(get_group_strains): New function. Joins Strain and StrainXRef by
group Id, optionally filtered to strains used for mapping, matching
gn-guile's gn.data.strains:strain-id-names (same InbredSetId and
used-for-mapping? arguments), returning strain names as well as ids.
* src/lib.rs (GroupStrainsParams): New struct, the used_for_mapping
query parameter.
(group_strains): New handler for GET /groups/{group}/strains. Looks
the group up the same way group_by_name does, for the same 404 shape
on an unknown group.
(app): Register it.
* tests/group_strains.rs, test/python/test_group_strains.py: New
files.
* tests/openapi.rs, test/python/test_openapi.py: Check the new path
is in the generated spec.
* README.md (GET /groups/{group}/strains): Document it, including the
used_for_mapping filter excluding BXD's parent strains.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'src')
-rw-r--r--src/db.rs36
-rw-r--r--src/lib.rs65
2 files changed, 100 insertions, 1 deletions
diff --git a/src/db.rs b/src/db.rs
index 3d46477..28251d7 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -170,3 +170,39 @@ pub fn get_group_by_name(name: &str) -> Result<Option<GroupDetail>, mysql::Error
         mysql::params! { "name" => name },
     )
 }
+
+/// One strain/sample belonging to a group, matching gn-guile's
+/// gn.data.strains:strain-id-names.
+#[derive(Serialize, utoipa::ToSchema)]
+pub struct Strain {
+    pub id: u32,
+    pub name: String,
+    pub used_for_mapping: bool,
+}
+
+/// Fetch the strains/samples belonging to the group with the given
+/// InbredSet Id, optionally restricted to those used for mapping.
+/// Mirrors gn-guile's gn.data.strains:strain-id-names, which takes the
+/// same InbredSetId and used-for-mapping? arguments; unlike it, this
+/// returns strain names as well as ids.
+pub fn get_group_strains(
+    group_id: u16,
+    used_for_mapping: Option<bool>,
+) -> Result<Vec<Strain>, mysql::Error> {
+    let pool = Pool::new(Opts::from_url(&sql_uri())?)?;
+    let mut conn = pool.get_conn()?;
+    conn.exec_map(
+        "SELECT StrainId, Strain.Name, Used_for_mapping \
+         FROM Strain, StrainXRef \
+         WHERE StrainXRef.StrainId = Strain.Id AND StrainXRef.InbredSetId = :group_id \
+                AND (:used_for_mapping IS NULL \
+                     OR Used_for_mapping = IF(:used_for_mapping, 'Y', 'N')) \
+         ORDER BY StrainId",
+        mysql::params! { "group_id" => group_id, "used_for_mapping" => used_for_mapping },
+        |(id, name, used_for_mapping): (u32, String, String)| Strain {
+            id,
+            name,
+            used_for_mapping: used_for_mapping == "Y",
+        },
+    )
+}
diff --git a/src/lib.rs b/src/lib.rs
index 00268bb..8a9142e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-use axum::extract::{Extension, Path};
+use axum::extract::{Extension, Path, Query};
 use axum::http::StatusCode;
 use axum::{Json, Router};
 use serde::Serialize;
@@ -30,6 +30,7 @@ pub fn app() -> Router {
         .routes(routes!(species_by_name))
         .routes(routes!(groups))
         .routes(routes!(group_by_name))
+        .routes(routes!(group_strains))
         .split_for_parts();
     router
         .route("/openapi.json", axum::routing::get(|| async { Json(api) }))
@@ -239,3 +240,65 @@ async fn group_by_name(
             .with_next("/groups")
         })
 }
+
+#[derive(serde::Deserialize)]
+struct GroupStrainsParams {
+    used_for_mapping: Option<bool>,
+}
+
+#[utoipa::path(get, path = "/groups/{group}/strains", params(
+    ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
+    ("used_for_mapping" = Option<bool>, Query, description = "Only strains/samples used for mapping")
+), responses(
+    (status = 200, description = "The strains/samples belonging to the group", body = Vec<db::Strain>),
+    (status = 404, description = "No matching group", body = ApiError),
+    (status = 500, description = "Database error", body = ApiError)
+))]
+async fn group_strains(
+    Extension(request_id): Extension<RequestId>,
+    Path(group): Path<String>,
+    Query(params): Query<GroupStrainsParams>,
+) -> Result<Json<Vec<db::Strain>>, ApiError> {
+    let rid = request_id_string(&request_id);
+    let not_found_name = group.clone();
+    let used_for_mapping = params.used_for_mapping;
+    tokio::task::spawn_blocking(move || {
+        db::get_group_by_name(&group).and_then(|group| match group {
+            Some(group) => db::get_group_strains(group.id, used_for_mapping).map(Some),
+            None => Ok(None),
+        })
+    })
+    .await
+    .map_err(|e| {
+        ApiError::new(
+            StatusCode::INTERNAL_SERVER_ERROR,
+            "internal_error",
+            "Internal error",
+            e.to_string(),
+            "group_strains",
+        )
+        .with_request_id(rid.clone())
+    })?
+    .map_err(|e| {
+        ApiError::new(
+            StatusCode::INTERNAL_SERVER_ERROR,
+            "database_error",
+            "Database error",
+            e.to_string(),
+            "group_strains",
+        )
+        .with_request_id(rid.clone())
+    })?
+    .map(Json)
+    .ok_or_else(|| {
+        ApiError::new(
+            StatusCode::NOT_FOUND,
+            "group_not_found",
+            "Group not found",
+            format!("No group matching '{not_found_name}'."),
+            "group_strains",
+        )
+        .with_request_id(rid)
+        .with_next("/groups")
+    })
+}