Reading trees and matching tips

First, let’s read in a data matrix and inspect it a bit. These are the datasets you’ll need:

# use the complete path
setwd("~/Documents/teaching/comparativeMethods/fall2017UI/exercises/")
anolisData<-read.csv("anolisDataAppended.csv")


mode(anolisData)
## [1] "list"
dim(anolisData)
## [1] 100  11
class(anolisData)
## [1] "data.frame"
#data.frame contains a mix of columns: numeric, character, logical, etc.

# these are the species names
anolisData[,1]
##   [1] ahli            alayoni         alfaroi         aliniger       
##   [5] allisoni        allogus         altitudinalis   alumina        
##   [9] alutaceus       angusticeps     argenteolus     argillaceus    
##  [13] armouri         bahorucoensis   baleatus        baracoae       
##  [17] barahonae       barbatus        barbouri        bartschi       
##  [21] bremeri         breslini        brevirostris    caudalis       
##  [25] centralis       chamaeleonides  chlorocyanus    christophei    
##  [29] clivicola       coelestinus     confusus        cooki          
##  [33] cristatellus    cupeyalensis    cuvieri         cyanopleurus   
##  [37] cybotes         darlingtoni     distichus       dolichocephalus
##  [41] equestris       etheridgei      eugenegrahami   evermanni      
##  [45] fowleri         garmani         grahami         guafe          
##  [49] guamuhaya       guazuma         gundlachi       haetianus      
##  [53] hendersoni      homolechis      imias           inexpectatus   
##  [57] insolitus       isolepis        jubar           krugi          
##  [61] lineatopus      longitibialis   loysiana        lucius         
##  [65] luteogularis    macilentus      marcanoi        marron         
##  [69] mestrei         monticola       noblei          occultus       
##  [73] olssoni         opalinus        ophiolepis      oporinus       
##  [77] paternus        placidus        poncensis       porcatus       
##  [81] porcus          pulchellus      pumilis         quadriocellifer
##  [85] reconditus      ricordii        rubribarbus     sagrei         
##  [89] semilineatus    sheplani        shrevei         singularis     
##  [93] smallwoodi      strahmi         stratulus       valencienni    
##  [97] vanidicus       vermiculatus    websteri        whitemani      
## 100 Levels: ahli alayoni alfaroi aliniger allisoni ... whitemani

Now we must reshape our data to prepare for comparative analysis. For almost all analyses in R, species names need to be rownames.

aData<-anolisData[,-1]
rownames(aData)<-anolisData[,1]

Now we can read in a phylogenetic tree. This one is in newick format so use read.tree

library(ape)
anolisTree<-read.tree("/Users/lukeharmon/Documents/teaching/r_course/data/anolis.phy") 
anolisTree
## 
## Phylogenetic tree with 100 tips and 99 internal nodes.
## 
## Tip labels:
##  ahli, allogus, rubribarbus, imias, sagrei, bremeri, ...
## 
## Rooted; includes branch lengths.
plot(anolisTree)
add.scale.bar()

# can't read the tips!
# better to plot as pdf and control size exactly
# now you DO need to know your working directory, or you can use a whole path
pdf(file="bigTree.pdf", width=18, height=35)
plot(anolisTree)
add.scale.bar()
dev.off()
## quartz_off_screen 
##                 2
class(anolisTree)
## [1] "phylo"
attributes(anolisTree)
## $names
## [1] "edge"        "Nnode"       "tip.label"   "edge.length"
## 
## $class
## [1] "phylo"
## 
## $order
## [1] "cladewise"
# we can interact with anolisTree as a list
anolisTree$tip.label
##   [1] "ahli"            "allogus"         "rubribarbus"    
##   [4] "imias"           "sagrei"          "bremeri"        
##   [7] "quadriocellifer" "ophiolepis"      "mestrei"        
##  [10] "jubar"           "homolechis"      "confusus"       
##  [13] "guafe"           "garmani"         "opalinus"       
##  [16] "grahami"         "valencienni"     "lineatopus"     
##  [19] "reconditus"      "evermanni"       "stratulus"      
##  [22] "krugi"           "pulchellus"      "gundlachi"      
##  [25] "poncensis"       "cooki"           "cristatellus"   
##  [28] "brevirostris"    "caudalis"        "marron"         
##  [31] "websteri"        "distichus"       "barbouri"       
##  [34] "alumina"         "semilineatus"    "olssoni"        
##  [37] "etheridgei"      "fowleri"         "insolitus"      
##  [40] "whitemani"       "haetianus"       "breslini"       
##  [43] "armouri"         "cybotes"         "shrevei"        
##  [46] "longitibialis"   "strahmi"         "marcanoi"       
##  [49] "baleatus"        "barahonae"       "ricordii"       
##  [52] "eugenegrahami"   "christophei"     "cuvieri"        
##  [55] "barbatus"        "porcus"          "chamaeleonides" 
##  [58] "guamuhaya"       "altitudinalis"   "oporinus"       
##  [61] "isolepis"        "allisoni"        "porcatus"       
##  [64] "argillaceus"     "centralis"       "pumilis"        
##  [67] "loysiana"        "guazuma"         "placidus"       
##  [70] "sheplani"        "alayoni"         "angusticeps"    
##  [73] "paternus"        "alutaceus"       "inexpectatus"   
##  [76] "clivicola"       "cupeyalensis"    "cyanopleurus"   
##  [79] "alfaroi"         "macilentus"      "vanidicus"      
##  [82] "argenteolus"     "lucius"          "bartschi"       
##  [85] "vermiculatus"    "baracoae"        "noblei"         
##  [88] "smallwoodi"      "luteogularis"    "equestris"      
##  [91] "monticola"       "bahorucoensis"   "dolichocephalus"
##  [94] "hendersoni"      "darlingtoni"     "aliniger"       
##  [97] "singularis"      "chlorocyanus"    "coelestinus"    
## [100] "occultus"
anolisTree$edge.length
##   [1] 0.044684149 0.034887323 0.067035195 0.137723713 0.127977919
##   [6] 0.347724073 0.109078899 0.130888730 0.130888730 0.239967629
##  [11] 0.587691702 0.179509950 0.129814050 0.087219218 0.061505998
##  [16] 0.257620404 0.147876752 0.109743652 0.109743652 0.319126403
##  [21] 0.406345620 0.284615227 0.042155772 0.090522719 0.118865952
##  [26] 0.118865952 0.209388672 0.251544443 0.293249779 0.122612861
##  [31] 0.217809914 0.019687199 0.200033581 0.200033581 0.219720780
##  [36] 0.437530694 0.088772492 0.471371062 0.471371062 0.205198623
##  [41] 0.149557576 0.352152059 0.213520271 0.213520271 0.077272447
##  [46] 0.030350781 0.131293037 0.326756065 0.326756065 0.071583090
##  [51] 0.386466013 0.386466013 0.093111691 0.395288192 0.395288192
##  [56] 0.314402578 0.098357487 0.026727495 0.275742347 0.105244885
##  [61] 0.170497462 0.170497462 0.302469841 0.400827328 0.010023460
##  [66] 0.075979992 0.067203897 0.802108502 0.052409975 0.262223661
##  [71] 0.219367178 0.268107688 0.268107688 0.487474866 0.161391311
##  [76] 0.588307215 0.211213375 0.377093840 0.377093840 0.078671717
##  [81] 0.250527521 0.091430025 0.106656009 0.342027127 0.005421884
##  [86] 0.069621835 0.266983407 0.266983407 0.144047107 0.044167182
##  [91] 0.148390953 0.148390953 0.192558135 0.196557801 0.252125335
##  [96] 0.252125335 0.540113161 0.208968411 0.107638550 0.090953340
## [101] 0.085111020 0.203602151 0.052636755 0.041730454 0.041730454
## [106] 0.094367210 0.297969361 0.383080380 0.474033721 0.434878004
## [111] 0.146794267 0.053688425 0.093105842 0.016803480 0.076302362
## [116] 0.076302362 0.082378874 0.094353148 0.078870445 0.046441175
## [121] 0.085228625 0.037034912 0.253892089 0.092203181 0.174889942
## [126] 0.174889942 0.267093123 0.224962282 0.296022930 0.296022930
## [131] 0.183201729 0.235625627 0.024976244 0.114216523 0.114216523
## [136] 0.139192767 0.374818395 0.643248749 0.125365985 0.377365981
## [141] 0.186957958 0.186957958 0.184942132 0.379381806 0.162169110
## [146] 0.217212696 0.217212696 0.243446819 0.404251581 0.120861969
## [151] 0.120861969 0.060952236 0.009227868 0.118973642 0.335959803
## [156] 0.249896772 0.086063031 0.086063031 0.174699507 0.280233938
## [161] 0.280233938 0.464161314 0.206480322 0.656433195 0.656433195
## [166] 0.105066626 0.120748239 0.249459768 0.524725367 0.524725367
## [171] 0.039535061 0.629719450 0.017899207 0.028491642 0.058539775
## [176] 0.037133600 0.021406175 0.021406175 0.087031418 0.104930625
## [181] 0.064716929 0.032887360 0.031492017 0.605553768 0.221443700
## [186] 0.384110068 0.233182975 0.150927093 0.150927093 0.637045785
## [191] 0.140310460 0.213562660 0.137705751 0.178354275 0.178354275
## [196] 0.316060025 0.529622686 0.894933374
anolisTree$edge
##        [,1] [,2]
##   [1,]  101  102
##   [2,]  102  103
##   [3,]  103  104
##   [4,]  104  105
##   [5,]  105  106
##   [6,]  106  107
##   [7,]  107  108
##   [8,]  108    1
##   [9,]  108    2
##  [10,]  107    3
##  [11,]  106    4
##  [12,]  105  109
##  [13,]  109  110
##  [14,]  110  111
##  [15,]  111  112
##  [16,]  112    5
##  [17,]  112  113
##  [18,]  113    6
##  [19,]  113    7
##  [20,]  111    8
##  [21,]  110    9
##  [22,]  109  114
##  [23,]  114  115
##  [24,]  115  116
##  [25,]  116   10
##  [26,]  116   11
##  [27,]  115   12
##  [28,]  114   13
##  [29,]  104  117
##  [30,]  117  118
##  [31,]  118  119
##  [32,]  119  120
##  [33,]  120   14
##  [34,]  120   15
##  [35,]  119   16
##  [36,]  118   17
##  [37,]  117  121
##  [38,]  121   18
##  [39,]  121   19
##  [40,]  103  122
##  [41,]  122  123
##  [42,]  123  124
##  [43,]  124   20
##  [44,]  124   21
##  [45,]  123  125
##  [46,]  125  126
##  [47,]  126  127
##  [48,]  127   22
##  [49,]  127   23
##  [50,]  126  128
##  [51,]  128   24
##  [52,]  128   25
##  [53,]  125  129
##  [54,]  129   26
##  [55,]  129   27
##  [56,]  122  130
##  [57,]  130  131
##  [58,]  131  132
##  [59,]  132   28
##  [60,]  132  133
##  [61,]  133   29
##  [62,]  133   30
##  [63,]  131   31
##  [64,]  130   32
##  [65,]  102  134
##  [66,]  134  135
##  [67,]  135  136
##  [68,]  136   33
##  [69,]  136  137
##  [70,]  137  138
##  [71,]  138  139
##  [72,]  139   34
##  [73,]  139   35
##  [74,]  138   36
##  [75,]  137  140
##  [76,]  140   37
##  [77,]  140  141
##  [78,]  141   38
##  [79,]  141   39
##  [80,]  135  142
##  [81,]  142  143
##  [82,]  143  144
##  [83,]  144  145
##  [84,]  145   40
##  [85,]  145  146
##  [86,]  146  147
##  [87,]  147   41
##  [88,]  147   42
##  [89,]  146  148
##  [90,]  148  149
##  [91,]  149   43
##  [92,]  149   44
##  [93,]  148   45
##  [94,]  144  150
##  [95,]  150   46
##  [96,]  150   47
##  [97,]  143   48
##  [98,]  142  151
##  [99,]  151  152
## [100,]  152  153
## [101,]  153  154
## [102,]  154  155
## [103,]  155  156
## [104,]  156   49
## [105,]  156   50
## [106,]  155   51
## [107,]  154   52
## [108,]  153   53
## [109,]  152   54
## [110,]  151  157
## [111,]  157   55
## [112,]  157  158
## [113,]  158   56
## [114,]  158  159
## [115,]  159   57
## [116,]  159   58
## [117,]  134  160
## [118,]  160  161
## [119,]  161  162
## [120,]  162  163
## [121,]  163  164
## [122,]  164  165
## [123,]  165  166
## [124,]  166  167
## [125,]  167   59
## [126,]  167   60
## [127,]  166   61
## [128,]  165  168
## [129,]  168   62
## [130,]  168   63
## [131,]  164  169
## [132,]  169  170
## [133,]  170  171
## [134,]  171   64
## [135,]  171   65
## [136,]  170   66
## [137,]  169   67
## [138,]  163   68
## [139,]  162  172
## [140,]  172  173
## [141,]  173   69
## [142,]  173   70
## [143,]  172  174
## [144,]  174   71
## [145,]  174  175
## [146,]  175   72
## [147,]  175   73
## [148,]  161  176
## [149,]  176  177
## [150,]  177   74
## [151,]  177   75
## [152,]  176  178
## [153,]  178  179
## [154,]  179  180
## [155,]  180   76
## [156,]  180  181
## [157,]  181   77
## [158,]  181   78
## [159,]  179  182
## [160,]  182   79
## [161,]  182   80
## [162,]  178   81
## [163,]  160  183
## [164,]  183   82
## [165,]  183   83
## [166,]  101  184
## [167,]  184  185
## [168,]  185  186
## [169,]  186   84
## [170,]  186   85
## [171,]  185  187
## [172,]  187  188
## [173,]  188  189
## [174,]  189  190
## [175,]  190   86
## [176,]  190  191
## [177,]  191   87
## [178,]  191   88
## [179,]  189   89
## [180,]  188   90
## [181,]  187  192
## [182,]  192  193
## [183,]  193  194
## [184,]  194   91
## [185,]  194  195
## [186,]  195   92
## [187,]  195  196
## [188,]  196   93
## [189,]  196   94
## [190,]  193   95
## [191,]  192  197
## [192,]  197  198
## [193,]  198  199
## [194,]  199   96
## [195,]  199   97
## [196,]  198   98
## [197,]  197   99
## [198,]  184  100

Some basic analyses with the tree:

# create cophenetic matrix
# patristic distances between all tips
anolisDistanceMatrix<-cophenetic(anolisTree)
head(anolisDistanceMatrix)
##                  ahli   allogus rubribarbus    imias    sagrei   bremeri
## ahli        0.0000000 0.2617775   0.4799353 1.175383 1.4313392 1.4313392
## allogus     0.2617775 0.0000000   0.4799353 1.175383 1.4313392 1.4313392
## rubribarbus 0.4799353 0.4799353   0.0000000 1.175383 1.4313392 1.4313392
## imias       1.1753834 1.1753834   1.1753834 0.000000 1.4313392 1.4313392
## sagrei      1.4313392 1.4313392   1.4313392 1.431339 0.0000000 0.5152408
## bremeri     1.4313392 1.4313392   1.4313392 1.431339 0.5152408 0.0000000
##             quadriocellifer ophiolepis   mestrei    jubar homolechis
## ahli              1.4313392  1.4313392 1.4313392 1.431339   1.431339
## allogus           1.4313392  1.4313392 1.4313392 1.431339   1.431339
## rubribarbus       1.4313392  1.4313392 1.4313392 1.431339   1.431339
## imias             1.4313392  1.4313392 1.4313392 1.431339   1.431339
## sagrei            0.5152408  0.6382528 0.8126912 1.072319   1.072319
## bremeri           0.2194873  0.6382528 0.8126912 1.072319   1.072319
##             confusus    guafe  garmani opalinus  grahami valencienni
## ahli        1.431339 1.431339 1.706787 1.706787 1.706787    1.706787
## allogus     1.431339 1.431339 1.706787 1.706787 1.706787    1.706787
## rubribarbus 1.431339 1.431339 1.706787 1.706787 1.706787    1.706787
## imias       1.431339 1.431339 1.706787 1.706787 1.706787    1.706787
## sagrei      1.072319 1.072319 1.706787 1.706787 1.706787    1.706787
## bremeri     1.072319 1.072319 1.706787 1.706787 1.706787    1.706787
##             lineatopus reconditus evermanni stratulus    krugi pulchellus
## ahli          1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
## allogus       1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
## rubribarbus   1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
## imias         1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
## sagrei        1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
## bremeri       1.706787   1.706787  1.840857  1.840857 1.840857   1.840857
##             gundlachi poncensis    cooki cristatellus brevirostris
## ahli         1.840857  1.840857 1.840857     1.840857     1.840857
## allogus      1.840857  1.840857 1.840857     1.840857     1.840857
## rubribarbus  1.840857  1.840857 1.840857     1.840857     1.840857
## imias        1.840857  1.840857 1.840857     1.840857     1.840857
## sagrei       1.840857  1.840857 1.840857     1.840857     1.840857
## bremeri      1.840857  1.840857 1.840857     1.840857     1.840857
##             caudalis   marron websteri distichus barbouri  alumina
## ahli        1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
## allogus     1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
## rubribarbus 1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
## imias       1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
## sagrei      1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
## bremeri     1.840857 1.840857 1.840857  1.840857 1.910632 1.910632
##             semilineatus  olssoni etheridgei  fowleri insolitus whitemani
## ahli            1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
## allogus         1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
## rubribarbus     1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
## imias           1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
## sagrei          1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
## bremeri         1.910632 1.910632   1.910632 1.910632  1.910632  1.910632
##             haetianus breslini  armouri  cybotes  shrevei longitibialis
## ahli         1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
## allogus      1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
## rubribarbus  1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
## imias        1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
## sagrei       1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
## bremeri      1.910632 1.910632 1.910632 1.910632 1.910632      1.910632
##              strahmi marcanoi baleatus barahonae ricordii eugenegrahami
## ahli        1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
## allogus     1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
## rubribarbus 1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
## imias       1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
## sagrei      1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
## bremeri     1.910632 1.910632 1.910632  1.910632 1.910632      1.910632
##             christophei  cuvieri barbatus   porcus chamaeleonides
## ahli           1.910632 1.910632 1.910632 1.910632       1.910632
## allogus        1.910632 1.910632 1.910632 1.910632       1.910632
## rubribarbus    1.910632 1.910632 1.910632 1.910632       1.910632
## imias          1.910632 1.910632 1.910632 1.910632       1.910632
## sagrei         1.910632 1.910632 1.910632 1.910632       1.910632
## bremeri        1.910632 1.910632 1.910632 1.910632       1.910632
##             guamuhaya altitudinalis oporinus isolepis allisoni porcatus
## ahli         1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
## allogus      1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
## rubribarbus  1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
## imias        1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
## sagrei       1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
## bremeri      1.910632      1.910632 1.910632 1.910632 1.910632 1.910632
##             argillaceus centralis  pumilis loysiana  guazuma placidus
## ahli           1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
## allogus        1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
## rubribarbus    1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
## imias          1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
## sagrei         1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
## bremeri        1.910632  1.910632 1.910632 1.910632 1.910632 1.910632
##             sheplani  alayoni angusticeps paternus alutaceus inexpectatus
## ahli        1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
## allogus     1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
## rubribarbus 1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
## imias       1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
## sagrei      1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
## bremeri     1.910632 1.910632    1.910632 1.910632  1.910632     1.910632
##             clivicola cupeyalensis cyanopleurus  alfaroi macilentus
## ahli         1.910632     1.910632     1.910632 1.910632   1.910632
## allogus      1.910632     1.910632     1.910632 1.910632   1.910632
## rubribarbus  1.910632     1.910632     1.910632 1.910632   1.910632
## imias        1.910632     1.910632     1.910632 1.910632   1.910632
## sagrei       1.910632     1.910632     1.910632 1.910632   1.910632
## bremeri      1.910632     1.910632     1.910632 1.910632   1.910632
##             vanidicus argenteolus   lucius bartschi vermiculatus baracoae
## ahli         1.910632    1.910632 1.910632        2            2        2
## allogus      1.910632    1.910632 1.910632        2            2        2
## rubribarbus  1.910632    1.910632 1.910632        2            2        2
## imias        1.910632    1.910632 1.910632        2            2        2
## sagrei       1.910632    1.910632 1.910632        2            2        2
## bremeri      1.910632    1.910632 1.910632        2            2        2
##             noblei smallwoodi luteogularis equestris monticola
## ahli             2          2            2         2         2
## allogus          2          2            2         2         2
## rubribarbus      2          2            2         2         2
## imias            2          2            2         2         2
## sagrei           2          2            2         2         2
## bremeri          2          2            2         2         2
##             bahorucoensis dolichocephalus hendersoni darlingtoni aliniger
## ahli                    2               2          2           2        2
## allogus                 2               2          2           2        2
## rubribarbus             2               2          2           2        2
## imias                   2               2          2           2        2
## sagrei                  2               2          2           2        2
## bremeri                 2               2          2           2        2
##             singularis chlorocyanus coelestinus occultus
## ahli                 2            2           2        2
## allogus              2            2           2        2
## rubribarbus          2            2           2        2
## imias                2            2           2        2
## sagrei               2            2           2        2
## bremeri              2            2           2        2
# tree manipulations
# dropping tips
anolisTree2<-drop.tip(anolisTree, "allogus")
anolisTree
## 
## Phylogenetic tree with 100 tips and 99 internal nodes.
## 
## Tip labels:
##  ahli, allogus, rubribarbus, imias, sagrei, bremeri, ...
## 
## Rooted; includes branch lengths.
anolisTree2
## 
## Phylogenetic tree with 99 tips and 98 internal nodes.
## 
## Tip labels:
##  ahli, rubribarbus, imias, sagrei, bremeri, quadriocellifer, ...
## 
## Rooted; includes branch lengths.
# drop more than one species
anolisTree3<-drop.tip(anolisTree, c("allogus", "sagrei", "bremeri"))
anolisTree3
## 
## Phylogenetic tree with 97 tips and 96 internal nodes.
## 
## Tip labels:
##  ahli, rubribarbus, imias, quadriocellifer, ophiolepis, mestrei, ...
## 
## Rooted; includes branch lengths.
# extras: other good tree functions
# branching times from root to present day
branching.times(anolisTree)
##        101        102        103        104        105        106 
## 1.00000000 0.95531585 0.92042853 0.85339333 0.71566962 0.58769170 
##        107        108        109        110        111        112 
## 0.23996763 0.13088873 0.53615967 0.40634562 0.31912640 0.25762040 
##        113        114        115        116        117        118 
## 0.10974365 0.25154444 0.20938867 0.11886595 0.56014355 0.43753069 
##        119        120        121        122        123        124 
## 0.21972078 0.20003358 0.47137106 0.71522991 0.56567233 0.21352027 
##        125        126        127        128        129        130 
## 0.48839988 0.45804910 0.32675607 0.38646601 0.39528819 0.40082733 
##        131        132        133        134        135        136 
## 0.30246984 0.27574235 0.17049746 0.94529239 0.86931240 0.80210850 
##        137        138        139        140        141        142 
## 0.74969853 0.48747487 0.26810769 0.58830722 0.37709384 0.79064068 
##        143        144        145        146        147        148 
## 0.54011316 0.44868314 0.34202713 0.33660524 0.26698341 0.19255813 
##        149        150        151        152        153        154 
## 0.14839095 0.25212533 0.58167227 0.47403372 0.38308038 0.29796936 
##        155        156        157        158        159        160 
## 0.09436721 0.04173045 0.14679427 0.09310584 0.07630236 0.86291352 
##        161        162        163        164        165        166 
## 0.76856037 0.68968992 0.64324875 0.55802012 0.52098521 0.26709312 
##        167        168        169        170        171        172 
## 0.17488994 0.29602293 0.37481839 0.13919277 0.11421652 0.56432394 
##        173        174        175        176        177        178 
## 0.18695796 0.37938181 0.21721270 0.52511355 0.12086197 0.46416131 
##        179        180        181        182        183        184 
## 0.45493345 0.33595980 0.08606303 0.28023394 0.65643319 0.89493337 
##        185        186        187        188        189        190 
## 0.77418514 0.52472537 0.73465007 0.10493062 0.08703142 0.05853978 
##        191        192        193        194        195        196 
## 0.02140618 0.66993315 0.63704578 0.60555377 0.38411007 0.15092709 
##        197        198        199 
## 0.52962269 0.31606003 0.17835427
# do the tips all line up?
is.ultrametric(anolisTree)
## [1] TRUE
# is the tree fully resolved? 
is.binary.tree(anolisTree)
## [1] TRUE
# check monophyly of clades
putativeClade<-c("ahli", "allogus", "rubibarbus")
is.monophyletic(anolisTree, putativeClade)
## [1] TRUE
# reroot the tree
rer<-root(anolisTree, outgroup="allogus")
plot(rer)

Now you try! Randomly drop 5 species from the tree, then reroot using a random taxon as outgroup. How does this affect your cophenetic distance matrix?

We can also read in many trees at once

# reading in many trees
manyTrees<-read.tree("/Users/lukeharmon/Documents/teaching/r_course/data/multiple_trees.phy")
manyTrees
## 100 phylogenetic trees
class(manyTrees)
## [1] "multiPhylo"
# access individual trees using [[ ]]
# first tree is 
manyTrees[[1]]
## 
## Phylogenetic tree with 84 tips and 83 internal nodes.
## 
## Tip labels:
##  Kogia_breviceps_KBU72040__, Kogia_simus_AF304072, Physeter_catodon_X75589, Platanista_gangetica_AF304070_, Platanista_minor_X92543, Tasmacetus_shepherdi_AF334484, ...
## 
## Rooted; includes branch lengths.
class(manyTrees[[1]])
## [1] "phylo"
plot(manyTrees[[1]])
plot(manyTrees[[45]])

# create a loop to plot all trees
pdf(file="multiTree.pdf")
for(i in 1:10) {
    plot(manyTrees[[i]])
}
dev.off()
## quartz_off_screen 
##                 2

Finally, we can combine tree and data

dim(aData)
## [1] 100  10
anolisTree
## 
## Phylogenetic tree with 100 tips and 99 internal nodes.
## 
## Tip labels:
##  ahli, allogus, rubribarbus, imias, sagrei, bremeri, ...
## 
## Rooted; includes branch lengths.
library(geiger)
name.check(anolisTree, aData)
## [1] "OK"
# EXTRA: this works - they match
# but sometimes you have to match tree and tip data
matchedData<-treedata(anolisTree, aData)

What if the match is poor?

phelData<-read.csv("phelsuma_messy.csv")
rownames(phelData)<-phelData[,1]
phelData<-phelData[,-1]

phelTree<-read.tree("phel2.phy")

# nothing matches!
name.check(phelTree, phelData)
## $tree_not_data
##  [1] "abbotti"        "astriata"       "barbouri"       "borbonica"     
##  [5] "cepedianaA"     "comorensis"     "dubia"          "grandis"       
##  [9] "guentheri"      "guimbeaui"      "guttata"        "laticauda"     
## [13] "lineata"        "longinsulae"    "mutabilis"      "ornata"        
## [17] "quadriocellata" "robertmertensi" "standingi"      "sunbergi"      
## 
## $data_not_tree
##  [1] "Phelsuma_abbotti"        "Phelsuma_astriata"      
##  [3] "Phelsuma_barbouri"       "Phelsuma_borbonica"     
##  [5] "Phelsuma_cepedianaA"     "Phelsuma_comorensis"    
##  [7] "Phelsuma_dubia"          "Phelsuma_grandis"       
##  [9] "Phelsuma_guentheri"      "Phelsuma_guimbeaui"     
## [11] "Phelsuma_guttatus"       "Phelsuma_laticauda"     
## [13] "Phelsuma_lineata"        "Phelsuma_longinsulae"   
## [15] "Phelsuma_mutabila"       "Phelsuma_ornata"        
## [17] "Phelsuma_quadriocellata" "Phelsuma_robertmertensi"
## [19] "Phelsuma_standingi"      "Phelsuma_sunbergi"
# let's try fuzzy matching
# example with first name

dd<-adist(rownames(phelData)[5], phelTree$tip.label, ignore.case = T)
mm<-which(dd==min(dd))
phelTree$tip.label[mm]
## [1] "dubia"
distMatrix<-adist(rownames(phelData), phelTree$tip.label)

foo<-function(x) {
  namePick<-which(x==min(x))
  return(phelTree$tip.label[namePick])
}

goodNames<-apply(distMatrix, 1, foo)
goodNames<-unlist(goodNames)

cbind(rownames(phelData), goodNames)
##                                 goodNames       
##  [1,] "Phelsuma_abbotti"        "abbotti"       
##  [2,] "Phelsuma_astriata"       "astriata"      
##  [3,] "Phelsuma_borbonica"      "borbonica"     
##  [4,] "Phelsuma_cepedianaA"     "cepedianaA"    
##  [5,] "Phelsuma_dubia"          "dubia"         
##  [6,] "Phelsuma_guentheri"      "guentheri"     
##  [7,] "Phelsuma_guttatus"       "guttata"       
##  [8,] "Phelsuma_laticauda"      "laticauda"     
##  [9,] "Phelsuma_lineata"        "lineata"       
## [10,] "Phelsuma_grandis"        "grandis"       
## [11,] "Phelsuma_mutabila"       "mutabilis"     
## [12,] "Phelsuma_ornata"         "ornata"        
## [13,] "Phelsuma_quadriocellata" "quadriocellata"
## [14,] "Phelsuma_longinsulae"    "longinsulae"   
## [15,] "Phelsuma_standingi"      "standingi"     
## [16,] "Phelsuma_sunbergi"       "sunbergi"      
## [17,] "Phelsuma_barbouri"       "barbouri"      
## [18,] "Phelsuma_guimbeaui"      "guimbeaui"     
## [19,] "Phelsuma_comorensis"     "comorensis"    
## [20,] "Phelsuma_robertmertensi" "robertmertensi"
rownames(phelData)<-goodNames
name.check(phelTree, phelData)
## [1] "OK"

Now try to match another dataset.