about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-15 12:04:46 +0000
committerPjotr <wrk@bosa.lan>2026-10-15 12:04:46 +0000
commitacdcb3a7ddfdfadf91531599f87190c451fd8624 (patch)
treecfc9b9a381842b27ff98750ceaa93272c7a7aee0
parent6de7a3440048f870463f0c4ceb6efa86dc2d57da (diff)
downloadgenenetwork4-acdcb3a7ddfdfadf91531599f87190c451fd8624.tar.gz
rqtl: return marker rows positionally, columns in meta.columns
Same space-saving shape as the gemma commit: /compute/rqtl's markers
are now positional arrays (name, chr, pos, lod, plus any 4-way-cross
extra effect columns trailing) instead of one keyed object per row,
with the fixed column names in meta.columns.

Also fixes tests/gemma.rs, which still asserted the old keyed shape
from the gemma change -- missed in that commit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
-rw-r--r--README.md23
-rw-r--r--src/routes/rqtl.rs11
-rw-r--r--src/rqtl.rs27
-rw-r--r--tests/gemma.rs21
-rw-r--r--tests/rqtl.rs8
5 files changed, 70 insertions, 20 deletions
diff --git a/README.md b/README.md
index a618690..466bbf1 100644
--- a/README.md
+++ b/README.md
@@ -1060,14 +1060,19 @@ you.
 {
   "data": {
     "markers": [
-      {"name": "rs31443144", "chr": "1", "pos": 3.010274, "lod": 0.0923496},
-      {"name": "rs6269442", "chr": "1", "pos": 3.492195, "lod": 0.1131095}
+      ["rs31443144", "1", 3.010274, 0.0923496],
+      ["rs6269442", "1", 3.492195, 0.1131095]
     ],
     "permutation": null
   },
-  "meta": {"state": "complete", "took_ms": 420, "request_id": "..."},
+  "meta": {
+    "state": "complete",
+    "took_ms": 420,
+    "request_id": "...",
+    "columns": ["name", "chr", "pos", "lod"]
+  },
   "notes": [
-    "markers come from R/qtl's scanone output; permutation is null unless nperm > 0 was requested.",
+    "markers come from R/qtl's scanone output, positional (see meta.columns for names); permutation is null unless nperm > 0 was requested.",
     "Rscript scripts/rqtl_wrapper.R --geno .../rqtl_sample1.geno --pheno .../pheno.txt --model normal --method hk --nperm 0 --scale mb --outdir /tmp/gn4-rqtl --filename <hash>-output.csv"
   ],
   "next": []
@@ -1075,8 +1080,14 @@ you.
 ```
 
 (Real output, against the tiny 6-marker/6-sample BXD riset fixture in
-`tests/test_data/`, trimmed to two markers. `permutation` is `null`
-unless `nperm` was set above 0; when it is, it's
+`tests/test_data/`, trimmed to two markers. Each marker row is
+positional, in `meta.columns`' order, rather than a
+`{"name": ..., "chr": ..., ...}` object per row -- same
+space-saving shape `/compute/gemma` uses for its own (much larger)
+rows; a 4-way cross's extra mean/SE effect columns, if present, trail
+each row and aren't named in `meta.columns` (see `MarkerResult`'s doc
+comment in `src/rqtl.rs`). `permutation` is `null` unless `nperm` was
+set above 0; when it is, it's
 `{"values": [...], "suggestive": ..., "significant": ...}`, the 67th-
 and 95th-percentile thresholds GeneNetwork3 itself uses -- computed
 here with `numpy.percentile`'s default linear interpolation,
diff --git a/src/routes/rqtl.rs b/src/routes/rqtl.rs
index 4535d30..638557c 100644
--- a/src/routes/rqtl.rs
+++ b/src/routes/rqtl.rs
@@ -73,7 +73,7 @@ concurrently -- same shape as /compute/gemma.",
     params(ComputeRqtlParams),
     request_body = RqtlRequest,
     responses(
-        (status = 200, description = "Mapped QTL markers (and, if requested, permutation thresholds)", body = Envelope<serde_json::Value>),
+        (status = 200, description = "Mapped QTL markers, as positional arrays (column names in meta.columns), and, if requested, permutation thresholds", body = Envelope<serde_json::Value>),
         Unauthorized,
         (status = 409, description = "id is already in use by another run", body = ApiError),
         (status = 422, description = "id not alnum/-/_ (max 64 chars)", body = ApiError),
@@ -122,9 +122,12 @@ pub async fn compute_rqtl(
     .await
     .map(|output| {
         let notes = output.notes.clone();
-        let mut envelope = ctx.envelope(rqtl::to_json(output)).with_note(
-            "markers come from R/qtl's scanone output; permutation is null unless nperm > 0 was requested.",
-        );
+        let mut envelope = ctx
+            .envelope(rqtl::to_json(output))
+            .with_columns(rqtl::MARKER_COLUMNS.iter().map(|s| s.to_string()).collect())
+            .with_note(
+                "markers come from R/qtl's scanone output, positional (see meta.columns for names); permutation is null unless nperm > 0 was requested.",
+            );
         for note in &notes {
             envelope = envelope.with_note(note);
         }
diff --git a/src/rqtl.rs b/src/rqtl.rs
index bac386d..9814efd 100644
--- a/src/rqtl.rs
+++ b/src/rqtl.rs
@@ -354,12 +354,35 @@ pub async fn compute(
     })
 }
 
+/// `MarkerResult`'s fixed fields, in the order `to_json` lays out each
+/// marker row -- the names a caller matches `markers`' positional rows
+/// up against (a 4-way cross's `extra` effect columns, if present,
+/// trail these and aren't named here; see `MarkerResult`'s own doc
+/// comment).
+pub const MARKER_COLUMNS: [&str; 4] = ["name", "chr", "pos", "lod"];
+
 /// `RqtlOutput` as the single JSON value the route returns -- see
 /// `crate::gemma`'s own note on why `serde_json::Value`, not a `Vec`,
-/// is what utoipa needs here.
+/// is what utoipa needs here. `markers` rows are positional (see
+/// `MARKER_COLUMNS`), the same space-saving shape `crate::gemma::compute`
+/// uses for its own (much larger) marker rows.
 pub fn to_json(output: RqtlOutput) -> Value {
+    let markers: Vec<Value> = output
+        .markers
+        .into_iter()
+        .map(|m| {
+            let mut row = vec![
+                Value::String(m.name),
+                Value::String(m.chr),
+                serde_json::json!(m.pos),
+                serde_json::json!(m.lod),
+            ];
+            row.extend(m.extra.into_iter().map(Value::String));
+            Value::Array(row)
+        })
+        .collect();
     serde_json::json!({
-        "markers": output.markers,
+        "markers": markers,
         "permutation": output.permutation,
     })
 }
diff --git a/tests/gemma.rs b/tests/gemma.rs
index 0e0d313..6374023 100644
--- a/tests/gemma.rs
+++ b/tests/gemma.rs
@@ -134,16 +134,27 @@ async fn compute_gemma_maps_a_qtl_from_the_bxd_fixtures() {
     assert!(!rows.is_empty(), "expected at least one mapped marker");
     // BXD_snps.txt has 7320 markers; every one should have a row.
     assert_eq!(rows.len(), 7320);
-    let first = &rows[0];
-    assert!(first.get("chr").is_some(), "{first}");
-    assert!(first.get("rs").is_some(), "{first}");
+
+    // Rows are positional now; look up each column's index via
+    // meta.columns rather than a fixed offset, since it varies with lmm.
+    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
+    let columns: Vec<&str> = envelope["meta"]["columns"]
+        .as_array()
+        .expect("meta.columns")
+        .iter()
+        .map(|c| c.as_str().unwrap())
+        .collect();
+    let index_of = |name: &str| columns.iter().position(|c| *c == name);
+    assert!(index_of("chr").is_some(), "{columns:?}");
+    assert!(index_of("rs").is_some(), "{columns:?}");
     // lmm defaults to 9 (GeneNetwork's convention); observed to behave
     // like LRT, carrying p_lrt rather than p_wald.
-    assert!(first.get("p_lrt").is_some(), "{first}");
+    assert!(index_of("p_lrt").is_some(), "{columns:?}");
+    let first = &rows[0];
+    assert!(first[index_of("chr").unwrap()].is_number(), "{first}");
 
     // The notes carry the exact gemma-wrapper invocations used, not
     // just a generic description.
-    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
     let notes = envelope["notes"].as_array().unwrap();
     let kinship_note = notes.iter().find(|n| n.as_str().unwrap().starts_with("Kinship:"));
     let gwa_note = notes.iter().find(|n| n.as_str().unwrap().starts_with("GWA:"));
diff --git a/tests/rqtl.rs b/tests/rqtl.rs
index c341254..1364eee 100644
--- a/tests/rqtl.rs
+++ b/tests/rqtl.rs
@@ -62,12 +62,14 @@ async fn compute_rqtl_maps_a_qtl_from_the_bxd_riset_fixture() {
     let markers = data["markers"].as_array().unwrap();
     // The fixture has 6 markers (3 per chromosome, 2 chromosomes).
     assert_eq!(markers.len(), 6);
-    assert_eq!(markers[0]["name"], "rs31443144");
-    assert_eq!(markers[0]["chr"], "1");
-    assert!(markers[0]["lod"].as_f64().unwrap() >= 0.0);
+    // Rows are positional (name, chr, pos, lod); see meta.columns.
+    assert_eq!(markers[0][0], "rs31443144");
+    assert_eq!(markers[0][1], "1");
+    assert!(markers[0][3].as_f64().unwrap() >= 0.0);
     assert!(data["permutation"].is_null(), "nperm defaults to 0: {data}");
 
     let envelope: Value = serde_json::from_slice(&bytes).unwrap();
+    assert_eq!(envelope["meta"]["columns"], serde_json::json!(["name", "chr", "pos", "lod"]));
     let notes = envelope["notes"].as_array().unwrap();
     let rscript_note = notes.iter().find(|n| n.as_str().unwrap().starts_with("Rscript"));
     assert!(rscript_note.is_some(), "{notes:?}");