基因課FTP地址:ftp://http://gsx.genek.tv/2020-3-10%E7%9B%B4%E6%92%AD%E4%B8%80%E4%B8%AA%E5%AE%8C%E6%95%B4%E7%9A%84%E8%BD%AC%E5%BD%95%E7%BB%84%E9%A1%B9%E7%9B%AE/
聽(tīng)張旭東老師的課
加載tidyverse
library(tidyverse)
導(dǎo)入數(shù)據(jù) 三張表
表達(dá)矩陣
gene_exp <- read.table('genes.TMM.EXPR.matrix', header = T, row.names = 1)樣本信息表
| samples | strain | stage | 指標(biāo)1 | 指標(biāo)2 | 指標(biāo)3 |
|---|---|---|---|---|---|
| BLO_S1_LD1 | BLO | S1 | 3.5 | 3.0 | 40 |
| BLO_S1_LD2 | BLO | S1 | 3.8 | 3.2 | 48 |
| BLO_S1_LD3 | BLO | S1 | 3.0 | 3.0 | 50 |
| BLO_S2_LD1 | BLO | S2 | 9.5 | 13.0 | 90 |
| BLO_S2_LD2 | BLO | S2 | 9.8 | 13.2 | 88 |
| BLO_S2_LD3 | BLO | S2 | 10.0 | 13.0 | 90 |
- 基因信息表
- Rstudio中導(dǎo)入數(shù)據(jù)時(shí)用 Comment選項(xiàng)跳過(guò)開(kāi)頭是 # 的行
- 信息數(shù)據(jù)不標(biāo)準(zhǔn),需要進(jìn)一步加工,所有建議導(dǎo)tibble的數(shù)據(jù)格式(Rstudio的Dataimport中第二項(xiàng)),用readr R包
不要行名 —— 因?yàn)閠ibble格式?jīng)]有行名 - 需要更換列名
%>%
select(Gene_Id = X1,
Gene_Symbol = X6,
GO = X7,
Ko = X9,
Pathway = X10,
COG = X21,
Gene_name = X22)
第一件事——樣本相關(guān)性分析
cor(gene_exp) # 相關(guān)性計(jì)算
- 相關(guān)系數(shù)分類(lèi)
- 皮爾森相關(guān)系數(shù) pearson
線性相關(guān) - 斯皮爾曼相關(guān)系數(shù) spearman
等級(jí)相關(guān) - 肯德?tīng)栂嚓P(guān)系數(shù) kendall
適用于離散變量、分類(lèi)型變量的相關(guān)系數(shù)
- 皮爾森相關(guān)系數(shù) pearson
- 舉例
- 計(jì)算兩個(gè)基因之間的相關(guān)系數(shù),用皮爾森相關(guān)系數(shù)
- 腫瘤分期相關(guān)基因,分期之間是等級(jí)相關(guān),用斯皮爾曼相關(guān)系數(shù)
- 哪些基因與性別相關(guān),用肯德?tīng)栂嚓P(guān)系數(shù)
- 計(jì)算樣本相關(guān)系數(shù),直接用皮爾森相關(guān)系數(shù)即可
- command
sample_cor <- round(cor(gene_exp) , digits = 2) # round保留兩位小數(shù)
sample_cor <- round(cor(gene_exp, method = 'spearman') , digits = 2) # 可以指定相關(guān)系數(shù)算法
library(pheatmap)
pheatmap(sample_cor)
樣本聚類(lèi)分析
-
第一步:計(jì)算距離矩陣
樣本兩兩之間,誰(shuí)與誰(shuí)的距離要算出來(lái)- sample_dist <- dist(t(gene_exp)) # dist算的是行之間的距離矩陣,所以需要將表達(dá)矩陣轉(zhuǎn)置,t()表示轉(zhuǎn)置
- "euclidean", 歐幾里得距離矩陣,最常用
- "maximum",
第二步:聚類(lèi)
層次聚類(lèi)法
sample_hc <- hclust(sample_dist)
plot(sample_hc)
聚類(lèi)方法簡(jiǎn)述 ?hclust可查看
single 最短聚類(lèi)法; comlpete 最長(zhǎng)聚類(lèi)法(默認(rèn)); median 平均距離法; 進(jìn)化樹(shù)構(gòu)建使用類(lèi)似方法 = UPGMA