DESeq2的多組比較

DESeq2 multiple group comparison (biostars.org)
說(shuō)來(lái)也神奇仰泻,中文是基本沒有關(guān)于DESeq2的多組比較是怎么搞的厢蒜,都說(shuō)去看文檔漓穿,搞得像是商業(yè)機(jī)密一樣,但是搜了一下英文是有人做過(guò)介紹的,基本運(yùn)行一下下面的代碼就知道怎么搞了老實(shí)說(shuō)不依靠這種,直接弄一個(gè)循環(huán)也是可以搞的,但是我就是嫌麻煩。屋吨。)

# create random data of 12 samples and 4800 genes
x <- round(matrix(rexp(480 * 10, rate=.1), ncol=12), 0)
rownames(x) <- paste("gene", 1:nrow(x))
colnames(x) <- paste("sample", 1:ncol(x))

# fake metadata
coldata <- data.frame(
  condition = factor(c(
    rep("ctl", 3),
    rep("A", 3),
    rep("B", 3),
    rep("C", 3))))

x
coldata
# set `ctl` as reference level
coldata$condition <- relevel(coldata$condition, ref = "ctl")
str(coldata)
library(DESeq2)
dds <- DESeqDataSetFromMatrix(
  countData = x,
  colData = coldata,
  design= ~ condition)
dds <- DESeq(dds, betaPrior = FALSE)
resultsNames(dds)
res <- results(dds, name="condition_A_vs_ctl")
# same as above but with lfc shrinkage
res <- lfcShrink(dds, coef = 'condition_A_vs_ctl', type = 'apeglm', res = res)
res
results(dds, c("condition", "A", "C"))

此外DESeq2文檔里面的確是有案例告訴你怎么搞的,只需要help(results)就有案例了山宾,同樣是運(yùn)行了才知道是怎么搞的,順便也貼在這里了至扰。


## Example 1: two-group comparison

dds <- makeExampleDESeqDataSet(m=4)

dds <- DESeq(dds)
res <- results(dds, contrast=c("condition","B","A"))

# with more than two groups, the call would look similar, e.g.:
# results(dds, contrast=c("condition","C","A"))
# etc.

## Example 2: two conditions, two genotypes, with an interaction term

dds <- makeExampleDESeqDataSet(n=100,m=12)
dds$genotype <- factor(rep(rep(c("I","II"),each=3),2))

design(dds) <- ~ genotype + condition + genotype:condition
dds <- DESeq(dds) 
resultsNames(dds)

# the condition effect for genotype I (the main effect)
results(dds, contrast=c("condition","B","A"))

# the condition effect for genotype II
# this is, by definition, the main effect *plus* the interaction term
# (the extra condition effect in genotype II compared to genotype I).
results(dds, list( c("condition_B_vs_A","genotypeII.conditionB") ))

# the interaction term, answering: is the condition effect *different* across genotypes?
results(dds, name="genotypeII.conditionB")
 
## Example 3: two conditions, three genotypes

# ~~~ Using interaction terms ~~~

dds <- makeExampleDESeqDataSet(n=100,m=18)
dds$genotype <- factor(rep(rep(c("I","II","III"),each=3),2))
design(dds) <- ~ genotype + condition + genotype:condition
dds <- DESeq(dds)
resultsNames(dds)

# the condition effect for genotype I (the main effect)
results(dds, contrast=c("condition","B","A"))

# the condition effect for genotype III.
# this is the main effect *plus* the interaction term
# (the extra condition effect in genotype III compared to genotype I).
results(dds, contrast=list( c("condition_B_vs_A","genotypeIII.conditionB") ))
 
# the interaction term for condition effect in genotype III vs genotype I.
# this tests if the condition effect is different in III compared to I
results(dds, name="genotypeIII.conditionB")

# the interaction term for condition effect in genotype III vs genotype II.
# this tests if the condition effect is different in III compared to II
results(dds, contrast=list("genotypeIII.conditionB", "genotypeII.conditionB"))

# Note that a likelihood ratio could be used to test if there are any
# differences in the condition effect between the three genotypes.

# ~~~ Using a grouping variable ~~~

# This is a useful construction when users just want to compare
# specific groups which are combinations of variables.

dds$group <- factor(paste0(dds$genotype, dds$condition))
design(dds) <- ~ group
dds <- DESeq(dds)
resultsNames(dds)

# the condition effect for genotypeIII
results(dds, contrast=c("group", "IIIB", "IIIA"))


用上面的代碼寫自己的多重比較代碼,把整個(gè)過(guò)程寫成函數(shù)资锰,只需要簡(jiǎn)單輸入組別敢课,每個(gè)組別有幾個(gè)樣本,表達(dá)矩陣,比較的結(jié)果就全部生成到這個(gè)文件夾里面


data<-read.csv("data2.csv")

rownames(data)<-data[,1]
data<-data[,-1]
data#就是普通的表達(dá)矩陣

group=c("A","B","C","D") #就是分成多少組
num=c(4,3,4,4) #這幾組每組分別有多少個(gè)
num
group_list=c("A","B","C","D") #就是分成多少組

num=c(3,3,3,2) #這幾組每組分別有多少個(gè)
num

group_list

num


Batch_Deseq_differnece<-function(exprSet,group,num,save_dir="Alldiffenece",save_dir2="NEW_MA"){
  ##create a folder 
  save_dir<-paste0(save_dir,"/")
  dir.create(save_dir)
  ## creat a group
  group_list= factor(rep(group,num))
  group_list
  colData=data.frame(row.names = colnames(exprSet),
                     group=group_list)
  
  #dat<-data.frame()
  ## use the Deseq2 to have Diffence analyse
  for (i in 1:length(group)){
    name=unique(group)[i]
    print(name)
    colData$group<-relevel(colData$group,ref=name)
    dds=DESeq2::DESeqDataSetFromMatrix(countData = exprSet,
                                       colData = colData,
                                       design = ~group) 
    dds <- dds[ rowSums(DESeq2::counts(dds)) > 10, ]
    dds <- DESeq2::DESeq(dds)
    for (j in 2:length(DESeq2::resultsNames(dds))){
      
      resname=DESeq2::resultsNames(dds)[j]
      
      res=DESeq2::results(dds, name=resname)
      
      res_lfc <- lfcShrink(dds, coef=j, res=res, type="apeglm")
      res_lfc
      #res=res_lfc
      
      summary(res_lfc)
      summary(res)
      
      dir.create(save_dir)
      write.csv(res,paste0(save_dir,resname,".csv"))
      save_dir2=paste0(save_dir2,"/")
      dir.create(save_dir2)
      
      
      
      save_dir_MA=paste0(save_dir2,"/",resname)
      dir.create(save_dir_MA)
      write.csv(res,paste0(save_dir_MA,"/",resname,"_res.csv"))
      write.csv(res_lfc,paste0(save_dir_MA,"/",resname,"_reslfc.csv"))
      png(paste0(save_dir_MA,"/",resname,"_MA.png"),width=600*3,height=3*600,res=72*3) 
      plotMA(res, ylim=c(-3,3),main=paste0(resname," MA"))
      
      dev.off()
      png(paste0(save_dir_MA,"/",resname,"_MAlfc.png"),width=600*3,height=3*600,res=72*3) 
      xlim <- c(1,1e5); ylim<-c(-3,3)
      plotMA( res_lfc, xlim=xlim, ylim=ylim, main=paste0(resname," apeglm"))
      
      dev.off()
      
    }
    
  }
  
}
Batch_Deseq_differnece(data2,group=group_list,num,save_dir = "New",save_dir2="NEW_MA")

一個(gè)簡(jiǎn)單的示例

# Load necessary package
if (!requireNamespace("DESeq2", quietly = TRUE)) {
    install.packages("DESeq2")
}
library(DESeq2)

# Set seed for reproducibility
set.seed(123)

# Generate a matrix of gene expression data
# Let's assume we have 100 genes and 11 samples
genes <- paste0("gene", 1:100)
samples <- paste0("sample", 1:11)
exprSet <- matrix(sample(1:500, 1100, replace=TRUE), nrow=100, ncol=11, 
                  dimnames=list(genes, samples))

# Define group list and num
group_list <- c("A", "B", "C", "D")
num <- c(3, 3, 3, 2)

# Run the function
Batch_Deseq_differnece(exprSet, group=group_list, num, save_dir = "New", save_dir2="NEW_MA")

這里備注一下绷杜,NEW_MA會(huì)有res_lfc的數(shù)據(jù)直秆,它的結(jié)果和res有點(diǎn)不一樣,具體相信哪個(gè)你可以翻一下deseq2的文檔鞭盟,一般來(lái)說(shuō)都是使用res的結(jié)果圾结,但是deseq2官方有點(diǎn)推薦res_lfc的結(jié)果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市齿诉,隨后出現(xiàn)的幾起案子筝野,更是在濱河造成了極大的恐慌晌姚,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件歇竟,死亡現(xiàn)場(chǎng)離奇詭異挥唠,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)焕议,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門宝磨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人盅安,你說(shuō)我怎么就攤上這事唤锉。” “怎么了别瞭?”我有些...
    開封第一講書人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵腌紧,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我畜隶,道長(zhǎng),這世上最難降的妖魔是什么号胚? 我笑而不...
    開封第一講書人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任籽慢,我火速辦了婚禮,結(jié)果婚禮上猫胁,老公的妹妹穿的比我還像新娘箱亿。我一直安慰自己,他們只是感情好弃秆,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開白布届惋。 她就那樣靜靜地躺著,像睡著了一般菠赚。 火紅的嫁衣襯著肌膚如雪脑豹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,079評(píng)論 1 285
  • 那天衡查,我揣著相機(jī)與錄音瘩欺,去河邊找鬼。 笑死拌牲,一個(gè)胖子當(dāng)著我的面吹牛俱饿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播塌忽,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼拍埠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了土居?” 一聲冷哼從身側(cè)響起枣购,我...
    開封第一講書人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤嬉探,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后坷虑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體甲馋,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年迄损,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了定躏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡芹敌,死狀恐怖痊远,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情氏捞,我是刑警寧澤碧聪,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站液茎,受9級(jí)特大地震影響逞姿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捆等,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一滞造、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧栋烤,春花似錦谒养、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至薯定,卻和暖如春始绍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背话侄。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工疆虚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人满葛。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓径簿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親嘀韧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子篇亭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

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