IAE Academy

FOR CORE AND IT JOBS · CHAPTER 7 OF 7

Gene expression data and programming for biology

This chapter takes RNA-seq reads to a table of normalised counts, then to a list of genes that change between conditions, with a correction for testing thousands of genes. It ends with a short Python program for FASTA files. You need it to read and write any analysis of expression data.

Updated 30 Oct 2026

What you will be able to do

You will turn a table of read counts into values you can compare across samples and genes. You will compute a log2 fold change and explain what a p-value does and does not say. You will see why testing thousands of genes needs a correction, and apply one by hand. You will read and write a short Python program that reports the length and GC content of sequences.

Before you start

  1. Chapter 6, Sequencing data and genomics (reads, FASTQ files, alignment to a reference)

  2. Logarithms to base 2

  3. The mean of a set of values

  4. Basic probability: the chance of an event, conditional probability, and the expected number of events

  5. Sorting a list of numbers from smallest to largest

  6. Python 3 basics: functions, loops, strings, lists, dictionaries, reading a file, and installing a package with pip

From reads to normalised counts

In RNA-seq you extract the RNA of a sample, copy it to DNA and sequence it. Each read is aligned to a reference (Chapter 6), and you count the reads that fall inside each gene. The result is a table: one row per gene, one column per sample. These raw counts cannot be compared directly. A sample sequenced twice as deeply gives about twice the counts for each gene, on average. A long gene gives more reads than a short gene with the same number of molecules.

SHARE OF READS FROM ONE GENE

Holds on average when reads are sampled evenly along the RNA molecules. is the read count of gene , the sample's total reads, the gene length (kb; the unit cancels), the fraction of RNA molecules from gene . A molecule gives reads in proportion to its length.

COUNTS PER MILLION

Removes the effect of sequencing depth . Fair for the same gene in different samples when their overall mix of RNA is similar. It does not remove length, so it is not fair between different genes.

TRANSCRIPTS PER MILLION

From the share formula, , where is the same for every gene. Summing over genes gives , since the add up to 1, so the ratio is ; scales it. The length unit cancels. TPM removes depth and length, so it compares genes within a sample; it adds up to .

WATCH OUT FOR

Using CPM to say one gene is higher than another gene. Test: do their lengths differ? If yes, divide by length first, as TPM does.

Gene

Length (kb)

Reads

A

1

300

B

3

300

C

0.5

400

WORKED EXAMPLE

The table above gives three genes of one sample, with their lengths and read counts. Find the CPM and TPM of each gene.

Solution
  1. Total reads N = 300 + 300 + 400 = 1000, since CPM divides by the depth of the sample.

  2. CPM: A = 300/1000 × 10^6 = 300,000. B = 300,000. C = 400,000. Length played no part, so A and B look equal.

  3. Reads per kb, because a longer gene catches more reads: A = 300/1 = 300 reads/kb. B = 300/3 = 100 reads/kb. C = 400/0.5 = 800 reads/kb.

  4. Sum of reads per kb = 300 + 100 + 800 = 1200 reads/kb. This sum is the TPM denominator.

  5. TPM: A = 300/1200 × 10^6 = 250,000. B = 100/1200 × 10^6 ≈ 83,333. C = 800/1200 × 10^6 ≈ 666,667.

  6. Check: 250,000 + 83,333 + 666,667 = 1,000,000, as TPM must.

Answer: CPM: 300,000, 300,000, 400,000. TPM: 250,000, 83,333, 666,667. As molecules, gene C is the most abundant and gene B the least.

Differential expression and many tests

To find genes that change between two conditions, you need replicates: separate samples prepared the same way in each condition, such as three dishes of control cells and three of treated cells. You compare each gene's normalised values across them. The effect size is the fold change, the ratio of the two condition means. Ratios are lopsided (doubling is 2, halving is 0.5), so you take the logarithm to base 2, which makes them symmetric.

LOG2 FOLD CHANGE

Needs both means above zero and in the same normalised units. is the mean normalised expression (for example TPM or CPM) over the replicates of one condition. Positive means higher in treated, negative means lower.

Ratio treated/control

Log2 fold change

1 (no change)

0

2 (doubled)

1

4

2

1/2 (halved)

-1

1/4

-2

Replicates of one condition differ from each other by chance, so a fold change can appear even when the gene has no true difference. A statistical test, such as a t-test, compares the difference between the condition means with the spread among the replicates, and returns a p-value. The p-value answers one question: if the gene had no true difference, how often would chance alone give a difference at least as large as yours?

THE P-VALUE

Computed for one gene by the chosen test. is the probability of given . A small means your data would be rare if the gene did not change.

FALSE POSITIVES FROM MANY TESTS

Holds when no gene truly changes and each test gives with chance for an unchanged gene. is the number of genes tested, the p-value cutoff. With and you expect 1000 false calls.

The false discovery rate (FDR) is the expected fraction of called genes that are false (counted as 0 when no gene is called). The Benjamini-Hochberg method controls it at a level , such as 0.05. Sort the p-values from smallest to largest. A gene's rank is its position in that list, 1 for the smallest. Compare each p-value with a threshold that grows with its rank.

BENJAMINI-HOCHBERG RULE

Valid when the tests are independent or positively related. is the number of genes tested, the chosen FDR level, the sorted p-values. Find the largest that passes and call genes to , even if a smaller rank failed.

WATCH OUT FOR

Reading p = 0.01 as a 1% chance the gene is unchanged. Test: the p-value assumes no change and says how rare the data are. It never gives the chance of the assumption.

WORKED EXAMPLE

Five genes have p-values 0.001, 0.008, 0.039, 0.041 and 0.60. One of them has a control mean of 40 CPM and a treated mean of 160 CPM. Find its log2 fold change, then apply Benjamini-Hochberg with q = 0.05.

Solution
  1. Log2 fold change: ratio = 160 CPM / 40 CPM = 4, and log2(4) = 2. We use log2 so a rise and a fall of the same size get the same magnitude.

  2. The rule compares rank k with (k/m) q. With m = 5 and q = 0.05 the thresholds are 0.01, 0.02, 0.03, 0.04, 0.05 for k = 1 to 5.

  3. Compare: 0.001 ≤ 0.01 passes. 0.008 ≤ 0.02 passes. 0.039 ≤ 0.03 fails. 0.041 ≤ 0.04 fails. 0.60 ≤ 0.05 fails.

  4. The largest passing rank sets the cut. It is k = 2, so the two smallest p-values are called.

  5. A plain cutoff at 0.05 would have called four genes. Two of those do not survive the correction.

Answer: log2 fold change = 2 (a fourfold rise). Benjamini-Hochberg calls 2 genes.

A first program for biology

Sequences are stored in FASTA files. A header line starts with > and gives the name, its first word. The lines after it hold the sequence, often split over several lines, as in this file of one DNA sequence written 5' to 3':

>geneA sample 1
ATGCGC
GTAA

Your program must join those lines before measuring anything.

GC CONTENT

Holds for a DNA sequence written in capitals ACGT. is the number of bases X; the result is in per cent. Letters other than A, C, G and T, such as N (an unknown base), are left out of the denominator.

The full program, in plain Python 3 with no extra packages. It assumes each header carries a different name.

def read_fasta(path):
    seqs, name = {}, None
    with open(path) as f:
        for line in f:
            line = line.strip()
            if line.startswith(">"):
                name = line[1:].split()[0]
                seqs[name] = []
            elif name and line:
                seqs[name].append(line.upper())
    return {n: "".join(p) for n, p in seqs.items()}

def gc_content(seq):
    gc = seq.count("G") + seq.count("C")
    acgt = gc + seq.count("A") + seq.count("T")
    return 100 * gc / acgt if acgt else 0.0

for name, seq in read_fasta("genes.fasta").items():
    print(name, len(seq), round(gc_content(seq), 1))

A header opens a new entry and each sequence line is appended to it, so many-line sequences are joined. len(seq) counts every letter, while gc_content divides by the count of A, C, G and T, as the formula does. Biopython is a separate package (install it with pip install biopython; version 1.80 or later). Its parser does the reading, so the same task is shorter:

from Bio import SeqIO
from Bio.SeqUtils import gc_fraction

for rec in SeqIO.parse("genes.fasta", "fasta"):
    print(rec.id, len(rec.seq), round(100 * gc_fraction(rec.seq), 1))

The two programs agree on sequences of A, C, G and T. Biopython also counts some other letters, such as S (G or C), so results can differ when those occur.

Check yourself

CHECK YOURSELF 1

A DNA sequence, written 5' to 3', is ATGCGCGTAA. What is its GC content, in per cent?

Answer and solution

Answer: (accepted range )

  1. Length = 10 bases, all A, C, G or T.

  2. Count G: positions 3, 5, 7 give 3. Count C: positions 4 and 6 give 2.

  3. GC% = (3 + 2)/10 × 100 = 50.

CHECK YOURSELF 2

Two genes in one sample have the same CPM, but gene X is three times longer than gene Y. Which statement is correct?

  1. (A)

    Gene X and gene Y are equally abundant as molecules.

  2. (B)

    Gene Y has more molecules than gene X, since reads per kb is higher for Y.

  3. (C)

    Gene X has more molecules, because it is longer.

  4. (D)

    Nothing can be said, since CPM does not use counts.

Answer and solution

Answer: (B)

  1. Same sample and equal CPM means equal counts.

  2. Reads per kb for X is one third of that for Y.

  3. Reads per kb is proportional to molecule abundance, so Y has more molecules.

CHECK YOURSELF 3

The mean normalised expression of a gene is 12 in control and 3 in treated samples. What is its log2 fold change (treated over control)?

Answer and solution

Answer: (accepted range )

  1. Ratio = 3/12 = 1/4.

  2. log2(1/4) = -2.

CHECK YOURSELF 4One or more options are correct

10,000 genes are tested at a p-value cutoff of 0.01, and in truth none of them changes. Which statements are correct?

  1. (A)

    About 100 genes are expected to be called significant.

  2. (B)

    No gene is expected to be called significant.

  3. (C)

    Benjamini-Hochberg aims to limit the expected fraction of called genes that are false.

  4. (D)

    A p-value of 0.01 means a 1% chance that the gene is unchanged.

Answer and solution

Answer: (A), (C)

  1. Expected false positives = m α = 10,000 × 0.01 = 100, so A is correct and B is not.

  2. C is the definition of controlling the false discovery rate.

  3. D confuses the p-value with the probability of the assumption, so it is wrong.

Used in these GATE topics