For columns where the allele frequency exceeds 0.5 (i.e., the coded allele is the major allele), flips dosage values: 0 becomes 2 and 2 becomes 0, with 1 unchanged. This ensures all variants are coded relative to the minor allele.

flip_alleles(G)

Arguments

G

Numeric matrix (n samples x p variants) of dosage values (0, 1, 2). Missing values (NA) should be imputed before calling this function.

Value

Numeric matrix of same dimensions with flipped columns where needed.

Examples

# Variant 2 has AF > 0.5, so it gets flipped
G <- matrix(c(0, 0, 2, 2,   # variant 1: AF = 0.5 (no flip)
              2, 2, 0, 0),   # variant 2: AF = 0.5 (no flip)
            nrow = 4, ncol = 2)
flip_alleles(G)
#>      [,1] [,2]
#> [1,]    0    2
#> [2,]    0    2
#> [3,]    2    0
#> [4,]    2    0

# Variant with AF = 0.75 gets flipped: 2→0, 0→2
G2 <- matrix(c(2, 2, 2, 0), ncol = 1)  # AF = 0.75
flip_alleles(G2)  # becomes c(0, 0, 0, 2)
#>      [,1]
#> [1,]    0
#> [2,]    0
#> [3,]    0
#> [4,]    2