diff options
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/prepare-genotypes.sh | 8 | ||||
| -rwxr-xr-x | scripts/qtl2-impute-bimbam.R | 62 |
2 files changed, 64 insertions, 6 deletions
diff --git a/scripts/prepare-genotypes.sh b/scripts/prepare-genotypes.sh index 36a959b..9624e0e 100755 --- a/scripts/prepare-genotypes.sh +++ b/scripts/prepare-genotypes.sh @@ -15,13 +15,14 @@ # # scripts/prepare-genotypes.sh --mode mean|hmm|none \ # --geno <bimbam geno> --snps <snps file> --out <prefix> \ -# [--cross <qtl2 cross dir>] [--probs <probs.rds|->] [--minprob N] +# [--cross <qtl2 cross dir>] [--probs <probs.rds|->] [--minprob N] \ +# [--individuals-file <names, one per line>] # # Writes <prefix>.bimbam.txt and <prefix>.snps.txt and prints the prefix. # Exits 2 on bad arguments, 1 when a step fails. set -eu -mode= geno= snps= out= cross_dir= probs= minprob=0 +mode= geno= snps= out= cross_dir= probs= minprob=0 individuals_file= while [ $# -gt 0 ]; do case $1 in --mode) mode=$2; shift 2 ;; @@ -31,6 +32,7 @@ while [ $# -gt 0 ]; do --cross) cross_dir=$2; shift 2 ;; --probs) probs=$2; shift 2 ;; --minprob) minprob=$2; shift 2 ;; + --individuals-file) individuals_file=$2; shift 2 ;; -h|--help) sed -n '2,26p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "prepare-genotypes.sh: unknown argument $1" >&2; exit 2 ;; esac @@ -66,7 +68,7 @@ hmm) # R's own complaint last, so a caller that reports the final stderr line # reports the cause rather than our wrapper sentence. if ! r_output=$("$RSCRIPT" "$(dirname "$0")/qtl2-impute-bimbam.R" \ - "$cross_dir" "${probs:--}" "$out" "$minprob" 2>&1); then + "$cross_dir" "${probs:--}" "$out" "$minprob" "$individuals_file" 2>&1); then echo "prepare-genotypes.sh: HMM imputation failed:" >&2 printf '%s\n' "$r_output" | tail -6 >&2 exit 1 diff --git a/scripts/qtl2-impute-bimbam.R b/scripts/qtl2-impute-bimbam.R index 2e11a40..ae95b48 100755 --- a/scripts/qtl2-impute-bimbam.R +++ b/scripts/qtl2-impute-bimbam.R @@ -26,6 +26,13 @@ if (length(args) < 3) { } cross_dir <- args[1]; probs_path <- args[2]; out_prefix <- args[3] minprob <- if (length(args) >= 4) as.numeric(args[4]) else 0 +# Optional: the individuals the caller is actually serving, one per line. The +# cross carries the whole panel; a request that drops parents and F1s serves a +# subset, and a genotype file with more columns than the phenotype covers makes +# every comparison between modes meaningless (measured: none/mean fitted 198 +# individuals, hmm 235, and the peaks landed on different chromosomes). +individuals_file <- if (length(args) >= 5 && nzchar(args[5])) args[5] else NULL +wanted <- if (!is.null(individuals_file)) trimws(readLines(individuals_file)) else NULL setwd(cross_dir) cross <- read_cross2("control.json") @@ -51,7 +58,45 @@ probs <- if (identical(probs_path, "-")) { # maxmarg returns one call per marker and individual, at the highest # probability above MINPROB -- or NA when the HMM is less sure than that. At # minprob 0 every cell is filled, which is what the mdb needs. -imputed <- suppressWarnings(maxmarg(probs, minprob = minprob, cores = 1)) +# The genetic map the HMM smooths along. Ours is often degenerate -- the .geno +# files carry no cM, so the converter writes 0 for many markers ("19 markers with +# missing genetic map position; using 0"), and with no distance between markers +# the HMM has nothing to smooth with: measured, P(BB) never exceeded 0.5 and +# every individual was called AA at a marker where 125 of 235 are BB. When the +# map has gaps, positions come from the physical map at 1 cM per Mb, which is +# crude but monotone and infinitely better than 0. +if (!is.null(cross$pmap)) { + for (chr_name in names(cross$gmap)) { + g <- cross$gmap[[chr_name]] + p_mb <- cross$pmap[[chr_name]][names(g)] / 1e6 + if (anyNA(g) || any(diff(g) <= 0) || any(g == 0)) { + if (!anyNA(p_mb)) cross$gmap[[chr_name]] <- unname(p_mb) + } + } +} + +# return_char: the genotype LETTERS (A/B, plus H if it were kept), not qtl2's +# internal genotype *indices*. Measured the hard way: maxmarg returns 1..n_gen +# -- 1 and 2 for a riself cross -- which are not the cross's numeric codes +# (A=1, B=3 per control.json). Mapping 2 to 1 read every BB animal as a +# heterozygote, which collapsed the genotypes (sd 0.21 where the panel has +# 0.46) and quietly killed the scan: the same marker that is p 5.9e-07 in the +# other modes came out at 2.0e-01. +imputed <- suppressWarnings(maxmarg(probs, minprob = minprob, cores = 1, + return_char = TRUE)) +if (!is.null(wanted)) { + present <- rownames(imputed[[1]]) + keep <- wanted[wanted %in% present] + if (length(keep) == 0) { + stop("none of the requested individuals appear in the cross", call. = FALSE) + } + for (chr_name in names(imputed)) { + imputed[[chr_name]] <- imputed[[chr_name]][keep, , drop = FALSE] + } + wanted_kept <- keep +} else { + wanted_kept <- rownames(imputed[[1]]) +} # How much work the HMM did: cells the cross itself had as missing (a 0 in an # explicit genotype map, never NA -- see read_cross2's own convention). @@ -61,7 +106,18 @@ left <- sum(vapply(imputed, function(g) sum(is.na(g)), integer(1))) # The calls come back as genotype *codes* in qtl2's numeric convention for this # cross (A = 1, B = 3, H = 2 per control.json); pangemma wants 0/1/2. -codes <- c(`1` = 0, `2` = 1, `3` = 2) +# The alphabet comes from the data: return_char gives the genotype *names* +# ("AA"/"BB" for a riself cross, "A"/"B" for a plain one, one name per founder +# for multiparent), so the first is homozygote 1 (0), the last homozygote 2 (2) +# and anything in between a heterozygote (1). Hard-coding A/H/B skipped every +# marker, because the values are "AA" and "BB" -- the run then had no +# chromosomes and no association files. +present <- sort(unique(as.vector(unlist(imputed)))) +present <- present[!is.na(present)] +if (length(present) < 2) { + stop("the HMM returned a single genotype state; nothing to map", call. = FALSE) +} +codes <- stats::setNames(c(0, rep(1, max(0, length(present) - 2)), 2), present) rows <- list(); snps <- list() # maxmarg returns individuals x markers (rows are individuals, which is the # opposite of the BIMBAM's marker-per-line shape, and the reason a first version @@ -90,7 +146,7 @@ writeLines(vapply(rows, paste, character(1), collapse = ","), # has to align to this, or the two disagree by name and every individual is # missing (measured: gemma answered "no individuals in common" and ravanan # reported no association files at all). -writeLines(rownames(imputed[[1]]), paste0(out_prefix, ".individuals.txt")) +writeLines(wanted_kept, paste0(out_prefix, ".individuals.txt")) writeLines(vapply(snps, paste, character(1), collapse = ","), paste0(out_prefix, ".snps.txt")) |
