-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalysisMaMi_2022_clean.R
2240 lines (1701 loc) · 100 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_July.txt", header = T, sep = "\t", as.is=T)
#write.table(perpopRED,"PerpopRED_MaMi2022_July.txt", row.names = F, sep = "\t", quote = F)
# list of pairwise comparisons :
FstListinfo<-read.table("FstListREDinfo_MaMi2022_July.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" ,"#f27059", "#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
source("/Users/chiarabarbieri/Library/Mobile Documents/com~apple~CloudDocs/ggworld.R")
#
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
#******************************************
#*
large_families<-c( "Atlantic-Congo" , "Indo-European", "Sino-Tibetan" , "Mongolic-Khitan",
"Afro-Asiatic", "Turkic" , "Austronesian" , "Uralic") # families above 8 pops
MainFamilies3<-MainFamilies2[which(MainFamilies2$MainFamilies%in%large_families),]
colorchoice3<-MainFamilies3$COLOR
# 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%large_families),],
aes(x = lon.1, y = lat.1, color=glottolog.NAME),
size = 2, alpha=0.5)+
scale_color_manual(values=colorchoice, labels = MainFamilies3$names)
ggsave("WholeGelatoMap_Pacific_LittleCrossesMajorFamilies_largerFig2_2022_SecondRevision.pdf", useDingbats=FALSE, width = 8.5, height = 5, dpi = 300, units = "in")
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%MainFamilies),],
aes(x = lon.1, y = lat.1, color=glottolog.NAME),
size = 2, alpha=0.5)+
geom_text(data = perpopREDSHIFTMAP,
aes(x = lon.1, y = lat.1, label=PopName),
color="black",size=0.3)+
scale_color_manual(values=colorchoice, labels = MainFamilies2$names)
ggsave("WholeGelatoMap_Pacific_PopNamesMajorFamilies_largerFig2_2022_SecondRevision.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(perpopRED$EnclavesMismatch)
### table to visualize how many cases
#************************
# July 2022
MATCH MISMATCH secondaryMISMATCH_FSTzeroDiffFamily ZeroSameFamilyNeighbors
0.130982368 0.068010076 0.005037783 0.050377834
MATCH MISMATCH secondaryMISMATCH_FSTzeroDiffFamily ZeroSameFamilyNeighbors
52 27 2 20
#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
#*
perpopRED$difference<-perpopRED$closeFstSameFamily-perpopRED$closeFstDIFFFamily
length(which(perpopRED$difference>0))/(nrow(perpopRED)-length(which(is.na(perpopRED$difference)))) # only for the comparisons for which i have a same family FST
[1] 0.1962865 ## July 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_july.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)){ # it takes a few minutes to run
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
## July
> length(ListMisaligned)/length(which(!is.na(perpopRED$MedDiff)))
[1] 0.2025316
> 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_July.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,method = "gam")+
geom_smooth(aes(color=SameFamily),se=F,method = "gam")+
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(expression(Linearized~F["ST"]))
ggsave("Fig2_smoothregressionPlotwithLinear_highlightMismatches_2022_October.pdf", useDingbats=FALSE, height = 6, width = 10)
ggsave("Fig2_smoothregressionPlotwithLinear_highlightMismatches_2022October.png", height = 5, width = 10)
#*********************************************
# JACKKNIFE REVIEWER 1
#
# idea from Damian: visualize the smooth regressions for each round of jackknife
#*********************************************
#*
library(mgcv)
FAMmatrixmelt<-c()
BlocktempTOTAL<-c()
for (i in 1:length(FAM)){
# for (i in 4:5){
target<-FAM[i]
blocktemp<-family_distances2[which(family_distances2$Family_plot==target),]
blocktemp<-blocktemp[!is.na(blocktemp$GEOdist),] # eliminate the rows for which i do not have geo coordinated in one of the pops
popfam<-blocktemp$Pop1[which(blocktemp$family1==target)] # all the populations included in the comparisons
popfam<-unique(popfam)
print(length(popfam))
RatioResidualsWithin_JACK<-c()
InterceptWithin_JACK<-c()
InterceptBEtween_JACK<-c()
for (k in 1:length(popfam)){ # here jackknife each population out
blocktemp_JACK<-blocktemp[-c(which(blocktemp$Pop1==popfam[k]),which(blocktemp$Pop2==popfam[k])),]
mod2gam <- gam(FstLinear ~ s(GEOdist),method = "REML", data=blocktemp_JACK) # the general GAM model
blocktemp_JACK$residuals<-mod2gam$residuals # add the residuals for each pair of pops according to the general simple model
tempWithin<-blocktemp_JACK[which(blocktemp_JACK$SameFamily=="YES"),] # only inside the same Family
tempbetween<-blocktemp_JACK[which(blocktemp_JACK$SameFamily=="NO"),] # outside the same Family
RatioResidualsWithin_JACK[k]=round(length(which(tempWithin$residuals<0))/nrow(tempWithin),digits = 2)
withinGAM <- gam(FstLinear ~ s(GEOdist),method = "REML", data=tempWithin) # GAM model within family
betweenGAM <- gam(FstLinear ~ s(GEOdist),method = "REML", data=tempbetween) # GAM model between family
InterceptWithin_JACK[k]<-withinGAM$coefficients[1]
InterceptBEtween_JACK[k]<-betweenGAM$coefficients[1]
blocktemp_JACKkeep<-blocktemp_JACK[,which(colnames(blocktemp_JACK)%in%c("FstLinear","popslistemp","GEOdist","SameFamily","residuals","FAMILY"))]
blocktemp_JACKkeep$PopJack<-popfam[k]
blocktemp_JACKkeep$FAM<-target
BlocktempTOTAL<-rbind(BlocktempTOTAL,blocktemp_JACKkeep)
}
FAMmatrixTEMP<-cbind( RatioResidualsWithin_JACK, InterceptWithin_JACK,InterceptBEtween_JACK,popfam) # add the name of the pop that got kicked out at every round of jackknife
FAMmatrixTEMP<-as.data.frame(FAMmatrixTEMP)
FAMmatrixTEMP$FAM=target
FAMmatrixTEMPmelt<-melt(FAMmatrixTEMP, id.vars=c("FAM","popfam" ))
FAMmatrixmelt<-rbind(FAMmatrixmelt,FAMmatrixTEMPmelt)
}
##############
write.csv(BlocktempTOTAL,"BlocktempTOTAL.csv")
write.csv(FAMmatrixmelt,"FAMmatrixmelt.csv")
##############
BlocktempTOTAL<-read.csv("BlocktempTOTAL.csv")
FAMmatrixmelt<-read.csv("FAMmatrixmelt.csv")
## plot all the smooth regressions, Damian's idea
BlocktempTOTAL$FAM<-factor(BlocktempTOTAL$FAM, levels=FAM) # force the order when doing facet wrap in the figure
a<-ggplot(data=BlocktempTOTAL,aes(y=FstLinear,x=GEOdist, group=PopJack))
#a+geom_smooth(aes(color=SameFamily),se=F,method = "gam")
sxa<-a+
# geom_smooth(aes(group=SameFamily),se=F, color="white", size=2.5,method = "gam")+
geom_line(aes(group=interaction(PopJack, SameFamily), color=SameFamily),se=F,stat="smooth", alpha=0.3)+
# geom_smooth(color="#FBB13C",linetype = "dotdash",alpha=0.4, size=1, se=F,method = lm)+
facet_wrap(~FAM,
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))+
theme(legend.position = "bottom", axis.text=element_text(size=7),strip.text = element_text(size = 12))+
xlab("Geographic Distance - km") +
ylab(expression(Linearized~F["ST"]))
ggsave("Fig2_smoothregressionPlot_Jackknife.pdf", useDingbats=FALSE, height = 5, width = 10)
#**********************************************************
#### COOKS DISTANCES
## script from Balthasar Bickel
library(tidyverse)
library(mgcv)
library(ggrepel)
# Read the distances and clean up:
# family_distances <- read.csv('family_distances.csv') %>%
family_distances <- family_distances %>%
select(Fst = FstLinear,
distance = GEOdist,
SameFamily,
Family = Family_plot,
type = listSingleCasesPOP1,
pair = popslistemp)
# Baseplot from paper:
ggplot(family_distances,
aes(y = Fst, x = distance,
color = SameFamily,
fill = SameFamily))+
geom_point(aes(size=SameFamily), shape=21, alpha=0.3, stroke=0)+
# geom_point(aes(shape=listSingleCasesPOP1), alpha=0.3, size=3, stroke=0.3)+
geom_smooth(se = T, method = "gam") +
facet_wrap(~Family, scales="free", ncol = 4) +
theme_minimal()+
scale_y_sqrt()+
scale_fill_manual(values=c("#7FDBEB","#E45B5B"))+
scale_color_manual(values=c("#198B9F","#BA1E1E"))+
# 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(expression(Linearized~F["ST"]))
# Fit the GAMs per family and condition:
gams <- group_by(family_distances, Family, SameFamily) %>%
nest() %>%
mutate(model = map(data, function(d) {
gam(Fst ~ s(distance, bs = 'cs'),
data = d)
}
))
# Extract residuals and Cook's D, curtesy of Erik Ringen:
cooks_gam <- function(model) {
eps_sq <- mgcv::residuals.gam(model, type = "scaled.pearson")^2
h <- mgcv::influence.gam(model)
k <- sum(model$edf) # effective degrees of freedom as approx for num params
D <- (eps_sq / (k + 1)) * (h / (1 - h))
return(D)
}
gam_diagnostics.df <- gams %>%
mutate(resids = map(model, residuals),
cooks = map(model, cooks_gam),
# and the subset-specific threshold following the standard 4/N rule:
cook4_threshold = 4/map_dbl(data, nrow)) %>%
unnest(c(data, resids, cooks)) %>%
select(-model)
# Sensitivity analysis: original GAMS without highly influential pairs
######################################################################
# Counts and proportions:
gam_diagnostics.df$cook4_threshold<-as.numeric(gam_diagnostics.df$cook4_threshold)
group_by(gam_diagnostics.df, Family, SameFamily) %>%
summarize(N_influential = sum(cooks > cook4_threshold),
Prop_Influential = round(mean(cooks > cook4_threshold),2)) %>%
arrange(-Prop_Influential)
# data without the highly influential pairs in each family and condition:
family_distances_trimmed <-
inner_join(family_distances, gam_diagnostics.df) %>%
group_by(Family, SameFamily) %>%
filter(cooks < cook4_threshold)
family_distances_trimmed$Family<-factor(family_distances_trimmed$Family, levels=FAM) # force the order when doing facet wrap in the figure
SXB<-ggplot(family_distances_trimmed,
aes(y = Fst, x = distance,
color = SameFamily,
fill = SameFamily))+
geom_point(aes(size=SameFamily), shape=21, alpha=0.3, stroke=0)+
# geom_point(aes(shape=listSingleCasesPOP1), alpha=0.3, size=3, stroke=0.3)+
geom_smooth(se = T, method = "gam") +
facet_wrap(~Family, scales="free", ncol = 4) +
theme_minimal()+
scale_y_sqrt()+
scale_fill_manual(values=c("#7FDBEB","#E45B5B"))+
scale_color_manual(values=c(blueNO,darkredYES))+
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(expression(Linearized~F["ST"]))
#**********************************************************
# end of script from Balthasar
# plot the two parts of jackknife figure S12
library(patchwork)
patchwork<-sxa / SXB
patchwork + plot_annotation(tag_levels = 'A')
ggsave("FIG_S12_jackknife_v3.pdf", height = 16, width = 17, useDingbats=FALSE)
# ***************************************************
#### OTHER 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"),]
perpopREDfamilynoDrift<-perpopREDfamilynoDrift[!is.na(perpopREDfamilynoDrift$MedDiff),] # exclude the pops for which i cannot calculate the Median Difference betwen Fst betwen-within
MainFamiliesTEMP<-MainFamilies[which(MainFamilies%in%perpopREDfamilynoDrift$glottolog.NAME)]
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 = MainFamilies2$COLOR[which(MainFamilies2$MainFamilies%in%MainFamiliesTEMP)])+
theme_light()+ xlab("Difference Median FST between - within") +
ylab("Confidence Interval median difference") +
geom_vline(xintercept=0)+
scale_y_log10()+
facet_wrap(~MainFamilies,ncol=5)
# ggsave("DifferenceMedianLogScaleMajorLangFam_July.pdf", height = 6, 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_July.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_July.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)
valuestop
[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(aes(x=threshold,y=GeoCloseUnrelated1),trim=FALSE, alpha=0.4)+
stat_summary(fun="median",
geom="point", 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_July.pdf", height = 12, width = 10, useDingbats=FALSE)
# ggsave("Fig_s03_evaluateIncidenceLanguageFamilies2022.png", height = 12, width = 10, dpi = 150)
##### analysis of divergence time
#******************************************
### Figure S1. compare different methods to calculate genetic divergence times