diff options
| author | Pjotr <wrk@bosa.lan> | 2026-10-11 19:49:57 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-11 19:49:57 +0000 |
| commit | 9e03cf40b875790c19d3fa1623297841b72b553e (patch) | |
| tree | 95b891af0f3015fbdd74243c23c4533fee97ae24 /src/routes | |
| parent | 8e13f31ceca05aa06b8a98b76e1141763b6a4140 (diff) | |
| download | genenetwork4-9e03cf40b875790c19d3fa1623297841b72b553e.tar.gz | |
gemma: add POST /compute/gemma, backed by the real gemma-wrapper/GEMMA
Maps a QTL from a genotype file, a SNP annotation file and trait
values, by running gemma-wrapper's K-matrix step then its LMM
association step, both synchronously (axum/tokio awaits the subprocess
directly, no job queue needed) and returning the parsed .assoc.txt
rows in the standard envelope.
Both steps use a bare --loco (gemma-wrapper derives the chromosome
list from the SNP annotation file). This was arrived at empirically,
not from GeneNetwork3's or GeneNetwork2's own gemma code: run for real
against the BXD fixtures,
- GeneNetwork3's gn3/computations/gemma.py composes a GWA command that
always keeps "-gk" alongside "-lmm"; real GEMMA rejects that
combination outright ("only one of -gk ... -lmm ... options is
allowed"), so none of its gwa-compute/k-gwa-compute endpoints can
produce output. Unsurprising: tests/integration/test_gemma.py is
entirely @unittest.skip, so this was never exercised.
- GeneNetwork2's wqflask/marker_regression/gemma_mapping.py correctly
omits "-gk" from the GWA step, but its non-LOCO branch fails too,
the same way my own first (non-LOCO) attempt did: gemma-wrapper only
ever archives a non-LOCO kinship matrix into a .tar.xz, with no
loose file at the path its own non-LOCO GWA code assumes
("Enforce failed for file_kin ... open file"). Only --loco, which
gemma-wrapper's own USAGE banner documents and which does unpack
that archive, actually works.
A --loco run's .assoc.txt files (one per chromosome) are themselves
only ever inside gemma-wrapper's own output archive, never loose, so
gemma.rs shells out to `tar` (one of gemma-wrapper's own propagated
dependencies) to extract them before concatenating all chromosomes'
rows into the response.
guix.scm now depends on (gn packages gemma) from guix-bioinformatics
for gemma-wrapper (propagated-input: it's invoked as a subprocess, not
linked, and this makes it land in a consuming profile too, same as
GeneNetwork3's and GeneNetwork2's own packages do it). That dependency
needed guix-bioinformatics' own gn/packages/gemma.scm fixed first (see
that repo's fix/gemma-which-shadowing branch) -- gemma-gn2 would not
build at all before that fix.
Verified end to end: `cargo test` passes in full, including a real
(non-mocked, non-skipped) run of gemma-wrapper/GEMMA against the BXD
fixtures copied from genenetwork3/tests/test_data, producing exactly
7320 association rows (one per marker in BXD_snps.txt) in ~19s.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/gemma.rs | 90 | ||||
| -rw-r--r-- | src/routes/mod.rs | 1 |
2 files changed, 91 insertions, 0 deletions
diff --git a/src/routes/gemma.rs b/src/routes/gemma.rs new file mode 100644 index 0000000..1506f1b --- /dev/null +++ b/src/routes/gemma.rs @@ -0,0 +1,90 @@ +//! Compute domain: /compute/gemma invokes gemma-wrapper (and the real +//! GEMMA binary it wraps) to map a QTL from a genotype file, a SNP +//! annotation file and a list of trait values. See `crate::gemma` for +//! the invocation itself. + +use axum::Json; +use axum::http::StatusCode; +use serde::Deserialize; + +use crate::auth::AuthToken; +use crate::config::Config; +use crate::error::{ApiError, Unauthorized}; +use crate::gemma::{self, GemmaError}; +use crate::response::{Ctx, Envelope, RequestInfo}; + +#[derive(Deserialize, utoipa::ToSchema)] +pub struct GemmaRequest { + /// Path to a BIMBAM genotype file (plain text or gzipped), passed + /// as GEMMA's `-g`. + pub genotype_file: String, + /// Path to a SNP annotation file (rs id, position, chromosome), + /// passed as GEMMA's `-a`. + pub snps_file: String, + /// One phenotype value per strain, in the genotype file's strain + /// order; `null` for a missing value. + pub trait_values: Vec<Option<f64>>, + /// GEMMA's `-lmm` model selector: 1 = Wald, 2 = LRT, 3 = score, + /// 4 = all three. Defaults to 4 when omitted. + pub lmm: Option<u8>, +} + +#[utoipa::path(post, path = "/compute/gemma", + summary = "Map a QTL from a genotype file and trait values using GEMMA", + description = "Runs gemma-wrapper's K-matrix step, then its LMM \ +association step, over the given genotype and SNP annotation files and \ +trait values, and returns the resulting rows of GEMMA's own \ +.assoc.txt output.", + request_body = GemmaRequest, + responses( + (status = 200, description = "Mapped QTL rows from GEMMA's .assoc.txt output, as a JSON array of row objects", body = Envelope<serde_json::Value>), + Unauthorized, + (status = 422, description = "lmm out of range (must be 1-4)", body = ApiError), + (status = 502, description = "gemma-wrapper or GEMMA failed", body = ApiError) + ))] +pub async fn compute_gemma( + ctx: Ctx, + _token: AuthToken, + Json(request): Json<GemmaRequest>, +) -> Result<Json<Envelope<serde_json::Value>>, ApiError> { + let rid = ctx.request_info(); + let lmm = request.lmm.unwrap_or(4); + if !(1..=4).contains(&lmm) { + return Err(ApiError::new( + StatusCode::UNPROCESSABLE_ENTITY, + "invalid_lmm", + "Invalid lmm", + format!("lmm must be 1-4 (got {lmm}); 1=Wald, 2=LRT, 3=score, 4=all three."), + "compute_gemma", + ) + .with_request_id(rid)); + } + + let config = Config::load(); + gemma::compute( + &config.gemma_wrapper_cmd, + &config.gemma_cache_dir, + &request.genotype_file, + &request.snps_file, + &request.trait_values, + lmm, + ) + .await + .map(|rows| { + Json(ctx.envelope(rows).with_note( + "Rows come from GEMMA's own .assoc.txt output; column names depend on the lmm value used.", + )) + }) + .map_err(|e| gemma_error_to_api_error(e, rid)) +} + +fn gemma_error_to_api_error(error: GemmaError, rid: RequestInfo) -> ApiError { + ApiError::new( + StatusCode::BAD_GATEWAY, + "gemma_failed", + "GEMMA computation failed", + error.to_string(), + "compute_gemma", + ) + .with_request_id(rid) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 55227e0..3a9f3e3 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -7,6 +7,7 @@ pub mod accessions; pub mod correlation; pub mod datasets; pub mod dev; +pub mod gemma; pub mod groups; pub mod mapping_methods; pub mod publications; |
