//! Effect sizes for named markers, via R/qtl's `fitqtl`. //! //! `/compute/rqtl` answers "where" -- `scanone` gives a LOD per marker -- //! but not "how much": a scan statistic is not a fitted model. `fitqtl` //! fits one at a marker or QTL position and reports the QTL's effect, its //! standard error, the model LOD and the variance explained. That is what a //! table of effects needs, and it is a different question, asked //! deliberately about *named* markers (usually the peaks a scan already //! found) rather than about every marker on the genome: `fitqtl` fits a //! model per marker, so a genome-wide sweep would be thousands of fits. //! //! `scripts/qtl_fitqtl.R` does the fitting, reading the same GeneNetwork //! `.geno` file and the same two-column phenotype file as //! `rqtl_wrapper.R`. This module runs it and turns its CSV into positional //! rows, the shape the rest of the API uses. use std::path::Path; use serde_json::Value; use crate::rqtl::Sample; use crate::streaming; /// The columns of every row, in order. pub const COLUMNS: [&str; 9] = [ "marker", "chr", "pos", "lod", "pct_var", "additive", "se", "t", "p", ]; /// Why a fit did not happen. #[derive(Debug)] pub enum FitqtlError { Io(std::io::Error), /// Rscript failed; the message is its stderr. CommandFailed(String), /// The output file was missing or unreadable. OutputFile(String), } impl std::fmt::Display for FitqtlError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { FitqtlError::Io(error) => write!(f, "{error}"), FitqtlError::CommandFailed(detail) => write!(f, "{detail}"), FitqtlError::OutputFile(detail) => write!(f, "{detail}"), } } } /// What one `fitqtl` run produced. pub struct FitqtlOutput { /// One row per marker that could be fitted, positional in [`COLUMNS`]. pub rows: Vec, /// What the script printed -- one line naming the strongest fit. pub summary: String, /// The exact command line, so the run is reproducible. pub command: String, } /// Fit `markers` in `genotype_file` against `samples`. /// /// `samples` must be in the genotype file's own individual order, exactly as /// for `/compute/rqtl` (the CSV cross format has no name matching). pub async fn compute( script: &str, outdir: &str, genotype_file: &str, trait_name: &str, samples: &[Sample], markers: &[String], covariates: &[crate::covariates::Covariate], model: &str, method: &str, scale: &str, ) -> Result { std::fs::create_dir_all(outdir).map_err(FitqtlError::Io)?; let unique = format!( "{}-{}", std::process::id(), std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0) ); let pheno_path = Path::new(outdir).join(format!("{unique}-pheno.txt")); crate::rqtl::write_pheno_file(&pheno_path, trait_name, samples, covariates) .map_err(FitqtlError::Io)?; // Which of the phenotype file's extra columns are factors: the script // dummy-expands exactly those. let covarstruct_path = Path::new(outdir).join(format!("{unique}-covarstruct.txt")); let covarstruct_arg = if covariates.is_empty() { None } else { crate::covariates::write_covarstruct_file(&covarstruct_path, covariates) .map_err(FitqtlError::Io)?; Some(covarstruct_path.to_string_lossy().into_owned()) }; let result_name = format!("{unique}-fitqtl.csv"); let pheno_arg = pheno_path.to_string_lossy().into_owned(); let marker_arg = markers.join(","); let args = vec![ script, "--geno", genotype_file, "--pheno", &pheno_arg, "--markers", &marker_arg, "--model", model, "--method", method, "--scale", scale, "--outdir", outdir, "--filename", &result_name, ]; let mut args = args; if let Some(covarstruct) = covarstruct_arg.as_deref() { args.push("--covarstruct"); args.push(covarstruct); } let command = format!("Rscript {}", args.join(" ")); let output = streaming::run_streaming("Rscript", &args, None) .await .map_err(FitqtlError::Io)?; if !output.status.success() { return Err(FitqtlError::CommandFailed( String::from_utf8_lossy(&output.stderr).into_owned(), )); } let contents = std::fs::read_to_string(Path::new(outdir).join(&result_name)) .map_err(|error| FitqtlError::OutputFile(error.to_string()))?; Ok(FitqtlOutput { rows: parse_rows(&contents), // The script's own last line is its one-line summary; the rest of // its stdout is read.cross's chatter. summary: String::from_utf8_lossy(&output.stdout) .lines() .filter(|line| !line.trim().is_empty()) .next_back() .unwrap_or_default() .trim() .to_string(), command, }) } /// Parse the script's CSV into positional rows. `chr` stays a string (it can /// be `X`), everything after it is a number when it parses as one. fn parse_rows(contents: &str) -> Vec { let mut lines = contents.lines(); let Some(header) = lines.next() else { return Vec::new(); }; let names: Vec<&str> = header.split(',').map(str::trim).collect(); lines .filter(|line| !line.trim().is_empty()) .map(|line| { let fields: Vec<&str> = line.split(',').map(str::trim).collect(); let mut row = Vec::with_capacity(COLUMNS.len()); for column in COLUMNS.iter() { let field = names .iter() .position(|name| name == column) .and_then(|at| fields.get(at).copied()) .unwrap_or(""); row.push(match *column { "marker" | "chr" => Value::String(field.to_string()), _ => field .parse::() .map(|n| serde_json::json!(n)) .unwrap_or(Value::Null), }); } Value::Array(row) }) .collect() } #[cfg(test)] mod tests { use super::*; #[test] fn rows_are_positional_and_chr_stays_text() { let contents = "marker,chr,pos,lod,pct_var,additive,se,t,p\n\ rs1,X,3.0,4.2,12.5,-0.51,0.8,-0.6,0.01\n"; let rows = parse_rows(contents); assert_eq!(rows.len(), 1); let row = rows[0].as_array().unwrap(); assert_eq!(row[0], Value::String("rs1".to_string())); assert_eq!(row[1], Value::String("X".to_string())); assert_eq!(row[3], serde_json::json!(4.2)); assert_eq!(row[5], serde_json::json!(-0.51)); assert_eq!(row.len(), COLUMNS.len()); } #[test] fn a_marker_that_could_not_be_fitted_is_simply_absent() { // The script skips markers it cannot find or fit rather than writing // a row of NAs, so the count can be lower than the request. let contents = "marker,chr,pos,lod,pct_var,additive,se,t,p\n"; assert!(parse_rows(contents).is_empty()); } }