This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
forked from christiandamsgaard/Retinaevolution
/
Retina_evolution.R
2171 lines (1752 loc) · 103 KB
/
Retina_evolution.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
####################################################
### R-script for retinal oxygen supply evolution ###
####################################################
###################
### Import data ###
###################
# Load Packages
Packages <-c("phyloseq","splitstackshape","RColorBrewer","ggsignif","ape","cowplot","readxl","geiger","phytools","ggplot2","nlme","coda","gridExtra","grid","phangorn")
lapply(Packages, library, character.only = TRUE)
# Set working directory, import data and tree
setwd("/Users/christiandamsgaard/Dropbox/Projects/Piscine_retina_elife/")
data <-as.data.frame(read_excel("./Data/data.xlsx", sheet = "Sheet1"))
data$Species <-sub("\\ ", "_", data$Species) # Separate genus and species by underscore
data$Root <-data$Root*100 # Root effect in percent
data$SAD<-data$`Surface area [mm2]`
tax <- read.csv("./Data/PFC_short_classification.csv") # Load the fish taxonomy
tax$genus.species <-sub("\\ ", "_", tax$genus.species) # Separate genus and species by underscore
tax[which(tax$genus.species=="Astyanax_mexicanus"),]<-c("Characiformes","Characidae","Astyanax","Astyanax_mexicanus_Surface")
tax[which(tax$genus.species=="Astyanax_jordani"),]<-c("Characiformes","Characidae","Astyanax","Astyanax_mexicanus_Chica")
tax2<-as.data.frame(rbind(c("Characiformes","Characidae","Astyanax","Astyanax_mexicanus_Micos"),
c("Characiformes","Characidae","Astyanax","Astyanax_mexicanus_Pachon")))
colnames(tax2) <-colnames(tax)
tax <-rbind(tax2,tax)
data$Order <-as.character(tax$order[match(data$Species,tax$genus.species)])
####################################################
### Table S1: De novo and literature data points ###
####################################################
fish<-data[which(data$Class=="Actinopterygii"),] # Whole data set, excluding all non-ray-finned fishes
not.fish<-data[which(data$Class!="Actinopterygii"),] # Whole data set, excluding all ray-finned fishes
T1 <-matrix(ncol = 9,nrow = 10) # Table 1
colnames(T1) <-c("De novo fishes","De novo others","De novo all",
"Literature fishes","Literature others","Literature all",
"Total fishes","Total others","Total all")
rownames(T1) <-c("Root effect magnitude","Retinal Thickness","Retinal layers thicknesses",
"Choroid rete mirabile surface area","Magnitude of pre-retinal capillarization",
"Presence of choroid rete mirabile","Presence of intra-retinal capillaries","Presence of pre-retinal capillaries",
"Eye mass","Total")
## ROOT EFFECT
length(which(fish$Root_ref[!is.na(fish$Root_ref)]=="This study")) ->T1[1,1] # De novo ray-finned fishes
length(which(not.fish$Root_ref[!is.na(not.fish$Root_ref)]=="This study")) ->T1[1,2] # De novo other vertebrates
length(which(data$Root_ref[!is.na(data$Root_ref)]=="This study")) ->T1[1,3] # De novo all
length(which(fish$Root_ref[!is.na(fish$Root_ref)]!="This study")) ->T1[1,4] # Literature ray-finned fishes
length(which(not.fish$Root_ref[!is.na(not.fish$Root_ref)]!="This study")) ->T1[1,5] # Literature other vertebrates
length(which(data$Root_ref[!is.na(data$Root_ref)]!="This study")) ->T1[1,6] # Literature all
length(fish$Root_ref[!is.na(fish$Root_ref)]) ->T1[1,7] # Total ray-finned fishes
length(not.fish$Root_ref[!is.na(not.fish$Root_ref)]) ->T1[1,8] # Total other vertebrates
length(data$Root_ref[!is.na(data$Root_ref)]) ->T1[1,9] # Total all
## MAXIMAL RETINAL THICKNESS
length(which(fish$Thickness_ref[!is.na(fish$Thickness_ref)]=="This study")) ->T1[2,1] # De novo ray-finned fishes
length(which(not.fish$Thickness_ref[!is.na(not.fish$Thickness_ref)]=="This study")) ->T1[2,2] # De novo other vertebrates
length(which(data$Thickness_ref[!is.na(data$Thickness_ref)]=="This study")) ->T1[2,3] # De novo all
length(which(fish$Thickness_ref[!is.na(fish$Thickness_ref)]!="This study")) ->T1[2,4] # Literature ray-finned fishes
length(which(not.fish$Thickness_ref[!is.na(not.fish$Thickness_ref)]!="This study")) ->T1[2,5] # Literature other vertebrates
length(which(data$Thickness_ref[!is.na(data$Thickness_ref)]!="This study")) ->T1[2,6] # Literature all
length(fish$Thickness_ref[!is.na(fish$Thickness_ref)]) ->T1[2,7] # Total ray-finned fishes
length(not.fish$Thickness_ref[!is.na(not.fish$Thickness_ref)]) ->T1[2,8] # Total other vertebrates
length(data$Thickness_ref[!is.na(data$Thickness_ref)]) ->T1[2,9] # Total all
## RETINAL LAYER THICKNESSES
length(fish$NFL[!is.na(fish$NFL)]) ->T1[3,c(1,7)]# De novo ray-finned fishes
length(not.fish$NFL[!is.na(not.fish$NFL!=0)]) ->T1[3,c(2,8)]# De novo other vertebrates
length(data$NFL[!is.na(data$NFL!=0)]) ->T1[3,c(3,9)]# De novo all
0 ->T1[3,4]->T1[3,5]->T1[3,6]
## CHOROID RETE MIRABILE SURFACE AREA
length(fish$SAD[!is.na(fish$SAD)]) ->T1[4,c(1,7)]# De novo ray-finned fishes
length(not.fish$SAD[!is.na(not.fish$SAD!=0)]) ->T1[4,c(2,8)]# De novo other vertebrates
length(data$SAD[!is.na(data$SAD!=0)]) ->T1[4,c(3,9)]# De novo all
0 ->T1[4,4]->T1[4,5]->T1[4,6]
## MAGNITUDE OF PRE-RETINAL CAPILLARIZATION
length(which(fish$PRC[!is.na(fish$PRC)]!=0)) ->T1[5,c(1,7)]# De novo ray-finned fishes
length(which(not.fish$PRC[!is.na(not.fish$PRC)]!=0)) ->T1[5,c(2,8)]# De novo other vertebrates
length(which(data$PRC[!is.na(data$PRC)]!=0)) ->T1[5,c(3,9)]# De novo all
0 ->T1[5,4]->T1[5,5]->T1[5,6]
## PRESENCE OF CHOROID RETE MIRABILE
length(which(fish$CRM_ref[!is.na(fish$CRM_ref)]=="This study")) ->T1[6,1] # De novo ray-finned fishes
length(which(not.fish$CRM_ref[!is.na(not.fish$CRM_ref)]=="This study")) ->T1[6,2] # De novo other vertebrates
length(which(data$CRM_ref[!is.na(data$CRM_ref)]=="This study")) ->T1[6,3] # De novo all
length(which(fish$CRM_ref[!is.na(fish$CRM_ref)]!="This study")) ->T1[6,4] # Literature ray-finned fishes
length(which(not.fish$CRM_ref[!is.na(not.fish$CRM_ref)]!="This study")) ->T1[6,5] # Literature other vertebrates
length(which(data$CRM_ref[!is.na(data$CRM_ref)]!="This study")) ->T1[6,6] # Literature all
length(fish$CRM_ref[!is.na(fish$CRM_ref)]) ->T1[6,7] # Total ray-finned fishes
length(not.fish$CRM_ref[!is.na(not.fish$CRM_ref)]) ->T1[6,8] # Total other vertebrates
length(data$CRM_ref[!is.na(data$CRM_ref)]) ->T1[6,9] # Total all
## INTRA-RETINAL CAPILLARIES
length(which(fish$IRC_ref[!is.na(fish$IRC_ref)]=="This study")) ->T1[7,1] # De novo ray-finned fishes
length(which(not.fish$IRC_ref[!is.na(not.fish$IRC_ref)]=="This study")) ->T1[7,2] # De novo other vertebrates
length(which(data$IRC_ref[!is.na(data$IRC_ref)]=="This study")) ->T1[7,3] # De novo all
length(which(fish$IRC_ref[!is.na(fish$IRC_ref)]!="This study")) ->T1[7,4] # Literature ray-finned fishes
length(which(not.fish$IRC_ref[!is.na(not.fish$IRC_ref)]!="This study")) ->T1[7,5] # Literature other vertebrates
length(which(data$IRC_ref[!is.na(data$IRC_ref)]!="This study")) ->T1[7,6] # Literature all
length(fish$IRC_ref[!is.na(fish$IRC_ref)]) ->T1[7,7] # Total ray-finned fishes
length(not.fish$IRC_ref[!is.na(not.fish$IRC_ref)]) ->T1[7,8] # Total other vertebrates
length(data$IRC_ref[!is.na(data$IRC_ref)]) ->T1[7,9] # Total all
## PRE-RETINAL CAPILLARISATION
length(which(fish$PRC_ref[!is.na(fish$PRC_ref)]=="This study")) ->T1[8,1] # De novo ray-finned fishes
length(which(not.fish$PRC_ref[!is.na(not.fish$PRC_ref)]=="This study")) ->T1[8,2] # De novo other vertebrates
length(which(data$PRC_ref[!is.na(data$PRC_ref)]=="This study")) ->T1[8,3] # De novo all
length(which(fish$PRC_ref[!is.na(fish$PRC_ref)]!="This study")) ->T1[8,4] # Literature ray-finned fishes
length(which(not.fish$PRC_ref[!is.na(not.fish$PRC_ref)]!="This study")) ->T1[8,5] # Literature other vertebrates
length(which(data$PRC_ref[!is.na(data$PRC_ref)]!="This study")) ->T1[8,6] # Literature all
length(fish$PRC_ref[!is.na(fish$PRC_ref)]) ->T1[8,7] # Total ray-finned fishes
length(not.fish$PRC_ref[!is.na(not.fish$PRC_ref)]) ->T1[8,8] # Total other vertebrates
length(data$PRC_ref[!is.na(data$PRC_ref)]) ->T1[8,9] # Total all
## EYE MASS
length(which(fish$EM_ref[!is.na(fish$EM_ref)]=="This study")) ->T1[9,1] # De novo ray-finned fishes
length(which(not.fish$EM_ref[!is.na(not.fish$EM_ref)]=="This study")) ->T1[9,2] # De novo other vertebrates
length(which(data$EM_ref[!is.na(data$EM_ref)]=="This study")) ->T1[9,3] # De novo all
length(which(fish$EM_ref[!is.na(fish$EM_ref)]!="This study")) ->T1[9,4] # Literature ray-finned fishes
length(which(not.fish$EM_ref[!is.na(not.fish$EM_ref)]!="This study")) ->T1[9,5] # Literature other vertebrates
length(which(data$EM_ref[!is.na(data$EM_ref)]!="This study")) ->T1[9,6] # Literature all
length(fish$EM_ref[!is.na(fish$EM_ref)]) ->T1[9,7] # Total ray-finned fishes
length(not.fish$EM_ref[!is.na(not.fish$EM_ref)]) ->T1[9,8] # Total other vertebrates
length(data$EM_ref[!is.na(data$EM_ref)]) ->T1[9,9] # Total all
## TOTAL NUMBER OF SPECIES IN DE NOVO DATA POINTS
# Ray-finned fishes
length(
unique(c(which(fish$EM_ref=="This study"),
which(fish$SAD>0),
which(fish$PRC>0),
which(fish$GCL>0),
which(fish$CRM_ref=="This study"),
which(fish$IRC_ref=="This study"),
which(fish$Root_ref=="This study"),
which(fish$Thickness_ref=="This study")
))
)->T1[10,1]
# All vertebrates except ray-finned fishes
length(
unique(c(which(not.fish$EM_ref=="This study"),
which(not.fish$SAD>0),
which(not.fish$PRC>0),
which(not.fish$GCL>0),
which(not.fish$CRM_ref=="This study"),
which(not.fish$IRC_ref=="This study"),
which(not.fish$Root_ref=="This study"),
which(not.fish$Thickness_ref=="This study")
))
)->T1[10,2]
# Total number of species
# All vertebrates except ray-finned fishes
length(
unique(c(which(data$EM_ref=="This study"),
which(data$SAD>0),
which(data$PRC>0),
which(data$GCL>0),
which(data$CRM_ref=="This study"),
which(data$IRC_ref=="This study"),
which(data$Root_ref=="This study"),
which(data$Thickness_ref=="This study")
))
)->T1[10,3]
## TOTAL NUMBER OF SPECIES WITH LITERATURE DATA POINTS
# Ray-finned fishes
length(
unique(c(which(fish$EM_ref!="This study"),
which(fish$CRM_ref!="This study"),
which(fish$IRC_ref!="This study"),
which(fish$Root_ref!="This study"),
which(fish$Thickness_ref!="This study")
))
)->T1[10,4]
# All vertebrates except ray-finned fishes
length(
unique(c(which(not.fish$EM_ref!="This study"),
which(not.fish$CRM_ref!="This study"),
which(not.fish$IRC_ref!="This study"),
which(not.fish$Root_ref!="This study"),
which(not.fish$Thickness_ref!="This study")
))
)->T1[10,5]
# Total number of species
length(
unique(c(which(data$EM_ref!="This study"),
which(data$CRM_ref!="This study"),
which(data$IRC_ref!="This study"),
which(data$Root_ref!="This study"),
which(data$Thickness_ref!="This study")
))
)->T1[10,6]
## TOTAL NUMBER OF SPECIES WITH WHOLE DATA SET
length(fish$Species)->T1[10,7]
length(not.fish$Species)->T1[10,8]
length(data$Species)->T1[10,9]
T1 # Table S1
# How many species were used to identify capillaries and Root effect in our data set?
length(data$Species[unique(c(which(data$CRM_ref=="This study"),
which(data$IRC_ref=="This study"),
which(data$Root_ref=="This study"),
which(data$Thickness_ref=="This study")))])
########################
### Custom functions ###
########################
# Prune phylogeny
keep.tip <-function(tree,tip) drop.tip(tree,setdiff(tree$tip.label,tip))
# Export 3D coordinates
coordinate <-function(tree,ace,tip) {
tree_ace<-drop.tip(tree,tree$tip.label[-match(names(tip),tree$tip.label)])
fit<-ace
hor <- as.data.frame(tree_layout(tree_ace)$edgeDT)
ver <- as.data.frame(tree_layout(tree_ace)$vertDT)
H<-matrix(nrow = length(hor$V1), ncol = 6)
for (i in 1:length(H[,1])) { H[i,1] <- hor$xleft[i] }
for (i in 1:length(H[,1])) { H[i,2] <- hor$xright[i] }
for (i in 1:length(H[,1])) { H[i,3] <- hor$y[i] }
for (i in 1:length(H[,1])) { H[i,4] <- hor$y[i] }
for (i in 1:length(H[,1])) { H[i,5] <- unname(fit[match(hor$V1[i],names(fit))]) }
for (i in 1:length(H[,1])) { H[i,6] <- ifelse(!is.na(hor$OTU[i]),
unname(tip[match(hor$OTU[i],names(tip))]),
unname(fit[match(hor$V2[i],names(fit))]))}
V<-matrix(nrow = length(ver$V1), ncol = 6)
for (i in 1:length(V[,1])) { V[i,1] <- tree_layout(tree_ace)$vertDT$x[i] }
for (i in 1:length(V[,1])) { V[i,2] <- tree_layout(tree_ace)$vertDT$x[i] }
for (i in 1:length(V[,1])) { V[i,3] <- tree_layout(tree_ace)$vertDT$vmin[i] }
for (i in 1:length(V[,1])) { V[i,4] <- tree_layout(tree_ace)$vertDT$vmax[i] }
for (i in 1:length(V[,1])) { V[i,5] <- unname(fit[match(ver$V1[i],names(fit))]) }
for (i in 1:length(V[,1])) { V[i,6] <- unname(fit[match(ver$V1[i],names(fit))]) }
D<-as.data.frame(rbind(H,V));colnames(D) <- c("x1","x2","y1","y2","z1","z2")
return(D)
}
# Pick model of discrete character evolution using likelihood ratio test
LRT.pick.model <- function (tree,x) {
ace_ER <- fitDiscrete(tree, x, type="discrete", model = "ER")
ace_SYM <-fitDiscrete(tree, x, type="discrete", model = "SYM")
ace_ARD <-fitDiscrete(tree, x, type="discrete", model = "ARD")
S <-c(ace_ER$opt$lnL,ace_ER$opt$k,ace_ER$opt$aicc,
ace_SYM$opt$lnL,ace_SYM$opt$k,ace_SYM$opt$aicc,
ace_ARD$opt$lnL,ace_ARD$opt$k,ace_ARD$opt$aicc,
pchisq(abs(2*(ace_ER$opt$lnL-ace_ARD$opt$lnL)) ,
ace_ARD$opt$k-ace_ER$opt$k, lower.tail=FALSE),
pchisq(abs(2*(ace_ER$opt$lnL-ace_SYM$opt$lnL)) ,
ace_SYM$opt$k-ace_ER$opt$k, lower.tail=FALSE) ,
pchisq(abs(2*(ace_SYM$opt$lnL-ace_ARD$opt$lnL)) ,
ace_ARD$opt$k-ace_SYM$opt$k, lower.tail=FALSE))
names(S)<-c("lnL_ER","df_ER","AICc_ER","lnL_SYM","df_SYM","AICc_SYM","lnL_ARD","df_ARD","AICc_ARD",
"p_ERvsARD","p_ERvsSYM","p_SYMvsARD")
ARDvsER <-ifelse(S[10]<=0.05 & S[9]<S[3],"ARD", "ER") #ARD vs ER
SYMvsER <-ifelse(S[11]<=0.05 & S[6]<S[3],"SYM", "ER") #SYM vs ER
ARDvsSYM<-ifelse(S[12]<=0.05 & S[9]<S[6],"ARD","SYM") #ARD vs SYM
model<-ifelse(test = ARDvsSYM=="SYM",yes = SYMvsER,no = ARDvsER)
model<-unname(model)
return(c(model,S))
}
# Calculate divergence times
divergencetimes <- function (phy,x,ACE) {
hor <- as.data.frame(tree_layout(phy)$edgeDT)
H<-matrix(nrow = length(hor$V1), ncol = 7)
for (i in 1:length(H[,1])) { H[i,1] <- max(hor$xright)-hor$xleft[i] } # branch in phylogeny goes from this age
for (i in 1:length(H[,1])) { H[i,2] <- max(hor$xright)-hor$xright[i] } # to this age
for (i in 1:length(H[,1])) { H[i,4] <- ACE[match(hor$V1[i],row.names(ACE))]} # Posterior probability at first point
for (i in 1:length(H[,1])) { H[i,5] <- ACE[match(hor$V2[i],row.names(ACE))]} # Posterior probability at second point
for (i in 1:length(H[,1])) { H[i,5] <- ifelse(is.na(ACE[match(hor$V2[i],row.names(ACE))])==T,
unname(ifelse(x[match(phy$tip[hor$V2[i]],names(x))]==0,1,0)),
H[i,5])} # Posterior probability at second point
for (i in 1:length(H[,1])) { H[i,6] <- ifelse(H[i,4]<0.5&H[i,5]>0.5,
(0.5-lm(c(H[i,4],H[i,5])~c(H[i,1],H[i,2]))$coefficients[1])/lm(c(H[i,4],H[i,5])~c(H[i,1],H[i,2]))$coefficients[2],
NA)}
for (i in 1:length(H[,1])) { H[i,7] <- ifelse(H[i,4]>0.5&H[i,5]<0.5,
(0.5-lm(c(H[i,4],H[i,5])~c(H[i,1],H[i,2]))$coefficients[1])/lm(c(H[i,4],H[i,5])~c(H[i,1],H[i,2]))$coefficients[2],
NA)}
wa<-H[,6];wa<-wa[!is.na(wa)]
aw<-H[,7];aw<-aw[!is.na(aw)]
M<-matrix(ncol = 2,nrow = (length(aw)+length(wa)))
for (i in 1:length(wa)) { M[i,1]<-"Loss"}
for (i in 1:length(aw)) { j=i+length(wa); M[j,1]<-"Gain"}
for (i in 1:length(wa)) { M[i,2]<-wa[i]}
for (i in 1:length(aw)) { j=i+length(wa); M[j,2]<-aw[i]}
df<-as.data.frame(M)
colnames(df)<-c("mode","time")
df$time<-as.numeric(as.character(df$time))
# Add label with time of transition to density map
k<-cbind(hor$V1[as.numeric(row.names((subset(as.data.frame(H),is.na(H[,6])==F))))],
hor$V2[as.numeric(row.names((subset(as.data.frame(H),is.na(H[,6])==F))))]);k
l<-cbind(hor$V1[as.numeric(row.names((subset(as.data.frame(H),is.na(H[,7])==F))))],
hor$V2[as.numeric(row.names((subset(as.data.frame(H),is.na(H[,7])==F))))]);l
g<-as.data.frame(phy$edge)
if (length(k[,2])>0) {
wa.edges<-matrix(ncol=1,nrow=length(k[,2]))
wa.nodes<-k[,2]
for (i in 1:length(k[,1])) {
wa.edges[i]<-rownames(subset(g,g$V1==k[i,1]&g$V2==k[i,2]))
}
}
if (length(l[,2])>0) {
aw.edges<-matrix(ncol=1,nrow=length(l[,2]))
aw.nodes<-l[,2]
for (i in 1:length(l[,1])) {
aw.edges[i]<-rownames(subset(g,g$V1==l[i,1]&g$V2==l[i,2]))
}}
df<-cbind(c(wa.edges,aw.edges),c(wa.nodes,aw.nodes),df)
colnames(df)[1:2]<-c("edge","node")
return(df)
}
# Set color theme
cols <- brewer.pal(8,"Dark2")
########################################
### Build the phylogenetic tree data ###
########################################
# Import phylogenetic tree of Ray-finned fishes
tree <-read.tree("/Users/christiandamsgaard/Dropbox/Projects/Air-breathing review_full/mcc.nexus") # MCC tree from Rabosky, download from github page
tree <-drop.tip(tree,c("Lepidosiren_paradoxa","Neoceratodus_forsteri","Latimeria_chalumnae","Protopterus_aethiopicus_annectens"))
hor <-as.data.frame(tree_layout(tree)$edgeDT);
actin_crown <-max(max(hor$xright)-hor$xleft)
tree$tip.label[which(tree$tip.label=="Astyanax_mexicanus")]<-"Astyanax_mexicanus_Surface"
tree$tip.label[which(tree$tip.label=="Astyanax_jordani")]<-"Astyanax_mexicanus_Chica"
# Add Astyanax mexicanus Pachon to the tree
tree <-bind.tip(tree = tree,
tip.label = "Astyanax_mexicanus_Pachon",
edge.length = 3,
where = match("Astyanax_mexicanus_Surface",
tree$tip.label,
nomatch = NA_integer_,
incomparables = NULL),
position = 3)
# Add Astyanax mexicanus Micos to the tree
tree <-bind.tip(tree = tree,
tip.label = "Astyanax_mexicanus_Micos",
edge.length = 0.03,
where = match("Astyanax_mexicanus_Surface",tree$tip.label,
nomatch = NA_integer_,
incomparables = NULL),
position = 0.03)
# Load sarcopterygean tree
sarc <-read.tree(text = "(((((((Pseudemys_scripta:44.1,Clemmys_guttata:44.1):74.5,(Chelydra_serpentina:113.1,Chelonia_mydas:113.1):5.5):145.9,(((((Puffinus_griseus:66.7,Larus_philadelphia:66.7):2.5,Aquila_rapax:69.2):0,(Streptopelia_capicola:39.1,Columba_livia:39.1):30.1):19.4,Gallus_gallus:88.6):150.5,(Crocodylus_acutus:33,Alligator_mississippiensis:33):206):25.5):35.3,((((((Thamnophis_melanogaster:35.5,Natrix_natrix:35.5):17.5,Coluber_constrictor:53):8.4,Agkistrodon_piscivorus:61.4):31.3,((Python_regius:16.7,Python_molurus:16.7):51,Boa_constrictor:67.7):25):91.9,((Iguana_iguana:26.7,Amblyrhynchus_cristatus:26.7):64.6,Phrynosoma_cornutum:91.3):93.3):17.5,Tarentola_mauritanica:202.1):97.7):29.3,(((((((Rattus_norvegicus:11.8,Mus_musculus:11.8):10,Microtus_pennsylvanicus:21.8):45.2,(Octodon_degus:38.1,Cavia_cutleri:38.1):28.9):10.1,Oryctolagus_cuniculus:77.1):4.5,((Macaca_mulatta:21.1,Homo_sapiens:21.1):10.2,Aotus_trivirgatus:31.3):50.3):8,((Sus_scrofa:81.2,Felis_domesticus:81.2):0,(Pteropus_vampyrus:65.6,Desmodus_rotundus:65.6):15.6):8.4):97.1,(Trichosurus_vulpecula:67.7,Dasyurus_viverrinus:67.7):119):142.4):40.2,(((((Rana_temporaria:50.6,Rana_catesbeiana:50.6):112.8,Bufo_americanus:163.4):54.1,Xenopus_laevis:217.5):5.7,(Bombinator_pachypus:199.4,Alytes_obstetricans:199.4):23.8):57.1,Ambystoma_mexicanum:280.3):89):41.4,((Protopterus_annectens:103.2,Lepidosiren_paradoxa:103.2):138.6,Neoceratodus_forsteri:241.8):168.9);")
plot(sarc)
edgelabels(sarc$edge.length,frame = "none")
plot(mamm)
edgelabels(mamm$edge.length,frame = "none")
# Add Latimeria_chalumnae to the sarcopterygean tree
sarc <-bind.tip(tree = sarc,
tip.label = "Latimeria_chalumnae",
edge.length = 409.4,
where = 94,
position = 167.6)
# Add mammals
mamm <-read.nexus(file = "./Data/composite mammal phylogeny.nex")
mamm$root.edge<-113.5242
mamm$edge.length<-mamm$edge.length*100
sarc_m<-drop.tip(sarc,data$Species[which(data$Class=="Mammalia")])
sarc<-bind.tree(sarc_m,mamm,where = 37,position = 29.3)
write.tree(sarc)
sarc<-read.tree(text = "(((((((Pseudemys_scripta:44.1,Clemmys_guttata:44.1):74.5,(Chelydra_serpentina:113.1,Chelonia_mydas:113.1):5.5):145.9,(((((Puffinus_griseus:66.7,Larus_philadelphia:66.7):2.5,Aquila_rapax:69.2):0,(Streptopelia_capicola:39.1,Columba_livia:39.1):30.1):19.4,Gallus_gallus:88.6):150.5,(Crocodylus_acutus:33,Alligator_mississippiensis:33):206):25.5):35.3,((((((Thamnophis_melanogaster:35.5,Natrix_natrix:35.5):17.5,Coluber_constrictor:53):8.4,Agkistrodon_piscivorus:61.4):31.3,((Python_regius:16.7,Python_molurus:16.7):51,Boa_constrictor:67.7):25):91.9,((Iguana_iguana:26.7,Amblyrhynchus_cristatus:26.7):64.6,Phrynosoma_cornutum:91.3):93.3):17.5,Tarentola_mauritanica:202.1):97.7):29.3,((Tachyglossus_aculeatus:46.7284,(Ornithorhynchus_anatinus:13.7,Zaglossus_bruijni:13.7):33.0284):168.8472,(((Didelphis_virginiana:29.06942,Marmosa_mexicana:29.06942):51.14518,(((((((Dasyurus_viverrinus:9.43491,Sarcophilus_harrisii:9.43491):9.08945,Antechinus_godmani:18.52436):6.39918,Sminthopsis_crassicaudata:24.92354):8.64746,Myrmecobius_fasciatus:33.571):5.01025,Thylacinus_cynocephalus:38.58125):25.67055,(Macrotis_lagotis:30.379,(Isoodon_macrourus:1.37071,Isoodon_obesulus:1.37071):29.00829):33.8728):3.4661,((Dendrolagus_bennettianus:52.829,(Tarsipes_rostratus:44.176,Petaurus_breviceps:44.176):8.6529):1.633,Trichosurus_vulpecula:54.462):13.256):12.4968):106.4645,((((Erinaceus_europaeus:70.1469,Talpa_europaea:70.1469):13.161,(((((Felis_domesticus:11.9,Acinonyx_jubatus:11.9):4.4,Panthera_leo:16.3):17.0576,(Viverra_zibetha:32.5906,(Hyaena_hyaena:27.6334,Cynictis_penicillata:27.6335):4.9572):0.767):23.3019,(((Canis_lupus:9,Canis_mesomelas:9):7.1,Vulpes_vulpes:16.1):31.8381,((Mephitis_mephitis:32.7652,(Procyon_lotor:29.724,Galictis_cuja:29.724):3.0412):6.5874,(Ursus_arctos:38.0534,Phoca_vitulina:38.0534):1.2992):8.5856):8.7214):24.5781,((Desmodus_rotundus:65.5663,Pteropus_vampyrus:65.5661):15.2307,((Equus_ferus:57.9101,(Ceratotherium_simum:51.8134,Tapirus_terrestris:51.8134):6.0967):22.3459,(Lama_glama:65.3597,(Sus_scrofa:63.9418,(((Bos_taurus:15.9,(Ovis_aries:8,Capra_hircus:8):7.9):2.5893,Moschus_fuscus:18.4893):40.8446,Globicephala_macrorhynchus:59.3339):4.6079):1.4179):14.8962):0.541):0.4409):2.0702):6.2888,((Tupaia_glis:80.4944,(Oryctolagus_cuniculus:77.1082,((Castor_canadensis:63.828,(((Mus_musculus:11.2,Rattus_norvegicus:11.2):8.1,(Meriones_unguiculatus:16.6,Psammomys_obesus:16.6):2.7):2.5087,Mesocricetus_auratus:21.8087):42.0194):3.1847,((Glis_glis:59.7773,(Marmota_flaviventris:8,Spermophilus_citellus:8):51.7773):5.8344,(Hystrix_africaeaustralis:44.6375,((Chinchilla_lanigera:34.9244,(Octodon_degus:24.7992,Myocastor_coypus:24.7993):10.1252):3.2207,(Cuniculus_paca:29.7814,(Dasyprocta_punctata:27.9103,(Cavia_cutleri:18.3,Hydrochoerus_hydrochaeris:18.3):9.6103):1.8711):8.3637):6.4925):20.9742):1.401):10.0954):3.3863):1.0725,(Galeopterus_variegatus:79.3647,((((Macaca_fascicularis:3.44,Macaca_mulatta:3.44):17.6272,Homo_sapiens:21.0672):25.6528,Aotus_trivirgatus:46.72):26.4179,(Otolemur_crassicaudatus:55.1057,Lemur_catta:55.1057):18.0321):6.2269):2.2022):8.0297):9.7154,((Dasypus_novemcinctus:67.7572,(Bradypus_variegatus:57.2717,Myrmecophaga_tridactyla:57.2716):10.4856):29.2448,(Loxodonta_africana:61.4128,Heterohyrax_brucei:61.4127):35.5893):2.3102):87.3669):28.8965):113.5242):40.2,(((((Rana_temporaria:50.6,Rana_catesbeiana:50.6):112.8,Bufo_americanus:163.4):54.1,Xenopus_laevis:217.5):5.7,(Bombinator_pachypus:199.4,Alytes_obstetricans:199.4):23.8):57.1,Ambystoma_mexicanum:280.3):89):41.4,(((Protopterus_annectens:103.2,Lepidosiren_paradoxa:103.2):138.6,Neoceratodus_forsteri:241.8):167.6,Latimeria_chalumnae:409.4):1.3):14;")
# Add Microtus_pennsylvanicus to sarc tree
sarc <-bind.tip(tree = sarc,
tip.label = "Microtus_pennsylvanicus",
edge.length = 15.5,
where = match("Mesocricetus_auratus",sarc$tip.label,
nomatch = NA_integer_,
incomparables = NULL),
position = 15.5)
# Add sarcopterygean tree to actinopterygean tree
hor <-as.data.frame(tree_layout(sarc)$edgeDT);
sarc_crown <-max(max(hor$xright)-hor$xleft)
actin_sarc_dt <-424.8 # Betancur-R 2017
sarc$root.edge <-actin_sarc_dt -sarc_crown
hor <-as.data.frame(tree_layout(tree)$edgeDT)
tree$root.edge <-actin_sarc_dt-actin_crown
tree <-bind.tree(tree,
sarc,
position = actin_sarc_dt-actin_crown)
# add shark phylogeny from timetree
elas <-read.tree(text = "(Hydrolagus_colliei:399.3989352,(((Scyliorhinus:139.3745,Mustelus_asterias:139.3745)29:59.6255,Squalus_acanthias:199)39:65.67780412,Dasyatis_centreoura:264.6778041)140:134.7211311);")
hor<-as.data.frame(tree_layout(elas)$edgeDT);elas_crown<-max(max(hor$xright)-hor$xleft)
elas_oste_sarc_dt <-462.4 # Betancur-R et al. 2015
elas$root.edge <-elas_oste_sarc_dt-elas_crown
hor<-as.data.frame(tree_layout(tree)$edgeDT);oste_crown<-max(max(hor$xright)-hor$xleft)
tree$root.edge <-actin_sarc_dt-actin_crown
tree <-bind.tree(tree,
elas,
position = elas_oste_sarc_dt-oste_crown)
# add agnathan phylogeny (from timetree)
agna <-read.tree(text = "(Myxine_glutinosa:470.51250000,(Lampetra_fluviatilis:16.00000000,Petromyzon_marinus:16.00000000)'14':454.51250000);")
hor <-as.data.frame(tree_layout(agna)$edgeDT);agna_crown<-max(max(hor$xright)-hor$xleft)
agna_elas_oste_sarc_dt <-615 # Timetree
agna$root.edge <-agna_elas_oste_sarc_dt-agna_crown
hor <-as.data.frame(tree_layout(tree)$edgeDT)
gnat_crown <-max(max(hor$xright)-hor$xleft)
tree$root.edge <-agna_elas_oste_sarc_dt-gnat_crown
tree <-bind.tree(tree,
agna,
position = agna_elas_oste_sarc_dt-gnat_crown)
################
### PLOTTING ###
################
## SETUP THEMES ##
# Scatter plot
scatter<-theme(
# Legend
#legend.position = c(1,0),
legend.position = "none",
legend.key.size = unit(0.10, "cm"),
legend.justification = c("right", "bottom"),
legend.box.just = "right",
# Text
legend.text = element_text(size=6),
legend.title = element_blank(),
axis.title.x = element_text(size=6,margin = margin(t = 0)),
axis.title.y = element_text(size=6,margin = margin(r = 0)),
axis.text.x = element_text(size=6,margin = margin(t = 0)),
axis.text.y = element_text(size=6,margin = margin(r = 0)),
# Axis
axis.ticks = element_blank(),
axis.line = element_line(size = 2),
panel.grid.major = element_line(colour = "grey90",size = 0.2),
# Margin
plot.margin=unit(rep(0.05,4),"cm")
)
###################################################################
### Fig. 1 Ancestral state reconstruction of retinal thickness ###
###################################################################
# Named vector of retinal thickness
df<-setNames(data$Thickness,data$Species)
Thickness<-df[!is.na(df)]
# Ancestral state reconstruction using maximum likelihood and an BM model for character evolution
tree.Thickness <- ladderize(keep.tip(tree,names(Thickness)))
asr_Thickness <- anc.ML(tree = tree.Thickness,
x = Thickness,
model="BM",
maxit = 100000)
# Plot figure 1A
pdf("./Figures/01_ASR_thickness.pdf",width = 5,height = 7,useDingbats = F)
par(mar = c(2,0,0,0))
plot(tree.Thickness,root.edge = F,use.edge.length = T,cex = 0.5,show.tip.label = T)
title(xlab="Time (MYA)",font.lab=1,cex.lab=0.5,line = 1,adj = .31)
nodelabels(round(asr_Thickness$ace,0),frame = "none",adj = c(1.2,1.3),cex = .5)
axisPhylo(lwd = 1,cex.axis=0.5,padj=-2.5)
dev.off()
df_1 <- as.data.frame(Thickness)
df_1$PEPRL <- rep(0,length(df_1$Thickness))
df_1$ONL <- rep(0,length(df_1$Thickness))
df_1$OPL <- rep(0,length(df_1$Thickness))
df_1$INL <- rep(0,length(df_1$Thickness))
df_1$IPL <- rep(0,length(df_1$Thickness))
df_1$GCL <- rep(0,length(df_1$Thickness))
df_1$NFL <- rep(0,length(df_1$Thickness))
for (i in 1:length(df_1$Thickness)) {
match <-match(rownames(df_1)[i],data$Species)
df_1$PEPRL[i] <-data$PEPRL[match]
df_1$ONL[i] <-data$ONL[match]
df_1$OPL[i] <-data$OPL[match]
df_1$INL[i] <-data$INL[match]
df_1$IPL[i] <-data$IPL[match]
df_1$GCL[i] <-data$GCL[match]
df_1$NFL[i] <-data$NFL[match]
}
df_1[is.na(df_1)]<-0
df_1$Thickness[df_1$PEPRL>0]=0
df_1b<-
rbind(
cbind(
rownames(df_1),
df_1$PEPRL,
rep("8.PEPRL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$ONL,
rep("7.ONL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$OPL,
rep("6.OPL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$INL,
rep("5.INL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$IPL,
rep("4.IPL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$GCL,
rep("3.GCL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$NFL,
rep("2.NFL",length(df_1$Thickness))
),
cbind(
rownames(df_1),
df_1$Thickness,
rep("1.Total",length(df_1$Thickness))
))
df_1b<-as.data.frame(df_1b)
colnames(df_1b)<-c("Species","Thickness","Layer")
df_1b$Species<-as.character(df_1b$Species)
df_1b$Layer<-as.character(df_1b$Layer)
df_1b$Thickness<-as.numeric(as.character(df_1b$Thickness))
is_tip <- tree.Thickness$edge[,2] <= length(tree.Thickness$tip.label)
ordered_tips <- tree.Thickness$edge[is_tip, 2]
ggplot(df_1b, aes(x = Species, y = Thickness, fill = Layer, label = Layer)) +
geom_bar(stat = "identity",width = 0.8,alpha = 0.8)+
scale_fill_manual(breaks = c("1.Total","2.PEPRL","3.ONL","4.OPL","5.INL","6.IPL","7.GCL","8.NFL"),
#labels = c("Thickness","PEPRL","ONL","OPL","INL","IPL","GCL","NFL"),
values = c("grey40",brewer.pal(7,"RdYlBu")))+
theme(
#legend.key.size = unit(.07, "cm"),
#legend.title = element_blank(),
#legend.text = element_text(size = 6),
legend.position = "none",
#legend.justification = c(0,0),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey90",size = 0.2),
axis.title=element_blank(),
axis.text=element_blank()
)+
scale_x_discrete(limits = tree.Thickness$tip.label[ordered_tips])+
scale_y_continuous(expand = c(0,0))+
ylab("Maximal retinal thickness (µm)")+
coord_flip()
ggsave("./Figures/01_barplot.pdf",width = 2.8263,height = 6.6008)
# Q: What is the inferred retinal thickness for the MRCA of bony fishes
asr_Thickness$ace[1]
# A: 194 micrometer
# Q: When did this ancestor live?
hor <- as.data.frame(tree_layout(tree.Thickness)$edgeDT);max(max(hor$xright)-hor$xleft)
# A: 425 MYA
# Q: Where does retinal thickness double?
which(asr_Thickness$ace>2*asr_Thickness$ace[1]) # Internal branches where retinal thickness doubles
which(Thickness>2*asr_Thickness$ace[1]) # Terminal branches where retinal thickness doubles
# A: retinal thickness doubled on 6 independent occations; all in terminal branches
# Q: Where does retinal thickness half?
which(asr_Thickness$ace<.5*asr_Thickness$ace[1]) # Internal branches where retinal thickness halved
Thickness[which(Thickness<.5*asr_Thickness$ace[1])] # Terminal branches where retinal thickness halved
# A: retinal thickness halved on 2 independent occations; both in terminal branches
#########################################################
### Fig. 2 Residual eye mass versus retinal thickness ###
#########################################################
## FIG 2A: LOG(EYEMASS) VS LOG(BODYMASS)
# Prune data set and tree
df_2a <- data.frame(logmass = data$logmass_mean,
logeyemass = data$logeyemass_mean,
Species = data$Species,
Class = data$Class)
row.names(df_2a) <-df_2a$Species
df_2a <-as.data.frame(na.omit(df_2a))
tree_pgls <-keep.tip(tree,row.names(df_2a))
# Brownian motion model
BM_2a <- gls(logeyemass~logmass,
correlation = corPagel(value = 1,phy = tree_pgls,fixed = F),
method = "ML",
data = df_2a)
summary(BM_2a) # Summary of model
# Ornstein–Uhlenbeck model
OU_2a <- gls(logeyemass~logmass,
data=df_2a,
correlation=corMartins(2, tree_pgls, fixed=FALSE), method="ML")
summary(OU_2a) # Summary of model
# Model selection based on Akaike weights
aicw(c(AIC(BM_2a),AIC(OU_2a)))
# Conclusion: Use brownian motion model
# Q: Is data heteroscedastic?
plot(BM_2a, resid(., type="n")~fitted(.), col="blue", main="Normalized Residuals vs. Fitted Values",abline=c(0,0))
# A: No
# Q: Does the residuals depart from a normal distribution?
rEM <-resid(BM_2a, type="n")
qqnorm(rEM)
qqline(rEM)
# A: No
# Add rEM to the data frame "data"
rEM <-setNames(as.vector(rEM),names(rEM))
data["rEM"] <-rep(NA,dim(data)[1])
data$rEM[match(names(rEM),data$Species)]<-unname(rEM)
# Plot the data
ggplot(df_2a,aes(x=logmass,y=logeyemass))+
stat_function(fun = function(x) BM_2a$coefficients[1]+x*BM_2a$coefficients[2], linetype = "solid",col = "black",lwd = 0.2) +
geom_point(size = 0.5,alpha = 1,pch = 16)+
scatter+
labs(x = expression("log"[10]*"(body mass [g])"),
y = expression("log"[10]*"(eye mass [g])"))->plot2a
## Fig. 2B: Scatter plot of retinal thickness vs. rEM
# Prune data set and tree
df_2b <-data.frame(rEM = data$rEM,
Thickness = data$Thickness,
Species = data$Species)
row.names(df_2b) <-df_2b$Species
df_2b <-as.data.frame(na.omit(df_2b))
tree_pgls <-keep.tip(tree,row.names(df_2b))
# Brownian motion model
BM_2b <- gls(Thickness~rEM,
correlation = corPagel(value = 0,phy = tree_pgls,fixed = F),
method = "ML",
data = df_2b)
summary(BM_2b) # Summary of model
# Ornstein–Uhlenbeck model model
OU_2b <- gls(Thickness~rEM,
data=df_2b,
correlation=corMartins(0.00000001, tree_pgls, fixed=FALSE), method="ML")
summary(OU_2b) # Summary of model
# Model selection based on Akaike weights
aicw(c(AIC(BM_2b),AIC(OU_2b)))
# Conclusion: Use # Ornstein–Uhlenbeck model model
# Q: Is data heteroscedastic?
plot(OU_2b, resid(., type="n")~fitted(.), col="blue", main="Normalized Residuals vs. Fitted Values",abline=c(0,0))
# A: No
# Q: Does the residuals depart from a normal distribution?
res <-resid(OU_2b, type="n")
qqnorm(res)
qqline(res)
# A: No
# Plot the data
ggplot(df_2b,aes(x=rEM,y=Thickness))+
scatter+
stat_function(fun = function(x) OU_2b$coefficients[1]+x*OU_2b$coefficients[2], linetype = "solid",col = "black",lwd = 0.2) +
labs(x = expression("rEM"),
y = expression("Maximal retinal thickness (µm)"))+
geom_point(size = 0.5,alpha = 1,pch = 16)->plot2b;plot2b
## Combine panels to final figure
pdf("./Figures/02_eyesize_thickness.pdf",width = 3,height = 1.5,useDingbats = F)
plot_grid(plot2a,plot2b,ncol = 2, align = 'h',labels = "AUTO",label_size = 12,hjust = c(-2.6,-3),vjust = 1.2)
dev.off()
jpeg(filename = "./Figures/02_eyesize_thickness.jpeg",width = 3,height = 1.5,units = "in",res = 2400)
plot_grid(plot2a,plot2b,ncol = 2, align = 'h',labels = "AUTO",label_size = 12,hjust = c(-2.6,-3),vjust = 1.2)
dev.off()
################################################################
### Fig. 3 FUNCTIONAL DIVERSIFICATION OF RETINAL OXYGENATION ###
################################################################
## EVOLUTION OF CHOROID RETE MIRABILE
# Named vector of choroid rete mirabile
CRM <-setNames(data$CRM,data$Species)
CRM <-CRM[CRM!=2]
# Stochastic character mapping (SCM) of choroid rete mirabile. This may take a few hours with 10000 simulations
tree.crm <-keep.tip(tree,names(CRM))
SCM_CRM_ER <-make.simmap(tree = tree.crm,
x = CRM,
nsim=10000,
model = "ER")
pdf("./Figures/Supporting figures/CRM_densitymap_ER.pdf",height = 50,width = 20)
densityMap(SCM_CRM_ER)
dev.off()
# Describe SCM
dSCM_CRM_ER <-describe.simmap(SCM_CRM_ER, plot = FALSE)
# Find branches where CRM evolved or was lost
dt_CRM <-divergencetimes(phy = tree.crm,x = CRM,ACE = dSCM_CRM_ER$ace)
gains_crm <-as.numeric(as.character(dt_CRM$node[which(dt_CRM$mode=="Gain")]))
losses_crm <-as.numeric(as.character(dt_CRM$node[which(dt_CRM$mode=="Loss")]))
# Q: When did the CRM evolve and when was it lost?
dt_CRM
# Q: What was the probability for the presence of the choroid rete mirabile at key divergence points?
# MRCA of jawed vertebrates
MRCA <-getMRCA(keep.tip(tree,names(CRM)),c("Homo_sapiens","Squalus_acanthias"))# Internal branch number for MRCA
dSCM_CRM_ER$ace[match(MRCA,row.names(dSCM_CRM_ER$ace)),]
# A: 0.2%
# MRCA of bony fishes
MRCA <-getMRCA(keep.tip(tree,names(CRM)),c("Homo_sapiens","Amia_calva"))
dSCM_CRM_ER$ace[match(MRCA,row.names(dSCM_CRM_ER$ace)),]
# A: 0.1%
# MRCA of teleosts
MRCA <-getMRCA(keep.tip(tree,names(CRM)),c("Anguilla_anguilla","Danio_rerio"))
dSCM_CRM_ER$ace[match(MRCA,row.names(dSCM_CRM_ER$ace)),]
# A: 94.2%
### Evolution of the intra-retinal capillarization ###
IRC <-setNames(data$IRC,data$Species) # Named vector of choroid rete mirabile
IRC <-IRC[!is.na(IRC)]
tree.irc <-ladderize(keep.tip(tree,names(IRC[IRC!=2])))
SCM_IRC_ER <-make.simmap(tree = tree.irc, # Stochastic character mapping (SCM) of choroid rete mirabile presence
x = IRC[IRC!=2],
nsim=10000,
model = "ER")
pdf("./Figures/Supporting figures/IRC_densitymap_ER.pdf",height = 50,width = 20)
densityMap(SCM_IRC_ER)
dev.off()
dSCM_IRC_ER <-describe.simmap(SCM_IRC_ER, plot = FALSE) # Describe SCM
transition_IRC <-divergencetimes(phy = tree.irc, # Find branches where IRC evolved and was lost
x = IRC[IRC!=2],
ACE = dSCM_IRC_ER$ace)
gains_irc <-as.numeric(as.character(transition_IRC$node[which(transition_IRC$mode=="Gain")])) # Branches where IRC evolved
losses_irc <-as.numeric(as.character(transition_IRC$node[which(transition_IRC$mode=="Loss")])) # Branches where IRC was lost
### Evolution of the intra-retinal capillarization ###
IRC <-setNames(data$IRC,data$Species) # Named vector of intra retinal capillaries
IRC <-IRC[!is.na(IRC)]
tree.irc <-ladderize(keep.tip(tree,names(IRC[IRC!=2])))
SCM_IRC_ER <-make.simmap(tree = tree.irc, # Stochastic character mapping (SCM) of intra retinal capillary presence
x = IRC[IRC!=2],
nsim=10000,
model = "ER")
pdf("./Figures/Supporting figures/IRC_densitymap_ER.pdf",height = 50,width = 20)
densityMap(SCM_IRC_ER)
dev.off()
dSCM_IRC_ER <-describe.simmap(SCM_IRC_ER, plot = FALSE) # Describe SCM
transition_IRC <-divergencetimes(phy = tree.irc, # Find branches where IRC evolved and was lost
x = IRC[IRC!=2],
ACE = dSCM_IRC_ER$ace)
gains_irc <-as.numeric(as.character(transition_IRC$node[which(transition_IRC$mode=="Gain")])) # Branches where IRC evolved
losses_irc <-as.numeric(as.character(transition_IRC$node[which(transition_IRC$mode=="Loss")])) # Branches where IRC was lost
# Probability for intra-retinal capillarization in MRCA of bony fishes
MRCA <-getMRCA(tree.irc,c("Homo_sapiens","Amia_calva"))
dSCM_IRC_ER$ace[match(MRCA,row.names(dSCM_IRC_ER$ace)),]
# A: 99.6% probability for absence IRC
# Probability for intra-retinal capillarization in MRCA of extant mammals
MRCA <-getMRCA(tree.irc,c("Homo_sapiens","Ornithorhynchus_anatinus"))
dSCM_IRC_ER$ace[match(MRCA,row.names(dSCM_IRC_ER$ace)),]
# A: 99.1% probability for IRC
### Evolution of pre-retinal capillarization ###
PRC <-setNames(data$PRC_bi,data$Species) # Named vector of pre-retinal capillary presence
PRC <-PRC[!is.na(PRC)]
tree.prc <-ladderize(keep.tip(tree,names(PRC[PRC!=2])))
SCM_PRC_ER <-make.simmap(tree = tree.prc,
x = PRC[PRC!=2],
nsim=10000,
model = "ER")
pdf("./Figures/Supporting figures/PRC_densitymap_ER.pdf",height = 50,width = 20)
densityMap(SCM_PRC_ER)
dev.off()
dSCM_PRC_ER <-describe.simmap(SCM_PRC_ER, plot = FALSE) # Describe SCM
transition_PRC <-divergencetimes(phy = tree.prc, # Find branches where PRC evolved and was lost
x = PRC[PRC!=2],
ACE = dSCM_PRC_ER$ace)
gains_prc <-as.numeric(as.character(transition_PRC$node[which(transition_PRC$mode=="Gain")])) # Branches where PRC evolved
losses_prc <-as.numeric(as.character(transition_PRC$node[which(transition_PRC$mode=="Loss")])) # Branches where PRC was lost
# Probability for presence of pre-retinal capillarization in MRCA of bony fishes
MRCA <-getMRCA(tree.prc,c("Homo_sapiens","Amia_calva"))
dSCM_PRC_ER$ace[match(MRCA,row.names(dSCM_PRC_ER$ace)),]
# A: 32.9%
# Generate tree for all species in data set
tree.plot<-ladderize(keep.tip(tree,data$Species),F)
old.tip.labels<-tree.plot$tip.label
tree.plot$root.edge<-35
no.data.species<-data$Species[which(data$CRM==2&data$PRC_bi==2&data$IRC==2)]
tree.plot<-drop.tip(tree.plot,no.data.species)
no.data.species
# Abbreviate tip labels
#species <-as.data.frame(tree.plot$tip.label) # Load all species names from the species-level BPP tree
#colnames(species) <-c("Names")
#a <-cSplit(indt = species,splitCols = "Names",sep="_",type.convert=FALSE) # Separate genus and species name
#tree.plot$tip.label <-paste(substr(a$Names_1,1,1),".",substr(a$Names_2,1,3),sep = "")
order.labels<-aggregate(Species~Order,data,length)
order.labels<-order.labels$Order[order.labels$Species>6]
order.labels
order.labels<-order.labels[-6]
order.labels
# PLOT THE FIGURE
fig3<-function(x=1){
par(mar = c(2,2,2,2))
## The phylogeny
plot(tree.plot,show.tip.label = F,
type = "fan",
lwd = 1,
cex = 5/12,
rotate.tree = 0,
root.edge = T,
label.offset = 46,
open.angle = 10)
## Mark larger orders of fishes, mammals and lungfishes
lapply(1:length(order.labels),function(i){
tip<-data$Species[which(data$Order==order.labels[i])]
tip<-tip[is.na(match(tip,no.data.species))]
arc.cladelabels(
text=order.labels[i],
node=getMRCA(phy = tree.plot,
tip = tip),
mark.node=FALSE,lwd=1,ln.offset = 1.07,lab.offset = 1.1,cex=0.5,orientation = "horizontal")
})
arc.cladelabels(text="Mammals",node=getMRCA(tree.plot,data$Species[data$Class=="Mammalia"]),mark.node=FALSE,lwd=1,ln.offset = 1.07,lab.offset = 1.1,cex = 0.5,orientation = "horizontal")
arc.cladelabels(text="Lungfishes",node=getMRCA(tree.plot,data$Species[data$Class=="Dipnoi"]),mark.node=FALSE,lwd=1,ln.offset = 1.07,lab.offset = 1.1,cex = 0.5,orientation = "horizontal")
## Mark major branch points in the phylogeny with dot and text
nodelabels(node = getMRCA(tree.plot,c("Homo_sapiens","Amia_calva")),pch=16,cex=1)
nodelabels(node = getMRCA(tree.plot,c("Polypterus_senegalus","Amia_calva")),pch=16,cex=1)
nodelabels(node = getMRCA(tree.plot,c("Anguilla_anguilla","Pangio_kuhlii")),pch=16,cex=1)
nodelabels(node = getMRCA(tree.plot,c("Homo_sapiens","Ambystoma_mexicanum")),pch=16,cex=1)
nodelabels(text = "Bony fishes",node = getMRCA(tree.plot,c("Homo_sapiens","Amia_calva")),frame = "none",cex = 0.5,adj = c(1.2,0))
nodelabels(text = "Ray-finned fishes",node = getMRCA(tree.plot,c("Polypterus_senegalus","Amia_calva")),frame = "none",cex = 0.5,adj = c(1.05,1.05))
nodelabels(text = "Teleosts",node = getMRCA(tree.plot,c("Anguilla_anguilla","Pangio_kuhlii")),frame = "none",cex = 0.5,adj = c(-0.3,0.6))
nodelabels(text = "Tetrapods",node = getMRCA(tree.plot,c("Homo_sapiens","Ambystoma_mexicanum")),frame = "none",cex = 0.5,adj = c(0.7,-2.2))
# Add time axis
obj<-axis(side = 1,pos=-20,at=c(650,550,450,350,250,150,50),cex.axis=0.5,labels=FALSE)
text(sort(obj,decreasing = T),rep(-50,length(obj)),(obj-50),cex=0.5)
text(mean(obj),-75,"Time (MYA)",cex=0.5)
## Tip phenotypes
lapply(1:length(tree.plot$tip.label),function(i){
# Intra-retinal capillaries
phenotype <-to.matrix(IRC[tree.plot$tip.label],seq=sort(unique(IRC)))[i,]
if(all(phenotype==c(1,0,0))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 22,bg="white")}
if(all(phenotype==c(0,1,0))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 22,bg=cols[2])}
# Choroid rete mirabile
phenotype <-to.matrix(CRM[tree.plot$tip.label],seq=sort(unique(CRM)))[i,]
if(all(phenotype==c(1,0))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 10,bg="white")}
if(all(phenotype==c(0,1))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 10,bg=cols[1])}
# Pre-retinal capillarization
phenotype <-to.matrix(PRC[tree.plot$tip.label],seq=sort(unique(PRC)))[i,]
if(all(phenotype==c(1,0,0))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 34,bg="white")}
if(all(phenotype==c(0,1,0))){
tiplabels(tip = i,pch = 21, col = "black",cex=.5,offset = 34,bg=cols[3])}
})
## Pie charts at the MRCA of bony fishes