-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstrained_species_tree.R
10449 lines (9093 loc) · 518 KB
/
constrained_species_tree.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
# Master R code for executing analyses for
# Molecular early burst associated with the diversification of birds at the K–Pg boundary
#load packages to get started
require(phytools)
require(ape)
require(treeio)
require(RColorBrewer)
require(ggtree)
library(doSNOW)
library(doParallel)
library(parallel)
require(geiger)
require(viridis)
require(coRdon)
require(seqinr)
require(Rphylopars)
require(pegas)
require(mvMORPH)
require(english)
require(car)
require(phylolm)
require(words2number)
require(OUwie)
require(GISTools)
require(Quartet)
require(readxl)
require(pbmcapply)
require(caper)
require(Biobase)
require(rr2)
require(dplyr)
require(ggridges)
require(ggplot2)
require(ggtree)
require(pheatmap)
require(ggpubr)
require(ggradar)
require(fmsb)
require(TeachingDemos)
require(ratematrix)
require(mapplots)
require(autoimage)
setwd('/Users/cotinga/jsb439@cornell.edu/Code/avian_molecular_shifts')
#read in function definitions
source("Functions_consensus_genetrees.R")
#system report and package references
{
#require(report)
#session <- sessionInfo()
#r <- report(session)
#saveRDS(r, file='./RDS/session_report.RDS')
##Note, treePL must be installed in your path for congruification to work
#report_system()
#Analyses were conducted using the R Statistical language (version 4.2.2; R Core Team, 2022) on macOS Ventura 13.2.1
#report_packages(include_R = FALSE)
#- geiger (version 2.0.10; Alfaro M et al., 2009)
#- iterators (version 1.0.14; Analytics R, Weston S, 2022)
#- magrittr (version 2.0.3; Bache S, Wickham H, 2022)
#- OUwie (version 2.10; Beaulieu JM, O'Meara B, 2022)
#- maps (version 3.4.1; Becker OScbRA et al., 2022)
#- ggradar (version 0.2; Bion R, 2022)
#- maptools (version 1.1.6; Bivand R, Lewin-Koh N, 2022)
#- rgeos (version 0.6.1; Bivand R, Rundel C, 2022)
#- GISTools (version 0.7.4; Brunsdon C, Chen H, 2014)
#- ratematrix (version 1.2.4; Caetano D, Harmon L, 2022)
#- seqinr (version 4.2.23; Charif D, Lobry J, 2007)
#- mvMORPH (version 1.1.6; Clavel J et al., 2015)
#- doParallel (version 1.0.17; Corporation M, Weston S, 2022)
#- doSNOW (version 1.0.20; Corporation M, Weston S, 2022)
#- coRdon (version 1.16.0; Elek A et al., 2022)
#- english (version 1.2.6; Fox J et al., 2021)
#- car (version 3.1.1; Fox J, Weisberg S, 2019)
#- carData (version 3.0.5; Fox J et al., 2022)
#- autoimage (version 2.2.3; French JP, 2017)
#- viridis (version 0.6.2; Garnier et al., 2021)
#- viridisLite (version 0.4.1; Garnier et al., 2022)
#- mvtnorm (version 1.1.3; Genz A et al., 2021)
#- mapplots (version 1.5.1; Gerritsen H, 2018)
#- Rphylopars (version 0.3.9; Goolsby E et al., 2022)
#- phylolm (version 2.6.2; Ho LST, Ane C, 2014)
#- Biobase (version 2.58.0; Huber W et al., 2015)
#- BiocGenerics (version 0.44.0; Huber et al., 2015)
#- rr2 (version 1.1.0; Ives AR, 2018)
#- nloptr (version 2.0.3; Johnson SG, ?)
#- ggpubr (version 0.5.0; Kassambara A, 2022)
#- subplex (version 1.8; King AA, Rowan T, 2022)
#- pheatmap (version 1.0.12; Kolde R, 2019)
#- pbmcapply (version 1.5.1; Kuang K et al., 2022)
#- report (version 0.5.7; Makowski D et al., 2023)
#- foreach (version 1.5.2; Microsoft, Weston S, 2022)
#- fmsb (version 0.7.5; Nakazawa M, 2023)
#- RColorBrewer (version 1.1.3; Neuwirth E, 2022)
#- caper (version 1.0.1; Orme D et al., 2018)
#- pegas (version 1.1; Paradis E, 2010)
#- ape (version 5.7; Paradis E, Schliep K, 2019)
#- sp (version 1.6.0; Pebesma EJ, Bivand RS, 2005)
#- phytools (version 1.2.0; Revell LJ, 2012)
#- corpcor (version 1.6.10; Schafer J et al., 2021)
#- Quartet (version 1.2.5; Smith MR, 2019)
#- TreeTools (version 1.9.0; Smith MR, 2019)
#- TeachingDemos (version 2.12; Snow G, 2020)
#- snow (version 0.4.4; Tierney L et al., 2021)
#- MASS (version 7.3.58.1; Venables WN, Ripley BD, 2002)
#- words2number (version 0.2.0; Vera Oteo J, Marwick B, 2018)
#- ggplot2 (version 3.4.1; Wickham H, 2016)
#- readxl (version 1.4.1; Wickham H, Bryan J, 2022)
#- dplyr (version 1.1.0; Wickham H et al., 2023)
#- ggridges (version 0.5.4; Wilke C, 2022)
#- treeio (version 1.22.0; Yu G, 2022)
#- ggtree (version 3.6.2; Yu G, 2022)
#cite_packages()
#- Alfaro M, Santini F, Brock C, Alamillo H, Dornburg A, Rabosky D, Carnevale G, Harmon L (2009). “Nine exceptional radiations plus high turnover explain species diversity in jawed vertebrates.” _Proceedings of the National Academy of Sciences of the United States of America_, *106*, 13410-13414. Eastman J, Alfaro M, Joyce P, Hipp A, Harmon L (2011). “A novel comparative method for identifying shifts in the rate of character evolution on trees.” _Evolution_, *65*, 3578-3589. Slater G, Harmon L, Wegmann D, Joyce P, Revell L, Alfaro M (2012). “Fitting models of continuous trait evolution to incompletely sampled comparative data using approximate Bayesian computation.” _Evolution_, *66*, 752-762. Harmon L, Weir J, Brock C, Glor R, Challenger W (2008). “GEIGER: investigating evolutionary radiations.” _Bioinformatics_, *24*, 129-131. Pennell M, Eastman J, Slater G, Brown J, Uyeda J, Fitzjohn R, Alfaro M, Harmon L (2014). “geiger v2.0: an expanded suite of methods for fitting macroevolutionary models to phylogenetic trees.” _Bioinformatics_, *30*, 2216-2218.
#- Analytics R, Weston S (2022). _iterators: Provides Iterator Construct_. R package version 1.0.14, <https://CRAN.R-project.org/package=iterators>.
#- Bache S, Wickham H (2022). _magrittr: A Forward-Pipe Operator for R_. R package version 2.0.3, <https://CRAN.R-project.org/package=magrittr>.
#- Beaulieu JM, O'Meara B (2022). _OUwie: Analysis of Evolutionary Rates in an OU Framework_. R package version 2.10, <https://github.com/thej022214/OUwie>.
#- Becker OScbRA, Minka ARWRvbRBEbTP, Deckmyn. A (2022). _maps: Draw Geographical Maps_. R package version 3.4.1, <https://CRAN.R-project.org/package=maps>.
#- Bion R (2022). _ggradar: Create radar charts using ggplot2_. R package version 0.2.
#- Bivand R, Lewin-Koh N (2022). _maptools: Tools for Handling Spatial Objects_. R package version 1.1-6, <https://CRAN.R-project.org/package=maptools>.
#- Bivand R, Rundel C (2022). _rgeos: Interface to Geometry Engine - Open Source ('GEOS')_. R package version 0.6-1, <https://CRAN.R-project.org/package=rgeos>.
#- Brunsdon C, Chen H (2014). _GISTools: Some further GIS capabilities for R_. R package version 0.7-4, <https://CRAN.R-project.org/package=GISTools>.
#- Caetano D, Harmon L (2022). _ratematrix: Bayesian Estimation of the Evolutionary Rate Matrix_. R package version 1.2.4, <https://CRAN.R-project.org/package=ratematrix>.
#- Charif D, Lobry J (2007). “SeqinR 1.0-2: a contributed package to the R project for statistical computing devoted to biological sequences retrieval and analysis.” In Bastolla U, Porto M, Roman H, Vendruscolo M (eds.), _Structural approaches to sequence evolution: Molecules, networks, populations_, series Biological and Medical Physics, Biomedical Engineering, 207-232. Springer Verlag, New York. ISBN : 978-3-540-35305-8.
#- Clavel J, Escarguel G, Merceron G (2015). “mvMORPH: an R package for fitting multivariate evolutionary models to morphometric data.” _Methods in Ecology and Evolution_, *6*, 1311-1319.
#- Corporation M, Weston S (2022). _doParallel: Foreach Parallel Adaptor for the 'parallel' Package_. R package version 1.0.17, <https://CRAN.R-project.org/package=doParallel>.
#- Corporation M, Weston S (2022). _doSNOW: Foreach Parallel Adaptor for the 'snow' Package_. R package version 1.0.20, <https://CRAN.R-project.org/package=doSNOW>.
#- Elek A, Kuzman M, Vlahovicek K (2022). _coRdon: Codon Usage Analysis and Prediction of Gene Expressivity_. R package version 1.16.0, <https://github.com/BioinfoHR/coRdon>.
#- Fox J, Venables B, Damico A, Salverda AP (2021). _english: Translate Integers into English_. R package version 1.2-6, <https://CRAN.R-project.org/package=english>.
#- Fox J, Weisberg S (2019). _An R Companion to Applied Regression_, Third edition. Sage, Thousand Oaks CA. <https://socialsciences.mcmaster.ca/jfox/Books/Companion/>.
#- Fox J, Weisberg S, Price B (2022). _carData: Companion to Applied Regression Data Sets_. R package version 3.0-5, <https://CRAN.R-project.org/package=carData>.
#- French JP (2017). “autoimage: Multiple Heat Maps for Projected Coordinates.” _The R Journal_, *9*, 284-297. <https://journal.r-project.org/archive/2017/RJ-2017-025/RJ-2017-025.pdf>.
#- Garnier, Simon, Ross, Noam, Rudis, Robert, Camargo, Pedro A, Sciaini, Marco, Scherer, Cédric (2021). _viridis - Colorblind-Friendly Color Maps for R_. doi:10.5281/zenodo.4679424 <https://doi.org/10.5281/zenodo.4679424>, R package version 0.6.2, <https://sjmgarnier.github.io/viridis/>.
#- Garnier, Simon, Ross, Noam, Rudis, Robert, Camargo, Pedro A, Sciaini, Marco, Scherer, Cédric (2022). _viridis - Colorblind-Friendly Color Maps for R_. doi:10.5281/zenodo.4679424 <https://doi.org/10.5281/zenodo.4679424>, R package version 0.4.1, <https://sjmgarnier.github.io/viridis/>.
#- Genz A, Bretz F, Miwa T, Mi X, Leisch F, Scheipl F, Hothorn T (2021). _mvtnorm: Multivariate Normal and t Distributions_. R package version 1.1-3, <https://CRAN.R-project.org/package=mvtnorm>. Genz A, Bretz F (2009). _Computation of Multivariate Normal and t Probabilities_, series Lecture Notes in Statistics. Springer-Verlag, Heidelberg. ISBN 978-3-642-01688-2.
#- Gerritsen H (2018). _mapplots: Data Visualisation on Maps_. R package version 1.5.1, <https://CRAN.R-project.org/package=mapplots>.
#- Goolsby E, Bruggeman J, Ane C (2022). _Rphylopars: Phylogenetic Comparative Tools for Missing Data and Within-Species Variation_. R package version 0.3.9, <https://CRAN.R-project.org/package=Rphylopars>.
#- Ho LST, Ane C (2014). “A linear-time algorithm for Gaussian and non-Gaussian trait evolution models.” _Systematic Biology_, *63*, 397-408.
#- Huber W, Carey VJ, Gentleman R, Anders S, Carlson M, Carvalho BS, Bravo HC, Davis S, Gatto L, Girke T, Gottardo R, Hahne F, Hansen KD, Irizarry RA, Lawrence M, Love MI, MacDonald J, Obenchain V, Ole's AK, Pag`es H, Reyes A, Shannon P, Smyth GK, Tenenbaum D, Waldron L, Morgan M (2015). “Orchestrating high-throughput genomic analysis with Bioconductor.” _Nature Methods_, *12*(2), 115-121. <http://www.nature.com/nmeth/journal/v12/n2/full/nmeth.3252.html>.
#- Huber, W., Carey, J. V, Gentleman, R., Anders, S., Carlson, M., Carvalho, S. B, Bravo, C. H, Davis, S., Gatto, L., Girke, T., Gottardo, R., Hahne, F., Hansen, D. K, Irizarry, A. R, Lawrence, M., Love, I. M, MacDonald, J., Obenchain, V., Ole's, K. A, Pag`es, H., Reyes, A., Shannon, P., Smyth, K. G, Tenenbaum, D., Waldron, L., Morgan, M. (2015). “Orchestrating high-throughput genomic analysis with Bioconductor.” _Nature Methods_, *12*(2), 115-121. <http://www.nature.com/nmeth/journal/v12/n2/full/nmeth.3252.html>.
#- Ives AR (2018). “R^2s for Correlated Data: Phylogenetic Models, LMMs, and GLMMs.” _Systematic Biology_, syy060. <https://doi.org/10.1093/sysbio/syy060>. Ives AR, Li D (2018). “rr2: An R package to calculate R^2s for regression models.” _The Journal of Open Source Software_, *3*(30), 1028. <https://doi.org/10.21105/joss.01028>.
#- Johnson SG (?). “The NLopt nonlinear-optimization package.” _?_, *?*(?), ?
#- Kassambara A (2022). _ggpubr: 'ggplot2' Based Publication Ready Plots_. R package version 0.5.0, <https://CRAN.R-project.org/package=ggpubr>.
#- King AA, Rowan T (2022). _subplex: Unconstrained Optimization using the Subplex Algorithm_. R package version 1.8, <https://CRAN.R-project.org/package=subplex>.
#- Kolde R (2019). _pheatmap: Pretty Heatmaps_. R package version 1.0.12, <https://CRAN.R-project.org/package=pheatmap>.
#- Kuang K, Kong Q, Napolitano F (2022). _pbmcapply: Tracking the Progress of Mc*pply with Progress Bar_. R package version 1.5.1, <https://CRAN.R-project.org/package=pbmcapply>.
#- Makowski D, Lüdecke D, Patil I, Thériault R, Ben-Shachar M, Wiernik B (2023). “Automated Results Reporting as a Practical Tool to Improve Reproducibility and Methodological Best Practices Adoption.” _CRAN_. <https://easystats.github.io/report/>.
#- Microsoft, Weston S (2022). _foreach: Provides Foreach Looping Construct_. R package version 1.5.2, <https://CRAN.R-project.org/package=foreach>.
#- Nakazawa M (2023). _fmsb: Functions for Medical Statistics Book with some Demographic Data_. R package version 0.7.5, <https://CRAN.R-project.org/package=fmsb>.
#- Neuwirth E (2022). _RColorBrewer: ColorBrewer Palettes_. R package version 1.1-3, <https://CRAN.R-project.org/package=RColorBrewer>.
#- Orme D, Freckleton R, Thomas G, Petzoldt T, Fritz S, Isaac N, Pearse W (2018). _caper: Comparative Analyses of Phylogenetics and Evolution in R_. R package version 1.0.1, <https://CRAN.R-project.org/package=caper>.
#- Paradis E (2010). “pegas: an R package for population genetics with an integrated-modular approach.” _Bioinformatics_, *26*, 419-420.
#- Paradis E, Schliep K (2019). “ape 5.0: an environment for modern phylogenetics and evolutionary analyses in R.” _Bioinformatics_, *35*, 526-528. doi:10.1093/bioinformatics/bty633 <https://doi.org/10.1093/bioinformatics/bty633>.
#- Pebesma EJ, Bivand RS (2005). “Classes and methods for spatial data in R.” _R News_, *5*(2), 9-13. <https://CRAN.R-project.org/doc/Rnews/>. Bivand RS, Pebesma E, Gomez-Rubio V (2013). _Applied spatial data analysis with R, Second edition_. Springer, NY. <https://asdar-book.org/>.
#- R Core Team (2022). _R: A Language and Environment for Statistical Computing_. R Foundation for Statistical Computing, Vienna, Austria. <https://www.R-project.org/>.
#- Revell LJ (2012). “phytools: An R package for phylogenetic comparative biology (and other things).” _Methods in Ecology and Evolution_, *3*, 217-223.
#- Schafer J, Opgen-Rhein R, Zuber V, Ahdesmaki M, Silva APD, Strimmer. K (2021). _corpcor: Efficient Estimation of Covariance and (Partial) Correlation_. R package version 1.6.10, <https://CRAN.R-project.org/package=corpcor>.
#- Smith MR (2019). _Quartet: comparison of phylogenetic trees using quartet and split measures_. doi:10.5281/zenodo.2536318 <https://doi.org/10.5281/zenodo.2536318>, R package version 1.2.5. Sand A, Holt MK, Johansen J, Brodal GS, Mailund T, Pedersen CNS (2014). “tqDist: a library for computing the quartet and triplet distances between binary or general trees.” _Bioinformatics_, *30*(14), 2079-2080. doi:10.1093/bioinformatics/btu157 <https://doi.org/10.1093/bioinformatics/btu157>. Smith MR (2019). “Bayesian and parsimony approaches reconstruct informative trees from simulated morphological datasets.” _Biology Letters_, *15*(2), 20180632. doi:10.1098/rsbl.2018.0632 <https://doi.org/10.1098/rsbl.2018.0632>.
#- Smith MR (2019). _TreeTools: create, modify and analyse phylogenetic trees_. Comprehensive R Archive Network. doi:10.5281/zenodo.3522725 <https://doi.org/10.5281/zenodo.3522725>, R package version 1.9.0.
#- Snow G (2020). _TeachingDemos: Demonstrations for Teaching and Learning_. R package version 2.12, <https://CRAN.R-project.org/package=TeachingDemos>.
#- Tierney L, Rossini AJ, Li N, Sevcikova H (2021). _snow: Simple Network of Workstations_. R package version 0.4-4, <https://CRAN.R-project.org/package=snow>.
#- Venables WN, Ripley BD (2002). _Modern Applied Statistics with S_, Fourth edition. Springer, New York. ISBN 0-387-95457-0, <https://www.stats.ox.ac.uk/pub/MASS4/>.
#- Vera Oteo J, Marwick B (2018). _words2number: Convert number words to numeric digits_. R package version 0.2.0, <https://github.com/benmarwick/words2number>.
#- Wickham H (2016). _ggplot2: Elegant Graphics for Data Analysis_. Springer-Verlag New York. ISBN 978-3-319-24277-4, <https://ggplot2.tidyverse.org>.
#- Wickham H, Bryan J (2022). _readxl: Read Excel Files_. R package version 1.4.1, <https://CRAN.R-project.org/package=readxl>.
#- Wickham H, François R, Henry L, Müller K, Vaughan D (2023). _dplyr: A Grammar of Data Manipulation_. R package version 1.1.0, <https://CRAN.R-project.org/package=dplyr>.
#- Wilke C (2022). _ggridges: Ridgeline Plots in 'ggplot2'_. R package version 0.5.4, <https://CRAN.R-project.org/package=ggridges>.
#- Yu G (2022). _Data Integration, Manipulation and Visualization of Phylogenetic Treess_, 1st edition edition. Chapman and Hall/CRC. <https://www.amazon.com/Integration-Manipulation-Visualization-Phylogenetic-Computational-ebook/dp/B0B5NLZR1Z/>. Wang L, Lam TT, Xu S, Dai Z, Zhou L, Feng T, Guo P, Dunn CW, Jones BR, Bradley T, Zhu H, Guan Y, Jiang Y, Yu G (2020). “treeio: an R package for phylogenetic tree input and output with richly annotated and associated data.” _Molecular Biology and Evolution_, *37*, 599-603. doi:10.1093/molbev/msz240 <https://doi.org/10.1093/molbev/msz240>.
#- Yu G (2022). _Data Integration, Manipulation and Visualization of Phylogenetic Treess_, 1st edition edition. Chapman and Hall/CRC. <https://www.amazon.com/Integration-Manipulation-Visualization-Phylogenetic-Computational-ebook/dp/B0B5NLZR1Z/>. Yu G (2020). “Using ggtree to Visualize Data on Tree-Like Structures.” _Current Protocols in Bioinformatics_, *69*(1), e96. doi:10.1002/cpbi.96 <https://doi.org/10.1002/cpbi.96>, <https://currentprotocols.onlinelibrary.wiley.com/doi/abs/10.1002/cpbi.96>. Yu G, Lam TT, Zhu H, Guan Y (2018). “Two methods for mapping and visualizing associated data on phylogeny using ggtree.” _Molecular Biology and Evolution_, *35*, 3041-3043. doi:10.1093/molbev/msy194 <https://doi.org/10.1093/molbev/msy194>, <https://academic.oup.com/mbe/article/35/12/3041/5142656>. Yu G, Smith D, Zhu H, Guan Y, Lam TT (2017). “ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data.” _Methods in Ecology and Evolution_, *8*, 28-36. doi:10.1111/2041-210X.12628 <https://doi.org/10.1111/2041-210X.12628>, <http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract>.
}
#Section 1, setting up
#this code uses bracket notation {} to delineate discrete sections
#each section is labeled with a comment,
#section contents are described within each section
#code is formatted to read .RDS files representing
#intermediate data objects
{
#generate file paths
{
##run analyses##
#import time scaled reference from kimball for congruification
#this is generated by running kimball_time_parser.R
#local file path (Jake's computer)
#time_scale<-read.tree(file="/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/timetree.prune.nameswap.tre")
#git repo file path
time_scale<-read.tree(file="./trees/nuclear/timetree.prune.nameswap.tre")
#important file paths
#file path for janus directory reflecting analysis of the entire dataset
#consensus.all.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/8952e31d/gamma/redo_m3/ALL_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/ALL_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
consensus.all.path<-"./janus/files/ALL_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/ALL_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
#file path for janus directory containing exploratory (not noted in text) analysis of individual gene trees
loci.standard.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/unconstrained_new/janus/0cba26ae/loci_standard/tmp_results/gophy"
#file path for janus directory containing analysis of the exon dataset
#consensus.exons.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/8952e31d/gamma/redo_m3/exons_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
consensus.exons.path<-"./janus/files/exons_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
#file path for janus directory containing analysis of the exon dataset
#consensus.introns.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/8952e31d/gamma/redo_m3/introns_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
consensus.introns.path<-"./janus/files/introns_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
#file path for janus directory containing analysis of the utr dataset
#consensus.utrs.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/8952e31d/gamma/redo_m3/utrs_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
consensus.utrs.path<-"./janus/files/utrs_MFP_MERGE_MRL3_constraint_G_UE_UL_M3/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre"
#file path for janus directory containing analysis of the mtdna (all) dataset
#consensus.mtdna.all.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/mtdnas/janus/concat_all/mtDNA_all_MRL3_constraint.janus.tre.gophy.results.tre"
consensus.mtdna.all.path<-"./janus/files/mtdnas/concat_all/mtDNA_all_MRL3_constraint.janus.tre.gophy.results.tre"
#file path for janus directory containing analysis of the mtdna (protein only) dataset
#consensus.mtdna.proteins.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/mtdnas/janus/concat_proteins/mtDNA_proteins_MRL3_constraint.janus.treefile.gophy.results.tre"
consensus.mtdna.proteins.path<-"./janus/files/mtdnas/concat_proteins/mtDNA_proteins_MRL3_constraint.janus.treefile.gophy.results.tre"
#file path for janus directory containing analysis of the mtdna (protein only) dataset
#consensus.mtdna.rRNAs.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/mtdnas/janus/concat_rRNA/mtDNA_rRNAs_MRL3_constraint.janus.treefile.gophy.results.tre"
consensus.mtdna.rRNAs.path<-"./janus/files/mtdnas/concat_rRNA/mtDNA_rRNAs_MRL3_constraint.janus.treefile.gophy.results.tre"
#file path for tree estimated with both alleles
#allele.tree.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample2haplo/tree_building/ALL_MFP_2haplo/ALL_MFP_MERGE_2haplo.treefile"
allele.tree.path<-"./trees/nuclear/ALL_MFP_2haplo/ALL_MFP_MERGE_2haplo.treefile"
#directories for gene trees with low support edges collapsed
#loci.standard.50.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/tree_building/loci_standard/collapsed/50"
#loci.standard.75.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/tree_building/loci_standard/collapsed/75"
#loci.standard.90.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/tree_building/loci_standard/collapsed/90"
#loci.standard.95.path<-"/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/tree_building/loci_standard/collapsed/95"
loci.standard.50.path<-"./trees/nuclear/loci_standard/collapsed/50"
loci.standard.75.path<-"./trees/nuclear/loci_standard/collapsed/75"
loci.standard.90.path<-"./trees/nuclear/loci_standard/collapsed/90"
loci.standard.95.path<-"./trees/nuclear/loci_standard/collapsed/95"
}
#read and process data
{
##### ALL GENE TREES
#read in the consensus tree from BF+G analysis
#v8952e31d
consensus.all<-read.beast.fixed(consensus.all.path, ladderize=T)
#genematch.all<-match_generator(refpath=consensus.all.path, targetpath=loci.standard.path)
#saveRDS(genematch.all, file="./RDS/genematch.all.RDS")
genematch.all<-readRDS(file="./RDS/genematch.all.RDS")
#concordance calculation on gene trees with low support edges collapsed
#all_concordance.95<-gene_concordance_perc(refpath=consensus.all.path, targetpath=loci.standard.95.path)
#saveRDS(all_concordance.95, file="./RDS/all_concordance.95.RDS")
all_concordance.95<-readRDS(file="./RDS/all_concordance.95.RDS")
##### EXONS ONLY
#read in the consensus tree from BF+G analysis
#v8952e31d
consensus.exons<-read.beast.fixed(consensus.exons.path, ladderize=T)
#consensus.exons.bl<-read.beast.fixed("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/branch_lengths/exons_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T)
#genematch.exons<-match_generator(refpath=consensus.exons.path, targetpath = loci.standard.path, kind="exon")
#saveRDS(genematch.exons, file="./RDS/genematch.exons.RDS")
genematch.exons<-readRDS(file="./RDS/genematch.exons.RDS")
#genematch.exons.allref<-match_generator(refpath=consensus.all.path, targetpath = loci.standard.path, kind="exon")
#saveRDS(genematch.exons.allref, file="./RDS/genematch.exons.allref.RDS")
genematch.exons.allref<-readRDS(file="./RDS/genematch.exons.allref.RDS")
#concordance data on gene trees with low support edges collapsed
#exons_concordance.allref<-gene_concordance_perc(refpath=consensus.all.path, targetpath = loci.standard.95.path, kind="exon")
#saveRDS(exons_concordance.allref, file="./RDS/exons_concordance.allref.RDS")
exons_concordance.allref<-readRDS(file="./RDS/exons_concordance.allref.RDS")
##### EXONS with MFP+MERGE codons
#read in the consensus tree from BFRM analysis
#consensus.exons<-read.beast.fixed("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T)
#consensus.exons.bl<-read.beast.fixed("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/branch_lengths/exons_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T)
#genematch.exons.MFP<-match_generator(refpath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", targetpath = "/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/unconstrained/exons_partitioned/results/gophytrees/gophy", kind="exon")
#concordance data
#exons_MFP_concordance<-gene_concordance_perc(refpath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", targetpath = "/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/unconstrained/exons_partitioned/results/gophytrees/gophy", kind="exon")
#trying with exons with failed partitions removed
#genematch.exons.MFP.sym<-match_generator(refpath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", targetpath = "/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/unconstrained/exons_partitioned_symtest/results/gophytrees/gophy", kind="exon")
#concordance data
#exons_MFP_concordance<-gene_concordance_perc(refpath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", targetpath = "/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/unconstrained/exons_partitioned_symtest/results/gophytrees/gophy", kind="exon")
##### INTRONS ONLY
#read in the consensus tree from BF+G analysis
#v8952e31d
consensus.introns<-read.beast.fixed(consensus.introns.path, ladderize=T)
#consensus.introns.bl<-read.beast.fixed("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/branch_lengths/introns_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T)
#genematch.introns<-match_generator(refpath = consensus.introns.path, targetpath = loci.standard.path, kind = "intron|Unknown")
#saveRDS(genematch.introns, file="./RDS/genematch.introns.RDS")
genematch.introns<-readRDS(file="./RDS/genematch.introns.RDS")
#genematch.introns.allref<-match_generator(refpath = consensus.all.path, targetpath = loci.standard.path, kind = "intron|Unknown")
#aveRDS(genematch.introns.allref, file="./RDS/genematch.introns.allref.RDS")
genematch.introns.allref<-readRDS(file="./RDS/genematch.introns.allref.RDS")
#concordance calcs
#introns_concordance.allref<-gene_concordance_perc(refpath = consensus.all.path, targetpath = loci.standard.95.path, kind = "intron|Unknown")
#saveRDS(introns_concordance.allref, file="./RDS/introns_concordance.allref.RDS")
introns_concordance.allref<-readRDS(file="./RDS/introns_concordance.allref.RDS")
##### UTRs ONLY
#read in the consensus tree from BF+G analysis
#v8952e31d
consensus.utrs<-read.beast.fixed(consensus.utrs.path, ladderize=T)
#consensus.utrs.bl<-read.beast.fixed("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/branch_lengths/utrs_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4_stderror/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T)
#genematch.utrs<-match_generator(refpath = consensus.utrs.path, targetpath = loci.standard.path, kind = "utr")
#saveRDS(genematch.utrs, file="./RDS/genematch.utrs.RDS")
genematch.utrs<-readRDS(file="./RDS/genematch.utrs.RDS")
#genematch.utrs.allref<-match_generator(refpath = consensus.utrs.path, targetpath = loci.standard.path, kind = "utr")
#saveRDS(genematch.utrs.allref, file="./RDS/genematch.utrs.allref.RDS")
genematch.utrs.allref<-readRDS(file="./RDS/genematch.utrs.allref.RDS")
#concordance calcs
#utrs_concordance.allref<-gene_concordance_perc(refpath = consensus.all.path, targetpath = loci.standard.95.path, kind = "utr")
#saveRDS(utrs_concordance.allref, file="./RDS/utrs_concordance.allref.RDS")
utrs_concordance.allref<-readRDS(file="./RDS/utrs_concordance.allref.RDS")
#reading in the mtDNA datasets
#mtDNAs ONLY
#mtDNA.data<-read_excel(path="/Users/cotinga/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/mtdnas/MITOGENOME-Berv-Aug-2021.xlsx", sheet = "jake_mod")
mtDNA.data<-read_excel(path="./mtDNA_REASSEMBLY/MITOGENOME-Berv-Aug-2021.xlsx", sheet = "jake_mod")
mtDNA.data<-data.frame(treeID=mtDNA.data$treeID, mtdnaNames = mtDNA.data$`NAMES FOR EXPORT`)
mtDNA.data<-mtDNA.data[complete.cases(mtDNA.data),]
#read in the consensus tree from BF+G analysis
#ran with build 0cba26ae
consensus.mtdnas.all<-read.beast.fixed(consensus.mtdna.all.path, ladderize=T)
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.all@phylo$tip.label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.all@phylo$tip.label<-nameswap$treeID
#mtdna protein only analysis
consensus.mtdnas.proteins<-read.beast.fixed(consensus.mtdna.proteins.path, ladderize=T)
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.proteins@phylo$tip.label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.proteins@phylo$tip.label<-nameswap$treeID
#now the rRNAs
consensus.mtdnas.rRNAs<-read.beast.fixed(consensus.mtdna.rRNAs.path, ladderize = T)
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.rRNAs@phylo$tip.label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.rRNAs@phylo$tip.label<-nameswap$treeID
#presently not worrying about concordance on the mtDNA because
#there are very few shifts, and therefore we would not expect any significant signal
}
#generate condordance summaries for each node relative to "all data" analysis
{
con_counts<-data.frame(exons=exons_concordance.allref[,2]*length(genematch.exons.allref[1,]), introns=introns_concordance.allref[,2]*length(genematch.introns.allref[1,]), utrs=utrs_concordance.allref[,2]*length(genematch.utrs.allref[1,]))
con_prop<-con_counts/length(genematch.all[1,])
#there seems to be one exon for which aves is not monophyletic, not sure how that can happen?
#calclate the discordance
con_prop$discordance <- 1- (con_prop$exons+con_prop$introns+con_prop$utrs)
con_prop$discordance.exons <- exons_concordance.allref$discordance
con_prop$discordance.introns <- introns_concordance.allref$discordance
con_prop$discordance.utrs <- utrs_concordance.allref$discordance
#visualize patterns of phylogenomic discordance across loci
#pairs(con_prop)
#pairs(con_prop)
#setting up the dataset for logistic regression
con_prop_logit<-con_prop
con_prop_logit$node.all<-seq(from=length(consensus.all@phylo$tip.label)+1, length.out=consensus.all@phylo$Nnode)
#make a time tree version of consensus.all
consensus.all.timetree<-make_timetree(ref = time_scale, tar = consensus.all@phylo)
#plot(consensus.all.timetree$phy, cex=0.001)
#nodelabels()
}
#collect the shift nodes from each dataset
{
shifts.allnuc<-as.data.frame(consensus.all@data)[,c(3,5)][seq(from=length(consensus.all@phylo$tip.label)+1, length.out=consensus.all@phylo$Nnode),]
shifts.allnuc$node.all<-match_phylo_nodes(consensus.all@phylo, consensus.all.timetree$phy)[,2]
shifts.allnuc<-shifts.allnuc[order(shifts.allnuc$node.all),]
shifts.exons<-as.data.frame(consensus.exons@data)[,c(3,5)][seq(from=length(consensus.exons@phylo$tip.label)+1, length.out=consensus.exons@phylo$Nnode),]
shifts.exons$node.all<-match_phylo_nodes(consensus.exons@phylo, consensus.all.timetree$phy)[,2]
shifts.exons<-shifts.exons[order(shifts.exons$node.all),]
shifts.introns<-as.data.frame(consensus.introns@data)[,c(3,5)][seq(from=length(consensus.introns@phylo$tip.label)+1, length.out=consensus.introns@phylo$Nnode),]
shifts.introns$node.all<-match_phylo_nodes(consensus.introns@phylo, consensus.all.timetree$phy)[,2]
shifts.introns<-shifts.introns[order(shifts.introns$node.all),]
shifts.utrs<-as.data.frame(consensus.utrs@data)[,c(3,5)][seq(from=length(consensus.utrs@phylo$tip.label)+1, length.out=consensus.utrs@phylo$Nnode),]
shifts.utrs$node.all<-match_phylo_nodes(consensus.utrs@phylo, consensus.all.timetree$phy)[,2]
shifts.utrs<-shifts.utrs[order(shifts.utrs$node.all),]
#mtdna
shifts.mtdnas.all<-as.data.frame(consensus.mtdnas.all@data)[,c(3,5)][seq(from=length(consensus.mtdnas.all@phylo$tip.label)+1, length.out=consensus.mtdnas.all@phylo$Nnode),]
shifts.mtdnas.all$node.all<-match_phylo_nodes(consensus.mtdnas.all@phylo, consensus.all.timetree$phy)[,2]
shifts.mtdnas.all<-shifts.mtdnas.all[order(shifts.mtdnas.all$node.all),]
shifts.mtdnas.proteins<-as.data.frame(consensus.mtdnas.proteins@data)[,c(3,5)][seq(from=length(consensus.mtdnas.proteins@phylo$tip.label)+1, length.out=consensus.mtdnas.proteins@phylo$Nnode),]
shifts.mtdnas.proteins$node.all<-match_phylo_nodes(consensus.mtdnas.proteins@phylo, consensus.all.timetree$phy)[,2]
shifts.mtdnas.proteins<-shifts.mtdnas.proteins[order(shifts.mtdnas.proteins$node.all),]
shifts.mtdnas.rRNAs<-as.data.frame(consensus.mtdnas.rRNAs@data)[,c(3,5)][seq(from=length(consensus.mtdnas.rRNAs@phylo$tip.label)+1, length.out=consensus.mtdnas.rRNAs@phylo$Nnode),]
shifts.mtdnas.rRNAs$node.all<-match_phylo_nodes(consensus.mtdnas.rRNAs@phylo, consensus.all.timetree$phy)[,2]
shifts.mtdnas.rRNAs<-shifts.mtdnas.rRNAs[order(shifts.mtdnas.rRNAs$node.all),]
#add back in missing data rows for those braches which are missing in the rRNA dataset
shifts.mtdnas.rRNAs<-merge(shifts.mtdnas.rRNAs, data.frame(node.all=shifts.mtdnas.proteins$node.all), by='node.all', all.y=T)
}
#checking uncex and unloc
as.data.frame(consensus.exons@data[!is.na(consensus.exons@data$uncex),])
as.data.frame(consensus.introns@data[!is.na(consensus.introns@data$uncex),])
as.data.frame(consensus.utrs@data[!is.na(consensus.utrs@data$uncex),])
as.data.frame(consensus.all@data[!is.na(consensus.all@data$uncex),])
as.data.frame(consensus.mtdnas.all@data[!is.na(consensus.mtdnas.all@data$uncex),])
as.data.frame(consensus.mtdnas.proteins@data[!is.na(consensus.mtdnas.proteins@data$uncex),])
as.data.frame(consensus.mtdnas.rRNAs@data[!is.na(consensus.mtdnas.rRNAs@data$uncex),])
#all cases have uncex and unloc at 100% model weight
#concordance processing
{
con_prop_logit<-cbind(con_prop_logit,
uncex.allnudatc=shifts.allnuc$uncex,
uncex.exons=shifts.exons$uncex,
uncex.introns=shifts.introns$uncex,
uncex.utrs=shifts.utrs$uncex,
uncex.mtdnas.all=shifts.mtdnas.all$uncex,
uncex.mtdnas.proteins=shifts.mtdnas.proteins$uncex,
uncex.mtdnas.rrnas=shifts.mtdnas.rRNAs$uncex)
#which nodes are considered in the janus estimation?
con_prop_logit$considered.nodes<-as.numeric(node_indices_N(tree=consensus.all.timetree$phy, min=4) >=4)
#create merged uncex column with merged uncex exons+introns+utrs
con_prop_logit$uncex.merged<-dplyr::coalesce(con_prop_logit$uncex.exons, con_prop_logit$uncex.introns, con_prop_logit$uncex.utrs)
con_prop_logit$uncex.merged.mtdnas<-dplyr::coalesce(con_prop_logit$uncex.merged, con_prop_logit$uncex.mtdnas.all, con_prop_logit$uncex.mtdnas.proteins, con_prop_logit$uncex.mtdnas.rrnas)
#con_prop_logit$uncex.merged.all<-dplyr::coalesce(con_prop_logit$uncex.allnucdat, con_prop_logit$uncex.merged, con_prop_logit$uncex.merged.mtdnas)
#cbind(con_prop_logit$node.all, con_prop_logit$uncex.merged, con_prop_logit$uncex.merged.mtdnas)
#there is one extra shift added by the mtDNA -- the shift on Neognathae
#con_prop_logit$node[con_prop_logit$uncex==1]
con_prop_logit[is.na(con_prop_logit)] <- 0
}
#notes
{
#
# #plot concordance % on model shift tree
# pdf(file="concordance_test.pdf", height=11, width=8.5)
# #plot(compute.brlen(consensus.all@phylo, 1), cex=0.5, no.margin=T, edge.width=0.75)
#
# plot_janus(treepath=consensus.all.path, ladderize=T, xlim=c(0, 0.2), width=1.5)
#
# par(lwd = 0.2)
# #nodelabels(pie=as.matrix(exons_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.725,0.5))
# #nodelabels(pie=as.matrix(introns_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.5,0.5))
# #nodelabels(pie=as.matrix(utrs_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.275,0.5))
# #nodelabels(pie=as.matrix(exons_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.50,0.5))
# #nodelabels(pie=as.matrix(introns_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.5,0.5))
# #nodelabels(pie=as.matrix(utrs_concordance[,2:3]), piecol=c("black", "white"), cex=0.2, adj=c(0.5,0.5))
#
# #labelplot(shifts=genematch.all, minscale=0.65)
# nodelabels(pie=as.matrix(con_prop), piecol=c(viridis_pal()(3), "white"), cex=0.3)
#
# dev.off()
#
#
# #plot_janus(treepath=consensus.all.path, ladderize=T, xlim=c(0, 0.2), width=1.5)
# #this looks OK
# plot(consensus.all.timetree$phy, cex=0.0001)
# nodelabels(pie=as.matrix(con_prop_logit$uncex.merged), piecol=c(viridis_pal()(3), "white"), cex=0.3)
# nodelabels(pie=as.matrix(con_prop_logit$uncex.merged.mtdnas), piecol=c(viridis_pal()(3), "white"), cex=0.3)
########################################
#
# #plot big comparison
#
# setwd("~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/")
#
# pdf(file="test.pdf", width=24, height=18)
#
# par(mfrow=c(3,5))
# #regular, estimate base freq shift
#
# par(mar = c(1.5,0.25,1,1))
# #plot an empty plot
# plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
# #plot the label
# text(x = 0.5, y = 0.5, "base freq shift",cex = 3.0, col = "black")
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/a65a5d46/bf/ALL_MFP_MERGE_MRL3/ALL_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T, xlim=c(0, 0.2), width=1.5)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/a65a5d46/bf/exons_MFP_MERGE_MRL3/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T, xlim=c(0, 0.15), width=1.5)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/a65a5d46/bf/introns_MFP_MERGE_MRL3/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T, xlim=c(0, 0.3), width=1.5)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/a65a5d46/bf/utrs_MFP_MERGE_MRL3/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", ladderize=T, xlim=c(0, 0.3), width=1.5)
#
#
# #regular, estimate base freq shift + rm shift
# #par(mfrow=c(1,5))
# #par(mar = c(1,0.25,1,0.25))
#
# #plot an empty plot
# plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
# #plot the label
# text(x = 0.5, y = 0.5, "base freq +\n rate matrix shift",cex = 3.0, col = "black")
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/ALL_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/ALL_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.2), ladderize=T, width=1.5)
# #title(main=paste("model shifts on", length(genematch.all[1,]), "loci"))
# labelplot(shifts=genematch.all, minscale=0.65)
# #labelplot.simple(shifts=genematch.all, minscale=0.1, matches.summary=matches.summary.all)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/exons_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.15), ladderize=T, width=1.5)
# #title(main=paste("model shifts on", length(genematch.exons[1,]), "exons"))
# #labelplot(shifts=genematch.exons.MFP, minscale=0.65, "red")
# labelplot(shifts=genematch.exons, minscale=0.65, "black")
#
# #labelplot.simple(shifts=genematch.exons, minscale=0.3, matches.summary=matches.summary.exons)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/introns_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.3), ladderize=T, width=1.5)
# #title(main=paste("model shifts on", length(genematch.introns[1,]), "introns"))
# labelplot(shifts=genematch.introns, minscale=0.65)
#
# plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/5d81a027/regular/utrs_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4_stderror/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.3), ladderize=T, width=1.5)
# #title(main=paste("model shifts on", length(genematch.utrs[1,]), "UTRs"))
# labelplot(shifts=genematch.utrs, minscale=0.65)
#
#
# #branch lengths
# #par(mar = c(0,0,0,0))
# #par(mar = c(2,0.25,1,0.25))
# #plot an empty plot
# plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
# #plot the label
# text(x = 0.5, y = 0.5, "base freq +\n rate matrix shift +\n re-estimate branch lengths",cex = 3.0, col = "black")
#
# compare.phylo(t1=(consensus.all@phylo), t2=(consensus.all.bl@phylo), limit=c(0,0.2), width=1.5)
# #plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/branch_lengths/ALL_MFP_MERGE_MRL3_constraint_RM_UE_UL_M4/ALL_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.2), ladderize=T, width=1.5)
#
# compare.phylo(t1=(consensus.exons@phylo), t2=(consensus.exons.bl@phylo), limit=c(0,0.15), width=1.5)
# #plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/branch_lengths/exons_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4/exons_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.15), ladderize=T, width=1.5)
#
# compare.phylo(t1=(consensus.introns@phylo), t2=(consensus.introns.bl@phylo), limit=c(0,0.3), width=1.5)
# #plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/branch_lengths/introns_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4/introns_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.3), ladderize=T, width=1.5)
#
# compare.phylo(t1=(consensus.utrs@phylo), t2=(consensus.utrs.bl@phylo), limit=c(0,0.3), width=1.5)
# #plot_janus(treepath="~/jsb439@cornell.edu/AnchoredEnrichment/bird2020/berv_alignments/unmasked/min2x/qual20_cov2_haplo_bestonly/initial_test_filters/min50bp_min10p_aligned/ALIGNED/phased/sample1haplo/phyhetnucbf/backbone_constraint/janus/bfrm_2_Dec_2020/branch_lengths/utrs_MFP_MERGE_MRL3_constraint_B_RM_UE_UL_M4_stderror/utrs_MFP_MERGE_MRL3_constraint.rooted.treefile.gophy.results.tre", xlim=c(0,0.3), ladderize=T, width=1.5)
#
#
# dev.off()
}
}
#intermediate plotting
#generating time scaled versions of the trees,
#then painting molecular regimes
{
par(mfrow=c(1,6))
par(mar = c(1.5,0.25,1,1))
#plot_janus_time_rect(reference = time_scale, target = consensus.all)
#title(main="all nuclear data")
#labelplot(shifts=genematch.all, minscale=0.65, make.transparent("black", 0.75), size=0.75)
plot_janus_time_rect(reference = time_scale, target = consensus.exons)
title(main="exons")
#labelplot(shifts=genematch.exons, minscale=0.65, make.transparent("black", 0.75), size=0.75)
plot_janus_time_rect(reference = time_scale, target = consensus.introns)
title(main="introns")
#labelplot(shifts=genematch.introns, minscale=0.65, make.transparent("black", 0.75), size=0.75)
plot_janus_time_rect(reference = time_scale, target = consensus.utrs)
title(main="UTRs")
#labelplot(shifts=genematch.utrs, minscale=0.65, make.transparent("black", 0.75), size=0.75)
plot_janus_time_rect(reference = time_scale, target = consensus.mtdnas.all)
title(main="all mtDNA")
plot_janus_time_rect(reference = time_scale, target = consensus.mtdnas.proteins)
title(main="mtDNA proteins")
plot_janus_time_rect(reference = time_scale, target = consensus.mtdnas.rRNAs)
title(main="mtDNA rRNAs")
}
#Section 2
#more set up, data formatting etc
{
### new section
#generating estimates of GC content, codon usage bias, and nucleotide diversity
{
#getting tip labels and associated node numbers
#read in the data for the "all data" analysis
consensus.all.data<-model2tipdata(output_path=consensus.all.path)
colnames(consensus.all.data)[1]<-"all_nuc_models"
#read in the data for the exon only analysis
consensus.exons.data<-model2tipdata(output_path=consensus.exons.path)
colnames(consensus.exons.data)[1]<-"exon_models"
#read in the data for the intron only analysis
consensus.introns.data<-model2tipdata(output_path = consensus.introns.path)
colnames(consensus.introns.data)[1]<-"intron_models"
#read in the data for the utr only analysis
consensus.utrs.data<-model2tipdata(output_path = consensus.utrs.path)
colnames(consensus.utrs.data)[1]<-"utr_models"
#read in the data for the "all" mtdna analysis
consensus.mtdnas.all.data<-model2tipdata(output_path = consensus.mtdna.all.path)
colnames(consensus.mtdnas.all.data)[1]<-"mtdna_all_models"
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.all.data$label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.all.data$label<-nameswap$treeID
#read in the data for the "protein" mtdna analysis
consensus.mtdnas.proteins.data<-model2tipdata(output_path = consensus.mtdna.proteins.path)
colnames(consensus.mtdnas.proteins.data)[1]<-"mtdna_proteins_models"
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.proteins.data$label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.proteins.data$label<-nameswap$treeID
#read in the data for the "rRNA" mtdna analysis
consensus.mtdnas.rRNAs.data<-model2tipdata(output_path = consensus.mtdna.rRNAs.path)
colnames(consensus.mtdnas.rRNAs.data)[1]<-"mtdna_rRNAs_models"
#need to translate the names to those which are phylogenetically equivalent
#in the nuclear dataset
nameswap<-merge(data.frame(mtdnaNames=consensus.mtdnas.rRNAs.data$label), mtDNA.data, by='mtdnaNames', sort=F)
consensus.mtdnas.rRNAs.data$label<-nameswap$treeID
#merge(x= consensus.mtdnas.rRNAs.data,y= , by.x = "node", by.y="node.all")
}
#estimating diversity from total branch length between alleles
{
two_haplo<-readrooter(treepath = allele.tree.path, outgroup = c("Caiman_croccodilus_1", "Caiman_croccodilus_2", "Crocodylus_porosus_1", "Crocodylus_porosus_2"), drop=T)
two_haplo.dist<-allele_dist(two_haplo)
}
#AUGUST 1## the merging below may not be valid if the node numbers across the consensus trees are not identical
#create new data matrix representing the model IDs and
#start building up the tip dataset
#add the allele distances
{
tip.data <- merge(x=consensus.all.data[,c(1,6)], y=two_haplo.dist, by="label")
#add in the exon model shifts
tip.data <- merge(consensus.exons.data[,c(1,6)], y = tip.data, by="label")
#add in the intron model shifts
tip.data <- merge(consensus.introns.data[,c(1,6)], y = tip.data, by="label")
#add in the utr model shifts
tip.data <- merge(consensus.utrs.data[,c(1,6)], y = tip.data, by="label")
#add in the mtdna "all" model shifts
tip.data <- merge(consensus.mtdnas.all.data[,c(1,6)], y = tip.data, by="label")
#add in the mtdna "protein" model shifts
tip.data <- merge(consensus.mtdnas.proteins.data[,c(1,6)], y = tip.data, by="label")
#add in the mtdna "rRNA" model shifts
tip.data <- merge(consensus.mtdnas.rRNAs.data[,c(1,6)], y = tip.data, by="label", all.y=T)
#fill in the missing models for the rRNAs assuming phylogenetic equivalence+signal
#it is identical to the protein model set, so just overwrite it
tip.data$mtdna_rRNAs_models<-tip.data$mtdna_proteins_models
#[is.na(tip.data$mtdna_rRNAs_models)]<-c(0,1,1,1,1,1,1,1,1)
}
#convert model shifts to characters for downstream anova
{
tip.data$all_nuc_models<-as.character(consensus.all.data$all_nuc_models)
tip.data$exon_models<-as.character(tip.data$exon_models)
tip.data$intron_models<-as.character(tip.data$intron_models)
tip.data$utr_models<-as.character(tip.data$utr_models)
tip.data$mtdna_all_models<-as.character(tip.data$mtdna_all_models)
tip.data$mtdna_proteins_models<-as.character(tip.data$mtdna_proteins_models)
tip.data$mtdna_rRNAs_models<-as.character(tip.data$mtdna_rRNAs_models)
#add rownames
rownames(tip.data)<-tip.data$label
#reorder to match tree order
tip.data <- tip.data[consensus.all.timetree$phy$tip.label,]
}
}
#Section 3
########################################################
### generating new datasets for logistic regression ####
########################################################
{
#try phylogenetic logistic regression with each of the model shift data types
#is phylogemic discordance related to the identification of a model shift?
{
logisticreg_tree<-consensus.all.timetree$phy
#logisticreg_tree$root.edge<-0.1
#nodelabels()
#plot(logisticreg_tree, cex=0.1, no.margin=T)
#graft in a zero length branch to each node (now set to 1 Ma)
graftLength<-1
logisticreg_tree<-grafter(logisticreg_tree, edge.length = graftLength, position = 0.0)
#fixing the graft
logisticreg_tree<-multi2di(logisticreg_tree, random=F)
logisticreg_tree$edge.length[logisticreg_tree$edge.length==0]<-graftLength
#now we have a tree with grafted tips like fossils
logisticreg.newdat<-data.frame(node=logisticreg_tree$tip.label, label=logisticreg_tree$tip.label)
#add the discordance states
logisticreg.newdat<-merge(x=con_prop_logit, y=logisticreg.newdat, by.x="node.all", by.y="node", all.y=T)
#is.unsorted(con_prop_logit$node.all)
#add row labels
rownames(logisticreg.newdat)<-logisticreg.newdat$label
#reorder to match tip label order
logisticreg.newdat<-logisticreg.newdat[logisticreg_tree$tip.label,]
#replace NAs with zeros
logisticreg.newdat[is.na(logisticreg.newdat)]<-0
#add in node ages
ages<-paleotree::dateNodes(consensus.all.timetree$phy, labelDates = F)
ages<-c(setNames(ages[1:198], consensus.all.timetree$phy$tip.label), ages[199:395])
logisticreg.newdat$ages<-ages[logisticreg.newdat$label]
#create alternative version with tips set to 0.0001 years
logisticreg.newdat$ages.nozero<-logisticreg.newdat$ages+0.0001
#logisticreg.newdat$ages.nozero[logisticreg.newdat$ages.nozero==0]<-0.0001
#same for discordance
logisticreg.newdat$discordance.nozeros<-logisticreg.newdat$discordance+0.0001
#logisticreg.newdat$discordance.nozeros[logisticreg.newdat$discordance.nozeros==0]<-0.0001
#is a node within 10 Ma of the K-Pg
logisticreg.newdat$ages.thresh10<-logisticreg.newdat$ages
logisticreg.newdat$ages.thresh10[logisticreg.newdat$ages >= 56 & logisticreg.newdat$ages <= 76] <- "yes"
logisticreg.newdat$ages.thresh10[logisticreg.newdat$ages <= 56 | logisticreg.newdat$ages >= 76] <- "no"
#is a node within 5 Ma of the K-Pg
logisticreg.newdat$ages.thresh5<-logisticreg.newdat$ages
logisticreg.newdat$ages.thresh5[logisticreg.newdat$ages >= 61 & logisticreg.newdat$ages <= 71] <- "yes"
logisticreg.newdat$ages.thresh5[logisticreg.newdat$ages <= 61 | logisticreg.newdat$ages >= 71] <- "no"
#time since KPg
logisticreg.newdat$ages.age_since<-abs(logisticreg.newdat$ages-66)
#time since Kpg, but stem ages
logisticreg.newdat$stem.ages<-abs(stem_age(consensus.all.timetree$phy, all=T)[logisticreg.newdat$label])
logisticreg.newdat$stem.ages_since<-abs(stem_age(consensus.all.timetree$phy, all=T)[logisticreg.newdat$label]-66)
#is a stem node within 10 Ma of the K-Pg
logisticreg.newdat$stem.ages.thresh10<-logisticreg.newdat$stem.ages
logisticreg.newdat$stem.ages.thresh10[logisticreg.newdat$stem.ages >= 56 & logisticreg.newdat$stem.ages <= 76] <- "yes"
logisticreg.newdat$stem.ages.thresh10[logisticreg.newdat$stem.ages <= 56 | logisticreg.newdat$stem.ages >= 76] <- "no"
#is a node within 5 Ma of the K-Pg
logisticreg.newdat$stem.ages.thresh5<-logisticreg.newdat$stem.ages
logisticreg.newdat$stem.ages.thresh5[logisticreg.newdat$stem.ages >= 61 & logisticreg.newdat$stem.ages <= 71] <- "yes"
logisticreg.newdat$stem.ages.thresh5[logisticreg.newdat$stem.ages <= 61 | logisticreg.newdat$stem.ages >= 71] <- "no"
#add midpoint ages
logisticreg.newdat$midpoint<-rowMeans(cbind(logisticreg.newdat$stem.ages, logisticreg.newdat$ages))
#add distance to the boundary
logisticreg.newdat$midpoint.ages_since<-abs(logisticreg.newdat$midpoint-66)
#create alternative versions which exclude the contemporary terminals
logisticreg_tree.alt<-drop.tip(logisticreg_tree, tip=consensus.all.timetree$phy$tip.label)
logisticreg.newdat.alt<-logisticreg.newdat[!rownames(logisticreg.newdat) %in% consensus.all.timetree$phy$tip.label,]
#save.image("~/jsb439@cornell.edu/Code/avian_molecular_shifts/workspace_Feb_3_2022.RData")
}
########################################################
#### end section datasets for logistic regression #####
########################################################
#notes
{
#
# test<-phylolm(log(ages.age_since) ~ as.factor(uncex.merged), phy = (logisticreg_tree), data = logisticreg.newdat)
# plot(log(ages.age_since) ~ as.factor(uncex.merged), data= logisticreg.newdat)
#
# test2<-phylolm(log(ages.age_since) ~ 1, phy = (logisticreg_tree), data = logisticreg.newdat)
# plot(log(ages.age_since) ~ 1, data= logisticreg.newdat)
#
# test #AIC 787.6
# test2 #AIC 799.7
# 10 AIC units improvement
#pageltest<-fitPagel(logisticreg_tree, x = setNames(logisticreg.newdat$ages.thresh10, logisticreg.newdat$label) , y= setNames(as.character(logisticreg.newdat$uncex.merged), logisticreg.newdat$label))
#pageltest2<-fitPagel(logisticreg_tree, x = setNames(logisticreg.newdat$ages.thresh5, logisticreg.newdat$label) , y= setNames(as.character(logisticreg.newdat$uncex.merged), logisticreg.newdat$label))
# tmp<-(logisticreg.newdat.alt[,c("label", "ages.thresh10","uncex.merged" )])
# tmp$uncex.merged<-as.character(tmp$uncex.merged)
# tmp2<-corDISC(phy = logisticreg_tree.alt, data = tmp, ntraits=2, model="ARD", node.states="marginal", diagn=FALSE)
# tmp2$states
# phylo.logisticreg.fit<- phyloglm(uncex.merged~(log(ages.age_since))+log(discordance), data=logisticreg.newdat.alt, phy=(logisticreg_tree.alt), boot=0, "logistic_IG10", btol=1000, log.alpha.bound=6)
# summary(phylo.logisticreg.fit)
#library(future); plan(multiprocess)
#using age since (MRCA)
#
# #log transformations
# phylo.logisticreg.fit.ages<- phyloglm(uncex.merged~(log(ages.age_since)), data=logisticreg.newdat, phy=(logisticreg_tree), method="logistic_IG10", boot=0)
# summary(phylo.logisticreg.fit.ages)
#
# phylo.logisticreg.fit.ages<- phyloglm(uncex.merged~(log(ages.age_since))+log(discordance.nozeros), data=logisticreg.newdat, phy=(logisticreg_tree), method="logistic_IG10", boot=0)
# summary(phylo.logisticreg.fit.ages)
#
# #arcsin sqrt transformation for discordance
# phylo.logisticreg.fit.ages<- phyloglm(uncex.merged~(log(ages.age_since))+asinTransform(discordance), data=logisticreg.newdat, phy=(logisticreg_tree), method="logistic_IG10")
# summary(phylo.logisticreg.fit.ages)
#
# #using age since (stem ages)
#
# #log transformations
#
# phylo.logisticreg.fit.stem.ages<- phyloglm(uncex.merged~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), method="logistic_IG10", btol=16)
# summary(phylo.logisticreg.fit.stem.ages)
#
#
# #models with discordance
# phylo.logisticreg.fit.stem.ages<- phyloglm(uncex.merged~(log(stem.ages_since))+log(discordance.nozeros), data=logisticreg.newdat, phy=(logisticreg_tree), method="logistic_IG10", btol=16)
# summary(phylo.logisticreg.fit.stem.ages)
#
# #arcsing sqrt transformation for discordance
# phylo.logisticreg.fit.stem.ages<- phyloglm(uncex.merged~(log(stem.ages_since))+asinTransform(discordance), data=logisticreg.newdat, phy=(logisticreg_tree), method="logistic_IG10", btol=16)
# summary(phylo.logisticreg.fit.stem.ages)
}
}
#Section 4
########################################################
###########running logistic regression #################
########################################################
#individual models for each data type
library(future); plan(multisession, workers=10)
#library(future); plan(sequential)
#source("/Users/cotinga/Downloads/phylolm-master\ copy/R/phyloglm.R")
#this custom version doesn't work now for some reason-- using boot mean values
#running regression models
{
#run logistic regression (stem ages)
{
#phylo.logisticreg.fit.stem.ages.allnucdata <- phyloglm(uncex.allnudatc~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.stem.ages.exons <- phyloglm(uncex.exons~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, start.alpha=0.1, log.alpha.bound=4)
phylo.logisticreg.fit.stem.ages.introns <- phyloglm(uncex.introns~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.15, log.alpha.bound=4)#, log.alpha.bound=4, boot = 1000)
phylo.logisticreg.fit.stem.ages.utrs<- phyloglm(uncex.utrs~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.1)#, start.alpha=0.2, log.alpha.bound=3, boot = 1000)
phylo.logisticreg.fit.stem.ages.mtdnas.all<- phyloglm(uncex.mtdnas.all~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.stem.ages.mtdnas.proteins<- phyloglm(uncex.mtdnas.proteins~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.2, boot = 1000)
phylo.logisticreg.fit.stem.ages.mtdnas.rrnas<- phyloglm(uncex.mtdnas.rrnas~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.2, boot = 1000)
phylo.logisticreg.fit.stem.ages.merged<- phyloglm(uncex.merged~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.stem.ages.merged.mtdnas<- phyloglm(uncex.merged.mtdnas~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
#individual models for each data type + discordance
#phylo.logisticreg.fit.stem.ages.allnucdata.discordance <- phyloglm(uncex.allnudatc~(log(stem.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), start.alpha=0.001, log.alpha.bound = 10, boot = 100)
phylo.logisticreg.fit.stem.ages.exons.discordance <- phyloglm(uncex.exons~(log(stem.ages_since))+(asinTransform(discordance.exons)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.1, log.alpha.bound = 4.5)
#phylo.logisticreg.fit.stem.ages.introns.discordance <- phyloglm(uncex.introns~(log(stem.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=1, log.alpha.bound = 10)
phylo.logisticreg.fit.stem.ages.introns.discordance <- phyloglm(uncex.introns~(log(stem.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)
phylo.logisticreg.fit.stem.ages.utrs.discordance <- phyloglm(uncex.utrs~(log(stem.ages_since))+(asinTransform(discordance.utrs)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)#, start.alpha=0.2, log.alpha.bound = 5)
phylo.logisticreg.fit.stem.ages.merged.discordance <- phyloglm(uncex.merged~(log(stem.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.1, log.alpha.bound = 4.5)
#min(fitted.values(phylo.logisticreg.fit.stem.ages.exons.discordance))
#max(fitted.values(phylo.logisticreg.fit.stem.ages.exons.discordance))
#
# phylo.logisticreg.fit.stem.ages.merged.mtdnas.discordance <- phyloglm(uncex.merged.mtdnas~scale(log(stem.ages_since))*scale(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 100)
# summary(phylo.logisticreg.fit.stem.ages.merged.mtdnas.discordance)
#
# test <- glm(uncex.merged.mtdnas~(log(stem.ages_since))+asinTransform(discordance), data=logisticreg.newdat.alt, family="binomial", correlation)
# summary(test)
# LogisticDx:::dx(test)
# plot_logistic_curve(test)
#
# plot(test$residuals~test$fitted.values)
# length(test$fitted.values)
# length(logisticreg.newdat.alt$uncex.merged.mtdnas[-197])
#
#
}
#run logistic regression (stem ages -- assuming fixed alpha) -- strong phylogenetic signal
{
phylo.logisticreg.fit.stem.ages.exons.alphabound <- phyloglm(uncex.exons~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.introns.alphabound <- phyloglm(uncex.introns~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.utrs.alphabound <- phyloglm(uncex.utrs~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.mtdnas.all.alphabound <- phyloglm(uncex.mtdnas.all~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.mtdnas.proteins.alphabound <- phyloglm(uncex.mtdnas.proteins~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.mtdnas.rrnas.alphabound <- phyloglm(uncex.mtdnas.rrnas~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.merged.alphabound <- phyloglm(uncex.merged~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.merged.mtdnas.alphabound <- phyloglm(uncex.merged.mtdnas~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
#individual models for each data type + discordance
phylo.logisticreg.fit.stem.ages.exons.discordance.alphabound <- phyloglm(uncex.exons~(log(stem.ages_since))+(asinTransform(discordance.exons)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.introns.discordance.alphabound <- phyloglm(uncex.introns~(log(stem.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.utrs.discordance.alphabound <- phyloglm(uncex.utrs~(log(stem.ages_since))+(asinTransform(discordance.utrs)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.stem.ages.merged.discordance.alphabound <- phyloglm(uncex.merged~(log(stem.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
}
#run logistic regression (midpoint ages)
{
#phylo.logisticreg.fit.stem.ages.allnucdata <- phyloglm(uncex.allnudatc~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.midpoint.ages.exons <- phyloglm(uncex.exons~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, start.alpha=0.1, log.alpha.bound=4)
phylo.logisticreg.fit.midpoint.ages.introns <- phyloglm(uncex.introns~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.15, log.alpha.bound=4)#, log.alpha.bound=4, boot = 1000)
phylo.logisticreg.fit.midpoint.ages.utrs<- phyloglm(uncex.utrs~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.1)#, start.alpha=0.2, log.alpha.bound=3, boot = 1000)
phylo.logisticreg.fit.midpoint.ages.mtdnas.all<- phyloglm(uncex.mtdnas.all~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.midpoint.ages.mtdnas.proteins<- phyloglm(uncex.mtdnas.proteins~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.2, boot = 1000)
phylo.logisticreg.fit.midpoint.ages.mtdnas.rrnas<- phyloglm(uncex.mtdnas.rrnas~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.2, boot = 1000)
phylo.logisticreg.fit.midpoint.ages.merged<- phyloglm(uncex.merged~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.midpoint.ages.merged.mtdnas<- phyloglm(uncex.merged.mtdnas~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
#individual models for each data type + discordance
#phylo.logisticreg.fit.stem.ages.allnucdata.discordance <- phyloglm(uncex.allnudatc~(log(stem.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), start.alpha=0.001, log.alpha.bound = 10, boot = 100)
phylo.logisticreg.fit.midpoint.ages.exons.discordance <- phyloglm(uncex.exons~(log(midpoint.ages_since))+(asinTransform(discordance.exons)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.1, log.alpha.bound = 4.5)
#phylo.logisticreg.fit.midpoint.ages.introns.discordance <- phyloglm(uncex.introns~(log(midpoint.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=1, log.alpha.bound = 10)
phylo.logisticreg.fit.midpoint.ages.introns.discordance <- phyloglm(uncex.introns~(log(midpoint.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.1, log.alpha.bound=4.5)
phylo.logisticreg.fit.midpoint.ages.utrs.discordance <- phyloglm(uncex.utrs~(log(midpoint.ages_since))+(asinTransform(discordance.utrs)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, start.alpha=0.1)#, start.alpha=0.2, log.alpha.bound = 5)
phylo.logisticreg.fit.midpoint.ages.merged.discordance <- phyloglm(uncex.merged~(log(midpoint.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000)#, start.alpha=0.1, log.alpha.bound = 4.5)
}
#run logistic regression (midpoint -- assuming fixed alpha) -- strong phylogenetic signal
{
#phylo.logisticreg.fit.stem.ages.allnucdata <- phyloglm(uncex.allnudatc~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.midpoint.ages.exons.alphabound <- phyloglm(uncex.exons~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.introns.alphabound <- phyloglm(uncex.introns~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.utrs.alphabound <- phyloglm(uncex.utrs~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.mtdnas.all.alphabound <- phyloglm(uncex.mtdnas.all~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.mtdnas.proteins.alphabound <- phyloglm(uncex.mtdnas.proteins~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.mtdnas.rrnas.alphabound <- phyloglm(uncex.mtdnas.rrnas~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.merged.alphabound <- phyloglm(uncex.merged~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.merged.mtdnas.alphabound <- phyloglm(uncex.merged.mtdnas~(log(midpoint.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
#individual models for each data type + discordance
phylo.logisticreg.fit.midpoint.ages.exons.discordance.alphabound <- phyloglm(uncex.exons~(log(midpoint.ages_since))+(asinTransform(discordance.exons)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound = 0)
phylo.logisticreg.fit.midpoint.ages.introns.discordance.alphabound <- phyloglm(uncex.introns~(log(midpoint.ages_since))+(asinTransform(discordance.introns)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.utrs.discordance.alphabound <- phyloglm(uncex.utrs~(log(midpoint.ages_since))+(asinTransform(discordance.utrs)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, log.alpha.bound=0)
phylo.logisticreg.fit.midpoint.ages.merged.discordance.alphabound <- phyloglm(uncex.merged~(log(midpoint.ages_since))+(asinTransform(discordance)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, log.alpha.bound=0)
}
#run logistic regression (crown ages)
{
#phylo.logisticreg.fit.stem.ages.allnucdata <- phyloglm(uncex.allnudatc~(log(stem.ages_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000)
phylo.logisticreg.fit.ages.exons <- phyloglm(uncex.exons~(log(ages.age_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot = 1000, start.alpha=0.1, log.alpha.bound=4.5)
phylo.logisticreg.fit.ages.introns <- phyloglm(uncex.introns~(log(ages.age_since)), data=logisticreg.newdat, phy=(logisticreg_tree.alt), boot=1000, start.alpha=0.15, log.alpha.bound=4.5)#, log.alpha.bound=4, boot = 1000)