These functions implement Principal Component Analysis (PCA) for
massive SNP datasets stored as genlight object. This
implementation has the advantage of never representing to complete data
matrix, therefore making huge economies in terms of rapid access
memory (RAM). When the parallel
package is available,
glPca
uses multiple-core ressources for more efficient
computations. glPca
returns lists with the class glPca
(see 'value').
Other functions are defined for objects of this class:
- print
: prints the content of a glPca
object.
- scatter
: produces scatterplots of principal components,
with a screeplot of eigenvalues as inset.
- loadingplot
: plots the loadings of the analysis for one given
axis, using an adapted version of the generic function loadingplot
.
glPca(x, center = TRUE, scale = FALSE, nf = NULL, loadings = TRUE,
alleleAsUnit = FALSE, useC = TRUE, parallel = FALSE,
n.cores = NULL, returnDotProd=FALSE, matDotProd=NULL)# S3 method for glPca
print(x, ...)
# S3 method for glPca
scatter(x, xax = 1, yax = 2, posi = "bottomleft", bg = "white",
ratio = 0.3, label = rownames(x$scores), clabel = 1, xlim = NULL,
ylim = NULL, grid = TRUE, addaxes = TRUE, origin = c(0, 0),
include.origin = TRUE, sub = "", csub = 1, possub = "bottomleft",
cgrid = 1, pixmap = NULL, contour = NULL, area = NULL, ...)
# S3 method for glPca
loadingplot(x, at=NULL, threshold=NULL, axis=1,
fac=NULL, byfac=FALSE, lab=rownames(x$loadings), cex.lab=0.7, cex.fac=1,
lab.jitter=0, main="Loading plot", xlab="SNP positions",
ylab="Contributions", srt = 90, adj = c(0, 0.5), ...)
=== glPca objects ===
The class glPca
is a list with the following
components:
the matched call.
a numeric vector of eigenvalues.
a matrix of principal components, containing the coordinates of each individual (in row) on each principal axis (in column).
(optional) a matrix of loadings, containing the loadings of each SNP (in row) for each principal axis (in column).
-
=== other outputs ===
Other functions have different outputs:
- scatter
return the matched call.
- loadingplot
returns information about the most contributing
SNPs (see loadingplot.default
)
for glPca
, a genlight object; for
print
, scatter
, and loadingplot
, a glPca
object.
a logical indicating whether the numbers of alleles should be centered; defaults to TRUE
a logical indicating whether the numbers of alleles should be scaled; defaults to FALSE
an integer indicating the number of principal components to be retained; if NULL, a screeplot of eigenvalues will be displayed and the user will be asked for a number of retained axes.
a logical indicating whether loadings of the alleles should be computed (TRUE, default), or not (FALSE). Vectors of loadings are not always useful, and can take a large amount of RAM when millions of SNPs are considered.
a logical indicating whether alleles are considered as units (i.e., a diploid genotype equals two samples, a triploid, three, etc.) or whether individuals are considered as units of information.
a logical indicating whether compiled C code should be used for faster computations; this option cannot be used alongside parallel option.
a logical indicating whether multiple cores -if
available- should be used for the computations (TRUE), or
not (FALSE, default); requires the package parallel
to be installed
(see details); this option cannot be used alongside useCoption.
if parallel
is TRUE, the number of cores to be
used in the computations; if NULL, then the maximum number of cores
available on the computer is used.
a logical indicating whether the matrix of dot products between individuals should be returned (TRUE) or not (FALSE, default).
an optional matrix of dot products between individuals, NULL by default. This option is used internally to speed up computation time when re-running the same PCA several times. Leave this argument as NULL unless you really know what you are doing.
further arguments to be passed to other functions.
integers
specifying which principal components
should be shown in x and y axes.
arguments used to customize the inset in scatterplots
of glPca
results. See add.scatter
documentation in the
ade4 package for more details.
arguments passed to s.class
; see ?s.label
for more information
an optional numeric vector giving the abscissa at which loadings are plotted. Useful when variates are SNPs with a known position in an alignement.
a threshold value above which values of x are identified. By default, this is the third quartile of x.
an integer indicating the column of x to be plotted; used only if x is a matrix-like object.
a factor defining groups of SNPs.
a logical stating whether loadings should be averaged by
groups of SNPs, as defined by fac
.
a character vector giving the labels used to annotate values above the threshold.
a numeric value indicating the size of annotations.
a numeric value indicating the size of annotations for groups of observations.
a numeric value indicating the factor of randomisation for the position of annotations. Set to 0 (by default) implies no randomisation.
the main title of the figure.
the title of the x axis.
the title of the y axis.
rotation of the labels; see ?text.
adjustment of the labels; see ?text.
Thibaut Jombart t.jombart@imperial.ac.uk
=== Using multiple cores ===
Most recent machines have one or several processors with multiple
cores. R processes usually use one single core. The package
parallel
allows for parallelizing some computations on
multiple cores, which can decrease drastically computational time.
Lastly, note that using compiled C code (useC=TRUE
)is an
alternative for speeding up computations, but cannot be used together
with the parallel option.
- genlight
: class of object for storing massive binary
SNP data.
- glSim
: a simple simulator for genlight objects.
- glPlot
: plotting genlight objects.
- dapc
: Discriminant Analysis of Principal Components.
if (FALSE) {
## simulate a toy dataset
x <- glSim(50,4e3, 50, ploidy=2)
x
plot(x)
## perform PCA
pca1 <- glPca(x, nf=2)
## plot eigenvalues
barplot(pca1$eig, main="eigenvalues", col=heat.colors(length(pca1$eig)))
## basic plot
scatter(pca1, ratio=.2)
## plot showing groups
s.class(pca1$scores, pop(x), col=colors()[c(131,134)])
add.scatter.eig(pca1$eig,2,1,2)
}
Run the code above in your browser using DataLab