rm(list=ls())
#setwd("~/Dropbox/FCU/Teaching/Mentoring/2020Fall/ChiYuWu/rcodeCYW/")
setwd("~/Dropbox/FCU/Teaching/Mentoring/2016Spring-2021Fall_AssociateProfessor/2020Fall/ChiYuWu/rcodeCYW/")


#mtx <- read.table("C:/Users/User/Dropbox/ChiYoWu/dataset/niewiarowski2004phylogenetic/mtxEOV-2.txt")
#mtx <- read.table("~/Dropbox/FCU/Teaching/Mentoring/2020Fall/ChiYuWu/dataset/niewiarowski2004phylogenetic/mtxEOV-2.txt")
library(phytools)
## Loading required package: ape
## Loading required package: maps
library(ape)
library(MASS)
library(ggplot2)
library(geepack)
library(geeM)
## Loading required package: Matrix
library(gee)
library(ggtree)
## ggtree v3.8.0 For help: https://yulab-smu.top/treedata-book/
## 
## If you use the ggtree package suite in published research, please cite
## the appropriate paper(s):
## 
## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam.
## ggtree: an R package for visualization and annotation of phylogenetic
## trees with their covariates and other associated data. Methods in
## Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
## 
## Shuangbin Xu, Lin Li, Xiao Luo, Meijun Chen, Wenli Tang, Li Zhan, Zehan
## Dai, Tommy T. Lam, Yi Guan, Guangchuang Yu. Ggtree: A serialized data
## object for visualization of a phylogenetic tree and annotation data.
## iMeta 2022, 1(4):e56. doi:10.1002/imt2.56
## 
## Guangchuang Yu.  Data Integration, Manipulation and Visualization of
## Phylogenetic Trees (1st edition). Chapman and Hall/CRC. 2022,
## doi:10.1201/9781003279242
## 
## Attaching package: 'ggtree'
## The following object is masked from 'package:Matrix':
## 
##     expand
## The following object is masked from 'package:ape':
## 
##     rotate
mtx <- read.table("~/Dropbox/FCU/Teaching/Mentoring/2016Spring-2021Fall_AssociateProfessor/2020Fall/ChiYuWu/dataset/niewiarowski2004phylogenetic/mtxEOV-2.txt")


newick<-"(17:2,((((((((1:1,2:0.67)28:1,3:2)25:1,((4:0.98,5:1)29:1,6:2)26:0.79)24:1,7:2)23:0.62,((8:1,9:0.98)30:1,(10:1,11:1)31:1)26:0.8)22:0.63,(12:0.98,13:0.97)32:2.77)21:1,(14:0.97,15:0.96)33:2.6)20:1,16:1)19:0.99)18:0;"

tree<-ape::read.tree(text="(17:2,((((((((1:1,2:0.67)28:1,3:2)25:1,((4:0.98,5:1)29:1,6:2)26:0.79)24:1,7:2)23:0.62,((8:1,9:0.98)30:1,(10:1,11:1)31:1)26:0.8)22:0.63,(12:0.98,13:0.97)32:2.77)21:1,(14:0.97,15:0.96)33:2.6)20:1,16:1)19:0.99)18:0;")


#tree$tip.label<-LETTERS[1:17]
tree$tip.label <-  c("S. undulatus(GA)","S. undulatus(OH)","S. undulatus(AL)","S. undulatus(NJ)","S. undulatus(PA)","S. undulatus(SC)","S. woodi","S. undulatus(AZ)","S. undulatus(UT)","S. undulatus(Huerfano.CO)","S. undulatus(Mesa.CO)","S. undulatus(NE)","S. undulatus(TX)","S. undulatus(Grant.NM)","S. undulatus(Hidalgo.NM)","S. virgatus","S. occidentalis")

tree$tip.label <-  c("GA","OH","AL","NJ","PA","SC","S. woodi","AZ","UT","Huerfano.CO","Mesa.CO","NE","TX","Grant.NM","Hidalgo.NM","S. virgatus","S. occidentalis")

plot(tree)

treempl<-chronoMPL(phy=tree, se = TRUE, test = TRUE)
plot(treempl)

library(ggtree)
library(gridExtra)
p1<-ggtree(tree)+ geom_tiplab()+xlim(0, 10)
p2<-ggtree(treempl)+ geom_tiplab()+xlim(0, 10)
grid.arrange(p1, p2, ncol=2)