首先友情宣传生信技能树
- 安装一些R包:
数据包: ALL, CLL, pasilla, airway
软件包:limma,DESeq2,clusterProfiler
工具包:reshape2
绘图包:ggplot2
不同领域的R包使用频率不一样,在生物信息学领域,尤其需要掌握bioconductor系列包。
rm(list=ls())
options(stringsAsFactors = F)
if (!require("ALL")) {BiocManager::install("ALL")}
if (!require("CLL")) {BiocManager::install("CLL")}
if (!require("pasilla")) {BiocManager::install("pasilla")}
if (!require("airway")) {BiocManager::install("airway")}
if (!require("limma")) {BiocManager::install("limma")}
if (!require("DESeq2")) {BiocManager::install("DESeq2")}
if (!require("clusterProfiler")) {BiocManager::install("clusterProfiler")}
if (!require("reshape2")) {BiocManager::install("reshape2")}
if (!require("ggplot2")) {BiocManager::install("ggplot2")}
- 了解ExpressionSet对象,比如
CLL
包里面就有data(sCLLex)
,找到它包含的元素,提取其表达矩阵(使用exprs函数),查看其大小
参考【1】:http://www.bio-info-trainee.com/bioconductor_China/software/limma.html
参考【2】:https://github.com/bioconductor-china/basic/blob/master/ExpressionSet.md
help("ExpressionSet")
library("CLL")
data(sCLLex)
exprSet <- exprs(sCLLex)
dim(exprSet)
#[1] 12625 22
head(rownames(exprSet))
#[1] "1000_at" "1001_at" "1002_f_at" "1003_s_at" "1004_at"
#[6] "1005_at"
colnames(exprSet)
# [1] "CLL11.CEL" "CLL12.CEL" "CLL13.CEL" "CLL14.CEL" "CLL15.CEL"
# [6] "CLL16.CEL" "CLL17.CEL" "CLL18.CEL" "CLL19.CEL" "CLL20.CEL"
#[11] "CLL21.CEL" "CLL22.CEL" "CLL23.CEL" "CLL24.CEL" "CLL2.CEL"
#[16] "CLL3.CEL" "CLL4.CEL" "CLL5.CEL" "CLL6.CEL" "CLL7.CEL"
#[21] "CLL8.CEL" "CLL9.CEL"
sCLLex
#ExpressionSet (storageMode: lockedEnvironment)
#assayData: 12625 features, 22 samples
# element names: exprs
#protocolData: none
#phenoData
# sampleNames: CLL11.CEL CLL12.CEL ... CLL9.CEL (22 total)
# varLabels: SampleID Disease
# varMetadata: labelDescription
#featureData: none
#experimentData: use 'experimentData(object)'
#Annotation: hgu95av2
- 了解 str,head,help函数,作用于第2题提取到的表达矩阵
str(exprSet)
#num [1:12625, 1:22] 5.74 2.29 3.31 1.09 7.54 ...
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:12625] "1000_at" "1001_at" "1002_f_at" "1003_s_at" ...
# ..$ : chr [1:22] "CLL11.CEL" "CLL12.CEL" "CLL13.CEL" "CLL14.CEL" ...
head(exprSet)
help(exprSet)
- 安装并了解
hgu95av2.db
包,看看 ls("package:hgu95av2.db")
后显示的那些变量
if (!require("hgu95av2.db")){BiocManager::install("hgu95av2.db")}
ls("package:hgu95av2.db")
# [1] "hgu95av2" "hgu95av2.db"
# [3] "hgu95av2_dbconn" "hgu95av2_dbfile"
# [5] "hgu95av2_dbInfo" "hgu95av2_dbschema"
# [7] "hgu95av2ACCNUM" "hgu95av2ALIAS2PROBE"
# [9] "hgu95av2CHR" "hgu95av2CHRLENGTHS"
#[11] "hgu95av2CHRLOC" "hgu95av2CHRLOCEND"
#[13] "hgu95av2ENSEMBL" "hgu95av2ENSEMBL2PROBE"
#[15] "hgu95av2ENTREZID" "hgu95av2ENZYME"
#[17] "hgu95av2ENZYME2PROBE" "hgu95av2GENENAME"
#[19] "hgu95av2GO" "hgu95av2GO2ALLPROBES"
#[21] "hgu95av2GO2PROBE" "hgu95av2MAP"
#[23] "hgu95av2MAPCOUNTS" "hgu95av2OMIM"
#[25] "hgu95av2ORGANISM" "hgu95av2ORGPKG"
#[27] "hgu95av2PATH" "hgu95av2PATH2PROBE"
#[29] "hgu95av2PFAM" "hgu95av2PMID"
#[31] "hgu95av2PMID2PROBE" "hgu95av2PROSITE"
#[33] "hgu95av2REFSEQ" "hgu95av2SYMBOL"
#[35] "hgu95av2UNIGENE" "hgu95av2UNIPROT"
- 理解
head(toTable(hgu95av2SYMBOL))
的用法,找到TP53
基因对应的探针ID
head(toTable(hgu95av2SYMBOL))
# probe_id symbol
#1 1000_at MAPK3
#2 1001_at TIE1
#3 1002_f_at CYP2C19
#4 1003_s_at CXCR5
#5 1004_at CXCR5
#6 1005_at DUSP1
IDs <- toTable(hgu95av2SYMBOL)
IDs[IDs$symbol=='TP53',]
# probe_id symbol
#966 1939_at TP53
#997 1974_s_at TP53
#1420 31618_at TP53
- 理解探针与基因的对应关系,总共多少个基因,基因最多对应多少个探针,是哪些基因,是不是因为这些基因很长,所以在其上面设计多个探针呢?
length(unique(IDs$symbol))
#[1] 8584
#总共有8584个基因
max(table(IDs$symbol))
#[1] 8
#一个基因最多对应8个探针
table(table(IDs$symbol)==8)
#FALSE TRUE
# 8579 5
#有5个同时对应8个探针的基因
a <- table(IDs$symbol)
for (i in 1:length(a)){
if (a[i]==8) {print(names(a[i]))}
}
[1] "GAPDH"
[1] "INPP4A"
[1] "MYB"
[1] "PTGER3"
[1] "STAT1"
- 第2题提取到的表达矩阵是12625个探针在22个样本的表达量矩阵,找到那些不在
hgu95av2.db
包收录的对应着SYMBOL的探针。提示:有1165个探针是没有对应基因名字的。
rownames(exprSet)[!(rownames(exprSet) %in% IDs$probe_id)]
library(dplyr)
rownames(exprSet)[!(rownames(exprSet) %in% IDs$probe_id)] %>% length
#[1] 1166
rownames(exprSet)[!(rownames(exprSet) %in% IDs$probe_id)] %>% head
#[1] "1007_s_at" "1047_s_at" "1089_i_at" "108_g_at" "1090_f_at"
#[6] "1099_s_at"
- 过滤表达矩阵,删除那1165个没有对应基因名字的探针。
tmp <-rownames(exprSet) %in% IDs$probe_id
head(tmp)
#[1] TRUE TRUE TRUE TRUE TRUE TRUE
new_eSet <- exprSet[tmp,]
nrow(exprSet)
#[1] 12625
nrow(new_eSet)
#[1] 11459
- 整合表达矩阵,多个探针对应一个基因的情况下,只保留在所有样本里面平均表达量最大的那个探针。
提示:
1.理解 tapply
,by
,aggregate
,split
函数 , 首先对每个基因找到最大表达量的探针。
2.然后根据得到探针去过滤原始表达矩阵
IDs$median <- apply(new_eSet,1,median)
nrow(IDs)
#[1] 11459
IDs <- IDs[order(IDs$symbol,IDs$median,decreasing = T),]
IDs <- IDs[!duplicated(IDs$symbol),]
nrow(IDs)
#[1] 8584
genes_expr <- new_eSet[as.character(IDs$probe_id),]
dim(genes_expr)
#[1] 8584 22
genes_expr[1:4,1:4]
# CLL11.CEL CLL12.CEL CLL13.CEL CLL14.CEL
#36456_at 6.645791 7.350613 6.333290 6.603640
#38626_at 5.289264 6.677600 4.447104 7.008260
#36958_at 3.949769 5.423343 3.540189 5.234420
#35995_at 4.316881 2.705329 3.131087 2.821306
- 把过滤后的表达矩阵更改行名为基因的symbol,因为这个时候探针和基因是一对一关系了。
rownames(genes_expr) <- IDs$symbol
genes_expr <- genes_expr[order(rownames(genes_expr)),]
genes_expr[1:4,1:4]
# CLL11.CEL CLL12.CEL CLL13.CEL CLL14.CEL
#AADAC 2.837647 2.893664 2.848301 2.868344
#AAK1 3.755016 3.807378 3.716134 3.759093
#AAMP 1.953082 2.010410 2.244479 1.498089
#AANAT 1.179319 1.202284 1.180669 1.211086
- 对第10题得到的表达矩阵进行探索,先画第一个样本的所有基因的表达量的boxplot,hist,density , 然后画所有样本的这些图
1.参考:http://bio-info-trainee.com/tmp/basic_visualization_for_expression_matrix.html
2.理解ggplot2的绘图语法,数据和图形元素的映射关系
library(ggplot2)
library(reshape2)
library(patchwork)
#读入表型信息
phenoData <- pData(sCLLex)
colnames(genes_expr) <- phenoData[colnames(genes_expr),1]
#表达矩阵转置为长数据框
expr_L <- melt(genes_expr)
colnames(expr_L) <- c("gene","sample","exp")
expr_L$pheno <- as.character(phenoData[expr_L$sample,2]) # 长数据框添加表型信息
expr_L[,1] <- as.character(expr_L[,1])
expr_L[,2] <- as.character(expr_L[,2])
#把第一个样本的所有数据提取出来作为一个新的数据框expr_L_1
expr_L_1 <- expr_L[expr_L$sample==expr_L$sample[1],]
#箱线图
(boxplot_1 <- ggplot(data = expr_L_1,aes(x=sample,y=exp)) +
geom_boxplot())
(boxplot_all <- ggplot(data = expr_L,aes(x=sample,y=exp,fill=pheno)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)))
draw_boxplot <- boxplot_1 / boxplot_all
ggsave('boxplot.PNG',draw_boxplot)
#频数图
(histogram_1 <- ggplot(data = expr_L_1,aes(exp,fill=pheno)) +
geom_histogram(bins=200))
(histogram_all <- ggplot(data = expr_L,aes(exp,fill=pheno)) +
geom_histogram() +
facet_wrap(~sample,nrow = 4))
ggsave('histogram_all.png',histogram_all)
#密度图
(density_1 <- ggplot(data = expr_L_1,aes(exp,fill=pheno)) +
geom_density())
(density_all <- ggplot(data = expr_L,aes(exp,fill=pheno)) +
geom_density() +
facet_wrap(~sample,nrow = 4))
ggsave("density_all.png",density_all)
- 理解统计学指标
mean
,median
,max
,min
,sd
,var
,mad
并计算出每个基因在所有样本的这些统计学指标,最后按照mad值排序,取top 50 mad值的基因,得到列表。
注意:这个题目出的并不合规,请仔细看。
e_mean <- apply(genes_expr,1,mean)
e_median <- apply(genes_expr,1,median)
e_max <- apply(genes_expr,1,max)
e_min <- apply(genes_expr,1,min)
e_sd <- apply(genes_expr,1,sd)
e_var <- apply(genes_expr,1,var)
e_mad <- apply(genes_expr,1,mad)
top50_mad <- head(sort(e_mad,decreasing = T),50)
head(top50_mad)
# FAM30A IGF2BP3 DMD TCF7 SLAMF1 FOS
#3.982191 3.234011 3.071541 2.993352 2.944105 2.938078
names(top50_mad)
# [1] "FAM30A" "IGF2BP3" "DMD" "TCF7" "SLAMF1" "FOS"
# [7] "LGALS1" "IGLC1" "ZAP70" "FCN1" "LHFPL2" "HBB"
#[13] "S100A8" "GUSBP11" "COBLL1" "VIPR1" "PCDH9" "IGH"
#[19] "ZNF804A" "TRIB2" "OAS1" "CCL3" "GNLY" "CYBB"
#[25] "VAMP5" "RNASE6" "RGS2" "PLXNC1" "CAPG" "RBM38"
#[31] "VCAN" "APBB2" "ARF6" "TGFBI" "NR4A2" "S100A9"
#[37] "ZNF266" "TSPYL2" "CLEC2B" "FLNA" "H1-10" "DUSP5"
#[43] "DUSP6" "ANXA4" "LPL" "THEMIS2" "P2RY14" "ARHGAP44"
#[49] "TNFSF9" "PFN2"
- 根据第12题得到top 50 mad值的基因列表来取表达矩阵的子集,并且热图可视化子表达矩阵。试试看其它5种热图的包的不同效果。
library(pheatmap)
top50_mad_matrix <- genes_expr[names(top50_mad),]
top50_mad_matrix <- t(scale(t(top50_mad_matrix)))
phenoData <- pData(sCLLex)
rownames(phenoData) <- phenoData$SampleID
annotation_col <- data.frame(
pheno <- factor(phenoData[colnames(top50_mad_matrix),2])
)
colnames(annotation_col) <- "phenoData"
rownames(annotation_col) <- colnames(top50_mad_matrix)
pheatmap(top50_mad_matrix,
cellwidth = 35, cellheight = 12, fontsize = 8,
annotation_col = annotation_col,
filename = "heatmap.png")
- 取不同统计学指标
mean
,median
,max
,min
,sd
,var
,mad
的各top50基因列表,使用UpSetR
包来看他们之间的overlap情况。
- 在第2题的基础上面提取
CLL
包里面的data(sCLLex)
数据对象的样本的表型数据。
phenoData <- pData(sCLLex)
phenoData
# SampleID Disease
#CLL11.CEL CLL11 progres.
#CLL12.CEL CLL12 stable
#CLL13.CEL CLL13 progres.
#CLL14.CEL CLL14 progres.
#CLL15.CEL CLL15 progres.
#CLL16.CEL CLL16 progres.
#CLL17.CEL CLL17 stable
#CLL18.CEL CLL18 stable
#CLL19.CEL CLL19 progres.
#CLL20.CEL CLL20 stable
#CLL21.CEL CLL21 progres.
#CLL22.CEL CLL22 stable
#CLL23.CEL CLL23 progres.
#CLL24.CEL CLL24 stable
#CLL2.CEL CLL2 stable
#CLL3.CEL CLL3 progres.
#CLL4.CEL CLL4 progres.
#CLL5.CEL CLL5 progres.
#CLL6.CEL CLL6 progres.
#CLL7.CEL CLL7 progres.
#CLL8.CEL CLL8 progres.
#CLL9.CEL CLL9 stable
- 对所有样本的表达矩阵进行聚类并且绘图,然后添加样本的临床表型数据信息(更改样本名)
- 对所有样本的表达矩阵进行PCA分析并且绘图,同样要添加表型信息。
- 根据表达矩阵及样本分组信息进行批量T检验,得到检验结果表格
- 使用
limma
包对表达矩阵及样本分组信息进行差异分析,得到差异分析表格,重点看logFC
和P
值,画个火山图(就是logFC和-log10(P值)的散点图。)。
- 对T检验结果的P值和limma包差异分析的P值画散点图,看看哪些基因相差很大。