Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • elizabeth000
    Member
    • Apr 2015
    • 21

    Working with DGEExact objects in a loop

    Objects of class DGEExact are the result of the exactTest() function in edgeR. I'm having trouble making loops to assign DGEExact objects to a variable. I'm probably making some very basic mistake in handling R data types...?

    > nlevels(data$samples$group)
    [1] 8

    Code:
    for(i in 1:nlevels(data$samples$group)) {
       index = 1
       while(index < nlevels(data$samples$group)) {
          extest[i] = exactTest(data, pair=c(i,1+index),)
          index = index + 1
       }
    }
    Error in extest[i] = exactTest(data, pair = c(i, 1 + index), ) :
    object 'extest' not found

    Sorry for the basic question, I hope it is easy to answer!
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    You need to create "extext" beforehand, that's why it's complaining that it can't be found. Just add this above your while loop:
    Code:
    extest = c()
    Of course, you'll have to deal with extest after the while loop or it'll get over-written.

    Comment

    • elizabeth000
      Member
      • Apr 2015
      • 21

      #3
      Thank you for your fast reply. Yes, this was completely wrong, and I fixed a few problems. Here is the complete loop:

      Code:
      extest = c()
      k = 1
      for(i in 1:nlevels(data$samples$group)) {
         print(c(i,"i"))
         for(j in (i+1):nlevels(data$samples$group)) {
            if (i < nlevels(data$samples$group)) {
               extest[k] = exactTest(data, pair=c(i,j))
               k = k+1
            }
         }
      }
      This is improved because it tests each pair only once, and I prevented extest from being overwritten. But... extest[k] is not structured as expected... for example, I cannot view head(extest[1]), the whole list prints to screen. And:

      > extest[1]$comparison
      NULL

      How can this strange behavior be explained? Is there any way to store DGEExact objects in a list or other R class?

      Comment

      • elizabeth000
        Member
        • Apr 2015
        • 21

        #4
        I find that extest[1] is a list:

        > class(extest[1])
        [1] "list"

        whereas it should be:

        > class(extest)
        [1] "DGEExact"
        attr(,"package")
        [1] "edgeR"

        So I need to find out how to store the DGEExact objects somewhere while creating them in the loop. There must be a way to do this????

        Comment

        • dpryan
          Devon Ryan
          • Jul 2011
          • 3478

          #5
          You might have to change extest to a list itself, since I'm not sure how/if coercion works in this scenario.

          Comment

          • elizabeth000
            Member
            • Apr 2015
            • 21

            #6
            By declaring extest as a list:

            Code:
            extest = list()
            k = 1
            for(i in 1:nlevels(data$samples$group)) {
               #print(c(i,"i"))
               for(j in (i+1):nlevels(data$samples$group)) {
                  if (i < nlevels(data$samples$group)) {
                     extest[k] = exactTest(data, pair=c(i,j))
                     #print(et$table)
                     k = k+1
                  }
               }
            }
            I get this warning for each execution of exactTest:
            28: In extest[k] = exactTest(data, pair = c(i, j)) :
            number of items to replace is not a multiple of replacement length

            > class(extest)
            [1] "list"
            > class(extest[1])
            [1] "list"

            Comment

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

              #7
              Not sure then, I'd have to play around with it. You might just play around with making a list of two instance by hand.

              Comment

              • elizabeth000
                Member
                • Apr 2015
                • 21

                #8
                > first = exactTest(data,pair=c(1,2))
                > second = exactTest(data,pair=c(2,3))
                > results = list(first,second)
                > results
                [[1]]
                An object of class "DGEExact"
                $table
                logFC logCPM PValue
                CGI_10000009 -2.3851234 1.493044 0.1171040419
                CGI_10000013 0.1844446 4.147738 0.7403461115
                CGI_10000014 -4.9376158 2.707417 0.0001137864
                CGI_10000015 0.4967844 3.773692 0.4339343285
                CGI_10000028 -0.2890212 2.476181 0.9883519753
                18055 more rows ...

                $comparison
                [1] "28cell" "blastula"

                $genes
                NULL


                [[2]]
                An object of class "DGEExact"
                $table
                logFC logCPM PValue
                CGI_10000009 -2.57188463 1.493044 0.8000000
                CGI_10000013 0.01444976 4.147738 1.0000000
                CGI_10000014 0.41462168 2.707417 0.9686494
                CGI_10000015 -0.36575812 3.773692 0.6137249
                CGI_10000028 0.64099475 2.476181 0.7954756
                18055 more rows ...

                $comparison
                [1] "blastula" "dlarvae"

                $genes
                NULL

                That seems to work... so why doesn't it work in my loop...

                Comment

                • dpryan
                  Devon Ryan
                  • Jul 2011
                  • 3478

                  #9
                  Try making the empty list first and then adding to it, since that will match all the coercion processes going on in in your real code.

                  Comment

                  • elizabeth000
                    Member
                    • Apr 2015
                    • 21

                    #10
                    Yes, that's what I was thinking. Instead of filling indices with list objects, I should append the new result to the list. Hmmmm but how do I do that in R?
                    I'm drawing a blank...

                    Comment

                    • elizabeth000
                      Member
                      • Apr 2015
                      • 21

                      #11
                      For the moment this works, but is really not ideal.
                      Code:
                      for(i in 1:nlevels(data$samples$group)) {
                         for(j in (i+1):nlevels(data$samples$group)) {
                            if (i < nlevels(data$samples$group)) {
                               et = exactTest(data, pair=c(i,j))
                               filen = paste("/media/liz/ACERDATA/Elizabeth_DATA/exacttest_dev_cds", i, j, ".txt", sep="")
                               #Open output file
                               sink(filen)
                               print(et$comparison)
                               print(topTags(et, n=100))
                               #Close output file
                               sink()
                            }
                         }
                      }

                      Comment

                      • elizabeth000
                        Member
                        • Apr 2015
                        • 21

                        #12
                        Actually the solution was super easy! You have to use double [[ to index a list! This works:

                        Code:
                        extest = list()
                        k = 1
                        for(i in 1:nlevels(data$samples$group)) {
                           for(j in (i+1):nlevels(data$samples$group)) {
                              if (i < nlevels(data$samples$group)) {
                                 extest[[k]] = exactTest(data, pair=c(i,j))
                                 k = k+1
                              }
                           }
                        }

                        Comment

                        Latest Articles

                        Collapse

                        • 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...
                          Yesterday, 05:17 AM
                        • GATTACAT
                          Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                          by GATTACAT
                          Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                          07-01-2026, 11:43 AM
                        • SEQadmin2
                          Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                          by SEQadmin2


                          I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                          Here are nine questions we think about, in roughly the order they matter, before...
                          06-18-2026, 07:11 AM

                        ad_right_rmr

                        Collapse

                        News

                        Collapse

                        Topics Statistics Last Post
                        Started by SEQadmin2, Yesterday, 10:08 AM
                        0 responses
                        6 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, 07-07-2026, 11:05 AM
                        0 responses
                        8 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, 07-02-2026, 11:08 AM
                        0 responses
                        31 views
                        0 reactions
                        Last Post SEQadmin2  
                        Started by SEQadmin2, 06-30-2026, 05:37 AM
                        0 responses
                        29 views
                        0 reactions
                        Last Post SEQadmin2  
                        Working...