Genome-wide association studies (GWAS) are widely used to help determine the genetic basis of diseases and traits, but they pose many computational challenges. We developed gdsfmt and SNPRelate (high-performance computing R packages for multi-core symmetric multiprocessing computer architectures) to accelerate two key computations in GWAS: principal component analysis (PCA) and relatedness analysis using identity-by-descent (IBD) measures1. The kernels of our algorithms are written in C/C++ and have been highly optimized. The calculations of the genetic covariance matrix in PCA and pairwise IBD coefficients are split into non-overlapping parts and assigned to multiple cores for performance acceleration, as shown in Figure 1.
GDS is also used by the R/Bioconductor package GWASTools as one of its data storage formats2,3. GWASTools provides many functions for quality control and analysis of GWAS, including statistics by SNP or scan, batch quality, chromosome anomalies, association tests, etc.
Figure 1: Flowchart of parallel computing for principal component analysis and identity-by-descent analysis.
~
R is the most popular statistical programming environment, but one not typically optimized for high performance or parallel computing which would ease the burden of large-scale GWAS calculations. To overcome these limitations we have developed a project named CoreArray (http://corearray.sourceforge.net/) that includes two R packages: gdsfmt to provide efficient, platform independent memory and file management for genome-wide numerical data, and SNPRelate to solve large-scale, numerically intensive GWAS calculations (i.e., PCA and IBD) on multi-core symmetric multiprocessing (SMP) computer architectures.
This vignette takes the user through the relatedness and principal component analysis used for genome wide association data. The methods in these vignettes have been introduced in the paper of Zheng et al. (2012)1. For replication purposes the data used here are taken from the HapMap Phase II project. These data were kindly provided by the Center for Inherited Disease Research (CIDR) at Johns Hopkins University and the Broad Institute of MIT and Harvard University (Broad). The data supplied here should not be used for any purpose other than this tutorial.
To install the package SNPRelate, you need a current version (>=2.14.0) of R and the R package gdsfmt. After installing R you can run the following commands from the R command shell to install the R package SNPRelate.
Install the package from Bioconductor repository:
source("http://bioconductor.org/biocLite.R")
biocLite("gdsfmt")
biocLite("SNPRelate")
Install the development version from Github:
library("devtools")
install_github("zhengxwen/gdsfmt")
install_github("zhengxwen/SNPRelate")
The install_github()
approach requires that you build from source, i.e. make
and compilers must be installed on your system – see the R FAQ for your operating system; you may also need to install dependencies manually.
To support efficient memory management for genome-wide numerical data, the gdsfmt package provides the genomic data structure (GDS) file format for array-oriented bioinformatic data, which is a container for storing annotation data and SNP genotypes. In this format each byte encodes up to four SNP genotypes thereby reducing file size and access time. The GDS format supports data blocking so that only the subset of data that is being processed needs to reside in memory. GDS formatted data is also designed for efficient random access to large data sets. A tutorial for the R/Bioconductor package gdsfmt can be found: http://corearray.sourceforge.net/tutorials/gdsfmt/.
# Load the R packages: gdsfmt and SNPRelate
library(gdsfmt)
library(SNPRelate)
## SNPRelate -- supported by Streaming SIMD Extensions 2 (SSE2)
Here is a typical GDS file:
snpgdsSummary(snpgdsExampleFileName())
## The file name: /tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/hapmap_geno.gds
## The total number of samples: 279
## The total number of SNPs: 9088
## SNP genotypes are stored in SNP-major mode (Sample X SNP).
snpgdsExampleFileName()
returns the file name of a GDS file used as an example in SNPRelate, and it is a subset of data from the HapMap project and the samples were genotyped by the Center for Inherited Disease Research (CIDR) at Johns Hopkins University and the Broad Institute of MIT and Harvard University (Broad). snpgdsSummary()
summarizes the genotypes stored in the GDS file. “Individual-major mode” indicates listing all SNPs for an individual before listing the SNPs for the next individual, etc. Conversely, “SNP-major mode” indicates listing all individuals for the first SNP before listing all individuals for the second SNP, etc. Sometimes “SNP-major mode” is more computationally efficient than “individual-major model”. For example, the calculation of genetic covariance matrix deals with genotypic data SNP by SNP, and then “SNP-major mode” should be more efficient.
# Open a GDS file
(genofile <- snpgdsOpen(snpgdsExampleFileName()))
## File: /tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/hapmap_geno.gds (709.6K)
## + [ ] *
## |--+ sample.id { VStr8 279 ZIP(29.9%), 679B }
## |--+ snp.id { Int32 9088 ZIP(34.8%), 12.3K }
## |--+ snp.rs.id { VStr8 9088 ZIP(40.1%), 36.2K }
## |--+ snp.position { Int32 9088 ZIP(94.7%), 33.6K }
## |--+ snp.chromosome { UInt8 9088 ZIP(0.94%), 85B } *
## |--+ snp.allele { VStr8 9088 ZIP(11.3%), 4.0K }
## |--+ genotype { Bit2 279x9088, 619.0K } *
## \--+ sample.annot [ data.frame ] *
## |--+ family.id { VStr8 279 ZIP(34.4%), 514B }
## |--+ father.id { VStr8 279 ZIP(31.5%), 220B }
## |--+ mother.id { VStr8 279 ZIP(30.9%), 214B }
## |--+ sex { VStr8 279 ZIP(17.0%), 95B }
## \--+ pop.group { VStr8 279 ZIP(6.18%), 69B }
The output lists all variables stored in the GDS file. At the first level, it stores variables sample.id, snp.id, etc. The additional information are displayed in the braces indicating data type, size, compressed or not + compression ratio. The second-level variables sex and pop.group are both stored in the folder of sample.annot. All of the functions in SNPRelate require a minimum set of variables in the annotation data. The minimum required variables are
Users can define the numeric chromosome codes which are stored with the variable snp.chromosome as its attributes when snp.chromosome is numeric only. For example, snp.chromosome has the attributes of chromosome coding:
# Get the attributes of chromosome coding
get.attr.gdsn(index.gdsn(genofile, "snp.chromosome"))
## $autosome.start
## [1] 1
##
## $autosome.end
## [1] 22
##
## $X
## [1] 23
##
## $XY
## [1] 24
##
## $Y
## [1] 25
##
## $M
## [1] 26
##
## $MT
## [1] 26
autosome.start is the starting numeric code of autosomes, and autosome.end is the last numeric code of autosomes. put.attr.gdsn()
can be used to add a new attribute or modify an existing attribute.
There are four possible values stored in the variable genotype: 0, 1, 2 and 3. For bi-allelic SNP sites, “0” indicates two B alleles, “1” indicates one A allele and one B allele, “2” indicates two A alleles, and “3” is a missing genotype. For multi-allelic sites, it is a count of the reference allele (3 meaning no call). “Bit2” indicates that each byte encodes up to four SNP genotypes since one byte consists of eight bits.
# Take out genotype data for the first 3 samples and the first 5 SNPs
(g <- read.gdsn(index.gdsn(genofile, "genotype"), start=c(1,1), count=c(5,3)))
## [,1] [,2] [,3]
## [1,] 2 1 0
## [2,] 1 1 0
## [3,] 2 1 1
## [4,] 2 1 1
## [5,] 0 0 0
Or take out genotype data with sample and SNP IDs, and four possible values are returned 0, 1, 2 and NA (3 is replaced by NA):
g <- snpgdsGetGeno(genofile, sample.id=..., snp.id=...)
# Get the attribute of genotype
get.attr.gdsn(index.gdsn(genofile, "genotype"))
## $sample.order
## NULL
The returned value could be either “snp.order” or “sample.order”, indicating individual-major mode (snp is the first dimension) and SNP-major mode (sample is the first dimension) respectively.
# Take out snp.id
head(read.gdsn(index.gdsn(genofile, "snp.id")))
## [1] 1 2 3 4 5 6
# Take out snp.rs.id
head(read.gdsn(index.gdsn(genofile, "snp.rs.id")))
## [1] "rs1695824" "rs13328662" "rs4654497" "rs10915489" "rs12132314"
## [6] "rs12042555"
There are two additional and optional variables:
The information of sample annotation can be obtained by the same function read.gdsn()
. For example, population information. “VStr8” indicates a character-type variable.
# Read population information
pop <- read.gdsn(index.gdsn(genofile, path="sample.annot/pop.group"))
table(pop)
## pop
## CEU HCB JPT YRI
## 92 47 47 93
# Close the GDS file
snpgdsClose(genofile)
The function snpgdsCreateGeno()
can be used to create a GDS file. The first argument should be a numeric matrix for SNP genotypes. There are possible values stored in the input genotype matrix: 0, 1, 2 and other values. “0” indicates two B alleles, “1” indicates one A allele and one B allele, “2” indicates two A alleles, and other values indicate a missing genotype. The SNP matrix can be either \(n_{sample} \times n_{snp}\) (snpfirstdim=FALSE, the argument in snpgdsCreateGeno()
) or \(n_{snp} \times n_{sample}\) (snpfirstdim=TRUE).
For example,
# Load data
data(hapmap_geno)
# Create a gds file
snpgdsCreateGeno("test.gds", genmat = hapmap_geno$genotype,
sample.id = hapmap_geno$sample.id, snp.id = hapmap_geno$snp.id,
snp.chromosome = hapmap_geno$snp.chromosome,
snp.position = hapmap_geno$snp.position,
snp.allele = hapmap_geno$snp.allele, snpfirstdim=TRUE)
# Open the GDS file
(genofile <- snpgdsOpen("test.gds"))
## File: /tmp/Rtmpf2Kf1D/Rbuild6bd67b09c5da/SNPRelate/vignettes/test.gds (79.6K)
## + [ ] *
## |--+ sample.id { Str8 279 ZIP_ra(31.2%), 715B }
## |--+ snp.id { Str8 1000 ZIP_ra(43.7%), 4.4K }
## |--+ snp.position { Float64 1000 ZIP_ra(56.1%), 4.4K }
## |--+ snp.chromosome { Int32 1000 ZIP_ra(2.25%), 97B }
## |--+ snp.allele { Str8 1000 ZIP_ra(14.1%), 571B }
## \--+ genotype { Bit2 1000x279, 68.1K } *
# Close the GDS file
snpgdsClose(genofile)
In the following code, the functions createfn.gds()
, add.gdsn()
, put.attr.gdsn()
, write.gdsn()
and index.gdsn()
are defined in the package gdsfmt:
# Create a new GDS file
newfile <- createfn.gds("your_gds_file.gds")
# add a flag
put.attr.gdsn(newfile$root, "FileFormat", "SNP_ARRAY")
# Add variables
add.gdsn(newfile, "sample.id", sample.id)
add.gdsn(newfile, "snp.id", snp.id)
add.gdsn(newfile, "snp.chromosome", snp.chromosome)
add.gdsn(newfile, "snp.position", snp.position)
add.gdsn(newfile, "snp.allele", c("A/G", "T/C", ...))
#####################################################################
# Create a snp-by-sample genotype matrix
# Add genotypes
var.geno <- add.gdsn(newfile, "genotype",
valdim=c(length(snp.id), length(sample.id)), storage="bit2")
# Indicate the SNP matrix is snp-by-sample
put.attr.gdsn(var.geno, "snp.order")
# Write SNPs into the file sample by sample
for (i in 1:length(sample.id))
{
g <- ...
write.gdsn(var.geno, g, start=c(1,i), count=c(-1,1))
}
#####################################################################
# OR, create a sample-by-snp genotype matrix
# Add genotypes
var.geno <- add.gdsn(newfile, "genotype",
valdim=c(length(sample.id), length(snp.id)), storage="bit2")
# Indicate the SNP matrix is sample-by-snp
put.attr.gdsn(var.geno, "sample.order")
# Write SNPs into the file sample by sample
for (i in 1:length(snp.id))
{
g <- ...
write.gdsn(var.geno, g, start=c(1,i), count=c(-1,1))
}
# Get a description of chromosome codes
# allowing to define a new chromosome code, e.g., snpgdsOption(Z=27)
option <- snpgdsOption()
var.chr <- index.gdsn(newfile, "snp.chromosome")
put.attr.gdsn(var.chr, "autosome.start", option$autosome.start)
put.attr.gdsn(var.chr, "autosome.end", option$autosome.end)
for (i in 1:length(option$chromosome.code))
{
put.attr.gdsn(var.chr, names(option$chromosome.code)[i],
option$chromosome.code[[i]])
}
# Add your sample annotation
samp.annot <- data.frame(sex = c("male", "male", "female", ...),
pop.group = c("CEU", "CEU", "JPT", ...), ...)
add.gdsn(newfile, "sample.annot", samp.annot)
# Add your SNP annotation
snp.annot <- data.frame(pass=c(TRUE, TRUE, FALSE, FALSE, TRUE, ...), ...)
add.gdsn(newfile, "snp.annot", snp.annot)
# Close the GDS file
closefn.gds(newfile)
The SNPRelate package provides a function snpgdsPED2GDS()
and snpgdsBED2GDS()
for converting a PLINK text/binary file to a GDS file:
# The PLINK BED file, using the example in the SNPRelate package
bed.fn <- system.file("extdata", "plinkhapmap.bed.gz", package="SNPRelate")
fam.fn <- system.file("extdata", "plinkhapmap.fam.gz", package="SNPRelate")
bim.fn <- system.file("extdata", "plinkhapmap.bim.gz", package="SNPRelate")
Or, uses your PLINK files:
bed.fn <- "C:/your_folder/your_plink_file.bed"
fam.fn <- "C:/your_folder/your_plink_file.fam"
bim.fn <- "C:/your_folder/your_plink_file.bim"
# Convert
snpgdsBED2GDS(bed.fn, fam.fn, bim.fn, "test.gds")
## Start snpgdsBED2GDS ...
## BED file: "/tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/plinkhapmap.bed.gz" in the SNP-major mode (Sample X SNP)
## FAM file: "/tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/plinkhapmap.fam.gz", DONE.
## BIM file: "/tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/plinkhapmap.bim.gz", DONE.
## Mon Dec 18 19:42:47 2017 store sample id, snp id, position, and chromosome.
## start writing: 60 samples, 5000 SNPs ...
## Mon Dec 18 19:42:47 2017 0%
## Mon Dec 18 19:42:47 2017 100%
## Mon Dec 18 19:42:47 2017 Done.
## Optimize the access efficiency ...
## Clean up the fragments of GDS file:
## open the file 'test.gds' (104.6K)
## # of fragments: 38
## save to 'test.gds.tmp'
## rename 'test.gds.tmp' (104.4K, reduced: 240B)
## # of fragments: 18
# Summary
snpgdsSummary("test.gds")
## The file name: /tmp/Rtmpf2Kf1D/Rbuild6bd67b09c5da/SNPRelate/vignettes/test.gds
## The total number of samples: 60
## The total number of SNPs: 5000
## SNP genotypes are stored in SNP-major mode (Sample X SNP).
The SNPRelate package provides a function snpgdsVCF2GDS()
to reformat a VCF file. There are two options for extracting markers from a VCF file for downstream analyses: 1. to extract and store dosage of the reference allele only for biallelic SNPs 2. to extract and store dosage of the reference allele for all variant sites, including bi-allelic SNPs, multi-allelic SNPs, indels and structural variants.
# The VCF file, using the example in the SNPRelate package
vcf.fn <- system.file("extdata", "sequence.vcf", package="SNPRelate")
Or, uses your VCF file:
vcf.fn <- "C:/your_folder/your_vcf_file.vcf"
# Reformat
snpgdsVCF2GDS(vcf.fn, "test.gds", method="biallelic.only")
## VCF Format ==> SNP GDS Format
## Method: exacting biallelic SNPs
## Number of samples: 3
## Parsing "/tmp/Rtmpf2Kf1D/Rinst6bd6375da7e4/SNPRelate/extdata/sequence.vcf" ...
## import 2 variants.
## + genotype { Bit2 3x2, 2B } *
## Optimize the access efficiency ...
## Clean up the fragments of GDS file:
## open the file 'test.gds' (2.4K)
## # of fragments: 39
## save to 'test.gds.tmp'
## rename 'test.gds.tmp' (2.2K, reduced: 228B)
## # of fragments: 20
# Summary
snpgdsSummary("test.gds")
## The file name: /tmp/Rtmpf2Kf1D/Rbuild6bd67b09c5da/SNPRelate/vignettes/test.gds
## The total number of samples: 3
## The total number of SNPs: 2
## SNP genotypes are stored in SNP-major mode (Sample X SNP).
We developed gdsfmt and SNPRelate (high-performance computing R packages for multi-core symmetric multiprocessing computer architectures) to accelerate two key computations in GWAS: principal component analysis (PCA) and relatedness analysis using identity-by-descent (IBD) measures.
# Open the GDS file
genofile <- snpgdsOpen(snpgdsExampleFileName())
# Get population information
# or pop_code <- scan("pop.txt", what=character())
# if it is stored in a text file "pop.txt"
pop_code <- read.gdsn(index.gdsn(genofile, path="sample.annot/pop.group"))
table(pop_code)
## pop_code
## CEU HCB JPT YRI
## 92 47 47 93
# Display the first six values
head(pop_code)
## [1] "YRI" "YRI" "YRI" "YRI" "CEU" "CEU"
It is suggested to use a pruned set of SNPs which are in approximate linkage equilibrium with each other to avoid the strong influence of SNP clusters in principal component analysis and relatedness analysis.
set.seed(1000)
# Try different LD thresholds for sensitivity analysis
snpset <- snpgdsLDpruning(genofile, ld.threshold=0.2)
## SNP pruning based on LD:
## Excluding 365 SNPs on non-autosomes
## Excluding 1 SNP (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 279 samples, 8,722 SNPs
## using 1 (CPU) core
## sliding window: 500,000 basepairs, Inf SNPs
## |LD| threshold: 0.2
## method: composite
## Chromosome 1: 75.42%, 540/716
## Chromosome 2: 72.24%, 536/742
## Chromosome 3: 74.71%, 455/609
## Chromosome 4: 73.31%, 412/562
## Chromosome 5: 77.03%, 436/566
## Chromosome 6: 75.58%, 427/565
## Chromosome 7: 75.42%, 356/472
## Chromosome 8: 71.31%, 348/488
## Chromosome 9: 77.88%, 324/416
## Chromosome 10: 74.33%, 359/483
## Chromosome 11: 77.40%, 346/447
## Chromosome 12: 76.81%, 328/427
## Chromosome 13: 75.58%, 260/344
## Chromosome 14: 76.95%, 217/282
## Chromosome 15: 76.34%, 200/262
## Chromosome 16: 72.66%, 202/278
## Chromosome 17: 74.40%, 154/207
## Chromosome 18: 73.68%, 196/266
## Chromosome 19: 85.00%, 102/120
## Chromosome 20: 71.62%, 164/229
## Chromosome 21: 76.98%, 97/126
## Chromosome 22: 75.86%, 88/116
## 6,547 markers are selected in total.
names(snpset)
## [1] "chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8"
## [9] "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16"
## [17] "chr17" "chr18" "chr19" "chr20" "chr21" "chr22"
head(snpset$chr1) # snp.id
## [1] 1 2 4 5 7 10
# Get all selected snp id
snpset.id <- unlist(snpset)
The functions in SNPRelate for PCA include calculating the genetic covariance matrix from genotypes, computing the correlation coefficients between sample loadings and genotypes for each SNP, calculating SNP eigenvectors (loadings), and estimating the sample loadings of a new dataset from specified SNP eigenvectors.
# Run PCA
pca <- snpgdsPCA(genofile, snp.id=snpset.id, num.thread=2)
## Principal Component Analysis (PCA) on genotypes:
## Excluding 2,541 SNPs (non-autosomes or non-selection)
## Excluding 0 SNP (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 279 samples, 6,547 SNPs
## using 2 (CPU) cores
## PCA: the sum of all selected genotypes (0,1,2) = 1826801
## CPU capabilities: Double-Precision SSE2
## Mon Dec 18 19:42:48 2017 (internal increment: 11612)
##
[..................................................] 0%, ETC: ---
[==================================================] 100%, completed in 0s
## Mon Dec 18 19:42:48 2017 Begin (eigenvalues and eigenvectors)
## Mon Dec 18 19:42:48 2017 Done.
The code below shows how to calculate the percent of variation is accounted for by the top principal components. It is clear to see the first two eigenvectors hold the largest percentage of variance among the population, although the total variance accounted for is still less the one-quarter of the total.
# variance proportion (%)
pc.percent <- pca$varprop*100
head(round(pc.percent, 2))
## [1] 10.34 5.56 1.04 0.98 0.87 0.78
In the case of no prior population information,
# make a data.frame
tab <- data.frame(sample.id = pca$sample.id,
EV1 = pca$eigenvect[,1], # the first eigenvector
EV2 = pca$eigenvect[,2], # the second eigenvector
stringsAsFactors = FALSE)
head(tab)
## sample.id EV1 EV2
## 1 NA19152 -0.08246488 -0.01006984
## 2 NA19139 -0.08225226 -0.01051959
## 3 NA18912 -0.08182177 -0.01274934
## 4 NA19160 -0.08793052 -0.01371887
## 5 NA07034 0.03160206 0.07831394
## 6 NA07055 0.03456066 0.08270710
# Draw
plot(tab$EV2, tab$EV1, xlab="eigenvector 2", ylab="eigenvector 1")
If there are population information,
# Get sample id
sample.id <- read.gdsn(index.gdsn(genofile, "sample.id"))
# Get population information
# or pop_code <- scan("pop.txt", what=character())
# if it is stored in a text file "pop.txt"
pop_code <- read.gdsn(index.gdsn(genofile, "sample.annot/pop.group"))
# assume the order of sample IDs is as the same as population codes
head(cbind(sample.id, pop_code))
## sample.id pop_code
## [1,] "NA19152" "YRI"
## [2,] "NA19139" "YRI"
## [3,] "NA18912" "YRI"
## [4,] "NA19160" "YRI"
## [5,] "NA07034" "CEU"
## [6,] "NA07055" "CEU"
# Make a data.frame
tab <- data.frame(sample.id = pca$sample.id,
pop = factor(pop_code)[match(pca$sample.id, sample.id)],
EV1 = pca$eigenvect[,1], # the first eigenvector
EV2 = pca$eigenvect[,2], # the second eigenvector
stringsAsFactors = FALSE)
head(tab)
## sample.id pop EV1 EV2
## 1 NA19152 YRI -0.08246488 -0.01006984
## 2 NA19139 YRI -0.08225226 -0.01051959
## 3 NA18912 YRI -0.08182177 -0.01274934
## 4 NA19160 YRI -0.08793052 -0.01371887
## 5 NA07034 CEU 0.03160206 0.07831394
## 6 NA07055 CEU 0.03456066 0.08270710
# Draw
plot(tab$EV2, tab$EV1, col=as.integer(tab$pop), xlab="eigenvector 2", ylab="eigenvector 1")
legend("bottomright", legend=levels(tab$pop), pch="o", col=1:nlevels(tab$pop))
Plot the principal component pairs for the first four PCs:
lbls <- paste("PC", 1:4, "\n", format(pc.percent[1:4], digits=2), "%", sep="")
pairs(pca$eigenvect[,1:4], col=tab$pop, labels=lbls)
Parallel coordinates plot for the top principal components:
library(MASS)
datpop <- factor(pop_code)[match(pca$sample.id, sample.id)]
parcoord(pca$eigenvect[,1:16], col=datpop)
To calculate the SNP correlations between eigenvactors and SNP genotypes:
# Get chromosome index
chr <- read.gdsn(index.gdsn(genofile, "snp.chromosome"))
CORR <- snpgdsPCACorr(pca, genofile, eig.which=1:4)
## SNP Correlation:
## Working space: 279 samples, 9088 SNPs
## using 1 (CPU) core
## using the top 4 eigenvectors
## Correlation: the sum of all selected genotypes (0,1,2) = 2553065
## Mon Dec 18 19:42:50 2017 (internal increment: 65536)
##
[..................................................] 0%, ETC: ---
[==================================================] 100%, completed in 0s
## Mon Dec 18 19:42:50 2017 Done.
savepar <- par(mfrow=c(2,1), mai=c(0.45, 0.55, 0.1, 0.25))
for (i in 1:2)
{
plot(abs(CORR$snpcorr[i,]), ylim=c(0,1), xlab="", ylab=paste("PC", i),
col=chr, pch="+")
}
par(savepar)
Given two or more populations, \(F_{st}\) can be estimated by the method of Weir & Cockerham (1984).
# Get sample id
sample.id <- read.gdsn(index.gdsn(genofile, "sample.id"))
# Get population information
# or pop_code <- scan("pop.txt", what=character())
# if it is stored in a text file "pop.txt"
pop_code <- read.gdsn(index.gdsn(genofile, "sample.annot/pop.group"))
# Two populations: HCB and JPT
flag <- pop_code %in% c("HCB", "JPT")
samp.sel <- sample.id[flag]
pop.sel <- pop_code[flag]
v <- snpgdsFst(genofile, sample.id=samp.sel, population=as.factor(pop.sel),
method="W&C84")
## Fst estimation on genotypes:
## Excluding 365 SNPs on non-autosomes
## Excluding 1,682 SNPs (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 94 samples, 7,041 SNPs
## Method: Weir & Cockerham, 1984
## # of Populations: 2
## HCB (47), JPT (47)
v$Fst # Weir and Cockerham weighted Fst estimate
## [1] 0.007560346
v$MeanFst # Weir and Cockerham mean Fst estimate
## [1] 0.00703106
summary(v$FstSNP)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -0.022312 -0.008565 -0.001147 0.007031 0.012537 0.193880 1
# Multiple populations: CEU HCB JPT YRI
# we should remove offsprings
father <- read.gdsn(index.gdsn(genofile, "sample.annot/father.id"))
mother <- read.gdsn(index.gdsn(genofile, "sample.annot/mother.id"))
flag <- (father=="") & (mother=="")
samp.sel <- sample.id[flag]
pop.sel <- pop_code[flag]
v <- snpgdsFst(genofile, sample.id=samp.sel, population=as.factor(pop.sel),
method="W&C84")
## Fst estimation on genotypes:
## Excluding 365 SNPs on non-autosomes
## Excluding 1 SNP (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 219 samples, 8,722 SNPs
## Method: Weir & Cockerham, 1984
## # of Populations: 4
## CEU (62), HCB (47), JPT (47), YRI (63)
v$Fst # Weir and Cockerham weighted Fst estimate
## [1] 0.1377293
v$MeanFst # Weir and Cockerham mean Fst estimate
## [1] 0.1206991
summary(v$FstSNP)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -0.009225 0.042554 0.091801 0.120699 0.167754 0.792465 1
For \(n\) study individuals, snpgdsIBS()
can be used to create a \(n \times n\) matrix of genome-wide average IBS pairwise identities:
ibs <- snpgdsIBS(genofile, num.thread=2)
## Identity-By-State (IBS) analysis on genotypes:
## Excluding 365 SNPs on non-autosomes
## Excluding 1 SNP (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 279 samples, 8,722 SNPs
## using 2 (CPU) cores
## IBS: the sum of all selected genotypes (0,1,2) = 2446510
## Mon Dec 18 19:43:25 2017 (internal increment: 65536)
##
[..................................................] 0%, ETC: ---
[==================================================] 100%, completed in 0s
## Mon Dec 18 19:43:25 2017 Done.
The heat map is shown:
# individulas in the same population are clustered together
pop.idx <- order(pop_code)
image(ibs$ibs[pop.idx, pop.idx], col=terrain.colors(16))
To perform multidimensional scaling analysis on the \(n \times n\) matrix of genome-wide IBS pairwise distances:
loc <- cmdscale(1 - ibs$ibs, k = 2)
x <- loc[, 1]; y <- loc[, 2]
race <- as.factor(pop_code)
plot(x, y, col=race, xlab = "", ylab = "",
main = "Multidimensional Scaling Analysis (IBS)")
legend("topleft", legend=levels(race), pch="o", text.col=1:nlevels(race))
To perform cluster analysis on the \(n \times n\) matrix of genome-wide IBS pairwise distances, and determine the groups by a permutation score:
set.seed(100)
ibs.hc <- snpgdsHCluster(snpgdsIBS(genofile, num.thread=2))
## Identity-By-State (IBS) analysis on genotypes:
## Excluding 365 SNPs on non-autosomes
## Excluding 1 SNP (monomorphic: TRUE, MAF: NaN, missing rate: NaN)
## Working space: 279 samples, 8,722 SNPs
## using 2 (CPU) cores
## IBS: the sum of all selected genotypes (0,1,2) = 2446510
## Mon Dec 18 19:43:25 2017 (internal increment: 65536)
##
[..................................................] 0%, ETC: ---
[==================================================] 100%, completed in 1s
## Mon Dec 18 19:43:26 2017 Done.
# Determine groups of individuals automatically
rv <- snpgdsCutTree(ibs.hc)
## Determine groups by permutation (Z threshold: 15, outlier threshold: 5):
## Create 3 groups.
plot(rv$dendrogram, leaflab="none", main="HapMap Phase II")
table(rv$samp.group)
##
## G001 G002 G003
## 93 94 92
Here is the population information we have known:
# Determine groups of individuals by population information
rv2 <- snpgdsCutTree(ibs.hc, samp.group=as.factor(pop_code))
## Create 4 groups.
plot(rv2$dendrogram, leaflab="none", main="HapMap Phase II")
legend("topright", legend=levels(race), col=1:nlevels(race), pch=19, ncol=4)
# Close the GDS file
snpgdsClose(genofile)
See: R Integration.
sessionInfo()
## R version 3.4.3 (2017-11-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.6-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.6-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] MASS_7.3-47 SNPRelate_1.12.2 gdsfmt_1.14.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.14 digest_0.6.13 crayon_1.3.4 rprojroot_1.3-1
## [5] backports_1.1.2 magrittr_1.5 evaluate_0.10.1 stringi_1.1.6
## [9] rmarkdown_1.8 tools_3.4.3 stringr_1.2.0 yaml_2.1.16
## [13] compiler_3.4.3 htmltools_0.3.6 knitr_1.17
The author would like to thank members of the GENEVA consortium (http://www.genome.gov/27541319) for access to the data used for testing the gdsfmt and SNPRelate packages.