4

I want to install the DESeq2 package so that I can step through it with the debugger.

The source code for this package is available through GitHub, but it's not clear to me how to install the package so that I can step through its R code in the debugger.

Is there a way to do this?


BTW, I tried the approaches proposed in this earlier thread, but I get nowhere:

> trace(DESeq2::plotPCA, browser, at=1)
> devnull <- DESeq2::plotPCA(rld, intgroup = "q", returnData = TRUE)
Tracing DESeq2::plotPCA(rld, intgroup = "q", returnData = TRUE) step 1 
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: `{`
Browse[2]> n
debug: standardGeneric("plotPCA")
Browse[2]> n
> 

(I.e., after the last n above, I'm back at the top-level prompt.)

If I type DESeq2::plotPCA at the top-level prompt, all I get is

> DESeq2::plotPCA
nonstandardGenericFunction for "plotPCA" defined from package "BiocGenerics"

function (object, ...) 
{
    standardGeneric("plotPCA")
}
<environment: 0x26bee20>
Methods may be defined for arguments: object
Use  showMethods("plotPCA")  for currently available ones.

I also tried just sourcing the source file where DESeq2::plotPCA is defined, but this fails with

Error in setMethod("plotDispEsts", signature(object = "DESeqDataSet"),  : 
  no existing definition for function ‘plotDispEsts’

So clearly one needs to do some setup before sourcing this file. This realization is what led to this post.

Community
  • 1
  • 1
kjo
  • 33,683
  • 52
  • 148
  • 265
  • 1
    You might also benefit from using the `debugonce` function – MichaelChirico Jan 17 '17 at 00:08
  • I've come across this issue also. I've tried building it from source using this blog : https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/ . Even using `library("devtools")` and then `install("DESeq2", keep_source=TRUE)`, I was unable to figure out how to get a useful installation for debugging. – irritable_phd_syndrome Feb 22 '18 at 19:16

2 Answers2

1

Use debug() with the signature= argument, e.g.,

> showMethods("plotPCA")
Function: plotPCA (package BiocGenerics)
object="DESeqTransform"

> debug(plotPCA, signature="DESeqTransform")
Tracing specified method for function "plotPCA" in environment
<namespace:BiocGenerics>

No need for a special installation, just BiocInstaller::biocLite("DESeq2").

Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
1

Martin Morgan's answer captures the essence of the solution, but there are several gotchya's that are very confusing for new R users. These gotchya's stem from the unique form of object orientation that R uses, which is incredibly confusing for those who are coming from a C/C++ or Python background.

Following the DESeq2 vignette with my own data set:

$ cat synth.dat
sample    g0     g1     g2     g3     g4     g5     g6     g7     g8     g9    
samp0    132    192    19     133    247    297    110    104    93     103    
samp1    173    152    23     139    245    307    83     77     76     123    
samp2    179    129    18     130    208    244    89     138    71     142        
samp3    178    145    22     157    323    277    79     93     102    97    
samp4    250    208    8      101    202    257    142    140    76     113    
samp5    221    157    12     79     261    341    140    94     56     123    
samp6    139    220    15     125    282    261    124    154    117    118    
samp7    213    121    16     115    377    322    117    154    57     81    
samp8    234    152    11     103    281    321    76     160    71     139        
samp9    254    120    13     134    323    207    122    122    82     91    
samp10   159    207    17     143    385    217    126    113    106    89     
samp11   214    136    14     90     364    365    149    102    93     111    
samp12   180    159    15     136    226    309    72     111    69     113    
samp13   151    137    17     122    229    297    131    108    112    70    
samp14   254    151    8      118    254    222    138    114    66     89    
samp15   275    121    13     105    238    408    122    156    57     72    
samp16   204    134    8      111    352    332    89     134    73     90         
samp17   265    144    11     144    211    281    134    98     71     114        
samp18   212    111    14     138    321    391    84     112    88     96    
samp19   155    164    12     119    174    380    129    106    66     86    

$ cat synth_design_matrix.txt
samp0  group0
samp1  group0
samp2  group0
samp3  group0
samp4  group0
samp5  group0
samp6  group0
samp7  group0
samp8  group0
samp9  group0
samp10  group1
samp11  group1
samp12  group1
samp13  group1
samp14  group1
samp15  group1
samp16  group1
samp17  group1
samp18  group1
samp19  group1

> library("DESeq2")
> dat <- read.table(file="synth.dat", header=TRUE, stringsAsFactors=FALSE, row.names=1)
> groups <- read.table(file="synth_design_matrix.txt", header=FALSE, stringsAsFactors=TRUE, row.names=1)
> colnames(groups) <- c("condition")
> datM <- t(as.matrix(dat))
> dds  <- DESeqDataSetFromMatrix(countData = datM, colData = groups, design = ~condition)
> dds$condition <-relevel(dds$condition, ref="group0")
> vsd <- vst(dds, blind=FALSE, nsub=10)
-- note: fitType='parametric', but the dispersion trend was not well captured by the
   function: y = a/x + b, and a local regression fit was automatically substituted.
   specify fitType='local' or 'mean' to avoid this message next time.

Now specify the trace point and step through.

> trace(what="plotPCA", tracer=browser, at=1, signature=c("DESeqTransform"))
[1] "plotPCA"
> plotPCA(vsd)
Tracing function ".local" in package "DESeq2"
Tracing .local(object, ...) step 1 
Called from: eval(expr, p)
Browse[1]> n
debug: `{`
Browse[2]> n
debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#184: rv <- rowVars(assay(object))
Browse[2]> n
debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#187: select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, length(rv)))]
Browse[2]> n
debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#190: pca <- prcomp(t(assay(object)[select, ]))
Browse[2]> c

Now here are the gotchya's:

  1. You can only directly set trace points on functions/classes that are exported in the package. They will contain a #' @export. See Hillary Parker's blog for concise details. In this example, we end up stepping through the plotPCA method and into the plotPCA.DESeqTransform function which is not visible in the DESeq2 namespace.

  2. If there are more than one argument for the method's signature, you need to specify it with R's c().

E.g. if the method prototype is:

setMethod("spin", signature(object="star", value="numeric"), function(object, value){some stuff here})

The tracepoint would be

trace(what="spin", tracer=browser, at=1, signature=c(object="star", value="numeric"))
  1. Beware of replace methods. If you don't understand them, it could be very confusing when debugging a package like DESeq2 (which has several). See here and here for more details.

  2. Familiarize yourself with S4 and S3 methods and R's object orientation. This will make it easier to understand what is happening within the package.

With these tools, you should be able to debug any R package downloaded CRAN or Bioconductor without any special installation instructions.

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60