Skip to contents

This function takes a data table with genotypes ("AA", "AB", "BA" or "BB") and converts them to their corresponding zygosity, i.e., to homozygous ("hom") or heterozygous ("het").

The argument genotypes is expected to be a data table whose first column is the SNP identifier, and remaining columns refer to samples on which the SNP has been genotyped. All columns except the first should be factors, and should contain the levels "AA", "AB", "BA" and "BB".

Note that this function changes its input by reference, meaning that the variable passed as genotypes will be modified after running as_zygous(). This is for performance reasons.

Usage

as_zygous(genotypes)

Arguments

genotypes

A data table whose first column is the SNP identifier (a character vector), and remaining columns refer to samples on which the SNP has been genotyped. All columns except the first should be factors, and should contain the levels "AA", "AB", "BA" and "BB".

Value

A data table with the same structure as the data table passed in genotypes. The only difference is that the sample columns (all except the first) have their levels recoded to "hom" (homozygous) or "het"(heterozygous).

Examples

levels <- c("AA", "AB", "BA", "BB")
(df <- data.frame(
  snp = c('rs123', 'rs456'),
  sample01 = factor(c('AB', 'BA'), levels),
  sample02 = factor(c('AA', 'BB'), levels)
))
#>     snp sample01 sample02
#> 1 rs123       AB       AA
#> 2 rs456       BA       BB
as_zygous(df)
#>     snp sample01 sample02
#> 1 rs123      het      hom
#> 2 rs456      het      hom
df
#>     snp sample01 sample02
#> 1 rs123      het      hom
#> 2 rs456      het      hom