R/epiMap.R
ef1d8e0d
 #' @title Make Pheatmap from Comparison Matrix
 #' @description
 #' Creates a pheatmap for the top 'loci.percent' of values
 #' of max standard deviation from the comparison matrix
 #' generated by compMatrix(). The rows represent the loci
 #' of the epiallele and the columns represent the sample
 #' names. The columns can be annotated by adding annotation
 #' information as a parameter.
 #' @param compare.matrix The comparison matrix generated from
 #' the compMatrix() function
 #' @param value The value to be graphed in the pheatmap.
 #' Possible values are 'read', 'pdr', 'meth', 'epipoly',
 #' and 'shannon'.
 #' @param annotate A dataframe containing the annotation information
 #' for the columns of the pheatmap. The row names must be the names
 #' of the samples. The columns (any number) are the annotations. E.g. a column
 #' called 'TET2' with factors 'Pos' and 'Neg' for each sample that
 #' is positive or negative for the TET2 gene
 #' @param clustering_distance_rows Distance measure used in clustering rows.
 #' @param clustering_distance_cols Distance measure used in clustering columns.
 #' @param clustering_method clustering method used.
 #' @param annotate.colors A list containing the colors for the
 #' annotation information. Each element in the list is a vector
 #' of colors with names that correspond to the columns of 'annotate'.
 #' @param color a vector of colors used in heatmap.
 #' @param loci.percent The top percentage of loci, as a decimal,
 #' to be displayed on the pheatmap based on standard deviation,
 #' e.g. a value of 0.20 is equivalent to the top 20\% of loci
 #' (default: 0.10)
 #' @param show.rows A boolean stating if the row names should be
 #' displayed on the pheatmap (default: FALSE)
 #' @param show.columns A boolean stating if the column names should
 #' be displayed on the pheatmap (default: FALSE)
 #' @param font.size An integer representing the font size to be used
 #' for the pheatmap labels (default: 6)
 #' @param pdf.height An integer representing the height (in inches)
 #' of the pdf file for the pheatmap (default: 10)
 #' @param pdf.width An integer representing the width (in inches)
 #' of the pdf file for the pheatmap (default: 10)
 #' @param sve A boolean to save the plot (default: FALSE)
 #' @param ... any arguments in the function pheatmap()
 #' @return A pheatmap object that contains the tree data for
 #' both rows and columns and the final pheatmap plot
 #' @examples
8c57f51d
 #' comp.Matrix<-data.frame(
ef1d8e0d
 #' p1=c(0.6,0.3,0.5,0.5,0.5,0.6,0.45,0.57,0.45,0.63,0.58,0.67,0.5,0.42,0.67),
 #' p2=c(0.62,0.63,0.55,0.75,0.84,0.58,1,0.33,1,0.97,0.57,0.68,0.73,0.72,0.82),
 #' p3=c(0.72,0.53,0.62,0.69,0.37,0.85,1,0.63,0.87,0.87,0.82,0.81,0.79,
 #' 0.62,0.68),
 #' N1=c(0.15,0.24,0.15,0.26,0.34,0.32,0.23,0.14,0.26,0.32,0.12,0.16,0.31,
 #' 0.24,0.32),
 #' N2=c(0.32,0.26,0.16,0.36,0.25,0.37,0.12,0.16,0.41,0.47,0.13,0.52,0.42,
 #' 0.41,0.23),
 #' N3=c(0.21,0.16,0.32,0.16,0.36,0.27,0.24,0.26,0.11,0.27,0.39,0.5,0.4,
 #' 0.31,0.33),
 #' type=rep(c("pdr","epipoly","shannon"),c(5,5,5)),
 #' location=rep(c("chr22-327:350:361:364","chr22-755:761:771:773",
 #' "chr22-761:771:773:781","chr22-821:837:844:849","chr22-838:845:850:858"),
 #' 3),stringsAsFactors =FALSE )
 #'
8c57f51d
 #' subtype <- data.frame(Type= c(rep('CEBPA_sil', 3), rep('Normal', 3)),
032a5c42
 #' row.names = colnames(comp.Matrix)[1:6],stringsAsFactors = FALSE)
ef1d8e0d
 #'
8c57f51d
 #' pmap <- epiMap(compare.matrix = comp.Matrix,
ef1d8e0d
 #' value = 'epipoly',annotate = subtype,
 #' clustering_distance_rows = "euclidean",
 #' clustering_distance_cols = "euclidean",
 #' clustering_method = "complete",annotate.colors = NA,
 #' color= colorRampPalette(c("blue","white","red"))(1000),
 #' loci.percent = 1, show.rows = FALSE,
 #' show.columns = TRUE, font.size = 15,
 #' pdf.height = 10, pdf.width = 10, sve = TRUE)
 #' @export
8c57f51d
 epiMap <- function(compare.matrix, value, annotate,
ef1d8e0d
     clustering_distance_rows = "euclidean",
     clustering_distance_cols = "euclidean",
     clustering_method = "complete", annotate.colors = NA,
     color = colorRampPalette(c("blue", "white", "red"))(1000),
     loci.percent = 0.1, show.rows = FALSE, show.columns = FALSE,
     font.size = 6, pdf.height = 10, pdf.width = 10,
     sve = FALSE, ...) {
a7eeccda
     values = c("read", "pdr", "meth", "epipoly", "shannon", "myValues")
ef1d8e0d
     if (!(value %in% values)) {
         stop("Invalid value '", value, "': Possible values are 'read',
a7eeccda
            'pdr', 'meth', 'epipoly', 'myValues' or 'shannon'")
ef1d8e0d
     }
8c57f51d
     value.matrix <- compare.matrix[compare.matrix$type == value,
ef1d8e0d
         -(length(compare.matrix) - 1)]
8c57f51d
     rownames(value.matrix) <- value.matrix$location
     value.matrix <- value.matrix[, -length(value.matrix)]
ef1d8e0d
     value.matrix$sd = apply(value.matrix, 1, sd, na.rm = TRUE)
8c57f51d
     value.matrix <- value.matrix[order(-value.matrix$sd), ]
     value.matrix <- value.matrix[, -length(value.matrix)]
     loci.number <- floor(loci.percent * nrow(value.matrix))
     title <- paste0("Top ", loci.percent * 100, "% of ", value, " values")
     matrix <- value.matrix[seq_len(loci.number), ]
ef1d8e0d
     if (sve) {
8c57f51d
         pmap <- pheatmap::pheatmap(matrix, show_rownames = show.rows,
ef1d8e0d
             show_colnames = show.columns, color = color,
             clustering_distance_rows = clustering_distance_rows,
             clustering_distance_cols = clustering_distance_cols,
             clustering_method = clustering_method,
             annotation_col = annotate, fontsize = font.size,
             annotation_colors = annotate.colors, height = pdf.height,
             width = pdf.width, main = title,
                 filename = paste0(value, "_pheatmap.pdf"), ...)
     } else {
8c57f51d
         pmap <- pheatmap::pheatmap(matrix, show_rownames = show.rows,
ef1d8e0d
             show_colnames = show.columns, color = color,
             clustering_distance_rows = clustering_distance_rows,
             clustering_distance_cols = clustering_distance_cols,
             clustering_method = clustering_method,
             annotation_col = annotate, fontsize = font.size,
             annotation_colors = annotate.colors, main = title, ...)
     }
     pmap
 }