vignettes/split-intuition.Rmd
split-intuition.Rmdlantern splits each sample’s alternate-allele dosage at a variant into one dosage per ancestral population. There are two ways to do this:
| Mode | Uses | How an allele is assigned |
|---|---|---|
Phased ("haplotype") |
the allele and the ancestry of each haplotype | to the population of the haplotype that carries it |
Unphased ("dosage") |
the genotype (0/1/2) and the pair of ancestries at the site | shared by estimated proportions when the genotype is ambiguous |
This vignette explains both on small matrices, without any files,
using the low-level functions that ancestry_split() calls
internally. Part A covers two populations, Part B any number of
populations, and Part C the shrinkage that ancestry_split()
applies to unphased splits. vignette("toy-vcf-msp-example")
shows the same splits starting from VCF and MSP files.
split_diploid()
Without phase we only know, for each sample at a variant, its genotype (0, 1 or 2 alternate alleles) and its pair of ancestries, coded as
For most samples the split is obvious. An AFR/AFR sample’s alleles are all African, and an AFR/EUR sample with genotype 2 has one allele on each haplotype, so one African and one European. The only ambiguous case is an AFR/EUR heterozygote: it has one alternate allele, and we cannot tell which haplotype carries it. The full rule:
| Ancestry | Genotype | African dosage | European dosage |
|---|---|---|---|
| AFR/AFR | 2 | 2 | 0 |
| AFR/AFR | 1 | 1 | 0 |
| AFR/EUR | 2 | 1 | 1 |
| AFR/EUR | 1 | ||
| EUR/EUR | 2 | 0 | 2 |
| EUR/EUR | 1 | 0 | 1 |
Genotype 0 contributes nothing to either dosage. Only the highlighted row needs the proportions and , which are estimated from the other rows as shown below.
Consider a single variant in six samples:
gt <- matrix(c(1, 2, 1, 1, 0, 2), nrow = 1,
dimnames = list("var1", paste0("S", 1:6)))
anc <- matrix(c(3, 3, 1, 2, 2, 2), nrow = 1,
dimnames = list("var1", paste0("S", 1:6)))
rbind(genotype = gt[1, ], ancestry = anc[1, ])
#> S1 S2 S3 S4 S5 S6
#> genotype 1 2 1 1 0 2
#> ancestry 3 3 1 2 2 2S1 and S2 are AFR/AFR carriers, S3 is a EUR/EUR carrier, S6 is an AFR/EUR homozygote and S4 is the one ambiguous AFR/EUR heterozygote. To split S4’s allele we estimate how this variant’s alternate alleles divide between African and European haplotypes, using only the unambiguous carriers. Writing for the number of samples of a given ancestry and genotype,
where is the number of unambiguous alternate alleles, that is, all alternate alleles at the variant minus those of the ambiguous heterozygotes:
An AFR/EUR homozygote contributes one allele to each population. The ambiguous heterozygote then gets of its allele as African dosage and as European dosage. For our variant:
n <- function(a, g) sum(anc == a & gt == g)
D <- 2 * n(3, 2) + n(3, 1) + 2 * n(2, 2) + 2 * n(1, 2) + n(1, 1)
p_afr <- (2 * n(3, 2) + n(3, 1) + n(2, 2)) / D
p_eur <- (n(2, 2) + 2 * n(1, 2) + n(1, 1)) / D
c(D = D, p_AFR = p_afr, p_EUR = p_eur)
#> D p_AFR p_EUR
#> 6.0000000 0.6666667 0.3333333split_diploid() returns the two dosage matrices. S4’s
allele is split 2/3 African and 1/3 European, and every other sample’s
split is whole:
res <- split_diploid(gt, anc)
split_table <- rbind(AFR = res$african[1, ], EUR = res$european[1, ])
colnames(split_table) <- colnames(gt)
split_table
#> S1 S2 S3 S4 S5 S6
#> AFR 1 2 0 0.6666667 0 1
#> EUR 0 0 1 0.3333333 0 1If every carrier of a variant is an ambiguous heterozygote,
and the proportions cannot be estimated. split_diploid()
then splits each allele 1/2 and 1/2:
gt_edge <- matrix(c(1, 0, 1), nrow = 1)
anc_edge <- matrix(c(2, 1, 2), nrow = 1)
split_diploid(gt_edge, anc_edge)
#> $african
#> [,1] [,2] [,3]
#> [1,] 0.5 0 0.5
#>
#> $european
#> [,1] [,2] [,3]
#> [1,] 0.5 0 0.5Part C shows that estimates from very few unambiguous carriers are
also unreliable, and how ancestry_split() handles that.
split_haplotype()
With phased genotypes and local ancestry for each haplotype, nothing
needs to be estimated: each haplotype’s allele goes to that haplotype’s
population. split_haplotype() takes four matrices of the
same shape (variants x samples): the alleles (0/1) on haplotype 0 and on
haplotype 1, and the ancestry of each haplotype, with RFMix’s codes 0 =
AFR and 1 = EUR.
ids <- list(paste0("var", 1:3), paste0("S", 1:3))
gt0 <- matrix(c(1, 0, 1, 0, 1, 0, 1, 1, 0), nrow = 3, dimnames = ids)
gt1 <- matrix(c(0, 1, 0, 1, 0, 1, 0, 1, 1), nrow = 3, dimnames = ids)
a0 <- matrix(c(0, 0, 1, 1, 0, 0, 0, 1, 0), nrow = 3, dimnames = ids)
a1 <- matrix(c(1, 1, 0, 0, 1, 1, 1, 0, 1), nrow = 3, dimnames = ids)Take S1 at var1: haplotype 0 carries the allele
(gt0 is 1) and is African (a0 is 0), and
haplotype 1 does not carry it. So S1 has an African dosage of 1 and a
European dosage of 0 at var1.
phased <- split_haplotype(gt0, gt1, a0, a1)
phased$african
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 0 1 1
#> [3,] 0 0 0
phased$european
#> [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 1 0 1
#> [3,] 1 1 1The two dosages always add up to the genotype,
gt0 + gt1:
split_diploid_multi() and
split_haplotype_multi() do the same for any number of
populations K. With two populations they give the same results as the
functions in Part A.
split_diploid_multi()
With K populations a sample’s pair of ancestries is either pure (both haplotypes from the same population) or mixed (from two different populations). There are K pure and K(K-1)/2 mixed combinations:
| K | Pure | Mixed | Total codes |
|---|---|---|---|
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
You choose a code for each combination and pass two descriptions to
the function: pure_codes, the code of each pure
combination, and mixed_codes, a table with the code and the
two populations of each mixed combination. For three populations
(African, European and Native American):
pure_codes <- c(AFR = 3L, EUR = 1L, NAT = 4L)
mixed_codes <- data.frame(code = c(2L, 5L, 6L),
pop1 = c("AFR", "AFR", "EUR"),
pop2 = c("EUR", "NAT", "NAT"))
mixed_codes
#> code pop1 pop2
#> 1 2 AFR EUR
#> 2 5 AFR NAT
#> 3 6 EUR NATWe simulate 12 rare variants in 30 samples from a three-way admixed group:
set.seed(2025)
gt3 <- matrix(sample(0:2, 12 * 30, replace = TRUE, prob = c(0.88, 0.10, 0.02)),
nrow = 12, ncol = 30)
anc3 <- matrix(sample(c(pure_codes, mixed_codes$code), 12 * 30, replace = TRUE,
prob = c(0.30, 0.20, 0.10, 0.20, 0.10, 0.10)),
nrow = 12, ncol = 30)
out <- split_diploid_multi(gt3, anc3, pure_codes, mixed_codes)
names(out)
#> [1] "AFR" "EUR" "NAT"The estimate works as in A.1, with one proportion per population. Pure carriers count for their population and mixed homozygotes contribute one allele to each of their two populations. With the number of pure population- samples with genotype , and the number of mixed / homozygotes,
so the add up to 1. An ambiguous heterozygote with ancestries and then gets of its allele in population and in population .
To check this, we take the first variant with an ambiguous AFR/EUR heterozygote and compute the proportions by hand:
v <- which(rowSums(anc3 == 2 & gt3 == 1) > 0)[1]
g <- gt3[v, ]
a <- anc3[v, ]
n <- function(code, genotype) sum(a == code & g == genotype)
num <- c(AFR = 2 * n(3, 2) + n(3, 1) + n(2, 2) + n(5, 2),
EUR = 2 * n(1, 2) + n(1, 1) + n(2, 2) + n(6, 2),
NAT = 2 * n(4, 2) + n(4, 1) + n(5, 2) + n(6, 2))
p <- num / sum(num) # sum(num) equals D
p
#> AFR EUR NAT
#> 0.4 0.2 0.4The heterozygote’s African share should be , which matches the function’s output:
split_haplotype_multi()
The phased split needs only the code of each population, as used in the MSP file (RFMix numbers populations from 0):
set.seed(2025)
hap0 <- matrix(rbinom(8 * 6, 1, 0.1), nrow = 8)
hap1 <- matrix(rbinom(8 * 6, 1, 0.1), nrow = 8)
anc0 <- matrix(sample(0:2, 8 * 6, replace = TRUE, prob = c(0.4, 0.4, 0.2)), nrow = 8)
anc1 <- matrix(sample(0:2, 8 * 6, replace = TRUE, prob = c(0.4, 0.4, 0.2)), nrow = 8)
phased3 <- split_haplotype_multi(hap0, hap1, anc0, anc1,
pop_codes = c(AFR = 0L, EUR = 1L, NAT = 2L))
phased3$AFR[1:4, ]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 0 0 0 0 0 0
#> [2,] 0 0 0 1 0 0
#> [3,] 0 0 0 0 0 0
#> [4,] 0 1 0 0 0 0Each allele goes to exactly one population, so the three dosages add up to the genotype:
all.equal(phased3$AFR + phased3$EUR + phased3$NAT, hap0 + hap1)
#> [1] TRUEWith two populations, split_haplotype_multi() gives the
same result as split_haplotype():
m2 <- split_haplotype_multi(gt0, gt1, a0, a1, pop_codes = c(AFR = 0L, EUR = 1L))
all.equal(unname(m2$AFR), phased$african)
#> [1] TRUEMore populations only means more entries in pure_codes
and more rows in mixed_codes, one for every pair of
populations. For four populations:
pure_4 <- c(AFR = 3L, EUR = 1L, NAT = 4L, EAS = 5L)
mixed_4 <- data.frame(code = c(2L, 6L, 7L, 8L, 9L, 10L),
pop1 = c("AFR", "AFR", "AFR", "EUR", "EUR", "NAT"),
pop2 = c("EUR", "NAT", "EAS", "NAT", "EAS", "EAS"))
set.seed(11)
gt4 <- matrix(sample(0:2, 6 * 16, replace = TRUE, prob = c(0.85, 0.12, 0.03)),
nrow = 6)
anc4 <- matrix(sample(c(pure_4, mixed_4$code), 6 * 16, replace = TRUE), nrow = 6)
out4 <- split_diploid_multi(gt4, anc4, pure_4, mixed_4)
names(out4)
#> [1] "AFR" "EUR" "NAT" "EAS"The proportions in A.1 and B.1 are estimated from the unambiguous carriers of each variant. Rare variants have few carriers, and an estimate from one or two of them can be far from the truth. Consider a variant carried by ten ambiguous AFR/EUR heterozygotes and a single EUR/EUR homozygote:
gt_dom <- matrix(c(rep(1L, 10), 2L), nrow = 1)
anc_dom <- matrix(c(rep(2L, 10), 1L), nrow = 1)
raw <- split_diploid_multi(gt_dom, anc_dom, pure_codes, mixed_codes)
c(AFR = sum(raw$AFR), EUR = sum(raw$EUR), NAT = sum(raw$NAT))
#> AFR EUR NAT
#> 0 12 0The only unambiguous carrier is European, so the estimate is , and all ten ambiguous alleles are assigned to European haplotypes on the strength of one sample.
ancestry_split() therefore pulls the estimated share
toward 1/2. How far depends on
,
the fraction of the variant’s alternate alleles that are ambiguous
(homozygotes count twice, as they carry two alleles). For an ambiguous
heterozygote with ancestries
and
,
When most of the evidence is unambiguous, is close to 0 and the estimate from A.1 or B.1 is used almost unchanged. When there is no unambiguous evidence, and the allele is split exactly 1/2 and 1/2, the same as the fallback in A.1. The shares stay within the pair, so an AFR/EUR heterozygote never gets Native American dosage.
The target is 1/2 because an AFR/EUR heterozygote has exactly one African and one European haplotype at the site. Which of the two carries the allele depends on how common the allele is on African versus European haplotypes, not on how much African ancestry the cohort has overall. An earlier version of lantern shrank toward the cohort’s average ancestry (about 0.82 African on chromosome 19 in our data). In simulations based on that data, ambiguous alleles were in fact on the African haplotype half of the time, and shrinking toward 1/2 assigned them more accurately.
The low-level functions take the target through their
gla argument (one row of target proportions) and
arm_id (which row each variant uses). A target of 1/K for
each of K populations gives 1/2 for every pair, which is what
ancestry_split() uses:
half <- matrix(1/3, nrow = 1, ncol = 3,
dimnames = list("all", c("AFR", "EUR", "NAT")))
shrunk <- split_diploid_multi(gt_dom, anc_dom, pure_codes, mixed_codes,
gla = half, arm_id = 0L)
rbind(raw = c(AFR = sum(raw$AFR), EUR = sum(raw$EUR)),
shrunk = c(AFR = sum(shrunk$AFR), EUR = sum(shrunk$EUR)))
#> AFR EUR
#> raw 0.000000 12.000000
#> shrunk 4.166667 7.833333Here 10 of the variant’s 12 alternate alleles are ambiguous, so . Each heterozygote’s African share becomes instead of 0:
shrunk$AFR[1, 1]
#> [1] 0.4166667The variant from A.1 has much more unambiguous evidence: one ambiguous allele out of seven, so , and S4’s African share moves only slightly, from 2/3 to :
# S4 is the 4th sample
split_diploid(gt, anc, gla = matrix(0.5, nrow = 1, ncol = 2), arm_id = 0L)$african[1, 4]
#> [1] 0.6428571You do not need to set the target yourself:
ancestry_split(mode = "dosage") always applies it.
sessionInfo()
#> R version 4.6.1 (2026-06-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.5 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] lantern_0.99.1
#>
#> loaded via a namespace (and not attached):
#> [1] digest_0.6.39 desc_1.4.3 R6_2.6.1 fastmap_1.2.0
#> [5] xfun_0.62 cachem_1.1.0 knitr_1.52 htmltools_0.5.9
#> [9] rmarkdown_2.32 lifecycle_1.0.5 cli_3.6.6 sass_0.4.10
#> [13] pkgdown_2.2.1 textshaping_1.0.5 jquerylib_0.1.4 systemfonts_1.3.2
#> [17] compiler_4.6.1 tools_4.6.1 ragg_1.5.2 bslib_0.12.0
#> [21] evaluate_1.0.5 yaml_2.3.12 otel_0.2.0 jsonlite_2.0.0
#> [25] rlang_1.3.0 fs_2.1.0