Skip to content

Commit

Permalink
row_dend/column_dend support extracting dendrograms on slices
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Mar 31, 2023
1 parent 772822f commit aa55a66
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 52 deletions.
48 changes: 24 additions & 24 deletions R/00_S4_generic_methods.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
55 changes: 43 additions & 12 deletions R/HeatmapList-get_order.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ setMethod(f = "column_order",
# == param
# -object A `HeatmapList-class` object.
# -name Name of a specific heatmap.
# -on_slice If the value is TRUE, it returns the dendrogram on the slice level.
#
# == value
# The format of the returned object depends on whether rows/columns of the heatmaps are split.
Expand All @@ -226,12 +227,13 @@ setMethod(f = "column_order",
# ht_list = Heatmap(mat, row_km = 2) + Heatmap(mat)
# ht_list = draw(ht_list)
# row_dend(ht_list)
# row_dend(ht_list, on_slice = TRUE)
# ht_list = Heatmap(mat, row_km = 2) \%v\% Heatmap(mat)
# ht_list = draw(ht_list)
# row_dend(ht_list)
setMethod(f = "row_dend",
signature = "HeatmapList",
definition = function(object, name = NULL) {
definition = function(object, name = NULL, on_slice = FALSE) {

if(!object@layout$initialized) {
warning_wrap("The heatmap list has not been initialized. You might have different results if you repeatedly execute this function, e.g. when row_km/column_km was set. It is more suggested to do as `ht_list = draw(ht_list); row_dend(ht_list)`.")
Expand All @@ -240,7 +242,7 @@ setMethod(f = "row_dend",
object = make_layout(object)

if(!is.null(name)) {
return(row_dend(object@ht_list[[ name[1] ]]))
return(row_dend(object@ht_list[[ name[1] ]], on_slice = on_slice))
}

n = length(object@ht_list)
Expand All @@ -250,6 +252,9 @@ setMethod(f = "row_dend",
}

if(object@direction == "horizontal") {
if(on_slice) {
return(object@ht_list[[ ht_index[1] ]]@row_dend_slice)
}
lt = object@ht_list[[ ht_index[1] ]]@row_dend_list
if(length(lt) == 1) {
return(lt[[1]])
Expand All @@ -259,8 +264,13 @@ setMethod(f = "row_dend",
} else {
lt_rd = list()
for(i in ht_index) {
lt = object@ht_list[[i]]@row_dend_list
lt_rd = c(lt_rd, list(lt))
if(on_slice) {
lt = object@ht_list[[i]]@row_dend_slice
lt_rd = c(lt_rd, list(lt))
} else {
lt = object@ht_list[[i]]@row_dend_list
lt_rd = c(lt_rd, list(lt))
}
}
names(lt_rd) = names(object@ht_list)[ht_index]
proper_format_lt(lt_rd)
Expand All @@ -273,6 +283,7 @@ setMethod(f = "row_dend",
#
# == param
# -object A `Heatmap-class` object.
# -on_slice If the value is TRUE, it returns the dendrogram on the slice level.
#
# == value
# The format of the returned object depends on whether rows/columns of the heatmaps are split.
Expand All @@ -291,13 +302,17 @@ setMethod(f = "row_dend",
#
setMethod(f = "row_dend",
signature = "Heatmap",
definition = function(object) {
definition = function(object, on_slice = FALSE) {

if(!object@layout$initialized) {
warning_wrap("The heatmap has not been initialized. You might have different results if you repeatedly execute this function, e.g. when row_km/column_km was set. It is more suggested to do as `ht = draw(ht); row_dend(ht)`.")
}

object = prepare(object)
object = prepare(object)

if(on_slice) {
return(object@row_dend_slice)
}

lt = object@row_dend_list
if(length(lt) == 1) {
Expand All @@ -313,6 +328,7 @@ setMethod(f = "row_dend",
# == param
# -object A `HeatmapList-class` object.
# -name Name of a specific heatmap.
# -on_slice If the value is TRUE, it returns the dendrogram on the slice level.
#
# == value
# The format of the returned object depends on whether rows/columns of the heatmaps are split.
Expand All @@ -328,6 +344,7 @@ setMethod(f = "row_dend",
# ht_list = Heatmap(mat, column_km = 2) + Heatmap(mat, column_km = 2)
# ht_list = draw(ht_list)
# column_dend(ht_list)
# column_dend(ht_list, on_slice = TRUE)
# ht_list = Heatmap(mat) \%v\% Heatmap(mat)
# ht_list = draw(ht_list)
# column_dend(ht_list)
Expand All @@ -336,7 +353,7 @@ setMethod(f = "row_dend",
# column_dend(ht_list)
setMethod(f = "column_dend",
signature = "HeatmapList",
definition = function(object, name = NULL) {
definition = function(object, name = NULL, on_slice = FALSE) {

if(!object@layout$initialized) {
warning_wrap("The heatmap list has not been initialized. You might have different results if you repeatedly execute this function, e.g. when row_km/column_km was set. It is more suggested to do as `ht_list = draw(ht_list); column_dend(ht_list)`.")
Expand All @@ -345,7 +362,7 @@ setMethod(f = "column_dend",
object = make_layout(object)

if(!is.null(name)) {
return(column_dend(object@ht_list[[ name[1] ]]))
return(column_dend(object@ht_list[[ name[1] ]], on_slice = on_slice))
}

n = length(object@ht_list)
Expand All @@ -355,6 +372,10 @@ setMethod(f = "column_dend",
}

if(object@direction == "vertical") {
if(on_slice) {
return(object@ht_list[[ ht_index[1] ]]@column_dend_slice)
}

lt = object@ht_list[[ ht_index[1] ]]@column_dend_list
if(length(lt) == 1) {
return(lt[[1]])
Expand All @@ -364,8 +385,13 @@ setMethod(f = "column_dend",
} else {
lt_rd = list()
for(i in ht_index) {
lt = object@ht_list[[i]]@column_dend_list
lt_rd = c(lt_rd, list(lt))
if(on_slice) {
lt = object@ht_list[[i]]@column_dend_slice
lt_rd = c(lt_rd, list(lt))
} else {
lt = object@ht_list[[i]]@column_dend_list
lt_rd = c(lt_rd, list(lt))
}
}
names(lt_rd) = names(object@ht_list)[ht_index]
proper_format_lt(lt_rd)
Expand All @@ -378,6 +404,7 @@ setMethod(f = "column_dend",
#
# == param
# -object A `Heatmap-class` object.
# -on_slice If the value is TRUE, it returns the dendrogram on the slice level.
#
# == value
# The format of the returned object depends on whether rows/columns of the heatmaps are split.
Expand All @@ -396,14 +423,18 @@ setMethod(f = "column_dend",
#
setMethod(f = "column_dend",
signature = "Heatmap",
definition = function(object) {
definition = function(object, on_slice = FALSE) {

if(!object@layout$initialized) {
warning_wrap("The heatmap has not been initialized. You might have different results if you repeatedly execute this function, e.g. when row_km/column_km was set. It is more suggested to do as `ht = draw(ht); column_dend(ht)`.")
}

object = prepare(object)


if(on_slice) {
return(object@column_dend_slice)
}

lt = object@column_dend_list
if(length(lt) == 1) {
return(lt[[1]])
Expand Down
2 changes: 1 addition & 1 deletion man/add_heatmap-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Method dispatch page for \code{add_heatmap}.

\itemize{
\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
3 changes: 2 additions & 1 deletion man/column_dend-Heatmap-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ Get Column Dendrograms from a Heatmap
Get Column Dendrograms from a Heatmap
}
\usage{
\S4method{column_dend}{Heatmap}(object)
\S4method{column_dend}{Heatmap}(object, on_slice = FALSE)
}
\arguments{

\item{object}{A \code{\link{Heatmap-class}} object.}
\item{on_slice}{If the value is TRUE, it returns the dendrogram on the slice level.}

}
\value{
Expand Down
4 changes: 3 additions & 1 deletion man/column_dend-HeatmapList-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Get Column Dendrograms from a hHeatmap List
Get Column Dendrograms from a hHeatmap List
}
\usage{
\S4method{column_dend}{HeatmapList}(object, name = NULL)
\S4method{column_dend}{HeatmapList}(object, name = NULL, on_slice = FALSE)
}
\arguments{

\item{object}{A \code{\link{HeatmapList-class}} object.}
\item{name}{Name of a specific heatmap.}
\item{on_slice}{If the value is TRUE, it returns the dendrogram on the slice level.}

}
\value{
Expand All @@ -29,6 +30,7 @@ column_dend(ht_list)
ht_list = Heatmap(mat, column_km = 2) + Heatmap(mat, column_km = 2)
ht_list = draw(ht_list)
column_dend(ht_list)
column_dend(ht_list, on_slice = TRUE)
ht_list = Heatmap(mat) \%v\% Heatmap(mat)
ht_list = draw(ht_list)
column_dend(ht_list)
Expand Down
2 changes: 1 addition & 1 deletion man/column_dend-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{column_dend}.
\code{column_dend} can be dispatched on following classes:

\itemize{
\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{column_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/column_order-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{column_order}.
\code{column_order} can be dispatched on following classes:

\itemize{
\item \code{\link{column_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{column_order,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{column_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/component_height-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{component_height}.
\code{component_height} can be dispatched on following classes:

\itemize{
\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
6 changes: 3 additions & 3 deletions man/draw-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Method dispatch page for \code{draw}.
\code{draw} can be dispatched on following classes:

\itemize{
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/make_layout-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{make_layout}.
\code{make_layout} can be dispatched on following classes:

\itemize{
\item \code{\link{make_layout,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{make_layout,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{make_layout,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
3 changes: 2 additions & 1 deletion man/row_dend-Heatmap-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ Get Row Dendrograms from a Heatmap
Get Row Dendrograms from a Heatmap
}
\usage{
\S4method{row_dend}{Heatmap}(object)
\S4method{row_dend}{Heatmap}(object, on_slice = FALSE)
}
\arguments{

\item{object}{A \code{\link{Heatmap-class}} object.}
\item{on_slice}{If the value is TRUE, it returns the dendrogram on the slice level.}

}
\value{
Expand Down
4 changes: 3 additions & 1 deletion man/row_dend-HeatmapList-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Get Row Dendrograms from a Heatmap List
Get Row Dendrograms from a Heatmap List
}
\usage{
\S4method{row_dend}{HeatmapList}(object, name = NULL)
\S4method{row_dend}{HeatmapList}(object, name = NULL, on_slice = FALSE)
}
\arguments{

\item{object}{A \code{\link{HeatmapList-class}} object.}
\item{name}{Name of a specific heatmap.}
\item{on_slice}{If the value is TRUE, it returns the dendrogram on the slice level.}

}
\value{
Expand All @@ -29,6 +30,7 @@ row_dend(ht_list)
ht_list = Heatmap(mat, row_km = 2) + Heatmap(mat)
ht_list = draw(ht_list)
row_dend(ht_list)
row_dend(ht_list, on_slice = TRUE)
ht_list = Heatmap(mat, row_km = 2) \%v\% Heatmap(mat)
ht_list = draw(ht_list)
row_dend(ht_list)
Expand Down
2 changes: 1 addition & 1 deletion man/row_dend-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{row_dend}.
\code{row_dend} can be dispatched on following classes:

\itemize{
\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{row_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
6 changes: 3 additions & 3 deletions man/show-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Method dispatch page for \code{show}.
\code{show} can be dispatched on following classes:

\itemize{
\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{show,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
\item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
}
}
\examples{
Expand Down

0 comments on commit aa55a66

Please sign in to comment.