@@ -17,7 +17,7 @@ bg__dropout_plot_base <- function (expr_mat, xlim = NA, suppress.plot=FALSE) {
1717 if (! suppress.plot ) {
1818 par(fg = " black" )
1919 if (! (sum(is.na(xlim )))) {
20- plot(xes ,gene_info $ p , main = " " , ylab = " Dropout Proportion" , xlab = " log (expression)" , col = dens.col ,pch = 16 , xlim = xlim , ylim = c(0 ,1 ))
20+ plot(xes ,gene_info $ p , main = " " , ylab = " Dropout Proportion" , xlab = " log10 (expression)" , col = dens.col ,pch = 16 , xlim = xlim , ylim = c(0 ,1 ))
2121 } else {
2222 plot(xes ,gene_info $ p , main = " " , ylab = " Dropout Proportion" , xlab = " log(expression)" , col = dens.col ,pch = 16 )
2323 }
@@ -55,10 +55,10 @@ bg__expression_heatmap <- function (genes, expr_mat, cell_labels=NA, gene_labels
5555 if (! is.numeric(genes )) {
5656 new_genes = match(genes , rownames(expr_mat ));
5757 nomatch = sum(is.na(new_genes ));
58- if (nomatch > 0 ) {warning(paste(nomatch , " genes could not be matched to data, they will not be included in the heatmap." ));}
58+ if (nomatch > 0 ) {warning(paste(" Warning: " , nomatch , " genes could not be matched to data, they will not be included in the heatmap." ));}
5959 genes = new_genes [! is.na(new_genes )];
6060 }
61- if (length(genes ) < 1 ) {warning( " No genes for heatmap." );return ();}
61+ if (length(genes ) < 1 ) {stop( " Error: No genes for heatmap." );return ();}
6262 # Plot heatmap of expression
6363 heatcolours <- rev(brewer.pal(11 ," RdBu" ))
6464 col_breaks = c(- 100 ,seq(- 2 ,2 ,length = 10 ),100 )
@@ -71,7 +71,7 @@ bg__expression_heatmap <- function (genes, expr_mat, cell_labels=NA, gene_labels
7171 if (! is.na(key_genes [1 ])) {
7272 rownames(heat_data )[rownames(expr_mat [genes ,]) %in% key_genes ] = rownames(expr_mat [genes ,])[rownames(expr_mat [genes ,]) %in% key_genes ];
7373 }
74- colnames(heat_data ) = rep( " " , length(heat_data [ 1 ,] ));
74+ colnames(heat_data ) = 1 : length(colnames( heat_data ));
7575 if (! is.na(key_cells [1 ])) {
7676 colnames(heat_data )[colnames(expr_mat [genes ,]) %in% key_cells ] = colnames(expr_mat [genes ,])[colnames(expr_mat [genes ,]) %in% key_cells ];
7777 }
@@ -98,7 +98,11 @@ bg__expression_heatmap <- function (genes, expr_mat, cell_labels=NA, gene_labels
9898 lmat = rbind(c(6 ,0 ,5 ),c(0 ,0 ,2 ),c(4 ,1 ,3 ))
9999
100100
101- heatmap_output = suppressWarnings(heatmap.2(heat_data , ColSideColors = ColColors , RowSideColors = RowColors , col = heatcolours , breaks = col_breaks , scale = " row" ,symbreaks = T , trace = " none" , dendrogram = " column" , key = FALSE , Rowv = TRUE , Colv = TRUE ,lwid = lwid , lhei = lhei ,lmat = lmat , hclustfun = function (x ){hclust(x ,method = " ward.D2" )}))
101+ if (dim(heat_data )[1 ] < 10000 ) {
102+ heatmap_output = suppressWarnings(heatmap.2(heat_data , ColSideColors = ColColors , RowSideColors = RowColors , col = heatcolours , breaks = col_breaks , scale = " row" ,symbreaks = T , trace = " none" , dendrogram = " column" , key = FALSE , Rowv = TRUE , Colv = TRUE ,lwid = lwid , lhei = lhei ,lmat = lmat , hclustfun = function (x ){hclust(x ,method = " ward.D2" )}))
103+ } else {
104+ heatmap_output = suppressWarnings(heatmap.2(heat_data , ColSideColors = ColColors , RowSideColors = RowColors , col = heatcolours , breaks = col_breaks , scale = " row" ,symbreaks = T , trace = " none" , dendrogram = " column" , key = FALSE , Rowv = FALSE , Colv = TRUE ,lwid = lwid , lhei = lhei ,lmat = lmat , hclustfun = function (x ){hclust(x ,method = " ward.D2" )}))
105+ }
102106 # Custom key
103107 par(fig = c(0 , 1 / (5.2 ),4 / (5.2 ), 1 ), mar = c(4 ,1 ,1 ,1 ), new = TRUE )
104108 scale01 <- function (x , low = min(x ), high = max(x )) {
@@ -149,8 +153,54 @@ M3Drop_Expression_Heatmap <- function(genes, expr_mat, cell_labels=NA, interesti
149153 if (is.numeric(key_cells ) | is.logical(key_cells )) {
150154 key_cells = rownames(expr_mat )[key_cells ];
151155 }
156+ if (is.factor(genes )) {
157+ genes = as.character(genes );
158+ }
159+ if (! is.vector(genes )) {
160+ stop(" Error: genes must be a vector." )
161+ }
152162 heatmap_output = bg__expression_heatmap(genes , expr_mat , cell_labels = cell_labels , gene_labels = as.numeric(gene_labels ), key_genes = as.character(key_genes ), key_cells = key_cells );
153163 invisible (heatmap_output );
154164}
155165
156- M3Drop_Get_Heatmap_Cell_Clusters <- function (heatmap_output , k ) {cutree(as.hclust(heatmap_output $ colDendrogram ), k = k )}
166+ M3Drop_Get_Heatmap_Cell_Clusters <- function (heatmap_output , k ) {
167+ tryCatch(
168+ returned_val <- cutree(as.hclust(heatmap_output $ colDendrogram ), k = k ),
169+ warning = function (w ) {print(w )},
170+ error = function (e ){
171+ print(e );
172+ print(" Dendrogram may have flat branches, trying again" );
173+ returned_val <- hidden_get_clusters(heatmap_output ,k )
174+ }
175+ )
176+ return (returned_val );
177+ }
178+
179+ hidden_get_clusters <- function (heatout , k ){
180+ dendro = heatout $ colDendrogram
181+ curr_k = 1 ;
182+ dendro_list = list (dendro )
183+ dendro_heights = attr(dendro , " height" )
184+ while ( curr_k < k ){
185+ to_split = which(dendro_heights == max(dendro_heights ))
186+ to_split_dendro = dendro_list [[to_split ]]
187+ to_split_height = dendro_heights [to_split ]
188+
189+ children = as.list(to_split_dendro )
190+ for (i in 1 : length(children )) {
191+ dendro_heights = c(dendro_heights ,attr(children [[i ]]," height" ))
192+ dendro_list [[length(dendro_list )+ 1 ]] <- children [[i ]]
193+ }
194+ # Remove to split
195+ dendro_list [to_split ] = NULL
196+ dendro_heights = dendro_heights [- to_split ]
197+ curr_k = curr_k - 1 + length(children )
198+ }
199+ # Make group vector
200+ names_orig_order = labels(dendro )[order(heatout $ colInd )]
201+ groups = rep(0 , times = length(names_orig_order ))
202+ for (i in 1 : length(dendro_list )) {
203+ groups [names_orig_order %in% labels(dendro_list [[i ]])] = i
204+ }
205+ return (groups );
206+ }
0 commit comments