Using SNPknock with Genotype Data

Matteo Sesia (msesia@stanford.edu)

2018-01-09

This vignette illustrates the usage of the SNPknock package in combination with the imputation software fastPhase to create knockoff copies of genotype data. Since fastPHASE is not available as an R package, this particular functionality of SNPknock requires the user to first obtain a copy of fastPHASE.

Obtaining fastPHASE

fastPHASE is a program to estimate missing genotypes and unobserved haplotypes. It is an implementation of the hidden Markov model described in Scheet & Stephens (2006).

Binary executables for Linux and Mac OS are available from http://scheet.org/software.html.

Download and extract the fastPHASE tarball from the above link and move the fastPHASE executable file into a convenient directory (e.g. β€œ~/bin/”).

Fitting the hidden Markov model

A small synthetic dataset can be found in the package installation directory, including a synthetic X matrix with 300 SNPs from 100 samples. We can load this with:

library(SNPknock)
X_file = system.file("extdata", "X.RData", package = "SNPknock")
load(X_file)
table(X)
## X
##     0     1     2 
##  9925  9135 10940

Below, we show how to fit a hidden Markov model to this data, with the help of fastPHASE. Since fastPHASE takes as input genotype sequences in β€œ.inp” format, we must first convert the X matrix by calling SNPknock.fp.writeX. By default, this function will write onto a temporary file in the R temporary directory.

# Convert X into the suitable fastPhase input format, write it into a temporary file
# and return the path to that file.
Xinp_file = SNPknock.fp.writeX(X)

Assuming that we have already downloaded fastPHASE, we can call it to fit the hidden Markov model to X.

fp_path  = "~/bin/fastPHASE" # Path to the fastPHASE executable
# Call fastPhase and return the path to the parameter estimate files
fp_outPath = SNPknock.fp.runFastPhase(fp_path, Xinp_file)
## SNPknock could find the fastPhase executable: '~/bin/fastPHASE' does not exist.
## If you have not downloaded it yet, you can obtain fastPhase from: http://scheet.org/software.html

Above, the SNPknock package could not find fastPhase because we did not provide the correct path (we cannot include third-party executable files within this package). However, if you install fastPhase separately and provide SNPknock with the correct path, this will work.

If the previous step worked for you, you can find the parameter estimates produced by fastPHASE in the following files:

r_file = paste(fp_outPath, "_rhat.txt", sep="")
theta_file = paste(fp_outPath, "_thetahat.txt", sep="")
alpha_file = paste(fp_outPath, "_alphahat.txt", sep="")

Otherwise, for the sake of this tutorial, you can use the example parameter files provided in the package installation directory:

r_file = system.file("extdata", "X_rhat.txt", package = "SNPknock")
theta_file = system.file("extdata", "X_thetahat.txt", package = "SNPknock")
alpha_file = system.file("extdata", "X_alphahat.txt", package = "SNPknock")

Then, we can construct the hidden Markov model with:

hmm = SNPknock.fp.loadFit(r_file, theta_file, alpha_file, X[1,])

Creating the knockoffs

Finally, we can use this hidden Markov model to create the knockoffs.

Xk = SNPknock.knockoffHMM(X, hmm$pInit, hmm$Q, hmm$pEmit)
table(Xk)
## Xk
##     0     1     2 
##  9965  9048 10987

See also

If you want to see some basic usage of SNPknock, see the introductory vignette.