1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,173 +0,0 @@ |
1 |
-##' download phylopic and convert to grob object |
|
2 |
-##' |
|
3 |
-##' |
|
4 |
-##' @title get.phylopic |
|
5 |
-##' @param id phylopic id |
|
6 |
-##' @param size size of the phylopic |
|
7 |
-##' @param color color |
|
8 |
-##' @param alpha alpha |
|
9 |
-##' @return grob object |
|
10 |
-##' @importFrom grid rasterGrob |
|
11 |
-##' @export |
|
12 |
-##' @author Guangchuang Yu |
|
13 |
-get.phylopic <- function(id, size=512, color="black", alpha=1) { |
|
14 |
- download.phylopic(id, size, color, alpha) %>% rasterGrob |
|
15 |
-} |
|
16 |
- |
|
17 |
-##' download phylopic |
|
18 |
-##' |
|
19 |
-##' @title download.phylopic |
|
20 |
-##' @param id phyopic id |
|
21 |
-##' @param size size of phylopic |
|
22 |
-##' @param color color |
|
23 |
-##' @param alpha alpha |
|
24 |
-##' @return matrix |
|
25 |
-##' @importFrom grDevices rgb |
|
26 |
-##' @importFrom grDevices col2rgb |
|
27 |
-## @importFrom EBImage readImage |
|
28 |
-## @importFrom EBImage channel |
|
29 |
-##' @export |
|
30 |
-##' @author Guangchuang Yu |
|
31 |
-download.phylopic <- function(id, size=512, color="black", alpha=1) { |
|
32 |
- imgfile <- tempfile(fileext = ".png") |
|
33 |
- download.phylopic_internal(id, size, imgfile) |
|
34 |
- |
|
35 |
- channel <- get_fun_from_pkg("EBImage", "channel") |
|
36 |
- readImage <- get_fun_from_pkg("EBImage", "readImage") |
|
37 |
- |
|
38 |
- img <- readImage(imgfile) |
|
39 |
- |
|
40 |
- color <- col2rgb(color) / 255 |
|
41 |
- |
|
42 |
- img <- channel(img, 'rgb') |
|
43 |
- img[,,1] <- color[1] |
|
44 |
- img[,,2] <- color[2] |
|
45 |
- img[,,3] <- color[3] |
|
46 |
- img[,,4] <- img[,,4]*alpha |
|
47 |
- |
|
48 |
- return(img) |
|
49 |
-} |
|
50 |
- |
|
51 |
-##' @importFrom utils download.file |
|
52 |
-##' @importFrom utils modifyList |
|
53 |
-download.phylopic_internal <- function(id, size=512, outfile=NULL) { |
|
54 |
- size %<>% as.character %>% |
|
55 |
- match.arg(c("64", "128", "256", "512", "1024")) |
|
56 |
- |
|
57 |
- imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
|
58 |
- if (is.null(outfile)) { |
|
59 |
- outfile <- sub(".*/", "", imgurl) |
|
60 |
- } |
|
61 |
- ## mode = "wb" for Windows platform |
|
62 |
- download.file(imgurl, outfile, mode="wb", quiet = TRUE) |
|
63 |
-} |
|
64 |
- |
|
65 |
- |
|
66 |
-##' add phylopic layer |
|
67 |
-##' |
|
68 |
-##' |
|
69 |
-##' @title phylopic |
|
70 |
-##' @param tree_view tree view |
|
71 |
-##' @param phylopic_id phylopic id |
|
72 |
-##' @param size size of phylopic to download |
|
73 |
-##' @param color color |
|
74 |
-##' @param alpha alpha |
|
75 |
-##' @param node selected node |
|
76 |
-##' @param x x position |
|
77 |
-##' @param y y position |
|
78 |
-##' @param width width of phylopic |
|
79 |
-##' @return phylopic layer |
|
80 |
-##' @export |
|
81 |
-##' @importFrom ggplot2 annotation_custom |
|
82 |
-##' @importFrom grid rasterGrob |
|
83 |
-##' @author Guangchuang Yu |
|
84 |
-phylopic <- function(tree_view, phylopic_id, |
|
85 |
- size=512, color="black", alpha=0.5, |
|
86 |
- node=NULL, x=NULL, y=NULL, width=.1) { |
|
87 |
- |
|
88 |
- message("The phylopic function will be defunct in next release, please use ggimage::geom_phylopic() instead.") |
|
89 |
- |
|
90 |
- width <- diff(range(tree_view$data$x)) * width |
|
91 |
- img <- download.phylopic(phylopic_id, size, color, alpha) |
|
92 |
- if ( is.null(node) ) { |
|
93 |
- xmin <- ymin <- -Inf |
|
94 |
- xmax <- ymax <- Inf |
|
95 |
- } else { |
|
96 |
- if (is.null(x) || is.null(y)) { |
|
97 |
- if (is.null(node)) { |
|
98 |
- stop("node or x and y should not be NULL...") |
|
99 |
- } |
|
100 |
- df <- tree_view$data |
|
101 |
- x <- df[match(node, df$node), "x"] |
|
102 |
- y <- df[match(node, df$node), "y"] |
|
103 |
- } |
|
104 |
- AR <- getAR(img) |
|
105 |
- xmin <- x - width/2 |
|
106 |
- xmax <- x + width/2 |
|
107 |
- ymin <- y - AR * width/2 |
|
108 |
- ymax <- y + AR * width/2 |
|
109 |
- } |
|
110 |
- |
|
111 |
- tree_view + annotation_custom(xmin=xmin, ymin=ymin, |
|
112 |
- xmax=xmax, ymax=ymax, |
|
113 |
- rasterGrob(img)) |
|
114 |
-} |
|
115 |
- |
|
116 |
-getAR <- function(img) { |
|
117 |
- dims <- dim(img)[1:2] |
|
118 |
- dims[1]/dims[2] |
|
119 |
-} |
|
120 |
- |
|
121 |
- |
|
122 |
-##' annotation taxa with images |
|
123 |
-##' |
|
124 |
-##' |
|
125 |
-##' @title annotation_image |
|
126 |
-##' @param tree_view tree view |
|
127 |
-##' @param img_info data.frame with first column of taxa name and second column of image names |
|
128 |
-##' @param width width of the image to be plotted in image |
|
129 |
-##' @param align logical |
|
130 |
-##' @param linetype line type if align = TRUE |
|
131 |
-##' @param linesize line size if align = TRUE |
|
132 |
-##' @param offset offset of image from the tree view |
|
133 |
-##' @return tree view |
|
134 |
-##' @export |
|
135 |
-##' @author Guangchuang Yu |
|
136 |
-annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetype="dotted", linesize =1, offset=0) { |
|
137 |
- df <- tree_view$data |
|
138 |
- idx <- match(img_info[,1], df$label) |
|
139 |
- x <- df[idx, "x"] |
|
140 |
- y <- df[idx, "y"] |
|
141 |
- |
|
142 |
- readImage <- get_fun_from_pkg("EBImage", "readImage") |
|
143 |
- images <- lapply(img_info[,2], readImage) |
|
144 |
- |
|
145 |
- ARs <- sapply(images, getAR) |
|
146 |
- |
|
147 |
- width <- width * diff(range(df$x)) |
|
148 |
- if (align) { |
|
149 |
- xmin <- max(df$x) + offset |
|
150 |
- xmin <- rep(xmin, length(x)) |
|
151 |
- } else { |
|
152 |
- xmin <- x - width/2 + offset |
|
153 |
- } |
|
154 |
- xmax <- xmin + width |
|
155 |
- |
|
156 |
- ymin <- y - ARs * width/2 |
|
157 |
- ymax <- y + ARs * width/2 |
|
158 |
- image_layers <- lapply(1:length(xmin), function(i) { |
|
159 |
- annotation_custom(xmin=xmin[i], ymin=ymin[i], |
|
160 |
- xmax=xmax[i], ymax=ymax[i], |
|
161 |
- rasterGrob(images[[i]])) |
|
162 |
- }) |
|
163 |
- |
|
164 |
- tree_view <- tree_view + image_layers |
|
165 |
- |
|
166 |
- if (align && (!is.null(linetype) && !is.na(linetype))) { |
|
167 |
- tree_view <- tree_view + geom_segment(data=df[idx,], |
|
168 |
- x=xmin, xend = x*1.01, |
|
169 |
- y = y, yend = y, |
|
170 |
- linetype=linetype, size=linesize) |
|
171 |
- } |
|
172 |
- tree_view |
|
173 |
-} |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
##' download phylopic and convert to grob object |
2 | 2 |
##' |
3 |
-##' |
|
3 |
+##' |
|
4 | 4 |
##' @title get.phylopic |
5 | 5 |
##' @param id phylopic id |
6 | 6 |
##' @param size size of the phylopic |
... | ... |
@@ -34,9 +34,9 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
34 | 34 |
|
35 | 35 |
channel <- get_fun_from_pkg("EBImage", "channel") |
36 | 36 |
readImage <- get_fun_from_pkg("EBImage", "readImage") |
37 |
- |
|
37 |
+ |
|
38 | 38 |
img <- readImage(imgfile) |
39 |
- |
|
39 |
+ |
|
40 | 40 |
color <- col2rgb(color) / 255 |
41 | 41 |
|
42 | 42 |
img <- channel(img, 'rgb') |
... | ... |
@@ -44,7 +44,7 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
44 | 44 |
img[,,2] <- color[2] |
45 | 45 |
img[,,3] <- color[3] |
46 | 46 |
img[,,4] <- img[,,4]*alpha |
47 |
- |
|
47 |
+ |
|
48 | 48 |
return(img) |
49 | 49 |
} |
50 | 50 |
|
... | ... |
@@ -59,13 +59,13 @@ download.phylopic_internal <- function(id, size=512, outfile=NULL) { |
59 | 59 |
outfile <- sub(".*/", "", imgurl) |
60 | 60 |
} |
61 | 61 |
## mode = "wb" for Windows platform |
62 |
- download.file(imgurl, outfile, mode="wb", quiet = TRUE) |
|
62 |
+ download.file(imgurl, outfile, mode="wb", quiet = TRUE) |
|
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
|
66 | 66 |
##' add phylopic layer |
67 | 67 |
##' |
68 |
-##' |
|
68 |
+##' |
|
69 | 69 |
##' @title phylopic |
70 | 70 |
##' @param tree_view tree view |
71 | 71 |
##' @param phylopic_id phylopic id |
... | ... |
@@ -84,6 +84,9 @@ download.phylopic_internal <- function(id, size=512, outfile=NULL) { |
84 | 84 |
phylopic <- function(tree_view, phylopic_id, |
85 | 85 |
size=512, color="black", alpha=0.5, |
86 | 86 |
node=NULL, x=NULL, y=NULL, width=.1) { |
87 |
+ |
|
88 |
+ message("The phylopic function will be defunct in next release, please use ggimage::geom_phylopic() instead.") |
|
89 |
+ |
|
87 | 90 |
width <- diff(range(tree_view$data$x)) * width |
88 | 91 |
img <- download.phylopic(phylopic_id, size, color, alpha) |
89 | 92 |
if ( is.null(node) ) { |
... | ... |
@@ -104,7 +107,7 @@ phylopic <- function(tree_view, phylopic_id, |
104 | 107 |
ymin <- y - AR * width/2 |
105 | 108 |
ymax <- y + AR * width/2 |
106 | 109 |
} |
107 |
- |
|
110 |
+ |
|
108 | 111 |
tree_view + annotation_custom(xmin=xmin, ymin=ymin, |
109 | 112 |
xmax=xmax, ymax=ymax, |
110 | 113 |
rasterGrob(img)) |
... | ... |
@@ -118,7 +121,7 @@ getAR <- function(img) { |
118 | 121 |
|
119 | 122 |
##' annotation taxa with images |
120 | 123 |
##' |
121 |
-##' |
|
124 |
+##' |
|
122 | 125 |
##' @title annotation_image |
123 | 126 |
##' @param tree_view tree view |
124 | 127 |
##' @param img_info data.frame with first column of taxa name and second column of image names |
... | ... |
@@ -149,7 +152,7 @@ annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetyp |
149 | 152 |
xmin <- x - width/2 + offset |
150 | 153 |
} |
151 | 154 |
xmax <- xmin + width |
152 |
- |
|
155 |
+ |
|
153 | 156 |
ymin <- y - ARs * width/2 |
154 | 157 |
ymax <- y + ARs * width/2 |
155 | 158 |
image_layers <- lapply(1:length(xmin), function(i) { |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@119521 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -32,8 +32,9 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
32 | 32 |
imgfile <- tempfile(fileext = ".png") |
33 | 33 |
download.phylopic_internal(id, size, imgfile) |
34 | 34 |
|
35 |
- requireNamespace("EBImage") |
|
36 |
- channel <- eval(parse(text=paste0("EBImage::", "channel"))) |
|
35 |
+ channel <- get_fun_from_pkg("EBImage", "channel") |
|
36 |
+ readImage <- get_fun_from_pkg("EBImage", "readImage") |
|
37 |
+ |
|
37 | 38 |
img <- readImage(imgfile) |
38 | 39 |
|
39 | 40 |
color <- col2rgb(color) / 255 |
... | ... |
@@ -135,6 +136,7 @@ annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetyp |
135 | 136 |
x <- df[idx, "x"] |
136 | 137 |
y <- df[idx, "y"] |
137 | 138 |
|
139 |
+ readImage <- get_fun_from_pkg("EBImage", "readImage") |
|
138 | 140 |
images <- lapply(img_info[,2], readImage) |
139 | 141 |
|
140 | 142 |
ARs <- sapply(images, getAR) |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@114602 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -93,8 +93,9 @@ phylopic <- function(tree_view, phylopic_id, |
93 | 93 |
if (is.null(node)) { |
94 | 94 |
stop("node or x and y should not be NULL...") |
95 | 95 |
} |
96 |
- x <- tree_view$data[node, "x"] |
|
97 |
- y <- tree_view$data[node, "y"] |
|
96 |
+ df <- tree_view$data |
|
97 |
+ x <- df[match(node, df$node), "x"] |
|
98 |
+ y <- df[match(node, df$node), "y"] |
|
98 | 99 |
} |
99 | 100 |
AR <- getAR(img) |
100 | 101 |
xmin <- x - width/2 |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@114159 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -32,12 +32,8 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
32 | 32 |
imgfile <- tempfile(fileext = ".png") |
33 | 33 |
download.phylopic_internal(id, size, imgfile) |
34 | 34 |
|
35 |
- EBImage <- "EBImage" |
|
36 |
- require(EBImage, character.only = TRUE) |
|
37 |
- |
|
38 |
- readImage <- eval(parse(text="readImage")) |
|
39 |
- channel <- eval(parse(text="channel")) |
|
40 |
- |
|
35 |
+ requireNamespace("EBImage") |
|
36 |
+ channel <- eval(parse(text=paste0("EBImage::", "channel"))) |
|
41 | 37 |
img <- readImage(imgfile) |
42 | 38 |
|
43 | 39 |
color <- col2rgb(color) / 255 |
... | ... |
@@ -51,6 +47,8 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
51 | 47 |
return(img) |
52 | 48 |
} |
53 | 49 |
|
50 |
+##' @importFrom utils download.file |
|
51 |
+##' @importFrom utils modifyList |
|
54 | 52 |
download.phylopic_internal <- function(id, size=512, outfile=NULL) { |
55 | 53 |
size %<>% as.character %>% |
56 | 54 |
match.arg(c("64", "128", "256", "512", "1024")) |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@113924 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -136,10 +136,6 @@ annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetyp |
136 | 136 |
x <- df[idx, "x"] |
137 | 137 |
y <- df[idx, "y"] |
138 | 138 |
|
139 |
- EBImage <- "EBImage" |
|
140 |
- require(EBImage, character.only = TRUE) |
|
141 |
- readImage <- eval(parse(text="readImage")) |
|
142 |
- |
|
143 | 139 |
images <- lapply(img_info[,2], readImage) |
144 | 140 |
|
145 | 141 |
ARs <- sapply(images, getAR) |
... | ... |
@@ -148,11 +144,11 @@ annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetyp |
148 | 144 |
if (align) { |
149 | 145 |
xmin <- max(df$x) + offset |
150 | 146 |
xmin <- rep(xmin, length(x)) |
151 |
- xmax <- xmin + width |
|
152 | 147 |
} else { |
153 |
- xmin <- x - width/2 |
|
154 |
- xmax <- x + width/2 |
|
148 |
+ xmin <- x - width/2 + offset |
|
155 | 149 |
} |
150 |
+ xmax <- xmin + width |
|
151 |
+ |
|
156 | 152 |
ymin <- y - ARs * width/2 |
157 | 153 |
ymax <- y + ARs * width/2 |
158 | 154 |
image_layers <- lapply(1:length(xmin), function(i) { |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@111813 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -24,14 +24,20 @@ get.phylopic <- function(id, size=512, color="black", alpha=1) { |
24 | 24 |
##' @return matrix |
25 | 25 |
##' @importFrom grDevices rgb |
26 | 26 |
##' @importFrom grDevices col2rgb |
27 |
-##' @importFrom EBImage readImage |
|
28 |
-##' @importFrom EBImage channel |
|
27 |
+## @importFrom EBImage readImage |
|
28 |
+## @importFrom EBImage channel |
|
29 | 29 |
##' @export |
30 | 30 |
##' @author Guangchuang Yu |
31 | 31 |
download.phylopic <- function(id, size=512, color="black", alpha=1) { |
32 | 32 |
imgfile <- tempfile(fileext = ".png") |
33 | 33 |
download.phylopic_internal(id, size, imgfile) |
34 | 34 |
|
35 |
+ EBImage <- "EBImage" |
|
36 |
+ require(EBImage, character.only = TRUE) |
|
37 |
+ |
|
38 |
+ readImage <- eval(parse(text="readImage")) |
|
39 |
+ channel <- eval(parse(text="channel")) |
|
40 |
+ |
|
35 | 41 |
img <- readImage(imgfile) |
36 | 42 |
|
37 | 43 |
color <- col2rgb(color) / 255 |
... | ... |
@@ -130,6 +136,10 @@ annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetyp |
130 | 136 |
x <- df[idx, "x"] |
131 | 137 |
y <- df[idx, "y"] |
132 | 138 |
|
139 |
+ EBImage <- "EBImage" |
|
140 |
+ require(EBImage, character.only = TRUE) |
|
141 |
+ readImage <- eval(parse(text="readImage")) |
|
142 |
+ |
|
133 | 143 |
images <- lapply(img_info[,2], readImage) |
134 | 144 |
|
135 | 145 |
ARs <- sapply(images, getAR) |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@106969 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -29,16 +29,9 @@ get.phylopic <- function(id, size=512, color="black", alpha=1) { |
29 | 29 |
##' @export |
30 | 30 |
##' @author Guangchuang Yu |
31 | 31 |
download.phylopic <- function(id, size=512, color="black", alpha=1) { |
32 |
- size %<>% as.character %>% |
|
33 |
- match.arg(c("64", "128", "256", "512", "1024")) |
|
32 |
+ imgfile <- tempfile(fileext = ".png") |
|
33 |
+ download.phylopic_internal(id, size, imgfile) |
|
34 | 34 |
|
35 |
- imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
|
36 |
- imgfile <- tempfile(fileext = ".png") |
|
37 |
- if (Sys.info()["sysname"] == "Windows") { |
|
38 |
- download.file(imgurl, imgfile, mode="wb", quiet = TRUE) |
|
39 |
- } else { |
|
40 |
- download.file(imgurl, imgfile, quiet = TRUE) |
|
41 |
- } |
|
42 | 35 |
img <- readImage(imgfile) |
43 | 36 |
|
44 | 37 |
color <- col2rgb(color) / 255 |
... | ... |
@@ -52,6 +45,19 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
52 | 45 |
return(img) |
53 | 46 |
} |
54 | 47 |
|
48 |
+download.phylopic_internal <- function(id, size=512, outfile=NULL) { |
|
49 |
+ size %<>% as.character %>% |
|
50 |
+ match.arg(c("64", "128", "256", "512", "1024")) |
|
51 |
+ |
|
52 |
+ imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
|
53 |
+ if (is.null(outfile)) { |
|
54 |
+ outfile <- sub(".*/", "", imgurl) |
|
55 |
+ } |
|
56 |
+ ## mode = "wb" for Windows platform |
|
57 |
+ download.file(imgurl, outfile, mode="wb", quiet = TRUE) |
|
58 |
+} |
|
59 |
+ |
|
60 |
+ |
|
55 | 61 |
##' add phylopic layer |
56 | 62 |
##' |
57 | 63 |
##' |
... | ... |
@@ -72,7 +78,8 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
72 | 78 |
##' @author Guangchuang Yu |
73 | 79 |
phylopic <- function(tree_view, phylopic_id, |
74 | 80 |
size=512, color="black", alpha=0.5, |
75 |
- node=NULL, x=NULL, y=NULL, width=5) { |
|
81 |
+ node=NULL, x=NULL, y=NULL, width=.1) { |
|
82 |
+ width <- diff(range(tree_view$data$x)) * width |
|
76 | 83 |
img <- download.phylopic(phylopic_id, size, color, alpha) |
77 | 84 |
if ( is.null(node) ) { |
78 | 85 |
xmin <- ymin <- -Inf |
... | ... |
@@ -85,9 +92,7 @@ phylopic <- function(tree_view, phylopic_id, |
85 | 92 |
x <- tree_view$data[node, "x"] |
86 | 93 |
y <- tree_view$data[node, "y"] |
87 | 94 |
} |
88 |
- |
|
89 |
- dims <- dim(img)[1:2] |
|
90 |
- AR <- dims[1]/dims[2] |
|
95 |
+ AR <- getAR(img) |
|
91 | 96 |
xmin <- x - width/2 |
92 | 97 |
xmax <- x + width/2 |
93 | 98 |
ymin <- y - AR * width/2 |
... | ... |
@@ -99,3 +104,60 @@ phylopic <- function(tree_view, phylopic_id, |
99 | 104 |
rasterGrob(img)) |
100 | 105 |
} |
101 | 106 |
|
107 |
+getAR <- function(img) { |
|
108 |
+ dims <- dim(img)[1:2] |
|
109 |
+ dims[1]/dims[2] |
|
110 |
+} |
|
111 |
+ |
|
112 |
+ |
|
113 |
+##' annotation taxa with images |
|
114 |
+##' |
|
115 |
+##' |
|
116 |
+##' @title annotation_image |
|
117 |
+##' @param tree_view tree view |
|
118 |
+##' @param img_info data.frame with first column of taxa name and second column of image names |
|
119 |
+##' @param width width of the image to be plotted in image |
|
120 |
+##' @param align logical |
|
121 |
+##' @param linetype line type if align = TRUE |
|
122 |
+##' @param linesize line size if align = TRUE |
|
123 |
+##' @param offset offset of image from the tree view |
|
124 |
+##' @return tree view |
|
125 |
+##' @export |
|
126 |
+##' @author Guangchuang Yu |
|
127 |
+annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetype="dotted", linesize =1, offset=0) { |
|
128 |
+ df <- tree_view$data |
|
129 |
+ idx <- match(img_info[,1], df$label) |
|
130 |
+ x <- df[idx, "x"] |
|
131 |
+ y <- df[idx, "y"] |
|
132 |
+ |
|
133 |
+ images <- lapply(img_info[,2], readImage) |
|
134 |
+ |
|
135 |
+ ARs <- sapply(images, getAR) |
|
136 |
+ |
|
137 |
+ width <- width * diff(range(df$x)) |
|
138 |
+ if (align) { |
|
139 |
+ xmin <- max(df$x) + offset |
|
140 |
+ xmin <- rep(xmin, length(x)) |
|
141 |
+ xmax <- xmin + width |
|
142 |
+ } else { |
|
143 |
+ xmin <- x - width/2 |
|
144 |
+ xmax <- x + width/2 |
|
145 |
+ } |
|
146 |
+ ymin <- y - ARs * width/2 |
|
147 |
+ ymax <- y + ARs * width/2 |
|
148 |
+ image_layers <- lapply(1:length(xmin), function(i) { |
|
149 |
+ annotation_custom(xmin=xmin[i], ymin=ymin[i], |
|
150 |
+ xmax=xmax[i], ymax=ymax[i], |
|
151 |
+ rasterGrob(images[[i]])) |
|
152 |
+ }) |
|
153 |
+ |
|
154 |
+ tree_view <- tree_view + image_layers |
|
155 |
+ |
|
156 |
+ if (align && (!is.null(linetype) && !is.na(linetype))) { |
|
157 |
+ tree_view <- tree_view + geom_segment(data=df[idx,], |
|
158 |
+ x=xmin, xend = x*1.01, |
|
159 |
+ y = y, yend = y, |
|
160 |
+ linetype=linetype, size=linesize) |
|
161 |
+ } |
|
162 |
+ tree_view |
|
163 |
+} |
Commit id: aafb6a1f108fdc52ca67714d36f7ed10431729b8
default value of width in phylopic
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@100517 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -72,7 +72,7 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
72 | 72 |
##' @author Guangchuang Yu |
73 | 73 |
phylopic <- function(tree_view, phylopic_id, |
74 | 74 |
size=512, color="black", alpha=0.5, |
75 |
- node=NULL, x=NULL, y=NULL, width=NULL) { |
|
75 |
+ node=NULL, x=NULL, y=NULL, width=5) { |
|
76 | 76 |
img <- download.phylopic(phylopic_id, size, color, alpha) |
77 | 77 |
if ( is.null(node) ) { |
78 | 78 |
xmin <- ymin <- -Inf |
... | ... |
@@ -85,10 +85,7 @@ phylopic <- function(tree_view, phylopic_id, |
85 | 85 |
x <- tree_view$data[node, "x"] |
86 | 86 |
y <- tree_view$data[node, "y"] |
87 | 87 |
} |
88 |
- if (is.null(width)) { |
|
89 |
- width <- 5 |
|
90 |
- } |
|
91 |
- |
|
88 |
+ |
|
92 | 89 |
dims <- dim(img)[1:2] |
93 | 90 |
AR <- dims[1]/dims[2] |
94 | 91 |
xmin <- x - width/2 |
Commit id: 339117c7b83dac1233cfef7ef7f73dba23c8de66
collapse, expand, hilight and phylopic
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@100516 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -52,3 +52,53 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
52 | 52 |
return(img) |
53 | 53 |
} |
54 | 54 |
|
55 |
+##' add phylopic layer |
|
56 |
+##' |
|
57 |
+##' |
|
58 |
+##' @title phylopic |
|
59 |
+##' @param tree_view tree view |
|
60 |
+##' @param phylopic_id phylopic id |
|
61 |
+##' @param size size of phylopic to download |
|
62 |
+##' @param color color |
|
63 |
+##' @param alpha alpha |
|
64 |
+##' @param node selected node |
|
65 |
+##' @param x x position |
|
66 |
+##' @param y y position |
|
67 |
+##' @param width width of phylopic |
|
68 |
+##' @return phylopic layer |
|
69 |
+##' @export |
|
70 |
+##' @importFrom ggplot2 annotation_custom |
|
71 |
+##' @importFrom grid rasterGrob |
|
72 |
+##' @author Guangchuang Yu |
|
73 |
+phylopic <- function(tree_view, phylopic_id, |
|
74 |
+ size=512, color="black", alpha=0.5, |
|
75 |
+ node=NULL, x=NULL, y=NULL, width=NULL) { |
|
76 |
+ img <- download.phylopic(phylopic_id, size, color, alpha) |
|
77 |
+ if ( is.null(node) ) { |
|
78 |
+ xmin <- ymin <- -Inf |
|
79 |
+ xmax <- ymax <- Inf |
|
80 |
+ } else { |
|
81 |
+ if (is.null(x) || is.null(y)) { |
|
82 |
+ if (is.null(node)) { |
|
83 |
+ stop("node or x and y should not be NULL...") |
|
84 |
+ } |
|
85 |
+ x <- tree_view$data[node, "x"] |
|
86 |
+ y <- tree_view$data[node, "y"] |
|
87 |
+ } |
|
88 |
+ if (is.null(width)) { |
|
89 |
+ width <- 5 |
|
90 |
+ } |
|
91 |
+ |
|
92 |
+ dims <- dim(img)[1:2] |
|
93 |
+ AR <- dims[1]/dims[2] |
|
94 |
+ xmin <- x - width/2 |
|
95 |
+ xmax <- x + width/2 |
|
96 |
+ ymin <- y - AR * width/2 |
|
97 |
+ ymax <- y + AR * width/2 |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ tree_view + annotation_custom(xmin=xmin, ymin=ymin, |
|
101 |
+ xmax=xmax, ymax=ymax, |
|
102 |
+ rasterGrob(img)) |
|
103 |
+} |
|
104 |
+ |
Commit id: da1e9c85367cf00f37ee72a3a5e938f03db5b164
fixed windows compile issue
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@100099 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -33,8 +33,12 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
33 | 33 |
match.arg(c("64", "128", "256", "512", "1024")) |
34 | 34 |
|
35 | 35 |
imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
36 |
- imgfile <- tempfile(fileext = ".PNG") ## .png is not recognize by WINDOWS platform |
|
37 |
- download.file(imgurl, imgfile, quiet = TRUE) |
|
36 |
+ imgfile <- tempfile(fileext = ".png") |
|
37 |
+ if (Sys.info()["sysname"] == "Windows") { |
|
38 |
+ download.file(imgurl, imgfile, mode="wb", quiet = TRUE) |
|
39 |
+ } else { |
|
40 |
+ download.file(imgurl, imgfile, quiet = TRUE) |
|
41 |
+ } |
|
38 | 42 |
img <- readImage(imgfile) |
39 | 43 |
|
40 | 44 |
color <- col2rgb(color) / 255 |
Commit id: 2484901a46377834f43aafe3e79326741b42deae
change .png to .PNG to satisfy WINDOWS
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@100014 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -33,7 +33,7 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) { |
33 | 33 |
match.arg(c("64", "128", "256", "512", "1024")) |
34 | 34 |
|
35 | 35 |
imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
36 |
- imgfile <- tempfile(fileext = ".png") |
|
36 |
+ imgfile <- tempfile(fileext = ".PNG") ## .png is not recognize by WINDOWS platform |
|
37 | 37 |
download.file(imgurl, imgfile, quiet = TRUE) |
38 | 38 |
img <- readImage(imgfile) |
39 | 39 |
|
Commit information:
Commit id: 5d32e6887092dbadba9dd2646d71778514343859
geom_phylopic
Committed by: GuangchuangYu
Author Name: GuangchuangYu
Commit date: 2015-02-10 20:10:38 +0800
Author date: 2015-02-10 20:10:38 +0800
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@99314 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -7,32 +7,44 @@ |
7 | 7 |
##' @param color color |
8 | 8 |
##' @param alpha alpha |
9 | 9 |
##' @return grob object |
10 |
-##' @importFrom grDevices rgb |
|
11 |
-##' @importFrom grDevices col2rgb |
|
12 |
-##' @importFrom RCurl getURLContent |
|
13 |
-##' @importFrom png readPNG |
|
14 | 10 |
##' @importFrom grid rasterGrob |
15 | 11 |
##' @export |
16 | 12 |
##' @author Guangchuang Yu |
17 |
-##' @references https://github.com/sckott/rphylopic/blob/master/R/add_phylopic.r |
|
18 | 13 |
get.phylopic <- function(id, size=512, color="black", alpha=1) { |
19 |
- size %<>% as.character %>% match.arg(c("64", "128", "256", "512", "1024")) |
|
20 |
- img <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") %>% |
|
21 |
- getURLContent %>% |
|
22 |
- readPNG |
|
14 |
+ download.phylopic(id, size, color, alpha) %>% rasterGrob |
|
15 |
+} |
|
23 | 16 |
|
24 |
- color %<>% col2rgb |
|
25 |
- n <- length(img[,,1]) |
|
17 |
+##' download phylopic |
|
18 |
+##' |
|
19 |
+##' @title download.phylopic |
|
20 |
+##' @param id phyopic id |
|
21 |
+##' @param size size of phylopic |
|
22 |
+##' @param color color |
|
23 |
+##' @param alpha alpha |
|
24 |
+##' @return matrix |
|
25 |
+##' @importFrom grDevices rgb |
|
26 |
+##' @importFrom grDevices col2rgb |
|
27 |
+##' @importFrom EBImage readImage |
|
28 |
+##' @importFrom EBImage channel |
|
29 |
+##' @export |
|
30 |
+##' @author Guangchuang Yu |
|
31 |
+download.phylopic <- function(id, size=512, color="black", alpha=1) { |
|
32 |
+ size %<>% as.character %>% |
|
33 |
+ match.arg(c("64", "128", "256", "512", "1024")) |
|
34 |
+ |
|
35 |
+ imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") |
|
36 |
+ imgfile <- tempfile(fileext = ".png") |
|
37 |
+ download.file(imgurl, imgfile, quiet = TRUE) |
|
38 |
+ img <- readImage(imgfile) |
|
39 |
+ |
|
40 |
+ color <- col2rgb(color) / 255 |
|
41 |
+ |
|
42 |
+ img <- channel(img, 'rgb') |
|
43 |
+ img[,,1] <- color[1] |
|
44 |
+ img[,,2] <- color[2] |
|
45 |
+ img[,,3] <- color[3] |
|
46 |
+ img[,,4] <- img[,,4]*alpha |
|
26 | 47 |
|
27 |
- matrix(ifelse(img[,,4] > 0, |
|
28 |
- rgb(red = rep(color[1,1], n), |
|
29 |
- green = rep(color[2,1], n), |
|
30 |
- blue = rep(color[3,1], n), |
|
31 |
- alpha = img[,,4] * 255 * alpha, maxColorValue = 255), |
|
32 |
- rgb(red = rep(1, n), |
|
33 |
- green = rep(1, n), |
|
34 |
- blue = rep(1, n), |
|
35 |
- alpha = img[,,4] * alpha)), |
|
36 |
- nrow = nrow(img)) %>% |
|
37 |
- rasterGrob |
|
48 |
+ return(img) |
|
38 | 49 |
} |
50 |
+ |
Commit information:
Commit id: 4e97e602c3e032f680bc3da4b47209dd77c97f36
phylopic
Committed by: GuangchuangYu
Author Name: GuangchuangYu
Commit date: 2015-01-30 17:47:29 +0800
Author date: 2015-01-30 17:47:29 +0800
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@98894 bc3139a8-67e5-0310-9ffc-ced21a209358
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,38 @@ |
1 |
+##' download phylopic and convert to grob object |
|
2 |
+##' |
|
3 |
+##' |
|
4 |
+##' @title get.phylopic |
|
5 |
+##' @param id phylopic id |
|
6 |
+##' @param size size of the phylopic |
|
7 |
+##' @param color color |
|
8 |
+##' @param alpha alpha |
|
9 |
+##' @return grob object |
|
10 |
+##' @importFrom grDevices rgb |
|
11 |
+##' @importFrom grDevices col2rgb |
|
12 |
+##' @importFrom RCurl getURLContent |
|
13 |
+##' @importFrom png readPNG |
|
14 |
+##' @importFrom grid rasterGrob |
|
15 |
+##' @export |
|
16 |
+##' @author Guangchuang Yu |
|
17 |
+##' @references https://github.com/sckott/rphylopic/blob/master/R/add_phylopic.r |
|
18 |
+get.phylopic <- function(id, size=512, color="black", alpha=1) { |
|
19 |
+ size %<>% as.character %>% match.arg(c("64", "128", "256", "512", "1024")) |
|
20 |
+ img <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png") %>% |
|
21 |
+ getURLContent %>% |
|
22 |
+ readPNG |
|
23 |
+ |
|
24 |
+ color %<>% col2rgb |
|
25 |
+ n <- length(img[,,1]) |
|
26 |
+ |
|
27 |
+ matrix(ifelse(img[,,4] > 0, |
|
28 |
+ rgb(red = rep(color[1,1], n), |
|
29 |
+ green = rep(color[2,1], n), |
|
30 |
+ blue = rep(color[3,1], n), |
|
31 |
+ alpha = img[,,4] * 255 * alpha, maxColorValue = 255), |
|
32 |
+ rgb(red = rep(1, n), |
|
33 |
+ green = rep(1, n), |
|
34 |
+ blue = rep(1, n), |
|
35 |
+ alpha = img[,,4] * alpha)), |
|
36 |
+ nrow = nrow(img)) %>% |
|
37 |
+ rasterGrob |
|
38 |
+} |