Count Table Anatomy
Both of our measurements arrive as a table of counts, and the checks you learn on one work on the other.
Slides
Exercise 1: Many looks at the same two tables
In this module Pi writes the code. Your job is to ask for numbers and plots, look at each one, and decide what they mean. Another plot costs you a minute and a few cents, so ask for more of them than you think you need. Every step ends with something for you to check or decide, and that part is not Pi’s job.
Open notebooks/02-count-table-anatomy.qmd and start Pi in the terminal. Ask Pi to put its code under the step you are working on. Steps 1 to 3 give you a request to start from. After that, write your own.
Part 1: Predict, before you run anything
Write your answers down. You will compare them to the real numbers in Part 2.
- The species table has 220 rows. In a single cervicovaginal sample, how many of those 220 species do you expect to see at all? Ten? Fifty? Two hundred?
- The gene table has 14,226 rows. Same question.
- Which table do you expect to have more zeros, as a fraction of its cells?
- One sample in the species table has 438 total reads. The median is about 28,700. What do you expect to be wrong with that sample’s numbers?
- A typical sample behaves like how many equally common species? And like how many equally expressed genes?
Part 2: Ask, look, decide
Ask Pi for the per-sample numbers from the slides:
In step 1 of my notebook, make one data frame with a row for every sample in each count table, species and genes, and a
tablecolumn saying which. Columns: library size, number of features detected, fraction of zeros, the share held by the largest feature, Shannon diversity, and the effective number of features.Check three things before you look at any plot:
- It should have 942 rows, 471 samples for each table. If it has 471, one table went missing and Pi may not have told you.
- Type
sum(species$P001_week_18)in the R console yourself and compare it with Pi’s library size for that sample. - Features detected can never be more than 220 for species or 14,226 for genes.
Ask for a short report:
Under that data frame, add a summary table with the median, smallest and largest value of every column, separately for each table. Then write three sentences about anything unusual.
Compare the medians with your Part 1 answers. Then read Pi’s three sentences against the table. Did it mention the 438-read sample? Did it claim anything you cannot find in the numbers?
One number, four plots:
Plot the library size of every sample four ways: a histogram, the same histogram on a log scale, the samples ranked from smallest to largest, and library size by visit. Use both tables.
Which view makes the 438-read sample obvious? Which one hides it? Which would you put in a paper, and which would you only look at yourself?
From here on, write your own requests. Ask for features detected against library size for both tables, with the Spearman correlation for each. The gene table shows a strong correlation. Ask Pi how many extra genes that amounts to between the shallowest and the deepest samples, then check its answer against the plot. A strong correlation can still be a small effect.
Decide which samples to drop. Ask Pi to suggest a minimum library size for the species table and to explain why. Then ask for a table: at cutoffs of 1,000, 5,000, 10,000 and 20,000 reads, how many samples each would drop, and from which visits and outcomes. Losing samples evenly costs you less than losing most of them from one group.
Pick a cutoff and write one sentence in your notebook saying why. Then ask Pi what happens to your results if you keep the samples you dropped. Does that answer agree with the reason it gave the first time?
Polish one figure. Take the effective number plot, or any other you like, and improve it one request at a time until you would put it on a poster:
- axis titles in words, not column names
- log-axis labels that read 1k and 10k, not 1e+03
- two colours of your choice for species and genes
- text large enough to read from the back of the room
- saved as a PNG, 6 by 4 inches
Then ask for something you would not know how to code yourself: label the 438-read sample with its ID, or draw the median as a line with its value printed beside it. At some point try “make it look nicer” on its own and compare what you get with a specific request.
Argue with it. Ask Pi which logarithm
log()is in R. Then ask what the effective number would be if the Shannon formula usedlog2(). The Shannon value changes with the base. The effective number does not, as long as you undo the log with the matching base (2^forlog2()). If Pi says otherwise, ask it to compute both ways and show you the numbers.
Discussion
- Which of your Part 1 predictions was furthest off?
- Someone in the room will have decided to drop the 438-read sample and someone will have kept it. Both can defend it. What does each choice cost you?
- Which plot did you get today that you would not have made if you had to write the code yourself? Did it change a decision?
- The gene table detects over 98% of its features in nearly every sample. Does that mean all 14,226 genes are expressed, or does it mean something about how the table was built?
Exercise 2: Filters are choices
Every filter and cutoff in this exercise is a choice that someone else could make differently. Pi can produce the result of each choice in seconds. Putting them side by side, and picking one you can defend, is your part.
Same notebook, second half. Type /new in Pi to start a fresh conversation, so that the first exercise does not steer this one.
Step 5 builds a file that this afternoon’s notebook reads. If you are short on time, do step 5 first.
Part 1: Predict
- If you keep only species present in at least 10% of samples, how many of the 220 survive? What share of all the bacterial reads do you lose?
- Apply the same 10% prevalence rule to the 14,226 genes. How many survive?
- You want to compare one species between two samples. Name the single operation you must do to the counts first.
- If you drop the rare species, does the effective number of species in a sample go up, go down, or stay about the same?
Part 2: Ask, look, decide
Prevalence. Ask Pi for the prevalence of every feature in both tables, the share of samples where its count is above zero, and a plot of its distribution in each table. Then ask for the filter curve from the slides: for prevalence cutoffs from 0 to 0.9, how many features survive and what share of all counts survives, in both tables.
Check it against the slides. At 10%, the species table keeps 59 of 220 species and loses about 0.8% of the reads, and the gene table keeps all 14,226 genes.
A grid of filters. Comparing one cutoff at a time is slow, so ask for all of them at once:
- species: prevalence of at least 5%, 10% or 25%, crossed with a mean relative abundance of at least 0, 0.01% or 0.1%
- genes: mean CPM of at least 1, 5 or 10, crossed with prevalence of at least 0%, 50% or 90%
For each combination, ask for the number of features kept and the share of counts kept, first as a table and then as a plot. Ask for one summary table rather than a filtered copy of the data for every combination. A folder full of copies with similar names is the kind of thing that confuses Pi later.
Pick one combination for each table and write one sentence for each saying why. Compare with your neighbour. Then ask Pi to make one filtered version of each table, named for what it is.
Did the filter change anything you care about? Ask for each sample’s effective number of species before and after your filter, as a scatter plot with a 1:1 line. Compare it with your answer to Part 1, question 4. Then ask for the same plot for features detected. Which number moved, and why did the other one barely move?
Normalise, then log. Ask Pi to add counts per million and
log2(cpm + 1)to your filtered tables. Pick one species and one gene and ask for histograms of each before and after the log.The
+ 1is a choice too. Ask for the log histogram of the same gene with+ 0.5,+ 1and+ 10side by side. Where does the choice change the picture, and where does it make no difference?Build the grouping column. This one feeds the rest of the week, so get it right before you move on:
From the full species table, before any filtering, compute the fraction of each sample’s reads that belong to the genus Lactobacillus. Use
03_microbiome_taxonomy.csvto find which species those are. Add a column calledcommunitythat islactobacillusif the fraction is 0.5 or more andanaerobeif it is below 0.5. Savesample_id,lacto_fractionandcommunitytodata/my_sample_groups.csv.This afternoon’s notebook reads that file back, and so does Wednesday’s. Check it before you rely on it:
- Ask Pi to list the species names it counted as Lactobacillus, and read the list. Matching with the wrong case, or matching part of a name, can catch something it should not.
lacto_fractionmust lie between 0 and 1. Ask for the smallest and largest value.- Ask Pi to show you one sample’s Lactobacillus counts and its total, then do the division yourself.
The 0.5 cutoff. Ask for a histogram of
lacto_fractionwith a line at 0.5. Then ask how many samples would change group if the cutoff were 0.3 or 0.8, and how the split across the three visits changes. Ask Pi to justify 0.5. Is its reason about this data, or would it say the same about any dataset?Ask for the effective number of species in each community group, as a boxplot on a log scale. Which group is more diverse? Is that what you would call healthy?
Bar charts and heatmaps. Ask Pi for the microViz stacked bar chart of the 10 largest genera, and a heatmap of the 20 largest genera with your
communitycolumn as a strip along the top. Then ask for the same two figures for the gene table, by gene symbol. The gene bar chart takes about a minute.Now ask for variations: bars ordered by
lacto_fraction, bars split into two panels by community, 20 genera instead of 10. Which version tells you the most? In the gene heatmap, find two genes whose rows look alike. Ask Pi what they do, then look one of them up yourself on NCBI Gene.
Part 3: Reach (optional)
Ask Pi to turn your notebook into a short QC report as a new Quarto document, with the figures you chose, ending in a section called ## Choices made. That section lists every cutoff and filter you chose, with one alternative you did not choose. Render it and read the list. Is every choice you made today on it?
Then ask Pi to run the same report on group_a_probiotic or group_b_contraception, changing only the data folder. If it has to change more than that, something was hard-coded.
Discussion
- What did you filter, and what did you lose? Did your neighbour choose differently, and would either choice change a result?
- The bar chart works for the species table and fails for the gene table. What is different about the two tables that explains it?
- The 0.5 cutoff is arbitrary. What would change in your conclusions if it were 0.3, or 0.8?
- In the gut, higher diversity is usually read as healthier. What did you find here, and why might the vagina be different?
- We are about to use a grouping built from the species table to test the gene table. Why is that a fair thing to do, and what would make it unfair?