DESEq2 - How to extract ONE results table/coefficient from the DESeqResults object
1
0
Entering edit mode
@paulavaccarello-24089
Last seen 4.2 years ago

My dataset: 3 different cell types, with 3 replicates each

Celltype is the factor and "Alveolarmacrophages" "Interstitialmacrophages" "Tcells" are the factor levels.

I created the DESeq object:

>dds <- DESeqDataSetFromMatrix(countData = only_counts,
                              colData = sample_info,
                              design= ~ Cell_type)

and calculated the results from the comparison of one factor level against the others:

>res <- results(dds, alpha=0.05)

So the DESeqResults object has three elements:

> resultsNames(dds) 
[1] "Intercept"                                                  "Cell_type_Interstitial_macrophages_vs_Alveolar_macrophages"
[3] "Cell_type_T_cells_vs_Alveolar_macrophages"   

Now I want to access ONE of these elements for further analysis. When I type 'res' I only see one of them

> res
log2 fold change (MLE): Cell type T cells vs Alveolar macrophages 
Wald test p-value: Cell type T cells vs Alveolar macrophages 
DataFrame with 1978 rows and 6 columns

So I would also like to access the other one, "Cell_type_Interstitial_macrophages_vs_Alveolar_macrophages". I have tried:

> res["Cell_type_Interstitial_macrophages_vs_Alveolar_macrophages"]
Error: subscript contains invalid names
>res[[2]]   #super large vector
> res_am <- results(dds, coef=2)
Error in results(dds, coef = 2) : unused argument (coef = 2).  #I find this error weird since the same is used in the DESeq vignette in the LFC example

When I do

>res_im_vs_am <- results(dds, name="Cell_type_Interstitial_macrophages_vs_Alveolar_macrophages")

this is not really accessing the element from 'res', it´s recalculating the results, all the information that I included when I did res<-results(dds,alpha=0.05) is lost; e.g. that alpha=0.05, or any other customization I could have added.

Is there any way to directly access the elements that resultsNames(dds) shows?

Right now I only have 3 levels, but later I will add others, so it doesn't make sense to do contrast by contrast.

Is there any way to do all the possible comparisons in a single line of code and store the resulting data frames in e.g. a list?

deseq2 results DESeqResults • 2.7k views
ADD COMMENT
1
Entering edit mode
@mikelove
Last seen 11 hours ago
United States

"all the information that I included when I did res<-results(dds,alpha=0.05) is lost;"

Why aren't you providing those arguments to your results() call?

Think of results() as an extractor, it doesn't modify dds which is the dataset object. This is why we have the paradigm:

dds <- modify(dds)
dds <- modify(dds)
...
res <- extract(dds)

We change the variable name in the documentation and vignette to indicate that the last function is not returning the dataset.

So if you want a results table for interstitial vs alveolar with target alpha of 0.05 you should call results() while specifying those as arguments.

"Is there any way to do all the possible comparisons in a single line of code and store the resulting data frames in e.g. a list?"

No, but you can easily call results() multiple times.

ADD COMMENT
0
Entering edit mode

Thanks! the last answer simplifies my question, if it's not possible then all the comparisons must be written by hand, or do this step in galaxy.

I have been doing this manually and in the end I encountered a problem, which refers to the first part of your answer. If results() does not modify dds, why resultsNames() is applied on dds and only the first results() that you used is considered? To explain myself better:

I started the results function with the first, default, level:

results(dds, alpha=0.05) res <- results(dds, alpha=0.05) #extracting the DESEqResults object resultsNames(dds) # lists the coefficients [1] "Intercept" "CelltypeInterstitialmacrophagesvsAlveolarmacrophages" [3] "CelltypeTcellsvsAlveolarmacrophages"

Then I started storing the different pairwise comparisons, after running results() every time and, additionally, doing a lfc shrinkage:

Comparisons that were included in the first results(dds):

resimvsam <- results(dds, alpha=0.05, name="CelltypeInterstitialmacrophagesvsAlveolarmacrophages") resimvsam <- lfcShrink(dds, coef=2, res=resimvs_am, type="apeglm")

restvsam <- results(dds, alpha=0.05, name="CelltypeTcellsvsAlveolarmacrophages") restvsam <- lfcShrink(dds, coef=3, res=res, type="apeglm")

The comparison missing:

restvsim <- results(dds,alpha=0.05, contrast=c("Celltype", "Tcells","Interstitialmacrophages"))
restvsim <- lfcShrink(dds, coef="Celltype Tcells vs Interstitialmacrophages", res=restvsim, type="apeglm") Error in lfcShrink(dds, coef = "Celltype Tcells vs Interstitialmacrophages", : coef %in% resultsNamesDDS is not TRUE

But here I could not apply the shrinkage because this last comparison was not included in results(dds), even though I specified another results table...so isn't it in this case results() information stored in dds?

I tried to "add" the last comparison to dds by running results() without assigning it to anything, but resultsNames() did not change.

results(dds,alpha=0.05, contrast=c("Celltype", "Tcells","Interstitial_macrophages"))

ADD REPLY
1
Entering edit mode

By the way, you can properly format your code by putting triple backticks around code blocks and single backticks around in-line code (e.g. objects).

x <- rnorm(10) # triple backticks around this

Single backticks: x, or y.

ADD REPLY
1
Entering edit mode

Re: your last question, this is answered in the vignette section about lfcShrink. The apeglm shrinkage only works on comparisons that are directly in resultsNames, not on more complicated contrasts. You can either follow the workaround described in the vignette for apeglm, or you can use type="ashr" which can work with contrasts.

"why resultsNames() is applied on dds and only the first results() that you used is considered"

I don't understand the question exactly. I think you should maybe just avoid calling results() without specifying name or contrast, as this seems to be a point of confusion. Imagine that it's not possible to call results() without also adding the argument name or contrast.

ADD REPLY
0
Entering edit mode

Thanks a lot! I just wanted to use '"apeglm"' because it's the one that it was used in a course about DESEq, so I thought it was the best. But now I will use '"ashr"' for all.

ADD REPLY

Login before adding your answer.

Traffic: 450 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