Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #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


    • #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


      • #4
        Hi Nicki and Benedikt,

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

        Thanks,
        Rozita

        Comment


        • #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


          • #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


            • #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


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

                Comment


                • #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


                  • #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


                    • #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


                      • #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

                        • seqadmin
                          Recent Advances in Sequencing Analysis Tools
                          by seqadmin


                          The sequencing world is rapidly changing due to declining costs, enhanced accuracies, and the advent of newer, cutting-edge instruments. Equally important to these developments are improvements in sequencing analysis, a process that converts vast amounts of raw data into a comprehensible and meaningful form. This complex task requires expertise and the right analysis tools. In this article, we highlight the progress and innovation in sequencing analysis by reviewing several of the...
                          05-06-2024, 07:48 AM
                        • seqadmin
                          Essential Discoveries and Tools in Epitranscriptomics
                          by seqadmin




                          The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
                          04-22-2024, 07:01 AM

                        ad_right_rmr

                        Collapse

                        News

                        Collapse

                        Topics Statistics Last Post
                        Started by seqadmin, Yesterday, 06:57 AM
                        0 responses
                        11 views
                        0 likes
                        Last Post seqadmin  
                        Started by seqadmin, 05-06-2024, 07:17 AM
                        0 responses
                        16 views
                        0 likes
                        Last Post seqadmin  
                        Started by seqadmin, 05-02-2024, 08:06 AM
                        0 responses
                        19 views
                        0 likes
                        Last Post seqadmin  
                        Started by seqadmin, 04-30-2024, 12:17 PM
                        0 responses
                        24 views
                        0 likes
                        Last Post seqadmin  
                        Working...
                        X