-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnalysisMaMi_2022_clean.R
1861 lines (1394 loc) · 84.2 KB
/
AnalysisMaMi_2022_clean.R
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#******************************************
#******************************************
### Analysis Paper "A global analysis of matches and mismatches between human genetic and linguistic histories"
# GeLaTo database
# Chiara Barbieri April 2022
#******************************************
#*
#*
#*
### read the two main files
# list of 397 populations :
perpopRED<-read.table("PerpopRED_MaMi2022.txt", header = T, sep = "\t", as.is=T)
# list of pairwise comparisons :
FstListinfo<-read.table("FstListREDinfo_MaMi2022.txt", header=T, sep="\t")
# color palette for major language families
#
Lfamil<-table(perpopRED$glottolog.NAME)
MainFamilies<-unlist(labels(Lfamil[which(Lfamil>4)])) # minimum 5 populations per Lang Family
sizes<-as.numeric(unlist((Lfamil[which(Lfamil>4)])))
perpopREDfamily<-perpopRED[which(perpopRED$glottolog.NAME %in% MainFamilies),]
colorchoice<-c( "darkorange4" ,"#93032E" , "#33A02C" , "#A6D854" , "#377EB8" , "#E31A1C" , "#FFD92F" , "#FF7F00" , "#666666" ,
"cyan4" , "#BC80BD" , "#FED9A6" , "tan3" , "#6A3D9A" , "deeppink" )
perpopRED$MainFamilies<-NA
perpopRED$MainFamilies[which(perpopRED$glottolog.NAME%in%MainFamilies)]<-perpopRED$glottolog.NAME[which(perpopRED$glottolog.NAME%in%MainFamilies)]
# #*********
# library(RColorBrewer)
# qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
# col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
# colorchoice<-sample(col_vector, length(MainFamilies))
# #*********
MainFamilies2<-as.data.frame(MainFamilies)
MainFamilies2$COLOR<-colorchoice
MainFamilies2$names<-paste0(MainFamilies, " (",sizes, ")" )
library(ggplot2)
# color palette blue and red
darkredYES<-"#BA1E1E"
blueNO<-"#198B9F"
#******************************************
### MAP
#******************************************
library(maps)
library('maps')
library('geosphere')
library(rworldmap)
# Script from Balthasar Bickel to plot maps Pacific Centered
source("ggworld.R") # map for Pacific Center projection, modified from Balthasar Bickel
#
perpopRED$lat<-as.numeric(perpopRED$lat)
perpopRED$lon<-as.numeric(perpopRED$lon)
perpopRED2<-perpopRED[!is.na(perpopRED$lat),] # exclude populations for which i do not have geographic coordinates
perpopREDSHIFTMAP<-MoveDataCoord(perpopRED2) # perform coordinate shift to plot Pacific centered maps
## map population location and language families
#### MAP WITH MAJOR LANGUAGE FAMILIES ASSIGNED TO COLOR CODE,
# Basic map
#******************************************
#*
# make a base_plot with contained latitudes
base_plot <- ggplot() +
geom_polygon(data = subset(world.sh.tr.df, lat > -60 & lat < 72),
aes(x = X, y = Y, group = group),
fill = base_fill,
color = base_color,
size = .1
) + theme_void()
base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color="black",shape=3,size=0.5)+
theme(legend.position="bottom", legend.text=element_text(size=8), legend.title = element_blank())+
geom_point(data = perpopREDSHIFTMAP[which(perpopREDSHIFTMAP$glottolog.NAME%in%MainFamiliesSHORT),],
aes(x = lon.1, y = lat.1, color=glottolog.NAME),
size = 2, alpha=0.5)+
scale_color_manual(values=colorchoice3, labels = MainFamilies3$names)
ggsave("WholeGelatoMap_Pacific_LittleCrossesMajorFamilies_largerFig2_2022.pdf", useDingbats=FALSE, width = 8.5, height = 5, dpi = 300, units = "in")
#******************************************
#*#******************************************
#* genetic and linguistic enclaves
#*#******************************************
# ***************************************************
# evaluate the incidence of neighbors from the SAME AND THE different L famiy for each population
# closer neighbor from same and different L family
perpopRED$hasOtherMatches<-"YES"
perpopRED$closeFstSameFamily<-NA
perpopRED$geodistSameFamily<-NA
perpopRED$closeFstDIFFFamily<-NA
perpopRED$geodistDIFFFamily<-NA
perpopRED$DIFFFamilyClosestpop<-NA
perpopRED$SameFamilyMostDistantClosestFst<-NA
perpopRED$SamefamilyClosestpop<-NA
# how far the same family is gen closer than other families?
for (i in 1:nrow(perpopRED)){
targetpop<-perpopRED$PopName[i]
temp<-FstListinfo[which(FstListinfo$Pop1==targetpop),]
samefamily<-temp[which(temp$FAMILY!="DIVERSE"),]
escludiniVicini<-which(samefamily$GEOdist<10&samefamily$glottocodeBase1==samefamily$glottocodeBase2)
if(length(escludiniVicini)!=0){
samefamily<-samefamily[-escludiniVicini,] # exclude when there is a neighbor too close (LESS THAN 10 KM) from same exact language as DUPLICATED SAMPLE
}
if(nrow(samefamily)==0){
perpopRED$hasOtherMatches[i]<-"NO"
}
else{
perpopRED$closeFstSameFamily[i]<-sort(samefamily$FST)[1] # the closest Fst
perpopRED$geodistSameFamily[i]<-samefamily$GEOdist[order(samefamily$FST)][1] # the geographic distance from the closest Fst
perpopRED$SamefamilyClosestpop[i]<-samefamily[order(samefamily$FST),][1,]$Pop2 # the pop which closest fst in match
}
DIFFfamily<-temp[which(temp$FAMILY=="DIVERSE"),]
perpopRED$closeFstDIFFFamily[i]<-sort(DIFFfamily$FST)[1] # the closest Fst from a different language family
perpopRED$geodistDIFFFamily[i]<-DIFFfamily$GEOdist[order(DIFFfamily$FST)][1] # the geographic distance from the closest Fst of a different language family
perpopRED$DIFFFamilyClosestpop[i]<-DIFFfamily[order(DIFFfamily$FST),][1,]$Pop2 # the pop which closest fst in mismatch
if(length(which(sort(samefamily$FST)<perpopRED$closeFstDIFFFamily[i]))==0){
perpopRED$SameFamilyMostDistantClosestFst[i]<-"NONE" }
else{
perpopRED$SameFamilyMostDistantClosestFst[i]<-tail(samefamily$GEOdist[which(sort(samefamily$FST)<perpopRED$closeFstDIFFFamily[i])],1) # the geographic distance from the closest Fst of the same language family that is less than the closest fst from diff L family
}
}
perpopRED$proportionFST_diff_sameFamily<- perpopRED$closeFstDIFFFamily/perpopRED$closeFstSameFamily
perpopRED$proportionGeoDistSameDiffFamily<- perpopRED$geodistDIFFFamily/perpopRED$geodistSameFamily
perpopRED$EnclavesMismatch<-NA
perpopRED$EnclavesMismatch[which(perpopRED$closeFstDIFFFamily==0&perpopRED$closeFstSameFamil!=0)] <- "secondaryMISMATCH_FSTzeroDiffFamily"
perpopRED$EnclavesMismatch[which(perpopRED$proportionFST_diff_sameFamily<1&perpopRED$proportionGeoDistSameDiffFamily>1)] <- "MISMATCH"
perpopRED$EnclavesMismatch[which(perpopRED$proportionFST_diff_sameFamily>1&perpopRED$proportionGeoDistSameDiffFamily<1)] <- "MATCH"
perpopRED$EnclavesMismatch[which(perpopRED$hasOtherMatches!="YES" )]<-"ZeroSameFamilyNeighbors"
table(perpopRED$EnclavesMismatch)/nrow(perpopRED)
### table to visualize how many cases
#************************
## april 2022
MATCH MISMATCH secondaryMISMATCH_FSTzeroDiffFamily ZeroSameFamilyNeighbors
0.130982368 0.070528967 0.005037783 0.055415617
MATCH MISMATCH secondaryMISMATCH_FSTzeroDiffFamily ZeroSameFamilyNeighbors
52 28 2 22
#************************
#Matches
ListMatches<-perpopRED$PopName [which(perpopRED$EnclavesMismatch=="MATCH")]
### List of very drifted populations to flag
DRIFTONI<-perpopRED[which(perpopRED$medianFSTregion>0.1&perpopRED$medianFST>0.1),]$PopName
> DRIFTONI
[1] "Baining_Marabu" "Chukchi" "Eskimo_Sireniki" "Itelmen" "Ju_hoan_North"
[6] "Karitiana" "Koryak" "Lahu" "Nanai" "Nganasan"
[11] "Nganasan_UstAvam" "Nganasan_Volochanka" "Nivh" "Onge" "Rennell_and_Bellona"
[16] "San" "She" "Surui"
## genetic and linguistic enclaves
ListEnclaves<-perpopRED$PopName [which(perpopRED$EnclavesMismatch%in%c("MISMATCH","secondaryMISMATCH_FSTzeroDiffFamily"))]
> ListEnclaves
[1] "Yoruba" "Mengen" "Bengali" "Hazara"
[5] "Kharia" "Gui" "Khwe" "Nama"
[9] "Mongola" "Avar_Gunibsky" "Aleut" "Dai"
[13] "Jew_Georgian" "Khomani" "Spanish_PaisVasco" "Yaquis"
[17] "Yukagir_Forest" "Yukagir_Tundra" "Zapotec" "Wayku"
[21] "Han-NChina" "Evenk_FarEast" "Cocama" "Guarani"
[25] "Guarani_GN" "Karitiana" "Surui" "Azeri_Azerbajan"
[29] "Hungarian1" "Hungarian2"
perpopRED$ListEnclaves<-NA
perpopRED$ListEnclaves[which(perpopRED$PopName%in%ListEnclaves)]<-"Enclave"
#*#******************************************
#*
#* different distribution FST closest same family and different family
#*
aa<-perpopRED[,c(1, which(colnames(perpopRED)%in%c("glottolog.NAME","closeFstSameFamily","closeFstDIFFFamily" )))]
aa$difference<-aa$closeFstSameFamily-aa$closeFstDIFFFamily
length(which(aa$difference>0))/(nrow(perpopRED)-length(which(is.na(aa$difference)))) # only for the comparisons for which i have a same family FST
[1] 0.1973333 ## april 2022
# ~20 % of the pops who have another genetic population of the same language family do have closer FST with speakers of another language family
# *****************************************
# ### figure 1B, target enclaves
# *****************************************
# # target match enclave Kalmyk BedouinB
# target genetic enclave Jew_Georgian
# target linguistic enclave Hungarian
library(reshape)
targetenclaves<-c("Jew_Georgian", "Hungarian1","Himba")
targetenclavesinfo<-perpopRED[which(perpopRED$PopName%in%targetenclaves),]
targetenclavesinfo<-targetenclavesinfo[,which(colnames(targetenclavesinfo)%in%
c( "PopName", "glottolog.NAME","closeFstSameFamily","geodistSameFamily" ,
"SamefamilyClosestpop" ,"closeFstDIFFFamily" , "geodistDIFFFamily" ,"DIFFFamilyClosestpop", "DIFFFamilyClosestFAM"))]
targetenclavesinfo
targetenclavesinfo[2,1]<-"Hungarian"
targetenclavesinfo[3,1]<-"Jewish Georgian"
colnames(targetenclavesinfo)[3]<-"YES"
colnames(targetenclavesinfo)[5]<-"NO"
testt<-melt(targetenclavesinfo[,-c(4,6)])
coso<-melt(targetenclavesinfo[,c(1,4,6)])
testt$GeoDist<-paste0(coso$value, " km")
testt$GeoDistTrue<-coso$value
testt$closestPOP<-testt$SamefamilyClosestpop
testt$closestPOP[which(testt$variable=="NO")]<-testt$DIFFFamilyClosestpop[which(testt$variable=="NO")]
testt$value[which(testt$value==0)]<-0.0001 # or it does not show up in the plot
testt<-testt[1:6,]
# Balthasar version
ggplot(testt, aes(x=GeoDistTrue,y= value, color=variable))+
geom_point(size=4) +
scale_color_manual("Same Family", values=c(darkredYES,blueNO), position="bottom")+
xlab("Geographic distance (km)") +
ylab("Fst") +
theme_minimal()+
facet_wrap(~PopName,scales="free")
ggsave("Fig1b_toyExamplesEnclaves_2022.pdf",height=2.5,width=8, useDingbats=FALSE)
#******************************************
#*#******************************************
#*#******************************************
#*#******************************************
#*#******************************************
#*#******************************************
#* COMPARING MEDIAN FST BETWEEN AND WITHIN
# POPULATION HEURISTIC case 2
#* FST distribution between and within language families MISALIGNED
#*#******************************************
#*#******************************************
## exclude neighbors who speak the same language as DUPLICATE POPS
escludiniVicini<-FstListinfo[which(FstListinfo$GEOdist<10&FstListinfo$glottocodeBase1==FstListinfo$glottocodeBase2),]
> dim(escludiniVicini)
[1] 32 25
escludiniVicini[which(escludiniVicini$case=="single"),][,c(1:3,11:12)]
Pop2 Pop1 FST family2 region2
73128 Kove_Tamuniai Kove 0.000000000 Austronesian OCEANIA
85508 Mamanwa1 Mamanwa 0.050776000 Austronesian SOUTHEAST_ASIA
115332 San Ju_hoan_North 0.001266060 Kxa AFRICA
130035 Sulka_Watwat Sulka_Ganai 0.000000000 Sulka OCEANIA
14937 BedouinB BedouinA 0.017572100 Afro-Asiatic EURASIA
42676 Greek_WGA Greek_Athens 0.003147220 Indo-European EURASIA
45019 GujaratiB GujaratiA 0.001633070 Indo-European EURASIA
45284 GujaratiC GujaratiB 0.003168390 Indo-European EURASIA
45342 GujaratiC GujaratiA 0.002983290 Indo-European EURASIA
46884 Han_HGDP Han-NChina 0.002480980 Sino-Tibetan EURASIA
49935 Hungarian2 Hungarian1 0.000000000 Uralic EURASIA
78287 Lebanese_Muslim Lebanese_Christian 0.001025050 Afro-Asiatic EURASIA
107168 Palestinian Druze 0.008987530 Afro-Asiatic EURASIA
107902 Peru_Quechua Cusco2 0.000427919 Quechuan AMERICAS
150014 Uzbek_Tashkent Uzbek 0.000917217 Turkic EURASIA
150910 Vietnamese_North Kinh 0.001434990 Austroasiatic SOUTHEAST_ASIA
## exclude DRIFTONI
## exclude drifted pops or the Fst averages will be higher than normal
DRIFTONI<-perpopRED[which(perpopRED$medianFSTregion>0.1&perpopRED$medianFST>0.1),]$PopName
FstListREDinfo_noDuplicateNeighbors<-FstListinfo[-which(FstListinfo$GEOdist<10&FstListinfo$glottocodeBase1==FstListinfo$glottocodeBase2),]
FstListREDinfo_noDuplicateNeighborsNoDrift<-FstListREDinfo_noDuplicateNeighbors[-c(which(FstListREDinfo_noDuplicateNeighbors$Pop2%in%DRIFTONI), which(FstListREDinfo_noDuplicateNeighbors$Pop1%in%DRIFTONI)),]
library(pairwiseCI)
perpopRED$MedDiff<-NA
perpopRED$MedDiffCIlower<-NA
perpopRED$MedDiffCIupper<-NA
perpopRED$MedianWithinSMALLER2<-NA
perpopRED$MedDiffCI<-NA
for (i in 1:nrow(perpopRED)){
TARGET<-perpopRED$PopName[i]
meltini<-FstListREDinfo_noDuplicateNeighborsNoDrift[which(FstListREDinfo_noDuplicateNeighborsNoDrift$Pop1==TARGET),]
meltiniSAME<-meltini[which(meltini$SameFamily=="YES"),]
maxgeofam<-max(meltiniSAME$GEOdist, na.rm = T)
if(maxgeofam<500){
maxgeofam<-500
}
meltini<-meltini[which(meltini$GEOdist<=maxgeofam),]
meltiniNO<-meltini[which(meltini$SameFamily!="YES"),]
if(length(which(meltini$SameFamily=="YES"))>2&
length(which(meltini$SameFamily=="NO"))>2){ # need to have at least 3 comparisons between and within family
perpopRED$MedDiff[i]=Median.diff(meltiniNO$FstLinear, meltiniSAME$FstLinear, conf.level=0.95, alternative="lesser",R=10000)$estimate
perpopRED$MedDiffCIlower[i]=Median.diff(meltiniNO$FstLinear, meltiniSAME$FstLinear, conf.level=0.95, alternative="lesser",R=10000)$conf.int[1]
perpopRED$MedDiffCIupper[i]=Median.diff(meltiniNO$FstLinear, meltiniSAME$FstLinear, conf.level=0.95, alternative="lesser",R=10000)$conf.int[2]
perpopRED$MedianWithinSMALLER2[i]<-median(meltiniSAME$FstLinear)<median(meltiniNO$FstLinear) # if TRUE, the median FST within is smaller than the FST between
}
}
perpopRED$MedDiffCI<-perpopRED$MedDiffCIupper-perpopRED$MedDiffCIlower
ListMisaligned<-perpopRED$PopName[which(perpopRED$MedDiff<0)]
length(ListMisaligned)/length(which(!is.na(perpopRED$MedDiff)))
#*#******************************************
[1] 0.2044728 ## 20% of populations in misalignment
#*#******************************************
> length(ListMisaligned)
[1] 64
perpopRED$listSingleCases<-"ND"
perpopRED$listSingleCases[which(perpopRED$PopName%in%ListEnclaves)]<-"Enclave"
perpopRED$listSingleCases[which(perpopRED$PopName%in%ListMisaligned)]<-"Misaligned"
perpopRED$listSingleCases[which(perpopRED$PopName%in%intersect(ListMisaligned, ListEnclaves))]<-"EnclaveANDMisaligned"
perpopRED$listSingleCases[which(perpopRED$PopName%in%ListMatches)]<-"Match"
perpopRED$listSingleCases[which(perpopRED$PopName%in%intersect(ListMisaligned, ListMatches))]<-"MatchBUTMisaligned"
perpopRED$listSingleCases[which(perpopRED$PopName%in%DRIFTONI)]<-"Drifted"
#**************************************************
### table of cases mismatch enclaves
# Supplementary Table S2
EnclavesMismatch<-perpopRED[grep("MISMATCH",perpopRED$EnclavesMismatch),]
perpopRED$DIFFFamilyClosestFAM<-perpopRED$glottolog.NAME[match(perpopRED$DIFFFamilyClosestpop,perpopRED$PopName)]
colnameschoice<-c("PopName","glottolog.NAME", "geodistSameFamily","geodistDIFFFamily","DIFFFamilyClosestpop", "DIFFFamilyClosestFAM")
EnclavesMismatch<-EnclavesMismatch[,colnameschoice]
EnclavesMismatch$MisalignedMedian<-NA
EnclavesMismatch$MisalignedMedian<-perpopRED$MedDiff[match(EnclavesMismatch$PopName,perpopRED$PopName)]
EnclavesMismatch$MisalignedMedian<-EnclavesMismatch$MisalignedMedian<0
EnclavesMismatch$MisalignedLowerCI<-NA
EnclavesMismatch$MisalignedLowerCI<-perpopRED$MedDiffCIlower[match(EnclavesMismatch$PopName,perpopRED$PopName)]
EnclavesMismatch$MisalignedLowerCI<-EnclavesMismatch$MisalignedLowerCI<0
EnclavesMismatch$MisalignedUpperCI<-NA
EnclavesMismatch$MisalignedUpperCI<-perpopRED$MedDiffCIupper[match(EnclavesMismatch$PopName,perpopRED$PopName)]
EnclavesMismatch$MisalignedUpperCI<-EnclavesMismatch$MisalignedUpperCI<0
# write.table(EnclavesMismatch[order(EnclavesMismatch$geodistDIFFFamily),],"tableSupplEnclaves2022.txt", sep="\t", row.names = F, quote=F)
#******************************************
## figure distribution FST SAME OR DIFFERENT FAMILY for each population
## Figure 1C, case studies
#******************************************
# subset case studies
# FstListREDinfo_noDuplicateNeighbors<-FstListinfo[-which(FstListinfo$GEOdist<10&FstListinfo$glottocodeBase1==FstListinfo$glottocodeBase2),]
# FstListREDinfo_noDuplicateNeighborsNoDrift<-FstListREDinfo_noDuplicateNeighbors[-c(which(FstListREDinfo_noDuplicateNeighbors$Pop2%in%DRIFTONI), which(FstListREDinfo_noDuplicateNeighbors$Pop1%in%DRIFTONI)),]
casestudies<-c( "Kalmyk", "Azeri_Azerbajan")
meltoni<-NA
for (i in 1:length(casestudies)){
TARGET<-casestudies[i]
meltini<-FstListREDinfo_noDuplicateNeighborsNoDrift[which(FstListREDinfo_noDuplicateNeighborsNoDrift$Pop1==TARGET),]
meltiniSAME<-meltini[which(meltini$SameFamily=="YES"),]
maxgeofam<-max(meltiniSAME$GEOdist, na.rm = T)
meltini<-meltini[which(meltini$GEOdist<=maxgeofam),]
meltini$TARGET<-TARGET
meltoni<-rbind(meltoni, meltini)
}
meltoni<-meltoni[-1,]
meltoni$TARGET<-factor(meltoni$TARGET, levels=casestudies)
ggg<-ggplot(meltoni,aes(y=FST, x=SameFamily,color=SameFamily))
ggg+ geom_jitter(shape=16, position=position_jitter(0.2))+
geom_violin(trim=FALSE, alpha=0.4)+
stat_summary(fun.y= median,
geom="pointrange", color="gold") +
theme_minimal()+
scale_y_log10()+
scale_color_manual(values=c(blueNO,darkredYES))+
guides(color=FALSE)+
facet_wrap(~TARGET,nrow = 1)
# scales="free")
ggsave("Fig1C_caseStudyMisalignedVIOLIN_YESorNO_2022.pdf", height=3,width=5.5, useDingbats=FALSE)
###
#*#******************************************
#* #*#******************************************
#* #*#******************************************
#*
#*
######################################################################
########################################################
##############
# language family comparisons
# geographic distance and smooth or linear regression
## testing if SAME FAMILY has an effect on Fst given GeoDist
############################
############################
#
# FstListREDinfo_noDuplicateNeighbors<-FstListinfo[-which(FstListinfo$GEOdist<10&FstListinfo$glottocodeBase1==FstListinfo$glottocodeBase2),]
#
# FstListREDinfo_noDuplicateNeighborsNoDrift<-FstListREDinfo_noDuplicateNeighbors[-c
# (which(FstListREDinfo_noDuplicateNeighbors$Pop2%in%DRIFTONI),
# which(FstListREDinfo_noDuplicateNeighbors$Pop1%in%DRIFTONI)),]
#*********************************************
### FIGURE 2
#*********************************************
#*
#*
### Within/between family comparisons
# original script from Damián Blasi, adapted by C. Barbieri
#
# FIGURE 2
#
library(tidyverse)
#large_families<-MainFamilies[-13] # exclude Tupi
large_families<-c( "Atlantic-Congo" , "Indo-European", "Sino-Tibetan" , "Mongolic-Khitan",
"Afro-Asiatic", "Turkic" , "Austronesian" , "Uralic") # families above 8 pops
pops_for_test<-FstListREDinfo_noDuplicateNeighborsNoDrift
pops_for_test$FAMILY<-factor(pops_for_test$FAMILY, levels=large_families) # force the order when doing facet wrap in the figure
pops_for_test$listSingleCasesPOP1<-perpopRED$listSingleCases[match(pops_for_test$Pop1, perpopRED$PopName)]
pops_for_test$listSingleCasesPOP1[which(pops_for_test$listSingleCasesPOP1=="Match")]<-"ND"
pops_for_test$listSingleCasesPOP1[which(pops_for_test$listSingleCasesPOP1=="MatchBUTMisaligned")]<-"ND"
pops_for_test$listSingleCasesPOP1[which(pops_for_test$listSingleCasesPOP1=="ND")]<-NA
pops_for_test$listSingleCasesPOP1[which(pops_for_test$SameFamily=="NO")]<-NA # do not want to plot mismatches in different lang family comparisons
FAM<- large_families
for(f in FAM) {
pops_for_test[[f]]<-(pops_for_test$family1==f)|(pops_for_test$family2==f)
threshold_geo<-max(pops_for_test$GEOdist[pops_for_test$FAMILY==f],
na.rm = T)
if(threshold_geo<500){
threshold_geo<-500
}
print(f)
print(threshold_geo)
pops_for_test[[f]]<-pops_for_test[[f]]*(pops_for_test$GEOdist<=threshold_geo)
pops_for_test[[f]]<-sapply(pops_for_test[[f]],function(x) ifelse(is.na(x),FALSE,x))
}
family_distances<-pivot_longer(pops_for_test,
cols = FAM,
names_to = "Family_plot",
values_to = "Include") %>%
filter(Include==TRUE)
#### add the within comparison symbols for listSingleCasesPOP1, flagging mismatches
family_distances$Family_plot<-factor(family_distances$Family_plot, levels=large_families) # force the order when doing facet wrap in the figure
family_distances %>%
ggplot(aes(y=FstLinear,x=GEOdist))+
geom_point(aes(fill=SameFamily, size=SameFamily),shape=21, alpha=0.3, stroke=0)+
geom_point(aes(fill=SameFamily, shape=listSingleCasesPOP1),alpha=0.3,size=3, stroke=0.3)+
geom_smooth(aes(group=SameFamily),se=F, color="white", size=2.5)+
geom_smooth(aes(color=SameFamily),se=F)+
geom_smooth(color="#FBB13C",linetype = "dotdash",alpha=0.4, size=1, method = lm)+
facet_wrap(~Family_plot,
scales="free",
nrow=length(FAM)/4)+
theme_minimal()+
scale_y_sqrt()+
scale_fill_manual(values=c("#7FDBEB","#E45B5B"))+
scale_color_manual(values=c(blueNO,darkredYES))+
scale_shape_manual(" ",values = c(25,23,22))+
scale_size_manual(values = c(1,2))+
theme(legend.position = "bottom", axis.text=element_text(size=7),strip.text = element_text(size = 12))+
xlab("Geographic Distance - km") +
ylab("Linear FST")
ggsave("Fig2_smoothregressionPlotwithLinear_highlightMismatches_2022.pdf", useDingbats=FALSE, height = 6, width = 10)
ggsave("Fig2_smoothregressionPlotwithLinear_highlightMismatches_2022.png", height = 5, width = 10)
# ***************************************************
#### SUPPLEMENTARY FIGURES
# ***************************************************
# ***************************************************
# ***************************************************
########################################################
# Figure S8 supplementary - comparison of difference of medians and CI, colored per language family
########################################################
MainFamilies<-unlist(labels(Lfamil[which(Lfamil>4)])) # minimum 5 populations per Lang Family
perpopREDfamily<-perpopRED[which(perpopRED$glottolog.NAME %in% MainFamilies),]
perpopREDfamilynoDrift<-perpopREDfamily[-which(perpopREDfamily$listSingleCases=="Drifted"),]
perpopREDEnclaves<-perpopRED[which(perpopRED$EnclavesMismatch%in%c("MATCH", "MISMATCH","secondaryMISMATCH_FSTzeroDiffFamily")),]
perpopREDEnclaves<-perpopRED[which(perpopRED$EnclavesMismatch%in%c("MATCH", "MISMATCH","secondaryMISMATCH_FSTzeroDiffFamily")),]
perpopRED$enclavesAgain<-NA
perpopRED$enclavesAgain[which(perpopRED$EnclavesMismatch=="MATCH")]<-"Match"
perpopRED$enclavesAgain[which(perpopRED$EnclavesMismatch=="MISMATCH")]<-"GeneticEnclave"
perpopRED$enclavesAgain[which(perpopRED$EnclavesMismatch=="secondaryMISMATCH_FSTzeroDiffFamily")]<-"LinguisticEnclave"
perpopREDnoDrift<-perpopRED[-which(perpopRED$listSingleCases=="Drifted"),]
library(ggrepel)
# panel A, color code language families all together
fst1<-ggplot(perpopREDnoDrift,aes(MedDiff,MedDiffCI, color=MainFamilies))+
geom_point(alpha=0.5, size=3,show.legend = FALSE)+
#geom_text_repel(aes(label=PopName, color=MainFamilies), size=0.5)+
scale_color_manual(values = colorchoice)+
theme_light() +
xlab("Difference Median FST between - within") +
ylab("Confidence Interval median difference") +
geom_vline(xintercept=0)+
scale_y_log10()
#ggsave("DifferenceMedianLogScale.pdf", height = 12, width = 14, useDingbats=FALSE)
# panel C, separate language families facet wrap
fst2<-ggplot(perpopREDfamilynoDrift ,aes(MedDiff,MedDiffCI, color=MainFamilies))+
geom_point(alpha=0.5, size=3,show.legend = FALSE)+
geom_text_repel(aes(label=PopName, color=MainFamilies), size=0.5, show.legend = FALSE)+
scale_color_manual(values = colorchoice)+
theme_light()+ xlab("Difference Median FST between - within") +
ylab("Confidence Interval median difference") +
geom_vline(xintercept=0)+
scale_y_log10()+
facet_wrap(~MainFamilies,ncol=6)
#ggsave("DifferenceMedianLogScaleMajorLangFam.pdf", height = 12, width = 14, useDingbats=FALSE)
# panel B, color code previous enclaves and matches
fst3<-ggplot(perpopREDnoDrift,aes(MedDiff,MedDiffCI, color=enclavesAgain))+
geom_point(alpha=0.5, size=2)+
geom_text_repel(data=perpopREDnoDrift[!is.na(perpopREDnoDrift$enclavesAgain),],
aes(MedDiff,MedDiffCI,label=PopName, color=enclavesAgain), size=1.2)+
# scale_color_manual(values = colorchoice2)+
theme_light() +
xlab("Difference Median FST between - within") +
ylab("Confidence Interval median difference") +
geom_vline(xintercept=0)+
theme(legend.title = element_blank())+
scale_y_log10()
#ggsave("DifferenceMedianwithTextENCLAVES.pdf", height = 12, width = 14, useDingbats=FALSE)
library(patchwork)
patchwork<-(fst1 | fst3) / fst2
patchwork + plot_annotation(tag_levels = 'A')
ggsave("DifferenceMedianLogScaleGLOBALandMajorLangFam_3plots_2022.pdf", height = 10, width = 12, useDingbats=FALSE)
ggsave("DifferenceMedianLogScaleGLOBALandMajorLangFam_3plots_2022.png", height = 10, width = 12, dpi = 150)
########
## the map with singular mismatches of linguistic and genetic migrants, misaligned and drifted
# Figure S10
########
library(ggrepel)
perpopRED2<-perpopRED[!is.na(perpopRED$lat),]
perpopREDSHIFTMAP<-MoveDataCoord(perpopRED2)
perpopREDSHIFTMAPinterest<-perpopREDSHIFTMAP[-which(perpopREDSHIFTMAP$listSingleCases=="ND"),]
#perpopREDSHIFTMAPmismatch<-perpopREDSHIFTMAPinterest[which(perpopREDSHIFTMAPinterest$singlepops%in%c("Misalligned","Enclave")),]
perpopREDSHIFTMAPnodata<-perpopREDSHIFTMAP[which(perpopREDSHIFTMAP$EnclavesMismatch=="ZeroSameFamilyNeighbors"),]
#perpopREDSHIFTMAPinterest<-perpopREDSHIFTMAPinterest[-which(perpopREDSHIFTMAPinterest$PopName%in% enclavesByMistake),]
#perpopREDSHIFTMAPmismatch<-perpopREDSHIFTMAPinterest[which(perpopREDSHIFTMAPinterest$EnclavesMismatch%in%c("MISMATCH","secondaryMISMATCH_FSTzeroDiffFamily")),]
### variant with language family color
# and symbols for single cases
base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color="black",shape=3,size=0.5)+
theme(legend.position="bottom",legend.title = element_blank())+
geom_point(data = perpopREDSHIFTMAP[which(perpopREDSHIFTMAP$MainFamilies%in%MainFamilies),],
aes(x = lon.1, y = lat.1, color=MainFamilies),
size = 2, alpha=0.6)+
geom_point(data=perpopREDSHIFTMAPinterest, aes(x = lon.1, y = lat.1,
shape=listSingleCases),alpha=0.5, size=2.5)+
# geom_point(data=perpopREDSHIFTMAPnodata, aes(x = lon.1, y = lat.1),
# color="ghostwhite", shape= 13,alpha=0.8)+
scale_shape_manual(values = c(11,6,5,8,7,0))+
scale_color_manual(values=colorchoice, labels = MainFamilies2$names)
ggsave("Fig_s10_WholeGelatoMap_Pacific_LittleCrossesMajorFamilies_SingleCases_rightSymbols_2022.pdf", useDingbats=FALSE, width = 14, height = 8, dpi = 300, units = "in")
ggsave("Fig_s10_WholeGelatoMap_Pacific_LittleCrossesMajorFamilies_SingleCases_rightSymbols_2022.png", width = 14, height = 8, dpi = 300, units = "in")
# ***************************************************
## Figure S3
library(plyr)
threshold<-c(500,1000,1500,2000,2500,3000)
blockneighborthresholdGeo<-c()
for (i in 1:length(threshold)){
testino2<-ddply(FstListinfo, "Pop1", function(x) length(which(x$GEOdist<threshold[i]&x$FAMILY=="DIVERSE"))) # no continental filter
colnames(testino2)<-c("PopName","GeoCloseUnrelated1")
testinomerged<-testino2
testinomerged$GeoCloseUnrelated<-testinomerged$GeoCloseUnrelated1/2
testinomerged$threshold<-threshold[i]
blockneighborthresholdGeo<-rbind(blockneighborthresholdGeo,testinomerged)
}
blockneighborthresholdGeoINFO<-merge(perpopRED,blockneighborthresholdGeo)
# frequencies at least one neighbor from diff family
freqOneneighbor<-c()
for (i in 1:length(threshold)){
thresholdtemp<-threshold[i]
popsNumberofNeighbors<-ddply(FstListinfo, "Pop1", function(x) length(which(x$GEOdist<thresholdtemp&x$FAMILY=="DIVERSE"))) # no continental filter
freqOneneighbor[i]<-length(which(popsNumberofNeighbors$V1>0))/nrow(perpopRED)
}
valuestop<-round(freqOneneighbor*100)
[1] 57 85 93 98 99 100 # add manually on the violin plot figure
valuestop<-paste0(valuestop, "%")
# violin plots with number of populations from a different language family
# fig S3B
blockneighborthresholdGeoINFO$threshold<-as.character(blockneighborthresholdGeoINFO$threshold)
blockneighborthresholdGeoINFO$threshold[which(blockneighborthresholdGeoINFO$threshold=="500")]<-" 500"
ga<-ggplot(blockneighborthresholdGeoINFO,aes(x=threshold,y=GeoCloseUnrelated1))
S3B <-ga+ geom_jitter(shape=16, position=position_jitter(0.2))+
geom_violin(trim=FALSE, alpha=0.4)+
stat_summary(fun.data="mean_sdl",
geom="pointrange", color="orange")+
theme_minimal()+
theme(axis.title.y = element_text(size = 8)) +
ylim(0,150)+xlab("Geographic radius in km")+
ylab("Number of populations from a different language family") +
annotate("text", x = 1:6, y = 150, label = valuestop)
# ***************************************************
# evaluate the incidence of neighbors from a different L famiy for each population
# count how many populations from different L families in a radius of 1000 km in the same continent
popsNumberofNeighbors<-ddply(FstListinfo, "Pop1", function(x) length(which(x$GEOdist<1000&x$FAMILY=="DIVERSE"))) # no continental filter
perpopRED$nNeighborsDiffFamily<-popsNumberofNeighbors
colnames(popsNumberofNeighbors)<-c("PopName","GeoCloseUnrelated")
perpopRED3<-merge(perpopRED,popsNumberofNeighbors)
perpopRED4<-perpopRED3[-which(perpopRED3$GeoCloseUnrelated==0),]
perpopRED4_SHIFTMAP<-MoveDataCoord(perpopRED4)
# plot on a map the density of unrelated pop geographically close
# fig S3A
S3A <- base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color = "black",
size = 0.5, shape=3)+
geom_point(data=perpopRED4_SHIFTMAP, aes(x = lon.1, y = lat.1,color=as.numeric(GeoCloseUnrelated)), alpha=0.5, size=2)+
# ggtitle("number of neighbors from a different language family")+
scale_color_gradient(low = "blue", high = "red",name="") +
xlab("") + ylab("")
# theme(legend.title = element_blank())
# ggsave("DensityPopulationsWithDifferentLFamilyNeighbors_noContinentFilter_map_PacificCenter_1000km_2021_.pdf", useDingbats=FALSE, width = 15, height = 8, dpi = 300, units = "in")
# count how many LANGUAGE FAMILIES in a radius of 1000 km in the same continent
popsNumberofNeighborFamilies<-ddply(FstListinfo, "Pop1", function(x) length(unique(x[which(x$GEOdist<1000&x$FAMILY=="DIVERSE"),]$family2))) # no continental filter
perpopRED$nDiffFamilies1000km<-popsNumberofNeighbors
colnames(popsNumberofNeighborFamilies)<-c("PopName","GeoCloseUnrelatedFamilies")
perpopRED3<-merge(perpopRED,popsNumberofNeighborFamilies)
perpopRED4<-perpopRED3[-which(perpopRED3$GeoCloseUnrelatedFamilies==0),]
perpopRED4_SHIFTMAP<-MoveDataCoord(perpopRED4)
# plot on a map the density of unrelated Language Families geographically close
# fig S3C
S3C <- base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color = "black",
size = 0.5, shape=3)+
geom_point(data=perpopRED4_SHIFTMAP, aes(x = lon.1, y = lat.1,color=as.numeric(GeoCloseUnrelatedFamilies)), alpha=0.5, size=2)+
# ggtitle("number of neighbors from a different language family")+
scale_color_gradient(low = "blue", high = "red",name="") +
xlab("") + ylab("")
# ggsave("DensityDifferentLFamilyNeighbors_noContinentFilter_map_PacificCenter_1000km_2021.pdf", useDingbats=FALSE, width = 15, height = 8, dpi = 300, units = "in")
## assemble
library(patchwork)
layout <- "
AAAA
AAAA
#BB#
CCCC
CCCC
"
S3A + S3B + S3C +
plot_layout(design = layout)+ plot_annotation(tag_levels = 'A')
ggsave("Fig_s03_evaluateIncidenceLanguageFamilies2022.pdf", height = 12, width = 10, useDingbats=FALSE)
ggsave("Fig_s03_evaluateIncidenceLanguageFamilies2022.png", height = 12, width = 10, dpi = 150)
#******************************************
# control the distribution of divergence time for each macro region
# fig S5
#******************************************
perpopREDNe<-perpopRED[which(perpopRED$USEforNe_calculation=="YES"),]
possiblepopswithNE<-perpopREDNe$PopName
meltFstREDinfoYESne<-FstListinfo[which(FstListinfo$Pop1%in%possiblepopswithNE),]
meltFstREDinfoYESne<-meltFstREDinfoYESne[which(meltFstREDinfoYESne$Pop2%in%possiblepopswithNE),]
meltFstGlotto_infowithinREgionTMRCA<-meltFstREDinfoYESne[which(meltFstREDinfoYESne$region1==meltFstREDinfoYESne$region2),]
meltFstGlotto_infowithinREgionTMRCA<-meltFstGlotto_infowithinREgionTMRCA[which(meltFstGlotto_infowithinREgionTMRCA$case=="single" ),]
## exclude drifted pops or the Fst averages will be higher than normal
#DRIFTONI<-perpopRED[which(perpopRED$medianFSTregion>0.1&perpopRED$medianFST>0.1),]$PopName
meltFstGlotto_infowithinREgionTMRCA<-meltFstGlotto_infowithinREgionTMRCA[-c(which(meltFstGlotto_infowithinREgionTMRCA$Pop2%in%DRIFTONI)),]
meltFstGlotto_infowithinREgionTMRCA<-meltFstGlotto_infowithinREgionTMRCA[-c(which(meltFstGlotto_infowithinREgionTMRCA$Pop1%in%DRIFTONI)),]
meltFstGlotto_infowithinREgionTMRCA$TMRCA_doubleNe<-as.numeric(meltFstGlotto_infowithinREgionTMRCA$TMRCA_doubleNe)
ggplot(meltFstGlotto_infowithinREgionTMRCA, aes(region1, TMRCA_doubleNe))+
geom_jitter(shape=16, position=position_jitter(0.2))+
geom_violin(trim=FALSE, alpha=0.4)+
stat_summary(fun.data="mean_sdl",
geom="pointrange", color="orange")+
ylim(0,70000)+xlab("")+ylab("population divergence time, years ago")+
theme_light()
ggsave("FigS05_distribTMRCA_ContinentsViolin2022.pdf", useDingbats=FALSE, width=6, height = 4)
#******************************************
#******************************************
#******************************************
#******************************************
# ANALYSIS PERCENTILES
#******************************************
# OVERVIEW OF MISMATCHES WITH CLOSE FST DISTANCES
#******************************************
#******************************************
#* Figure S6
# ------------------------------------------------------
# CONTINENT MEDIAN FST and a range of percentile threshold values
## adjust dataset without very Drifted and within and between regions
FstListGlotto_infowithinREgion<-FstListinfo[which(FstListinfo$region1==FstListinfo$region2),]
## exclude drifted pops or the Fst averages will be higher than normal
#DRIFTONI<-perpopRED[which(perpopRED$medianFSTregion>0.1&perpopRED$medianFST>0.1),]$PopName
FstListGlotto_infowithinREgionNoDrif<-FstListGlotto_infowithinREgion[-c(which(FstListGlotto_infowithinREgion$Pop2%in%DRIFTONI), which(FstListGlotto_infowithinREgion$Pop1%in%DRIFTONI)),]
FstListGlottoIBD_infoNoDrift<-FstListinfo[-c(which(FstListinfo$Pop2%in%DRIFTONI), which(FstListinfo$Pop1%in%DRIFTONI)),]
# set a series of percentiles up to 0.5
percentiles<-seq(from = .002, to = .5, by = .002)
#percentilesCoarse<-seq(from = .005, to = .5, by = .005)
# calculate thresholds per continents
library(plyr)
medianGEO<-ddply(FstListGlotto_infowithinREgionNoDrif, "region1", function(x) median(x$FST))
valuespercentileRegions<-data.frame(row.names =medianGEO$region1)
for (i in 1:length(percentiles)){
percTarget<-percentiles[i]
temp<-ddply(FstListGlotto_infowithinREgionNoDrif, "region1", function(x) quantile(x$FST, percTarget))
valuespercentileRegions[,i]<-temp[,2]
#colnames(valuespercentileRegions[i])<-colnames(temp)[2]
}
colnames(valuespercentileRegions)<-percentiles
medianGEO<-cbind(medianGEO,valuespercentileRegions)
colnames(medianGEO)[2]<-"medianRegion"
aggiuntaWorld<-c() # the list of percentile threshold on global distribution
for (i in 1:length(percentiles)){
percTarget<-percentiles[i]
aggiuntaWorld[i]<-quantile(FstListGlottoIBD_infoNoDrift$FST, percTarget)
}
aggiuntaWorldline<-c("WORLD", median(FstListGlottoIBD_infoNoDrift$FST),aggiuntaWorld)
medianGEO<-rbind(medianGEO,aggiuntaWorldline)
# assign the lowest percentile to each pair fst
FstListinfo$percentileFST<-NA
for (i in 1:nrow(FstListinfo)){
if(FstListinfo$region1[i]!=FstListinfo$region2[i] ){
FstListinfo$percentileFST[i]<- percentiles[which(FstListinfo$FST[i]<aggiuntaWorld)][1]
}
else {
reftemp<- valuespercentileRegions[which( medianGEO$region1== FstListinfo$region2[i]),] # list of percentiles corresponding to the continent of the two populations
FstListinfo$percentileFST[i]<- percentiles[which(FstListinfo$FST[i]<reftemp)][1]
}
}
# prepare plottable file for pairwise connections
betweenfamilSMALLgeoplottabile1<-FstListinfo
betweenfamilSMALLgeoplottabile1$index<-c(1:nrow(FstListinfo))
betweenfamilSMALLgeoplottabile1<-betweenfamilSMALLgeoplottabile1[,-which(colnames(betweenfamilSMALLgeoplottabile1)%in%c("lat2","lon2"))]
betweenfamilSMALLgeoplottabile2<-FstListinfo
betweenfamilSMALLgeoplottabile2$index<-c(1:nrow(FstListinfo))
betweenfamilSMALLgeoplottabile2<-betweenfamilSMALLgeoplottabile2[,-which(colnames(betweenfamilSMALLgeoplottabile2)%in%c("lat1","lon1"))]
betweenfamilSMALLgeoplottabile<-rbind(betweenfamilSMALLgeoplottabile1,setNames(betweenfamilSMALLgeoplottabile2,names(betweenfamilSMALLgeoplottabile1)))
betweenfamilSMALLgeoplottabile$lat=as.numeric(as.character(betweenfamilSMALLgeoplottabile$lat1))
betweenfamilSMALLgeoplottabile$lon=as.numeric(as.character(betweenfamilSMALLgeoplottabile$lon1))
betweenfamilSMALLgeoplottabile$index=as.numeric(as.character(betweenfamilSMALLgeoplottabile$index))
betweenfamilSMALLgeoplottabile<-betweenfamilSMALLgeoplottabile[!is.na(betweenfamilSMALLgeoplottabile$lat),]
betweenfamilSMALLgeoplottabile<-betweenfamilSMALLgeoplottabile[!is.na(betweenfamilSMALLgeoplottabile$percentileFST),]
#betweenfamilSMALLgeoplottabile$percentileFST<-1-betweenfamilSMALLgeoplottabile$percentileFST
betweenfamilSMALLgeoplottabileSHIFTMAP<-MoveDataCoord(betweenfamilSMALLgeoplottabile)
# one map with increasing color connections for increasing percentile
# from percentile 0.2 to 0.01
betweenfamilSMALLgeoplottabileSHIFTMAP2<-betweenfamilSMALLgeoplottabileSHIFTMAP[which(betweenfamilSMALLgeoplottabileSHIFTMAP$percentileFST<0.1),]
betweenfamilSMALLgeoplottabileSHIFTMAP2<-betweenfamilSMALLgeoplottabileSHIFTMAP2[order(betweenfamilSMALLgeoplottabileSHIFTMAP2$percentileFST, decreasing = T),]
# only different L family pairs
betweenfamilSMALLgeoplottabileSHIFTMAP3<-betweenfamilSMALLgeoplottabileSHIFTMAP2[which(betweenfamilSMALLgeoplottabileSHIFTMAP2$FAMILY=="DIVERSE"),]
fig6a<-base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color = "black",
size = 0.5, shape=3)+
geom_path(data=betweenfamilSMALLgeoplottabileSHIFTMAP3, aes(x = lon.1, y = lat.1,
group=index, alpha=percentileFST, size=percentileFST, color=percentileFST))+
scale_alpha(range = c(0.2,0.1))+
scale_size(range = c(0.5,.1))+
scale_colour_gradient2(midpoint=0.1, low="darkred", mid="darkorange",
high="white", name="Percentile FST distribution")+
#ggtitle("weight of mismatches according to FST percentile distribution")+
xlab("") + ylab("") +
guides(alpha=FALSE, size=FALSE)
### Supplementary Figure S6A
ggsave("MapPairMismatchesPercentileDistribution_until10percentile_2022_.pdf", useDingbats=FALSE, width = 15, height = 8, dpi = 300, units = "in")
# ***************************************************
# sensitivity test with different geo thresholds
# Figure S2
### all the close FST distances on a map
base_plot + geom_point(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1),
color = "black",
size = 0.5, shape=3)+
geom_path(data=betweenfamilSMALLgeoplottabileSHIFTMAP2, aes(x = lon.1, y = lat.1,
group=index, alpha=percentileFST, color=percentileFST))+
scale_alpha(range = c(0.2,0.01))+
scale_size(range = c(0.5,.01))+
scale_colour_gradient2(midpoint=0.05, low="purple", mid="yellow",
high="white", name="Percentile FST distribution")+
xlab("") + ylab("") +
guides(alpha=FALSE, size=FALSE)
ggsave("MapPairFSTPercentileDistribution_2022.pdf", useDingbats=FALSE, width = 15, height = 8, dpi = 300, units = "in")
##-------------------------------------
# figure area proportion of linguistically unrelated in close FSTs
# supplementary Fig S6B
##-------------------------------------
#FstListREDinfo_diverse<-FstListREDinfo[which(FstListREDinfo$FAMILY=="DIVERSE"),]
FstListGlotto_infowithinREgion<-FstListinfo[which(FstListinfo$region1==FstListinfo$region2),]
FstListREDinfo_BetweenRegion<-FstListinfo[which(FstListinfo$region1!=FstListinfo$region2),]
numberTotalSmallFST<-c()
numberTotalSmallFSTDIVERSE<-c()
percentageMismatchesinTotalSmallFST<-c()
meltFstGlottoIBD_infoWithPercentageMismatch<-data.frame(row.names = FstListinfo$popslistemp)
for (j in 1:length(percentiles)){
mismatchlist<-c()
percTarget<-percentiles[j]
for (i in 1:5){ # the five macro continents
mismatchlist1<-FstListGlotto_infowithinREgion[which(FstListGlotto_infowithinREgion$region1==medianGEO[i,1]),] # select continent
thresholdtemp<-as.numeric(medianGEO[i,which(colnames(medianGEO)==percTarget)]) # select percentile value fst
mismatchlist1<-mismatchlist1[which(mismatchlist1$FST<= thresholdtemp),]
mismatchlist<-rbind(mismatchlist,mismatchlist1)
}
thresholdtempworld<-as.numeric(medianGEO[6,which(colnames(medianGEO)==percTarget)])
mismatchlistBEtweenregion<-FstListREDinfo_BetweenRegion[which(FstListREDinfo_BetweenRegion$FST<= thresholdtempworld),] # use the target quantile all over the world Fst WORLD
TheMismatches<-rbind(mismatchlist, mismatchlistBEtweenregion)
numberTotalSmallFST[j]<-nrow(TheMismatches) # number of pairs within FST percentile
numberTotalSmallFSTDIVERSE[j]<-length(which(TheMismatches$FAMILY=="DIVERSE")) # number of pairs in mismatch
percentageMismatchesinTotalSmallFST[j]<-length(which(TheMismatches$FAMILY=="DIVERSE"))/nrow(TheMismatches)
meltFstGlottoIBD_infoWithPercentageMismatch[,j]<-NA
meltFstGlottoIBD_infoWithPercentageMismatch[TheMismatches$popslistemp,j]<-percTarget
}
colnames(meltFstGlottoIBD_infoWithPercentageMismatch)<-percentiles
listperpercentileNumberCases<-rbind(numberTotalSmallFST,numberTotalSmallFSTDIVERSE,percentageMismatchesinTotalSmallFST)
colnames(listperpercentileNumberCases)<-percentiles
# count the cases below each percentile
f<-function(x, output){sort(x)[1]}
FstListinfo$percentileFSTdoublecheck<-apply(meltFstGlottoIBD_infoWithPercentageMismatch,
1, f)
plot(FstListinfo$percentileFSTdoublecheck, FstListinfo$percentileFST) # check if the two slightly different method give the same percentiles
plotproportion<-data.frame(percentiles,nrow(FstListinfo)-numberTotalSmallFST,numberTotalSmallFST-numberTotalSmallFSTDIVERSE,numberTotalSmallFSTDIVERSE)
colnames(plotproportion)<-c("percentiles","pairsOutside","pairsInsideSameFamily","pairsInsideDiffFamily")
fig6B<-ggplot(plotproportion,aes(x=as.numeric(percentiles), y=percentageMismatchesinTotalSmallFST, color=as.numeric(percentiles)))+
geom_segment(aes(xend=as.numeric(percentiles), yend=0, color=as.numeric(percentiles)), alpha=0.9)+
geom_line(size=1)+
# geom_area(aes(x=as.numeric(percentiles), y=percentageMismatchesinTotalSmallFST), alpha=0.5, fill="darkorange", color="darkorange")+
#ggtitle("proportion of couples from Diff L Families over number of pairs genetically close - FST percentile distrib")+
labs(x="Percentile global Fst distribution", y="proportion of pairs from Different Language Families")+
#geom_vline(xintercept = 0.2, color="darkorange", size=1)+
scale_colour_gradient2(midpoint=0.2, low="darkred", mid="darkorange",