基因表达聚类分析之初探SOM - 自组织特征图
之前的培訓有老師提出要做SOM分析,最后卡在code plot只能出segment plot卻出不來line plot。查了下,沒看到解決方案。今天看了下源碼,設置了一個參數,得到趨勢圖。也順便學習了SOM分析的整個過程,整理下來,以備以后用到。
更多聚類相關見:
基因共表達聚類分析和可視化
聚類分析factoextra
獲取pheatmap聚類后和標準化后的結果
WGCNA分析,簡單全面的最新教程
一文學會網絡分析——Co-occurrence網絡圖在R中的實現
SOM分析基本理論
SOM (Self-Organizing Feature Map,自組織特征圖)是基于神經網絡方式的數據矩陣和可視化方式。與其它類型的中心點聚類算法如K-means等相似,SOM也是找到一組中心點 (又稱為codebook vector),然后根據最相似原則把數據集的每個對象映射到對應的中心點。在神經網絡術語中,每個神經元對應于一個中心點。
與K-means類似,數據集中的每個對象每次處理一個,判斷最近的中心點,然后更新中心點。與K-means不同的是,SOM中中心點之間存在拓撲形狀順序,在更新一個中心點的同時,鄰近的中心點也會隨著更新,直到達到設定的閾值或中心點不再有顯著變化。最終獲得一系列的中心點 (codes)隱式地定義多個簇,與這個中心點最近的對象歸為同一個簇。
SOM強調簇中心點之間的鄰近關系,相鄰的簇之間相關性更強,更有利于解釋結果,常用于可視化網絡數據或基因表達數據。
Even though SOM is similar to K-means, there is a fundamental difference. Centroids used in SOM have a predetermined topographic ordering relationship. During the training process, SOM uses each data point to update the closest centroid and centroids that are nearby in the topographic ordering. In this way, SOM produces an ordered set of centroids for any given data set. In other words, the centroids that are close to each other in the SOM grid are more closely related to each other than to the centroids that are farther away. Because of this constraint, the centroids of a two-dimensional SOM can be viewed as lying on a two-dimensional surface that tries to fit the n-dimensional data as well as possible. The SOM centroids can also be thought of as the result of a nonlinear regression with respect to the data points. At a high level, clustering using the SOM technique consists of the steps described in Algorithm below:
1: Initialize the centroids. 2: repeat 3: ? ? Select the next object. 4: ? ? Determine the closest centroid to the object. 5: ? ? Update this centroid and the centroids that are close, i.e., in a specified neighborhood. 6: until The centroids don't change much or a threshold is exceeded. 7: Assign each object to its closest centroid and return the centroids and clusters.SOM分析實戰
下面是R中用kohonen包進行基因表達數據的SOM分析。
加載或安裝包
### LOAD LIBRARIES - install with: #install.packages(c("kohonen") library(kohonen)讀入數據并進行標準化
data <- read.table("ehbio_trans.Count_matrix.xls", row.names=1, header=T, sep="\t")# now train the SOM using the Kohonen method # 標準化數據 data_train_matrix <- as.matrix(t(scale(t(data)))) names(data_train_matrix) <- names(data)head(data_train_matrix) untrt_N61311 untrt_N052611 untrt_N080611 untrt_N061011 trt_N61311 ENSG00000223972 ? ?1.6201852 ? ?-0.5400617 ? ?-0.5400617 ? ?-0.5400617 -0.5400617 ENSG00000227232 ? -1.0711639 ? ? 1.0274429 ? ? 0.6776751 ? ? 0.8525590 -1.2460478 ENSG00000278267 ? -1.6476479 ? ? 1.3480756 ? ? 0.1497862 ? ? 0.7489309 -0.4493585 ENSG00000237613 ? ?2.4748737 ? ?-0.3535534 ? ?-0.3535534 ? ?-0.3535534 -0.3535534 ENSG00000238009 ? -0.3535534 ? ?-0.3535534 ? ?-0.3535534 ? ?-0.3535534 ?2.4748737 ENSG00000268903 ? -0.7020086 ? ? 0.9025825 ? ?-0.7020086 ? ?-0.7020086 -0.7020086 trt_N052611 trt_N080611 trt_N061011 ENSG00000223972 ? 1.6201852 ?-0.5400617 ?-0.5400617 ENSG00000227232 ?-1.2460478 ? 0.5027912 ? 0.5027912 ENSG00000278267 ? 0.7489309 ? 0.1497862 ?-1.0485032 ENSG00000237613 ?-0.3535534 ?-0.3535534 ?-0.3535534 ENSG00000238009 ?-0.3535534 ?-0.3535534 ?-0.3535534 ENSG00000268903 ? 0.9025825 ?-0.7020086 ? 1.7048781訓練SOM模型
# 定義網絡的大小和形狀 ? som_grid <- somgrid(xdim = 10, ydim=10, topo="hexagonal") ?# Train the SOM model! som_model <- supersom(data_train_matrix, grid=som_grid, keep.data = TRUE)可視化SOM結果
# Plot of the training progress - how the node distances have stabilised over time. # 展示訓練過程,距離隨著迭代減少的趨勢,判斷迭代是否足夠;最后趨于平穩比較好 plot(som_model, type = "changes")計量每個SOM中心點包含的基因的數目
## custom palette as per kohonen package (not compulsory) coolBlueHotRed <- function(n, alpha = 0.7) {rainbow(n, end=4/6, alpha=alpha)[n:1] }# shows the number of objects mapped to the individual units. # Empty units are depicted in gray. plot(som_model, type = "counts", main="Node Counts", palette.name=coolBlueHotRed)計量SOM中心點的內斂性和質量
# map quality # shows the mean distance of objects mapped to a unit to # the codebook vector of that unit. # The smaller the distances, the better the objects are # represented by the codebook vectors. plot(som_model, type = "quality", main="Node Quality/Distance", palette.name=coolBlueHotRed)鄰居距離-查看潛在邊界點
# 顏色越深表示與周邊點差別越大,越是分界點 # neighbour distances # shows the sum of the distances to all immediate neighbours. # This kind of visualization is also known as a U-matrix plot. # Units near a class boundary can be expected to have higher average distances to their neighbours. # Only available for the "som" and "supersom" maps, for the moment. plot(som_model, type="dist.neighbours", main = "SOM neighbour distances", palette.name=grey.colors)查看SOM中心點的變化趨勢
#code spread plot(som_model, type = "codes", codeRendering="lines")獲取每個SOM中心點相關的基因
table(som_model$unit.classif) # 只顯示一部分1 ? 2 ? 3 ? 4 ? 5 ? 6 197 172 434 187 582 24995 ?96 ?97 ?98 ?99 100 168 919 226 419 193 241 # code是從左至右,從下至上進行編號的 som_model_code_class = data.frame(name=rownames(data_train_matrix), code_class=som_model$unit.classif) head(som_model_code_class) ? ? ? ? ? ? name code_class 1 ENSG00000223972 ? ? ? ? 81 2 ENSG00000227232 ? ? ? ? 37 3 ENSG00000278267 ? ? ? ? 93 4 ENSG00000237613 ? ? ? ? 51 5 ENSG00000238009 ? ? ? ? 11 6 ENSG00000268903 ? ? ? ? ?4SOM結果進一步聚類
# 選擇合適的聚類數目 # show the WCSS metric for kmeans for different clustering sizes. # Can be used as a "rough" indicator of the ideal number of clusters mydata <- as.matrix(as.data.frame(som_model$codes)) wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) for (i in 2:15) wss[i] <- sum(kmeans(mydata, centers=i)$withinss) par(mar=c(5.1,4.1,4.1,2.1)) plot(1:15, wss, type="b", xlab="Number of Clusters",ylab="Within groups sum of squares", main="Within cluster sum of squares (WCSS)") # Form clusters on grid ## use hierarchical clustering to cluster the codebook vectors som_cluster <- cutree(hclust(dist(mydata)), 6) # Colour palette definition cluster_palette <- function(x, alpha = 0.6) {n = length(unique(x)) * 2rainbow(n, start=2/6, end=6/6, alpha=alpha)[seq(n,0,-2)] }cluster_palette_init = cluster_palette(som_cluster) bgcol = cluster_palette_init[som_cluster]#show the same plot with the codes instead of just colours plot(som_model, type="codes", bgcol = bgcol, main = "Clusters", codeRendering="lines") add.cluster.boundaries(som_model, som_cluster)
有一些類的模式不太明顯,以后再看怎么優化。
SOM獲取基因所在的新類
som_model_code_class_cluster = som_model_code_class som_model_code_class_cluster$cluster = som_cluster[som_model_code_class$code_class] head(som_model_code_class_cluster) ? ? ? ? ? ? name code_class cluster 1 ENSG00000223972 ? ? ? ? 81 ? ? ? 2 2 ENSG00000227232 ? ? ? ? 37 ? ? ? 8 3 ENSG00000278267 ? ? ? ? 93 ? ? ? 8 4 ENSG00000237613 ? ? ? ? 51 ? ? ? 7 5 ENSG00000238009 ? ? ? ? 11 ? ? ? 4 6 ENSG00000268903 ? ? ? ? ?4 ? ? ? 3映射某個屬性到SOM圖
# 此處選擇一個樣本作為示例,可以關聯很多信息, # 比如基因通路,只要在矩陣后增加新的屬性就可以。 color_by_var = names(data_train_matrix)[1] color_by = data_train_matrix[,color_by_var] unit_colors <- aggregate(color_by, by=list(som_model$unit.classif), FUN=mean, simplify=TRUE) plot(som_model, type = "property", property=unit_colors[,2], main=color_by_var, palette.name=coolBlueHotRed)R統計和作圖
Graphpad,經典繪圖工具初學初探
維恩(Venn)圖繪制工具大全 (在線+R包)
在R中贊揚下努力工作的你,獎勵一份CheatShet
別人的電子書,你的電子書,都在bookdown
R語言 - 入門環境Rstudio
R語言 - 熱圖繪制 (heatmap)
R語言 - 基礎概念和矩陣操作
R語言 - 熱圖簡化
R語言 - 熱圖美化
R語言 - 線圖繪制
R語言 - 線圖一步法
R語言 - 箱線圖(小提琴圖、抖動圖、區域散點圖)
R語言 - 箱線圖一步法
R語言 - 火山圖
R語言 - 富集分析泡泡圖
R語言 - 散點圖繪制
R語言 - 韋恩圖
R語言 - 柱狀圖
R語言 - 圖形設置中英字體
R語言 - 非參數法生存分析
R語言 - 繪制seq logo圖
WGCNA分析,簡單全面的最新教程
psych +igraph:共表達網絡構建
一文學會網絡分析——Co-occurrence網絡圖在R中的實現
一文看懂PCA主成分分析
富集分析DotPlot,可以服
基因共表達聚類分析和可視化
R中1010個熱圖繪制方法
還在用PCA降維?快學學大牛最愛的t-SNE算法吧, 附Python/R代碼
一個函數抓取代謝組學權威數據庫HMDB的所有表格數據
文章用圖的修改和排版
network3D:?交互式桑基圖
network3D 交互式網絡生成
Seq logo 在線繪制工具——Weblogo
生物AI插圖素材獲取和拼裝指導
ggplot2高效實用指南 (可視化腳本、工具、套路、配色)
圖像處理R包magick學習筆記
SOM基因表達聚類分析初探
利用gganimate可視化全球范圍R-Ladies(R社區性別多樣性組織)發展情況
一分鐘繪制磷脂雙分子層:AI零基礎入門和基本圖形繪制
AI科研繪圖(二):模式圖的基本畫法
你知道R中的賦值符號箭頭(<-)和等號(=)的區別嗎?
R語言可視化學習筆記之ggridges包
利用ComplexHeatmap繪制熱圖(一)
ggplot2學習筆記之圖形排列
R包reshape2,輕松實現長、寬數據表格轉換
用R在地圖上繪制網絡圖的三種方法
PCA主成分分析實戰和可視化 附R代碼和測試數據
iTOL快速繪制顏值最高的進化樹!
12個ggplot2擴展包幫你實現更強大的可視化
編程模板-R語言腳本寫作:最簡單的統計與繪圖,包安裝、命令行參數解析、文件讀取、表格和矢量圖輸出
R語言統計入門課程推薦——生物科學中的數據分析Data Analysis for the Life Sciences
數據可視化基本套路總結
你知道R中的賦值符號箭頭<-和等號=的區別嗎?
使用dplyr進行數據操作30例
交集intersect、并集union、找不同setdiff
R包reshape2,輕松實現長、寬數據表格轉換
1數據類型(向量、數組、矩陣、 列表和數據框)
2讀寫數據所需的主要函數、與外部環境交互
3數據篩選——提取對象的子集
4向量、矩陣的數學運算
5控制結構
6函數及作用域
7認識循環函數lapply和sapply
8分解數據框split和查看對象str
9模擬—隨機數、抽樣、線性模型
1初識ggplot2繪制幾何對象
2圖層的使用—基礎、加標簽、注釋
3工具箱—誤差線、加權數、展示數據分布
4語法基礎
5通過圖層構建圖像
6標度、軸和圖例
7定位-分面和坐標系
8主題設置、存儲導出
9繪圖需要的數據整理技術
創建屬于自己的調色板
28個實用繪圖包,總有幾個適合你
熱圖繪制
R做線性回歸
繪圖相關系數矩陣corrplot
相關矩陣可視化ggcorrplot
繪制交互式圖形recharts
交互式可視化CanvasXpress
聚類分析factoextra
LDA分析、作圖及添加置信-ggord
解決散點圖樣品標簽重疊ggrepel
添加P值或顯著性標記ggpubr
Alpha多樣性稀釋曲線rarefraction curve
堆疊柱狀圖各成分連線畫法:突出組間變化
沖擊圖展示組間時間序列變化ggalluvial
桑基圖riverplot
微生物環境因子分析ggvegan
五彩進化樹與熱圖更配ggtree
多元回歸樹分析mvpart
隨機森林randomForest?分類Classification?回歸Regression
加權基因共表達網絡分析WGCNA
circlize包繪制circos-plot
R語言搭建炫酷的線上博客系統
28個實用繪圖包,總有幾個適合你
熱圖繪制
R做線性回歸
繪圖相關系數矩陣corrplot
相關矩陣可視化ggcorrplot
繪制交互式圖形recharts
交互式可視化CanvasXpress
聚類分析factoextra
LDA分析、作圖及添加置信-ggord
解決散點圖樣品標簽重疊ggrepel
添加P值或顯著性標記ggpubr
Alpha多樣性稀釋曲線rarefraction curve
堆疊柱狀圖各成分連線畫法:突出組間變化
沖擊圖展示組間時間序列變化ggalluvial
桑基圖riverplot
微生物環境因子分析ggvegan
五彩進化樹與熱圖更配ggtree
多元回歸樹分析mvpart
隨機森林randomForest?分類Classification?回歸Regression
加權基因共表達網絡分析WGCNA
circlize包繪制circos-plot
R語言搭建炫酷的線上博客系統
維恩(Venn)圖繪制工具大全 (在線+R包)
R包circlize:柱狀圖用膩了?試試好看的弦狀圖
獲取pheatmap聚類后和標準化后的結果
增強火山圖,要不要試一下?
一個震撼的交互型3D可視化R包 - 可直接轉ggplot2圖為3D
贈你一只金色的眼 - 富集分析和表達數據可視化
是Excel的圖,不!是R的圖
道友,來Rstudio里面看動畫了
用了這么多年的PCA可視化竟然是錯的!!!
R語言可視化學習筆記之ggridges包
萬能轉換:R圖和統計表轉成發表級的Word、PPT、Excel、HTML、Latex、矢量圖等
那天空飄過的梅花月餅,是今年最好的中秋禮物
高顏值免費在線繪圖
往期精品
畫圖三字經?生信視頻?生信系列教程?
心得體會?TCGA數據庫?Linux?Python?
高通量分析?免費在線畫圖?測序歷史?超級增強子
生信學習視頻?PPT?EXCEL?文章寫作?ggplot2
海哥組學?可視化套路?基因組瀏覽器
色彩搭配?圖形排版?互作網絡
自學生信?2019影響因子?GSEA?單細胞?
后臺回復“生信寶典福利第一波”或點擊閱讀原文獲取教程合集
總結
以上是生活随笔為你收集整理的基因表达聚类分析之初探SOM - 自组织特征图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招聘 | 微生太高薪招聘多组学生信官(硕
- 下一篇: “harmony”整合不同平台的单细胞数