Distances and Ordination
This module measures how similar whole samples are to each other. You pick a distance, draw the ordination, then put a number on whatever pattern you think you see.
Slides
Exercise 1: Distances and ordination on the species table
As this morning, Pi writes the code and you decide what it means. An ordination is easy to produce and easy to over-read: a clean split on a plot invites a story about axis 1. This exercise asks you to get the numbers behind each story before you believe it.
Open notebooks/03-ordination.qmd, run the setup chunk, and start Pi with a fresh conversation. The setup chunk builds ps_species and ps_genes and attaches your community column to both. Tell Pi to use those objects rather than building new ones.
Part 1: Predict
- Bray-Curtis uses abundances. Jaccard uses only presence and absence. Our species table is about 89% zeros. Which of the two do you expect to separate the two community types more clearly, and why?
- If you shuffled the
outcomelabel randomly across all 471 samples, whatR2would PERMANOVA report? Give a number. - One sample has 438 reads. Where do you expect it to sit in a Bray-Curtis ordination?
Part 2: Ask, look, decide
The first ordination:
Using
ps_speciesfrom the setup chunk, make a Bray-Curtis PCoA with microViz at the species level and colour it bycommunity. Put the share of variation on each axis title.Check it before you read anything into it. Ask Pi how many points are on the plot (there should be 471) and how many samples have no
communityvalue (there should be none). If the colours are all grey, your column did not attach.Three distances, side by side:
Make the same ordination three ways, side by side:
"identity"with"bray","binary"with"jaccard", and"clr"with"euclidean". Under them, add a scree plot for each showing the share of variation held by the first eight axes.Compare the three with your answer to question 1. Then decide which one you would put in a paper and write one sentence in your notebook saying why.
One plot, many colourings. Ask Pi for the Bray-Curtis ordination as a grid of panels, coloured by
outcome,time_point,nugent,lacto_fractionand library size. Which colouring lines up with axis 1? Which one shows nothing you can see by eye?What is axis 1? From here on, write your own requests. Ask Pi to interpret your ordination. It will probably write a confident paragraph about the biology of axis 1. Now ask for the numbers behind it: the correlation of axis 1 with
lacto_fraction, and the five genera whose abundance tracks axis 1 most closely, each with its correlation. Does the paragraph still hold up?PERMANOVA. Ask for the
R2and p-value ofcommunity,outcome,time_pointandrnaseq_batch, as a table and as a bar chart. Check it against the slides: about 17.5% for community, 8.7% for visit, 0.5% for outcome, and an RNA batch that explains nothing here.Then make the null distribution yourself:
Shuffle the
outcomelabels across samples 20 times, run PERMANOVA on each shuffled version with 99 permutations, and plot the 20R2values as a histogram with the real outcomeR2marked as a line.How does this compare with your answer to question 2? Where does the real value sit?
Argue about the settings. The slides used
by = "margin". Ask Pi to run it again withby = "terms"and to put both results side by side, then to explain why they differ when labels are related to each other. Then ask it: “Each participant appears three times. Does PERMANOVA know that, and does it matter?” Push on the answer until you could explain it to your neighbour.The 438-read sample. Ask Pi to mark it and label it on the ordination. Was your prediction right? Then ask for the ordination again without the samples you dropped this morning, side by side with the original. Did anything else move?
Polish one figure. Take the community-coloured Bray-Curtis plot and ask for variations: ellipses around each group, one panel per visit, point size by library size, lines joining each participant’s three visits for ten participants. Keep the version that shows the most, then make it ready for a talk: your own colours, axis titles in words, a legend people can read, saved as a PNG.
Discussion
- Which of the three distances would you publish? Defend it in one sentence.
communityexplains a very large share of this table. Is that a finding?- Pi’s first paragraph about axis 1: what did it get right, and what did it say that the numbers did not support?
- Your ordination shows visit structure. Each participant appears three times. Does PERMANOVA know that, and does it matter?
Exercise 2: The identical pipeline on 14,000 genes
The same pipeline runs on a table sixty times larger with three words changed. That makes it easy to try many versions. The hard part is the batch effect: removing it is a choice with consequences, and Pi will make that choice for you if you let it.
Same notebook. Type /new in Pi first.
Part 1: Predict
- Bray-Curtis is standard for microbiome data. Would you use it on gene counts? Say why or why not before you try.
communityexplained about 18% of the species table. What share do you expect it to explain of the gene table?- The gene table was produced in two sequencing batches. What share do you expect
rnaseq_batchto explain?
Part 2: Ask, look, decide
The gene ordination:
Using
ps_genesfrom the setup chunk, make a CLR plus Euclidean PCoA at thesymbollevel, as three panels coloured bycommunity,outcomeandrnaseq_batch.Ask Pi how many genes
ps_genesholds after its filter, and what pseudocount the CLR transform used for zeros. Ask where that number came from. Write both down.Species and genes side by side. Ask for PERMANOVA on the gene ordination with the same four labels as Exercise 1, then one table and one bar chart with the species and gene
R2values next to each other. Check it against the slides: in the gene table, community, outcome and batch each explain about 2%.Ask for a report, then audit it. Ask Pi to write a paragraph comparing the two tables. Read it one sentence at a time and mark each sentence as backed by a number you can point to, or not. If it says the gene table has “weaker biological signal”, is that a claim the ordination can support? Tomorrow you test the genes one at a time. Write down what you expect to find.
Is batch tangled up with the biology? Ask for
rnaseq_batchcross-tabulated againstoutcome,communityandtime_point, as tables and as one plot. Ask also how participants are spread across batches: are a participant’s three visits always in the same batch?If the groups were unbalanced across batches, what would that mean for tomorrow’s differential tests, which compare the groups one gene at a time?
Two ways to handle batch. First, ask for PERMANOVA with
rnaseq_batchentered beforeoutcome, usingby = "terms", next tooutcomeon its own. Does the outcomeR2change?Then ask Pi to remove the batch effect and redraw the ordination, before and after, coloured by batch and by outcome. Tell it to use only packages that are already installed. Then ask exactly what it did to the numbers. “Batch correction” covers several different operations. Which one did it pick, and could it have removed some of the outcome signal along with the batch?
Other choices, side by side. Ask for the gene ordination three more ways and put them next to your CLR version: Bray-Curtis on the gene table, and CLR with a looser gene filter and with a stricter one. Which differences would change what you conclude, and which are cosmetic?
One figure for a talk. Ask Pi to put the species and gene ordinations side by side, coloured by
communitywith the same two colours in both panels, one shared legend, and the communityR2in each panel’s subtitle. Iterate on it until you would show it in a lab meeting.
Part 3: Reach (optional)
Ask Pi to plot gene axis 1 against species axis 1 for the same samples, and to give you the correlation. Then ask for axis 2 against axis 2. Are the two tables related? This scatter plot is the simplest form of multiomics integration, and Friday morning starts from it.
If you have time after that, ask for a short Quarto report of this afternoon’s work ending in ## Choices made: the distance for each table, the gene filter, the pseudocount, what you did about batch, and one alternative for each.
Discussion
- Both tables showed a significant outcome effect of about 2% or less. Would you write that in a paper, and how would you phrase it?
- The batch effect is as large as the biological effect. What should the people who generated this data have done differently?
- What did Pi do when you asked it to remove the batch effect, and would you keep that step in your analysis?
- You now have two ordinations of the same 471 samples. What would it take to combine them, and what would you hope to gain?