hi welcome back to discovering patterns in the microbiome in this video we'll be talking about beta diversity this is one of the steps that we perform after we've processed all the sequences and generated an OTU table so beta diversity as we mentioned is is the diversity between environments and so for example we looked at this plot from the human microbiome project showing the overall clustering of the different body habitats by the by their micro biomes and there are a couple of other interesting figures in that paper that give you an example of how you can
look at beta diversity so this is this is a plot of the Beta Beta diversity between samples within each body site so for example there's more there's more variability between samples on the skin so you see higher inter simple distance on the skin within those skin body sites then you see inter sample distance or interpersonal difference in the oral body sites in here and another nice figure there examines the beta diversity between replicates and between different visits for people so technical replicates fortunately have the lowest the lowest overall change between them the lowest beta diversity
and then between visits is a little bit is a little bit lower than between subjects so the same subject over time tends to be less different from his himself or herself than from another person beta diversity is a measure of the overall change in the whole community that's what's it's intended to be so it takes into account the whole distribution of species and can can be run on species ODU's genus level phylum jeans and so on and so here's an example from a dietary study that I helped analyze a few years ago where where our
collaborators had given people a controlled diet over a period of time and this is a plot where it's it's showing which samples are most similar to one another but the samples that and the samples are colored by subject so if you look at each of the subjects clusters first of all each subject has a different microbiome which is normal but if you look at each of the clusters there's one point that tends to be an outlier in a lot of them and that's the initial time point before the dietary intervention so it's a nice way
of seeing that there was on average an overall shift in the community you can run beta diversity and chime most of the common beta diversity metrics are in there there are some fancier ones with but but with some really important applications that are that are in our that we're going to talk about too so so this is a list of all the various beta diversity metrics that are in there now we're gonna start with Euclidean distance this is the most common distance metric not in microbiome analysis but just in general that's just like the actual
distance in space between two points and I think of it as the most dangerous distance so the the problem is it's simple everyone knows it but it actually does a really poor job of measuring ecological distances between communities and and I think of it as being analogous to the square knot which is or the overhand knot which is the most dangerous knot and rock climbing because it's the knot that everybody knows it's really easy to tie but it comes undone very easily so we're going to examine Euclidean distance on a nice study of the Guerrero
No microbial mat this was this was in collaboration with norm paces group a few years back so it's basically this microbial microbial mat that is has different bugs living at each layer and they had sampled each of the layers and then we got to analyze the data so so in order to in order to run beta diversity and then visualize it we have to have some way of taking the huge inter sample distance matrix which is going to tell you all of the distances ecological distances between every pair of samples I mean every sample and
every other sample so it's all of the pairwise distances we have to take that huge matrix and somehow collapse it into two dimensions so that we can view it on the screen and there are ways a number of different ways to do this the most common way in microbiome analysis is principal coordinates analysis it's basically it's it's quite similar to principal components analysis if you happen to know that one but it's principal components analysis is is sort of like doing principal coordinates analysis with a Euclidean distance so the nice thing about principal coordinates analysis is
it lets us put any distance metric in Euclidean break Curtis unis frack all of these other ones that we're going to talk about so so the easiest way to generate beta diversity plots in chyme is actually through the core diversity analyses script and to run that you pass in an OT u table an output folder you have to tell it what's the at what level to verify the data so this is 950 sequences per sample you have to pass in a phylogeny that tells how the different ot use are related to one another and that's
so can calculate some phylogenetic diversity metrics and you have to give it a mapping file the - B just tells it to be verbose and print out lots of output so I'm going to show you the output from this in this in this particular analysis this is of the global gut data it's actually a subset of 66 adults that I pulled out and you'll see in the in this output folder there's an index.html so you want to open that in in your favorite web browser I have it open already in in Chrome and you get
this nice little summary output from chime that that gives you information about the biome table it gives you taxonomic information alpha diversity beta diversity and so on but we're just going to look at beta diversity right now so if you go to where it says Pico a plot and click index.html you get this nice 3d plot and here it's it's it's actually showing you the first three dimensions of principal coordinates analysis now I'm not going to go into details about how principal coordinates analysis works we'll be doing that later on in the course but it's
basically trying to show you as best as possible all of the distances between the samples so here we can rotate this around and see which samples are similar to one another right now it looks like they're colored by the sample ID which isn't very informative so if we go over to colors we can we can oh these are colored by age we can color them by age group but these are all adults so there's nothing interesting their body site they're all stool samples but if we look at country now we see this really strong separation
so the blue samples are from the USA the red and orange are from Malawi and Venezuela and pretty much any way you look at it these are these are very separate distributions so so this is this is the best that we can do to visualize the the whole beta diversity matrix is find some way to project it down into two or three dimensions and then show that you know on a computer screen or a piece of paper now the so this is this is a very nice way of examining whether any of the factors in
your experiment are related to to the microbiome so for example let's look at sequencing run these data were run on two different sequencing runs it would be really bad if if the data split according to sequencing run because we'd have a big batch effect but instead instead it looks like sequencing run didn't really matter that much so that's good news so anyway that's that's how we're looking at this for the rest of this video will be sticking with just just a 2d representation of the first two axes of variation now how do we generate Euclidean
distances well we're actually going to do this we're going to do this in our and I have this in the code browser for the course so if you go to the course repository and visit the code browser you can see this beta diversity number four here using the Guerrero negro data and this tells you everything you need to do to get this working so these are commands that you would run in are so you need to are installed and then you'd have to install the biome and the vegan package if you don't have them so
you only need to run that once load the packages then this is how we load the data load the metadata that's by the mapping file that tells information about the samples and then here's where we're going to calculate distance so we're going to calculate we're going to calculate three different distance metrics Euclidean Break Curtis and chi-square the first one we'll look at is Euclidean so once we have the o2 table read in we just run this disk command and we store it I'm I just made up a variable named D yook that I store that
distance matrix in this is the command we would use to run principal coordinates so that's taking the whole distance matrix and collapsing it down to the best two axes and storing it in this PC u k-- variable I'm making up a color gradient that goes from red to blue that we're gonna use to color the principal coordinates plot according to the depth of the samples in that microbial mat here's where I pull the the depth out or this is actually which layer the sample was taken from from that metadata table and then here's where we
plot so we plot the first coordinates the first axis of coordinates the second axis of coordinates four colors I'm going to use the the red to blue gradient but it's it's indexed by whatever whatever layer the sample came from and let's use some big points and little plus filled circles and this is the plot we get so what can you see well clearly there's some kind of gradient going on here going from from the shallow samples to the deep samples although although it's not perfect perfectly obvious and there might be some other ways that we
can look at it so so that's what we get out of Euclidean the next one we're going to look at is chi-square so case the Chi square distance actually works really well for gradients and and we're going to be studying it later on when we dig into how you do how you do ordination like alternatives to principal coordinates analysis so chi-square is it's like the chi-square statistic these are the formula for how you calculate it so I invite you to pause the video and and read through this and make sure you understand and then let's
look at how how those plots look so we're going to use the same approach so except that here we're actually using a command from the vegan package in R to calculate the chi-squared distances so this is called the CCA command and I'm going to pull the distance matrix out of there I'm also pulling the principal coordinates out of there and then we're gonna plot it so actually that's right here so same type of plot and this one looks it looks a little bit better in the sense that the first axis PC one is perfectly correlated
with the layers so this is really an amazing plot because it's showing that that the the top layer which is in I guess blue so the top layer in blue is most similar to the second layer and the second layer is most similar to the top layer and the third layer and the third layer is kind of in between the second and the fourth and it's monotonic it's this perfect ordering so it's a really nice biological result and we can see it very well graphically with with chi-square so this is what we get from Chi
square the last distance metric that I'm going to show you is break hurtis so break curtiss the the way it works is you walk through each of the species in a particular sample so remember beta diversity is comparing two different samples and we want to find out overall how different their microbiomes are so you walk through each of the species in each sample and for each species you find which sample has the lowest value and you take the sum of those lower values so that's shown here as CIJ so that's the low value u and
then you divide by the total count for all of the species in in each of the samples so break Curtis is the way I think of it is it's how much does one sample kind of undershoot the other sample and in terms of the the abundance of of a buck and so it's it's the sum of all of the lowest values for all of the species divided by the total sum of all the species and break curtis is is a nice ecological distance with a lot of history and macro ecology tends to work very well
in the microbiome - you can try lots of other ecological distances Morrissey - horn Manhattan distance there are many but the Hellinger but they often give you basically the same results so let's look at the code for break curtis that's actually up here same thing we we calculate break curtis distances here again we're using this vegan package to do break curtis then this is where we get the principal coordinates and then we're gonna make the plot right here and here's what we get now i want to point something out the the plot here is upside
down relative to the other plots but that actually doesn't matter the the sign of the axes the taxes and any of the axes and principal coordinates analysis is totally arbitrary so you could flip this upside down and it would be the same all the plot is showing you is the relative distances between the samples so so that's what we get from break curtis here i've flipped it over for you and here's a comparison of the three so what do you see well first of all this data set is so clean that they kind of all
work reasonably well which one does best though well if if you're trying if you're looking at just the first axis of variation in the distance matrix that's that's PC one only one of these metrics sorts the samples correctly according to the gradient and that's Chi square so Euclidean has a bunch compressed on the and the blue ends and break Curtis also has some compressed on the blue end as well so Chi square Chi square is actually really good at identifying gradients in data and it's the distance metric that underlies correspondence analysis which is an ordination
technique we'll talk about later but the the main takeaway is if you've got gradients you might want to try Chi square so in summary beta diversity measures the the diversity between communities in general most people use Break Curtis or unique frac or both in the microbiome we haven't talked about uni frac yet that's going to be in another video and a last chi-square is usually the best the the best to use for gradients and as I said unit frac will be discussed in the next video