Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Chequer
    Junior Member
    • Mar 2016
    • 4

    DESeq - Changing PCA Axis to PC3 and PC4.

    Hi all, I'm new to R and I'm currently going through the DESeq introduction guide by Anders and Huber entitled 'Di fferential expression of RNA-Seq data at the gene level.'

    Simply, I have created a PCA plot using:
    print(plotPCA(vsdFull, intgroup=c("condition", "libType")))

    My question is how do I produce a graph showing other eigenvectors like PC3 and PC4, ideally using DESeq and gplots.

    Thanks in advance.
  • fanli
    Senior Member
    • Jul 2014
    • 197

    #2
    You can modify/rewrite the existing plotPCA function. Bolded sections are modified to include PC3 and PC4:

    Code:
    > DESeq2:::plotPCA.DESeqTransform
    function (object, intgroup = "condition", ntop = 500, returnData = FALSE) 
    {
        rv <- rowVars(assay(object))
        select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, 
            length(rv)))]
        pca <- prcomp(t(assay(object)[select, ]))
        percentVar <- pca$sdev^2/sum(pca$sdev^2)
        if (!all(intgroup %in% names(colData(object)))) {
            stop("the argument 'intgroup' should specify columns of colData(dds)")
        }
        intgroup.df <- as.data.frame(colData(object)[, intgroup, 
            drop = FALSE])
        group <- if (length(intgroup) > 1) {
            factor(apply(intgroup.df, 1, paste, collapse = " : "))
        }
        else {
            colData(object)[[intgroup]]
        }
        d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], [B]PC3 = pca$x[, 3], PC4 = pca$x[, 4],[/B] group = group, 
            intgroup.df, name = colnames(object))
        if (returnData) {
            attr(d, "percentVar") <- percentVar[1:2]
            return(d)
        }
        ggplot(data = d, aes_string(x = "[B]PC3[/B]", y = "[B]PC4[/B]", color = "group")) + 
            geom_point(size = 3) + xlab(paste0("[B]PC3[/B]: ", round(percentVar[[B]3[/B]] * 
            100), "% variance")) + ylab(paste0("[B]PC4[/B]: ", round(percentVar[[B]4[/B]] * 
            100), "% variance")) + coord_fixed()
    }

    Comment

    • Chequer
      Junior Member
      • Mar 2016
      • 4

      #3
      Originally posted by fanli View Post
      You can modify/rewrite the existing plotPCA function. Bolded sections are modified to include PC3 and PC4:

      Code:
      > DESeq2:::plotPCA.DESeqTransform
      function (object, intgroup = "condition", ntop = 500, returnData = FALSE) 
      {
          rv <- rowVars(assay(object))
          select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, 
              length(rv)))]
          pca <- prcomp(t(assay(object)[select, ]))
          percentVar <- pca$sdev^2/sum(pca$sdev^2)
          if (!all(intgroup %in% names(colData(object)))) {
              stop("the argument 'intgroup' should specify columns of colData(dds)")
          }
          intgroup.df <- as.data.frame(colData(object)[, intgroup, 
              drop = FALSE])
          group <- if (length(intgroup) > 1) {
              factor(apply(intgroup.df, 1, paste, collapse = " : "))
          }
          else {
              colData(object)[[intgroup]]
          }
          d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], [B]PC3 = pca$x[, 3], PC4 = pca$x[, 4],[/B] group = group, 
              intgroup.df, name = colnames(object))
          if (returnData) {
              attr(d, "percentVar") <- percentVar[1:2]
              return(d)
          }
          ggplot(data = d, aes_string(x = "[B]PC3[/B]", y = "[B]PC4[/B]", color = "group")) + 
              geom_point(size = 3) + xlab(paste0("[B]PC3[/B]: ", round(percentVar[[B]3[/B]] * 
              100), "% variance")) + ylab(paste0("[B]PC4[/B]: ", round(percentVar[[B]4[/B]] * 
              100), "% variance")) + coord_fixed()
      }
      Unfortunately on using that code, I get this error:

      Code:
      > print(plotPCA(vsdFull, intgroup=c("condition", "libType")))
      Error in (function (classes, fdef, mtable)  : 
        unable to find an inherited method for function ‘plotPCA’ for signature ‘"ExpressionSet"’
      I understand that I need to use an argument but simply what code do I need to make it work. I do apologise for being so new to R.

      Comment

      • fanli
        Senior Member
        • Jul 2014
        • 197

        #4
        Hmm, I should have been more specific. I think if you copy and paste the following code into your R session it should work:

        Code:
        plotPCA <- function (object, intgroup = "condition", ntop = 500, returnData = FALSE) 
        {
            rv <- rowVars(assay(object))
            select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, 
                length(rv)))]
            pca <- prcomp(t(assay(object)[select, ]))
            percentVar <- pca$sdev^2/sum(pca$sdev^2)
            if (!all(intgroup %in% names(colData(object)))) {
                stop("the argument 'intgroup' should specify columns of colData(dds)")
            }
            intgroup.df <- as.data.frame(colData(object)[, intgroup, 
                drop = FALSE])
            group <- if (length(intgroup) > 1) {
                factor(apply(intgroup.df, 1, paste, collapse = " : "))
            }
            else {
                colData(object)[[intgroup]]
            }
            d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], PC3 = pca$x[, 3], PC4 = pca$x[, 4], group = group, 
                intgroup.df, name = colnames(object))
            if (returnData) {
                attr(d, "percentVar") <- percentVar[1:2]
                return(d)
            }
            ggplot(data = d, aes_string(x = "PC3", y = "PC4", color = "group")) + 
                geom_point(size = 3) + xlab(paste0("PC3: ", round(percentVar[1] * 
                100), "% variance")) + ylab(paste0("PC4: ", round(percentVar[2] * 
                100), "% variance")) + coord_fixed()
        }
        
        print(plotPCA(vsdFull, intgroup=c("condition", "libType")))
        By the way, just to verify, are you using DESeq2 or DESeq?

        Comment

        • Chequer
          Junior Member
          • Mar 2016
          • 4

          #5
          Originally posted by fanli View Post
          Hmm, I should have been more specific. I think if you copy and paste the following code into your R session it should work:

          Code:
          plotPCA <- function (object, intgroup = "condition", ntop = 500, returnData = FALSE) 
          {
              rv <- rowVars(assay(object))
              select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, 
                  length(rv)))]
              pca <- prcomp(t(assay(object)[select, ]))
              percentVar <- pca$sdev^2/sum(pca$sdev^2)
              if (!all(intgroup %in% names(colData(object)))) {
                  stop("the argument 'intgroup' should specify columns of colData(dds)")
              }
              intgroup.df <- as.data.frame(colData(object)[, intgroup, 
                  drop = FALSE])
              group <- if (length(intgroup) > 1) {
                  factor(apply(intgroup.df, 1, paste, collapse = " : "))
              }
              else {
                  colData(object)[[intgroup]]
              }
              d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], PC3 = pca$x[, 3], PC4 = pca$x[, 4], group = group, 
                  intgroup.df, name = colnames(object))
              if (returnData) {
                  attr(d, "percentVar") <- percentVar[1:2]
                  return(d)
              }
              ggplot(data = d, aes_string(x = "PC3", y = "PC4", color = "group")) + 
                  geom_point(size = 3) + xlab(paste0("PC3: ", round(percentVar[1] * 
                  100), "% variance")) + ylab(paste0("PC4: ", round(percentVar[2] * 
                  100), "% variance")) + coord_fixed()
          }
          
          print(plotPCA(vsdFull, intgroup=c("condition", "libType")))
          By the way, just to verify, are you using DESeq2 or DESeq?
          I'm using DESeq but I also have DESeq2 installed. Unfortunately, the same error is returned. I'm working through the DESeq guide and once I use this new code, my original PCA fails to work at all with the error. I massively appreciate your help. I just feel as if I need to learn the code a bit more to understand why its not working.

          Comment

          • fanli
            Senior Member
            • Jul 2014
            • 197

            #6
            Oh, sorry. I was basing everything on DESeq2. Try this:

            Code:
            plotPCA <- function (x, intgroup = "condition", ntop = 500, ax1="PC1", ax2="PC2") 
            {
                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 = " : "))
                colours = if (nlevels(fac) >= 3) 
                    brewer.pal(nlevels(fac), "Paired")
                else c("green", "blue")
                xyplot(as.formula(sprintf("%s ~ %s", ax2, ax1)), 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)))
            }
            
            plotPCA(vsdFull, intgroup=c("condition", "libType"), ax1="PC3", ax2="PC4")

            Comment

            • Chequer
              Junior Member
              • Mar 2016
              • 4

              #7
              Originally posted by fanli View Post
              Oh, sorry. I was basing everything on DESeq2. Try this:

              Code:
              plotPCA <- function (x, intgroup = "condition", ntop = 500, ax1="PC1", ax2="PC2") 
              {
                  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 = " : "))
                  colours = if (nlevels(fac) >= 3) 
                      brewer.pal(nlevels(fac), "Paired")
                  else c("green", "blue")
                  xyplot(as.formula(sprintf("%s ~ %s", ax2, ax1)), 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)))
              }
              
              plotPCA(vsdFull, intgroup=c("condition", "libType"), ax1="PC3", ax2="PC4")
              Worked perfectly! Thank you ever so much!

              Comment

              Latest Articles

              Collapse

              • mylaser
                Reply to Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                by mylaser
                Kheloyar – Everything You Need to Know About Kheloyaar Login and Kheoyar Id
                If you are looking for an online gaming platform that offers a user-friendly experience, Kheloyar has become a name that many users search for. Whether you're interested in creating a new account, accessing your dashboard through Kheloyaar Login, or learning how to obtain a Kheoyar Id, understanding the platform's features and account process is essential.
                This guide explains everything you need to know about...
                07-11-2026, 01:13 AM
              • SEQadmin2
                Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                by SEQadmin2



                Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                ...
                07-09-2026, 11:10 AM
              • SEQadmin2
                Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                by SEQadmin2



                Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                07-08-2026, 05:17 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 07-09-2026, 10:04 AM
              0 responses
              23 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-08-2026, 10:08 AM
              0 responses
              14 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-07-2026, 11:05 AM
              0 responses
              33 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-02-2026, 11:08 AM
              0 responses
              31 views
              0 reactions
              Last Post SEQadmin2  
              Working...