GEO數據挖掘練習
搜索文獻,找到GSE號
? ? ? 成骨細胞礦化對基因的影響(Matrix mineralization controls gene expression in osteoblasts)
GSE114237
實驗設計:4個非礦化樣本 4個礦化樣本
R代碼參考
https://mp.weixin.qq.com/s/Z4fK6RObUEfjEyY_2VS4Nw
安裝或載入所需各種包
library(stringi)
library(GEOquery)
library(limma)
library(ggfortify)
library(ggstatsplot)
library(VennDiagram)
? ? 如果有沒安裝的包? 先設置鏡像
r[ "CRAN" ] <- "- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/";
/";
options( repos = r )
BioC <- getOption( "BioC_mirror" );
BioC[ "BioC_mirror" ] <- "- "https://mirrors.ustc.edu.cn/bioc/";
/";
options( BioC_mirror = BioC )
下載GEO數據
library( "GEOquery" )
GSE_name = 'GSE114237'? #這填要下的號
options( 'download.file.method.GEOquery' = 'libcurl' )
gset <- getGEO( GSE_name, getGPL = T )? #getGPL = T意思就是下載這個GSE用的平臺
save( gset, file = 'gset.Rdata' )
制作數據集
load( './'./gset.Rdata' )
View(gset)
library( "GEOquery" )
gset = gset[[1]]? #這時候gset是個list 提取list中的第一個元素 就是表達相關信息
exprSet = exprs( gset )? #Geoquery包函數exprs用來提取表達矩陣
pdata = pData( gset )? #Geoquery包函數pData用來提取樣本信息
group_list = as.character( pdata[, 24] )? #grouplist是個分組信息 作圖用的 該取哪列取哪列 進去看一眼改個數 折騰半天? 他給分成min組合demin組(礦化非礦化)
dim( exprSet )
exprSet[ 1:3, 1:5 ]? #看一眼
n_expr = exprSet[ , grep( "^min", group_list )]? #^這個符號表示取開頭是什么什么的東西
View(n_expr)
g_expr = exprSet[ , grep( "^demin", group_list )]? #n和g這倆命名照代碼上抄的 應該自己改一個能看懂的
exprSet = cbind( n_expr, g_expr )
View(exprSet)
group_list = c(rep( 'min', ncol( n_expr ) ),
rep( 'demin', ncol( g_expr ) ) )
dim( exprSet )? #就是讓min和demin重復多少列就多少遍完寫grouplist里頭
exprSet[ 1:8, 1:8 ]? #瞅一眼
table( group_list )
save( exprSet, group_list, file = 'exprSet_by_group.Rdata')? ? #保存Rdata的寫法
篩選探針? 去除沒有注釋的 或者沒測出來東西的探針
GPL = gset@featureData@data? #之前下GSE同時下了平臺 GPL就是平臺對探針的注釋(意思就是每個探針都對應啥玩意)
colnames( GPL )
view( GPL )
看完感覺不對? 因為GPL里頭探針號沒有對應的Gene symbol? 得下個包找找這個平臺的對應信息 先去GEO上找這個平臺對應什么GPL號? 然后根據GPL號
? 上曾老師博客里找這個平臺對應什么探針? 找著了就把包下下來
BiocInstaller::biocLite('hugene10sttranscriptcluster.db')? #這包里有信息
library(hugene10sttranscriptcluster.db)
toTable(hugene10sttranscriptclusterSYMBOL)? #toTable可以查看包里的對應信息
hgnc_id=toTable(hugene10sttranscriptclusterSYMBOL)? #把這個信息賦值
View(hgnc_id)
合并提取出來的矩陣和對應信息
exprSet = exprSet[ rownames(exprSet) %in% hgnc_id[ , 1 ], ]
hgnc_id = hgnc_id[ match(rownames(exprSet), hgnc_id[ , 1 ] ), ]
dim( exprSet )
dim( hgnc_id )
tail( sort( table( hgnc_id[ , 2 ] ) ), n = 12L )? #n=12L啥意思沒看懂? 回頭查查說明書 先往下進行
取出現頻率最大的交集
MAX = by( exprSet, hgnc_id[ , 2 ],
? ? ? ? ? function(x) rownames(x)[ which.max( rowMeans(x) ) ] )
MAX = as.character(MAX)
exprSet = exprSet[ rownames(exprSet) %in% MAX , ]
rownames( exprSet ) = hgnc_id[ match( rownames( exprSet ), hgnc_id[ , 1 ] ), 2 ]
exprSet = log(exprSet)
dim(exprSet)
exprSet[1:5,1:5]
save(exprSet, group_list, file = 'final_exprSet.Rdata')
聚類分析
install.packages("dplyr")
library(ggfortify)
library(stringi)
install.packages("stringi")
library(ggfortify)
colnames( exprSet ) = paste( group_list, 1:ncol( exprSet ), sep = '_' )? #sep = '_'分隔符默認
nodePar <- list( lab.cex = 0.3, pch = c( NA, 19 ), cex = 0.3, col = "red" )
hc = hclust( dist( t( exprSet ) ) )
png('hclust.png', res = 250, height = 1800)
plot( as.dendrogram( hc ), nodePar = nodePar, horiz = TRUE )
dev.off()
PCA圖
data = as.data.frame( t( exprSet ) )
data$group = group_list
png( 'pca_plot.png', res=80 )
autoplot( prcomp( data[ , 1:( ncol( data ) - 1 ) ] ), data = data, colour = 'group',
? ? ? ? ? label =T, frame = T) + theme_bw()
dev.off()
出完圖發現有兩個樣本數據偏差跟別的比特別大? 決定給他倆刪了
剔除不好的數據樣本
exprSet= exprSet[,1:2&4:7]
exprSet = exprSet[,-3]
exprSet = exprSet[,-7]
完事又發現group list又不對了? 好像還得改一下group list 可能有不太蠢的辦法 但我用了下面這個
#重新定義一下group list
table(group_list)
n_expr=n_expr[,-3]
g_expr=g_expr[,-4]
group_list = c(rep( 'min', ncol( n_expr ) ),
? ? ? ? ? ? ? rep( 'demin',? ? ncol( g_expr ) ) )
table(group_list)
再做一次PCA看看? 結果還湊合吧
data = as.data.frame( t( exprSet ) )
data$group = group_list
png( 'pca_plot.png', res=80 )
autoplot( prcomp( data[ , 1:( ncol( data ) - 1 ) ] ), data = data, colour = 'group',
? ? ? ? ? label =T, frame = T) + theme_bw()
dev.off()
差異分析
library( "limma" )
design <- model.matrix( ~0 + factor( group_list ) )
colnames( design ) = levels( factor( group_list ) )
rownames( design ) = colnames( exprSet )
design
contrast.matrix <- makeContrasts( "demin-min", levels = design )? #這塊得改引號里的名 該改啥改啥
contrast.matrix
fit <- lmFit( exprSet, design )
fit2 <- contrasts.fit( fit, contrast.matrix )
fit2 <- eBayes( fit2 )
nrDEG = topTable( fit2, coef = 1, n = Inf )
write.table( nrDEG, file = "nrDEG.out")
head(nrDEG)
熱圖
library( "pheatmap" )
choose_gene = head( rownames( nrDEG ), 50 )? #取了前50個差異最大的基因? 這數可以自己定
choose_matrix = exprSet[ choose_gene, ]
# choose_matrix = t( scale( t( exprSet ) ) )? 不知道這句是干啥的 給注釋了感覺就好用了
annotation_col = data.frame( CellType = factor( group_list ) )
rownames( annotation_col ) = colnames( exprSet )
pheatmap( fontsize = 5, choose_matrix, annotation_col = annotation_col, show_rownames = T,
? ? ? ? ? annotation_legend = T, filename = "heatmap.png")
火山圖
library( "ggplot2" )
logFC_cutoff <- with( nrDEG, mean( abs( logFC ) ) + 2 * sd( abs( logFC ) ) )
logFC_cutoff
logFC_cutoff = 1? #這1就是火山圖橫坐標 把1里頭的都算成沒啥變異? 這個也可以自己定 根據圖定吧 可以調調
nrDEG$change = as.factor( ifelse( nrDEG$P.Value < 0.05 & abs(nrDEG$logFC) > logFC_cutoff,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ifelse( nrDEG$logFC > logFC_cutoff , 'UP', 'DOWN' ), 'STABLE' ) )? #這down up stable都是自己寫的? 可以寫別的 我感覺寫這個比較好
save( nrDEG, file = "nrDEG.Rdata" )
this_tile <- paste0( 'Cutoff for logFC is ', round( logFC_cutoff, 3 ),
? ? ? ? ? ? ? ? ? ? '\nThe number of up gene is ', nrow(nrDEG[ nrDEG$change =='UP', ] ),
? ? ? ? ? ? ? ? ? ? '\nThe number of down gene is ', nrow(nrDEG[ nrDEG$change =='DOWN', ] ) )? #這是寫圖上面的那三行字
volcano = ggplot(data = nrDEG, aes( x = logFC, y = -log10(P.Value), color = change)) +
? geom_point( alpha = 0.4, size = 1) +? #這里頭這個size是火山圖上那個小點點的大小 可以改 別的也能改 這里頭帶數的都可以改改包括下面的? 我還沒試
? theme_set( theme_set( theme_bw( base_size = 15 ) ) ) +
? xlab( "log2 fold change" ) + ylab( "-log10 p-value" ) +
? ggtitle( this_tile ) + theme( plot.title = element_text( size = 15, hjust = 0.5)) +
? scale_colour_manual( values = c('blue','black','red') )
print( volcano )
ggsave( volcano, filename = 'volcano.png' )
save.image(file = 'project1.Rdata') #保存了一下? 有點干太多了
葫蘆圖
隨便挑了4個基因? 做了一下
special_gene = c( 'GRB14', 'KDM7A', 'DPP4', 'MYPN' )
for( gene in special_gene ){
# gene='GRB14' # 先做一個看看好使不? 要好使就注釋了
filename <- paste( gene, '.png', sep = '' )
? TMP = exprSet[ rownames( exprSet ) == gene, ]
? data = as.data.frame(TMP)
? data$group = group_list
? p <- ggbetweenstats(data = data, x = group,? y = TMP,ylab = gene,xlab = 'g')
? ggsave( p, filename = filename)
}
save.image(file = 'project1.Rdata')