【生信技能樹】R語言練習(xí)題 - 高級(jí)

首先友情宣傳生信技能樹


題目來源:http://www.bio-info-trainee.com/3409.html


  1. 安裝一些R包:
    數(shù)據(jù)包: ALL, CLL, pasilla, airway
    軟件包:limma,DESeq2停巷,clusterProfiler
    工具包:reshape2
    繪圖包:ggplot2
    不同領(lǐng)域的R包使用頻率不一樣瓮顽,在生物信息學(xué)領(lǐng)域缅刽,尤其需要掌握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")}
  1. 了解ExpressionSet對(duì)象,比如CLL包里面就有data(sCLLex) 刃榨,找到它包含的元素弹砚,提取其表達(dá)矩陣(使用exprs函數(shù)),查看其大小
    參考【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 
  1. 了解 str,head,help函數(shù)枢希,作用于第2題提取到的表達(dá)矩陣
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)
help
  1. 安裝并了解 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"  
  1. 理解 head(toTable(hgu95av2SYMBOL)) 的用法桌吃,找到TP53 基因?qū)?yīng)的探針I(yè)D
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
  1. 理解探針與基因的對(duì)應(yīng)關(guān)系,總共多少個(gè)基因苞轿,基因最多對(duì)應(yīng)多少個(gè)探針茅诱,是哪些基因,是不是因?yàn)檫@些基因很長(zhǎng)搬卒,所以在其上面設(shè)計(jì)多個(gè)探針呢瑟俭?
length(unique(IDs$symbol))
#[1] 8584 
#總共有8584個(gè)基因
max(table(IDs$symbol))
#[1] 8
#一個(gè)基因最多對(duì)應(yīng)8個(gè)探針
table(table(IDs$symbol)==8)
#FALSE  TRUE 
# 8579     5 
#有5個(gè)同時(shí)對(duì)應(yīng)8個(gè)探針的基因
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"
  1. 第2題提取到的表達(dá)矩陣是12625個(gè)探針在22個(gè)樣本的表達(dá)量矩陣,找到那些不在 hgu95av2.db 包收錄的對(duì)應(yīng)著SYMBOL的探針契邀。提示:有1165個(gè)探針是沒有對(duì)應(yīng)基因名字的摆寄。
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"
  1. 過濾表達(dá)矩陣,刪除那1165個(gè)沒有對(duì)應(yīng)基因名字的探針坯门。
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. 整合表達(dá)矩陣微饥,多個(gè)探針對(duì)應(yīng)一個(gè)基因的情況下,只保留在所有樣本里面平均表達(dá)量最大的那個(gè)探針古戴。
    提示:
    1.理解 tapply欠橘,byaggregate允瞧,split函數(shù) , 首先對(duì)每個(gè)基因找到最大表達(dá)量的探針简软。
    2.然后根據(jù)得到探針去過濾原始表達(dá)矩陣
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
  1. 把過濾后的表達(dá)矩陣更改行名為基因的symbol,因?yàn)檫@個(gè)時(shí)候探針和基因是一對(duì)一關(guān)系了述暂。
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
  1. 對(duì)第10題得到的表達(dá)矩陣進(jìn)行探索痹升,先畫第一個(gè)樣本的所有基因的表達(dá)量的boxplot,hist,density , 然后畫所有樣本的這些圖
    1.參考:http://bio-info-trainee.com/tmp/basic_visualization_for_expression_matrix.html
    2.理解ggplot2的繪圖語法畦韭,數(shù)據(jù)和圖形元素的映射關(guān)系
library(ggplot2)
library(reshape2)
library(patchwork)
#讀入表型信息
phenoData <- pData(sCLLex)
colnames(genes_expr) <- phenoData[colnames(genes_expr),1]
#表達(dá)矩陣轉(zhuǎn)置為長(zhǎng)數(shù)據(jù)框
expr_L <- melt(genes_expr)
colnames(expr_L) <- c("gene","sample","exp")
expr_L$pheno <- as.character(phenoData[expr_L$sample,2]) # 長(zhǎng)數(shù)據(jù)框添加表型信息
expr_L[,1] <- as.character(expr_L[,1])
expr_L[,2] <- as.character(expr_L[,2])
#把第一個(gè)樣本的所有數(shù)據(jù)提取出來作為一個(gè)新的數(shù)據(jù)框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)
#頻數(shù)圖
(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)
箱線圖

頻數(shù)圖

密度圖
  1. 理解統(tǒng)計(jì)學(xué)指標(biāo)mean疼蛾,medianmax艺配,min察郁,sdvar转唉,mad并計(jì)算出每個(gè)基因在所有樣本的這些統(tǒng)計(jì)學(xué)指標(biāo)皮钠,最后按照mad值排序,取top 50 mad值的基因赠法,得到列表麦轰。
    注意:這個(gè)題目出的并不合規(guī),請(qǐng)仔細(xì)看砖织。
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" 
  1. 根據(jù)第12題得到top 50 mad值的基因列表來取表達(dá)矩陣的子集款侵,并且熱圖可視化子表達(dá)矩陣。試試看其它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")
heatmap.png
  1. 取不同統(tǒng)計(jì)學(xué)指標(biāo)mean新锈,medianmax眶熬,min妹笆,sdvar聋涨,mad的各top50基因列表晾浴,使用UpSetR包來看他們之間的overlap情況。
  1. 在第2題的基礎(chǔ)上面提取CLL包里面的data(sCLLex) 數(shù)據(jù)對(duì)象的樣本的表型數(shù)據(jù)牍白。
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
  1. 對(duì)所有樣本的表達(dá)矩陣進(jìn)行聚類并且繪圖脊凰,然后添加樣本的臨床表型數(shù)據(jù)信息(更改樣本名)

  1. 對(duì)所有樣本的表達(dá)矩陣進(jìn)行PCA分析并且繪圖,同樣要添加表型信息茂腥。
  1. 根據(jù)表達(dá)矩陣及樣本分組信息進(jìn)行批量T檢驗(yàn)狸涌,得到檢驗(yàn)結(jié)果表格
  1. 使用limma包對(duì)表達(dá)矩陣及樣本分組信息進(jìn)行差異分析,得到差異分析表格最岗,重點(diǎn)看logFCP值帕胆,畫個(gè)火山圖(就是logFC和-log10(P值)的散點(diǎn)圖。)般渡。

  1. 對(duì)T檢驗(yàn)結(jié)果的P值和limma包差異分析的P值畫散點(diǎn)圖懒豹,看看哪些基因相差很大芙盘。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市脸秽,隨后出現(xiàn)的幾起案子儒老,更是在濱河造成了極大的恐慌,老刑警劉巖记餐,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驮樊,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡片酝,警方通過查閱死者的電腦和手機(jī)囚衔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雕沿,“玉大人练湿,你說我怎么就攤上這事∩舐郑” “怎么了鞠鲜?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)断国。 經(jīng)常有香客問我贤姆,道長(zhǎng),這世上最難降的妖魔是什么稳衬? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任霞捡,我火速辦了婚禮,結(jié)果婚禮上薄疚,老公的妹妹穿的比我還像新娘碧信。我一直安慰自己,他們只是感情好街夭,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布砰碴。 她就那樣靜靜地躺著,像睡著了一般板丽。 火紅的嫁衣襯著肌膚如雪呈枉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天埃碱,我揣著相機(jī)與錄音猖辫,去河邊找鬼。 笑死砚殿,一個(gè)胖子當(dāng)著我的面吹牛啃憎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播似炎,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼辛萍,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼悯姊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起贩毕,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤挠轴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后耳幢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欧啤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年睛藻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片邢隧。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡店印,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出倒慧,到底是詐尸還是另有隱情按摘,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布纫谅,位于F島的核電站炫贤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏付秕。R本人自食惡果不足惜兰珍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望询吴。 院中可真熱鬧掠河,春花似錦、人聲如沸猛计。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奉瘤。三九已至勾拉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盗温,已是汗流浹背望艺。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肌访,地道東北人找默。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像吼驶,于是被迫代替她去往敵國和親惩激。 傳聞我的和親對(duì)象是個(gè)殘疾皇子店煞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容