about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/response.rs9
-rw-r--r--src/routes/plot.rs13
-rw-r--r--src/routes/rqtl.rs12
-rw-r--r--src/rqtl.rs65
4 files changed, 63 insertions, 36 deletions
diff --git a/src/response.rs b/src/response.rs
index c2a6cc9..974a585 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -279,6 +279,15 @@ impl<T: Serialize + utoipa::ToSchema> Envelope<T> {
         self
     }
 
+    /// Puts one endpoint-specific value into `meta.extra` (qualified data that
+    /// belongs beside the rows rather than around them -- the permutation null
+    /// of a scan, say).
+    pub fn with_extra(mut self, key: &str, value: serde_json::Value) -> Self {
+        let extra = self.meta.extra.get_or_insert_with(serde_json::Map::new);
+        extra.insert(key.to_string(), value);
+        self
+    }
+
     /// Attaches [`Timing`] to `meta`.
     pub fn with_timing(mut self, timing: Timing) -> Self {
         self.meta.timing = Some(timing);
diff --git a/src/routes/plot.rs b/src/routes/plot.rs
index d33f5d7..0f698be 100644
--- a/src/routes/plot.rs
+++ b/src/routes/plot.rs
@@ -158,11 +158,16 @@ fn check_gemma_result(posted: &serde_json::Value) -> Result<(), String> {
 /// What `/plot/rqtl` validates in a posted R/qtl result: `data.markers`,
 /// each row `[name, chr, pos, lod, ...]`.
 fn check_rqtl_result(result: &serde_json::Value) -> Result<(), String> {
-    let rows = result["data"]["markers"]
-        .as_array()
-        .ok_or("`data.markers` must be the R/qtl marker array from /compute/rqtl -- post that response back, not a request")?;
+    // `data` is the marker array; a reply from a build before that change kept
+    // it under `data.markers`, and a caller may still be holding one.
+    let rows = if result["data"].is_array() {
+        result["data"].as_array()
+    } else {
+        result["data"]["markers"].as_array()
+    }
+    .ok_or("`data` must be the R/qtl marker array from /compute/rqtl -- post that response back, not a request")?;
     if rows.is_empty() {
-        return Err("`data.markers` is empty: nothing to plot".to_string());
+        return Err("`data` is empty: nothing to plot".to_string());
     }
     let first = rows[0]
         .as_array()
diff --git a/src/routes/rqtl.rs b/src/routes/rqtl.rs
index 84c43e2..0fbfdf4 100644
--- a/src/routes/rqtl.rs
+++ b/src/routes/rqtl.rs
@@ -546,11 +546,16 @@ pub async fn compute_rqtl(
         // responses from the same panel apart, and `/plot/rqtl` reads it
         // for the title.
         let mut envelope = ctx
-            .envelope(rqtl::to_json(output))
+            // `data` is the marker rows, like every other compute endpoint:
+            // the permutation null is qualifier information and goes to
+            // meta.extra, so a caller can write one script for all of them
+            // (measured: Rob Williams's test-drive read data.markers here and
+            // data itself on GEMMA, and had to special-case both).
+            .envelope(rqtl::marker_rows(&output))
             .with_columns(rqtl::MARKER_COLUMNS.iter().map(|s| s.to_string()).collect(), num_rows)
             .with_note(&format!("Trait: {}", request.trait_name))
             .with_note(
-                "markers come from R/qtl's scanone output, positional (see meta.columns for names); permutation is null unless nperm > 0 was requested.",
+                "markers come from R/qtl's scanone output, positional (see meta.columns for names); the permutation null, when nperm > 0 was requested, is meta.permutation.",
             )
             // The conversion is not silent in the status log, but it was
             // silent here: every heterozygous call is dropped and X becomes an
@@ -562,6 +567,9 @@ pub async fn compute_rqtl(
                  panel with heterozygotes at 0.5, so the two are different models, not two \
                  implementations of one.",
             );
+        if let Some(permutation) = rqtl::permutation_json(&output) {
+            envelope = envelope.with_extra("permutation", permutation);
+        }
         for note in &notes {
             envelope = envelope.with_note(note);
         }
diff --git a/src/rqtl.rs b/src/rqtl.rs
index 2bf6de3..7dd608d 100644
--- a/src/rqtl.rs
+++ b/src/rqtl.rs
@@ -495,32 +495,39 @@ pub const MARKER_COLUMNS: [&str; 4] = ["name", "chr", "pos", "lod"];
 /// 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": markers,
-        "permutation": output.permutation,
-    })
+pub fn marker_rows(output: &RqtlOutput) -> Value {
+    Value::Array(
+        output
+            .markers
+            .iter()
+            .map(|m| {
+                let mut row = vec![
+                    Value::String(m.name.clone()),
+                    Value::String(m.chr.clone()),
+                    serde_json::json!(m.pos),
+                    serde_json::json!(m.lod),
+                ];
+                row.extend(m.extra.iter().cloned().map(Value::String));
+                Value::Array(row)
+            })
+            .collect(),
+    )
+}
+
+/// The permutation null, for `meta.extra` beside the rows -- it qualifies the
+/// scan rather than being one of its rows, which is why it is not in `data`
+/// (that is where every other compute endpoint keeps its rows).
+pub fn permutation_json(output: &RqtlOutput) -> Option<Value> {
+    output
+        .permutation
+        .as_ref()
+        .and_then(|permutation| serde_json::to_value(permutation).ok())
 }
 
 /// The marker with the strongest LOD in a `to_json` result, if there is
 /// one: the QTL's peak, which is what a PheWAS wants to be centred on.
 pub fn peak_marker(data: &Value) -> Option<String> {
-    data.get("markers")?
-        .as_array()?
+    data.as_array()?
         .iter()
         .filter_map(|row| {
             let row = row.as_array()?;
@@ -661,16 +668,14 @@ mod tests {
 
     #[test]
     fn the_peak_marker_is_the_strongest_lod() {
-        let data = serde_json::json!({
-            "markers": [
-                ["rs1", "1", 3.0, 0.5],
-                ["rs2", "3", 9.0, 4.2],
-                ["rs3", "5", 12.0, 1.1]
-            ],
-            "permutation": null
-        });
+        // `data` is the rows themselves; the permutation null is in meta.
+        let data = serde_json::json!([
+            ["rs1", "1", 3.0, 0.5],
+            ["rs2", "3", 9.0, 4.2],
+            ["rs3", "5", 12.0, 1.1]
+        ]);
         assert_eq!(peak_marker(&data).as_deref(), Some("rs2"));
-        assert_eq!(peak_marker(&serde_json::json!({"markers": []})), None);
+        assert_eq!(peak_marker(&serde_json::json!([])), None);
         assert_eq!(peak_marker(&serde_json::json!({})), None);
     }