Steps needed to change design (relevel factors) in DESeq2
1
3
Entering edit mode
@krassowskimichal-19240
Last seen 4.0 years ago

The vignette states that:

Instead, one need only run nbinomWaldTest to re-estimate MLE coefficients – these are necessary for apeglm – and then run lfcShrink specifying the coefficient of interest in resultsNames(dds).

We give some examples below of producing equivalent designs for use with coef. We show how the coefficients change with model.matrix, but the user would, for example, either change the levels of dds$condition or replace the design using design(dds)<-, then run nbinomWaldTest followed by lfcShrink.

Which suggests that it is sufficient to replace the design or overwrite the condition factors.

However, it also states:

You should only change the factor levels of variables in the design before running the DESeq2 analysis, not during or afterward.

Which seems contradictory to the above:

cnts <- matrix(rnbinom(n=1000, mu=100, size=1/0.5), ncol=15)
cond <- factor(rep(1:3, each=5))

dds <- DESeq(DESeqDataSetFromMatrix(cnts, DataFrame(cond), ~ cond))
resultsNames(dds)

[1] "Intercept" "cond2vs1" "cond3vs1"

# does not work
dds$cond = relevel(cond, 2)
resultsNames(dds)

[1] "Intercept" "cond2vs1" "cond3vs1"

# works
dds$cond = relevel(cond, 2)
resultsNames(DESeq(dds))

[1] "Intercept" "cond1vs2" "cond3vs2"

And what a user (like me) may incorrectly read from the first paragraph as the "suggested" way:

# did not work when I tried:
dds$cond = relevel(cond, 2)
# I forgot to save the result here:
nbinomWaldTest(dds)
# thus this line was raising an error
lfcShrink(dds, coef='cond_3_vs_2')

Error in lfcShrink(dds, coef = "cond3vs_2") : coef %in% resultsNames(dds) is not TRUE

deseq2 DESeq2 • 9.7k views
ADD COMMENT
0
Entering edit mode

I think that my confusion arose from the re-use of dds which is used to mean "DESeqDataSetFromMatrix" in one place and "DESeq" in the other. Thinking like a C-programmer I've overwritten the first with the second one. But it might be an issue with the vignette as well.

ADD REPLY
2
Entering edit mode
@mikelove
Last seen 10 hours ago
United States

Is there really a problem? When you relevel, try using characters to specify the reference instead of a number.

E.g. if you want the reference level to be "2", you should use

dds$condition <- relevel(dds$condition, "2")

I'll edit the second quote you have above to say this is one exception to the design-change rule (just for re-arranging coefficients).

ADD COMMENT
0
Entering edit mode

I think that you may have missed my point. Please have a look at the last example - it produces an error if I follow the first quote literally.

Re-using characters - this is just a quick minimal reproducible example, I missed the quotes ;)

ADD REPLY
2
Entering edit mode

I don't get an error, can you see where the discrepancy is between your actual R code and mine?

Better to copy paste your actual code when posting so that we can see where the problem lies.

> dds <- makeExampleDESeqDataSet()
> dds$condition <- factor(rep(1:3,each=4)) # suppose 3 groups
> dds <- DESeq(dds)
> resultsNames(dds)
[1] "Intercept"        "condition_2_vs_1" "condition_3_vs_1"
> dds$condition <- relevel(dds$condition, "2")
> dds <- nbinomWaldTest(dds)
> resultsNames(dds)
[1] "Intercept"        "condition_1_vs_2" "condition_3_vs_2"
> lfc <- lfcShrink(dds, coef="condition_3_vs_2", type="apeglm")
ADD REPLY
0
Entering edit mode

I found it, it was my fault. This is the actual code and the error is in question - I did not save the result from nbinomWaldTest(dds) onto new dds.

Edit, because I cannot post more comments due to new-users restrictions: Thank you for maintaining DESeq2 and for your help here.

ADD REPLY
0
Entering edit mode

Ok, and still thanks for pointing out the second quote which does need updating.

ADD REPLY
0
Entering edit mode

Thanks for the example code, would the "condition_1_vs_2" and "condition_2_vs_1" giving same or different results on DEGs and normalized read counts?

I guess my questions is for 3 groups pair-wise comparisons, should I do

lfc <- lfcShrink(dds, coef="condition_2_vs_1", type="apeglm")
lfc <- lfcShrink(dds, coef="condition_3_vs_1", type="apeglm")
lfc <- lfcShrink(dds, coef="condition_3_vs_2", type="apeglm")

or

lfc <- lfcShrink(dds, coef="condition_1_vs_2", type="apeglm")
lfc <- lfcShrink(dds, coef="condition_3_vs_2", type="apeglm")
lfc <- lfcShrink(dds, coef="condition_3_vs_1", type="apeglm")

Thank you very much!

ADD REPLY
1
Entering edit mode

It's not a big problem really - I posted here because I needed 1.5 hours to find out why it's not working as I thought it would - of course, an experienced user should not have that problem, but I thought that maybe this will help someone, someday.

ADD REPLY

Login before adding your answer.

Traffic: 929 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6