Further reading: not part of the course. This material is not included in the course, but it is relevant if you want to learn more about the topic.

Quiz: log and pseudolog transforms

Short questions to test your understanding of log and pseudolog transforms of count data. Try each on paper first, then open the answer.


Q1. Give two reasons why single-cell counts are log-transformed before PCA.

Think about how far apart the gene means are, and about how the variance of a count depends on its mean.

  1. Expression spans orders of magnitude: a few genes (ribosomal, mitochondrial) have hundreds of UMIs per cell, most fewer than one. Biological changes are multiplicative (fold changes), and the log puts a doubling at 1 UMI and at 200 UMIs on the same footing. (2) For counts the variance grows with the mean (Poisson: variance = mean; negative binomial: more). On raw or normalized counts the few highly expressed genes have by far the largest variance, so PCA, which looks for directions of largest variance, describes their noise instead of the cell types. After the log, genes that vary because of biology (markers) have the largest variance.

Q2. A gene has a \(\log_2\) fold change of \(-3\) between treated and control. What happened to its expression? What is the \(\log_2\) fold change of a gene that goes from 30 to 120?

\(\log_2\) FC \(= \log_2(\text{treated} / \text{control})\).

\(2^{-3} = 1/8\): expression dropped to one eighth (8-fold down). From 30 to 120 is a fold change of 4, so \(\log_2\) FC \(= 2\). On the log scale, 8-fold up and 8-fold down are \(+3\) and \(-3\): symmetric.


Q3. Is \(f(x) = \log x\) a linear function? Justify with the linear test and one numerical example.

Linear means \(f(a + b) = f(a) + f(b)\) and \(f(c\,a) = c\,f(a)\). Try \(a = 10\), \(b = 1000\) with \(\log_{10}\).

No. \(\log_{10}(10 + 1000) = 3.004\), but \(\log_{10} 10 + \log_{10} 1000 = 4\). Scaling fails too: \(\log_{10}(2 \cdot 10) = 1.30 \ne 2 \log_{10} 10 = 2\). The log turns products into sums (\(\log(ab) = \log a + \log b\)), not sums into sums, and scaling by \(c\) becomes adding \(\log c\). So no matrix can do it, and the order of operations matters.


Q4. You want the expression of a gene in a cluster of cells, to compare between patients. Colleague A averages the log1p-normalized values of the cells. Colleague B sums the raw counts of the cells (pseudobulk), then normalizes and logs. Why can they get different answers, and which one is the usual choice for comparing samples?

Is “the mean of the logs” the same as “the log of the mean”?

Because the log is not linear, the mean of logs is not the log of the mean; it is always smaller or equal (it is the log of a geometric mean, which gives large values less weight). For counts 0, 1, 3, 15: the mean of \(\log_2(1 + x)\) is 1.75, but \(\log_2(1 + 4.75) = 2.52\). Summing is linear, so it belongs on the count scale: pseudobulk (sum counts per sample, then normalize, log, or use a count-based bulk method) is the usual choice for comparing samples.


Q5. What is the pseudolog, what problem does it solve, and what happens to a zero?

What is \(\log 0\)? What is \(\log(1 + 0)\)?

The pseudolog is \(\log(1 + x)\), the log with a pseudocount of 1 (log1p(x) in R). Single-cell matrices are mostly zeros and \(\log 0 = -\infty\); adding 1 makes the log defined everywhere. A zero stays a zero (\(\log 1 = 0\)), so the matrix stays sparse. For large \(x\) it is almost \(\log x\); near 0 it is almost \(x\).


Q6. Two analysts compute a \(\log_2\) fold change for a lowly expressed gene with mean 1 UMI in group A and 3 in group B. One uses pseudocount \(c = 0.1\), the other \(c = 10\). What does each get, and what is the general lesson?

\(\log_2 \frac{3 + c}{1 + c}\).

\(c = 0.1\): \(\log_2(3.1 / 1.1) = 1.49\), close to the true \(\log_2 3 = 1.58\). \(c = 10\): \(\log_2(13 / 11) = 0.24\), almost no change. A large pseudocount squashes low counts and shrinks their fold changes towards 0; a small one keeps them but also blows up noise, e.g. 0 → 1 UMI becomes \(\log_2(1.1 / 0.1) = 3.46\). Counts well above \(c\) are hardly affected. The pseudocount is a choice that sets below which count level differences are damped.


Q7. Seurat’s NormalizeData computes \(\log(1 + \tfrac{x}{n} \cdot 10{,}000)\), with \(n\) the total UMIs of the cell. What is the pseudocount in raw counts for a cell with 5,000 UMIs, and for a cell with 20,000? Why does this matter?

Factor out \(\tfrac{10{,}000}{n}\): \(1 + \tfrac{10{,}000}{n} x = \tfrac{10{,}000}{n}\big(x + \tfrac{n}{10{,}000}\big)\).

The pseudocount in raw counts is \(n / 10{,}000\): 0.5 for the 5,000-UMI cell and 2 for the 20,000-UMI cell. So the pseudocount differs between cells, and changing the scale factor changes it for all cells: CPM (\(10^6\)) makes it about 100 times smaller, so the step from 0 to 1 UMI becomes huge. The scale factor is therefore not just a choice of units.


Q8. You plot the expression of a T-cell marker in B cells, where most cells have 0, using scale_y_log10(). The boxplot shows a B-cell median of about 4. What went wrong, and how do you fix it?

What is \(\log_{10} 0\)? What does ggplot2 do with values that become infinite?

\(\log_{10} 0 = -\infty\). A log axis transforms the values before the boxplot statistics are computed, so all zeros are removed (only a console warning says so), and the median is that of the few B cells that expressed the gene. The true median is 0. Fix: plot log1p(expr) (labels can still be in counts), so every cell is kept.