Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • skoddo
    Junior Member
    • Jul 2012
    • 3

    DESeq plotPCA changes

    Hi everyone,
    I have a couple of questions concerning the plotPCA function in the DESeq package. I'm relatively new to R, so maybe they are relatively easy to answer.

    1. Is it possible to extract the calculated coordinates from the PCA? I'm talking about the x-axis and y-axis values for the different samples.

    2. How can I change the colors in the PCA? I can see from the function code

    Code:
    function (x, intgroup = "condition", ntop = 500) 
    {
        rv = rowVars(exprs(x))
        select = order(rv, decreasing = TRUE)[seq_len(ntop)]
        pca = prcomp(t(exprs(x)[select, ]))
        fac = factor(apply(pData(x)[, intgroup, drop = FALSE], 1, 
            paste, collapse = " : "))
        if (length(fac) >= 3) 
            colours = brewer.pal(nlevels(fac), "Paired")
        else colours = c("green", "blue")
        xyplot(PC2 ~ PC1, groups = fac, data = as.data.frame(pca$x), 
            pch = 16, cex = 2, aspect = "iso", col = colours, main = draw.key(key = list(rect = list(col = colours), 
                text = list(levels(fac)), rep = FALSE)))
    }
    <environment: namespace:DESeq>
    that it uses the "Paired" color palette from the RColorBrewer package. When I try to replicate the function with a different palette, I get an error that it can't find the "rowVars" function. I cannot find this function in R or in the DESeq manual. The default plotPCA is working fine though. I tried attaching my new function to DESeq namespace but I couldn't. I attached it to the package environment but it still didn't work. It seems to me that my problem is a fairly stupid one but even with the excellent manual and some intensive internet recherches I can't figure it out.

    3. Is it possible to change the default colored circles to different shapes for different groups?

    Thanks in advance for any help you can offer.
    Kind Regards
    Benedikt
  • Nicki1984
    Junior Member
    • May 2013
    • 1

    #2
    Same problem hacking plotPCA in DESeq

    Hi there,

    I have just encountered exactly the same problem with the rowVars function when trying to figure out how to extract the calculated co-ordinates. I was wondering if you ever received an answer to your question of managed to solve the problem yourself?

    I would appreciate any help anyone can give!

    Many thanks,
    Nicki

    Comment

    • skoddo
      Junior Member
      • Jul 2012
      • 3

      #3
      Hi Nicky,
      unfortunately I could never solve this issue although in hindsight Seqanswers wasn't the right platform to ask for this specific problem anyway.

      If you are just interested in your PCA coordinates, you can use a number of alternative packages to get them.

      Just take your normalized/ transformed data from DeSeq and load them into pcaMethods. There you can easily calculate any number of PCAs and get your desired coordinates for the loadings and the scores.

      Afterwards its just a matter of basic scatter plotting in R if you want to have different colours/shapes etc.

      Hope that helps.

      Comment

      • rozitaa
        Member
        • Jun 2013
        • 51

        #4
        Hi Nicki and Benedikt,

        I have the same problem. Have you got any answer for it?

        Thanks,
        Rozita

        Comment

        • dpryan
          Devon Ryan
          • Jul 2011
          • 3478

          #5
          It's from the genefilter package, so just genefilter::rowVars() to use it. You could also just directly library(genefilter) and then not have to deal with the extra typing (the package is only loaded via a namespace, not attached).

          Comment

          • rozitaa
            Member
            • Jun 2013
            • 51

            #6
            Originally posted by dpryan View Post
            It's from the genefilter package, so just genefilter::rowVars() to use it. You could also just directly library(genefilter) and then not have to deal with the extra typing (the package is only loaded via a namespace, not attached).
            Hej dpryan,

            I didn't understand! This is DeSeq package we are talking about then how I can include gene filter package? There is no rowVar() even there! Can you please explain a bit more.

            Thanks

            Comment

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

              #7
              Yes, I know that you're talking about DESeq. DESeq doesn't provide that function, it uses genefilter. If you "library(genefilter)", you'll find that the rowVars() (not rowVar()) function is then useable.

              Comment

              • rozitaa
                Member
                • Jun 2013
                • 51

                #8
                Thanks! Do you also know if it is possible to change PCA plot colors and shapes in Deseq?

                Comment

                • dpryan
                  Devon Ryan
                  • Jul 2011
                  • 3478

                  #9
                  You just need to modify the plotPCA function. Colors are assigned by the "col=colours" parameter in the xyplot function and shapes are the "pch=16". You can provide a vector of shapes if you want and then different groups can have different shapes.

                  Comment

                  • bpb9
                    Member
                    • Aug 2012
                    • 24

                    #10
                    I am able to change the colors by modifying the plotPCA function (check out ?brewer.pal to see the options for colors--there are several color schemes if you don't like "paired"). Or you can just remove these lines of the plotPCA code altogether:
                    if (length(fac) >= 3)
                    colours = brewer.pal(nlevels(fac), "Paired")
                    else colours = c("green", "blue")

                    and instead do any of the R colors you like:
                    colours = c("red","blue","green","darkgoldenrod","hotpink") etc

                    The second part of the question--pulling the actual values for PC1 and PC2 that are plotted--I haven't quite figured out yet. If I enter parts of the plotPCA function line by line, I am able to print all principal components to the screen, but I cannot save the object (or any subset of columns from the object) as a table, even after trying to data.frame(pca).

                    Comment

                    • dpryan
                      Devon Ryan
                      • Jul 2011
                      • 3478

                      #11
                      I'm not by a computer with R at the moment, but I recall that it uses prcomp to compute the principal components. I recall that that returns a list, so converting it to a data.frame probably won't work well (what would reasonable dimensions be when the list elements include matrices?). Just read the help page for prcomp and you should be able to tell which list element has the values you're after.

                      Comment

                      • bpb9
                        Member
                        • Aug 2012
                        • 24

                        #12
                        Pull Principal Components from DESeq plotPCA

                        #Load libraries
                        library(gplots)
                        library(RColorBrewer)
                        library(lattice)
                        library(genefilter)

                        #Define the plotPCA function
                        plotPCA4<-function (x, intgroup = "condition", ntop = 500)
                        {
                        rv = rowVars(exprs(x))
                        select = order(rv, decreasing = TRUE)[seq_len(ntop)]
                        pca = prcomp(t(exprs(x)[select, ]))
                        fac = factor(apply(pData(x)[, intgroup, drop = FALSE], 1,
                        paste, collapse = " : "))
                        if (length(fac) >= 3)
                        #colours = brewer.pal(nlevels(fac), "YlOrRd")
                        colours = brewer.pal(nlevels(fac), "Paired")
                        else colours = c("darkred", "darkblue")
                        xyplot(PC2 ~ PC1, groups = fac, data = as.data.frame(pca$x),
                        pch = 16, cex = 2, aspect = "iso", col = colours, main = draw.key(key = list(rect = list(col = colours),
                        text = list(levels(fac)), rep = FALSE)))
                        }

                        #To make a pca object using vsd transformed data, for example:
                        pca = prcomp(t(exprs(vsd)[select, ]))

                        #To view the actual values of each principal component in each individual:
                        pca$x[,1:10]

                        #This let me pull, for each of my samples, the first ten principal components.

                        Comment

                        Latest Articles

                        Collapse

                        • SEQadmin2
                          From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                          by SEQadmin2


                          Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                          The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                          ...
                          Yesterday, 10:05 AM
                        • SEQadmin2
                          Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
                          by SEQadmin2


                          With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


                          Introduction

                          Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
                          05-22-2026, 06:42 AM
                        • SEQadmin2
                          Environmental Genomics in the Age of NGS: From Microbes to Conservation Strategies
                          by SEQadmin2

                          Studying ecosystems means dealing with complex, multi-species communities that are hard to observe at scale. This complexity, however, hides many important questions to be answered, from how biogeochemical cycles work and how climate change can affect species distribution to how conservation strategies can work best.


                          Genomics, particularly since the expansion of NGS, has transformed ecosystem ecology. By sequencing environmental DNA, we can now assess biodiversity without direct...
                          05-06-2026, 09:04 AM

                        ad_right_rmr

                        Collapse

                        News

                        Collapse

                        Topics Statistics Last Post
                        Started by SEQadmin2, Yesterday, 12:03 PM
                        0 responses
                        19 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, Yesterday, 11:40 AM
                        0 responses
                        14 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, 05-28-2026, 11:40 AM
                        0 responses
                        29 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, 05-26-2026, 10:12 AM
                        0 responses
                        31 views
                        0 reactions
                        Last Post SEQadmin2  
                        Working...