Replacement for lapply on a GRanges?
2
0
Entering edit mode
@stephanie-m-gogarten-5121
Last seen 5 weeks ago
University of Washington

lapply used to work with GRanges objects in Bioconductor 3.6, but it no longer works in 3.7. What is the recommended replacement?

> x <- GRanges(seqnames=c(1,1), ranges=IRanges(start=c(1,10), width=10))
> lapply(x, print)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘getListElement’ for signature ‘"GRanges"’

> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets
[8] methods   base

other attached packages:
[1] GenomicRanges_1.32.3 GenomeInfoDb_1.16.0  IRanges_2.14.10
[4] S4Vectors_0.18.2     BiocGenerics_0.26.0

loaded via a namespace (and not attached):
[1] zlibbioc_1.26.0        compiler_3.5.0         XVector_0.20.0
[4] GenomeInfoDbData_1.1.0 RCurl_1.95-4.10        bitops_1.0-6
genomicranges • 2.7k views
ADD COMMENT
1
Entering edit mode
@herve-pages-1542
Last seen 18 hours ago
Seattle, WA, United States

Hi Stephanie,

The recommended replacement is to loop over seq_along(x) instead of x so:

lapply(seq_along(x), function(i) print(x[i]))

Cheers,

H.

ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 12 days ago
United States

Hi Stephanie -- please see this thread.

ADD COMMENT
0
Entering edit mode

Thanks! I remembered that this thread existed, but failed to find it because I was searching for "lapply" instead of "apply" or "sapply". And I thought it might be useful to have this question answered on the support site.

ADD REPLY
0
Entering edit mode

Hi, I thought this might be relevant too:

how do I subset the Granges object by an element of the metadata? if i want to access to the one specific chr I can:

r[seqnames(r)=="chr7"]

but if i want the granges subset of an entry whose id is specified in the metadata? 

both

r[r$tx_name=="uc057bpa.1"]

and

r[elementMetadata(r)$tx_name=="uc057bpa.1"]

give:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘getListElement’ for signature ‘"GRanges"’

elementMetadata(r)$tx_name=="uc057bpa.1" and r$tx_name=="uc057bpa.1" are LogicalLists, how do I use them to select the ranges I need?

many thanks

ADD REPLY
0
Entering edit mode

There can be multiple transcript names per element of the GRanges. How do you want to resolve that case? If you know that there is exactly one per range, just unlist() it, otherwise you might want something like any(). The point is, you need to consider the complexity of the data. 

ADD REPLY

Login before adding your answer.

Traffic: 804 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