Any quick solution for fixing error : "$ operator is invalid for atomic vectors" ?
2
0
Entering edit mode
@jurat-shahidin-9488
Last seen 4.6 years ago
Chicago, IL, USA

Hi:

I have set of genomic interval in GRanges objects. However I intend to filter them conditionally, while export them as bed format files. I have implemented function that possibly accomplish this task, but I have an error. I  have used dplyr packages for facilitating output more efficiently. I simulated same data set as data.frame and it worked perfectly. How can I fix this issue? Any quick solution ?

mini example:

grs <- GRangesList(
  foo = GRanges( seqnames=Rle("chr1", 4),ranges=IRanges(c(3,33,54,91), c(26,42,71,107)),
                  rangeName=c("a1", "a2", "a3", "a4"), score=c(22, 6,13, 7)),
  bar = GRanges(seqnames=Rle("chr1", 6),ranges=IRanges(c(5,12,16,21,37,78), c(9,14,19,29,45,84)),
                 rangeName=c("b1", "b2", "b3", "b4", "b5", "b6"), score=c(3, 5, 3, 9, 4, 3)),
  cat = GRanges(seqnames=Rle("chr1", 7),ranges=IRanges(c(1,8,18,35,42,59,81), c(6,13,27,40,46,63,114)),
                 rangeName=c("c1", "c2", "c3", "c4","c5","c6","c7"), score= c(2.1, 3, 5.1, 3.5, 7, 2, 10))
)

helper function :

.cast.Pvl<- function(x, pvalueBase = 1L, ...) {
  if(is.null(x$pvalue)){
    x$p.value <- 10^(score(x)/(- pvalueBase))
    colnames(mcols(x))[3] <- "p.value"
  } else {
    x
  }
  return(x)
}

I intend to implement this sketch function :

mylist2 <- lapply(seq_along(grs), function(i) {
  if(is.null(i$p.value)) {
    i <- .cast.Pvl(i, 1L)
  } else {
    splitter <- function(i, tau.w) {
      require(dplyr)
      DF <- grs[[i]]
      DF <- as(DF, "data.frame")
      DF %>%
        filter(p.value >= tau.w) %>%
        write.csv(., sprintf("dropped.%s.csv", i), row.names = FALSE)
      total.ERs <- filter(DF, p.value <= tau.w)
      return(total.ERs)
    }
  }
  res <- splitter(i, 1.0E-04)
})

I have an error like this:

Error in i$p.value : $ operator is invalid for atomic vectors

can't figure out what is possible reason. Can anyone tell me what's going on? How can I fix this bug ? How can I facilitate the output of myList as I expected ? Thanks a lot

r granges dplyr error handling • 18k views
ADD COMMENT
2
Entering edit mode
Mike Smith ★ 6.6k
@mike-smith
Last seen 23 minutes ago
EMBL Heidelberg

I don't full understand what you're trying to achieve, but you're getting this error as a result of the seq_along() function.  If you run this in isolation you'll see it gives you an sequential integer vector of the same length as grs e.g.

> test <- seq_along(grs)
> test
[1] 1 2 3

Your code then applies the function to each element of that vector.  Since each element is a single integer it doesn't have a p.value element and indeed $ isn't valid on vectors e.g.

> test[1]$p.value
Error in test[1]$p.value : $ operator is invalid for atomic vectors

If you want to perform your function on each member of grs you can simply leave out the seq_along() part.  Something like

mylist2 <- lapply( grs, function(i) {
  if(is.null(i$p.value)) {
    i <- .cast.Pvl(i, 1L)
  }
})

 

ADD COMMENT
0
Entering edit mode

Thanks for your respond. The main purpose of I implement above sketch function, given list of genomic interval, first I need to filter them by its p.value, but I let my function only return genomic regions whose p.value less then given threshold, while I need also export dropped features as Bed files (just statistical showed which one dropped). Michael suggest do not use writing my own bed exported. What's the feasible approach ? Any suggestion to make more code more clear and clean ? Thank you

ADD REPLY
0
Entering edit mode

The rtracklayer package contains the function export() which can take a GRanges object and output a number of different formats including bed files.

ADD REPLY
1
Entering edit mode
@michael-lawrence-3846
Last seen 3.0 years ago
United States

Use the debugging features of R and investigate the type of i. It's probably not what you want. Also, I would recommend against writing your own BED exporter, because it's easy to make mistakes and more generally makes your code more complex. In this case, mistakes include not subtracting 1 from the start values (to make 0-based ranges), and using comma instead of tab as the separator.

ADD COMMENT
0
Entering edit mode

Thanks for your quick hit. Could you bit more specify your motivation please ? what possible changes is needed ? 

ADD REPLY

Login before adding your answer.

Traffic: 950 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6