Sets and Enrichment

Grouping genes into named sets lets you describe a long list of hits in one sentence. The same method helps much less on a table of 53 species.

Slides

Make slides full screen

Exercise 1: From a gene list to a sentence

An enrichment table turns 1,698 genes into a sentence you could put in an abstract, and Pi will happily write that sentence for you. The work in this exercise is checking whether the sentence is real: whether the identifiers matched, whether the sets are separate findings, and whether a lopsided ranking could have produced the same table.

Open notebooks/05-enrichment.qmd and run the setup chunk. It reads the three result files you saved yesterday. Start Pi with a fresh conversation.

Part 1: Predict

  1. You are about to test 50 gene sets. If none were truly enriched, how many would come out with padj < 0.05?
  2. Your preterm-versus-term gene list had 838 genes up and 860 down, which is almost perfectly balanced. Does that make you more or less willing to believe an enrichment result?
  3. Name one biological program you expect to be elevated in the people who delivered preterm. You will check this against the answer.

Part 2: Ask, look, decide

  1. Run the enrichment:

    Using genes_by_outcome from the setup chunk, make a ranking vector from stat, named by the Ensembl ID in feature, with missing values dropped. Get the human Hallmark sets with msigdbr, using Ensembl IDs. Run fgsea(sets, stat, minSize = 15, maxSize = 500) and sort by padj.

    Then ask Pi how many genes are in the ranking (about 13,700) and how many of them appear in at least one Hallmark set. If that second number is close to zero, the identifiers did not match, and fgsea will still return a plausible-looking table. Ask how many sets are significant and how many point each way. You should see 26, with 23 higher in preterm.

  2. One result, several plots. Ask for a bar chart of NES for the top twelve sets, coloured by direction; the running-score plot for the top set; and a dot plot of all 50 sets with NES on one axis, -log10(padj) on the other and dot size for set size. Which would you put in a paper, and which tells you most about how the method works?

  3. A report, audited. From here on, write your own requests. Ask Pi to interpret the top five sets in a paragraph. It will probably write something confident about inflammation and preterm birth. Then ask for a table of how many genes each pair of those five sets share. Mark which of Pi’s “findings” are the same genes counted twice.

  4. Could the ranking have done this on its own? First ask Pi that question about your outcome result, and see whether it checks or just reassures you.

    Then run the same enrichment on genes_by_community and ask for one table with both rankings: genes up, genes down, sets significant, sets pointing one way. Then ask for the share of positive statistics among all genes, among significant genes, and among the 500 genes with the largest absolute stat, for both rankings. Check it against the slides: about 50% for outcome and about 4% for community among the 500 strongest.

    Now look at what sits in that lopsided tail. Ask for the 20 genes with the largest absolute stat in genes_by_community, as gene symbols from d$gene_info, with the direction of each. What kind of cell expresses most of them? Think back to the figure of the two epithelia in the slides.

    Write one sentence saying which of the two enrichment results you trust and why, and one saying what the community result tells you about these swabs.

  5. Alternatives side by side. Ask for the cutoff version as well: a hypergeometric test using only the genes with padj below 0.05, run for up and down genes separately. Then ask for fgsea on the Reactome collection (msigdbr collection C2, subcollection CP:REACTOME). Put the top results of all three next to each other. Where do they agree? Where they disagree, which method threw away information?

  6. Look at the genes. Ask for the leading edge of your top set, mapped to gene symbols with d$gene_info, and read the list. Then ask for the counts per million of two of those genes at week 18, by outcome. Does the plot look like the effect the enrichment table describes?

  7. Polish one figure. Make the NES bar chart ready for a talk: set names without the HALLMARK_ prefix and in lower case, colours labelled “higher in preterm” and “higher in term”, set size shown somewhere, and text large enough for a projector.

Part 3: Reach (optional)

Test the idea from step 4 directly: could the community ranking alone have produced its table? Ask Pi to draw 100 random sets of 190 genes from the community ranking and run the same fgsea call on them. How many of the random sets point toward the Lactobacillus end, and how many have padj < 0.05?

Compare that with the significant Hallmark sets from step 4. What does the lopsided tail explain, and what does it leave unexplained? Ask Pi how it would check whether the Hallmark sets share genes with that tail more often than random sets do.

Discussion

  • Was your Part 1 prediction of the elevated program right?
  • Two of the top sets overlap heavily. How would you report them without double counting?
  • Yesterday preterm against term gave you 1,698 genes and no clear story; today it gives you one sentence. What did enrichment add, and what did it hide?
  • Which of Pi’s claims would you have believed if you had not asked for the overlap table and the balance check?
  • If this study had been asking about immune activation in the cervix, what would you check before reporting the community result?

Exercise 2: The same function, on 53 species

The identical fgsea() call runs on 53 species, with sets built from taxonomy instead of curation. Pi will run it without complaint. Whether it was worth running is the question you have to answer.

Same notebook. Type /new in Pi first.

Part 1: Predict

  1. A gene set holds about 200 genes. A genus in our table holds between 2 and 12 species. What does that do to your ability to detect enrichment?
  2. Which genus do you expect to come out associated with preterm birth?
  3. Will testing ten genus sets need as aggressive a multiple-testing correction as testing fifty gene sets?

Part 2: Ask, look, decide

  1. Build the sets and run it:

    Using species_by_outcome from the setup chunk, make a ranking vector from stat, named by feature. Build one set per genus from the Genus column, keeping genera with at least two species. Run fgsea(genus_sets, species_stat, minSize = 2, maxSize = 50) and sort by padj.

    Check the sets before the results. Ask Pi how many genus sets there are

    1. and how many species are in the largest (12, Prevotella). Ask it to confirm that no species is in two sets and that there is no set called NA, which is what species with a missing genus become if nobody checks. Then read the results. Which genus is on top, and does anything pass?
  2. Family instead of genus. Ask for the same analysis with Family, and both tables side by side. Does the top family contain the top genus? Do the two levels tell the same story?

  3. The simple way. Ask for the 53 species sorted by stat as a table with their genus, and as a plot of rank against stat with the species of the top two genera coloured. Would you have reached the same conclusion without any enrichment analysis at all?

  4. Argue with it. Ask Pi whether enrichment analysis was useful on the species table. Then ask it to defend the opposite position. Compare the two answers and decide which argument is stronger.

  5. Write two or three sentences in your notebook answering: for which of our two tables was set-based analysis worth doing, and what feature of the table decides that?

Part 3: Reach (optional)

Invent a better set definition for the species table. Rather than taxonomy, group species by something functional: whether they are Lactobacillus or not, whether they are strict anaerobes, whether they are on a published list of BV-associated organisms. Ask Pi to help you build the sets, then read every set it makes. Where did it get the information that a species is a strict anaerobe, and can you check it?

Run fgsea with your hand-built sets. A set definition that encodes real biology can beat a taxonomic one, and designing such sets is research in its own right.

If you have time after that, ask for one Quarto report covering yesterday’s tests and today’s enrichment, ending in ## Choices made.

Discussion

  • The top genus is the one every clinician would have guessed, even though it does not pass. Is that reassuring, or a sign the analysis added nothing?
  • Taxonomy is a hierarchy and gene sets are a flat list. Does that difference matter for how you correct for multiple testing?
  • This is the last single-table module. Tomorrow we put the two tables together. Before you leave, write down what you think combining them could tell you that neither told you alone.