4040# ' @importFrom stringr str_replace_all str_wrap
4141# ' @importFrom colorspace RGB diverge_hcl heat_hcl hex
4242# ' @importFrom grDevices colorRampPalette
43+ # ' @importFrom RColorBrewer brewer.pal
4344# '
4445# ' @export
4546# '
@@ -76,7 +77,7 @@ heatmapSC <- function(object,
7677 color.space <- colorspace :: RGB(runif(n ), runif(n ), runif(n ))
7778 color.space <- as(color.space , " LAB" )
7879
79-
80+
8081 # function to create large palette of colors for annotation tracks
8182 .distinctColorPalette <- function (k = 1 , seed ) {
8283 current.color.space <- color.space @ coords
@@ -89,14 +90,14 @@ heatmapSC <- function(object,
8990
9091 # # Function to create cyan to mustard palette
9192 .pal <- function (n ,
92- h = c(237 , 43 ),
93- c = 100 ,
94- l = c(70 , 90 ),
95- power = 1 ,
96- fixup = TRUE ,
97- gamma = NULL ,
98- alpha = 1 ,
99- ... ) {
93+ h = c(237 , 43 ),
94+ c = 100 ,
95+ l = c(70 , 90 ),
96+ power = 1 ,
97+ fixup = TRUE ,
98+ gamma = NULL ,
99+ alpha = 1 ,
100+ ... ) {
100101 if (n < 1L )
101102 return (character (0L ))
102103 h <- rep(h , length.out = 2L )
@@ -230,15 +231,33 @@ heatmapSC <- function(object,
230231 samples.to.include <- samples.to.include [samples.to.include != " " ]
231232 samples.to.include <- gsub(" -" , " _" , samples.to.include )
232233
234+ # Error messaging for metadata
235+
236+ if (is.null(metadata )){
237+ stop(" Error: You should choose at least one annotation track under metadata_to_plot" )
238+ }
239+
240+ if (sum(grepl(" Barcode" ,metadata ,ignore.case = TRUE )) > 0 ){
241+ sprintf(" Annotation Track cannot include Barcode" )
242+ metadata <- metadata [! grepl(' Barcode' , metadata , ignore.case = TRUE )]
243+ }
244+
233245 # Clean up transcript names and print missing genes:
234246 transcripts = gsub(" " , " " , transcripts )
235247
236248 l1 <- length(transcripts )
249+ p1 <- length(proteins )
250+
251+ if (l1 + p1 == 0 ){
252+ stop(sprintf(" At least 1 transcript and/or protein is needed for plotting" ))
253+ }
254+
237255 dups <- transcripts [duplicated(transcripts )]
238256 transcripts <- transcripts [! duplicated(transcripts )]
239257
258+
240259 l2 <- length(transcripts )
241- print( sprintf(" There are %s total unique genes/proteins in the dataset" , l2 ) )
260+ sprintf(" There are %s total unique genes/proteins in the dataset" , l2 )
242261 if (l1 > l2 ) {
243262 warning(sprintf(" \n\n The following duplicate genes were removed: %s" ,
244263 dups ))
@@ -269,21 +288,28 @@ heatmapSC <- function(object,
269288 )
270289 )
271290 }
272- transcripts <- transcripts [transcripts %in% rownames(object )]
273291
292+ transcripts <- transcripts [transcripts %in% rownames(object )]
274293
275294 # Clean up protein names and print missing proteins:
276295 if (! is.null(object @ assays $ Protein )) {
277296 proteins = gsub(" " , " " , proteins )
278297 if (proteins [1 ] != " " ) {
279298 protmiss = setdiff(proteins , rownames(object $ Protein @ scale.data ))
280299 if (length(protmiss ) > 0 ) {
281- print( sprintf(" missing proteins: %s" , protmiss ) )
300+ sprintf(" missing proteins: %s" , protmiss )
282301 }
283302 }
284303 proteins = proteins [proteins %in% rownames(object $ Protein @ scale.data )]
285304 }
286305
306+ # Error messaging for protein annotation tracks:
307+
308+ if (add.gene.or.protein == FALSE & (! is.null(protein.annotations ) | ! is.null(rna.annotations ))) {
309+ stop(" Error: You should choose to add gene or protein annotation tracks if you add protein or rna annotations" )
310+ }
311+
312+
287313 # collect transcript expression data from SCT slot
288314 df.mat1 = NULL
289315 if (length(transcripts ) > 0 ) {
@@ -380,7 +406,7 @@ heatmapSC <- function(object,
380406 annot <- cbind(annot , annot2 )
381407 colnames(annot )[colnames(annot ) == " annot2" ] <- rna.annotations
382408 }
383-
409+
384410 # Arrange columns by metadata tracks:
385411 if (arrange.by.metadata == TRUE ) {
386412 annot <- annot %> % arrange(across(all_of(colnames(annot ))))
@@ -400,7 +426,7 @@ heatmapSC <- function(object,
400426 annotation.col <- annotation.col %> %
401427 mutate_if(is.logical , as.factor )
402428 rownames(annotation.col ) <- rownames(annot )
403- if (dim(annot )[2 ] == 2 ) {
429+ if (dim(annot )[2 ] == 1 ) {
404430 annottitle = colnames(annot )[1 ]
405431 colnames(annotation.col ) = annottitle
406432 }
0 commit comments