This implements the function to index genotypes so that we can put them into matrices. The paper mentions this indexing, in the section "Implementation."

index_ab(a, b, A)

Arguments

a

The first allele in the genotype

b

The second allele in the genotype

A

the total number of alleles at the locus in the population.

Details

If a is not an allele with index lower than b, then this function automatically reorients the alleles so that it is. This is vectorized and so should work over long vectors of alleles.

Examples

# Create some data to test/demonstrate this:
A <- 7
all_genos <- expand.grid(1:A, 1:A)[,c(2,1)] %>%
 setNames(c("a", "b"))

# when a < b
right_ord <- all_genos %>%
 dplyr::filter(a <= b) %>%
dplyr::mutate(Idx = 1:length(a),
      Indab = index_ab(a, b, A))

# when a > b
wrong_ord <- all_genos %>%
 dplyr::filter(a >= b) %>%
 dplyr::arrange(b, a) %>%
 dplyr::mutate(
   Idx = 1:length(a),
   Indab = index_ab(b, a, A)
)

# then check to make sure it all checks out
all(right_ord$Idx == right_ord$Indab)
#> [1] TRUE
all(wrong_ord$Idx == wrong_ord$Indab)
#> [1] TRUE