R/ancestry_split.R
split_diploid_multi.RdGeneralises split_diploid to an arbitrary number of
ancestries. For each variant, population allele proportions
\(p_1, \ldots, p_K\) (summing to 1) are estimated from samples with
unambiguous ancestry (pure-ancestry homozygotes and heterozygotes, plus
mixed hom-alts which each contribute one allele to each parent pool).
Ambiguous heterozygotes in each mixed pair are then split by the
conditional pairwise ratio \(p_i / (p_i + p_j)\).
When no unambiguous alt alleles exist for a pair (singleton edge case),
the split defaults to 0.5 / 0.5.
split_diploid_multi(
gt_genotype,
ancestry,
pure_codes = c(AFR = 3L, EUR = 1L),
mixed_codes = data.frame(code = 2L, pop1 = "AFR", pop2 = "EUR"),
gla = NULL,
arm_id = NULL
)Integer matrix (variants x samples) of genotypes (0/1/2).
Integer matrix (variants x samples) of ancestry codes,
same dimensions as gt_genotype.
Named integer vector mapping each population label to its
pure-ancestry diploid code.
Example: c(AFR = 3L, EUR = 1L, NAT = 4L). The default is the
two-population coding used by split_diploid
(3 = AFR/AFR, 1 = EUR/EUR).
Data frame with three columns:
Integer ancestry code for this mixed pair.
Character name of the first parent population
(must be a name in pure_codes).
Character name of the second parent population
(must be a name in pure_codes).
One row per mixed-ancestry diploid type. The default is the
two-population mixed code 2 = AFR/EUR, so with both defaults
split_diploid_multi(gt, ancestry) matches
split_diploid(gt, ancestry).
Example for 3 populations:
data.frame(code=c(2L,5L,6L), pop1=c("AFR","AFR","EUR"), pop2=c("EUR","NAT","NAT"))
Optional shrinkage target: numeric matrix with one row per
target group and columns matching names(pure_codes). Each
ambiguous pair (a, b) is shrunk toward \(t_a / (t_a + t_b)\) by the
pair's ambiguous share \(w\) of the evidence.
ancestry_split uses a single row of 1/K (i.e. 1/2 for
every pair). When NULL (default), no shrinkage: the raw p[k]
ratio is used, with a 0.5/0.5 fallback only when a pair has no
unambiguous evidence.
Optional integer vector (length = nrow(gt_genotype))
giving each variant's row index into gla (0-based; all zeros for
a one-row target). Required together with gla.
Named list of K numeric matrices (variants x samples), one per
population in pure_codes, in the same order. Each element is
named after the corresponding entry in pure_codes.
split_diploid for the hardcoded 2-population
(AFR + EUR) wrapper; split_haplotype_multi for the phased
K-population analogue.
# 3-population example: AFR=3, EUR=1, NAT=4; mixed codes 2/5/6
set.seed(1)
gt <- matrix(sample(0:2, 20, replace = TRUE, prob = c(.8,.15,.05)),
nrow = 4, ncol = 5)
# ancestry codes: 3=AFR/AFR, 1=EUR/EUR, 4=NAT/NAT,
# 2=AFR/EUR, 5=AFR/NAT, 6=EUR/NAT
anc <- matrix(sample(c(1L,2L,3L,4L,5L,6L), 20, replace = TRUE),
nrow = 4, ncol = 5)
pure <- c(AFR = 3L, EUR = 1L, NAT = 4L)
mixed <- data.frame(code = c(2L, 5L, 6L),
pop1 = c("AFR", "AFR", "EUR"),
pop2 = c("EUR", "NAT", "NAT"))
out <- split_diploid_multi(gt, anc, pure, mixed)
names(out) # "AFR" "EUR" "NAT"
#> [1] "AFR" "EUR" "NAT"