Skip to content

Commit

Permalink
Merge pull request #93 from stemangiola/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
stemangiola authored May 13, 2022
2 parents b41f17e + 24bb78d commit 4521bf5
Show file tree
Hide file tree
Showing 14 changed files with 1,319 additions and 1,328 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidyHeatmap
Title: A Tidy Implementation of Heatmap
Version: 1.6.0
Version: 1.7.0
Authors@R:
c(person(given = "Stefano",
family = "Mangiola",
Expand Down
12 changes: 6 additions & 6 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' @param .vertical The name of the column vertically presented in the heatmap
#' @param .abundance The name of the transcript/gene abundance column
#' @param transform A function, used to transform .value, for example log1p
#' @param .scale A character string. Possible values are c(\"none\", \"row\", \"column\", \"both\")
#' @param scale A character string. Possible values are c(\"none\", \"row\", \"column\", \"both\")
#' @param palette_value A character vector, or a function for higher customisation (colorRamp2). This is the palette that will be used as gradient for abundance. If palette_value is a vector of hexadecimal colours, it should have 3 values. If you want more customisation, you can pass to palette_value a function, that is derived as for example `colorRamp2(c(-2, 0, 2), palette_value)`
#' @param palette_grouping A list of character vectors. This is the list of palettes that will be used for grouping
#' @param ... Further arguments to be passed to ComplexHeatmap::Heatmap
Expand All @@ -53,7 +53,7 @@ input_heatmap = function(.data,
.vertical,
.abundance,
transform = NULL,
.scale = "row",
scale = "none",
palette_value = c("#440154FF", "#21908CFF", "#fefada" ), #c(viridis(3)[1:2],"#fefada")
palette_grouping = list(),
...) {
Expand Down Expand Up @@ -105,18 +105,18 @@ input_heatmap = function(.data,
~ (.)
) %>%

# If .scale row
# If scale row
when(
.scale %in% c("row", "both") ~ (.) %>%
scale %in% c("row", "both") ~ (.) %>%
nest(data = -!!.vertical) %>%
mutate(data = map(data, ~ .x %>% mutate(!!.abundance := !!.abundance %>% scale_robust()))) %>%
unnest(data),
~ (.)
) %>%

# If .scale column
# If scale column
when(
.scale %in% c("column", "both") ~ (.) %>%
scale %in% c("column", "both") ~ (.) %>%
nest(data = -!!.horizontal) %>%
mutate(data = map(data, ~ .x %>% mutate(!!.abundance := !!.abundance %>% scale_robust()))) %>%
unnest(data),
Expand Down
70 changes: 22 additions & 48 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,11 @@ setMethod("show", "InputHeatmap", function(object){
#' @param .column The name of the column horizontally presented in the heatmap
#' @param .value The name of the column for the value of the element/feature pair
#' @param transform A function, used to transform .value row-wise (e.g., transform = log1p)
#' @param .scale A character string. Possible values are c(\"none\", \"row\", \"column\", \"both\")
#' @param scale A character string. Possible values are c(\"none\", \"row\", \"column\", \"both\")
#' @param palette_value A character vector This is the palette that will be used as gradient for .value. For example c("red", "white", "blue"). For higher flexibility you can use circlize::colorRamp2\(c\(-2, -1, 0, 1, 2\), viridis::magma\(5\)\)
#' @param palette_grouping A list of character vectors. This is the list of palettes that will be used for grouping. For example list(RColorBrewer::brewer.pal(8, "Accent")) or list(c("#B3E2CD", "#FDCDAC", "#CBD5E8")) or list(c("black", "red"))
#' @param ... Further arguments to be passed to ComplexHeatmap::Heatmap
#'
#' @param annotation DEPRECATED. please use the annotation functions add_* function \(\* one of tile, point, bar, line \).
#' @param type DEPRECATED. please use the annotation functions add_* function \(\* one of tile, point, bar, line \).
#' @param palette_discrete DEPRECATED. please use the annotation functions add_* function \(\* one of tile, point, bar, line \).
#' @param palette_continuous DEPRECATED. please use the annotation functions add_* function \(\* one of tile, point, bar, line \).
#' @param .scale DEPRECATED. please use scale instead \( with no dot prefix \).
#' @param ... The arguments that will be passed to the Heatmap function of ComplexHeatmap backend
#'
#' @details This function takes a tbl as an input and creates a `ComplexHeatmap` plot. The information is stored in a `InputHeatmap` object that is updated along the pipe statement, for example adding annotation layers.
Expand Down Expand Up @@ -162,15 +158,12 @@ setGeneric("heatmap", function(.data,
.column,
.value,
transform = NULL,
.scale = "row",
scale = "none",
palette_value = c("#440154FF", "#21908CFF", "#fefada" ),
palette_grouping = list(),

# DEPRECATED
annotation = NULL,
type = rep("tile", length(quo_names(annotation))),
palette_discrete = list(),
palette_continuous = list(),
.scale = NULL,
...) standardGeneric("heatmap"))

#' Creates a `InputHeatmap` object from `tbl_df` on evaluation creates a `ComplexHeatmap`
Expand All @@ -187,37 +180,33 @@ heatmap_ <-
.column,
.value,
transform = NULL,
.scale = "row",
scale = "none",
palette_value = c("#440154FF", "#21908CFF", "#fefada" ),
palette_grouping = list(),

# DEPRECATED
annotation = NULL,
type = rep("tile", length(quo_names(annotation))),
palette_discrete = list(),
palette_continuous = list(),
.scale = NULL,
...)
{
# Comply with CRAN NOTES
. = NULL

# Make col names
annotation = enquo(annotation)

# Check if transform is of correct type
if(!(is.null(transform) || is_function(transform))) stop("tidyHeatmap says: transform has to be a function. is_function(transform) == TRUE")

# Check if .scale is of correct type
if(.scale %in% c("none", "row", "column", "both") %>% `!`) stop("tidyHeatmap says: the .scale parameter has to be one of c(\"none\", \"row\", \"column\", \"both\")")
# Check if scale is of correct type
if(scale %in% c("none", "row", "column", "both") %>% `!`) stop("tidyHeatmap says: the scale parameter has to be one of c(\"none\", \"row\", \"column\", \"both\")")

# Check if type is of the right kind
if(type %>% setdiff(names(type_to_annot_function)) %>% length %>% gt(0))
stop("tidyHeatmap says: not all components of `type` parameter are valid.")
# # Message about change of style, once per session
# if(length(palette_grouping)==0 & getOption("tidyHeatmap_white_group_message",TRUE)) {
# message("tidyHeatmap says: (once per session) from release 1.2.3 the grouping labels have white background by default. To add color for one-ay grouping specify palette_grouping = list(c(\"red\", \"blue\"))")
# options("tidyHeatmap_white_group_message"=FALSE)
# }

# Message about change of style, once per session
if(length(palette_grouping)==0 & getOption("tidyHeatmap_white_group_message",TRUE)) {
message("tidyHeatmap says: (once per session) from release 1.2.3 the grouping labels have white background by default. To add color for one-ay grouping specify palette_grouping = list(c(\"red\", \"blue\"))")
options("tidyHeatmap_white_group_message"=FALSE)
# Message about change of scale, once per session
if(scale == "none" & getOption("tidyHeatmap_default_scaling_none",TRUE)) {
message("tidyHeatmap says: (once per session) from release 1.7.0 the scaling is set to \"none\" by default. Please use scale = \"row\", \"column\" or \"both\" to apply scaling")
options("tidyHeatmap_default_scaling_none"=FALSE)
}

.row = enquo(.row)
Expand All @@ -227,29 +216,14 @@ heatmap_ <-
# Validation
.data %>% validation(!!.column, !!.row, !!.value)

# DEPRECATION OF ANNOTATION
if (is_present(annotation) & !quo_is_null(annotation)) {
# DEPRECATION OF SCALE
if (is_present(.scale) && !is.null(.scale)) {

# Signal the deprecation to the user
deprecate_warn("1.1.0", "tidyHeatmap::heatmap(annotation = )", details = "Please use the new annotation framework instead: heatmap(...) %>% add_tile(...) %>% add_point(...) %>% add_bar() %>% add_line() %>% ...")
deprecate_warn("1.7.0", "tidyHeatmap::heatmap(.scale = )", details = "Please use scale (without dot prefix) instead: heatmap(scale = ...)")

# Deal with the deprecated argument for compatibility
return( .data %>%
plot_heatmap(
.horizontal = !!.column,
.vertical = !!.row,
.abundance = !!.value,
annotation = !!annotation,
type = type,
transform = transform,
.scale = .scale,
palette_value = palette_value,
palette_discrete = palette_discrete,
palette_continuous = palette_continuous,
...
))
scale = .scale
}


.data %>%

Expand All @@ -265,7 +239,7 @@ heatmap_ <-
.vertical = !!.row,
.abundance = !!.value,
transform = transform,
.scale = .scale,
scale = scale,
palette_value = palette_value,
palette_grouping = palette_grouping,
...
Expand Down
63 changes: 35 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ mtcars
``` r
mtcars_heatmap <-
mtcars_tidy |>
heatmap(`Car name`, Property, Value ) |>
heatmap(`Car name`, Property, Value, scale = "row" ) |>
add_tile(hp)
```

## tidyHeatmap says: (once per session) from release 1.2.3 the grouping labels have white background by default. To add color for one-ay grouping specify palette_grouping = list(c("red", "blue"))

``` r
mtcars_heatmap
```

Expand All @@ -170,7 +166,7 @@ mtcars_tidy_groupings =

mtcars_tidy_groupings |>
group_by(vs, property_group) |>
heatmap(`Car name`, Property, Value ) |>
heatmap(`Car name`, Property, Value, scale = "row" ) |>
add_tile(hp)
```

Expand All @@ -182,7 +178,8 @@ We can provide colour palettes to groupings
mtcars_tidy_groupings |>
group_by(vs, property_group) |>
heatmap(
`Car name`, Property, Value ,
`Car name`, Property, Value ,
scale = "row",
palette_grouping = list(

# For first grouping (vs)
Expand All @@ -201,7 +198,7 @@ We can split based on the cladogram

``` r
mtcars_tidy |>
heatmap(`Car name`, Property, Value ) |>
heatmap(`Car name`, Property, Value, scale = "row" ) |>
split_rows(2) |>
split_columns(2)
```
Expand All @@ -214,7 +211,8 @@ stochastic)
``` r
mtcars_tidy |>
heatmap(
`Car name`, Property, Value ,
`Car name`, Property, Value,
scale = "row",
row_km = 2,
column_km = 2
)
Expand All @@ -232,7 +230,8 @@ mtcars_tidy |>
heatmap(
`Car name`,
Property,
Value,
Value,
scale = "row",
palette_value = c("red", "white", "blue")
)
```
Expand All @@ -246,7 +245,8 @@ mtcars_tidy |>
heatmap(
`Car name`,
Property,
Value,
Value,
scale = "row",
palette_value = circlize::colorRamp2(
seq(-2, 2, length.out = 11),
RColorBrewer::brewer.pal(11, "RdBu")
Expand All @@ -263,7 +263,8 @@ mtcars_tidy |>
heatmap(
`Car name`,
Property,
Value,
Value,
scale = "row",
palette_value = circlize::colorRamp2(c(-2, -1, 0, 1, 2), viridis::magma(5))
)
```
Expand All @@ -278,7 +279,8 @@ tidyHeatmap::pasilla |>
heatmap(
.column = sample,
.row = symbol,
.value = `count normalised adjusted`
.value = `count normalised adjusted`,
scale = "row"
) |>
add_tile(condition) |>
add_tile(activation)
Expand All @@ -295,7 +297,8 @@ tidyHeatmap::pasilla |>
heatmap(
.column = sample,
.row = symbol,
.value = `count normalised adjusted`,
.value = `count normalised adjusted`,
scale = "row",
show_heatmap_legend = FALSE
) |>
add_tile(condition, show_legend = FALSE) |>
Expand Down Expand Up @@ -323,7 +326,8 @@ pasilla_plus |>
heatmap(
.column = sample,
.row = symbol,
.value = `count normalised adjusted`
.value = `count normalised adjusted`,
scale = "row"
) |>
add_tile(condition) |>
add_point(activation) |>
Expand All @@ -344,7 +348,8 @@ pasilla_plus |>
heatmap(
.column = sample,
.row = symbol,
.value = `count normalised adjusted`
.value = `count normalised adjusted`,
scale = "row"
) |>
add_tile(condition, size = unit(0.3, "cm"), annotation_name_gp= gpar(fontsize = 8)) |>
add_point(activation, size = unit(0.3, "cm"), annotation_name_gp= gpar(fontsize = 8)) |>
Expand All @@ -368,7 +373,8 @@ tidyHeatmap::pasilla |>
heatmap(
.column = sample,
.row = symbol,
.value = `count normalised adjusted`
.value = `count normalised adjusted`,
scale = "row"
) |>
layer_point(
`count normalised adjusted log` > 6 & sample == "untreated3"
Expand All @@ -384,7 +390,8 @@ tidyHeatmap::pasilla |>
``` r
mtcars_tidy |>
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
rect_gp = grid::gpar(col = "#161616", lwd = 0.5)
)
```
Expand All @@ -396,7 +403,8 @@ mtcars_tidy |>
``` r
mtcars_tidy |>
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
cluster_rows = FALSE
)
```
Expand All @@ -407,15 +415,11 @@ mtcars_tidy |>

``` r
library(forcats)
```

## Warning: package 'forcats' was built under R version 4.1.2

``` r
mtcars_tidy |>
mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
cluster_rows = FALSE
)
```
Expand All @@ -428,7 +432,8 @@ mtcars_tidy |>
mtcars_tidy |>
mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
column_dend_height = unit(0.2, "cm"),
row_dend_width = unit(0.2, "cm")
)
Expand All @@ -442,7 +447,8 @@ mtcars_tidy |>
mtcars_tidy |>
mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
row_names_gp = gpar(fontsize = 7),
column_names_gp = gpar(fontsize = 7),
column_title_gp = gpar(fontsize = 7),
Expand All @@ -461,7 +467,8 @@ library(patchwork)
p_heatmap =
mtcars_tidy |>
heatmap(
`Car name`, Property, Value,
`Car name`, Property, Value,
scale = "row",
show_heatmap_legend = FALSE,
row_names_gp = gpar(fontsize = 7)
)
Expand Down
Binary file modified man/fragments/figures/unnamed-chunk-18-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-19-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-26-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/fragments/figures/unnamed-chunk-27-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4521bf5

Please sign in to comment.