//! `POST /compute/fitqtl`: effect sizes at named markers. //! //! Two checks, both meaningful: the fixture request exercises the endpoint's //! shapes, and the group request cross-checks the *numbers* against //! `/compute/rqtl` -- fitqtl at a marker and scanone at the same marker must //! report the same LOD, because they are the same cross and the same //! phenotype. (They do, to every digit the CSV carries: `fitqtl`'s `$lod` is //! the fitted model's LOD at that position, which is what scanone computes //! there.) 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 test_data_path(name: &str) -> String { format!("{}/tests/test_data/{name}", env!("CARGO_MANIFEST_DIR")) } async fn fixture_with_token() -> Option { if !rscript_available() { eprintln!("skipping: Rscript not on PATH"); 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 } } } /// The six-sample fixture: three markers fitted, in the documented columns, /// with the effect's sign following the phenotype. #[tokio::test] async fn a_fixture_fitqtl_returns_effects_in_the_documented_columns() { let Some(token) = fixture_with_token().await else { return; }; let body = serde_json::json!({ "genotype_file": test_data_path("rqtl_sample1.geno"), "trait_name": "mytrait", // 9.5 … 4.3 against BXD1, BXD2, BXD5, BXD6, BXD8, BXD9 -- a // descending series, so the B-allele effect must be negative. "trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3], "markers": ["rs31443144", "rs6269442", "rs32285189"] }); let response = common::post_json_with_token( "POST /compute/fitqtl", "/compute/fitqtl", &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 columns: Vec<&str> = envelope["meta"]["columns"] .as_array() .unwrap() .iter() .map(|name| name.as_str().unwrap()) .collect(); assert_eq!( columns, vec!["marker", "chr", "pos", "lod", "pct_var", "additive", "se", "t", "p"] ); assert_eq!(envelope["meta"]["num_rows"], 3); let at = |row: &Value, column: &str| -> Value { let index = columns.iter().position(|name| *name == column).unwrap(); row[index].clone() }; for row in envelope["data"].as_array().unwrap() { assert!(at(row, "lod").as_f64().unwrap() >= 0.0); assert!(at(row, "se").as_f64().unwrap() > 0.0); let p = at(row, "p").as_f64().unwrap(); assert!((0.0..=1.0).contains(&p), "p is a probability: {row}"); assert!( at(row, "additive").as_f64().unwrap() < 0.0, "the phenotype falls with the B allele at these markers: {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: mytrait"))); assert!( notes.iter().any(|note| note.starts_with("Rscript ")), "the exact command line comes back: {notes:?}" ); assert!( notes.iter().any(|note| note.starts_with("fitqtl: wrote")), "and the script's own summary: {notes:?}" ); } /// fitqtl and scanone must agree at the same marker, or one of them is /// describing a different cross than the other. #[tokio::test] async fn the_lod_agrees_with_the_scan_at_the_same_marker() { let Some(token) = fixture_with_token().await else { return; }; if !common::db_available() { eprintln!("skipping the_lod_agrees_with_the_scan_at_the_same_marker: 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 scan_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, &scan_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 mut rows = scan["data"]["markers"].as_array().unwrap().clone(); rows.sort_by(|left, right| { right[3] .as_f64() .unwrap() .partial_cmp(&left[3].as_f64().unwrap()) .unwrap() }); let peak = rows[0][0].as_str().unwrap().to_string(); let scan_lod = rows[0][3].as_f64().unwrap(); let fit_body = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.4", "trait_name": "BXD::10001", "samples": samples, "markers": [peak.clone()], }); let response = common::post_json_with_token( "POST /compute/fitqtl (group)", "/compute/fitqtl", &token, &fit_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 row = &envelope["data"][0]; assert_eq!(row[0].as_str().unwrap(), peak); let fit_lod = row[3].as_f64().unwrap(); assert!( (fit_lod - scan_lod).abs() < 1e-6, "fitqtl at {peak} gives LOD {fit_lod}, scanone gives {scan_lod}" ); assert!( row[5].as_f64().unwrap().abs() > 0.0, "and it comes with an effect, not just a statistic: {row}" ); } /// Refusals: no markers, too many markers, both genotype inputs. #[tokio::test] async fn fitqtl_refuses_inputs_it_cannot_fit() { let Some(token) = fixture_with_token().await else { return; }; for (body, code) in [ ( serde_json::json!({ "genotype_file": test_data_path("rqtl_sample1.geno"), "trait_values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0], "markers": [] }), "missing_markers", ), ( serde_json::json!({ "group": "BXD", "genotype_file": test_data_path("rqtl_sample1.geno"), "markers": ["rs31443144"] }), "ambiguous_genotype", ), ] { let response = common::post_json_with_token("POST /compute/fitqtl", "/compute/fitqtl", &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}"); } } /// Covariates, as on `/compute/rqtl`: a case attribute by name, resolved and /// coded the same way, and reported in `notes`. A covariate can be spread /// across the whole panel yet constant among the individuals a trait /// happens to cover -- R/qtl then stops with "system is exactly singular", /// so the script leaves those columns out and says which. #[tokio::test] async fn fitqtl_fits_a_covariate_and_reports_what_it_dropped() { let Some(token) = fixture_with_token().await else { return; }; if !common::db_available() { eprintln!("skipping fitqtl_fits_a_covariate_and_reports_what_it_dropped: 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, "markers": ["mCV24409492"], "covariates": [{"case_attribute": "Status"}], }); let response = common::post_json_with_token( "POST /compute/fitqtl (covariate)", "/compute/fitqtl", &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"]["num_rows"], 1, "the marker is fitted with the covariate: {}", envelope["notes"] ); 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("Covariate Status")), "the same note /compute/rqtl gives: {notes:?}" ); assert!( notes.iter().any(|note| note.starts_with("fitqtl:")) , "{notes:?}" ); // Fitting a covariate changes the fit, so the LOD is no longer the // plain scan's -- that is the point of fitting it. let row = &envelope["data"][0]; assert!(row[3].as_f64().unwrap() > 0.0); assert!(row[5].as_f64().unwrap().abs() > 1.0); // An attribute the group does not define is refused, listing its own. let body = serde_json::json!({ "group": "BXD", "trait_name": "t", "samples": [{"name": "BXD1", "value": 1.0}], "markers": ["mCV24409492"], "covariates": [{"case_attribute": "Nope"}], }); let response = common::post_json_with_token( "POST /compute/fitqtl (bad covariate)", "/compute/fitqtl", &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"], "covariate_unavailable", "{error}"); }