<p>In general, the process works as such:</p>

<ol>
<li>Variants are found at a number of loci in each sample (missing loci are often imputed).</li>
<li>Each sample is scored at each locus, often according to the number of copies of the reference allele that it has (so, 0-2).</li>
<li>The resulting matrix is used for PCA.</li>
</ol>

CC BY-SA 3.0
2017-12-17T19:10:56.700Z
@terdon "...according to the number of copies of the reference allele that it has (so, 0-2)."    So the PCA is related to heterozygosity?
5r9n
CC BY-SA 3.0
2017-12-17T19:13:04.500Z
You said each locus was scored often based on number of copies- what else could it be scored on?
5r9n
CC BY-SA 3.0
2017-12-17T20:24:29.383Z
So you build a matrix (often) based on allele copy (0, 1, or 2) then make a covariance matrix and do associated eigendecomposition stuff to generate a PCA?
5r9n
CC BY-SA 3.0
2017-12-17T20:31:20.843Z
One could consider making a numeric score for the genotype, such that each non-reference genotype got a different value (instead of them being all lumped into 0/1). I've not seen someone try that, so I don't know whether it's useful. Usually PCA is done via SVD these days, but really however you want to do it should lead to similarish results.
Devon Ryan
CC BY-SA 3.0
2017-12-16T21:01:02.127Z
2017-12-17T14:55:48.450Z
2017-12-17T14:55:48.450Z
Devon Ryan
<p>I'm curious how a PCA for population structure is built from an alignment/BAM or SAM file. How do programs like <a href="http://www.popgen.dk/angsd/index.php/ANGSD" rel="nofollow noreferrer">angsd</a> and <a href="https://github.com/mfumagalli/ngsPopGen" rel="nofollow noreferrer">ngsCovar</a> actually build a PCA? What exactly makes things come out as different and how can this be determined? If I have 3 alignments that look like this:</p>

<hr>

<p>Locus1:</p>

<pre><code>&gt;Indiv1
ACGT
&gt;Indiv2
ACGC
&gt;Indiv3
ACGA
</code></pre>

<hr>

<p>Locus2:</p>

<pre><code>&gt;Indiv1
GACT
&gt;Indiv2
GACA
&gt;Indiv3
GAGT
</code></pre>

<hr>

<p>Locus3:</p>

<pre><code>&gt;Indiv1
GGTC
&gt;Indiv2
GGCT
&gt;Indiv3
GCTA
</code></pre>

<hr>

<p>(These example alignments leave out quality scores for simplicity, assume that each has high quality across the board.)</p>

<p>What algorithm or system would be used to graph them on a PCA? How many dimensions would that PCA have? Is it possible to model this by hand so that I can understand what's going on/how it is built?</p>

CC BY-SA 3.0
2017-12-16T20:05:37.403Z
Can you update your post to somewhere mention that this is related to population structure? There are other uses of PCA using BAM files that use completely unrelated methods (e.g., deepTools).
Devon Ryan
<sam><phylogenetics><algorithms>
How is a principal component analysis for population structure made from a bam or other alignment file?
# How is a principal component analysis for population structure made from a bam or other alignment file?

(Asked by: username_0 on 2017-12-16)

I'm curious how a PCA for population structure is built from an alignment/BAM or SAM file. How do programs like angsd and ngsCovar actually build a PCA? What exactly makes things come out as different and how can this be determined? If I have 3 alignments that look like this:

Locus1:
>Indiv1
ACGT
>Indiv2
ACGC
>Indiv3
ACGA


Locus2:
>Indiv1
GACT
>Indiv2
GACA
>Indiv3
GAGT


Locus3:
>Indiv1
GGTC
>Indiv2
GGCT
>Indiv3
GCTA


(These example alignments leave out quality scores for simplicity, assume that each has high quality across the board.)
What algorithm or system would be used to graph them on a PCA? How many dimensions would that PCA have? Is it possible to model this by hand so that I can understand what's going on/how it is built?

**Tags:** sam, phylogenetics, algorithms
> **Comments:**
> **username_1**: Can you update your post to somewhere mention that this is related to population structure? There are other uses of PCA using BAM files that use completely unrelated methods (e.g., deepTools).  

---

## 1 answers

### Answer by username_1 on 2017-12-16. Score: 4 | Accepted answer

In general, the process works as such:

1. Variants are found at a number of loci in each sample (missing loci are often imputed).
2. Each sample is scored at each locus, often according to the number of copies of the reference allele that it has (so, 0-2).
3. The resulting matrix is used for PCA.

> **Comments:**
> **username_0**: @terdon "...according to the number of copies of the reference allele that it has (so, 0-2)."    So the PCA is related to heterozygosity?  
> **username_0**: You said each locus was scored often based on number of copies- what else could it be scored on?  
> **username_0**: So you build a matrix (often) based on allele copy (0, 1, or 2) then make a covariance matrix and do associated eigendecomposition stuff to generate a PCA?  
> **username_1**: One could consider making a numeric score for the genotype, such that each non-reference genotype got a different value (instead of them being all lumped into 0/1). I've not seen someone try that, so I don't know whether it's useful. Usually PCA is done via SVD these days, but really however you want to do it should lead to similarish results.  

---

