Computes Principal Component Analysis (PCA) for variable x sample genotype data, such as Single Nucleotide Polymorphisms (SNP), in combination with Permutational Multivariate Analysis of Variance (PERMANOVA) and Permutational Multivariate Analysis of Dispersion (PERMDISP).
A wrapper of functions smart_pca
, smart_permanova
and smart_permdisp
.
Genetic markers such as SNPs can be scaled by centering
, z-scores and genetic drift-based dispersion.
The latter follows the SMARTPCA implementation of Patterson, Price and Reich (2006).
Optimized to run fast computation for big datasets.
Returns a list containing the following elements:
pca.snp_loadings
: Dataframe of principal coefficients of SNPs. One set of coefficients per PCA axis computed.
pca.eigenvalues
: Dataframe of eigenvalues, variance and cumulative variance explained. One eigenvalue per PCA axis computed.
pca_sample_coordinates
: Dataframe showing PCA sample summary. Column Group assigns samples to groups. Column Class specifies if samples were "Removed" from PCA or "Projected" onto PCA space. Additional columns show principal components (coordinates) of samples in PCA space (e.g., PC1, PC2, ...).
test_samples
: Dataframe showing test sample summary. Column Group assigns samples to tested groups. Column Class specifies if samples were used in or removed from testing (PERMANOVA and/or PERMDISP). Column Sample_dispersion
shows sample dispersion relative to spatial "median"
or "centroid"
used in PERMDISP.
permanova.global_test
: List with PERMANOVA results including degrees of freedom, sum of squares, mean sum of squares, F statistic, variance explained (R2), and p-value.
permanova.pairwise_test
: List with PERMANOVA results including F statistic, variance explained (R2), p-value and corrected p-value per group pair.
permdisp.global_test
: List with PERMDISP results including degrees of freedom, sum of squares, mean sum of squares, F statistic, and p-value.
permdisp.pairwise_test
: List with PERMDISP results including F statistic, p-value, and corrected p-value per group pair. Only returned if pairwise = TRUE
.
permdisp.bias
: Character string indicating whether PERMDISP dispersion was corrected for unequal group sizes.
permdisp.group_location
: Dataframe showing coordinates of group "medians"
or "centroids"
in PERMDISP.
test.pairwise_correction
: Character string describing the multiple testing correction used in PERMANOVA and/or PERMDISP.
test.permutation_number
: Number of permutations used to calculate F-statistics.
test.permutation_seed
: Seed used for reproducible permutation results in PERMANOVA and/or PERMDISP.
File name read from working directory.
SNP = rows, samples = columns without row names or column headings.
SNP values must be count data (no decimals allowed).
File extension detected automatically whether text or EIGENSTRAT
.
See details.
Logical value for EIGENSTRAT
, irrelevant for text data.
Default packed_data = FALSE
assumes uncompressed EIGENSTRAT
.
packed_data = TRUE
for compressed or binary EIGENSTRAT
(PACKENDANCESTRYMAP
).
Character or numeric vector assigning samples to groups. Coerced to factor.
Logical FALSE
or numeric vector indicating column numbers (samples) to be removed from computations.
Default sample_remove = FALSE
keeps all samples.
Logical FALSE
or numeric vector indicating row numbers (SNPs) to be removed from computations.
Default snp_remove = FALSE
keeps all SNPs.
See details.
Logical indicating if PCA is computed.
Default TRUE
.
Logical indicating if PERMANOVA is computed.
Default TRUE
Logical indicating if PERMDISP is computed.
Default TRUE
.
Number 9
or string NA
indicating missing value.
Default missing_value = 9
as in EIGENSTRAT
.
If no missing values present, no effect on computation.
String handling missing values.
Default missing_impute = "mean"
replaces missing values of each SNP by mean of non-missing values across samples.
missing_impute = "remove"
removes SNPs with at least one missing value.
If no missing values present, no effect on computation.
String. Default scaling = "drift"
scales SNPs to control for expected allele frequency dispersion caused by genetic drift (SMARTPCA).
scaling = "center"
for centering
(covariance-based PCA).
scaling = "sd"
for centered
SNPs divided by standard deviation (correlation-based PCA).
scaling = "none"
for no scaling.
See details.
String indicating R package computing single value decomposition (SVD).
Default program_svd = "Rspectra"
for svds
.
program_svd = "bootSVD"
for fastSVD
.
See details.
Numeric vector indicating column numbers (ancient samples) projected onto (modern) PCA space.
Default sample_project = FALSE
implements no projection.
See details.
Numeric vector indicating the ranks of the PCA axes ancient samples are projected onto. Default pc_ancient = c(1, 2)
for PCA axes 1 and 2.
If program_svd = "RSpectra"
, length(pc_ancient)
must be smaller than or equal to pc_axes
.
No effect on computation, if no ancient samples present.
Type of inter-sample proximity computed (distance, similarity, dissimilarity).
Default is Euclidean distance
.
See details.
A string value indicating R package to estimate proximities between pairs of samples.
Default program_distance = "Rfast"
uses function Dist
; program_distance = "vegan"
uses vegdist
.
See details.
String.
Default target_space = "multidimensional"
applies PERMANOVA and/or PERMDISP to sample-by-sample triangular matrix computed from variable-by-sample data, pc_axes
has no effect on computation. target_space = "pca"
applies PERMANOVA and/or PERMDISP to sample-by-sample data in PCA space, pc_axes
determines number of PCA axes for testing.
Number of PCA axes computed always starting with PCA axis 1.
Default pc_axes = 2
computes PCA axes 1 and 2 if target_space = "pca"
.
No effect on computation if target_space = "multidimensional"
.
Logical.
Default pairwise = FALSE
computes global test. pairwise = TRUE
computes global and pairwise tests.
String specifying type of correction for multiple testing.
Default "holm"
.
Number of permutations resulting in PERMANOVA/PERMDISP test p value.
Default 9999
.
Number fixing random generator of permutations.
Default 1
.
String indicating quantification of group dispersion whether relative to spatial "median"
or "centroid"
in PERMDISP.
Default "median"
.
Logical. samplesize_bias = TRUE
for dispersion weighted by number of samples per group in PERMDISP.
Default pairwise = FALSE
for no weighting.
See details in other functions for conceptualization of PCA (smart_pca
) (Hotelling 1993), SMARTPCA (Patterson, Price and Reich 2006), PERMANOVA (smart_permanova
) (Anderson 2001) and PERMDISP (smart_permdisp
(Anderson 2006), types of scaling, ancient projection, and correction for multiple testing.
Users can compute any combination of the three analyses by assigning TRUE
or FALSE
to pca
and/or permanova
and/or permdisp
.
PERMANOVA and PERMDISP exclude samples (columns) specified in either sample_remove
or sample_project
.
Projected samples are not used for testing as their PCA coordinates are derived from, and therefore depend on, the coordinates of non-projected samples.
Data read from working directory with SNPs as rows and samples as columns. Two alternative formats: (1) text file of SNPs by samples (file extension and column separators recognized automatically) read using fread
; or (2) duet of EIGENSTRAT
files (see https://reich.hms.harvard.edu/software) using vroom_fwf
, including a genotype file of SNPs by samples (*.geno
), and a sample file (*.ind
) containing three vectors assigning individual samples to unique user-predefined groups (populations), sexes (or other user-defined descriptor) and alphanumeric identifiers.
For EIGENSTRAT
, vector sample_group
assigns samples to groups retrievable from column 3 of file *.ind
.
SNPs with zero variance removed prior to SVD to optimize computation time and avoid undefined values if scaling = "sd"
or "drift"
.
Users can select subsets of samples or SNPs by introducing a vector including column numbers for samples (sample_remove
) and/or row numbers for SNPs (snp_remove
) to be removed from computations.
Function stops if the final number of SNPs is 1 or 2.
EIGENSOFT
was conceived for the analysis of human genes and its SMARTPCA suite so accepts 22 (autosomal) chromosomes by default.
If >22 chromosomes are provided and the internal parameter numchrom
is not set to the target number chromosomes of interest, SMARTPCA automatically subsets chromosomes 1 to 22.
In contrast, smart_mva
accepts any number of autosomes with or without the sex chromosomes from an EIGENSTRAT
file.
smart_pca
,
smart_permanova
,
smart_permdisp
# Path to example genotype matrix "dataSNP"
pathToGenoFile = system.file("extdata", "dataSNP", package = "smartsnp")
# Assign 50 samples to each of two groups and colors
my_groups <- as.factor(c(rep("A", 50), rep("B", 50))); cols = c("red", "blue")
# Run PCA, PERMANOVA and PERMDISP
mvaR <- smart_mva(snp_data = pathToGenoFile, sample_group = my_groups)
mvaR$pca$pca.eigenvalues # extract PCA eigenvalues
head(mvaR$pca$pca.snp_loadings) # extract principal coefficients (SNP loadings)
head(mvaR$pca$pca.sample_coordinates) # extract PCA PCs (sample position in PCA space)
# plot PCA
plot(mvaR$pca$pca.sample_coordinates[,c("PC1","PC2")], cex = 2,
pch = 19, col = cols[my_groups], main = "genotype smartpca")
legend("topleft", legend = levels(my_groups), cex = 1,
pch = 19, col = cols, text.col = cols)
# Extract PERMANOVA table
mvaR$test$permanova.global_test
# Extract PERMDISP table
mvaR$test$permdisp.global_test # extract PERMDISP table
# Extract sample summary and dispersion of individual samples used in PERMDISP
mvaR$test$test_samples
Run the code above in your browser using DataLab