Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gaps and GRangesList #17

Open
kasperdanielhansen opened this issue Oct 4, 2018 · 3 comments
Open

gaps and GRangesList #17

kasperdanielhansen opened this issue Oct 4, 2018 · 3 comments

Comments

@kasperdanielhansen
Copy link

gaps() does not work on a GRangesList:

> gaps(grIntrons.24)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘gaps’ for signature ‘"CompressedGRangesList"’

I would expect it to work :) Running endoapply(grIntrons.24, gaps) takes forever.

@malcook
Copy link

malcook commented Aug 26, 2019

FWIW, and to do more than simply +1 this issue, perhaps provoke a reaction, or help a fellow traveler, here is my workaround for same:

 grlGaps<-function(grl) {
    ## ALAS: [gaps does not work on a
    ## GRangesList](https://github.com/Bioconductor/GenomicRanges/issues/17)
    ##
    ## Here is quick-to-code but slow-to-run implementation:
    ## return(endoapply(grl,gaps,NA,NA))
    ## NB: the NA arguments are required to on include "internal"
    ## gaps.
    ## 
    ## By my timing, the below is about 175x faster than endoapply.
    ## It requires grl to be 'named' uniquely.  There may be some
    ## other ways its assumptions are not what you would expect by
    ## gaps on a GRangesList.
    stopifnot(0<length(names(grl)))
    stopifnot(0==anyDuplicated(names(grl)))
    r<-unlist(range(grl))
    girl<-gaps(ranges(grl),NA,NA) # gaps as an iranges list
    l<-lengths(girl)
    rdf<-as.data.frame(r)
    lkup<-rdf[names(girl),]
    res<-GRanges(rep(lkup$seqnames,l),unlist(girl),rep(lkup$strand,l))
    res<-split(unname(res),names(res))
    missing<-setdiff(names(grl),names(res))
    missing<-`names<-`(makeGRangesListFromFeatureFragments(missing
                                                          ,rep(list(NULL),length(missing))
                                                          ,rep(list(NULL),length(missing))),
                       missing)
    res<-c(res,missing)[names(grl)] # put them in the same order as grl.
    res
  }

@lawremi
Copy link
Contributor

lawremi commented Aug 26, 2019

In most cases, it's often preferable to do something like getting the transcript ranges and then doing a psetdiff(tx, introns_or_exons).

@malcook
Copy link

malcook commented Aug 27, 2019

Thanks @lawremi - even faster and cleaner your way, which method I had forgotten about.
So, my function (if we still want a function at all) can be re-written

  grlGaps<-function(grl) {
    ## for discussion, c.f.  [gaps does not work on a GRangesList](https://github.com/Bioconductor/GenomicRanges/issues/17) .
   psetdiff(unlist(range(grl),use.names=FALSE),grl))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants