diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/db.rs | 139 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/routes/accessions.rs | 116 | ||||
| -rw-r--r-- | src/routes/mapping_methods.rs | 49 | ||||
| -rw-r--r-- | src/routes/mod.rs | 2 |
5 files changed, 295 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, } }, ) diff --git a/src/lib.rs b/src/lib.rs index 21ad242..63c25a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,6 +53,8 @@ pub fn app() -> Router { let mut api_router = OpenApiRouter::with_openapi(ApiDoc::openapi()) .routes(routes!(hello_world)) .routes(routes!(version)) + .routes(routes!(routes::accessions::accession)) + .routes(routes!(routes::mapping_methods::mapping_methods)) .routes(routes!(routes::species::species)) .routes(routes!(routes::species::species_by_name)) .routes(routes!(routes::species::species_datasets)) diff --git a/src/routes/accessions.rs b/src/routes/accessions.rs new file mode 100644 index 0000000..f4ce3b1 --- /dev/null +++ b/src/routes/accessions.rs @@ -0,0 +1,116 @@ +//! Accessions domain endpoint: GeneNetwork accession numbers ("GN602"). + +use axum::Json; +use axum::extract::Path; +use axum::http::StatusCode; + +use crate::auth::AuthToken; +use crate::db; +use crate::error::{ApiError, Unauthorized}; +use crate::response::{Ctx, Envelope, path_segment}; + +/// The number in "GN602" (the prefix is optional and case-insensitive). +fn parse_accession(text: &str) -> Option<u32> { + let digits = match text.get(..2) { + Some(prefix) if prefix.eq_ignore_ascii_case("gn") => &text[2..], + _ => text, + }; + digits.parse().ok() +} + +#[utoipa::path(get, path = "/accessions/{accession}", summary = "Resolve a GeneNetwork accession number such as GN602 to its dataset", params( + ("accession" = String, Path, description = "The accession number, with or without the GN prefix, e.g. GN602 or 602") +), responses( + (status = 200, description = "The dataset the accession names, with its category and group", body = Envelope<db::Accession>), + Unauthorized, + (status = 400, description = "Not an accession number", body = ApiError), + (status = 404, description = "No dataset has this accession", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn accession( + ctx: Ctx, + Path(accession): Path<String>, + _token: AuthToken, +) -> Result<Json<Envelope<db::Accession>>, ApiError> { + let rid = ctx.request_info(); + let Some(number) = parse_accession(&accession) else { + return Err(ApiError::new( + StatusCode::BAD_REQUEST, + "invalid_parameter", + "Invalid parameter", + format!("'{accession}' is not an accession number; expected e.g. GN602 or 602."), + "accession", + ) + .with_request_id(rid)); + }; + tokio::task::spawn_blocking(move || db::get_accession(number)) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "accession", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "accession", + ) + .with_request_id(rid.clone()) + })? + .map(|found| { + let group = found.group.as_deref().map(path_segment); + let name = path_segment(&found.name); + let category = found.category.clone(); + let mut envelope = ctx.envelope(found); + if let Some(group) = &group { + envelope = envelope.with_next(&format!("/groups/{group}"), "The group the dataset belongs to"); + } + match (category.as_deref(), &group) { + (Some("probeset"), _) => { + envelope = envelope.with_next(&format!("/datasets/{name}"), "The expression dataset"); + } + (Some("publish"), Some(group)) => { + envelope = envelope.with_next( + &format!("/groups/{group}/traits"), + "The dataset's published traits (can be very large)", + ); + } + _ => {} + } + Json(envelope) + }) + .ok_or_else(|| { + ApiError::new( + StatusCode::NOT_FOUND, + "accession_not_found", + "Accession not found", + format!("No dataset has accession {accession}."), + "accession", + ) + .with_request_id(rid) + }) +} + +#[cfg(test)] +mod tests { + use super::parse_accession; + + #[test] + fn accessions_parse_with_or_without_the_gn_prefix() { + assert_eq!(parse_accession("GN602"), Some(602)); + assert_eq!(parse_accession("gn602"), Some(602)); + assert_eq!(parse_accession("602"), Some(602)); + assert_eq!(parse_accession("GN"), None); + assert_eq!(parse_accession("GNx"), None); + assert_eq!(parse_accession("BXDPublish"), None); + assert_eq!(parse_accession(""), None); + } +} diff --git a/src/routes/mapping_methods.rs b/src/routes/mapping_methods.rs new file mode 100644 index 0000000..9805fe8 --- /dev/null +++ b/src/routes/mapping_methods.rs @@ -0,0 +1,49 @@ +//! Mapping methods domain endpoint: the mapping tools GeneNetwork offers. + +use axum::Json; +use axum::http::StatusCode; + +use crate::auth::AuthToken; +use crate::db; +use crate::error::{ApiError, Unauthorized}; +use crate::response::{Ctx, Envelope}; + +#[utoipa::path(get, path = "/mapping-methods", summary = "List the mapping tools (QTL mapping methods) GeneNetwork offers", responses( + (status = 200, description = "The MappingMethod rows, including tools no group currently uses", body = Envelope<Vec<db::MappingMethod>>), + Unauthorized, + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn mapping_methods( + ctx: Ctx, + _token: AuthToken, +) -> Result<Json<Envelope<Vec<db::MappingMethod>>>, ApiError> { + let rid = ctx.request_info(); + tokio::task::spawn_blocking(db::get_mapping_methods) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "mapping_methods", + ) + .with_request_id(rid.clone()) + })? + .map(|methods| { + Json(ctx.envelope(methods).with_next( + "/groups", + "Groups, each with the mapping tool offered for it (`mapping_method`)", + )) + }) + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "mapping_methods", + ) + .with_request_id(rid) + }) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 0362206..55227e0 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,10 +3,12 @@ //! and utoipa annotations for one domain; lib.rs just registers them //! on the router. +pub mod accessions; pub mod correlation; pub mod datasets; pub mod dev; pub mod groups; +pub mod mapping_methods; pub mod publications; pub mod search; pub mod species; |
