about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-10 21:39:01 +0000
committerPjotr <wrk@bosa.lan>2026-11-10 21:39:01 +0000
commit0253fa66f2ef464d56324c27ba51cec1e27e3108 (patch)
treeb02e9db8590c5974ff5ae2a00d01ae18e2bea7b2 /src
parent92a7d0282897dd056b5694a20b2aee7cdc1ce6e3 (diff)
downloadgenenetwork4-0253fa66f2ef464d56324c27ba51cec1e27e3108.tar.gz
uploads: map an uploaded genotype by name, and clean up after it
Three follow-ups from putting an upload through the whole loop.

  * /compute/qtl2's `genotype_file` branch only accepted positional
    `trait_values`, i.e. it demanded the file's column order -- which a
    caller with an uploaded population does not have. It now takes
    `samples` (name and value) or a `basket` and aligns them onto the
    cross by name, leaving individuals without a value as missing.
    Positional `trait_values` still work for a file whose order is known.
  * DELETE /uploads/genotypes/{name} now removes the .geno it wrote on
    demand: a file nobody can name is a file that confuses the next
    operator.
  * scripts/example-genotype-upload.py spreads its markers across
    chromosomes. One chromosome of markers is a poor example -- a
    leave-one-chromosome-out kinship has nothing to leave out, so the
    scan came back with no rows at all, which looked like a broken upload
    and was really a property of the example. 20 markers over the genome
    map cleanly (19 rows, the 20th used for the HMM), and the README says
    to use "kinship": "overall" if a file really has one chromosome.
Diffstat (limited to 'src')
-rw-r--r--src/genoupload.rs7
-rw-r--r--src/routes/qtl2.rs80
2 files changed, 68 insertions, 19 deletions
diff --git a/src/genoupload.rs b/src/genoupload.rs
index 54bc513..61be5f9 100644
--- a/src/genoupload.rs
+++ b/src/genoupload.rs
@@ -504,6 +504,13 @@ pub fn contents(dir: &Path, owner: &str, name: &str) -> Result<String, BasketErr
 
 /// Delete an upload.
 pub fn delete(dir: &Path, owner: &str, name: &str) -> Result<(), BasketError> {
+    // The .geno written on demand goes too: a file nobody can name is a file
+    // that will confuse the next operator.
+    let written = dir
+        .join("genotypes")
+        .join(crate::baskets::file_stem(owner))
+        .join(format!("{}.geno", crate::baskets::file_stem(name)));
+    let _ = std::fs::remove_file(written);
     let connection = crate::baskets::open(dir, owner)?;
     let changed = connection
         .execute(
diff --git a/src/routes/qtl2.rs b/src/routes/qtl2.rs
index 833511f..52fc118 100644
--- a/src/routes/qtl2.rs
+++ b/src/routes/qtl2.rs
@@ -321,13 +321,28 @@ pub async fn compute_qtl2(
         }
         (Some(file), None) => {
             let values = request.trait_values.clone().unwrap_or_default();
-            if values.is_empty() {
+            // Values by name are the natural thing to give for an uploaded
+            // genotype: the caller has a trait's values for its animals, not
+            // the file's column order. Positional `trait_values` still work,
+            // for a file whose order the caller does know.
+            let given: Vec<Sample> = request
+                .samples
+                .clone()
+                .unwrap_or_default()
+                .into_iter()
+                .map(|sample| Sample {
+                    name: sample.name,
+                    value: sample.value,
+                })
+                .collect();
+            let by_name = !given.is_empty();
+            if values.is_empty() && !by_name {
                 return Err(ApiError::new(
                     StatusCode::UNPROCESSABLE_ENTITY,
                     "missing_trait_values",
                     "No trait values",
-                    "with an explicit genotype_file, give `trait_values` (one per individual, \
-                     in the file's order)."
+                    "with an explicit genotype_file, give `samples` (name and value), a `basket`, \
+                     or `trait_values` (one per individual, in the file's order)."
                         .to_string(),
                     "compute_qtl2",
                 )
@@ -349,14 +364,18 @@ pub async fn compute_qtl2(
             // The cross's own individual order is the only name source a
             // path request has; the values are positional over it, so they
             // are named after the conversion (below), not here.
-            let samples: Vec<Sample> = values
-                .into_iter()
-                .enumerate()
-                .map(|(index, value)| Sample {
-                    name: format!("individual-{}", index + 1),
-                    value,
-                })
-                .collect();
+            let samples: Vec<Sample> = if by_name {
+                given
+            } else {
+                values
+                    .into_iter()
+                    .enumerate()
+                    .map(|(index, value)| Sample {
+                        name: format!("individual-{}", index + 1),
+                        value,
+                    })
+                    .collect()
+            };
             (Source::Convert(resolved), samples)
         }
         (Some(_), Some(_)) => {
@@ -461,14 +480,37 @@ pub async fn compute_qtl2(
     let samples: Vec<Sample> = match &source {
         // A path request's values were positional: give them the cross's own
         // individual names, which is what the script aligns on.
-        Source::Convert(_) if request_group.is_none() => samples
-            .into_iter()
-            .zip(cross.individuals.iter())
-            .map(|(sample, name)| Sample {
-                name: name.clone(),
-                value: sample.value,
-            })
-            .collect(),
+        // A path request's values were positional (or already named, for a
+        // basket or `samples`): align them onto the cross either way, so the
+        // script sees one entry per individual and missing ones as `None`.
+        Source::Convert(_) if request_group.is_none() => {
+            let positional = samples
+                .iter()
+                .all(|sample| sample.name.starts_with("individual-"));
+            if positional && samples.len() == cross.individuals.len() {
+                samples
+                    .into_iter()
+                    .zip(cross.individuals.iter())
+                    .map(|(sample, name)| Sample {
+                        name: name.clone(),
+                        value: sample.value,
+                    })
+                    .collect()
+            } else {
+                let by_name: std::collections::HashMap<&str, Option<f64>> = samples
+                    .iter()
+                    .map(|sample| (sample.name.as_str(), sample.value))
+                    .collect();
+                cross
+                    .individuals
+                    .iter()
+                    .map(|name| Sample {
+                        name: name.clone(),
+                        value: by_name.get(name.as_str()).copied().flatten(),
+                    })
+                    .collect()
+            }
+        }
         Source::Convert(_) => samples,
         // A shipped cross is its own population: keep the values for the
         // individuals it holds, and say how many fell outside it.