環狀基因表達熱圖.png
1.讀取基因表達數據
這是我的基因表達量數據
Fig1.PNG
myfiles <- list.files(pattern = "*.csv")
myfiles
matrix<-read.csv(myfiles[1],sep=',',header=T,check.names = FALSE,row.names = 1)
1.1提取部分數據集
matrix<-subset(matrix,padj<0.2)
1.2提取基因表達值所在的列,組成新的矩陣,并將矩陣轉置
為什么要轉置,參考http://www.lxweimin.com/p/115d07af3029
mat =t(matrix[,7:12])
7-12列為每個樣本的基因表達量
1.3基因表達歸一化
mat=scale(mat, center = TRUE, scale = TRUE)
View(mat)
scale函數可以對矩陣歸一化;參考http://www.lxweimin.com/p/115d07af3029
1.4對數據進行聚類,從而得到其dendrogram對象
dend <-as.dendrogram(hclust(dist(t(mat))))
dist函數計算microRNA間的距離,hclust來進行層次聚類
1.5定義進化樹顏色
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聚類后的矩陣
如圖Fig 2,聚類后的矩陣的列的順序會發生變化。按此順序,重新排列矩陣。
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
只有基因名順序,也就是列名順序發生變化。
nr = nrow(mat2);nr
[1] 6
nc = ncol(mat2);nc
[1] 86
1.8 構建顏色轉變函數
require("circlize")
col_fun = colorRamp2(c(-1.5, 0, 1.5), c("skyblue", "white", "red"))
1.9 矩陣中的數值轉變為顏色
col_mat = col_fun(mat2)
1.9.1 查看第1列
col_mat[,1]
1.9.1.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"
2.1 畫板初始化設置
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設置1.3,是為了防止添加基因名時溢出
2.2 添加第一個軌道
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 添加基因名稱
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 添加進化樹
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"))