R version of SNP-HWE

The R version of SNP-HWE can be downloaded by following the link below and right clicking on the source code. Once this function has been incorporated into your R program, it can be called by passing observed counts for each of the three SNP genotypes (heterozygote, homozygote_1, and homozygote_2) as function arguments. The code snippet below illustrates a typical calling sequence to read in genotype counts listed in a text file ("genotype_counts_r.txt") and perform the exact test of Hardy-Weinberg on each marker:

genotype_counts_r.txt:

          HET   HOM1  HOM2
MARKER_1  100    3    5
MARKER_2   57   14   50
MARKER_3   31   32   51
MARKER_4   47    3    5
MARKER_5  150   32   55
MARKER_6  122    7   32
MARKER_7   99    3   14
MARKER_8  146   13   54
MARKER_9  177  100   57
MARKER_10 184   57  155


Calling code from run_marker_tests.r:

data <- read.table("genotype_counts_r.txt")                
n_markers <- 10
p_values <-rep(0, n_markers)
   
test_markers <- function()
   {
   for (i in 1:n_markers)
      {
      hets  <- data[i, 1]
      hom_1 <- data[i, 2]
      hom_2 <- data[i, 3]   
      p_values[i] <- SNPHWE(hets, hom_1, hom_2)
      print(p_values[i])
      }
   }
   
test_markers()

Source distribution

    SNP-HWE (R)