git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@123191 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -4,10 +4,11 @@ Title: an R package for visualization and annotation of phylogenetic trees with |
4 | 4 |
Version: 1.7.1 |
5 | 5 |
Authors@R: c( |
6 | 6 |
person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre")), |
7 |
- person("Tommy Tsan-Yuk", "Lam", email = "tylam.tommy@gmail.com", rol = "ths"), |
|
7 |
+ person("Tommy Tsan-Yuk", "Lam", email = "tylam.tommy@gmail.com", rol = c("aut", "ths")), |
|
8 | 8 |
person("Justin", "Silverman", email = "jsilve24@gmail.com", rol = "ctb", comment = "geom_balance"), |
9 | 9 |
person("Casey", "Dunn", email = "casey_dunn@brown.edu", rol = "ctb", |
10 |
- comment = "propose using txtConnection so that parser functions can use tree strings as input") |
|
10 |
+ comment = c("propose using txtConnection so that parser functions can use tree strings as input", |
|
11 |
+ "modified nhx parsing to retain tip node numbers")) |
|
11 | 12 |
) |
12 | 13 |
Maintainer: Guangchuang Yu <guangchuangyu@gmail.com> |
13 | 14 |
Description: 'ggtree' extends the 'ggplot2' plotting system which implemented the grammar of graphics. |
... | ... |
@@ -126,6 +126,7 @@ export(theme_tree) |
126 | 126 |
export(theme_tree2) |
127 | 127 |
export(viewClade) |
128 | 128 |
export(write.jplace) |
129 |
+export(xlim_tree) |
|
129 | 130 |
exportClasses(apeBootstrap) |
130 | 131 |
exportClasses(beast) |
131 | 132 |
exportClasses(codeml) |
... | ... |
@@ -189,6 +190,7 @@ importFrom(ggplot2,element_rect) |
189 | 190 |
importFrom(ggplot2,facet_grid) |
190 | 191 |
importFrom(ggplot2,fortify) |
191 | 192 |
importFrom(ggplot2,geom_bar) |
193 |
+importFrom(ggplot2,geom_blank) |
|
192 | 194 |
importFrom(ggplot2,geom_rect) |
193 | 195 |
importFrom(ggplot2,geom_segment) |
194 | 196 |
importFrom(ggplot2,geom_text) |
... | ... |
@@ -1,5 +1,11 @@ |
1 | 1 |
CHANGES IN VERSION 1.7.1 |
2 | 2 |
------------------------ |
3 |
+ o xlim_tree layer and test <2016-10-31, Mon> |
|
4 |
+ + set x axis limit for Tree panel for facet_plot |
|
5 |
+ o update read.nhx <2016-10-30, Sun> |
|
6 |
+ + add tip numbers to @nhx_tags and add tests |
|
7 |
+ + https://github.com/GuangchuangYu/ggtree/pull/83 |
|
8 |
+ + store nhx_tags$node as numeric values <2016-10-31, Mon> |
|
3 | 9 |
o facet_plot supports ggbio::geom_alignment <2016-10-26, Wed> |
4 | 10 |
+ https://github.com/tengfei/ggbio/issues/83 |
5 | 11 |
o make tree stats available in facet_plot <2016-10-24, Mon> |
... | ... |
@@ -5,7 +5,7 @@ |
5 | 5 |
##' @param file nhx file |
6 | 6 |
##' @return nhx object |
7 | 7 |
##' @export |
8 |
-##' @author Guangchuang Yu \url{http://ygc.name} |
|
8 |
+##' @author Guangchuang Yu \url{https://guangchuangyu.github.io} |
|
9 | 9 |
read.nhx <- function(file) { |
10 | 10 |
treetext <- suppressWarnings(readLines(file)) |
11 | 11 |
treetext <- treetext[treetext != ""] |
... | ... |
@@ -27,7 +27,7 @@ read.nhx <- function(file) { |
27 | 27 |
|
28 | 28 |
phylo2 <- read.tree(text = tree2) |
29 | 29 |
treeinfo <- fortify(phylo2) |
30 |
- node <- as.character(treeinfo$node[match(nlab, treeinfo$label)]) |
|
30 |
+ node <- treeinfo$node[match(nlab, sub(".+(X\\d+)$","\\1",treeinfo$label))] # as.character |
|
31 | 31 |
|
32 | 32 |
nhx.matches <- gregexpr("(\\w+)?(:?\\d*\\.?\\d*[Ee]?[\\+\\-]?\\d*)?\\[&&NHX.*?\\]", treetext) |
33 | 33 |
matches <- nhx.matches[[1]] |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
##' plot tree associated data in an additional panel |
2 | 2 |
##' |
3 |
-##' |
|
3 |
+##' |
|
4 | 4 |
##' @title facet_plot |
5 | 5 |
##' @param p tree view |
6 | 6 |
##' @param panel panel name for plot of input data |
... | ... |
@@ -27,3 +27,18 @@ add_panel <- function(p, panel) { |
27 | 27 |
p$data <- df |
28 | 28 |
p + facet_grid(.~panel, scales="free_x") |
29 | 29 |
} |
30 |
+ |
|
31 |
+##' set x axis limits for Tree panel |
|
32 |
+##' |
|
33 |
+##' |
|
34 |
+##' @title xlim_tree |
|
35 |
+##' @param xlim xlim, should be of length 2 |
|
36 |
+##' @return updated tree view |
|
37 |
+##' @export |
|
38 |
+##' @importFrom ggplot2 geom_blank |
|
39 |
+##' @author guangchuang yu |
|
40 |
+xlim_tree <- function(xlim) { |
|
41 |
+ dummy <- data.frame(x=xlim, panel='Tree') |
|
42 |
+ geom_blank(aes_(x=~x), dummy, inherit.aes = FALSE) |
|
43 |
+} |
|
44 |
+ |
... | ... |
@@ -111,7 +111,7 @@ |
111 | 111 |
} else if (is(data, "data.frame") || is(data, "tbl_df")) { |
112 | 112 |
data <- as.data.frame(data) |
113 | 113 |
## res <- merge(df[, c('label', 'y')], data, by.x='label', by.y=1) ## , all.x=TRUE) |
114 |
- res <- merge(df, data, by.x='label', by.y=1) ## , all.x=TRUE) |
|
114 |
+ res <- merge(df[, !names(df) %in% c('node', 'parent', 'x', 'branch', 'angle')], data, by.x='label', by.y=1) |
|
115 | 115 |
res$panel <- factor(lv[length(lv)], levels=lv) |
116 | 116 |
} else { |
117 | 117 |
stop("input 'data' is not supported...") |
... | ... |
@@ -3,7 +3,7 @@ ggtree: an R package for visualization and annotation of phylogenetic trees with |
3 | 3 |
|
4 | 4 |
[](https://bioconductor.org/packages/ggtree) [](https://github.com/GuangchuangYu/ggtree) [](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#since) [](https://bioconductor.org/packages/stats/bioc/ggtree) [](https://bioconductor.org/packages/stats/bioc/ggtree) |
5 | 5 |
|
6 |
-[](http://www.repostatus.org/#active) [](https://codecov.io/gh/GuangchuangYu/ggtree) [](https://github.com/GuangchuangYu/ggtree/commits/master) [](https://github.com/GuangchuangYu/ggtree/network) [](https://github.com/GuangchuangYu/ggtree/stargazers) [](https://awesome-r.com/#awesome-r-graphic-displays) |
|
6 |
+[](http://www.repostatus.org/#active) [](https://codecov.io/gh/GuangchuangYu/ggtree) [](https://github.com/GuangchuangYu/ggtree/commits/master) [](https://github.com/GuangchuangYu/ggtree/network) [](https://github.com/GuangchuangYu/ggtree/stargazers) [](https://awesome-r.com/#awesome-r-graphic-displays) |
|
7 | 7 |
|
8 | 8 |
[](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#archives) [](https://bioconductor.org/checkResults/devel/bioc-LATEST/ggtree/) [](https://travis-ci.org/GuangchuangYu/ggtree) [](https://ci.appveyor.com/project/GuangchuangYu/ggtree) [](http://bioconda.github.io/recipes/bioconductor-ggtree/README.html) |
9 | 9 |
|
... | ... |
@@ -17,7 +17,7 @@ Please cite the following article when using `ggtree`: |
17 | 17 |
|
18 | 18 |
**G Yu**, DK Smith, H Zhu, Y Guan, TTY Lam<sup>\*</sup>. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. ***Methods in Ecology and Evolution***. *accepted* |
19 | 19 |
|
20 |
-[](http://dx.doi.org/10.1111/2041-210X.12628) [](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627) [](https://www.altmetric.com/details/10533079) |
|
20 |
+[](http://dx.doi.org/10.1111/2041-210X.12628) [](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627) [](https://www.altmetric.com/details/10533079) |
|
21 | 21 |
|
22 | 22 |
------------------------------------------------------------------------ |
23 | 23 |
|
22 | 22 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,21 @@ |
1 |
+% Generated by roxygen2: do not edit by hand |
|
2 |
+% Please edit documentation in R/facet_plot.R |
|
3 |
+\name{xlim_tree} |
|
4 |
+\alias{xlim_tree} |
|
5 |
+\title{xlim_tree} |
|
6 |
+\usage{ |
|
7 |
+xlim_tree(xlim) |
|
8 |
+} |
|
9 |
+\arguments{ |
|
10 |
+\item{xlim}{xlim, should be of length 2} |
|
11 |
+} |
|
12 |
+\value{ |
|
13 |
+updated tree view |
|
14 |
+} |
|
15 |
+\description{ |
|
16 |
+set x axis limits for Tree panel |
|
17 |
+} |
|
18 |
+\author{ |
|
19 |
+guangchuang yu |
|
20 |
+} |
|
21 |
+ |
0 | 22 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,77 @@ |
1 |
+context('nhx') |
|
2 |
+ |
|
3 |
+# Some sample NHX tree strings |
|
4 |
+test_nhx_text = "(((ADH2:0.1[&&NHX:S=human], ADH1:0.11[&&NHX:S=human]):0.05[&&NHX:S=primates:D=Y:B=100], ADHY:0.1[&&NHX:S=nematode],ADHX:0.12[&&NHX:S=insect]):0.1[&&NHX:S=metazoa:D=N], (ADH4:0.09[&&NHX:S=yeast],ADH3:0.13[&&NHX:S=yeast], ADH2:0.12[&&NHX:S=yeast],ADH1:0.11[&&NHX:S=yeast]):0.1 [&&NHX:S=Fungi])[&&NHX:D=N];" |
|
5 |
+ |
|
6 |
+test_phyldog_nhx_text = "(((Prayidae_D27SS7@2825365:0.0682841[&&NHX:Ev=S:S=58:ND=0],(Kephyes_ovata@2606431:0.0193941[&&NHX:Ev=S:S=69:ND=1],Chuniphyes_multidentata@1277217:0.0121378[&&NHX:Ev=S:S=70:ND=2]):0.0217782[&&NHX:Ev=S:S=60:ND=3]):0.0607598[&&NHX:Ev=S:S=36:ND=4],((Apolemia_sp_@1353964:0.11832[&&NHX:Ev=S:S=31:ND=9],(((Bargmannia_amoena@263997:0.0144549[&&NHX:Ev=S:S=37:ND=10],Bargmannia_elongata@946788:0.0149723[&&NHX:Ev=S:S=38:ND=11]):0.0925388[&&NHX:Ev=S:S=33:ND=12],Physonect_sp_@2066767:0.077429[&&NHX:Ev=S:S=61:ND=13]):0.0274637[&&NHX:Ev=S:S=24:ND=14],(Stephalia_dilata@2960089:0.0761163[&&NHX:Ev=S:S=52:ND=15],((Frillagalma_vityazi@1155031:0.0906068[&&NHX:Ev=S:S=53:ND=16],Resomia_ornicephala@3111757:1e-06[&&NHX:Ev=S:S=54:ND=17]):1e-06[&&NHX:Ev=S:S=45:ND=18],((Lychnagalma_utricularia@2253871:0.120851[&&NHX:Ev=S:S=65:ND=19],Nanomia_bijuga@717864:0.133939[&&NHX:Ev=S:S=71:ND=20]):1e-06[&&NHX:Ev=S:S=56:ND=21],Cordagalma_sp_@1525873:0.0693814[&&NHX:Ev=S:S=64:ND=22]):1e-06[&&NHX:Ev=S:S=46:ND=23]):0.0333823[&&NHX:Ev=S:S=40:ND=24]):1e-06[&&NHX:Ev=S:S=35:ND=25]):0.0431861[&&NHX:Ev=D:S=24:ND=26]):1e-06[&&NHX:Ev=S:S=19:ND=27],Rhizophysa_filiformis@3073669:0.22283[&&NHX:Ev=S:S=26:ND=28]):0.0292362[&&NHX:Ev=S:S=17:ND=29]):0.185603[&&NHX:Ev=D:S=17:ND=8],(Hydra_magnipapillata@52244:0.0621782[&&NHX:Ev=S:S=16:ND=5],Ectopleura_larynx@3556167:0.332505[&&NHX:Ev=S:S=15:ND=6]):0.185603[&&NHX:Ev=S:S=12:ND=7])[&&NHX:Ev=S:S=9:ND=30];" |
|
7 |
+ |
|
8 |
+test_notung_nhx_text = "((((Rhizophysa_filiformis@2564549:0.09666991738603078[&&NHX:S=Rhizophysa_filiformis],((Marrus_claudanielis@2027078:0.03368582974818837[&&NHX:S=Marrus_claudanielis],((Erenna_richardi@1434201:0.014306889954561298[&&NHX:S=Erenna_richardi],Marrus_claudanielis@2027079:0.010842363778569869[&&NHX:S=Marrus_claudanielis])n5940011:0.01779384958849464[&&NHX:S=n57:D=N],(((Agalma_elegans@88626:0.05872379503260147[&&NHX:S=Agalma_elegans],Lychnagalma_utricularia@1828459:0.04211137470826968[&&NHX:S=Lychnagalma_utricularia])n5940018:0.02375590664436535[&&NHX:S=n47:D=N],(((Bargmannia_amoena@3459111:0.19058396964770352[&&NHX:S=Bargmannia_amoena],Bargmannia_elongata@469437:1.00000050002909E-6[&&NHX:S=Bargmannia_elongata])n5939974:0.11560220708003867[&&NHX:S=n22:D=N],Cordagalma_sp_@1115328:0.04829417133033771[&&NHX:S=Cordagalma_sp_])n5939976:0.011316847557531757[&&NHX:S=n62:D=N],Forskalia_asymmetrica@1220430:0.01667566952752948[&&NHX:S=Forskalia_asymmetrica])n5939978:0.0063213422810751655[&&NHX:S=n62:D=Y])n5940014:0.017792661031819083[&&NHX:S=n62:D=Y],(Resomia_ornicephala@2657185:0.004262563771468986[&&NHX:S=Resomia_ornicephala],Frillagalma_vityazi@663744:0.028441637105547157[&&NHX:S=Frillagalma_vityazi])n5939981:0.006136291467151878[&&NHX:S=n51:D=N])n5940013:0.013546839136761205[&&NHX:S=n62:D=Y])n5940012:0.011839606018978143[&&NHX:S=n62:D=Y])n5940008:0.013840645450221475[&&NHX:S=n62:D=Y],(((Chelophyes_appendiculata@1615707:0.007647023552225329[&&NHX:S=Chelophyes_appendiculata],Clytia_hemisphaerica@756642:0.643907456299178[&&NHX:S=Clytia_hemisphaerica])n5939984:0.08603691877960613[&&NHX:S=n67:D=N],(Chuniphyes_multidentata@930929:0.01248550133310033[&&NHX:S=Chuniphyes_multidentata],Kephyes_ovata@1966030:0.014671165587181996[&&NHX:S=Kephyes_ovata])n5939987:0.013285803501636162[&&NHX:S=n27:D=N])n5939988:0.008000411801689693[&&NHX:S=n67:D=Y],(((Hippopodius_hippopus@1084434:0.0505718831943577[&&NHX:S=Hippopodius_hippopus],Prayidae_D27D2@2878798:0.00905875758406546[&&NHX:S=Prayidae_D27D2])n5939991:0.021772123626769023[&&NHX:S=n38:D=N],Prayidae_D27SS7@2181711:0.029009000260863272[&&NHX:S=Prayidae_D27SS7])n5939993:1.00000050002909E-6[&&NHX:S=n38:D=Y],Prayidae_D27D2@2878801:1.00000050002909E-6[&&NHX:S=Prayidae_D27D2])n5939995:0.00916688375355408[&&NHX:S=n38:D=Y])n5939996:0.05191099091093772[&&NHX:S=n67:D=Y])n5940006:0.03953811931719265[&&NHX:S=n67:D=Y])n5940005:0.10134081070615458[&&NHX:S=n67:D=Y],(Podocoryna_carnea@3033951:0.11270255504816476[&&NHX:S=Podocoryna_carnea],Hydractinia_symbiolongicarpus@1679508:0.030168043235021993[&&NHX:S=Hydractinia_symbiolongicarpus])n5939999:0.17223048099362362[&&NHX:S=n11:D=N])n5940003:0.16233679521228994[&&NHX:S=n67:D=Y],Hydra_magnipapillata@801936:0.585696573276294[&&NHX:S=Hydra_magnipapillata])n5940002:0.4403044529817829[&&NHX:S=n68:D=N],Aegina_citrea@825314:0.4403044529817829[&&NHX:S=Aegina_citrea])n5942419[&&NHX:S=n70:D=N];" |
|
9 |
+ |
|
10 |
+# A function to simplify NHX text so that it can be parsed by |
|
11 |
+# ape::read.tree(). Discards much useful information. Intent is to |
|
12 |
+# be able to compare node annotations that have been independently |
|
13 |
+# parsed with different methods. |
|
14 |
+simplify_nhx_string <- function( text ){ |
|
15 |
+ # Remove branch lengths so NHX tags are adjacent to nodes |
|
16 |
+ # Accommodate lengths in scientific notation, eg 1e-6 |
|
17 |
+ text = gsub( "\\:[\\d\\.]+e[\\d\\-]+","", text, perl=TRUE ) |
|
18 |
+ text = gsub( "\\:[\\d\\.]+","", text, perl=TRUE ) |
|
19 |
+ |
|
20 |
+ # Remove NHX tags at tips |
|
21 |
+ text = gsub( "([^\\)])\\[.+?\\]","\\1", text, perl=TRUE ) |
|
22 |
+ |
|
23 |
+ # Remove brackets |
|
24 |
+ text = gsub( "\\[&&NHX:","", text, perl=TRUE ) |
|
25 |
+ text = gsub( "\\]","", text, perl=TRUE ) |
|
26 |
+ |
|
27 |
+ # Replace NHX tag formatting characters that aren't allowed |
|
28 |
+ text = gsub( ":","_", text, perl=TRUE ) |
|
29 |
+ text = gsub( "=","-", text, perl=TRUE ) |
|
30 |
+ |
|
31 |
+ return(text) |
|
32 |
+} |
|
33 |
+ |
|
34 |
+ |
|
35 |
+test_that("can parse example ggtree nhx tree string", { |
|
36 |
+ nhx <- read.nhx( textConnection(test_nhx_text) ) |
|
37 |
+ ntips = length(nhx@phylo$tip.label) |
|
38 |
+ |
|
39 |
+ # Correct number of tips |
|
40 |
+ expect_equal( ntips , 8 ) |
|
41 |
+ |
|
42 |
+ # All node numbers, including tips, are parsed |
|
43 |
+ expect_true( all(!is.na(nhx@nhx_tags$node)) ) |
|
44 |
+}) |
|
45 |
+ |
|
46 |
+test_that("can parse phyldog nhx tree string", { |
|
47 |
+ nhx <- read.nhx( textConnection(test_phyldog_nhx_text) ) |
|
48 |
+ ntips = length(nhx@phylo$tip.label) |
|
49 |
+ |
|
50 |
+ # Correct number of tips |
|
51 |
+ expect_equal( ntips , 16 ) |
|
52 |
+ |
|
53 |
+ # All node numbers, including tips, are parsed |
|
54 |
+ expect_true( all(!is.na(nhx@nhx_tags$node)) ) |
|
55 |
+ |
|
56 |
+ # Verify that the S field of internal nodes was correctly parsed |
|
57 |
+ # Assumes that node identity order is the same between phy and nhx@phylo |
|
58 |
+ tags = nhx@nhx_tags |
|
59 |
+ tags$node = as.numeric(tags$node) |
|
60 |
+ tags = tags[order(tags$node),] |
|
61 |
+ internal_tags = tags[ tags$node > length(nhx@phylo$tip.label), ] # Consider internal nodes only |
|
62 |
+ |
|
63 |
+ |
|
64 |
+ phy = read.tree(text=simplify_nhx_string(test_phyldog_nhx_text)) |
|
65 |
+ phy_S=unlist(lapply(strsplit(phy$node.label, "_"), function(x) x[[2]])) # Get the S field |
|
66 |
+ phy_S=unlist(lapply(strsplit(phy_S, "-"), function(x) x[[2]])) # Get the value |
|
67 |
+ phy_S=as.numeric(phy_S) |
|
68 |
+ expect_equal( phy_S, as.numeric(internal_tags$S) ) |
|
69 |
+ |
|
70 |
+ # Verify that S fild of tips was correctly parsed |
|
71 |
+ # by comparison against expected values |
|
72 |
+ tip_tags = tags[1:length(nhx@phylo$tip.label),] |
|
73 |
+ tip.labels = c("Prayidae_D27SS7@2825365", "Kephyes_ovata@2606431", "Chuniphyes_multidentata@1277217", "Apolemia_sp_@1353964", "Bargmannia_amoena@263997", "Bargmannia_elongata@946788", "Physonect_sp_@2066767", "Stephalia_dilata@2960089", "Frillagalma_vityazi@1155031", "Resomia_ornicephala@3111757", "Lychnagalma_utricularia@2253871", "Nanomia_bijuga@717864", "Cordagalma_sp_@1525873", "Rhizophysa_filiformis@3073669", "Hydra_magnipapillata@52244", "Ectopleura_larynx@3556167") |
|
74 |
+ S.tip.values = c(58, 69, 70, 31, 37, 38, 61, 52, 53, 54, 65, 71, 64, 26, 16, 15) |
|
75 |
+ expect_equal( S.tip.values[match(nhx@phylo$tip.label, tip.labels)], as.numeric(tip_tags$S)) |
|
76 |
+ |
|
77 |
+}) |
|
0 | 78 |
\ No newline at end of file |
1 | 79 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,14 @@ |
1 |
+context("xlim_tree") |
|
2 |
+ |
|
3 |
+test_that("dummy layer to set x axis limits of Tree panel", { |
|
4 |
+ set.seed(2016-10-31) |
|
5 |
+ tr <- rtree(50) |
|
6 |
+ tr$tip.label <- paste(tr$tip.label, tr$tip.label, sep="_") |
|
7 |
+ p <- ggtree(tr) + geom_tiplab(align=TRUE) + theme_tree2() |
|
8 |
+ |
|
9 |
+ d <- data.frame(id = tr$tip.label, v= rnorm(50)) |
|
10 |
+ |
|
11 |
+ p2 <- facet_plot(p + xlim_tree(c(NA, 6)), geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + ggtitle('*set_tree_xlim* only change x axis limits of *Tree* panel') |
|
12 |
+ |
|
13 |
+ expect_true(is.ggplot(p2)) # should plot appropriately |
|
14 |
+}) |