













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














will open the manpage of the spatial principal component analysis [8]. At
the end of a manpage, an ‘example’ section often shows how to use a func-
tion. This can be copied and pasted to the console, or directly executed
from the console using example. For further questions concerning R, the
function RSiteSearch is a powerful tool to make an online research using
keywords in R’s archives (mailing lists and manpages).
adegenet has a few extra documentation sources. Information can be
found from the website (http://adegenet.r-forge.r-project.org/), in
the ‘documents’ section, including two tutorials, a manual which includes
all manpages of the package, and a dedicated mailing list with searchable
archives. To open the website from , use:
The same can be done for tutorials, using adegenetTutorial (see manpage
to choose the tutorial to open).
You will also find a listing of the main functions of the package typing:
?(adegenet)Note that you can also browse help pages as html pages, using:
To go to the adegenet page, click ‘packages’, ‘adegenet’, and ‘adegenet-
package’.
Lastly, several mailing lists are available to find different kinds of infor-
mation on R; to name a few:
R-help (https://stat.ethz.ch/mailman/listinfo/r-help): general ques-
tions about R
R-sig-genetics (https://stat.ethz.ch/mailman/listinfo/r-sig-genetics):
genetics in R
adegenet forum (https://lists.r-forge.r-project.org/cgi-bin/mailman/
listinfo/adegenet-forum): adegenet and multivariate analysis of
genetic markers
2.1 Data formats
Two principal types of genetic data can be handled in R. The first one is
(preferably aligned) DNA sequences, and the second one is genetic markers.
DNA sequences can be used to calibrate models of evolution and compute
genetic distances, which can in turn be used for phylogenetic reconstruction
or in multivariate analyses. In , DNA sequences are best handled as
DNAbin objects, in the ape package. See ?DNAbin for more details about this
class. After loading ape, we load the dataset woodmouse:
Use str, unclass and attributes to explore the content of the DNAbin
object. Convert the dataset to a matrix of characters using as.character.
What is the size of this new object (use object.size)? Compare it to the
original DNAbin object. Why is this class optimal for handling DNA se-
quences?
Genetic markers are features of DNA sequences that vary between indi-
viduals. There is not a single type of genetic marker: some are codominant
(all alleles are visible), some are dominant (some alleles hide the presence of
others); some have many alleles (like microsatellites), and others are most of-
ten binary (like SNPs). In general, codominant (more informative) markers
are the rule, and dominant markers become more and more rare.
Both types of markers can be handled in using the class genind
implemented in adegenet. This class uses the S4 system of class definition
and methods, with which users are generally not familiar. Load the
dataset nancycats:
Look at the @tab component of the genind object. To extract this table
with original labels, use truenames. How is information stored? Does the
Euclidean distance between the rows of this matrix make some biological
sense? This is a canonical way of storing information for any dominant
markers, irrespective of the degree of ploidy.
It sometimes happens that analyses are performed at a population level,
rather than at an individual level. In such cases, genpop objects are used.
These objects contain allele numbers per populations rather than relative
frequencies, but are otherwise similar to genind objects. genpop can be
obtained from genind objects using genind2genpop. Use this function to
compute allele counts for the cat colonies of Nancy (data nancycats), and
compare the the structure of the obtained object to the genind object.
2.2 Importing / exporting data
DNA sequences with usual formats are read using read.dna from the ape
package.
Genetic markers come in a wider variety of formats. import2genind
can be used to import some of the most frequent, and less twisted formats.
However, it is also possible to convert data from a data.frame to a genind
using df2genind. Using the example above, create a data.frame with two
tetraploid (i.e. 4 alleles) individuals and two loci. Note that alleles can be
given any name, and do not need to be letters. Then, used df2genind to
convert it to a genind object. Use truenames and genind2df to check that
the data conversion has worked.
It also happens that genetic markers are derived from DNA sequences.
In such a case, only sites that vary between individuals are retained; these
are called Single Nucleotide Polymorphism (SNPs). They can be extracted
from DNAbin objects using DNAbin2genind. Use this function to extract the
polymorphic sites from the woodmouse dataset. The locus names correspond
to the position of the polymorphic site. Use this information to plot the
distribution of polymorphism along the DNA sequence; you should arrive at
something along the lines of (using density with a width of 30):
density.default(x = x, width = 30)
What can we say about the distribution of SNPs on the DNA sequence?
2.3 Manipulating data
It is often useful to be able to manipulate genetic marker data in different
ways. There is no particular manipulation for DNAbin objects, which are
essentially matrices or lists. genind objects benefit from different facilities
for manipulating data. First, the [ operator can be used to subset individuals
and alleles; this is done like for a matrix, and is based on the @tab slot. For
instance, to retain only the first 10 individuals of the nancycats dataset,
use:
By population
By marker
What are your conclusions? What can we say about the hypothesis of
random mating within these cat colonies?
To investigate the data further, we can measure and test the significance
of the genetic differentiation of the cat colonies. Use fstat to compute
the F st of these populations. This is a measure of population differentia-
tion, which can be roughly interpreted as the proportion of genetic variance
explained by differences between groups. Values less than 0.05 are often
considered as negligible.
Then, test the significance of the overall group differentiation using
gstat.randtest, and plot the results; are there differences between groups?
Genetic data are often represented using trees. In this section, we won’t
tackle phylogenetic reconstruction based on maximum likelihood or parsi-
mony, which should be covered in a practical of its own. For this, see the
well-documented package phangorn. In the following, we illustrate briefly
how to contruct tree representations of genetic distances.
4.1 Hierarchical clustering
Several algorithm of hierarchical clustering are implemented in the function
hclust. This function requires a distance matrix produced by the function
dist, or similar function (e.g. dist.dna, dist.genpop).
We will try to describe the genetic variability in the cat colonies of Nancy
using clustering methods. First, we need to compute a distance matrix. Re-
place missing data in nancycats using na.replace, and then compute the
Euclidean distances between individuals using dist on the table of relative
allele frequencies ($tab of the object in which NAs have been replaced). Use
hclust to compute a clustering with complete linkage, and one with single
linkage. How do they compare? How would you interprete these graphs?
Can you assess the number of actual genetic clusters in these data?
As a comparison, repeat these analyses with the dataset microbov, which
contains genotypes of various cattle breeds in France and Africa.
4.2 ape and phylogenies
ape is the main package for phylogenetic analyses. It implements various
methods for reconstructing trees based on genetic distances. It also provides
nice graphical functions for plotting trees.
Use the genetic distances obtained for the nancycat data to compute a
Neighbour-Joining tree (nj).
Then, plot the tree, specifying that you want to plot an unrooted tree, and
use tiplabels to represent colonies by coloured dots. To define one color
for each colony, use:
Since some colours are very light, you are best using a grey background for
the plot. The final result should ressemble:
ll
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
ll
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
ll
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l l
l
l l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
lll
l
l
l
l
l
l
l l l
l
l ll l
l
l
l
l l
l (^) l l
l
l
l
l
l
l
l
l l l
l
l
l
l
l l l
l l
l
l
l
l
l
l
l
l
l l l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l l
l
l l
l l l
l l
l l
l
l
l
l
l
l l
l
l
l
l
l
l
l
ll
l
l
l
l
l
l
l
l
ll
l
ll
ll
l
l
For a comparison, repeat these analyses with the cattle breed data (microbov).
To define one color for each breed of the dataset, use:
You should obtain a tree like this one:
l
l
l
l
l
l
l
l
l
l
l
ll
l
l ll
l
l
l l
l l
ll l l
l
l
l
l
l
l
l
l l
l l
ll l l (^) l l
l
l
l
l
l
l
l ll
l
l
ll
l
l
l
l l (^) l
l
l
l
l
l (^) l
l
l
l
l ll
l l
l (^) l l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
ll
l
l
l
l
l l l
l ll
l
l
l l
l
l l (^) lll l
l l
l
l
l l (^) ll l
l l l
l l ll ll
l l
ll
l l
l
l
l
l l l
ll
l
ll
l
l
l l
l
l
ll
l l
l ll
l
l
l l
l ll l l
l ll l l l l
ll
l
l
l l
l l
l
l l l l (^) l
l
l
l
l
l
l l
l l
l
l
l
l l
l
l
l
l
l
l
l
ll l
l l
l
l
l
l
l
l
l
l l
l l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l l
l
ll
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l l
l
ll l l l
l
l
l
l l
l ll
l
l ll ll
l
l
l
l
l l
l
l l
ll
l
l
l (^) l l
l
ll
l
l l
l
l l l
l
l l
l
l
l
l
l l
l
l l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l l l
l
l
l l
l
l l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
ll
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l l
l
l l l
l (^) l l
l
l
l
l l
l
l
l l
l l
l
l l
l l l
l
l
l l l (^) ll l l l
l
l
l
l
l
l
l
l l
l l
l l l
l l
l
l
l
l
l l
l
l
l
l
l
l
l l l
l
ll
l
l
l
l l l
l
l l
l
l l l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l ll l
l
l
l l l
l
ll
l
l
l l
l
l
l
l
l
l
l
l
l l l
l
l
l l^ l l
l l
l l
l
l
l ll l
l
l
l l
l
l
l l
l
l
l (^) l ll
ll
l
l
l
l l (^) l l
l
l
l
l
l
l
l l l l l
l
l
l
l l
l
l l
l ll
l
l
l
l
l l l l
l (^) l
l l
l
l l l
l l
l
l
l l
l
l
l
l l l
l
l
l
l
l l
l
l
l l
ll l
l
l
l
l
l
l
l
l
l
l
l
l
l l l
l
l
l
l
l
l
l
l
l
l
l
l
l l l l
l
l
l
l l
l
ll
l
There are two species, Bos taurus and Bos indicus, in these cattle breeds;
the species information is stored in microbov$other$spe. Use the same ap-
proach to represent the species information on the tips of the tree. Is this
classification reflected by a clear-cut structure on the tree?
The quality of a tree is unfortunately rarely questioned in terms of how
well the real distances are represented by the tree. The literature is even
full of examples of analyses based on distances computed from trees, while
the original distances that had generated the tree could have been used. A
emphasizes that most multivariate analyses can be unified under a single
general algorithm. Alternatively, some statisticians refer to this algorithm
as “generalized PCA”.
In ade4, multivariate analyses all use the same underlying algorithm
(as.dudi) and produce an object with the class dudi (for “duality diagram”).
Methods such as Principal Component Analysis (PCA) or Principal Coor-
dinates Analysis (PCoA) are implemented by functions where the methods’
abbreviation is preceeded by dudi; for instance, PCA is implemented by
dudi.pca, while PCoA is implemented by dudi.pco.
Apart from a few specific methods like spatial PCA (spca, [8]) or Dis-
criminant Analysis of Principal Components (dapc, [7]), multivariate meth-
ods are not implemented in adegenet. Procedures from ade4 are used after
transforming genind or genpop objects in an appropriate way, which often
consists in replacing missing data, and computing and standardizing allele
frequencies before analysis.
5.2 Principal Component Analysis (PCA)
PCA can be used on standardized allele frequencies, at the individual or
populational level. The function scaleGen is used to standardize the data
and replace possible missing data. Use it on the microbov dataset to obtain
a table of centred, but not scaled allele frequencies.
Then, perform the PCA of these data using dudi.pca. Be careful to set
the argument scale to FALSE to avoid erasing the scaling choice made in
scaleGen.
Eigenvalues
The function dudi.pca displays a barplot of eigenvalues and asks for a
number of retained principal components. In general, eigenvalues represent
the amount of genetic diversity — as measured by the multivariate method
being used — represented by each principal component (PC). Here, each
eigenvalue is the variance of the corresponding PC.
Once you have selected the number of retained axes, your PCA should
ressemble:
l
l
l
l
l
l
l
l
l
l
l
l l
l l
l
l
l
l
l
l
l ll
l
l
l (^) l l
l l
l
l l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l l
l
l
l
l
l
l
l
l
l
l l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l l
l
l
l
l
l l
l
l
l
l l l
l
l
l l
l
l l
l
l
l l
l (^) l l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l (^) l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l l
l l^
l
l
l
l l
l
l
l
l
l
l
l l
l
l
l l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l l
l
l
l l
l
l
l
l
l
l
l
l
l
l ll
l
l
l l
l
l
l
l l
l
l
l
l
l
l
l
l l l
l
l l
l l l l
l
l
l l
l
l
l
l
l
l
l
l
l l l
l
l
l
l
l
l l l l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l l
l
l
l l
l
l
l
l l
l
l
l
l l
l
l
l l
l l l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l l
l l
l
l
l
l
l l
l
l
l
l l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l l
l
l
l l
l l
l l
l
l
ll
l
l
l
l
l
l
l
l
l l lll l
l
l
l
l
l
l (^) l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l (^) l
l
l
l
l
l
l
l
l
l
l
l
l l l l
l
l
l l l l
l
l (^) l
l
l
l
ll
l
l
l
l l
l
l l
l
l
l
l
l l
l
l
l
l ll
l
l
l l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l l l
l
l (^) l l
l
l
l
l l
l
l
l
l
l
l l l
l
l
l l
l
l l
l
l l
l
l l
l
l
l
l
l l l
l
ll
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l l
l l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
l
l
l
l
l l
l
l
l
l
l
Represent the results for the axes 1-2, and then 2-3. How can you in-
terprete these principal components? What is the major factor of genetic
differentiation in these cattle breeds? What is the second one? What is the
third one?
In PCA, the eigenvalues indicate the variance of the corresponding prin-
cipal component. Verify that this is indeed the case, for the first and sec-
ond principal components. Note that this is also, up to a constant, the
mean squared Euclidean distance between individuals. This is because (for
x ∈ R
n ):
var(x) =
∑ n
i=
∑ n
j=
(x i − x j )
2
2 n(n − 1)
This again can be checked; assuming this analyis is called pcaX:
We sometimes want to express eigenvalues as a percentage of the total
variation in the data. What is the percentage of variance explained by the
first three eigenvalues?
Allele contributions can sometimes be informative. The basic graphics
for representing allele loadings is s.arrow. Use it to represent the results
of the PCA; is this informative? An alternative is offered by loadingplot,
which represents one axis at a time. Use it to represent the squared loadings
of the analysis; how does it compare to previous representation? The func-
tion returns invisibly some information; adapting the arguments and saving
the returned value, identify the 2% alleles contributing most to showing the
diversity within African breeds. You should find:
5.3 Principal Coordinates Analysis (PCoA)
We have just seen that the variance is, up to a constant, equal to the mean
squared distance between all pairs of observations. Therefore, maximizing
the variance along an axis should be exactly the same as maximizing the
sum of all squared Euclidean distances. This latter maximization is the
objective of Principal Coordinates Analysis (PCoA), also known as Met-
ric Multidimensional Scaling (MDS). This method is implemented in ade
by dudi.pco. After scaling the relative allele frequencies of the microbov
dataset, perform this analysis.
Represent the principal components ($li) of this analysis and compare
them to the PCs of the previous PCA. What is the correlation between
them? Compare the scatter plots of both analyses; are there any differ-
ences? In practice, in which cases should we prefer PCA over PCoA, or the
contrary?
As an exercise, compute genetic similarities based on the proportion of
shared alleles (propShared) on the microbov data. Transform the similar-
ities into a distance (no dedicated function for this), and perform a PCoA
of the resulting distance. Is this distance Euclidean? If not, it needs to
be. Transform it into an Euclidean distance (using cailliez) and redo the
PCoA. How do the results compare to the previous analyses?