diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-03 16:14:49 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-04 06:31:16 +0000 |
| commit | e7191463db66fcc3c2618ccdf004e798e1d3ac6a (patch) | |
| tree | 2e76f71985ee8b6a49c49c1c17606fffbceb056f /src | |
| parent | 0980c21ff71bf4c8fa73d3193460f7b7f69d9f6b (diff) | |
| download | genenetwork4-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')
| -rw-r--r-- | src/db.rs | 184 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/routes/datasets.rs | 213 | ||||
| -rw-r--r-- | src/routes/groups.rs | 55 | ||||
| -rw-r--r-- | src/routes/mod.rs | 1 |
5 files changed, 457 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 }, + ) +} diff --git a/src/lib.rs b/src/lib.rs index 93485a8..6b1d3a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,10 @@ pub fn app() -> Router { .routes(routes!(routes::groups::group_strains)) .routes(routes!(routes::groups::group_trait_values)) .routes(routes!(routes::groups::group_trait_info)) + .routes(routes!(routes::groups::group_datasets)) + .routes(routes!(routes::datasets::dataset_by_name)) + .routes(routes!(routes::datasets::dataset_probeset_info)) + .routes(routes!(routes::datasets::dataset_probeset_values)) .routes(routes!(routes::correlation::correlation)) .split_for_parts(); router diff --git a/src/routes/datasets.rs b/src/routes/datasets.rs new file mode 100644 index 0000000..f1b5671 --- /dev/null +++ b/src/routes/datasets.rs @@ -0,0 +1,213 @@ +//! Expression (ProbeSet) dataset domain endpoints: read-only access to +//! the ProbeSetFreeze/ProbeSetXRef/ProbeSet tables. + +use axum::Json; +use axum::extract::{Extension, Path}; +use axum::http::StatusCode; +use tower_http::request_id::RequestId; + +use crate::db; +use crate::error::ApiError; +use crate::request_id_string; + +#[utoipa::path(get, path = "/datasets/{dataset}", summary = "Get one expression dataset's full record by name or numeric id", params( + ("dataset" = String, Path, description = "Dataset Name, Name2, FullName, ShortName or numeric Id") +), responses( + (status = 200, description = "The matching row of the ProbeSetFreeze table", body = db::Dataset), + (status = 404, description = "No matching dataset", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn dataset_by_name( + Extension(request_id): Extension<RequestId>, + Path(dataset): Path<String>, +) -> Result<Json<db::Dataset>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_name = dataset.clone(); + tokio::task::spawn_blocking(move || db::get_dataset_by_name(&dataset)) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "dataset_by_name", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "dataset_by_name", + ) + .with_request_id(rid.clone()) + })? + .map(Json) + .ok_or_else(|| { + ApiError::new( + StatusCode::NOT_FOUND, + "dataset_not_found", + "Dataset not found", + format!("No dataset matching '{not_found_name}'."), + "dataset_by_name", + ) + .with_request_id(rid) + }) +} + +/// What looking up a probeset trait within a dataset found, the +/// expression-dataset equivalent of GroupTraitLookup above. +enum DatasetProbesetLookup<T> { + DatasetNotFound, + ProbesetNotFound, + Found(T), +} + +fn get_dataset_probeset_info_result( + dataset: &str, + probeset: &str, +) -> Result<DatasetProbesetLookup<db::ProbeSetInfo>, mysql::Error> { + match db::get_dataset_by_name(dataset)? { + None => Ok(DatasetProbesetLookup::DatasetNotFound), + Some(dataset) => match db::get_dataset_probeset_info(dataset.id, probeset)? { + None => Ok(DatasetProbesetLookup::ProbesetNotFound), + Some(info) => Ok(DatasetProbesetLookup::Found(info)), + }, + } +} + +#[utoipa::path(get, path = "/datasets/{dataset}/probesets/{probeset}", summary = "Get one expression trait's ProbeSetXRef/ProbeSet metadata", params( + ("dataset" = String, Path, description = "Dataset Name, Name2, FullName, ShortName or numeric Id"), + ("probeset" = String, Path, description = "ProbeSet.Name within the dataset") +), responses( + (status = 200, description = "The probeset's ProbeSetXRef/ProbeSet metadata", body = db::ProbeSetInfo), + (status = 404, description = "No matching dataset, or no matching probeset in that dataset", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn dataset_probeset_info( + Extension(request_id): Extension<RequestId>, + Path((dataset, probeset)): Path<(String, String)>, +) -> Result<Json<db::ProbeSetInfo>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_dataset = dataset.clone(); + let not_found_probeset = probeset.clone(); + tokio::task::spawn_blocking(move || get_dataset_probeset_info_result(&dataset, &probeset)) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "dataset_probeset_info", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "dataset_probeset_info", + ) + .with_request_id(rid.clone()) + }) + .and_then(|lookup| match lookup { + DatasetProbesetLookup::Found(info) => Ok(Json(info)), + DatasetProbesetLookup::DatasetNotFound => Err(ApiError::new( + StatusCode::NOT_FOUND, + "dataset_not_found", + "Dataset not found", + format!("No dataset matching '{not_found_dataset}'."), + "dataset_probeset_info", + ) + .with_request_id(rid.clone())), + DatasetProbesetLookup::ProbesetNotFound => Err(ApiError::new( + StatusCode::NOT_FOUND, + "probeset_not_found", + "Probeset not found", + format!("No probeset '{not_found_probeset}' in dataset '{not_found_dataset}'."), + "dataset_probeset_info", + ) + .with_request_id(rid)), + }) +} + +fn get_dataset_probeset_values_result( + dataset: &str, + probeset: &str, +) -> Result<DatasetProbesetLookup<Vec<db::ProbeSetValue>>, mysql::Error> { + match db::get_dataset_by_name(dataset)? { + None => Ok(DatasetProbesetLookup::DatasetNotFound), + Some(dataset) => { + let values = db::get_dataset_probeset_values(dataset.id, probeset)?; + if values.is_empty() { + Ok(DatasetProbesetLookup::ProbesetNotFound) + } else { + Ok(DatasetProbesetLookup::Found(values)) + } + } + } +} + +#[utoipa::path(get, path = "/datasets/{dataset}/probesets/{probeset}/values", summary = "Get one expression trait's value in each strain in the dataset", params( + ("dataset" = String, Path, description = "Dataset Name, Name2, FullName, ShortName or numeric Id"), + ("probeset" = String, Path, description = "ProbeSet.Name within the dataset") +), responses( + (status = 200, description = "The dataset's per-strain values for the probeset", body = Vec<db::ProbeSetValue>), + (status = 404, description = "No matching dataset, or no matching probeset in that dataset", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn dataset_probeset_values( + Extension(request_id): Extension<RequestId>, + Path((dataset, probeset)): Path<(String, String)>, +) -> Result<Json<Vec<db::ProbeSetValue>>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_dataset = dataset.clone(); + let not_found_probeset = probeset.clone(); + tokio::task::spawn_blocking(move || get_dataset_probeset_values_result(&dataset, &probeset)) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "dataset_probeset_values", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "dataset_probeset_values", + ) + .with_request_id(rid.clone()) + }) + .and_then(|lookup| match lookup { + DatasetProbesetLookup::Found(values) => Ok(Json(values)), + DatasetProbesetLookup::DatasetNotFound => Err(ApiError::new( + StatusCode::NOT_FOUND, + "dataset_not_found", + "Dataset not found", + format!("No dataset matching '{not_found_dataset}'."), + "dataset_probeset_values", + ) + .with_request_id(rid.clone())), + DatasetProbesetLookup::ProbesetNotFound => Err(ApiError::new( + StatusCode::NOT_FOUND, + "probeset_not_found", + "Probeset not found", + format!("No probeset '{not_found_probeset}' in dataset '{not_found_dataset}'."), + "dataset_probeset_values", + ) + .with_request_id(rid)), + }) +} diff --git a/src/routes/groups.rs b/src/routes/groups.rs index 652940d..18a6f66 100644 --- a/src/routes/groups.rs +++ b/src/routes/groups.rs @@ -308,3 +308,58 @@ pub async fn group_trait_info( .with_request_id(rid)), }) } + +#[utoipa::path(get, path = "/groups/{group}/datasets", summary = "List the expression (ProbeSet) datasets belonging to a group", params( + ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id") +), responses( + (status = 200, description = "The expression (ProbeSet) datasets belonging to the group", body = Vec<db::Dataset>), + (status = 404, description = "No matching group", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn group_datasets( + Extension(request_id): Extension<RequestId>, + Path(group): Path<String>, +) -> Result<Json<Vec<db::Dataset>>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_name = group.clone(); + tokio::task::spawn_blocking(move || { + db::get_group_by_name(&group).and_then(|group| match group { + Some(group) => db::get_group_datasets(group.id).map(Some), + None => Ok(None), + }) + }) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "group_datasets", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "group_datasets", + ) + .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_datasets", + ) + .with_request_id(rid) + .with_next("/groups") + }) +} + diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 4cd3f7b..578761a 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -4,5 +4,6 @@ //! on the router. pub mod correlation; +pub mod datasets; pub mod groups; pub mod species; |
