環(huán)狀基因表達(dá)熱圖.png
1.讀取基因表達(dá)數(shù)據(jù)
這是我的基因表達(dá)量數(shù)據(jù)
Fig1.PNG
myfiles <- list.files(pattern = "*.csv")
myfiles
matrix<-read.csv(myfiles[1],sep=',',header=T,check.names = FALSE,row.names = 1)
1.1提取部分?jǐn)?shù)據(jù)集
matrix<-subset(matrix,padj<0.2)
1.2提取基因表達(dá)值所在的列,組成新的矩陣,并將矩陣轉(zhuǎn)置
為什么要轉(zhuǎn)置,參考http://www.lxweimin.com/p/115d07af3029
mat =t(matrix[,7:12])
7-12列為每個(gè)樣本的基因表達(dá)量
1.3基因表達(dá)歸一化
mat=scale(mat, center = TRUE, scale = TRUE)
View(mat)
scale函數(shù)可以對(duì)矩陣歸一化;參考http://www.lxweimin.com/p/115d07af3029
1.4對(duì)數(shù)據(jù)進(jìn)行聚類(lèi),從而得到其dendrogram對(duì)象
dend <-as.dendrogram(hclust(dist(t(mat))))
dist函數(shù)計(jì)算microRNA間的距離,hclust來(lái)進(jìn)行層次聚類(lèi)
1.5定義進(jìn)化樹(shù)顏色
library(dendextend)
n=3
dend <-dend %>% set("branches_k_color", k = n)
n可自定義
1.6可視化
par(mar=c(7.5,3,1,0))
plot(dend)
Fig2.png
1.7聚類(lèi)后的矩陣
如圖Fig 2,聚類(lèi)后的矩陣的列的順序會(huì)發(fā)生變化。按此順序,重新排列矩陣。
mat2 = mat[, order.dendrogram(dend)]
1.7.1查看矩陣重排后的樣本名
lable1=row.names(mat2);lable1
[1] "H-CK-1-3" "H-CK-2-3" "H-CK-3-3" "H-PA-1-3" "H-PA-2-3" "H-PA-3-3"
1.7.2查看矩陣重排后的基因名
lable2=colnames(mat2);lable2
只有基因名順序,也就是列名順序發(fā)生變化。
nr = nrow(mat2);nr
[1] 6
nc = ncol(mat2);nc
[1] 86
1.8 構(gòu)建顏色轉(zhuǎn)變函數(shù)
require("circlize")
col_fun = colorRamp2(c(-1.5, 0, 1.5), c("skyblue", "white", "red"))
1.9 矩陣中的數(shù)值轉(zhuǎn)變?yōu)轭伾?/h2>
col_mat = col_fun(mat2)
1.9.1 查看第1列
col_mat[,1]
1.9.1.1 結(jié)果
H-CK-1-3 H-CK-2-3 H-CK-3-3 H-PA-1-3 H-PA-2-3 H-PA-3-3
"#FF0000FF" "#FFDED3FF" "#FFAF96FF" "#ABDBF1FF" "#DCF0F9FF" "#BDE3F4FF"
2.1 畫(huà)板初始化設(shè)置
par(mar=c(0,0,0,0))
circos.clear();circos.par(canvas.xlim =c(-1.3,1.3),
canvas.ylim = c(-1.3,1.3),
cell.padding = c(0,0,0,0),
gap.degree =90)
factors = "a"
circos.initialize(factors, xlim = c(0, ncol(mat2)))
xlim和ylim設(shè)置1.3,是為了防止添加基因名時(shí)溢出
2.2 添加第一個(gè)軌道
circos.track(ylim = c(0, nr),bg.border = NA,track.height = 0.1*nr,
panel.fun = function(x, y) {
for(i in 1:nr) {
circos.rect(xleft = 1:nc - 1, ybottom = rep(nr - i, nc),
xright = 1:nc, ytop = rep(nr - i + 1, nc),
border = "white",
col = col_mat[i,])
circos.text(x = nc,
y = 6.4 -i,
labels = lable1[i],
facing = "downward", niceFacing = TRUE,
cex = 0.6,
adj = c(-0.2, 0))
}
})
2.3 添加基因名稱(chēng)
for(i in 1:nc){
circos.text(x = i-0.4,
y = 7,
labels = lable2[i],
facing = "clockwise", niceFacing = TRUE,
cex = 0.4,adj = c(0, 0))
}
2.4 添加進(jìn)化樹(shù)
max_height <-max(attr(dend, "height"))
circos.track(ylim = c(0, max_height),bg.border = NA,track.height = 0.3,
panel.fun = function(x, y){
circos.dendrogram(dend = dend,
max_height = max_height)
})
circos.clear()
2.5 添加圖例
library(ComplexHeatmap)
lgd <- Legend(at = c(-2,-1, 0, 1, 2), col_fun = col_fun,
title_position = "topcenter",title = "Z-score")
draw(lgd, x = unit(0.7, "npc"), y = unit(0.7, "npc"))
col_mat = col_fun(mat2)
col_mat[,1]
H-CK-1-3 H-CK-2-3 H-CK-3-3 H-PA-1-3 H-PA-2-3 H-PA-3-3
"#FF0000FF" "#FFDED3FF" "#FFAF96FF" "#ABDBF1FF" "#DCF0F9FF" "#BDE3F4FF"
par(mar=c(0,0,0,0))
circos.clear();circos.par(canvas.xlim =c(-1.3,1.3),
canvas.ylim = c(-1.3,1.3),
cell.padding = c(0,0,0,0),
gap.degree =90)
factors = "a"
circos.initialize(factors, xlim = c(0, ncol(mat2)))
xlim和ylim設(shè)置1.3,是為了防止添加基因名時(shí)溢出
circos.track(ylim = c(0, nr),bg.border = NA,track.height = 0.1*nr,
panel.fun = function(x, y) {
for(i in 1:nr) {
circos.rect(xleft = 1:nc - 1, ybottom = rep(nr - i, nc),
xright = 1:nc, ytop = rep(nr - i + 1, nc),
border = "white",
col = col_mat[i,])
circos.text(x = nc,
y = 6.4 -i,
labels = lable1[i],
facing = "downward", niceFacing = TRUE,
cex = 0.6,
adj = c(-0.2, 0))
}
})
for(i in 1:nc){
circos.text(x = i-0.4,
y = 7,
labels = lable2[i],
facing = "clockwise", niceFacing = TRUE,
cex = 0.4,adj = c(0, 0))
}
max_height <-max(attr(dend, "height"))
circos.track(ylim = c(0, max_height),bg.border = NA,track.height = 0.3,
panel.fun = function(x, y){
circos.dendrogram(dend = dend,
max_height = max_height)
})
circos.clear()
library(ComplexHeatmap)
lgd <- Legend(at = c(-2,-1, 0, 1, 2), col_fun = col_fun,
title_position = "topcenter",title = "Z-score")
draw(lgd, x = unit(0.7, "npc"), y = unit(0.7, "npc"))