ape包02:phylo類,畫圖參數(shù)設(shè)置

#首先導(dǎo)入ape
if(!require(ape)){
    install.packages("ape")  #'ape' will be installed if needed
    library(ape)
}

tree使用 “phylo” 類

我們使用 rtree() function 生成一個樹,參數(shù) n= 30個 tips (可以理解為末端節(jié)點)

phy <- rtree(n=30)    #n specifies the number of tips we want.
plot(phy)
image.png

查看樹是由哪些數(shù)據(jù)構(gòu)成的,我們先來理解下“phylo”類

class(phy)     #查看phy類型
## [1] "phylo"

phy   #包含30個tips 和29個內(nèi)部節(jié)點(可以理解為樹的分叉點)
## Phylogenetic tree with 30 tips and 29 internal nodes.
## 
## Tip labels:
##  t13, t28, t8, t22, t19, t16, ...
## 
## Rooted; includes branch lengths.
str(phy)  #其實phy類似于list類型
## List of 4
##  $ edge       : int [1:58, 1:2] 31 32 33 34 35 36 37 37 38 38 ...
##  $ tip.label  : chr [1:30] "t13" "t28" "t8" "t22" ...
##  $ edge.length: num [1:58] 0.2235 0.4492 0.0535 0.9588 0.2777 ...
##  $ Nnode      : int 29
##  - attr(*, "class")= chr "phylo"
##  - attr(*, "order")= chr "cladewise"

依次來看一下各數(shù)據(jù)結(jié)構(gòu)及代表什么

head(phy$edge)    #一個兩列的數(shù)據(jù)框
##      [,1] [,2]
## [1,]   31   32
## [2,]   32   33
## [3,]   33   34
##  ..

phy$tip.label    #樹末端的label
##  [1] "t13" "t28" "t8"  "t22" "t19" "t16" "t7"  "t11" "t4"  "t24" "t17"
## [12] "t26" "t15" "t1"  "t12" "t30" "t27" "t6"  "t29" "t3"  "t20" "t25"
## [23] "t10" "t21" "t23" "t14" "t18" "t9"  "t5"  "t2"

phy$edge.length   #樹枝的長度
##  [1] 0.223515275 0.449214761 0.053517260 0.958796252 0.277726818
## ..
## [46] 0.339903616 0.173343718 0.993939807 0.176320243 0.087404924
## [51] 0.113624431 0.108095456 0.150655877 0.317285047 0.572281965
## [56] 0.019839419 0.529848145 0.998013315

phy$Nnode  #中間節(jié)點的個數(shù)
## [1] 29

Newick格式的tree文件

我們構(gòu)建一個tips為 A-F的樹,使用 read.tree()讀取

mini.phy <- read.tree(text = "((((A,B), C), (D,E)),F);") #defining a tree in bracket form
plot(mini.phy, cex=2)
image.png

這個tree只包含3個數(shù)據(jù),因為我們沒提供edge的長度

str(mini.phy)
## List of 3
##  $ edge     : int [1:10, 1:2] 7 8 9 10 10 9 8 11 11 7 ...
##  $ tip.label: chr [1:6] "A" "B" "C" "D" ...
##  $ Nnode    : int 5
##  - attr(*, "class")= chr "phylo"
##  - attr(*, "order")= chr "cladewise"

我們看看這些邊(edges)是怎么表示的

mini.phy$edge
##       [,1] [,2]
##  [1,]    7    8
##  [2,]    8    9
##  [3,]    9   10
##  [4,]   10    1
##  [5,]   10    2
##  [6,]    9    3
##  [7,]    8   11
##  [8,]   11    4
##  [9,]   11    5
## [10,]    7    6

我們看到一個矩陣有10行和2列。這個矩陣表示節(jié)點和下一節(jié)點組合,定義了樹的每一個分支。下面圖中標(biāo)出每個節(jié)點的數(shù)字

plot(mini.phy, label.offset=0.2)
nodelabels() #add node numbers
tiplabels()    #add tip numbers
image.png

使用tiplabels()給tip加上不同的顏色

mycol<-c("blue", "blue", "blue", "red", "red", "red") #顏色向量
plot(mini.phy, adj=0, label.offset=0.75)  #adjust if necessary/desired!
tiplabels(pch=21, col="black", adj=1, bg=mycol, cex=2) #ditto! try different values!
image.png

交換樹枝的位置

有時旋轉(zhuǎn)節(jié)點是很有必要的,使用rotate()function。繼續(xù)使用mini.phy數(shù)據(jù)

plot(mini.phy, label.offset=0.2)   #same as before
nodelabels()  #add node numbers
tiplabels()   #add tip numbers
image.png

交換分支clades (D, E) and (A, B, C)? 圖中可以看出他們的最近共同祖先是node 8.

rot.phy <- rotate(mini.phy, node=8)
plot(rot.phy, label.offset=0.2)   #still the same as before
nodelabels()          
tiplabels()  
image.png

找到指定分支下的所有tips,可以使用geiger包 的the tips() function得到分支下所有端點

library(geiger)
cladeABC <- tips(rot.phy, node=9) # node 9 defines the clade composed of (A, B, C)
cladeABC
## [1] "A" "B" "C"

另外還有個修剪枝的函數(shù)drop.tip(),下面是剪切掉cladeABC分支

pruned.phy <- drop.tip(rot.phy, cladeABC)
plot(pruned.phy, label.offset=0.2)
nodelabels()
tiplabels() #did it work?
image.png

It may also be useful to select all branches in a specific group of tips. This is implemented in theapepackage; the which.edge() function finds all edges in a specified group. For example, let’s identify all branches of the cladeABC group as defined above.

cladeABCbranches <- which.edge(rot.phy, cladeABC) 
#cladeABC was defined earlier, using the tips() function
cladeABCbranches #this should be a numerical vector containing 6, 7, 8, 9
## [1] 6 7 8 9

And as we can see, rows 6-9 of the
edge.length matrix represent the expected branches. Let’s first plot the tree, and then look at the edge matrix for cross-checking.

plot(rot.phy, label.offset=0.2) #we are sticking to the same plot settings
nodelabels()
tiplabels()
image.png
rot.phy$edge
##       [,1] [,2]
##  [1,]    7    8
##  [2,]    8   11
##  [3,]   11    4
##  [4,]   11    5
##  [5,]    8    9
##  [6,]    9   10
##  [7,]   10    1
##  [8,]   10    2
##  [9,]    9    3
## [10,]    7    6

Here would be a good opportunity to show you how to assign different branch colors. For example, how can one emphasize the branches of the clade formed by A, B, and C? We first create a color vector, only consisting of grey colors. Then we’ll assign black to all branches of clade ABC.

clcolr <- rep("darkgrey", dim(rot.phy$edge)[1]) #defining a color vector; look up rep() and dim() functions! 
clcolr[cladeABCbranches] <- "red" #specifying color of branches in clade (A, B, C)
lot(rot.phy, edge.color=clcolr, edge.width=2) #also making the branches a little thicker
image.png

Let’s conclude this section with one last exercise: combining trees. Assume you have two different phylogenies, with two different sets of taxa (no overlap). Another assumption is that you have knowledge how the trees may fit together. Then the bind.tree() function of ape package can help. The function takes in two phylo objects. The position of where the trees are bound is defined by tip- or node number within the first tree. Note that you can also specify the “root” as binding position.

#the first tree
tree1 <- rtree(n=10)
tree1$tip.label <- rep("apples", 10) #renaming labels to enhance readability
plot(tree1, label.offset=0.1); nodelabels(); tiplabels()
image.png
#the second tree
tree2 <- rtree(n=10)
tree2$tip.label <- rep("oranges", 10)
plot(tree2)
image.png
#merging trees
combined.tree <- bind.tree(tree1, tree2, where=5) #combining trees at position "5"
plot(combined.tree)
image.png

Try at least two different merging positions. For each example, plot the two original trees and the merged version in one figure.

Fully resolved and polytomous trees

All tree examples so far were fully resolved, i.e. each tree was fully binary. It’s very easy to access visually for small trees, but one can also do this more formally:

is.binary.tree(mini.phy)
## [1] TRUE

Let’s make a tree that is not fully resolved.

poly.phy <- read.tree(text = "(((A,B,C),(D,E)),F);") #A, B, and C form a polytomy
plot(poly.phy)
image.png

Is this tree binary?

is.binary.tree(poly.phy) # "FALSE"!
## [1] FALSE

OK. Many comparative methods require fully resolved trees. But what to do if that’s not the case? The multi2di() function can resolve polytomies, either randomly or in the order in which they appear in the tree. The default setting is to resolve polytomies randomly.

resolved.phy <- multi2di(poly.phy)
is.binary.tree(resolved.phy)
## [1] TRUE
plot(resolved.phy) # visual inspection.
image.png

If you repeat the above lines a few times you will see the effect of randomly resolving the polytomy.

Please plot two randomly resolved trees next to each other!

畫圖參數(shù)設(shè)置

通過修改參數(shù),改變tree的樣式

phy <- rtree(n=10) # n specifies the number of tips we want.
plot(phy) #default orientation is right-handed
plot(phy, direction="upwards",  # "rightwards" (default), "leftwards", and "downwards"
     show.tip.label=FALSE,  #顯示tip.label
     #cex=.3       #tip.lable 字體大小
     edge.width=2,               #枝寬度
     edge.color="blue")       # 樹枝顏色 ;藍色
image.png

There are many other options… type ?plot.phylo in the console to read more. One last useful command I’d like to introduce is the ladderize() function. It reorganizes the tree structure, normally yielding much more readable trees.

ladderized.phy <- ladderize(phy)
plot(ladderized.phy, direction="upwards", 
     show.tip.label=FALSE, edge.width=2, edge.color="blue")
image.png

來自:http://schmitzlab.info/phylo.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,011評論 0 23
  • 今天中午在辦公室小憩一會,忽然一個朋友給我發(fā)消息:超哥,我要辭職了。我一下子醒了,說道:怎么了?工作上有什么問題么...
    水田夏木閱讀 373評論 0 0
  • 嫁個上海人 “要我說,你還是老老實實地在上海待著,找個上海人嫁了比較實在。”某次向老娘匯報完近況,她老人家聽了我瘋...
    雪利說閱讀 691評論 4 2
  • 【讀】 【以】 感情修復(fù)嘗試:夫妻雙方共同努力,通過一些語言或行動(不管是愚蠢的還是聰明的)來防止消極情緒的升級而...
    小米女_水草萍閱讀 414評論 0 1
  • 靜待花開 學(xué)而思 養(yǎng)育男孩 今天靜怡老師帶讀的是《養(yǎng)育男孩》,作為兩個男孩的媽媽我非常期待。今天主要講...
    gxl水月亮閱讀 344評論 0 0

友情鏈接更多精彩內(nèi)容