How can I combine results tables into a single output?
1
1
Entering edit mode
minardsmitha ▴ 10
@minardsmitha-24162
Last seen 17 months ago
United States

I have run DESeq2 on my data. Single tissue with multiple dosages. I now want R to combine the results tables (dosages vs no dose) into a single table which I will import into IPA for further analysis. I have been trying several ways to make R do this for me but have not found the correct method yet.
Example table from one dosage:

    baseMean    log2FoldChange  lfcSE   stat    pvalue  padj
Apoa1   7966.051594 -0.705606796    0.235025877 -3.002251515    0.002679907 0.136722046
Tf  22072.7993  -0.179756822    0.189490556 -0.94863209 0.342807764 0.794017269
Tmsb4x  157.3827118 0.067799129 0.225978449 0.300024755 0.764158273 0.951962885
Alb 25950.79641 -0.328876922    0.186021559 -1.767950574    0.077069167 0.519223404
AABR07035374.1  2497.943534 -5.31783853 0.66793913  -7.9615616  1.70E-15    1.53E-12

I have results like the above for all dosage levels in my experiment (total six dosages).

In the end I want a single table that combines the results to look like this:

    Dose75.TissueLiver  Dose75.TissueLiver  Dose75.TissueLiver  Dose75.TissueLiver  Dose75.TissueLiver  Dose75.TissueLiver  Dose100.TissueLiver Dose100.TissueLiver Dose100.TissueLiver Dose100.TissueLiver Dose100.TissueLiver Dose100.TissueLiver
    baseMean    log2FoldChange  lfcSE   stat    pvalue  padj    baseMean    log2FoldChange  lfcSE   stat    pvalue  padj
Apoa1   5151.782821 -2.95773217 0.79691926  -3.711457757    0.000206069 0.044300813 5151.782821 -0.119272921    0.837429931 -0.142427344    0.886742465 0.96499833
Tf  14364.18444 1.462082327 0.55760012  2.6220983   0.008739022 0.224529946 14364.18444 -0.282310471    0.523104751 -0.539682483    0.589416023 0.8511549
Tmsb4x  125.862414  -0.341481221    0.442174871 -0.772276408    0.439950736 0.791900368 125.862414  -0.577306938    0.443929995 -1.300445891    0.19344819  0.580082285
Alb 16843.23554 -0.096265021    0.840515091 -0.114530985    0.908816886 0.974329685 16843.23554 -0.828972307    0.798389329 -1.038305846    0.299127688 0.681157936
AABR07035374.1  1572.869669 -4.517914215    1.085794081 -4.160930965    3.17E-05    0.013449407 1572.869669 2.624268633 1.203498005 2.180534262 0.029217882 0.282455014

I have been working with this code but it isn't giving the output I desire.

comparisons <- resultsNames(dds2)

for (comp in comparisons){
    res <- results(dds2, name = comp)
    datalist <- res
}

alltables <- do.call(cbind, datalist)

Which produces a table that looks like this:

head(alltables)
      baseMean log2FoldChange     lfcSE       stat     pvalue      padj
[1,]  7966.0516    -0.07502324 0.2350207 -0.3192197 0.74955990 0.8980526
[2,] 22072.7993     0.15710019 0.1894960  0.8290420 0.40708062 0.7006270
[3,]   157.3827    -0.41405487 0.2297919 -1.8018690 0.07156602 0.3004964
[4,] 25950.7964    -0.24037295 0.1860329 -1.2920993 0.19632274 0.4992489
[5,]  2497.9435     0.17106298 0.6656823  0.2569739 0.79719892 0.9194901
[6,] 12854.2955     0.40788557 0.2155525  1.8922794 0.05845376 0.2700465

Is it possible to basically append all comparisons by column to a single table? I also recognize that I will need to figure out how to add the header to my output as well.

deseq2 • 1.2k views
ADD COMMENT
0
Entering edit mode

For starters, avoid cbind. Someday, you'll try to merge datasets that don't naturally line up perfectly, and you will get a mess. Result tables have gene names as rownames. Merge on rownames (the one thing about merging is that it turns your rownames into an ordinary column called row.names, so you might have to clean that up)

ADD REPLY
0
Entering edit mode

@swbarnes Thank you for the explanation of the difference in combining using cbind and merge. This is important to know.

ADD REPLY
2
Entering edit mode
@james-w-macdonald-5106
Last seen 3 days ago
United States

Something like this?

z <- lapply(comparisons, function(x) {
       tmp <- results(dds, name = x))
       names(tmp) <- paste(x, names(tmp), sep = ".")
       return(tmp)})

alltables <- do.call(cbind, z)

ADD COMMENT
0
Entering edit mode

@James W. MacDonald There seem to possibly be some bracket mismatches in your code above. Or I am not fully understanding what you are suggesting. Can you elaborate on your example?

ADD REPLY
0
Entering edit mode

Yes. Remove the extraneous closing parenthesis on the second line.

The lapply function creates a list of DataFrame objects that you subsequently cbind. And as @swbarnes2 points out, a bare cbind assumes that you are doing something reasonable. And since results just puts out the results from the model fit, in the order of the genes, it's fine. But in general you would use more defensive programming, where you ensure that the rows match up.

ADD REPLY

Login before adding your answer.

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