Question: Replacement for lapply on a GRanges?
0
gravatar for Stephanie M. Gogarten
16 months ago by
University of Washington
Stephanie M. Gogarten720 wrote:

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 • 835 views
ADD COMMENTlink modified 16 months ago by Hervé Pagès ♦♦ 14k • written 16 months ago by Stephanie M. Gogarten720
Answer: Replacement for lapply on a GRanges?
1
gravatar for Hervé Pagès
16 months ago by
Hervé Pagès ♦♦ 14k
United States
Hervé Pagès ♦♦ 14k wrote:

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 COMMENTlink modified 16 months ago • written 16 months ago by Hervé Pagès ♦♦ 14k
Answer: Replacement for lapply on a GRanges?
0
gravatar for Martin Morgan
16 months ago by
Martin Morgan ♦♦ 23k
United States
Martin Morgan ♦♦ 23k wrote:

Hi Stephanie -- please see this thread.

ADD COMMENTlink modified 16 months ago • written 16 months ago by Martin Morgan ♦♦ 23k

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 REPLYlink written 16 months ago by Stephanie M. Gogarten720

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 REPLYlink written 15 months ago by fabiola0

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 REPLYlink written 15 months ago by Michael Lawrence11k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 139 users visited in the last hour