//! `POST /compute/qtl2`: R/qtl2 scans with an LMM and LOCO kinship. //! //! Two levels: the riset fixture (does the endpoint work at all), and a real //! panel (`BXD.4`) cross-checked against `/compute/rqtl` -- an LMM and a //! Haley-Knott scan are different models, so the assertion is that they //! agree about *which chromosome* the peak is on and that the LMM is the //! better-behaved of the two, not that the LODs match. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; fn rscript_available() -> bool { std::process::Command::new("Rscript").arg("--version").output().is_ok() } fn qtl2_available() -> bool { std::process::Command::new("Rscript") .args(["-e", "library(qtl2)"]) .output() .map(|output| output.status.success()) .unwrap_or(false) } fn test_data_path(name: &str) -> String { format!("{}/tests/test_data/{name}", env!("CARGO_MANIFEST_DIR")) } async fn ready() -> Option { if !rscript_available() { eprintln!("skipping: Rscript not on PATH"); return None; } if !qtl2_available() { eprintln!("skipping: r-qtl2 not on the R library path (see guix.scm)"); return None; } match common::auth_token().await { Some(token) => Some(token), None => { eprintln!("skipping: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); None } } } #[tokio::test] async fn a_fixture_panel_scans_into_marker_chr_pos_lod() { let Some(token) = ready().await else { return }; let body = serde_json::json!({ "genotype_file": test_data_path("rqtl_sample1.geno"), "trait_name": "fixture", "trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3], }); let response = common::post_json_with_token("POST /compute/qtl2", "/compute/qtl2", &token, &body).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(envelope["meta"]["columns"], serde_json::json!(["marker", "chr", "pos", "lod"])); let rows = envelope["data"].as_array().unwrap(); assert!(!rows.is_empty(), "the fixture's markers are all scanned"); for row in rows { assert!(row[0].is_string(), "{row}"); assert!(row[2].is_number() && row[3].is_number(), "{row}"); } let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); assert!(notes.iter().any(|note| note.starts_with("Trait: fixture"))); assert!( notes.iter().any(|note| note.starts_with("Cross: riself")), "the cross type is reported: {notes:?}" ); assert!(notes.iter().any(|note| note.starts_with("Rscript "))); } /// The point of the endpoint: on a real panel its LMM finds the same /// chromosome as the Haley-Knott scan, and the response says what ran. #[tokio::test] async fn a_group_panel_scans_and_agrees_with_the_haley_knott_scan() { let Some(token) = ready().await else { return }; if !common::db_available() { eprintln!("skipping a_group_panel_scans_and_agrees_with_the_haley_knott_scan: no usable database"); return; } let response = common::get_with_token( "GET /groups/BXD/traits/10001/values", "/groups/BXD/traits/10001/values", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let samples: Value = common::data(&bytes); let body = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.4", "trait_name": "BXD::10001", "samples": samples, "cores": 4, }); let response = common::post_json_with_token("POST /compute/qtl2 (group)", "/compute/qtl2", &token, &body) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let rows = envelope["data"].as_array().unwrap(); assert!(rows.len() > 3000, "the whole panel is scanned: {}", rows.len()); let peak = rows .iter() .max_by(|left, right| { left[3] .as_f64() .unwrap() .partial_cmp(&right[3].as_f64().unwrap()) .unwrap() }) .unwrap(); assert_eq!(peak[1].as_str().unwrap(), "8", "the peak is on chr8: {peak}"); // And the same trait through the Haley-Knott scan: same chromosome. let body = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.4", "trait_name": "BXD::10001", "samples": samples, }); let response = common::post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &body).await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let scan: Value = serde_json::from_slice(&bytes).unwrap(); let hk_peak = scan["data"]["markers"] .as_array() .unwrap() .iter() .max_by(|left, right| { left[3] .as_f64() .unwrap() .partial_cmp(&right[3].as_f64().unwrap()) .unwrap() }) .unwrap(); assert_eq!(hk_peak[1].as_str().unwrap(), "8", "scanone agrees: {hk_peak}"); } /// Refusals: something to scan, a kinship name that exists, a model that /// exists. #[tokio::test] async fn qtl2_refuses_inputs_it_cannot_scan() { let Some(token) = ready().await else { return }; for (body, code) in [ (serde_json::json!({"trait_values": [1.0, 2.0]}), "missing_genotype"), ( serde_json::json!({ "genotype_file": test_data_path("rqtl_sample1.geno"), "trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3], "kinship": "sometimes" }), "invalid_kinship", ), ] { let response = common::post_json_with_token("POST /compute/qtl2", "/compute/qtl2", &token, &body) .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], code, "{error}"); } } /// A cross that ships its own control file is the only route to a /// multiparent scan, so its refusals matter: the name must exist, the /// samples must belong to the cross, and the two genotype sources cannot /// both be named. #[tokio::test] async fn a_shipped_multiparent_cross_is_named_not_guessed() { let Some(token) = ready().await else { return }; for (body, code) in [ ( serde_json::json!({"rqtl2_cross": "nonesuch", "samples": [{"name": "S1", "value": 1.0}]}), "unsupported_cross", ), (serde_json::json!({"rqtl2_cross": "do"}), "missing_samples"), ( serde_json::json!({"rqtl2_cross": "do", "samples": [{"name": "not-an-individual", "value": 1.0}]}), "no_samples_in_cross", ), ( serde_json::json!({"rqtl2_cross": "do", "group": "BXD", "samples": [{"name": "BXD1", "value": 1.0}]}), "ambiguous_genotype", ), ] { let response = common::post_json_with_token("POST /compute/qtl2 (cross)", "/compute/qtl2", &token, &body) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{error}"); assert_eq!(error["code"], code, "{error}"); } } /// The real thing: the DO, 8 founder alleles, an LMM with LOCO kinship. /// Ignored because the genotype probabilities for 742 individuals over 6 413 /// markers take minutes the first time (then they are cached); run it with /// `cargo test --test qtl2 -- --ignored`. #[tokio::test] #[ignore] async fn a_do_scan_fits_eight_founder_alleles() { let Some(token) = ready().await else { return }; let pheno_path = std::path::Path::new( "/home/wrk/tmp/ai-root/factory/gn4-data/genotype-files/do/do_pheno.csv", ); let Ok(text) = std::fs::read_to_string(pheno_path) else { eprintln!("skipping: {} is not readable", pheno_path.display()); return; }; let mut samples = Vec::new(); for line in text.lines().filter(|line| !line.starts_with('#')) { let fields: Vec<&str> = line.split(',').collect(); if fields.len() < 2 || fields[0] == "id" { continue; } samples.push(serde_json::json!({ "name": fields[0], "value": fields[1].parse::().ok(), })); } assert!(samples.len() > 700, "the DO's 742 individuals: {}", samples.len()); let body = serde_json::json!({ "rqtl2_cross": "do", "trait_name": "DO::WBC", "samples": samples, "cores": 8, }); let response = common::post_json_with_token("POST /compute/qtl2 (DO)", "/compute/qtl2", &token, &body).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let rows = envelope["data"].as_array().unwrap(); assert!(rows.len() > 5000, "the DO's autosomes: {}", rows.len()); let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); let cross_note = notes.iter().find(|note| note.starts_with("Cross: ")).unwrap(); assert!(cross_note.contains("do"), "{cross_note}"); assert!(cross_note.contains("8 founder alleles"), "{cross_note}"); // WBC's known peak region: the scan says something, not a flat line. let best = rows .iter() .map(|row| row[3].as_f64().unwrap()) .fold(0.0_f64, f64::max); assert!(best > 3.0, "a real peak, not noise: {best}"); } /// The two genotype models, and what the reply says about each: `riself` drops /// the heterozygous calls (and says how many), `f2` keeps them as a third /// genotype as a diagnostic. BXD trait 16627 is the case where the difference /// is visible -- see the README's "Why R/qtl and R/qtl2 disagree". #[tokio::test] async fn the_genotype_model_is_reported_and_switchable() { let Some(token) = ready().await else { return }; if !common::db_available() { eprintln!("skipping the_genotype_model_is_reported_and_switchable: no database"); return; } let values = common::get_with_token( "GET /groups/BXD/traits/16627/values", "/groups/BXD/traits/16627/values", &token, ) .await; assert_eq!(values.status(), StatusCode::OK); let bytes = values.into_body().collect().await.unwrap().to_bytes(); let samples: Value = common::data(&bytes); let mut peaks = Vec::new(); for model in [None, Some("f2")] { let mut body = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.8", "trait_name": "BXD::16627", "samples": samples, "cores": 4, }); if let Some(model) = model { body["genotype_model"] = serde_json::json!(model); } let response = common::post_json_with_token( "POST /compute/qtl2 (genotype_model)", "/compute/qtl2", &token, &body, ) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); let cross = notes .iter() .find(|note| note.starts_with("Cross: ")) .copied() .unwrap(); match model { None => { assert!(cross.contains("riself"), "{cross}"); assert!( notes.iter().any(|note| note.starts_with("genotype_model riself: ") && note.contains("heterozygous call(s)")), "the reply counts what it dropped: {notes:?}" ); } Some(_) => { assert!(cross.contains("f2"), "{cross}"); assert!( notes.iter().any(|note| note.contains("kept as a third genotype")), "the reply says it is a diagnostic: {notes:?}" ); } } let rows = envelope["data"].as_array().unwrap(); let peak = rows .iter() .max_by(|left, right| { left[3] .as_f64() .unwrap() .partial_cmp(&right[3].as_f64().unwrap()) .unwrap() }) .unwrap(); peaks.push((cross.to_string(), peak[3].as_f64().unwrap())); } // Keeping the calls recovers the signal the dropped ones cost: the point // of the switch. (Measured 3.7 against 9.7; assert the direction only, // since the phenotypes and the panel may change.) assert!( peaks[1].1 > peaks[0].1 + 1.0, "f2 should find more than riself here: {peaks:?}" ); // An unknown model is refused rather than ignored. let response = common::post_json_with_token( "POST /compute/qtl2 (bad genotype_model)", "/compute/qtl2", &token, &serde_json::json!({ "group": "BXD", "trait_name": "t", "samples": samples, "genotype_model": "genril2-ish", }), ) .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "invalid_het"); }