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:
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.
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"))
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.
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.