bioinfoer

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

373

主题
发表于 2025-4-8 10:08:36 | 查看: 75| 回复: 1

背景

用R代码画出文章PMID: 29053835里的图。

Figure 2 Results of the clustering analysis per state. Only 5% of the functional connectivity matrix in each state is shown, representing the strongest connections (i.e. the largest absolute value correlation coefficients). Each square colour represents one of the seven networks. Red lines represent positive functional connectivity, and blue lines represent negative functional connectivity. BG = basal ganglia; AUD = auditory; SMN = sensorimotor; VIS = visual; CEN = cognitive executive; CB = cerebellar network

应用场景

如果展示A-B-C之间的关系,用sankey图(桑基图)就够了。

如果还要展示A-C的关系,直线排列的sankey图就不适合了。

本图用圆圈和连线展示多个亚组之间的关系,包括且不限于基因、基因片段、亚型。

  • 场景一:展示基因融合(fusion),同一个基因的多个片段用同一种颜色,用线把不同基因间融合的片段连接起来。常见于肿瘤基因组DNA测序数据。
  • 场景二:像例图那样,同一组基因用相同的颜色展示,基因间的正、负调控关系用两种颜色的线展示,组间的调控关系一目了然。

输入文件

需要三个文件:

  • Gene_ID.txt,组,此处一个Gene为一组;
  • Gene_feature.txt,亚组,Gene内包含多个Subgroup;
  • Links.txt,Subgroup之间的inks。

如果你要突出显示某个基因或某条连线,在输入文件中找到相应的行,修改颜色或高度即可。

#组ID、组的起止位置
GeneID <- read.table("Gene_ID.txt", header = TRUE, sep = "\t")
head(GeneID)

#亚组的名字和起止位置,至少要有前四列
#最后一列颜色为可选项,可以在输入文件中写好,也可以在后面的代码中指定
GeneFe <- read.table("Gene_feature.txt", header = TRUE, sep = "\t")
head(GeneFe)

#哪两个亚组之间要画连线、线在亚组上的相对位置,至少要有前四列
#后两列颜色“link_col”和高度“link_height”为可选项,可以在输入文件中写好,也可以在后面的代码中指定
Links <- read.table("Links.txt", header = TRUE, sep = "\t")
head(Links)

# 把组ID转换成factor,因为后面画图需要factor
GeneID$GeneID <- factor(GeneID$GeneID, levels = GeneID$GeneID)

GeneID前几行如下:

GeneFe前几行如下:
13.png
Links前几行如下:
14.png

开始画图

#install.packages("circlize")
#install.packages("stringi")
library(stringi)
library(circlize)

#### 绘图区设置

pdf("circlize.pdf",width = 8,height = 6)
par(mar=rep(0,4))
circos.clear()
circos.par(start.degree = 90, #从哪里开始画,沿着逆时针顺序
           gap.degree = 2, #每两个sectors间的间隔大小,此处是每两个组间的间隔
           track.margin = c(0,0.15), #gap.degree规定了左右,track.margin规定上下
           cell.padding = c(0,0,0,0) #?
           )


#### 根据"Gene_ID.txt"画组

#图是多个组围成的一个圈,其中每个组及其亚组都画在一个单独的坐标系里
#基因的方向作为x轴
circos.initialize(factors = GeneID$GeneID,
                  xlim = cbind(GeneID$Gene_Start, GeneID$Gene_End))

circos.trackPlotRegion(ylim = c(0, 1), factors = GeneID$GeneID, track.height=0.1,
                     
                       #panel.fun for each sector
                       panel.fun = function(x, y) {
                         #select details of current sector
                         name = get.cell.meta.data("sector.index") 
                         i = get.cell.meta.data("sector.numeric.index") 
                         xlim = get.cell.meta.data("xlim")
                         ylim = get.cell.meta.data("ylim")
                       
                         #组ID文字的设置
                         circos.text(x = mean(xlim), 
                                     y = 1,
                                     labels = name,
                                     cex = 1.5,   #组ID文字大小
                                     #col = "darkgrey", #组ID的颜色
                                     adj = c(0.5,-0.8)
                                     #如果想让组ID呈放射状排列,就用下面三行代替上面那行
                                     #niceFacing = TRUE, #组ID头朝上
                                     #facing = "reverse.clockwise",
                                     #adj = c(1.5, -0.8) #组ID的位置
                                     )
                       
                         #画组
                         circos.rect(xleft = xlim[1], 
                                     ybottom = ylim[1],
                                     xright = xlim[2], 
                                     ytop = ylim[2],
                                     col = "white",
                                     border = NA)
                         })

#### 根据"Gene_feature.txt"画亚组

#提需求的小伙伴还想让圆圈图中每个方块颜色不同,整体显示彩虹色,就像circlize_rainbow.pdf那样的效果,就把下面这个for循环里的“col = ...”行换成“col = bgcol[i], ”
#运行下面两行,设置彩虹色
library(graphics)
bgcol <- rainbow(sum(GeneID$Gene_End), s = 1, v = 1, start = 0, end = max(1, sum(GeneID$Gene_End) - 1)/sum(GeneID$Gene_End), alpha = 1)

for (i in 1:nrow(GeneFe)){
  ylim = c(0, 1)
  circos.rect(sector.index = GeneFe$GeneID[i], 
              track.index = 1, 
              xleft = GeneFe$Feature_Start[i], # + 0.01, #让feature之间流出空隙
              ybottom = ylim[1], 
              xright = GeneFe$Feature_End[i], # - 0.01, #让feature之间流出空隙
              ytop = ylim[2],
              #如果想让每个方块颜色不同,用彩虹色画每一个Gene feature就运行下面这行
              #col = bgcol[i], 
              #如果想让每个组用同一个颜色,调用输入文件中写好的颜色,就运行下面这行
              col = paste("#", GeneFe$bar_col[i], sep = ""), 
              border = "black")
}

# 亚组的文字
for (i in 1:nrow(GeneFe)){
  ylim = c(0, 1)
  circos.text(GeneFe$Feature_Start[i] + 0.5,
              ylim[1] + 0.5,
              GeneFe$Subgroup[i],
              sector.index = GeneFe$GeneID[i], 
              facing = "inside",
              cex = 0.8)
}

#### 根据"Links.txt"画连线
for(i in 1:nrow(Links)){
  circos.link(sector.index1 = Links$Gene_1[i], 
              point1 = Links[i, 2],
              sector.index2 = Links$Gene_2[i], 
              point2 = Links[i, 4],
            
              #在输入文件中已经写好了连线的颜色
              col = paste("#", Links$link_col[i], sep = ""), 
              #如果不想在输入文件中写颜色,还可以运行下面这行,在这里指定颜色
              #col = "red", 
            
              border = FALSE, 
              lwd = 2, #连线的粗细
              rou = 0.7, #连线起始和终止点的y值(圆半径的百分比)
              h.ratio = 0.7 #连线的拱形高度,数值越大,拱形越低
              #h.ratio = Links$link_height[i] #在输入文件中指定拱形高度
  )
}


#Legend of gene
GeneCol<-data.frame(legendID=GeneFe$GeneID,legendCol=GeneFe$bar_col)
GeneCol_uniq<-base::unique(GeneCol) 
legend(1,0.6,
       bty = "n",#不要边框
       pch = 16,#画成正方形,圆点是16
       legend = GeneCol_uniq$legendID,
       col = paste0("#",GeneCol_uniq$legendCol),
       horiz = FALSE)

#Legend of links
LinksCol<-data.frame(Links$link_col)
LinksCol_uniq<-base::unique(LinksCol) 
legend(1,0.8,
       bty = "n",
       lty = 1,#画线
       lwd = 2,#线的粗细
       legend = c("Pos.","Neg."),
       col = paste0("#",LinksCol_uniq$Links.link_col),
       horiz = FALSE)

dev.off()


用彩虹色画每一个Gene feature如下图

收藏回复 显示全部楼层 道具 举报

发表于 2025-4-8 10:10:30

文中所用数据可以关注公众号“生信喵实验柴”
1.png
发送关键词“20250407”获取

回复 显示全部楼层 道具 举报

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|bioinfoer ( 萌ICP备20244422号 )

GMT+8, 2025-5-10 18:00 , Processed in 0.111816 second(s), 42 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表