Complete pipeline: split genotype matrix by ancestry, return dosage matrices. Handles sample and variant mismatches by using only overlapping data. Memory efficient: single-pass subsetting using index vectors.

ancestry_split_dosage(
  gt_matrix = NULL,
  pt_matrix = NULL,
  vcf_path = NULL,
  msp_path = NULL,
  chrom = NULL,
  verbose = TRUE
)

Arguments

gt_matrix

Integer matrix of genotypes (rows=variants, cols=samples). Values: 0=homozygous ref, 1=heterozygous, 2=homozygous alt. rownames: variant IDs (e.g., "chr:pos"). colnames: sample IDs. May be NULL when vcf_path and msp_path are supplied.

pt_matrix

Integer matrix of parent-of-origin ancestry codes (rows=samples, cols=regions/variants). Values: 1=EUR/EUR, 2=AFR/EUR (mixed), 3=AFR/AFR. rownames: sample IDs (must match colnames of gt_matrix). colnames: region IDs (e.g., "chr:start-end"). May be NULL when vcf_path and msp_path are supplied.

vcf_path

Path to a phased VCF or BCF file. When supplied together with msp_path, the function delegates to ancestry_split (Step 1, mode = "dosage"), bypassing the gt_matrix/pt_matrix arguments.

msp_path

Path to an RFMix MSP file (plain text or gzipped TSV). Used together with vcf_path as a shortcut input.

chrom

Chromosome name to restrict to (e.g. "chr19"). Only used when vcf_path/msp_path are supplied.

verbose

Print progress messages (default TRUE)

Value

List with elements:

african

African ancestry-specific dosage matrix (variants x samples)

european

European ancestry-specific dosage matrix

counts

List (african, european, mixed) of per-variant counts of samples with ancestry code 3, 1 and 2

overlap

Overlap information

Shrinkage

Both input paths split ambiguous mixed-ancestry heterozygotes with the same shrinkage toward 1/2 as ancestry_split (see its "Shrinkage toward 1/2" section). The target is the same for every variant, so the direct gt_matrix/pt_matrix path needs no position information.

Matching variants to regions

Each variant (row of gt_matrix) is paired with an ancestry column of pt_matrix by, in order of preference: identical names; position, when rownames look like "chr:pos" and colnames like "chr:start-end" (the first region containing the variant; several variants may share a region); or, when either matrix has no names, position in the matrix.

Examples

# Example with mismatched dimensions
gt <- matrix(c(2, 1, 0, 1, 2, 1), nrow = 2, ncol = 3,
             dimnames = list(c("chr1:100", "chr1:200"),
                            c("sample_A", "sample_B", "sample_C")))

pt <- matrix(c(3, 2, 1, 3, 2, 1), nrow = 3, ncol = 2,
             dimnames = list(c("sample_A", "sample_B", "sample_D"),
                            c("chr1:50-150", "chr1:150-250")))

result <- ancestry_split_dosage(gt, pt)
#> === LANTERN Ancestry Pipeline ===
#> Step 1: Finding sample overlap...
#>   Samples: 3 in GT, 3 in PT
#>   Common samples: 2
#>   Dropped: sample_C, sample_D
#> 
#> Step 2: Finding variant/region overlap...
#>   No exact matches. Trying coordinate-based matching...
#>   Found 2 variants in 2 regions
#>   Variants in GT: 2
#>   Regions in PT: 2
#>   Matched variants: 2
#> 
#> Step 3: Subsetting matrices...
#> 
#> Step 3b: Filtering monomorphic variants...
#> 
#> Step 4: Splitting genotypes by ancestry...
#> 
#> Step 5: Counting ancestries per variant...
#> 
#> === Pipeline Complete ===
# Only sample_A and sample_B are kept (common to both)
# Only chr1:100 and chr1:200 that overlap regions are kept

# Shortcut: build matrices directly from VCF + MSP
vcf <- system.file("extdata", "toy.vcf.gz", package = "lantern")
msp <- system.file("extdata", "toy.msp.tsv", package = "lantern")
result <- ancestry_split_dosage(vcf_path = vcf, msp_path = msp,
                                chrom = "chr19", verbose = FALSE)