写了一个自动设置pheatmap的annotation_colo
2021-03-04 本文已影响0人
一只烟酒僧
pheatmap的annotation_color参数需要一个列表对象,并且列表名要与行和列注释数据框的列名相同,同时列表每个元素的是一个向量,向量名要与变量名相同,手动设置很麻烦,因此就写了一个函数
可以自己上传颜色,也可以用默认的颜色
get_anno_for_heatmap2<-function(annocol,annorow=NULL,color=NULL,only.color=F){
require(plyr)
require(stringr)
if(is.null(color)){
require(RColorBrewer)
color=c(brewer.pal(12,"Set3"),brewer.pal(8,"Set2"),brewer.pal(9,"Set1"),brewer.pal(8,"Dark2"))
}
# annocolor=do.call(as.list,list(x=annocol))
# annocolor=lapply(annocolor,function(x){if(is.factor(x)){x=levels(x);a=color[1:length(x)];names(a)=x;return(a)}else{x=unique(x);a=color[1:length(x)];names(a)=x;return(a)}})
# if(!is.null(annorow)){
# annocolor.row<-do.call(as.list,list(x=annorow))
# annocolor.row=lapply(annocolor.row,function(x){if(is.factor(x)){x=levels(x);a=color[1:length(x)];names(a)=x;return(a)}else{x=unique(x);a=color[1:length(x)];names(a)=x;return(a)}})
# }else{annocolor.row=NULL}
# annocolor=c(annocolor,annocolor.row)
annocolor_col<-as.list(annocol)
annocolor_row<-as.list(annorow)
annocolor<-c(annocol,annorow)
annocolor<-lapply(annocolor,function(x){if(is.factor(x)){x=levels(x);return(x)}else{x=unique(x);return(x)}})
annocolor<-do.call(c,annocolor)
annocolor<-data.frame(var_name=as.factor(str_replace(names(annocolor),"[0-9]{1,}$","")),
var=annocolor,
color=color[1:length(annocolor)])
annocolor<-split(annocolor,annocolor$var_name)
annocolor<-lapply(annocolor,function(x){a=x$var;b=as.character(x$color);names(b)=a;return(b)})
if(only.color){
anno_res<-annocolor
}else{anno_res<-list(annocol=annocol,
annorow=annorow,
annocolor=annocolor)}
return(anno_res)
}
image.png