Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separator/break #10

Closed
shathab opened this issue Mar 13, 2019 · 5 comments
Closed

Separator/break #10

shathab opened this issue Mar 13, 2019 · 5 comments

Comments

@shathab
Copy link

shathab commented Mar 13, 2019

would it be possible to add a horizontal/vertical line at a specific position in the heatmap to separate groups? something similar to rowsep or colsep in heatmap.2

Thank you in advance for your help!

@taunometsalu
Copy link
Owner

It is possible to separate clusters by adding white space when you specify some larger number under "change data options" - "Number of clusters in rows" or "Number of clusters in columns".

@shathab
Copy link
Author

shathab commented Mar 14, 2019

Thank you for getting back to me. I am using r to generate heatmaps. I am using no cluster option for columns (clustDistCols = NA) but I was wondering if it is possible to add a gap manually between columns in the heatmap to separate groups similar to (gaps_col) or (gaps_row) in pheatmap.

Thanks again appreciate your help

@taunometsalu
Copy link
Owner

Thanks for the clarification. This is not possible indeed at the moment.

I will think a bit what would be the best solution here.

@shathab
Copy link
Author

shathab commented Mar 18, 2019

Thanks for your response. Actually, I was able to add the row/column gap option by modifying your original code as below.

Thanks for the amazing package!

Regards

createHeatmap2 = function(clust, nbrClustersRows, nbrClustersCols, colorAnnoRow, colorAnnoCol, legendColorScheme, plotWidth, plotRatio, colorRangeMin, colorRangeMax, matrixColorScheme, revScheme, cellBorder, fontSizeGeneral, showNumbers, fontSizeNumbers, precisionNumbers, showRownames, fontSizeRownames, showColnames, fontSizeColnames, showAnnoTitlesRow, showAnnoTitlesCol, maxAnnoLevels,gapscol,gapsrow,cellwidth , cellheight,legend ,annotation_legend ){
matFinal = clust$matFinal
annoCol = clust$annoCol
annoRow = clust$annoRow
hcRows = clust$hcRows
hcCols = clust$hcCols
captionInfo = clust$captionInfo

#filter row annotations:
if(!is.null(annoRow) && (length(colorAnnoRow) == 1) && all(is.na(colorAnnoRow))){
colorAnnoRow = 1:ncol(annoRow) #keep all
}
if(!is.null(annoRow) && length(colorAnnoRow) > 0){
if(is.numeric(colorAnnoRow)) colorAnnoRow = colnames(annoRow)[colorAnnoRow]
if(!all(colorAnnoRow %in% colnames(annoRow))) return(frame())
annoRow2 = annoRow[, colorAnnoRow, drop = FALSE]
} else {
annoRow2 = NA
}

#filter column annotations:
if(!is.null(annoCol) && (length(colorAnnoCol) == 1) && is.na(colorAnnoCol)){
colorAnnoCol = 1:ncol(annoCol) #keep all
}
if(!is.null(annoCol) && length(colorAnnoCol) > 0){
if(is.numeric(colorAnnoCol)) colorAnnoCol = colnames(annoCol)[colorAnnoCol]
if(!all(colorAnnoCol %in% colnames(annoCol))) return(frame())
annoCol2 = annoCol[, colorAnnoCol, drop = FALSE]
} else {
annoCol2 = NA
}

#calculate default values if needed:
hmOptions = calculateHmOptions(matFinal)
if(is.na(colorRangeMin)) colorRangeMin = hmOptions$colorRangeMin
if(is.na(colorRangeMax)) colorRangeMax = hmOptions$colorRangeMax
if(is.na(fontSizeRownames)) fontSizeRownames = hmOptions$fontSizeRownames
if(is.na(fontSizeColnames)) fontSizeColnames = hmOptions$fontSizeColnames

#remove annotations with large number of levels:
alr = annoLevels(annoRow2, maxAnnoLevels)
alc = annoLevels(annoCol2, maxAnnoLevels)
annoRow2 = alr$anno
annoCol2 = alc$anno
removed = c(alr$removed, alc$removed)
if(!is.null(removed)){
message = paste0("The following annotations have more than ", maxAnnoLevels, " levels and were removed from the plot: '", paste0(removed, collapse = "', '"), "'.")
} else {
message = NULL
}

#image dimensions:
picwIn = plotWidth / 2.54
pichIn = picwIn * plotRatio
dotsPerCm = 96 / 2.54 #how many points per cm
picw = picwIn * 2.54 * dotsPerCm
pich = pichIn * 2.54 * dotsPerCm

#color scheme:
colScheme = RColorBrewer::brewer.pal(n = 7, name = matrixColorScheme)
if(revScheme) colScheme = rev(colScheme)
nbrColors = 100
colVec = colorRampPalette(colScheme)(nbrColors)
colBreaks = seq(colorRangeMin, colorRangeMax, length.out = nbrColors + 1)

#outside the specified range, set color to respective min/max color:
if(colorRangeMin != hmOptions$colorRangeMin){
colVec = c(head(colVec, 1), colVec)
colBreaks = c(hmOptions$colorRangeMin, colBreaks)
}
if(colorRangeMax != hmOptions$colorRangeMax){
colVec = c(colVec, tail(colVec, 1))
colBreaks = c(colBreaks, hmOptions$colorRangeMax)
}

#current implementation of pheatmap reverses the annotations:
annoRow2 = rev(annoRow2)
annoCol2 = rev(annoCol2)

#calculate annotation colors, default ones are sometimes strange
legendColors = c(lapply(annoRow2, calcAnnoLegendColors, legendColorScheme), lapply(annoCol2, calcAnnoLegendColors, legendColorScheme))
legendColors = legendColors[sapply(legendColors, length) > 0] #default colors if not factor or character

q = pheatmap::pheatmap(matFinal,
annotation_row = annoRow2, annotation_col = annoCol2, annotation_colors = legendColors,
cluster_rows = hcRows, cluster_cols = hcCols,
cutree_rows = nbrClustersRows, cutree_cols = nbrClustersCols,
color = colVec, breaks = colBreaks,
border_color = cellBorder,
show_rownames = showRownames, fontsize_row = fontSizeRownames,
show_colnames = showColnames, fontsize_col = fontSizeColnames,
annotation_names_row = showAnnoTitlesRow, annotation_names_col = showAnnoTitlesCol,
display_numbers = showNumbers, number_format = paste0("%.", precisionNumbers, "f"),
fontsize = fontSizeGeneral, fontsize_number = fontSizeNumbers,
width = picwIn, height = pichIn, silent = TRUE,gaps_col=gapscol,gaps_row=gapsrow,cellwidth = cellwidth, cellheight = cellwidth,legend = legend,annotation_legend=annotation_legend

)
graphics.off()

caption = createCaption(type = "hm", info = captionInfo)
if(class(hcRows) != "hclust"){
wr = 1:nrow(matFinal)
} else {
wr = q$tree_row$order
}
if(class(hcCols) != "hclust"){
wc = 1:ncol(matFinal)
} else {
wc = q$tree_col$order
}
cells = matFinal[wr, wc, drop = FALSE]
list(q = q, pich = pich, picw = picw, pichIn = pichIn, picwIn = picwIn, message = message, caption = caption, cells = cells)
}

generateHeatmapedited = function(proc, showImputed = TRUE, transpose = FALSE, clustDistRows = "correlation", clustMethodRows = "average", treeOrderingRows = NA, nbrClustersRows = 1, clustDistCols = "correlation", clustMethodCols = "average", treeOrderingCols = NA, nbrClustersCols = 1, colorAnnoRow = NA, colorAnnoCol = NA, legendColorScheme = "Set1", plotWidth = 25, plotRatio = 0.8, colorRangeMin = NA, colorRangeMax = NA, matrixColorScheme = "RdBu", revScheme = TRUE, cellBorder = "grey60", fontSizeGeneral = 10, showNumbers = FALSE, fontSizeNumbers = 12, precisionNumbers = 2, showRownames = TRUE, fontSizeRownames = NA, showColnames = TRUE, fontSizeColnames = NA, showAnnoTitlesRow = TRUE, showAnnoTitlesCol = TRUE, maxAnnoLevels = 50,gapscol=NULL,gapsrow=NULL,cellwidth = NA, cellheight = NA,legend = TRUE,annotation_legend=TRUE){
if(!(class(proc) %in% c("proc", "NULL"))){
stop("class of the proc parameter is incorrect!")
}
trans = transposeMatrix(proc, showImputed = showImputed, transpose = transpose)
clust = clusterMatrix(trans, clustDistRows = clustDistRows, clustMethodRows = clustMethodRows, treeOrderingRows = treeOrderingRows, clustDistCols = clustDistCols, clustMethodCols = clustMethodCols, treeOrderingCols = treeOrderingCols)
l = createHeatmap2(clust = clust, nbrClustersRows = nbrClustersRows, nbrClustersCols = nbrClustersCols, colorAnnoRow = colorAnnoRow, colorAnnoCol = colorAnnoCol, legendColorScheme = legendColorScheme, plotWidth = plotWidth, plotRatio = plotRatio, colorRangeMin = colorRangeMin, colorRangeMax = colorRangeMax, matrixColorScheme = matrixColorScheme, revScheme = revScheme, cellBorder = cellBorder, fontSizeGeneral = fontSizeGeneral, showNumbers = showNumbers, fontSizeNumbers = fontSizeNumbers, precisionNumbers = precisionNumbers, showRownames = showRownames, fontSizeRownames = fontSizeRownames, showColnames = showColnames, fontSizeColnames = fontSizeColnames, showAnnoTitlesRow = showAnnoTitlesRow, showAnnoTitlesCol = showAnnoTitlesCol, maxAnnoLevels = maxAnnoLevels,gapscol=gapscol,gapsrow=gapsrow,cellwidth = cellwidth, cellheight = cellheight,legend = legend,annotation_legend=annotation_legend)
structure(l, class = "hm")
}

@taunometsalu
Copy link
Owner

I added ellipsis to make it possible to pass custom parameters to pheatmap function: 73e8d66

This is a more general solution and it is now possible to call generateHeatmap function like this:
generateHeatmap(proc, clustDistRows = NA, gaps_row = c(5, 10, 15))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants