about summary refs log tree commit diff
path: root/src/response.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-16 00:30:42 +0000
committerPjotr <wrk@bosa.lan>2026-11-16 00:30:42 +0000
commit1ae2f5d712e18c016fdb7bd04da99eab4da6915b (patch)
tree31bb20e52585506351d81198b65c13ec3a229af0 /src/response.rs
parent9c3cd6f2300bb290ddd05c5a46c5b813b22c0ce1 (diff)
downloadgenenetwork4-1ae2f5d712e18c016fdb7bd04da99eab4da6915b.tar.gz
qtl2: permutations for the LOD thresholds
POST /compute/qtl2 gains nperm, perm_seed and perm_strata, and the reply
gains meta.permutation {n, seed, significant, suggestive, values} -- the
genome-wide maximum of every permutation, so a histogram needs no second
call. `data` stays the positional rows array on purpose: /plot/rqtl posts
it straight back and the monitor counts its rows, so the null went beside
the rows rather than around them (a generic Envelope::with_meta/Meta::extra,
skip_serializing_if, so no other endpoint's response changes).

Thresholds come from R/qtl2's scan1perm over the same genotype
probabilities, kinship and covariates as the scan, with chr_lengths from
the map -- without those it derives chromosome lengths from marker
positions and the numbers stop being comparable with R/qtl's. The
permutation CSV is written in R/qtl's two-column shape so one parser and
one percentile routine serve both engines: PermutationResult::from_values
is now shared with /compute/rqtl instead of duplicated.

Semantics, each with the note that says it:

* scan1perm shuffles phenotypes across individuals -- right for an
  outbred cross, only approximate for a selfing RIL panel, where qtl2perm
  is the correct package (not installed here, and not packaged);
* perm_strata takes a case attribute name (sex, Epoch, ...) and permutes
  within it, because shuffling across sexes inflates the null;
* fewer than 100 permutations gets a note that the 0.05 quantile is a
  quantile of too few values; more than 10 000 is refused, since each
  permutation is a whole genome scan;
* a seed is reported always -- without one, two identical requests would
  give different thresholds.

Cost, measured on BXD trait 10001 (34 phenotyped, 20 chromosomes, LOCO
kinship, 4 cores): 20 permutations 0.6 s, 100 in 2.2 s, 500 in 14 s,
1000 in 24 s through the API; 0.022-0.03 s per permutation. Thresholds
converge as expected (0.05: 4.331 at 100 and 200 permutations, 4.374 at
1000 on the raw probe) and are reproducible: the same seed gives the same
numbers on 1 and 4 cores.

Four things that only surfaced by running it, all commented where they
bit: optparse keeps the dash (--perm-out is opt[["perm-out"]], so
opt$perm_out was always NULL and the file landed somewhere the caller did
not look); perm_strata must be a named character vector, not a factor;
the LOD in the CSV is the LAST numeric field (R/qtl's shape puts the
permutation index first, and a first-field parser reads that as the LOD);
and percentiles interpolate at (n-1)p/100, so 100 maxima give 0.6733, not
0.67.

bin/monitor-gn4 gains compute-qtl2-perm (nperm 10, judging
meta.permutation) so a deploy that loses the R path shows up as a red
check rather than a surprise.
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/response.rs b/src/response.rs
index 556a0f8..5e6fcff 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -53,6 +53,13 @@ pub struct Meta {
     /// to count it itself.
     #[serde(skip_serializing_if = "Option::is_none")]
     pub num_cols: Option<usize>,
+    /// Endpoint-specific additions, flattened into `meta` so the shape stays
+    /// `meta.<name>` and never a nested `meta.extra`. Used where something
+    /// belongs beside the rows rather than around them: `/compute/qtl2`'s
+    /// permutation null is qualifier information about the scan in `data`.
+    #[serde(flatten, skip_serializing_if = "Option::is_none")]
+    #[schema(inline)]
+    pub extra: Option<serde_json::Map<String, serde_json::Value>>,
 }
 
 /// A success response: the payload, facts about the response, notes on
@@ -170,6 +177,16 @@ impl<T: Serialize + utoipa::ToSchema> Envelope<T> {
         self
     }
 
+    /// Adds one `meta.<key>` entry, for endpoint-specific facts that qualify
+    /// `data` (see `Meta::extra`).
+    pub fn with_meta(mut self, key: &str, value: serde_json::Value) -> Self {
+        self.meta
+            .extra
+            .get_or_insert_with(serde_json::Map::new)
+            .insert(key.to_string(), value);
+        self
+    }
+
     /// Marks the response `partial` and records why in `notes`.
     pub fn partial(mut self, note: &str) -> Self {
         self.meta.state = State::Partial;
@@ -246,6 +263,7 @@ impl Ctx {
                 columns: None,
                 num_rows: None,
                 num_cols: None,
+                extra: None,
             },
             notes: Vec::new(),
             next: Vec::new(),