diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-10 11:25:49 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-10 11:25:49 +0000 |
| commit | 8f4a9512d7fb4ac518341f23a4b062ccf80e2a7d (patch) | |
| tree | 698de016efbd16f2a659eac3f57c8df40babdf2f /scripts | |
| parent | 7646276e63fb7c4a7433530cfc3314ace3d362d8 (diff) | |
| download | genenetwork4-8f4a9512d7fb4ac518341f23a4b062ccf80e2a7d.tar.gz | |
compute/qtl2: multiparent crosses, by name
A multiparent population cannot come from a .geno. That file carries two
codes per marker (B/D, A/H/B) -- a collapse, not a founder allele -- and
the collapse is not invertible, so DOL's 194 DO animals cannot be fitted
as a DO however much reference data appears. What can be is a cross that
ships its own qtl2 control file with founder genotypes, so the endpoint
now takes one by name:
{"rqtl2_cross": "do", "trait_name": "DO::10001 (WBC)", "samples": ...}
* resolve_control_file looks in genotype/rqtl2/{name}.json, then
{name}/{name}.json (do/do.json), then {name}.json under
GENOTYPE_FILES; prepare_control_cross reads the cross's pheno, maps
and founder_geno headers without converting anything. It reports
crosstype, founders and whether the cross is multiparent, and
refuses a multiparent crosstype with no founder_geno rather than
silently scanning it as biallelic.
* Samples are matched to the cross's own population by name and the
strays are counted in notes; rqtl2_cross cannot be combined with
group or genotype_file.
* The gate is given individuals x founders, because a multiparent scan
fits one coefficient per founder allele at every marker.
* Autosomes only now applies to shipped crosses as well: the DO's X is
left out (369 markers) instead of being fitted without a sex
covariate, and the script subsets with cross[, chr = autosomal].
Measured on the DO (WBC, 742 individuals, 8 founders, 6044 autosomal
markers): probabilities 174 s / 1.39 GB (cached), LOCO kinship 15 s
(cached), scan 21 s. scan1 runs on one core for a multiparent cross
because its forked path stalls on allele probabilities here -- 25+
minutes at 8 cores with no CPU consumed, against 21 s serial.
Two bugs found by running an actual published trait through it:
* do_pmap.csv is in Mb, not the bp qtl2 documents, so every DO peak was
reported at 0.0 Mb. The map's unit is now detected from its largest
position and printed.
* The phenotype file was read with the default whitespace separator, so
a label with a space ("DO::10001 (WBC)") split into three fields and
read.table stopped with "line 1 did not have 3 elements". Both the
pheno and covariate reads are tab-separated now.
The R error extraction also joins R's wrapped continuation line, so a
complaint reads as a sentence rather than "Error in scan(...) :".
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/qtl2_scan.R | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/scripts/qtl2_scan.R b/scripts/qtl2_scan.R index 6c0d355..6044db3 100755 --- a/scripts/qtl2_scan.R +++ b/scripts/qtl2_scan.R @@ -65,7 +65,10 @@ if (!is.null(opt[["cache-dir"]])) { # the same panel (an older one in another cache directory, say) can have # identical control files and identical file sizes, and reusing the first # one's probabilities silently scans the wrong cross. - parts <- list(dirname(normalizePath(opt$control, mustWork = FALSE)), control) + # The rules this script applies to the cross (the autosome subset) are + # part of the key too: caching probabilities that were computed under a + # different rule would silently change what a scan means. + parts <- list("scan-rules-v1", dirname(normalizePath(opt$control, mustWork = FALSE)), control) for (key in c("geno", "founder_geno", "gmap", "pmap", "pheno", "covar")) { path <- control[[key]] if (!is.null(path)) { @@ -84,6 +87,25 @@ cross <- read_cross2(opt$control, quiet = opt$quiet) say("cross:", cross$crosstype, "| individuals:", n_ind(cross), "| markers:", sum(n_mar(cross)), "| chromosomes:", n_chr(cross)) +# ---- autosomes only --------------------------------------------------- +# A cross this API converted has its non-autosomal markers removed already; +# one read from a control file (the DO, say) carries X, Y and MT as the panel +# does. X needs a sex covariate to be fitted properly and Y/MT degenerate the +# per-chromosome kinship, so both sources get the same treatment here and the +# number left out is printed rather than silently changing the scan. +chromosomes <- names(cross$gmap) +if (is.null(chromosomes)) chromosomes <- names(cross$pmap) +if (!is.null(chromosomes)) { + autosomal <- chromosomes[grepl("^[0-9]+$", chromosomes)] + left_out <- setdiff(chromosomes, autosomal) + if (length(left_out) > 0 && length(autosomal) > 0) { + before <- sum(n_mar(cross)) + cross <- cross[, chr = autosomal] + say("autosomes only: left out", paste(left_out, collapse = ","), "|", + before - sum(n_mar(cross)), "markers") + } +} + # ---- genotype probabilities, cached per chromosome -------------------- # Multiparent means more than two founder alleles -- eight for the DO. The # founder genotypes are the honest test; a biallelic cross has none. @@ -129,7 +151,11 @@ if (opt$kinship != "none") { } # ---- phenotype and covariates, aligned by name ------------------------ -df <- read.table(opt$pheno, na.strings = "x", header = TRUE, check.names = FALSE) +# sep = "\t" because a trait label may contain a space: with the default +# whitespace separator, "DO::10001 (WBC)" splits into three fields and +# read.table stops with "line 1 did not have 3 elements", which reads like a +# malformed file rather than a label with a space in it. +df <- read.table(opt$pheno, sep = "\t", na.strings = "x", header = TRUE, check.names = FALSE) trait_name <- colnames(df)[2] # The cross's individuals, whatever shape its pheno slot takes: a matrix # with rownames when the cross file carries several phenotypes, a named @@ -149,7 +175,7 @@ pheno <- matrix(values, ncol = 1, dimnames = list(ind, trait_name)) addcovar <- NULL if (!is.null(opt$covariates)) { - covar_df <- read.table(opt$covariates, header = TRUE, check.names = FALSE, na.strings = "x") + covar_df <- read.table(sep = "\t", opt$covariates, header = TRUE, check.names = FALSE, na.strings = "x") struct <- read.table(opt$covarstruct, header = FALSE, sep = "\t", stringsAsFactors = FALSE, col.names = c("name", "kind"), quote = "") addcovar <- data.frame(row.names = ind) @@ -181,13 +207,36 @@ if (!is.null(opt$covariates)) { # ---- the scan --------------------------------------------------------- started <- Sys.time() +# Multiparent crosses are scanned on one core, deliberately. scan1's forked +# path stalls here on allele probabilities -- the parent sits in waitpid with +# the children idle (measured: 25+ minutes with cores=8, no CPU consumed), +# while cores=1 scans the whole DO genome in 21 s. The expensive step, the +# genotype probabilities, is still parallel; only the scan is serial, and it +# is the cheap one. +scan_cores <- if (multiparent) 1L else opt$cores +if (multiparent && opt$cores > 1) { + say("scan1 on 1 core: the forked path stalls on allele probabilities") +} scan <- scan1(scan_probs, pheno, kinship = kinship, addcovar = addcovar, - model = opt$model, cores = opt$cores) + model = opt$model, cores = scan_cores) say("scan1:", round(as.numeric(difftime(Sys.time(), started, units="secs")), 1), "s") # ---- rows: marker, chr, pos(Mb), lod ---------------------------------- map <- if (!is.null(cross$pmap)) cross$pmap else cross$gmap in_mb <- !is.null(cross$pmap) +# qtl2 documents a physical map in base pairs, and the control files this API +# writes do hold bp -- but a map that ships with a panel may be in Mb (the +# DO's is: 74.22 for a chr6 marker, not 74223530). Dividing by 1e6 twice put +# every DO peak at 0.0 Mb, which plots as a peak at the telomere of nothing. +# A whole mouse chromosome is ~2e8 bp and ~200 Mb, so the largest position +# says which unit the map is in. +if (in_mb) { + largest <- suppressWarnings(max(unlist(cross$pmap), na.rm = TRUE)) + if (is.finite(largest) && largest < 1e6) { + say("physical map: already in Mb (largest position", round(largest, 1), ")") + in_mb <- FALSE + } +} rows <- list() for (chr_name in names(map)) { positions <- map[[chr_name]] |
