diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/gemma.rs | 84 |
1 files changed, 67 insertions, 17 deletions
diff --git a/src/gemma.rs b/src/gemma.rs index 42a7de9..3f8db70 100644 --- a/src/gemma.rs +++ b/src/gemma.rs @@ -497,16 +497,18 @@ fn ravanan_output_paths<'a>(outputs: &'a Value, id: &str) -> Result<Vec<&'a str> .collect() } -/// The distinct chromosome names in `snps_file` (whitespace-separated: -/// rs id, position, chromosome), in first-seen order -- reproduces -/// gemma-wrapper's own `get_chromosomes`, used here to build -/// `compute_ravanan`'s `chromosomes` scatter input (CWL's `scatter` -/// needs a concrete array at invocation time, which can't be +/// The distinct chromosome names in a SNP annotation file's contents +/// (whitespace-separated: rs id, position, chromosome), in first-seen +/// order -- reproduces gemma-wrapper's own `get_chromosomes`, used here +/// to build `compute_ravanan`'s `chromosomes` scatter input (CWL's +/// `scatter` needs a concrete array at invocation time, which can't be /// discovered by a step inside the workflow itself without another /// round of plumbing -- see `cwl/gemma/gemma-loco.scm`'s own doc /// comment on this input). -fn chromosomes_in(snps_file: &str) -> Result<Vec<String>, GemmaError> { - let contents = std::fs::read_to_string(snps_file).map_err(GemmaError::Io)?; +/// +/// Takes the contents, not the path: the caller also hashes them (see +/// [`gemma_wrapper_input_key`]) and should not read the file twice. +fn chromosomes_from(contents: &str) -> Vec<String> { let mut seen = std::collections::HashSet::new(); let mut chromosomes = Vec::new(); for line in contents.lines() { @@ -516,7 +518,42 @@ fn chromosomes_in(snps_file: &str) -> Result<Vec<String>, GemmaError> { } } } - Ok(chromosomes) + chromosomes +} + +/// A stable identity for one request's GEMMA inputs, used to give each +/// distinct input set its own gemma-wrapper cache root (see +/// `compute_ravanan`). +/// +/// The genotype file is identified by its path and size rather than its +/// contents: hashing a genome-sized file on every request would cost +/// more than the cache saves, and the path is stable in practice (it is +/// the caller's own file, interned verbatim into ravanan's store). +fn gemma_wrapper_input_key( + genotype_file: &str, + snps_contents: &str, + trait_values: &TraitValues, + lmm: u8, +) -> String { + let mut material = Vec::new(); + material.extend_from_slice(genotype_file.as_bytes()); + material.push(0); + if let Ok(metadata) = std::fs::metadata(genotype_file) { + material.extend_from_slice(&metadata.len().to_le_bytes()); + } + material.push(0); + material.extend_from_slice(snps_contents.as_bytes()); + material.push(0); + material.extend_from_slice(lmm.to_string().as_bytes()); + material.push(0); + for value in trait_values { + match value { + Some(value) => material.extend_from_slice(&value.to_bits().to_le_bytes()), + None => material.extend_from_slice(b"NA"), + } + material.push(b','); + } + crate::indexer::sha256::hex_digest(&material)[..16].to_string() } /// The same GEMMA `--loco` kinship-then-GWA computation as `compute`, @@ -578,7 +615,8 @@ pub async fn compute_ravanan( std::fs::create_dir_all(store_dir.join(subdir)).map_err(GemmaError::Io)?; } - let chromosomes = chromosomes_in(snps_file)?; + let snps_contents = std::fs::read_to_string(snps_file).map_err(GemmaError::Io)?; + let chromosomes = chromosomes_from(&snps_contents); if chromosomes.is_empty() { return Err(GemmaError::NoAssocFiles); } @@ -588,15 +626,27 @@ pub async fn compute_ravanan( .collect::<Vec<_>>() .join(", "); - // One persistent cache directory per chromosome, reused across - // requests, so gemma-wrapper's content-hash cache actually gets hit - // on repeated identical requests -- unlike "." (each job's own - // hermetic CWL working directory under ravanan's --store, fresh + // A persistent cache directory per (input set, chromosome), reused + // across requests, so gemma-wrapper's content-hash cache actually + // gets hit on repeated identical requests -- unlike "." (each job's + // own hermetic CWL working directory under ravanan's --store, fresh // every run), which never accumulates a cache at all. Keyed by - // chromosome, not shared, because gemma-wrapper's cache-hit hash - // does not vary with --chromosomes: a shared directory would let - // one chromosome's job silently reuse another's cached result. - let wrapper_cache_dir = Path::new(cache_dir).join("gemma-wrapper-cache"); + // chromosome as well as the input set, because gemma-wrapper's + // cache-hit hash does not vary with --chromosomes: a directory + // shared between chromosomes would let one chromosome's job + // silently reuse another's cached result. + // + // Keyed by the input set too, because gemma-wrapper names its + // archives by a *content* hash: two different input sets (another + // trait, or another snps/genotype file) sharing a `chr-N` directory + // leave two `*-gemma-cXX.tar.xz` files there, and the CWL's + // single-File glob output binding (`$(inputs.cache_dir)/*-gemma-cXX.tar.xz`) + // then matches more than one path and the step fails -- with an + // error that ravanan's `user-error` silently drops in a job script. + let input_key = gemma_wrapper_input_key(genotype_file, &snps_contents, trait_values, lmm); + let wrapper_cache_dir = Path::new(cache_dir) + .join("gemma-wrapper-cache") + .join(&input_key); let mut chr_cache_dirs = Vec::new(); for chr in &chromosomes { let chr_cache_dir = wrapper_cache_dir.join(format!("chr-{chr}")); |
