-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2024-haam-summerschool-presentation.qmd
976 lines (730 loc) · 43.1 KB
/
2024-haam-summerschool-presentation.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
---
title: "Introduction to Population Genetics"
subtitle: "2024 HAAM Summerschool Presentation"
author: Stephan Schiffels
institute: Max Planck Institute for Evolutionary Anthropology
date: 2024-06-19
format:
revealjs:
smaller: true
scrollable: true
---
## Source code
Sourcecode for this presentation and the code for many of the figures can be found at [https://github.com/stschiff/2024-haam-lecture](https://github.com/stschiff/2024-haam-lecture).
# Outline
- Human genetic variation
- Genetic Drift
- FSt and friends
- Visualising population structure through PCA
- Investigating Admixture with F-statistics
# Human Genetic variation
## Demo:[https://popgen.uchicago.edu/ggv](https://popgen.uchicago.edu/ggv)
## Variation in space (Example 1)

## Variation in space (Example 2)

## Variation in space (Example 3)

# Genetic Drift
- Random process of allele frequency change.
- Wright-Fisher model:
- Parent generation of $N$ individuals
- Exactly $N$ individuals as next generation.
- Parents drawn randomly per child
- results in binomial random draws.
## Example
- $N=100$ (haploid individuals)
- two alleles $A$ and $B$
- Generation 1: 50 / 50, so allele freuqency of $B$ is 50%.
- Generation 2: 47 / 53, so allele freuqency of $B$ is 53%.
## Simulation
Implementation in R:
```{R echo=TRUE, `code-line-numbers` = "|1|2|3|4-6|"}
wfsim <- function(n, g, x0) {
res <- numeric(g + 1)
res[1] <- x0
for (i in 2:(g + 1)) {
res[i] <- (rbinom(1, n, res[i - 1])) / n
}
return(res)
}
```
We can test it:
```{R echo=TRUE}
set.seed(1)
wfsim(100, 10, 0.5)
```
---
```{R echo=TRUE}
time_series <- wfsim(100, 100, 0.5)
plot(time_series, type = "l", ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
```
## Population size dependency
::: {.panel-tabset}
### N = 100
```{R}
gens <- 100
sims100 <- replicate(50, wfsim(100, gens, 0.5))
matplot(sims100, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
```
### N = 1000
```{R}
gens <- 100
sims1000 <- replicate(50, wfsim(1000, gens, 0.5))
matplot(sims1000, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
```
### N = 10000
```{R}
gens <- 100
sims10000 <- replicate(50, wfsim(10000, gens, 0.5))
matplot(sims10000, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
```
:::
## Population size dependency
::: {.panel-tabset}
### N = 100
```{R}
par(mfrow = c(1, 2))
matplot(sims100, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
plot(apply(sims100, 1, var), type = "l", ylim = c(0, 0.15),
xlab = "generations", ylab = "Variance")
```
### N = 1000
```{R}
par(mfrow = c(1, 2))
matplot(sims1000, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
plot(apply(sims1000, 1, var), type = "l", ylim = c(0, 0.15),
xlab = "generations", ylab = "Variance")
```
### N = 10000
```{R}
par(mfrow = c(1, 2))
matplot(sims10000, type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
plot(apply(sims10000, 1, var), type = "l", ylim = c(0, 0.15),
xlab = "generations", ylab = "Variance")
```
:::
## Fixation
```{R}
gens <- 1000
sim_long <- replicate(100, wfsim(100, gens, 0.5))
par(mfrow = c(1, 2))
matplot(sim_long[,1:20], type = "l", lty = 1, col = "black",
ylim = c(0, 1),
xlab = "generation", ylab = "allele frequency")
plot(apply(sim_long, 1, var), type = "l", xlab = "generations", ylab = "Variance")
```
# The fixation index $F_\text{ST}$
## Formal definition of $F_\text{ST}$
Conditional probability of allele frequency $p_i$ in population $i$, given an ancestral allele frequency $p_\text{anc}$:
$$E(p_i|p_\text{anc}) = p_\text{anc}$$
and variance $$Var(p_i|p_\text{anc}) = F_\text{ST}^i p_\text{anc}(1-p_\text{anc}).$$
---
Intuition for $Var(p_i|p_\text{anc}) = F_\text{ST}^i p_\text{anc}(1-p_\text{anc}):$
- For $F_\text{ST}^i=0$: $p_i = p_\text{anc}$, no variance
- For $F_\text{ST}^i=1$: Variance is $p_\text{anc}(1-p_\text{anc})$
```{R}
gens <- 1000
sims_x05 <- replicate(1000, wfsim(100, gens, 0.5))
sims_x03 <- replicate(1000, wfsim(100, gens, 0.3))
sims_x02 <- replicate(1000, wfsim(100, gens, 0.2))
plot_dat <- cbind(
apply(sims_x05, 1, var),
apply(sims_x03, 1, var),
apply(sims_x02, 1, var)
)
par(mfrow = c(1, 1))
cols <- c("blue", "red", "green")
matplot(plot_dat, type = "l", ylim = c(0, 0.25), lty = 1,
xlab = "generations", ylab = "Variance", col = cols)
legend(x = "bottomright",
legend = c("p_anc = 0.5", "p_anc = 0.3", "p_anc = 0.2", "FST = 1"),
lty = c(1, 1, 1, 2), col = c(cols, "black"))
theory_values <- sapply(c(0.5, 0.3, 0.2), function(x) x * (1 - x))
abline(h = theory_values, lty = 2, col = cols)
```
## Estimating $F_\text{ST}$ from genomic data
From Theory:
$$F_\text{ST}(A,B) = \frac{F_\text{ST}^A+F_\text{ST}^B}{2}$$
Hudson-Estimator:
$$F_\text{ST}=1-\frac{H_w}{H_b}$$
- $H_w$ is heterozygosity within each population
- $H_b$ is heterozygosity between two populations
Intuition for $F_\text{ST}=1-\frac{H_w}{H_b}$:
- $F_\text{ST}=0$ if and only if $H_w=H_b$, (A and B are same population)
- $F_\text{ST}=1$ if and only if $H_w=0$, all variants fully fixed in both populations.
## Estimation from allele frequencies
$$F_\text{ST}(A,B)=\frac{\left\langle(a-b)^2\right\rangle}{\left\langle a(1-b)+b(1-a)\right\rangle}$$
- $a$ and $b$ are _population allele frequencies_ (in principle unobserved), but can be approximated by _sample allele frequencies_.
- Bias-corrected version:
$$F'_\text{ST}(A,B) = \frac{\left\langle\left(a-b\right)^2-\frac{a(1-a)}{n_a-1} - \frac{b(1-a}{n_b-1}\right\rangle}{\left\langle a(1-b) + b(1-a)\right\rangle}$$
## Relation to $F_2$ Statistics
$F_\text{ST}(A,B)$ is closely related to F2-statistics, introduced in [@Patterson2012]:
$$F_2(A,B)=(a-b)^2$$
- Both statistics range technically from 0 to 1, but differently scaled!
- $F_2:$
- Theoretical value of $F_2=1$: Both populations are fixed at different alleles in all studied SNPs (impossible).
- Timescale for $F_2=1$: Mutation-time-scale $1/\mu$ (hundreds of millions of generations)
- $F_\text{ST}$:
- Theoretical value of $F_\text{ST}=1$: Both populations are fixed at all alleles, but not necessarily at different ones.
- Timescale for $F_\text{ST}=1$: Drift time-scale $1/N$ (tens of thousands of generations)
## Computing FST using xerxes
Poseidon Software xerxes [](https://www.poseidon-adna.org)
We here chose a number of populations from [@Patterson2012] with more than 10 samples per population, and prepare the following config file for xerxes:
```Yaml
fstats:
- type: FST
a: ["Adygei", "Balochi", "Basque", "BedouinA", "BedouinB", "Biaka", "Brahui", "Burusho", "Druze", "French", "Han", "Hazara", "Italian_North", "Japanese", "Kalash", "Karitiana", "Makrani", "Mandenka", "Mayan", "Mozabite", "Orcadian", "Palestinian", "Papuan", "Pathan", "Pima", "Russian", "Sardinian", "Sindhi_Pakistan", "Yakut", "Yoruba"]
b: ["Adygei", "Balochi", "Basque", "BedouinA", "BedouinB", "Biaka", "Brahui", "Burusho", "Druze", "French", "Han", "Hazara", "Italian_North", "Japanese", "Kalash", "Karitiana", "Makrani", "Mandenka", "Mayan", "Mozabite", "Orcadian", "Palestinian", "Papuan", "Pathan", "Pima", "Russian", "Sardinian", "Sindhi_Pakistan", "Yakut", "Yoruba"]
- type: F2
a: ["Adygei", "Balochi", "Basque", "BedouinA", "BedouinB", "Biaka", "Brahui", "Burusho", "Druze", "French", "Han", "Hazara", "Italian_North", "Japanese", "Kalash", "Karitiana", "Makrani", "Mandenka", "Mayan", "Mozabite", "Orcadian", "Palestinian", "Papuan", "Pathan", "Pima", "Russian", "Sardinian", "Sindhi_Pakistan", "Yakut", "Yoruba"]
b: ["Adygei", "Balochi", "Basque", "BedouinA", "BedouinB", "Biaka", "Brahui", "Burusho", "Druze", "French", "Han", "Hazara", "Italian_North", "Japanese", "Kalash", "Karitiana", "Makrani", "Mandenka", "Mayan", "Mozabite", "Orcadian", "Palestinian", "Papuan", "Pathan", "Pima", "Russian", "Sardinian", "Sindhi_Pakistan", "Yakut", "Yoruba"]
```
This will then produce all combinations of $FST(A, B)$ and $F_2(A, B)$ as indicated in the population lists.
We run this config file using the command line
```bash
REPO=/path/to/community-archive/2012_PattersonGenetics
xerxes fstats -d $REPO --statConfig fstat_world_config.yaml -f fstat_world_output.tsv > fstat_world_table.txt
```
## Xerxes output
- The standard output, is a nicely layouted ASCII Table, which looks like this (skipping thousands of lines)
- The `-f` flag outputs a tab-separated file, here named `fstat_world_output.tsv`, which is easier to read into R.
```text
.-----------.-----------------.-----------------.---.---.---------.----------------.--------------------.------------------.--------------------.
| Statistic | a | b | c | d | NrSites | Estimate_Total | Estimate_Jackknife | StdErr_Jackknife | Z_score_Jackknife |
:===========:=================:=================:===:===:=========:================:====================:==================:====================:
| FST | Adygei | Adygei | | | 593124 | 0.0000 | 0.0000 | 0.0000 | NaN |
| FST | Adygei | Balochi | | | 593124 | 1.2789e-2 | 1.2789e-2 | 3.3572e-4 | 38.09517110646904 |
| FST | Adygei | Basque | | | 593124 | 1.8790e-2 | 1.8790e-2 | 4.0141e-4 | 46.810358341103225 |
| FST | Adygei | BedouinA | | | 593124 | 1.3017e-2 | 1.3017e-2 | 2.9647e-4 | 43.90737238689979 |
| FST | Adygei | BedouinB | | | 593124 | 3.3455e-2 | 3.3454e-2 | 5.7648e-4 | 58.03217592610529 |
| FST | Adygei | Biaka | | | 593124 | 0.1716 | 0.1716 | 1.2185e-3 | 140.85275693678508 |
| FST | Adygei | Brahui | | | 593124 | 1.4644e-2 | 1.4644e-2 | 3.4481e-4 | 42.46989237781921 |
| FST | Adygei | Burusho | | | 593124 | 1.8566e-2 | 1.8566e-2 | 3.8156e-4 | 48.6573908240317 |
| FST | Adygei | Druze | | | 593124 | 1.2173e-2 | 1.2173e-2 | 2.6659e-4 | 45.65975464203526 |
| FST | Adygei | French | | | 593124 | 9.7730e-3 | 9.7730e-3 | 3.1627e-4 | 30.9006924987833 |
| FST | Adygei | Han | | | 593124 | 9.8759e-2 | 9.8759e-2 | 1.1973e-3 | 82.48660429503893 |
| FST | Adygei | Hazara | | | 593124 | 3.0725e-2 | 3.0726e-2 | 7.1478e-4 | 42.98629834431124 |
| FST | Adygei | Italian_North | | | 593124 | 8.6600e-3 | 8.6601e-3 | 2.7883e-4 | 31.058813893781032 |
...
| F2 | Yoruba | Kalash | | | 593124 | 4.5530e-2 | 4.5530e-2 | 3.1580e-4 | 144.17623831657042 |
| F2 | Yoruba | Karitiana | | | 593124 | 8.0274e-2 | 8.0274e-2 | 5.0430e-4 | 159.18045338908084 |
| F2 | Yoruba | Makrani | | | 593124 | 3.7091e-2 | 3.7091e-2 | 2.9054e-4 | 127.66205740966917 |
| F2 | Yoruba | Mandenka | | | 593124 | 2.2053e-3 | 2.2053e-3 | 5.0215e-5 | 43.91726459507651 |
| F2 | Yoruba | Mayan | | | 593124 | 5.9758e-2 | 5.9758e-2 | 4.8204e-4 | 123.968969339996 |
| F2 | Yoruba | Mozabite | | | 593124 | 2.8717e-2 | 2.8717e-2 | 2.9425e-4 | 97.59603068441464 |
| F2 | Yoruba | Orcadian | | | 593124 | 4.3925e-2 | 4.3925e-2 | 3.0915e-4 | 142.08521834047312 |
| F2 | Yoruba | Palestinian | | | 593124 | 3.5635e-2 | 3.5635e-2 | 2.6730e-4 | 133.31454891682029 |
| F2 | Yoruba | Papuan | | | 593124 | 6.8316e-2 | 6.8316e-2 | 6.1762e-4 | 110.6122879514611 |
| F2 | Yoruba | Pathan | | | 593124 | 3.8770e-2 | 3.8770e-2 | 2.9069e-4 | 133.3741386458573 |
| F2 | Yoruba | Pima | | | 593124 | 6.9994e-2 | 6.9994e-2 | 5.4248e-4 | 129.02469385208832 |
| F2 | Yoruba | Russian | | | 593124 | 4.2705e-2 | 4.2705e-2 | 3.1362e-4 | 136.16918827517011 |
| F2 | Yoruba | Sardinian | | | 593124 | 4.4227e-2 | 4.4227e-2 | 3.1663e-4 | 139.68078608155477 |
| F2 | Yoruba | Sindhi_Pakistan | | | 593124 | 3.8543e-2 | 3.8543e-2 | 2.8137e-4 | 136.9825670475205 |
| F2 | Yoruba | Yakut | | | 593124 | 5.0727e-2 | 5.0727e-2 | 3.9113e-4 | 129.69267912526192 |
| F2 | Yoruba | Yoruba | | | 593124 | 0.0000 | 0.0000 | 0.0000 | NaN |
'-----------'-----------------'-----------------'---'---'---------'----------------'--------------------'------------------'--------------------'
```
## Plotting results in R
We first load the data
```{r echo=TRUE}
dat <- subset(read.table("fst_working/fstat_world_output.tsv", sep="\t", header = TRUE),
select=-c(c, d, Z_score_Jackknife))
datFST <- dat[dat$Statistic == "FST",]
datF2 <- dat[dat$Statistic == "F2",]
head(datFST[,1:6])
```
Let's check out the largest values
```{r echo=TRUE}
head(dat[order(-dat$Estimate_Total),1:6])
```
## Histograms
::: {.panel-tabset}
### FST
```{r echo = TRUE}
avg <- mean(datFST$Estimate_Total)
hist(datFST$Estimate_Total, xlab = "FST", ylab = "Nr of pairs", main = paste0("mean=", round(avg, 2)))
```
### F2
```{r echo = TRUE}
avg <- mean(datF2$Estimate_Total)
hist(datF2$Estimate_Total, xlab = "F2", ylab = "Nr of pairs", main = paste0("mean=", round(avg, 2)))
```
:::
## Heatmaps
```{r}
fstMat <- xtabs(Estimate_Total ~ a + b, datFST)
f2Mat <- xtabs(Estimate_Total ~ a + b, datF2)
```
::: {.panel-tabset}
### FST
```{r}
#| fig-height: 7
#| fig-width: 7
heatmap(fstMat, symm = TRUE, hclustfun = function(m) hclust(m, method="ward.D2"))
```
### F2
```{r}
#| fig-height: 7
#| fig-width: 7
heatmap(f2Mat, symm = TRUE, hclustfun = function(m) hclust(m, method="ward.D2"))
```
:::
## Dendrograms
::: {.panel-tabset}
### FST
```{r}
fstDist <- as.dist(fstMat)
dendro <- hclust(fstDist, method="ward.D2")
plot(dendro, hang = -1, ylab = "FST", xlab = "", main = "")
```
### F2
```{r}
f2Dist <- as.dist(f2Mat)
dendro <- hclust(f2Dist, method="ward.D2")
plot(dendro, hang = -1, ylab = "F2", xlab = "", main = "")
```
:::
- FST affected by total drift (inversely proportional to population size)
- F2 more closely reflecting evolutionary time
# Visualising population structure through PCA
## Principal Components Analyses
Genotype matrix
```text
Test Indivdual 1 (Selected SNPs) : T G A C C G G . . .
Test Indivdual 2 (Selected SNPs) : T A A C C G G . . .
Test Indivdual 3 (Selected SNPs) : C A A C C G G . . .
Test Indivdual 4 (Selected SNPs) : T A A C C G G . . .
Test Indivdual 5 (Selected SNPs) : C G G C C G G . . .
Test Indivdual 6 (Selected SNPs) : C G G C C G G . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
```
How can we visualise hundreds of individuals with a million SNPs?
## Principal Components Analyses
- Basic problem in multivariate statistics
- highdimensional data → two-dimensional visualisation, dimension reduction
- Idea: Order entities in a two-dimensional room such that the distance between them reflects the true high-dimensional distances most accurately
- Distances can be computed straight-forward, even for many dimensions
- Example: Pixel-wise distances in flags:

## PCA Example: Flags
:::: {.columns}
::: {.column width="50%"}

:::
::: {.column width="50%"}
- pixel-wise distances
- similar flags are placed nearby.
:::
::::
::: aside
Data pulled and processed in Wolfram Mathematica
:::
## PCA Example: EU Flags
{width="50%"}
::: aside
Data pulled and processed in Wolfram Mathematica
:::
## PCA Example: Greek Letters
:::: {.columns}
::: {.column width="50%"}

:::
::: {.column width="50%"}
- letters rasterised
- again pixel-wise distances
- layout corresponds to similarities
:::
::::
::: aside
Data pulled and processed in Wolfram Mathematica
:::
## PCA Example: Van Gogh Paintings
{width="50%"}
::: aside
Data pulled and processed in Wolfram Mathematica
:::
## PCA and Genetics (World)
:::: {.columns}
::: {.column width="40%"}

:::
::: {.column width="40%"}
- First Component separates groups from African and those outside of Africa
- second component separates Asia and America from Europe
:::
::::
::: aside
Data from Patterson, Nick, Priya Moorjani, Yontao Luo, Swapan Mallick, Nadin Rohland, Yiping Zhan, Teri Genschoreck, Teresa Webster, and David Reich. 2012. “Ancient Admixture in Human History.” Genetics 192 (3): 1065–93.
:::
## PCA and Genetics (Europe)
{width="60%"}
::: aside
Novembre, John, Toby Johnson, Katarzyna Bryc, Zoltán Kutalik, Adam R. Boyko, Adam Auton, Amit Indap, et al. 2008. “Genes Mirror Geography within Europe.” Nature 456 (7218): 98–101.
:::
## Genetics PCA through space and time
:::: {.columns}
::: {.column width="50%"}

:::
::: {.column width="50%"}

:::
::::
::: aside
Created by me, using data from Patterson, Nick, Priya Moorjani, Yontao Luo, Swapan Mallick, Nadin Rohland, Yiping Zhan, Teri Genschoreck, Teresa Webster, and David Reich. 2012. “Ancient Admixture in Human History.” Genetics 192 (3): 1065–93.
:::
## Genetics PCA through space and time
:::: {.columns}
::: {.column width="60%"}

:::
::: {.column width="40%"}

:::
::::
::: aside
Schmid, Clemens, and Stephan Schiffels. 2023. “Estimating Human Mobility in Holocene Western Eurasia with Large-Scale Ancient Genomic Data.” Proceedings of the National Academy of Sciences of the United States of America 120 (9): e2218375120.
:::
# Investigating Admixture with F-statistics
## Admixture - F3 Statistics
- F3 statistics are a useful to understand population relationships.
- Test whether a target population (C) is admixed between two source populations (A and B)
$$F3(A,B;C)=\langle(c−a)(c−b)\rangle$$
- If $F3(A, B; C)$ is negative, proof that population C is admixed between populations A and B:
<img src="images/fstats/f3_phylogeny.png" alt="F3-phylogeny" style="width: 300px;"/>
## Intermediate allele frequencies
- F3 statistics becomes negative if allele frequency of target population C is on average intermediate between the allele frequencies of A and B.
- Extreme example: $a=0$, $b=1$ and $c=0.5$. Then: $(c−a)(c−b)=−0.25$, negative.
- If entire statistics is negative: many positions like this
- Proofs admixture between the two sources.
| SNP | A | B | C | $(c-a)(c-b)$ |
|---------------|-----|-----|-----|--------------|
| 1 | 1 | 0 | 0.5 | -0.25 |
| 2 | 0.8 | 0 | 0 | 0 |
| 3 | 0 | 0.7 | 0 | 0 |
| 4 | 0.1 | 0.5 | 0.3 | -0.04 |
| 5 | 0 | 0.1 | 0.2 | 0.02 |
| 6 | 1 | 0.2 | 0.9 | -0.07 |
| $F_3(A, B;C)$ | | | | **-0.057** |
:::{.callout-caution}
If an F3 statistics is *not* negative, it does *not* proof that there is no admixture!
:::
## Computing Admixture-F3 with xerxes
- We test if Finnish are admixed between East and West
- For West, we use French, Icelandic, Lithuanian and Norwegian as source
- For East: Nganasan and one of the ancient individuals analysed in [@Lamnidis2018], from the Russian site of *Bolshoy Oleni Ostrov* (3500 years before present).
### Getting the data
If you happen to have downloaded a copy of the [Poseidon Community Archive](https://www.poseidon-adna.org/#/archive_overview?id=public-poseidon-archives) already, then just use the path to that archive in the following commands. Otherwise you download the entire archive via
```bash
trident fetch -d /somewhere/to/store/the/archive --downloadAll
```
or just the relevant packages for the examples in this chapter:
```bash
trident fetch -d /somewhere/to/store/the/archive -f "*2012_PattersonGenetics*,*2014_RaghavanNature*,*2014_LazaridisNature*,*2018_Lamnidis_Fennoscandia*"
```
## Running fstats
We use the software `xerxes fstats` from the [Poseidon Framework](https://www.poseidon-adna.org/#/xerxes?id=xerxes-cli-software). Here is a command line that computes 8 statistics for us:
```bash
xerxes fstats -d ~/dev/poseidon-framework/community-archive \
--stat "F3(Nganasan,French,Finnish)" \
--stat "F3(Nganasan, Icelandic, Finnish)" \
--stat "F3(Nganasan, Lithuanian, Finnish)" \
--stat "F3(Nganasan, Norwegian, Finnish)" \
--stat "F3(Russia_Bolshoy, French, Finnish)" \
--stat "F3(Russia_Bolshoy, Icelandic, Finnish)" \
--stat "F3(Russia_Bolshoy, Lithuanian, Finnish)" \
--stat "F3(Russia_Bolshoy, Norwegian, Finnish)" \
```
Note that `xerxes fstats` will automatically find the right packages from your local archive that contain these groups. You can see in the output of the program which packages contribute:
```text
[Info] 5 relevant packages for chosen statistics identified:
[Info] *2012_PattersonGenetics-2.1.3*
[Info] *2014_LazaridisNature-4.0.2*
[Info] *2016_LazaridisNature-2.1.3*
[Info] *2018_Lamnidis_Fennoscandia-2.1.0*
[Info] *2019_Flegontov_PalaeoEskimo-2.2.1*
```
So these five packages contain the samples requested in these statistics. You can inquire about this also more manually using [`trident list`](https://www.poseidon-adna.org/#/trident?id=list-command).
---
Here is the result that you should get, nicely layouted in a Text-table:
```text
.-----------.----------------.------------.---------.---.---------.----------------.--------------------.------------------.---------------------.
| Statistic | a | b | c | d | NrSites | Estimate_Total | Estimate_Jackknife | StdErr_Jackknife | Z_score_Jackknife |
:===========:================:============:=========:===:=========:================:====================:==================:=====================:
| F3 | Nganasan | French | Finnish | | 593124 | -1.0450e-3 | -1.0451e-3 | 1.2669e-4 | -8.249133659451905 |
| F3 | Nganasan | Icelandic | Finnish | | 593124 | -1.1920e-3 | -1.1920e-3 | 1.3381e-4 | -8.908381869946188 |
| F3 | Nganasan | Lithuanian | Finnish | | 593124 | -1.1605e-3 | -1.1605e-3 | 1.5540e-4 | -7.4680182465607245 |
| F3 | Nganasan | Norwegian | Finnish | | 593124 | -1.0913e-3 | -1.0914e-3 | 1.3921e-4 | -7.83945981272796 |
| F3 | Russia_Bolshoy | French | Finnish | | 542789 | -6.1807e-4 | -6.1809e-4 | 1.0200e-4 | -6.059872900228102 |
| F3 | Russia_Bolshoy | Icelandic | Finnish | | 542789 | -6.2801e-4 | -6.2802e-4 | 1.1792e-4 | -5.325695961373772 |
| F3 | Russia_Bolshoy | Lithuanian | Finnish | | 542789 | -3.7310e-4 | -3.7310e-4 | 1.2973e-4 | -2.8760029685791637 |
| F3 | Russia_Bolshoy | Norwegian | Finnish | | 542789 | -3.7646e-4 | -3.7653e-4 | 1.1630e-4 | -3.2375830440434323 |
'-----------'----------------'------------'---------'---'---------'----------------'--------------------'------------------'---------------------'
```
:::{.callout-tip}
Use the option `-f <FILE>` to output the results additionally to a tab-separated file, or `--raw` if you prefer the standard output to be tab-separated
:::
- All cases of this statistic are negative (`Estimate_Total`).
- Columns `Estimate_Jackknife` and `StdErr_Jackknife` are computed using Jackknifing.
- Z score is key: It gives the deviation of the f3 statistic from zero in units of the standard error.
- As general rule, a Z score of -3 or more suggests a significant rejection of the Null hypothesis that the statistic is not negative.
- Conclusion: Finnish have ancestral admixture of East and West Eurasian ancestry.
## Running xerxes via a configuration file
- Problem: command line gets long, requiring a separate `--stat` option for every statistic.
- More powerful interface to xerxes: configuration files:
```Yaml
fstats:
- type: F3
a: ["Nganasan", "Russia_Bolshoy"]
b: ["French", "Icelandic", "Lithuanian", "Norwegian"]
c: ["Finnish"]
```
You can then run xerxes as
```bash
xerxes fstats -d ~/dev/poseidon-framework/community-archive --statConfig fstats_working/F3_finnish.config
```
- `xerxes` automatically runs all combinations of `a`, `b` and `c`.
:::{.callout-note}
Note that there are actually three types of F3-statistics supported by xerxes:
* `F3vanilla`: The purest form, defined literally as $\langle(c−a)(c−b)\rangle$
* `F3`: A bias-corrected version, which is only valid for groups in C that have non-zero heterozygosity
* `F3star`: This one is normalised by the heterozgygosity of the third population, C, as suggested in [@Patterson2012] and implemented in the [Admixtools package](https://github.com/DReichLab/AdmixTools).
The [white-paper]((https://github.com/poseidon-framework/poseidon-analysis-hs/blob/main/docs/xerxes_whitepaper.pdf)) explains this in detail.
:::
## More about configuration files
The configuration file format has a lot more options. Here is a bit more complex example, but see also [the documentation](https://www.poseidon-adna.org/#/xerxes?id=input-via-a-configuraton-file):
```Yaml
# You can define groups right within the configuration file.
# here we use negative selection to remove individuals from the
# newly defined groups
groupDefs:
CEU2: ["CEU.SG", "-<NA12889.SG>", "-<NA12890.SG>"]
FIN2: ["FIN.SG", "-<HG00383.SG>", "-<HG00384.SG>"]
GBR2: ["GBR.SG", "-<HG01791.SG>", "-<HG02215.SG>"]
IBS2: ["IBS.SG", "-<HG02238.SG>", "-<HG02239.SG>"]
fstats:
- type: F2 # this will create 2x2 = 4 F2-Statistics
a: ["French", "Spanish"]
b: ["Han", "CEU2"]
- type: F3vanilla # This will create 3x2x1 = 6 Statistics
a: ["French", "Spanish", "Mbuti"]
b: ["Han", "CEU2"]
c: ["<Chimp.REF>"]
- type: F4 # This will create 5x5x4x1 = 100 Statistics
a: ["<I0156.SG>", "<I0157.SG>", "<I0159.SG>", "<I0160.SG>", "<I0161.SG>"]
b: ["<I0156.SG>", "<I0157.SG>", "<I0159.SG>", "<I0160.SG>", "<I0161.SG>"]
c: ["CEU2", "FIN2", "GBR2", "IBS2"]
d: ["<Chimp.REF>"]
# Altogether: 110 statistics of different types
```
which will not just create multiple statistic using row-combinations, as described, but also uses newly defined groups and combines multiple statistic types (F2, F3 and F4) in one run.
## F4 Statistics
- a different way to test for admixture
- “F4 statistics” and “D statistics” are very similar ^[Patterson, Nick, Priya Moorjani, Yontao Luo, Swapan Mallick, Nadin Rohland, Yiping Zhan, Teri Genschoreck, Teresa Webster, and David Reich. 2012. “Ancient Admixture in Human History.” Genetics 192 (3): 1065–93.]
- Also defined as correlations of allele frequency differences, similarly to F3 statistics
- involve four populations, not three.
$$F4(A,B;C,D)=\langle(a−b)(c−d)\rangle.$$
:::: {.columns}
::: {.column width="70%"}
- Allele frequency difference between A and B independent from that between C and D.
- F4(A, B; C, D) should then not be statistically different from zero
- With gene flow from C or D into A or B, $F_4$ positive or negative.
- If significantly negative -> gene flow between either C and B, or D and A.
- If significantly positive -> gene flow between A and C, or B and D.
:::
::: {.column width="30%"}

:::
::::
## Shaping intuition - the ABBA- and BABA-sites
| SNP | A | B | C | D | $(a-b)(c-d)$ |
|------------------|---|---|---|---|--------------|
| 1 | 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 0 | 1 | 1 | 0 |
| 3 | 0 | 1 | 1 | 0 | -1 |
| 4 | 0 | 1 | 0 | 1 | 1 |
| 5 | 1 | 0 | 0 | 1 | -1 |
| 6 | 1 | 0 | 0 | 0 | 0 |
| $F_4(A, B;C, D)$ | | | | | **-0.0167** |
- The only SNPs that contribute positively are SNPs of type `1010` and `0101`
- The only SNPs that contribute negatively are SNPs of type `1001` and `0110`.
- Inuition: In SNPs polymorphis in both $(A,B)$ and $(C,D)$, is B is genetically more similar to C than it is to D?
- Test for "treeness": If A, B, C, D are on a perfect tree, then C should be equally close to C as to D.
- If B is closer to C than to D, or vice versa, the tree is violated: Closer connection between B and C or B and D, depending on the sign of the statistic.
## From single samples to allele frequencies
- ABBA- and BABA-categories of SNPs useful for intuition with single haploid genomes.
- What about population allele frequencies?
- A nice feature of F4-Statistics: averages over all quadruples of samples: With multiple samples in one or multiple slots A, B, C or D, total F4-statistic of the _groups_ is exactly equal to the _average_ of F4-Statistics of the _individuals_.
Let's say we have 2 individuals in each of A and B: $A=\{A_1,A_2\}$ and $B=\{B_1,B_2\}$.
$$F4(A, B; C, D) = \text{Average of}[F4(A_1, B_1; C, D), F4(A_1, B_2; C, D), F4(A_2, B_1; C, D), F4(A_2, B_2; C, D)]$$
In other words: An F4-Statistic _always_ measures the _average_ excess of pairwise BABA SNPs over ABBA SNPs.
:::{.callout-note}
F4-statistics have been famously used to show that Neanderthals are more closely related to Non-African populations than to Africans, suggesting gene-flow between Neanderthals and Non-Africans (shown in [@Green2010]). You can reproduce this famous result with
```bash
xerxes fstats -d ~/poseidon_repo --stat 'F4(<Chimp.REF>,<Altai_published.DG>,Yoruba,French)' \
--stat 'F4(<Chimp.REF>,<Altai_published.DG>,Sardinian,French)'
```
which shows that the first statistic is significantly positive with a Z-score of 7.99, while the second one is insignificantly different from zero (Z=1.01)
:::
## Running F4-Statistics with xerxes
We test for East Asian ancestry in Finns, similarly to the test using Admixture F3 statistics above. We will again use `xerxes fstats`. Configuration file:
```Yaml
fstats:
- type: F4
a: ["Mbuti"]
b: ["Nganasan", "Russia_Bolshoy"]
c: ["French", "Icelandic", "Lithuanian", "Norwegian"]
d: ["Finnish"]
```
Run via
```bash
xerxes fstats -d ~/dev/poseidon-framework/community-archive --statConfig fstats_working/F4_finnish.config
```
Result:
```text
.-----------.-------.----------------.------------.---------.---------.----------------.--------------------.------------------.--------------------.
| Statistic | a | b | c | d | NrSites | Estimate_Total | Estimate_Jackknife | StdErr_Jackknife | Z_score_Jackknife |
:===========:=======:================:============:=========:=========:================:====================:==================:====================:
| F4 | Mbuti | Nganasan | French | Finnish | 593124 | 2.3114e-3 | 2.3115e-3 | 1.2676e-4 | 18.235604067907143 |
| F4 | Mbuti | Nganasan | Icelandic | Finnish | 593124 | 1.6590e-3 | 1.6590e-3 | 1.4861e-4 | 11.163339072181776 |
| F4 | Mbuti | Nganasan | Lithuanian | Finnish | 593124 | 1.3290e-3 | 1.3290e-3 | 1.4681e-4 | 9.052979707622278 |
| F4 | Mbuti | Nganasan | Norwegian | Finnish | 593124 | 1.6503e-3 | 1.6503e-3 | 1.5358e-4 | 10.745850997260929 |
| F4 | Mbuti | Russia_Bolshoy | French | Finnish | 542789 | 1.8785e-3 | 1.8785e-3 | 1.2646e-4 | 14.854487416366263 |
| F4 | Mbuti | Russia_Bolshoy | Icelandic | Finnish | 542789 | 1.0829e-3 | 1.0828e-3 | 1.4963e-4 | 7.236818881873822 |
| F4 | Mbuti | Russia_Bolshoy | Lithuanian | Finnish | 542789 | 5.4902e-4 | 5.4907e-4 | 1.4601e-4 | 3.7605973064589096 |
| F4 | Mbuti | Russia_Bolshoy | Norwegian | Finnish | 542789 | 9.3473e-4 | 9.3475e-4 | 1.5302e-4 | 6.108881868125652 |
'-----------'-------'----------------'------------'---------'---------'----------------'--------------------'------------------'--------------------'
```
In all cases, the Z score is positive and larger than 3, indicating a significant deviation from zero, and implying gene flow between Nganasan and Finnish, and BolshoyOleniOstrov and Finnish, when compared to French, Icelandic, Lithuanian or Norwegian.
## Outgroup-F3-Statistics
Outgroup F3 statistics are a special case how to use F3 statistics. The definition is the same as for Admixture F3 statistics, but instead of a target C and two source populations A and B, one now gives an outgroup C and two test populations A and B.
To get an intuition for this statistics, consider the following tree:

In this scenario, the statistic F3(A, B; C) measures the branch length from C to the common ancestor of A and B, coloured red. So this statistic is simply a measure of how closely two population A and B are related with each other, as measured from a distant outgroup. It is thus a similarity measure: The higher the statistic, the more genetically similar A and B are to one another.
Here is again a SNP table to illustrate, using haploid individuals:
| SNP | A | B | C | $(c-a)(c-b)$ |
|---------------|---|---|---|--------------|
| 1 | 1 | 0 | 0 | 0 |
| 2 | 1 | 0 | 0 | 0 |
| 3 | 0 | 0 | 1 | 1 |
| 4 | 1 | 0 | 1 | 0 |
| 5 | 1 | 1 | 0 | 1 |
| 6 | 0 | 0 | 1 | 1 |
| $F_3(A, B;C)$ | | | | **0.5** |
You can see that each position which is similar between A and B, but different to C contributes 1, all other SNPs 0. So it directly measures similarity between A and B on alleles that differ from the outgroup C.
:::{.callout-note}
Note that the averaging-relation shown for F4 statistics above is also true for Outgroup-F3 statistics, but only for populations A and B, not for C. So if you have multiple samples in A and B, you may think of this statistic being the _average_ over all pairwise nucleotide similarities between individuals in A and B with respect to the same outgroup C.
:::
## Running Outgroup-F3
We can use this statistic to measure for example the genetic affinity to East Asia, by performing the statistic F3(Han, X; Mbuti), where Mbuti is a distant African population and acts as outgroup here, Han denote Han Chinese, and X denotes various European populations that we want to test.
You can again define a configuration file that performs looping over various populations X for you:
```Yaml
fstats:
- type: F3
a: ["Han"]
b: ["Chuvash", "Albanian", "Armenian", "Bulgarian", "Czech", "Druze", "English",
"Estonian", "Finnish", "French", "Georgian", "Greek", "Hungarian", "Icelandic",
"Italian_North", "Italian_South", "Lithuanian", "Maltese", "Mordovian", "Norwegian",
"Orcadian", "Russian", "Sardinian", "Scottish", "Sicilian", "Spanish_North",
"Spanish", "Ukrainian", "Finland_Levanluhta", "Russia_Bolshoy", "Russia_Chalmny_Varre", "Saami.DG"]
c: ["Mbuti"]
```
which cycles through many populations from Europe, including the ancient individuals from Chalmny Varre, Bolshoy Oleni Ostrov and Levänluhta (described in [@Lamnidis2018]). We store this file in a file called `fstats_working/OutgroupF3_europe.config` and run via:
```bash
xerxes fstats --statConfig fstats_working/OutgroupF3_europe.config -d ~/dev/poseidon-framework/community-archive -f fstats_working/outgroupf3_europe.tsv
```
:::{.callout-warning}
Often in Outgroup-F3-statistics you use single genomes for population C, sometimes even single haploid genomes. In this case, `F3` and `F3star` will get undefined results, because ordinary `F3` and `F3star` statistics require population C to have non-zero average heterozygosity, so you will need at least one diploid sample, or multiple haploid or diploid samples.
Use `F3vanilla` if your third population C is a single pseudo-haploid sample.
:::
## Results (Outgroup F3)
Here is the output of this run (but note that a tab-separated version was also stored in `fstats_working/outgroupf3_europe.tsv` using the option `-f`):
```text
.-----------.-----.----------------------.-------.---.---------.----------------.--------------------.------------------.--------------------.
| Statistic | a | b | c | d | NrSites | Estimate_Total | Estimate_Jackknife | StdErr_Jackknife | Z_score_Jackknife |
:===========:=====:======================:=======:===:=========:================:====================:==================:====================:
| F3 | Han | Chuvash | Mbuti | | 593124 | 5.3967e-2 | 5.3967e-2 | 5.0668e-4 | 106.51180329550319 |
| F3 | Han | Albanian | Mbuti | | 593124 | 4.9972e-2 | 4.9973e-2 | 4.9520e-4 | 100.91326321202445 |
| F3 | Han | Armenian | Mbuti | | 593124 | 4.9531e-2 | 4.9531e-2 | 4.7771e-4 | 103.68366652942314 |
| F3 | Han | Bulgarian | Mbuti | | 593124 | 5.0103e-2 | 5.0103e-2 | 4.8624e-4 | 103.04188532686614 |
| F3 | Han | Czech | Mbuti | | 593124 | 5.0536e-2 | 5.0536e-2 | 4.9261e-4 | 102.58792370749681 |
| F3 | Han | Druze | Mbuti | | 593124 | 4.8564e-2 | 4.8564e-2 | 4.6788e-4 | 103.79674299622445 |
| F3 | Han | English | Mbuti | | 593124 | 5.0280e-2 | 5.0281e-2 | 4.9183e-4 | 102.23198323949656 |
| F3 | Han | Estonian | Mbuti | | 593124 | 5.1154e-2 | 5.1155e-2 | 5.0350e-4 | 101.59882496016485 |
| F3 | Han | Finnish | Mbuti | | 593124 | 5.1784e-2 | 5.1784e-2 | 5.0603e-4 | 102.33488758899031 |
| F3 | Han | French | Mbuti | | 593124 | 5.0207e-2 | 5.0208e-2 | 4.8552e-4 | 103.40976592749682 |
| F3 | Han | Georgian | Mbuti | | 593124 | 4.9711e-2 | 4.9711e-2 | 4.8100e-4 | 103.34881140790415 |
| F3 | Han | Greek | Mbuti | | 593124 | 4.9874e-2 | 4.9874e-2 | 4.8994e-4 | 101.79554640756365 |
| F3 | Han | Hungarian | Mbuti | | 593124 | 5.0497e-2 | 5.0498e-2 | 4.9878e-4 | 101.24215699276706 |
| F3 | Han | Icelandic | Mbuti | | 593124 | 5.0680e-2 | 5.0680e-2 | 4.9729e-4 | 101.91303336514295 |
| F3 | Han | Italian_North | Mbuti | | 593124 | 4.9903e-2 | 4.9904e-2 | 4.8436e-4 | 103.03094306099203 |
| F3 | Han | Italian_South | Mbuti | | 592980 | 4.9201e-2 | 4.9201e-2 | 5.1170e-4 | 96.15239597674244 |
| F3 | Han | Lithuanian | Mbuti | | 593124 | 5.0896e-2 | 5.0896e-2 | 5.0638e-4 | 100.50984037418753 |
| F3 | Han | Maltese | Mbuti | | 593124 | 4.8751e-2 | 4.8751e-2 | 4.7500e-4 | 102.63442479673623 |
| F3 | Han | Mordovian | Mbuti | | 593124 | 5.1820e-2 | 5.1820e-2 | 4.8853e-4 | 106.07409963190884 |
| F3 | Han | Norwegian | Mbuti | | 593124 | 5.0724e-2 | 5.0724e-2 | 4.9514e-4 | 102.4454387098217 |
| F3 | Han | Orcadian | Mbuti | | 593124 | 5.0469e-2 | 5.0469e-2 | 4.9485e-4 | 101.98814656611475 |
| F3 | Han | Russian | Mbuti | | 593124 | 5.1277e-2 | 5.1277e-2 | 4.8613e-4 | 105.48070801791317 |
| F3 | Han | Sardinian | Mbuti | | 593124 | 4.9416e-2 | 4.9417e-2 | 4.8908e-4 | 101.04049389691913 |
| F3 | Han | Scottish | Mbuti | | 593124 | 5.0635e-2 | 5.0635e-2 | 5.0565e-4 | 100.13962104744425 |
| F3 | Han | Sicilian | Mbuti | | 593124 | 4.9194e-2 | 4.9194e-2 | 4.8157e-4 | 102.15353663091187 |
| F3 | Han | Spanish_North | Mbuti | | 593124 | 5.0032e-2 | 5.0032e-2 | 4.9377e-4 | 101.32594226555439 |
| F3 | Han | Spanish | Mbuti | | 593124 | 4.9693e-2 | 4.9693e-2 | 4.8551e-4 | 102.35200847948641 |
| F3 | Han | Ukrainian | Mbuti | | 593124 | 5.0731e-2 | 5.0731e-2 | 4.9506e-4 | 102.47529111692852 |
| F3 | Han | Finland_Levanluhta | Mbuti | | 303033 | 5.4488e-2 | 5.4488e-2 | 5.7681e-4 | 94.46487653920919 |
| F3 | Han | Russia_Bolshoy | Mbuti | | 542789 | 5.7273e-2 | 5.7273e-2 | 5.2875e-4 | 108.31739594898687 |
| F3 | Han | Russia_Chalmny_Varre | Mbuti | | 428215 | 5.4000e-2 | 5.4000e-2 | 5.6936e-4 | 94.84371082564112 |
| F3 | Han | Saami.DG | Mbuti | | 585193 | 5.4727e-2 | 5.4728e-2 | 5.5546e-4 | 98.5265149143263 |
'-----------'-----'----------------------'-------'---'---------'----------------'--------------------'------------------'--------------------'
```
## Plotting Outgroup F3
Now it’s time to plot these results using R. Let's first read in the table:
```{r echo=TRUE}
d <- read.csv("fstats_working/outgroupf3_europe.tsv", sep = "\t")
```
We can check that it worked:
```{r echo=TRUE}
head(d)
```
Nice, now on to plotting in Base-R:
---
```{r echo=TRUE}
#| fig-height: 8
#| fig-width: 8
#| out-width: "100%"
#| fig-align: "center"
order <- order(d$Estimate_Total) # order the estimates for visual effect
x <- d$Estimate_Jackknife[order]
xErr <- d$StdErr_Jackknife[order]
y <- seq_along(d$b)
plot(x, y, xlab = "Z Score", ylab = NA, yaxt = "n", # plot no y-axis ticks
xlim = c(0.048,0.06),
main = "F3(Han, X; Mbuti)")
# plot the labels
text(x + 0.001, y, labels = d$b, adj=0)
# plot the error bars
arrows(x - xErr, y, x + xErr, y, length=0.05, angle=90, code=3)
```
As expected, the ancient samples and modern Saami are the ones with the highest allele sharing with present-day East Asians (as represented by Han) compared to many other Europeans.
## A real Example
Admixture-F3:

::: aside
Patterson et al. 2012
:::