You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Principal Component Analysis (PCA) is commonly used for exploring population structure in genetic datasets, where it is usually computed from SNP genotyped data. In the context of ARGs, it is also possible to perform branch PCA as implemented in `tskit`. This does not use variant data. (Of course, it may indirectly rely on variant data if the ARG was inferred from data.)
20
20
21
21
In this tutorial, we demonstrate both approaches. We will apply these to haplotypes and diploid genotype data.
22
22
23
23
The documentation of `tskit.TreeSequence.pca` can be found [here](https://tskit.dev/tskit/docs/stable/python-api.html#tskit.TreeSequence.pca).
24
24
25
+
+++
25
26
26
27
:::{note}
27
28
Usually, PCA is carried out on a diploid genotype matrix (individuals in rows, loci in columns) with values 0, 1, and 2. PCA can then be achieved through singular value decomposition (SVD) of the column-centred genotype matrix. This results in a matrix of principal component (PC) scores, which are linear combinations of the genotype columns. The PC scores are ordered, decreasingly, by the amount of variation from the original data they account for.
28
29
:::
29
30
31
+
+++
30
32
31
33
First, we'll simulate an ARG with population structure:
To demstrate that branch PCA works without variant data, we run it on the ARG without mutations, `ts`.
123
125
124
-
125
126
```{code-cell} ipython3
126
127
# haplotypes, each sample haplotype is ues by default
127
128
hapBranchPca=ts.pca(num_components=10)
@@ -167,6 +168,7 @@ plt.show()
167
168
168
169
The plots on the left show one dot per haplotype. These have twice as many dots as the plots on the right, which show individuals. The colours indicate from which of the five islands a haplotype or individual was sampled. As expected with low geneflow, there is some grouping by island. Feel free to re-run with higher or lower values of `migRate` to see how the separations between the island samples changes.
169
170
171
+
+++
170
172
171
173
## Comparing variance components between branch and SNP PCA
172
174
Both `numpy.linalg.svd` and `tskit.TreeSequence.pca` return information about the amount of variation accounted for by each PC. These information are stored in the slots `S` (standard variation for SVD) and `eigenvalues` (variance for branch PCA). To make the two match, we need to multiply the eigenvalues by the mutation rate before taking the square root.
@@ -183,7 +185,6 @@ We now fit a least-squares regression model to demonstrate the match between SVD
$r^2$ is close to 1. Let us visualise this. Each dot below shows a standard deviation value associate with one PC. The fact that they are well correlated suggests that both SNP and branch PCA yielded very similar results.
@@ -234,151 +235,7 @@ pctime[7].factors[:20,:10]
234
235
Here, we demonstrated using simulated data how SNPs and ARG branches lead to equivalent PCA results. For empirical data, the ancestral states of variant sites are not known a priori, which will in practice often lead to polarisation differences. That may affect the outcome of PCA.
To compute a SNP PCA, we start by extracting the haploid 'genotypes' from the ARG. We then make use of the `TreeSequence` object's `individuals_nodes` property (an array) to select each individual's two haplotypes and to add them to create individual diploid genotypes.
The plots on the left show one dot per haplotype. These have twice as many dots as the plots on the right, which show individuals. The colours indicate from which of the five islands a haplotype or individual was sampled. As expected with low geneflow, there is some grouping by island. Feel free to re-run with higher or lower values of `migRate` to see how the separations between the island samples changes.
317
-
318
-
319
-
## Comparing variance components between branch and SNP PCA
320
-
Both `numpy.linalg.svd` and `tskit.TreeSequence.pca` return information about the amount of variation accounted for by each PC. These information are stored in the slots `S` (standard variation for SVD) and `eigenvalues` (variance for branch PCA). To make the two match, we need to multiply the eigenvalues by the mutation rate before taking the square root.
321
-
322
-
```python
323
-
# square root of (branch eigenvalues multiplied by the mutation rate)
324
-
xx=np.sqrt(hapBranchPca.eigenvalues * mu)
325
-
# SVD S values
326
-
yy=htSvd.S[:10]
327
-
```
328
-
329
-
We now fit a least-squares regression model to demonstrate the match between SVD standard variation and transformed eigenvalues.
$r^2$ is close to 1. Let us visualise this. Each dot below shows a standard deviation value associate with one PC. The fact that they are well correlated suggests that both SNP and branch PCA yielded very similar results.
plt.xlabel(r"Branches: $\sqrt{eigenvals *\mu}$") # use raw string to avoid error message about \s
345
-
plt.ylabel("SNPs: $S$")
346
-
plt.title("Variance components of SNP and branch PCA")
347
-
plt.grid()
348
-
plt.show()
349
-
```
350
-
351
-
## Time windows
352
-
Above we showed how variant and branch-based PCA are equivalent. But the ARG is a much richer data type than the genotype matrix. ARGs contain information about the historic relationships between the samples (possibly blurred by a inference step). Branch PCA allows one to specify a time window over which the PCA is to be computed, something that cannot be done for SNP PCA. Next, we compute PCA in time slices with breaks 0, 10, 100, 1000, 10,000, 100,000, 100,0000, 1,000,000, and 10,000,000. The results are stored in a list.
353
-
354
-
```python
355
-
pctime=[tsm.pca(num_components=10, time_windows=[10**i, 10**(i+1)]) for i inrange(8)]
356
-
```
357
-
358
-
Being of class `PCAResult`, the elements of the list have a `factors` property. This has a shape of (100,10). I.e., 10 PCs for 100 haplotypes.
When selecting a very old window, each individual contributes to its own PC, causing most to be plotted at the origin (0,0). We can see this when inspecting the oldest window's PC scores, which are an identityt matrix. All haplotypes below the first two have 0 entries for the first two PC scores (the two left-most columns).
239
+
```{code-cell} ipython3
376
240
377
-
```python
378
-
pctime[7].factors[:20,:10]
379
241
```
380
-
381
-
## Empirical data
382
-
Here, we demonstrated using simulated data how SNPs and ARG branches lead to equivalent PCA results. For empirical data, the ancestral states of variant sites are not known a priori, which will in practice often lead to polarisation differences. That may affect the outcome of PCA.
0 commit comments