Hi
I'm missing something here but I cannot figure out what. What I can see is
that the same code works when I load it via source(...) yet fails when I
execute it after loading the package I have built (which includes the code.
Below is a transcript of my R session. First I load the code from a file,
using source(). Then I execute it fine. Then I remove the function object, I
load the package, and execute the same code from the package - and I get an
error.
Please help!
Mick
library(Rsamtools)
source("viRome/R/read.bam.R")
read.bam
function(bamfile=NULL, chr=NULL, start=1, end=1e07, what=c("qname", "flag",
"rname", "strand", "pos", "qwidth", "mapq", "cigar", "mrnm", "mpos",
"isize", "seq"), tag=c("NM"), removeN=TRUE) {
which <- RangesList(chr = IRanges(start,end))
names(which) <- chr
param <- ScanBamParam(which = which, what = what, tag=tag)
bam <- scanBam(bamfile, param=param)
lst <- lapply(names(bam[[1]]), function(elt) {do.call(c, unname(lapply
(bam, "[[", elt)))})
names(lst) <- names(bam[[1]])
# convert to data.frame
df <- do.call("DataFrame", lst)
df <- df[!is.na(df$qwidth),]
df$seq <- as.character(df$seq)
df$cigar <- as.character(df$cigar)
df$rname <- rep(chr, nrow(df))
if (removeN==TRUE) {
ns <- grep("N", df$seq)
if (length(ns) > 0) {
df <- df[-ns,]
}
}
return(as.data.frame(df))
}
bam <- read.bam("../../example/SRR389185_vs_SINV_sorted.bam", chr="SINV")
# works! remove the function
rm(read.bam)
# load the package
library(viRome)
Loading required package: seqinr
Attaching package: âseqinrâ
The following object(s) are masked from âpackage:Biostringsâ:
translate
Loading required package: plyr
Attaching package: âplyrâ
The following object(s) are masked from âpackage:seqinrâ:
count
The following object(s) are masked from âpackage:IRangesâ:
compact, desc, rename
Loading required package: gsubfn
Loading required package: proto
Loading required namespace: tcltk
Loading Tcl/Tk interface ... done
Loading required package: seqLogo
Loading required package: grid
read.bam
function (bamfile = NULL, chr = NULL, start = 1, end = 1e+07,
what = c("qname", "flag", "rname", "strand", "pos", "qwidth",
"mapq", "cigar", "mrnm", "mpos", "isize", "seq"), tag = c("NM"),
removeN = TRUE)
{
which <- RangesList(chr = IRanges(start, end))
names(which) <- chr
param <- ScanBamParam(which = which, what = what, tag = tag)
bam <- scanBam(bamfile, param = param)
lst <- lapply(names(bam[[1]]), function(elt) {
do.call(c, unname(lapply(bam, "[[", elt)))
})
names(lst) <- names(bam[[1]])
df <- do.call("DataFrame", lst)
df <- df[!is.na(df$qwidth), ]
df$seq <- as.character(df$seq)
df$cigar <- as.character(df$cigar)
df$rname <- rep(chr, nrow(df))
if (removeN == TRUE) {
ns <- grep("N", df$seq)
if (length(ns) > 0) {
df <- df[-ns, ]
}
}
return(as.data.frame(df))
}
<environment: namespace:viRome>
# same code now produces an error
bam <- read.bam("../../example/SRR389185_vs_SINV_sorted.bam", chr="SINV")
Error in as.data.frame.default(df) :
cannot coerce class 'structure("DataFrame", package = "IRanges")' into a