Workshop 2, Module 2
Network: Golden_Tulip
Click on Conference
Password: Diamond254
Today the same table has 220 rows of bacteria, 14,226 rows of human genes, and 471 columns.
Three of the six files in the folder. Each one becomes a tibble.
| file | what it holds |
|---|---|
| 01_metadata.csv | one row per sample: participant, visit, outcome, clinical measures |
| 02_microbiome_counts.csv | 220 bacterial species x 471 samples |
| 03_microbiome_taxonomy.csv | what each species is: domain down to species |
| 04_rnaseq_counts.csv.gz | 14,226 human genes x 471 samples |
| 05_gene_annotation.csv | what each gene is: Ensembl id, symbol, name |
| 06_cytokines.csv | measured protein concentrations, for the group projects |
| feature | P001_week_18 | P001_week_24 | P001_week_34 |
|---|---|---|---|
| lactobacillus_iners | 2956 | 2301 | 7528 |
| lactobacillus_crispatus | 30 | 45 | 30 |
| gardnerella_swidsinskii_leopoldii | 193 | 17977 | 10336 |
| lactobacillus_jensenii | 18362 | 9 | 0 |
| candidatus_lachnocurva_vaginae_bvab1 | 0 | 6 | 9 |
| gardnerella_vaginalis | 46 | 3961 | 4184 |
6 of 220 rows and 3 of 471 columns: participant P001 at three visits.
| feature | P001_week_18 | P001_week_24 | P001_week_34 |
|---|---|---|---|
| ENSG00000188976 | 275 | 432 | 705 |
| ENSG00000187961 | 20 | 8 | 33 |
| ENSG00000187583 | 63 | 17 | 109 |
| ENSG00000187642 | 0 | 0 | 24 |
| ENSG00000188290 | 62 | 39 | 44 |
| ENSG00000187608 | 132 | 71 | 70 |
The same three samples. Both tables have features in rows, samples in columns and counts in the cells.
| taxon | sample_id | count |
|---|---|---|
| lactobacillus_iners | P001_week_18 | 2956 |
| lactobacillus_iners | P001_week_24 | 2301 |
| lactobacillus_iners | P001_week_34 | 7528 |
| lactobacillus_crispatus | P001_week_18 | 30 |
| lactobacillus_crispatus | P001_week_24 | 45 |
| lactobacillus_crispatus | P001_week_34 | 30 |
220 species times 471 samples gives 103,620 rows.
In this shape, group_by(sample_id) works on one sample at a time and group_by(taxon) works on one species at a time.
Answering them is the quality control step, and it is all we do today.

| table | median | smallest | largest |
|---|---|---|---|
| species | 28,703 | 438 | 155,686 |
| genes | 12,220,381 | 6,026,167 | 28,835,256 |
Both spread over more than an order of magnitude. Note the axis labels.
One sample has 438 bacterial reads. What would you do with it?

Deeper gene libraries do detect more genes (rho = 0.50), but only about 110 more from the shallowest fifth of samples to the deepest, out of 14,226.
In the species table depth barely matters at these depths (rho = 0.06). The community does: samples dominated by Lactobacillus show a median of 17.5 species, and the rest show 33.
| table | features in the table | median detected | median share detected | median cells that are zero |
|---|---|---|---|---|
| species | 220 | 24 | 10.9% | 89.1% |
| genes | 14,226 | 14,084 | 99.0% | 1.0% |
The species table is mostly zeros and the gene table is almost solid, so the two tables need different filters.

Half of our bacterial samples are more than 60% one species. The most extreme is 99.9%.

Which bag is more diverse? Counting colours says they are nearly the same.
Draw one ball. If its colour makes up a share p of the bag, your surprise is −ln(p).
| bag | colours | top colour | Shannon |
|---|---|---|---|
| bag 1 | 6 | 84% | 0.66 |
| bag 2 | 6 | 76% | 0.90 |
| bag 3 | 7 | 70% | 1.12 |
| bag 4 | 7 | 46% | 1.56 |
| one colour only | 1 | 100% | 0.00 |
| 8 colours, 6 of each | 8 | 12% | 2.08 |
exp(Shannon) is how many equally common colours would give the same Shannon value.
| bag | colours | effective number |
|---|---|---|
| bag 1 | 6 | 1.9 |
| bag 2 | 6 | 2.5 |
| bag 3 | 7 | 3.1 |
| bag 4 | 7 | 4.8 |
| one colour only | 1 | 1.0 |
| 8 colours, 6 of each | 8 | 8.0 |
Bag 1 has six colours but behaves like two. Bag 4 behaves like five.
log() in R is the natural log, and exp() undoes it.
Start from genes_long and the same six lines give the numbers for the gene table.

The median vaginal sample behaves like 3 equally common species. The median transcriptome behaves like about 1,000 equally expressed genes.

Samples dominated by Lactobacillus behave like about 2 species. The rest behave like about 9. In the gut, more diverse usually reads as healthier. Here it is the reverse.
| Shannon against | species | genes |
|---|---|---|
| library size | 0.03 | 0.06 |
| features detected | 0.76 | 0.35 |
| top feature’s share | -0.97 | -0.93 |
Depth barely moves it, because depth mostly changes how many rare features you catch, and rare features add little. In both tables it is close to the mirror image of dominance, the share held by the top feature.
Nobody reports the Shannon diversity of a transcriptome, even though “behaves like about a thousand equally expressed genes” is a fair summary of one.
Still, the number of features you detected and how evenly the counts are spread are what you look at to decide which samples to throw away, in both experiments.
Microbiome papers report these numbers as alpha diversity. RNA-seq papers bury them in the QC section.

| prevalence cutoff | species: features kept | species: counts kept | genes: features kept | genes: counts kept |
|---|---|---|---|---|
| 0% | 220 | 100.00% | 14,226 | 100.00% |
| 10% | 59 | 99.24% | 14,226 | 100.00% |
| 25% | 32 | 95.79% | 14,225 | 100.00% |
| 50% | 18 | 90.62% | 14,220 | 100.00% |
| sparse table (species) | dense table (genes) | |
|---|---|---|
| the problem | most features are absent from most samples | nearly everything is present at some level |
| filter on | prevalence | expression level |
| typical rule | present in at least 10% of samples | mean CPM of at least 5 |
| what it buys you | fewer tests, less noise | fewer tests, less noise |


Microbiome papers call this “relative abundance”. RNA-seq papers call it “CPM”.

The + 1 is there because log2(0) is negative infinity. That choice changes your answer, and you should be able to say why you made it.
as_counts() builds the same kind of object from either table, and we use it all week.
microViz was written for microbiome data. It wants a count table, a sample table, and a table saying what each feature is. A gene annotation fills that third slot, so the same verbs work on genes.
| microViz wants | species | genes |
|---|---|---|
| counts | 02_microbiome_counts.csv |
04_rnaseq_counts.csv.gz |
| samples | 01_metadata.csv |
01_metadata.csv |
| what each feature is | 03_microbiome_taxonomy.csv: Domain down to species |
05_gene_annotation.csv: locus_group, then symbol |
The first is the stacked bar chart from almost every microbiome paper you have read.
Nobody makes the second. The next two slides show why.

In a typical sample the ten largest genera hold 99% of the reads.

In a typical sample the ten largest genes hold 26% of the reads. The grey block is the other 14,216 genes.
A stacked bar has to share one axis between all the features, so anything below a few percent disappears.
In a heatmap each feature gets a row and each sample a column. The colour of a cell is that feature’s share of that sample, on a log scale.

Darker cells are larger shares. comp_heatmap() puts similar samples next to each other. The strip on top is the grouping column you build in Exercise 2.
The code changes in two places: the rank is "symbol", and we ask for 25 rows.

HBA1, HBA2 and HBB are haemoglobin, and they rise and fall together. What does a sample that is dark in those three rows have in it?
| stacked bar chart | heatmap | |
|---|---|---|
| shows | shares that add up to the whole sample | each feature on its own row |
| scale | linear | log |
| suits | a few features that hold most of the counts | many features, large and small |
| species table | yes | yes |
| gene table | no | yes |
A Codespace is a computer that GitHub runs for you. R, Quarto, the packages and the data are already on it.
VS Code is the editor you see in the browser tab. Your files are on the left, the open notebook is in the middle, and the terminal is at the bottom.
Nothing is installed on your laptop.


With a chat window you copy code into R, run it, and copy the error back. An agent has tools for those steps and repeats them until it thinks the task is done.
You type: plot the library size of every sample in the species table
notebooks/02-count-table-anatomy.qmd02_microbiome_counts.csvPi does not stop to ask you between steps. Read the code from step 5 before you believe the summary in step 6.
Before it answers, Pi reads CLAUDE.md at the top of your repository. Ours says you are a biologist who is new to coding.
After that it knows only the files it opens and the conversation so far.
Type /new to start a fresh conversation when you change task.
All three are coding agents that run in a terminal and work in the same loop.
| Pi | Claude Code | Codex | |
|---|---|---|---|
| made by | an open-source project | Anthropic | OpenAI |
| models | from any provider | built for Claude | built for GPT |
| you pay through | our OpenRouter key, per request | an Anthropic plan or API key | an OpenAI plan or API key |
| before running a command | does not ask | asks, unless you change it | asks, unless you change it |
We chose Pi because one key reaches models from many companies, so you can change model with one command and see what each costs. What you practise with Pi carries over to the other two.
Your key has a $3 daily limit. Every request spends some of it.
z-ai/glm-5.3-flash, which is cheap and fast./model or Ctrl+L switches to z-ai/glm-5.3, the larger model from the same family. It costs more per request.A long conversation costs more per message than a short one, because the whole conversation is sent with every request.
Open your Codespace, open a terminal, type pi and press Enter.
Follow along while we give Pi its first task.
Code is cheap now. Cognitive load is more expensive, because we can do more than we used to. It is still your job, as a scientist or a statistician, to understand the choices you made and be able to defend them.
So you spend less time coding and more time reviewing. Ask for lots of plots. Ask for reports. Argue with the computer.
You can get to the first polished-looking output very fast, but in some ways that is when your job begins now.
Do not skip step 5. This afternoon you colour your ordinations by that column, and Wednesday afternoon starts from it.
Open notebooks/02-count-table-anatomy.qmd in your Codespace.