Skip to content

Commit

Permalink
use htanno to implement gganno
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jul 22, 2024
1 parent 717163d commit 75c1f0f
Show file tree
Hide file tree
Showing 70 changed files with 1,268 additions and 1,504 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.radian_history
Scratch.RConsole
ggheat.Rproj
inst/doc
22 changes: 10 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,25 @@ Imports:
tidyr
Collate:
'activate.R'
'ggheat.R'
'anno-.R'
'anno-add.R'
'anno-and.R'
'anno-build.R'
'anno-initialize.R'
'anno-setup_data.R'
'annotations.R'
'add-methods.R'
'build-methods.R'
'dendrogram.R'
'geom-draw.R'
'gganno-.R'
'ggdendrogram.R'
'ggheat-package.R'
'ggheatmap-add.R'
'ggheatmap-and.R'
'heatmap-add.R'
'ggheat.R'
'htanno-.R'
'htanno-dendrogram.R'
'htanno-gg.R'
'htanno-group.R'
'htanno-kmeans.R'
'htanno-reorder.R'
'import-standalone-assert.R'
'import-standalone-cli.R'
'import-standalone-obj-type.R'
'initialize-htanno.R'
'plot-add.R'
'plot-and_add.R'
'plot-build.R'
'utils-assert.R'
'utils-grid.R'
Expand All @@ -54,5 +49,8 @@ ByteCompile: true
URL: https://github.com/Yunuuuu/ggheat
BugReports: https://github.com/Yunuuuu/ggheat/issues
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
56 changes: 21 additions & 35 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
# Generated by roxygen2: do not edit by hand

S3method("$<-",HtannoProto)
S3method(activate,ggheatmap)
S3method(anno_add,gganno)
S3method(anno_add,htanno)
S3method(anno_and_add,gganno)
S3method(anno_and_add,htanno)
S3method(anno_build,gganno)
S3method(anno_build,htanno)
S3method(anno_initialize,anno)
S3method(anno_initialize,htanno)
S3method(anno_setup_data,"NULL")
S3method(anno_setup_data,"function")
S3method(anno_setup_data,character)
S3method(anno_setup_data,data.frame)
S3method(anno_setup_data,matrix)
S3method(anno_setup_data,numeric)
S3method(annotations_add,anno)
S3method(annotations_add,HtannoProto)
S3method(annotations_add,default)
S3method(deactivate,ggheatmap)
S3method(get_context,annotations)
S3method(get_context,ggheatmap)
S3method(gganno_add,CoordFlip)
S3method(gganno_add,default)
S3method(gganno_add,facetted_pos_scales)
S3method(gganno_add,gg)
S3method(gganno_add,labels)
S3method(gganno_and_add,facetted_pos_scales)
S3method(gganno_and_add,gg)
S3method(gganno_and_add,labels)
S3method(ggheat,"NULL")
S3method(ggheat,character)
S3method(ggheat,data.frame)
Expand All @@ -38,40 +17,44 @@ S3method(ggheat_build,default)
S3method(ggheat_build,ggheatmap)
S3method(ggheatmap_add,"NULL")
S3method(ggheatmap_add,CoordFlip)
S3method(ggheatmap_add,HtannoProto)
S3method(ggheatmap_add,active)
S3method(ggheatmap_add,anno)
S3method(ggheatmap_add,data.frame)
S3method(ggheatmap_add,default)
S3method(ggheatmap_add,facetted_pos_scales)
S3method(ggheatmap_add,gg)
S3method(ggheatmap_add,labels)
S3method(ggheatmap_add,list)
S3method(ggheatmap_add,matrix)
S3method(ggheatmap_and_add,"NULL")
S3method(ggheatmap_and_add,default)
S3method(ggheatmap_and_add,facetted_pos_scales)
S3method(ggheatmap_and_add,gg)
S3method(ggheatmap_and_add,labels)
S3method(ggplot_build,ggheatmap)
S3method(grid.draw,ggheat)
S3method(heatmap_add,CoordFlip)
S3method(heatmap_add,default)
S3method(heatmap_add,facetted_pos_scales)
S3method(heatmap_add,gg)
S3method(heatmap_add,labels)
S3method(htanno_add,default)
S3method(htanno_add,facetted_pos_scales)
S3method(htanno_and_add,default)
S3method(htanno_and_add,facetted_pos_scales)
S3method(htanno_dendro_add,CoordFlip)
S3method(htanno_dendro_add,gg)
S3method(htanno_dendro_add,labels)
S3method(htanno_add,gg)
S3method(htanno_add,labels)
S3method(htanno_setup_data,"NULL")
S3method(htanno_setup_data,"function")
S3method(htanno_setup_data,character)
S3method(htanno_setup_data,data.frame)
S3method(htanno_setup_data,matrix)
S3method(htanno_setup_data,numeric)
S3method(order2,dendrogram)
S3method(order2,hclust)
S3method(plot,anno)
S3method(plot,htanno)
S3method(print,ggheatmap)
S3method(set_context,annotations)
S3method(set_context,ggheatmap)
export(GeomDraw)
export(HtannoProto)
export(activate)
export(active)
export(anno)
export(deactivate)
export(dendrogram_data)
export(geom_draw)
Expand All @@ -84,10 +67,11 @@ export(ggdendrogram)
export(ggheat)
export(ggheat_build)
export(ggheatmap_add)
export(ggheatmap_and_add)
export(gpar)
export(hclust2)
export(htanno)
export(htanno_dendro)
export(htanno_gg)
export(htanno_group)
export(htanno_kmeans)
export(htanno_reorder)
Expand All @@ -97,8 +81,10 @@ exportMethods("&")
exportMethods("+")
exportMethods(show)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,ggplot_build)
importFrom(ggplot2,is.ggplot)
importFrom(ggplot2,theme)
importFrom(ggplot2,waiver)
importFrom(grid,gpar)
importFrom(grid,grid.draw)
Expand Down
2 changes: 1 addition & 1 deletion R/activate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @param x A [ggheatmap][ggheat] object.
#' @param what What should get activated? Possible values are `"top"`, `"left"`,
#' ` "bottom"`, and `"right"`. For `active`, this can be also `NULL`, which
#' `"bottom"`, and `"right"`. For `active`, this can be also `NULL`, which
#' means set active context into the `heatmap` itself.
#' @return
#' - `activate`/`deactivate`: A object with the same class of `x`, whose active
Expand Down
87 changes: 87 additions & 0 deletions R/add-methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
####################################################################
# add annotation into annotation list
new_annotations <- function(list, active) {
structure(list, active = active, class = c("annotations", "ggheat"))
}

#' @keywords internal
annotations_add <- function(object, annotations, object_name) {
UseMethod("annotations_add")
}

#' @export
annotations_add.default <- function(object, annotations,
object_name) {
cli::cli_abort("Cannot add {.code {object_name}} into annotations")
}

#' @export
annotations_add.HtannoProto <- function(object, annotations, object_name) {
# add annotation -------------------------------------
new <- list(object)

# check annotation name is unique --------------------
if (!is.null(name <- .subset2(object, "name"))) {
if (any(rlang::names2(annotations) == name)) {
cli::cli_warn(paste(
"{object_name}: {name} annotation is already present",
"in the {position} annotation of the heatmap"
))
annotations[[name]] <- NULL
}
names(new) <- name
}
if (.subset(.subset2(object, "set_context"), 2L)) {
anno_active <- length(annotations) + 1L
} else {
anno_active <- get_context(annotations)
}
new_annotations(c(annotations, new), anno_active)
}

#######################################################
# used to add elements for heatmap
#' @keywords internal
heatmap_add <- function(object, heatmap, object_name) UseMethod("heatmap_add")

#' @export
heatmap_add.gg <- function(object, heatmap, object_name) {
heatmap@heatmap <- ggplot2::ggplot_add(
object, slot(heatmap, "heatmap"), object_name
)
heatmap
}

#' @export
heatmap_add.labels <- heatmap_add.gg

#' @export
heatmap_add.facetted_pos_scales <- function(object, heatmap, object_name) {
slot(heatmap, "facetted_pos_scales") <- object
heatmap
}

####################################################################
# Following methods are used to add elements to `htanno` object
htanno_add <- function(object, htanno, object_name) UseMethod("htanno_add")

#' @export
htanno_add.gg <- function(object, htanno, object_name) {
if (is.null(plot <- .subset2(htanno, "plot"))) {
cli::cli_abort(paste(
"Can't add {.var {object_name}} to a",
"{.fn {snake_class(htanno)}} annotation"
), call = .subset2(htanno, "call"))
}
htanno$plot <- ggplot2::ggplot_add(object, plot, object_name)
htanno
}

#' @export
htanno_add.labels <- htanno_add.gg

#' @export
htanno_add.facetted_pos_scales <- function(object, htanno, object_name) {
htanno$facetted_pos_scales <- object
htanno
}
Loading

0 comments on commit 75c1f0f

Please sign in to comment.