LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Guided project

Module 4 · RNA-seq analysis · Self-checked

Counts to results

Counts to results

Counts to results

Test four practice samples for differential expression, starting from a count table. Because the data were simulated, you know the right answer and can check every step. Nothing is submitted.

⏱ 11 min readModule 4 · RNA-seq analysis · Self-checkedFree
Lesson at a glance6 steps · about 11 min
  1. How to approach this
  2. Counts
  3. Differential expression
  4. Compare with the truth
  5. A volcano plot and a methods paragraph
  6. What the project shows

Research question

Which of the 12 practice genes change after treatment, and does your analysis find the four changes we built in?

This is the advantage of simulated data: reads generated by a computer from a known recipe, with chosen genes made to change by chosen amounts. In real experiments you never know the true answer, so you cannot tell whether a missed gene is a failure of the method or a gene that did not change. Here you can check every step against the truth, which is the best way to learn what each tool does and where it falls short.

Project filesA count table made from simulated reads with HISAT2 and featureCounts (you can make it yourself in Omics, Bioinformatics and AI, Module 5), and the sample sheet.
practice_counts.txtfeatureCounts output: 12 genes × 4 samples, two control and two treated practice_samples.csvSample sheet: sample name and condition

HISAT2 is an aligner: it finds where in the genome each read came from. featureCounts then counts how many aligned reads fall in each gene, producing a table like the airway count matrix of RNA-seq counts and exploration. A featureCounts table starts with a comment line beginning with # that records the command used. Below it, each gene's row begins with six columns of information about the gene (its ID, chromosome, start, end, strand and length), followed by one count column per sample, headed by the name of the alignment file it came from.

Those headers are the most common snag with featureCounts output. They are often long file paths, and R changes characters such as / and - to dots when it reads them, so they will not match the names in the sample sheet. That is why task 1 asks you to rename the columns yourself, in the sample sheet's order, and then run the same all(colnames(...) == rownames(...)) check as RNA-seq counts and exploration. If you ever rename by position, open both files and confirm the order by eye first.

The counts are whole numbers of reads, just like the airway counts, so DESeq2 can use them directly. The only real differences from the airway analysis are the size, 12 genes instead of tens of thousands, and the design: four independent samples rather than four donors each treated and untreated.

Counts to results · LSMA free course Already done: simulated reads for four samples were aligned with HISAT2 and counted per gene with featureCounts. Your four tasks follow: read the counts, run DESeq2, compare with the true changes, then report with a volcano plot and methods. 1 · ALREADY DONE FOR YOU Simulated readsHISAT2featureCounts 2 control, 2 treatedalign to the genomecount reads per gene 2 · YOUR TASKS TASK 1TASK 2TASK 3TASK 4 CountsDESeq2Truth checkReport 12 genes × 4padj < 0.05×4, ×3, ×2, ×¼volcano, methods
The top row produced practice_counts.txt; the bottom row is this project. The true changes in task 3 are the ones built into the simulation. Schematic.

How to approach this

  • Work from the lessons. Each task follows a lesson of this module. Reopen its code, then adapt it: the main changes are the input file, the sample names and the design.
  • Write one script that reads the files, runs every step and saves the outputs, so you can rerun it from the top.
  • Predict before you run. You know which genes should change and in which direction. Write your expectation down first, then compare. Where the output differs, work out why before moving on.
  • Remember the scale. With 12 genes and 2 samples per group, this is far smaller than a real experiment. Some behaviour you see here, especially in task 3, comes from that small size, and the checkpoints say so.

A few points from the lessons apply with extra force at this size. The filtering rule from RNA-seq counts and exploration was written for groups of four; with two samples per group, a rule such as "at least 10 reads in at least 2 samples" plays the same role. With only two replicates, each gene's own dispersion estimate is very rough, so DESeq2 leans heavily on the trend shared across genes, and with only 12 genes that trend is itself rough. Expect the analysis to have little power, the chance of detecting a real change, for modest effects. The four built-in changes were chosen to be large enough to find.

A PCA of four samples is still worth drawing, as a check that the two treated samples sit apart from the two controls, but with 12 genes it is a sketch rather than a summary of thousands of genes. vst() works from a subset of 1,000 genes by default, so with 12 genes it stops with an error; the message suggests calling varianceStabilizingTransformation() directly, and rlog() or a plain look at the normalised counts serves the same purpose here.

Finally, treat the project as a small version of a real analysis. In A reproducible workflow you kept data, scripts and outputs in one project folder and recorded the packages you used with sessionInfo(). Do the same here: save the results table, the volcano plot and the session information from the end of your script, so the methods paragraph in task 4 can quote versions you actually ran.

1

Counts

RNA-seq counts and exploration

  • Read practice_counts.txt with read.delim(..., comment.char = "#"), keep the four count columns, and give them sample names that match the sample sheet.
  • Check the library size of each sample with colSums().

Hint. read.delim() reads a tab-separated file, and comment.char = "#" tells it to skip the comment line at the top. Use the gene ID column as row names. The count columns are the last four, after the six gene-information columns, and their headers are alignment file names, so rename them in the same order as the samples appear in practice_samples.csv. The library size is a sample's total number of counted reads: the sum of its column.

Checkpoint: 12 genes × 4 samples; library sizes 8,998, 10,999, 10,000 and 8,500; PRG005 has the most reads in the control samples, and PRG010, which treatment induces, in treat_1.

2

Differential expression

RNA-seq counts and exploration and Differential expression with DESeq2

  • Run DESeq2 with design = ~ condition and control as the reference.
  • List genes with padj < 0.05.

Hint. Follow RNA-seq counts and exploration to build the dataset, but with the simpler design: there are no donors here, so the condition is the only variable. Make sure the sample sheet's row order matches the count columns before you build it, and set the reference level as RNA-seq counts and exploration did for dex. Then run the analysis and extract the treated-versus-control comparison as in Differential expression with DESeq2. When you list genes below the cut-off, remember that some padj values may be NA, so exclude those explicitly.

Checkpoint: DESeq2 finds exactly the four built-in genes: PRG003 and PRG004 up, PRG007 down and PRG010 up. (PyDESeq2 in Python finds the same four and may also flag PRG008, a gene with very few reads, just under 0.05: a reminder to treat low-count genes near the threshold with caution.)

3

Compare with the truth

Differential expression with DESeq2

  • The true changes were PRG003 ×4, PRG004 ×3, PRG010 ×2 and PRG007 ×¼. Compare them with DESeq2's fold changes, and with simple CPM fold changes (each count ÷ the sample's library size × 1,000,000).

Hint. DESeq2 reports log2 fold changes, so convert them back to plain ratios by raising 2 to that power: a log2 fold change of 1 is ×2, and −2 is ×¼. For CPM (counts per million), divide each count by its sample's library size from task 1 and multiply by 1,000,000; then average the two control samples and the two treated samples for each gene and divide treated by control. A small table with one row per built-in gene and columns for truth, DESeq2 and CPM makes the comparison clear. Then ask why the estimates differ: think about what happens to library sizes, and to the median-of-ratios size factors of RNA-seq counts and exploration, when four of only twelve genes change.

Checkpoint: DESeq2's estimates are closer to the truth than CPM's but still smaller for the induced genes (for example about ×2.8 rather than ×4 for PRG003), because with only 12 genes the normalisation is itself affected by the changes. Real experiments with thousands of genes are much less affected.

4

A volcano plot and a methods paragraph

Volcano plot and interpretation, and Module 3

  • Draw a volcano plot of your results, labelling the four significant genes.
  • Write a methods paragraph with every package, version and setting, and two limitations.

Hint. Reuse the volcano code from Volcano plot and interpretation, giving the labelling layer only the genes with padj below 0.05. With so few genes, labels will not crowd, but check that each sits on the side its fold change says. For the methods, start from the template in Volcano plot and interpretation and add what this analysis did differently: the aligner and counting tool, the design formula, the reference level and the cut-off. A limitation is a feature of the study that limits what the results can show; consider how many samples each group has and where the data came from.

Checkpoint: four labelled genes on the correct sides; someone could rerun your analysis from your methods alone; your limitations mention two replicates per group and simulated data.

What the project shows

If your results match the checkpoints, you have run a complete differential expression analysis and checked it against a known answer. You have also seen two honest limits of the method: estimates of the size of a change can be pulled towards zero when the normalisation is disturbed, and genes with very few reads sit close to the line between significant and not. Both lessons carry over to real data, where you have no truth to check against and must rely on the exploration, filtering and shrinkage steps of RNA-seq counts and exploration and Differential expression with DESeq2.

The reason for the shrunken estimates in task 3 is worth spelling out, because it is the composition bias of RNA-seq counts and exploration in miniature. Three of the four built-in genes rise with treatment, so they take a larger share of the treated samples' reads. Dividing by library size, as CPM does, then makes every treated gene look a little lower than it is, which pulls the induced genes' fold changes down. The median of ratios resists this by following the eight unchanged genes, but with only eight, the four changes still move the median. In an experiment with thousands of genes the same few changes would barely shift it.

The extra gene that PyDESeq2 may flag, PRG008, teaches the other half of the lesson. A gene with very few reads can cross padj < 0.05 in one implementation and not in another, because small differences in how dispersion is estimated matter most when the counts are tiny. The right response is not to choose the software that gives the answer you prefer, but to treat any low-count gene near the threshold as uncertain, look at its counts, and say so if you report it.

That completes the RNA-seq module. Across four modules you have gone from your first vectors and data frames, through the statistics that say how far a result can be trusted, to figures that show data honestly, and to a complete RNA-seq analysis checked against a known answer. The habits matter as much as the code: look at the data before testing, choose the analysis before seeing the results, report effect sizes with their uncertainty, and record exactly what you ran.

If you want to go on to machine learning, Module 4 of Introduction to Python builds on the same habits. The PCA and clustering you used to check samples become tools for finding structure in their own right, and checking a result against a known answer becomes cross-validation and an untouched test set.

Go deeper: normalising on genes you trust

Task 3 showed that when a third of the genes change, the median-of-ratios size factors are pulled along with them. DESeq2 lets you base the size factors only on genes you have reason to think are stable: dds <- estimateSizeFactors(dds, controlGenes = ctrl), where ctrl gives the row numbers, or a TRUE/FALSE vector, marking those genes. DESeq() run afterwards keeps these size factors instead of recalculating them.

In this simulated project you know the eight unchanged genes, so you can try it and see whether the fold changes move towards the truth. In real experiments the choice is harder. Classic housekeeping genes are not always stable, so a more reliable option is spike-ins: synthetic RNA, such as the ERCC controls, added in the same amount to every sample. Use control genes only with a clear reason, and say so in the methods.

Take it further

Repeat this module's lessons on the airway data for your portfolio. To make the count table yourself from raw reads, align the reads (FASTQ files) to the genome with a splice-aware aligner such as STAR or HISAT2, then count them per gene with featureCounts, the tool whose output this project reads. Or quantify them with Salmon or kallisto and import the estimates into R with the tximport package.

For a portfolio, keep the script, the saved figures and a short write-up together in one project folder, so a reader can see both what you found and exactly how. The airway analysis is a good showcase because readers can check your results against a published experiment and against the expected glucocorticoid-response genes.

Further reading · Module 4Love, Huber & Anders 2014: the method, and why it shrinks fold changes. · All reading for this course

Up next

Finish

Course complete

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.