diff options
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]] |
