Last updated: 2020-11-27

Checks: 7 0

Knit directory: muse/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200712) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 4da96c0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/pheatmap.Rmd) and HTML (docs/pheatmap.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 4da96c0 Dave Tang 2020-11-27 More clusters
html 2db13ea davetang 2020-11-10 Build site.
Rmd 268312f davetang 2020-11-10 wflow_publish(files = c(“analysis/index.Rmd”, “analysis/google_trends.Rmd”,
html 586b91f Dave Tang 2020-07-12 Build site.
Rmd b1d6edd Dave Tang 2020-07-12 pheatmap

Making a heatmap using the pheatmap package.

example_file <- "https://davetang.org/file/TagSeqExample.tab"
data <- read.delim(example_file, header = TRUE, row.names = "gene")
data_subset <- as.matrix(data[rowSums(data)>50000,])

Default heatmap using pheatmap.

pheatmap(data_subset)

Version Author Date
586b91f Dave Tang 2020-07-12
cal_z_score <- function(x){
  (x - mean(x)) / sd(x)
}

data_subset_norm <- t(apply(data_subset, 1, cal_z_score))
pheatmap(data_subset_norm)

Version Author Date
586b91f Dave Tang 2020-07-12

Using scale produces the same heatmap as using cal_z_score.

pheatmap(data_subset, scale = "row")

Version Author Date
2db13ea davetang 2020-11-10

Two heatmaps.

one <- pheatmap(data_subset, silent = TRUE)
two <- pheatmap(data_subset, silent = TRUE)

grid.arrange(grobs = list(one[[4]], two[[4]]))

Version Author Date
586b91f Dave Tang 2020-07-12

Reproduce the gene dendrogram.

par(mar = c(3.1, 2.1, 1.1, 5.1))

my_hclust_gene <- hclust(dist(data_subset), method = "complete")
my_hclust_gene$height
 [1]  2502.208  3771.244  4252.402  4366.211  4700.444  5069.851  5208.367
 [8]  6439.545  6474.863  6938.482  7983.369  8141.632  9198.185  9849.175
[15] 10818.256 10868.066 11127.621 11168.654 12699.557 12871.187 13511.763
[22] 13549.622 14483.876 14856.478 14860.904 15033.046 16304.877 16574.315
[29] 16935.384 17713.534 18798.131 18904.899 20250.185 22302.634 22512.593
[36] 24345.199 29826.722 30846.374 31530.137 31849.145 40048.202 43714.148
[43] 47029.264 48908.962 56038.953 67891.667 74124.247 95015.400
as.dendrogram(my_hclust_gene) %>%
  plot(horiz = TRUE)

Version Author Date
586b91f Dave Tang 2020-07-12

Reproduce the sample dendrogram.

my_hclust_sample <- hclust(dist(t(data_subset)), method = "complete")

as.dendrogram(my_hclust_sample) %>%
  plot()

Version Author Date
586b91f Dave Tang 2020-07-12

Add annotations.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 2)
my_gene_col <- data.frame(cluster = ifelse(test = my_gene_col == 1, yes = "cluster 1", no = "cluster 2"))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

set.seed(1984)
my_random <- as.factor(sample(x = 1:2, size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

pheatmap(data_subset, annotation_row = my_gene_col, annotation_col = my_sample_col)

Version Author Date
586b91f Dave Tang 2020-07-12

More clusters.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 6)
my_gene_col <- data.frame(cluster = paste0("cluster ", my_gene_col), row.names = names(my_gene_col))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

set.seed(1984)
my_random <- as.factor(sample(x = 1:2, size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

pheatmap(data_subset, annotation_row = my_gene_col, annotation_col = my_sample_col)

Change annotation colours and ordering.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 2)
my_gene_col <- data.frame(cluster = ifelse(test = my_gene_col == 1, yes = "cluster1", no = "cluster2"))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

# change order
my_sample_col$sample <- factor(my_sample_col$sample, levels = c("normal", "tumour"))

set.seed(1984)
my_random <- as.factor(sample(x = c("random1", "random2"), size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

my_colour = list(
    sample = c(normal = "#5977ff", tumour = "#f74747"),
    random = c(random1 = "#82ed82", random2 = "#9e82ed"),
    cluster = c(cluster1 = "#e89829", cluster2 = "#cc4ee0")
)

p <- pheatmap(data_subset,
              annotation_colors = my_colour,
              annotation_row = my_gene_col,
              annotation_col = my_sample_col,
              cellheight = 7,
              cellwidth = 18)

Version Author Date
586b91f Dave Tang 2020-07-12
save_pheatmap_png <- function(x, filename, width=1200, height=1000, res = 150) {
  png(filename, width = width, height = height, res = res)
  grid::grid.newpage()
  grid::grid.draw(x$gtable)
  dev.off()
}

# not run
# save_pheatmap_png(p, "heatmap_colour.png")

Introduce breaks by cutting the dendrogram.

pheatmap(data_subset,
         annotation_row = my_gene_col,
         annotation_col = my_sample_col,
         cutree_rows = 2,
         cutree_cols = 2)

Version Author Date
586b91f Dave Tang 2020-07-12

Dendrogram results from pheatmap().

par(mar = c(3.1, 2.1, 1.1, 5.1))

my_heatmap <- pheatmap(data_subset, silent = TRUE)
names(my_heatmap)
[1] "tree_row" "tree_col" "kmeans"   "gtable"  
my_heatmap$tree_row %>%
  as.dendrogram() %>%
  plot(horiz = TRUE)

Version Author Date
586b91f Dave Tang 2020-07-12

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.3     dendextend_1.14.0 pheatmap_1.0.12   workflowr_1.6.2  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         pillar_1.4.7       compiler_4.0.3     later_1.1.0.1     
 [5] RColorBrewer_1.1-2 git2r_0.27.1       viridis_0.5.1      tools_4.0.3       
 [9] digest_0.6.27      viridisLite_0.3.0  evaluate_0.14      lifecycle_0.2.0   
[13] tibble_3.0.4       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.8       
[17] rstudioapi_0.13    yaml_2.2.1         xfun_0.19          dplyr_1.0.2       
[21] stringr_1.4.0      knitr_1.30         generics_0.1.0     fs_1.5.0          
[25] vctrs_0.3.5        tidyselect_1.1.0   rprojroot_2.0.2    grid_4.0.3        
[29] glue_1.4.2         R6_2.5.0           rmarkdown_2.5      farver_2.0.3      
[33] purrr_0.3.4        ggplot2_3.3.2      magrittr_2.0.1     whisker_0.4       
[37] scales_1.1.1       promises_1.1.1     ellipsis_0.3.1     htmltools_0.5.0   
[41] colorspace_2.0-0   httpuv_1.5.4       stringi_1.5.3      munsell_0.5.0     
[45] crayon_1.3.4