美文网首页
2021-12-14 CompleHeatmap、环形热图

2021-12-14 CompleHeatmap、环形热图

作者: 千容安 | 来源:发表于2021-12-15 00:26 被阅读0次
    library(ComplexHeatmap)
    library('circlize')
    setwd("C:\\Users\\Administrator.DESKTOP-4UQ3Q0K\\Desktop")
    data <- read.table("RNA-seq-2.txt",header = T,row.names = 1,sep="\t",check.names = F)
    # library("readxl")
    # data <- read_excel("RNA-seq-2.xlsx")
    library(dplyr)
    library(ggplot2)
    library(ggrepel)
    data <- data[c(-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22)]
    data<-as.numeric(as.matrix(data))
    # 归一化数据,由于scale函数默认对列进行转化,所以进行两次转置
    df_scaled <-t(scale(t(data)))
    
    #转化为矩阵并对其进行归一化:
    madt<-as.matrix(data)
    madt2<-t(scale(t(madt)))
    #默认参数绘制普通热图
    Heatmap(madt2)
    
    报错:Error in hclust(get_dist(submat, distance), method = method) : 
      NA/NaN/Inf in foreign function call (arg 10)
    test <- data[apply(data, 1, function(x) sd(x)!=0),]
    
    pheatmap(test,scale = "row")
    

    有两千多个基因所以堆在一起,后面将基因减到50个



    #计算数据大小范围
    range(madt2)
    #重新定义热图颜色梯度:
    mycol=colorRamp2(c(-1.7, 0.3, 2.3),c("blue", "white", "red"))
    #绘制基础环形热图:
    circos.heatmap(madt2,col=mycol)
    circos.clear()#绘制完成后需要使用此函数完全清除布局
    #在circos.heatmap()中添加参数进行环形热图的调整和美化:
    circos.par(gap.after=c(50))
    circos.heatmap(madt2,col=mycol,dend.side="inside",rownames.side="outside",
    rownames.col="black",
    rownames.cex=0.9,
    rownames.font=1,
    cluster=TRUE)
    circos.clear()
    #circos.par()调整圆环首尾间的距离,数值越大,距离越宽
    #dend.side:控制行聚类树的方向,inside为显示在圆环内圈,outside为显示在圆环外圈
    #rownames.side:控制矩阵行名的方向,与dend.side相同;但注意二者不能在同一侧,必须一内一外
    #cluster=TRUE为对行聚类,cluster=FALSE则不显示聚类
    #聚类树的调整和美化(需要用到两个别的包):
    install.packages("dendextend")#改颜色
    install.packages("dendsort")#聚类树回调
    library(dendextend)
    library(dendsort)
    circos.par(gap.after=c(50))
    circos.heatmap(madt2,col=mycol,dend.side="inside",rownames.side="outside",track.height = 0.38,
    rownames.col="black",
    rownames.cex=0.9,
    rownames.font=1,
    cluster=TRUE,
    dend.track.height=0.18,
    dend.callback=function(dend,m,si) {
    color_branches(dend,k=15,col=1:15)
    }
    )
    circos.clear()
    #track.height:轨道的高度,数值越大圆环越粗
    #dend.track.height:调整行聚类树的高度
    #dend.callback:用于聚类树的回调,当需要对聚类树进行重新排序,或者添加颜色时使用
    #包含的三个参数:dend:当前扇区的树状图;m:当前扇区对应的子矩阵;si:当前扇区的名称
    #color_branches():修改聚类树颜色
    
    
    #添加列名:
    circos.track(track.index=get.current.track.index(),panel.fun=function(x,y){
    if(CELL_META$sector.numeric.index==1){
    cn=colnames(madt2)
    n=length(cn)
    circos.text(rep(CELL_META$cell.xlim[2],n)+convert_x(0.8,"mm"),#x坐标
    7.8+(1:n)*1.1,#y坐标
    cn,cex=0.8,adj=c(0,1),facing="inside")
    }
    },bg.border=NA)
    circos.clear()
    

    Error: Your circular plot has not been initialized yet!
    出现这种问题的原因是,上面进行circos.clear(),下边的添加列和上面是一起的,分开就会出现报错
    解决(之一):

    circos.initializeWithIdeogram()
    text(0, 0, "default", cex = 1)
    

    或删掉circos.clear()试试。

    如何提取热图聚类簇中的基因?

    library(pheatmap)
    df1 <- read.table("RNA-seq-2.txt",header = T,row.names = 1,sep="\t",check.names = F)
    #自定义渐变色;
    mycol = colorRampPalette(c("purple", "white","orange"))(100)
    #创建分组颜色条所需的数据框;
    Group = factor(c(rep("case",4), rep("control",4)))
    anndf <- data.frame(Group)
    # 报错:Error in `.rowNamesDF<-`(x, value = value) : 'row.names'的长度不对   解决办法:
    df1 <- df1[c(-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22)]
    rownames(anndf) <- colnames(df1)
    
    #自定义分组颜色条的颜色;
    anncol = list(Group=c(case="#99CC00",control="#FF9900"))
    
    #绘制热图;
    #对热图中的文字字号,聚类树高度,渐变颜色条做了自定义;
    #同时,根据聚类结果在行和列方向都添加了“gap”;
    htmap<- pheatmap(df1, scale = "row",
    border="white",
    cellwidth = 18,
    cellheight = 6,
    color = mycol,
    cutree_rows=4,
    cutree_cols=4,
    fontsize = 7,
    fontsize_col = 6,
    fontsize_row = 6,
    treeheight_row=30,
    treeheight_col=30,
    annotation_col=anndf,
    annotation_colors=anncol,
    annotation_legend=F,
    legend_breaks=c(-2.5,-1.5,0,1.5,2.5),
    legend_labels=c("-2.5","-1.5","0","1.5","2.5"),
    angle_col = 90)
    
    #按照热图的聚类顺序获取全部基因列表;
    order <- htmap$tree_row$order
    genelist <- row.names(df1)[order]
    #查看前8个;
    genelist[1:8]
    #提取热图的行方向(基因)的聚类树;
    cluster <- htmap$tree_row
    #绘制聚类树;
    plot(cluster,hang = -1,cex=0.6,axes=FALSE,ann=FALSE)
    
    #对聚类树进行分簇;
    cut <- cutree(cluster,2)
    cut
    #然后,获取第2簇的genes,注意第2簇位于热图的上方;
    (genes<- names(cut)[cut==2])
    #提取相应的表达量数据;
    (genesdf <- df1[genes,])
    #导出到本地;
    write.csv(genesdf,file = "genesdf_4.csv")
    
    df_scaled <- t(scale(t(df1)))
    #接下来获得数据的范围;
    range(df_scaled)
    # [1] -1.577570  2.474874
    #然后,根据数据范围建立自定义颜色映射关系;
    #green-red;
    col_fun = colorRamp2(c(-1.6,0.4,2.4), c("greenyellow","white", "red"))
    #准备分组颜色条数据;
    class = anno_block(gp = gpar(fill = c("#c77cff","#FF9999","#99CC00","#FF9900"),
                                 col="white"),height = unit(5, "mm"),
                       labels = c("case", "control"),
                       labels_gp = gpar(col = "white", fontsize = 8,fontface="bold"))
    group= HeatmapAnnotation(group=class)
    #绘制聚类热图,注意,Heatmap函数其实可以直接指定现有的聚类树的;
    Heatmap(df_scaled,
            rect_gp = gpar(col="white"),
            row_names_gp = gpar(fontsize = 6),
            column_names_gp = gpar(fontsize = 8),
            col = col_fun,
            column_split = 4,
            row_split = 4,
            column_title = NULL,
            row_title = NULL,
            cluster_rows = TRUE,
            show_row_dend = TRUE,   #当基因数较多时可以隐藏行方向的聚类树 = FALSE;
            top_annotation =group,
            name = "Exp")
    

    相关文章

      网友评论

          本文标题:2021-12-14 CompleHeatmap、环形热图

          本文链接:https://www.haomeiwen.com/subject/ocyefrtx.html