LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 73 lessons

Lesson 5 of 7

Module 5 · Sequencing data

Counting reads per gene

Counting reads per gene

Counting reads per gene

featureCounts compares each alignment with the gene annotation and counts the read pairs that fall in each gene. The result, a count matrix, is the input for every differential expression tool.

⏱ 12 min readModule 5 · Sequencing dataFree
Lesson at a glance7 steps · about 12 min
  1. From alignments to a count matrix
  2. What this does
  3. Reading the output
  4. How to read this table
  5. A first look in R
  6. What this does, line by line
  7. Reading the output

From alignments to a count matrix

featureCounts is part of the Subread package you installed in From sample to reads. For every aligned fragment it asks one question: which gene's exons does this overlap? If the answer is exactly one gene, that gene's count goes up by one. By default, fragments that overlap two genes are left out as ambiguous, and fragments that align equally well to several places in the genome are left out too, because they cannot be given to one gene with confidence. The result is a count matrix: a table with one row per gene, one column per sample, and a whole number in every cell.

Two choices in that description are worth understanding. Fragments are counted, not reads, because the two reads of a pair come from one RNA molecule, as From sample to reads showed; counting both would count that molecule twice. And only exons are used, because the mature RNA you sequenced has had its introns removed. Reads that fall in introns usually come from RNA caught before splicing had finished, and counting them would mix that unfinished RNA into the measure of the gene.

The gene positions come from the annotation file, practice_genes.gtf. GTF is a close relative of the GFF format from Module 2: one line per feature, with the chromosome, the start and end, the strand, the feature type in column 3, and a list of attributes in column 9, such as gene_id. featureCounts only compares two sets of positions, where each fragment aligned and where each exon is, so the annotation must use the same genome build and chromosome names as the alignments, as Aligning reads to a genome warned.

Terminal
# align the other three samples the same way (or use a loop, as in Trimming and filtering), then:
featureCounts -p --countReadPairs -t exon -g gene_id \
              -a data/practice_genes.gtf -o counts/practice_counts.txt aligned/*.bam
cat counts/practice_counts.txt.summary | head -n 3
cut -f 1,7- counts/practice_counts.txt | tail -n +2
Status aligned/ctrl_1.bam aligned/ctrl_2.bam aligned/treat_1.bam aligned/treat_2.bam Assigned 8998 10999 10000 8500 Unassigned_Unmapped 2 1 0 0 Geneid aligned/ctrl_1.bam aligned/ctrl_2.bam aligned/treat_1.bam aligned/treat_2.bam PRG001 234 366 251 158 PRG002 1047 1674 1193 941 PRG003 653 845 1800 1393 PRG004 192 156 358 277 PRG005 3161 3945 2461 2545 PRG006 295 373 216 175 PRG007 344 796 102 105 PRG008 47 80 25 26 PRG009 384 442 370 310 PRG010 2287 1995 2984 2322 PRG011 335 300 218 239 PRG012 19 27 22 9

What this does

  • The comment reminds you that all four samples must be aligned first; Trimming and filtering showed how to loop over them.
  • featureCounts reads every BAM file in aligned/ and the annotation given with -a, and writes the counts to the file given with -o. The options are explained in the table below. It also writes a second file, with .summary added to the name, that says why fragments were or were not counted.
  • head -n 3 shows the first three lines of that summary.
  • cut -f 1,7- keeps column 1 and every column from 7 onwards, and tail -n +2 prints from line 2 onwards, skipping the first line, a comment in which featureCounts records the command it ran.

Reading the output

  • The summary: in ctrl_1, 8,998 fragments were assigned to a gene and 2 were not counted because neither read of the pair aligned (Unassigned_Unmapped). 8,998 + 2 = 9,000, every pair in the sample. The HISAT2 log from Aligning reads to a genome agrees: of the 34 pairs that did not align as a pair, 32 still had one read aligned, which leaves 2 pairs with no aligned read at all.
  • The count matrix: one row per gene, PRG001 to PRG012, and one column per BAM file. For example, PRG003 has 653 and 845 fragments in the two controls and 1,800 and 1,393 in the treated samples.
  • The numbers in each column add up to that sample's assigned total; for ctrl_1, 234 + 1,047 + … + 19 = 8,998.
  • The full summary file has more lines than the three shown, one for each reason a fragment can go uncounted, such as Unassigned_NoFeatures (aligned where no exon is annotated), Unassigned_Ambiguity (overlaps more than one gene) and Unassigned_MultiMapping (aligned to several places). The practice data were simulated from the annotated genes, so these are zero. On real data they are not, and a sample with far more NoFeatures than the others can point to genomic DNA in the RNA or to a wrong annotation.
OptionWhat it does
-p --countReadPairsPaired-end data: count each fragment (pair) once, not each read.
-t exon -g gene_idCount reads overlapping exons, and add them up per gene.
-s 0, 1 or 2Strandedness of the library: 0 unstranded (the default), 1 or 2 for stranded kits. Check your kit; the wrong setting loses most counts.

How to read this table

Each row is an option from the command. Two terms need explaining. A feature is one line of the annotation, such as an exon; -t exon says which lines to count against, and -g gene_id says which attribute in column 9 groups them into genes, so a gene's exons share one total. Strandedness describes whether the library remembers which DNA strand the RNA was copied from. In an unstranded library a read can come from either strand. In a stranded library it cannot, so featureCounts can tell apart two genes that overlap on opposite strands; but if you give the wrong setting, it looks for every read on the wrong strand and counts almost nothing. If you are unsure, run featureCounts with each setting on one sample and compare the number assigned: the right setting stands out.

One option is not in the table because the command does not need it: what to do with fragments that align to several places. The default, used here, leaves them out. Counting them, with -M, gives each such fragment to every place it aligned, so the same molecule adds to several genes. For a first analysis the default is the safer choice; just remember that genes with close copies elsewhere in the genome are undercounted, as Aligning reads to a genome explained.

Go deeper: reading the strand setting from the numbers

Most stranded RNA-seq kits in use today, including Illumina's TruSeq Stranded kits, use a method called dUTP marking, in which read 1 comes from the strand opposite to the RNA. For these kits the right featureCounts setting is -s 2, not -s 1, which surprises many people.

The test run described above gives a clear pattern. For a dUTP library, -s 2 might assign, say, 80% of fragments, -s 1 only a few per cent, and -s 0 about the same as -s 2. For an unstranded library, -s 1 and -s 2 each assign roughly half of what -s 0 does. When the numbers fall between these patterns, suspect a mislabelled kit or contamination with genomic DNA. The script infer_experiment.py, from the RSeQC package, runs a similar check directly on a BAM file.

A first look in R

The first six columns describe each gene; the counts start at column 7. Read them into R and compare the groups with counts per million (CPM).

Counts per million adjusts for depth: each count is divided by the sample's total and multiplied by one million, so it answers "how many of every million fragments in this sample came from this gene?". A log2 fold change compares two groups on a doubling scale: 1 means twice as high, 2 four times as high, −1 half as high and 0 no change.

R
fc <- read.delim("counts/practice_counts.txt", comment.char = "#")
counts <- as.matrix(fc[, 7:10])
rownames(counts) <- fc$Geneid
colnames(counts) <- sub("aligned\\.(.*)\\.bam", "\\1", colnames(counts))
colSums(counts)

cpm <- t(t(counts) / colSums(counts)) * 1e6
lfc <- log2(rowMeans(cpm[, 3:4]) + 1) - log2(rowMeans(cpm[, 1:2]) + 1)
round(sort(lfc), 2)
ctrl_1 ctrl_2 treat_1 treat_2 8998 10999 10000 8500 PRG007 PRG008 PRG006 PRG012 PRG001 PRG005 PRG011 PRG002 PRG009 PRG010 PRG004 -2.29 -1.17 -0.66 -0.49 -0.44 -0.38 -0.37 -0.22 -0.17 0.39 0.95 PRG003 1.20

What this does, line by line

  • read.delim reads the tab-separated count file into a data frame; comment.char = "#" skips the first line, which records the command.
  • fc[, 7:10] takes columns 7 to 10, the four samples, and as.matrix turns them into a numeric matrix. The gene IDs become the row names.
  • When R read the file, it changed the column names to make them valid R names: aligned/ctrl_1.bam became aligned.ctrl_1.bam. The sub line keeps only the part between aligned. and .bam, the sample name.
  • colSums(counts) adds up each column: the library sizes.
  • The cpm line divides each column by its total and multiplies by one million. The two t() calls, which flip the matrix on its side and back, make R divide each column, not each row, by the right total.
  • The lfc line averages the CPM of the two treated samples (columns 3 and 4) and of the two controls (columns 1 and 2), adds 1 to each so that a gene with zero counts does not produce the log of zero, and takes the difference of the log2 values.
  • Adding 1 hardly matters here, because the practice libraries are tiny and every CPM is in the hundreds or thousands. On real data, with tens of millions of fragments, many genes have a CPM below 1, and adding 1 then pulls their fold changes towards zero. That is one reason why genes with very low counts are usually removed before any testing.
  • round(sort(lfc), 2) lists the genes from lowest to highest fold change, to two decimal places.

Reading the output

The library sizes match the Assigned line of the summary. The fold changes run from −2.29 for PRG007, the gene built to fall to a quarter, to 1.20 for PRG003, built to rise four-fold. Compare them with the true changes in the worked example below: the order is right, but most numbers are off, and the next section explains why.

Keep in mind that with two samples per group these fold changes are only averages. They say nothing about how much each gene varies between replicates, and so nothing yet about whether a change is larger than chance. PRG007, for example, has 344 and 796 fragments in the two controls, a spread of more than double between two samples of the same condition. Weighing a change against that kind of variation is the job of a statistical test such as DESeq2 or edgeR, introduced in Beyond RNA-seq: other assays and pipelines.

Worked example · why CPM can mislead

we built in four changes: PRG003 ×4, PRG004 ×3, PRG010 ×2, PRG007 ×¼
CPM: PRG003 only ×2.3 (log2 1.20), and most unchanged genes look lower in treated (−0.2 to −0.7)
reason: more of each treated library goes to the induced genes, so less is left for the rest

Step by step for PRG003. CPM in ctrl_1 is 653 ÷ 8,998 × 1,000,000 ≈ 72,572, and in ctrl_2 845 ÷ 10,999 × 1,000,000 ≈ 76,825; their mean is about 74,698. In treat_1 it is 1,800 ÷ 10,000 × 1,000,000 = 180,000, and in treat_2 1,393 ÷ 8,500 × 1,000,000 ≈ 163,882; their mean is about 171,941. The ratio is 171,941 ÷ 74,698 ≈ 2.3, and log2 of 2.3 is about 1.20. The true change was ×4, which is log2 = 2.

What the answer means. A gene that did not change at all, such as PRG005, comes out at −0.38, about 23% lower in the treated group. Nothing happened to that gene; the treated libraries gave a larger share of their reads to the induced genes, as the figure below shows. A test built on CPM alone would report many unchanged genes as falling.

Real experiments show the same effect whenever a few genes take a large share of the reads and change between groups. A strong immune response, or a treatment that switches on a handful of very abundant genes, shrinks the share left for everything else, so CPM shows thousands of genes as slightly lower. The practice data exaggerate the effect so that it is easy to see, but the cause is the same.

This is a composition effect. CPM assumes every library has the same make-up; DESeq2's median-of-ratios normalisation assumes instead that most genes do not change, which is more robust. It is applied when the counts are tested for differential expression, the step that follows counting.

Counting reads per gene · LSMA free course For each sample, the share of its counted fragments that fall in the three induced genes PRG003, PRG004 and PRG010: 34.8% in ctrl_1, 27.2% in ctrl_2, 51.4% in treat_1 and 47.0% in treat_2. The other nine genes share what is left. SHARE OF EACH LIBRARY fragments ctrl_134.8%8,998 ctrl_227.2%10,999 treat_151.4%10,000 treat_247.0%8,500 PRG003, PRG004 and PRG010 (induced) the other nine genes
Calculated from the count matrix above: each gene's count divided by the sample's total. The treated libraries spend about half their reads on the three induced genes, so every other gene gets a smaller slice, and CPM, which only divides by the total, reads that smaller slice as lower expression.

featureCounts is described by Liao, Smyth and Shi (2014, Bioinformatics). The composition effect in the worked example was set out by Robinson and Oshlack (2010, Genome Biology), who introduced the TMM normalisation used by edgeR; the median-of-ratios method used by DESeq2 comes from Anders and Huber (2010, Genome Biology). Report the Subread version with your counts. In recent releases -p on its own no longer counts each pair once, which is why the command adds --countReadPairs; scripts written for older versions can give different totals.

Key point

A count matrix plus a sample sheet is all DESeq2 needs. Check that the column order matches the sample sheet every time you load them.

Here is why the order matters. Analysis tools generally pair the first column of counts with the first row of the sample sheet, the second with the second, and so on. If the sheet lists treat_1 first while the matrix starts with ctrl_1, a control would be analysed as treated, and every result that follows would be wrong. Checking that the column names and the sample names match, in the same order, takes a moment and catches this.

The count matrix is also the hand-over point between this module's command-line steps and the statistics in R. Everything before it, from FASTQ files to BAM files, is about placing reads correctly; everything after it is about comparing numbers. If a result later looks strange, the summary file and the alignment logs from Aligning reads to a genome are the first places to look.

Further reading · Module 5Conesa et al. 2016: the whole RNA-seq workflow, from design to interpretation. · All reading for this course

Check your understanding

1. Why use --countReadPairs for paired-end data?

2. Nearly all reads are “Unassigned_NoFeatures” with -s 1 . What is the likely cause?

3. One gene rises strongly after treatment. Using CPM, other unchanged genes will tend to look:

Up next

Lesson 6

Beyond RNA-seq: other assays and pipelines

Ask BioTutor

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