1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
//! 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<Value>,
/// 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<FitqtlOutput, FitqtlError> {
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<Value> {
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::<f64>()
.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());
}
}
|