Question: Weaknesses with GRanges data type, or just me ?
gravatar for hauken_heyken
23 months ago by
hauken_heyken60 wrote:

So a GRangeslist have group and group_name, but when you do this conversion, the info is lost:


Grangeslist -> data.frame -> GRanges ->GrangesList

Is there a way to preserve the information ?

I tried to save the grouping of the original GRangeslist, but since the final GRangeslist only have 1 group, it does not work.


Of course you can do something like: a global data.frame of all granges

GRangesList(lapply($names, function(x) getGRLbyName(x)))
getGRLbyName = function(name){
  tempEquals =[$name == name] #find all in group by name
  a = GRanges( tempEquals )
  names(a) = rep(x,nrow(tempEquals)) # make names

But this is just bad..

Another artifact is that now the group will be added as a metacolumn in the final GRangesList.

How do you guys do this ?


Next thing is how GRanges accesses rows:

Usually this is done by name, i.g gr["ENST0009123123"], but what if I want to access by exon_id, which is a meta column ?

It looks like this is not possible ? and I therefor have to transform to data.frame, and I lose the grouping..

I feel like GRanges is not suited if you want to do big changes to specific data in it, because you can not do what you want, unless you transform to data.frame

ADD COMMENTlink modified 23 months ago by Michael Lawrence11k • written 23 months ago by hauken_heyken60
Answer: Weaknesses with GRanges data type, or just me ?
gravatar for Michael Lawrence
23 months ago by
United States
Michael Lawrence11k wrote:

Information will be lost through coercion to a more general data structure. I guess there could be a way to coerce from data.frame to GRangesList, making an inference on the grouping column based on its name. 

Typically though this round trip should not be necessary. GRanges shares much of the same functionality as data.frame. For example, to subset to a specific exon_id, you could do something like:

gr[gr$exon_id == "exon1"]


ADD COMMENTlink written 23 months ago by Michael Lawrence11k

Yes, that is good, but since I need the grouping I need to do it on a GRangesList:

This does not work:

#grl: granges list grouped by transcript

grl[grl$exon_id == 1]


Output is:

GRangesList object of length 0:
<0 elements>

Only way I could get an output is something like this:

grl[names(unlist(grl)$exon_id == 1)]

But since all exons in each group have same name, it will only return the original list.

Any way around this without making granges or dataframe and preserve the grouping ?

ADD REPLYlink modified 23 months ago • written 23 months ago by hauken_heyken60

By looking through this post: A: Fast way to access metadata for GRangesList?


I found that this works, but not totaly what I want:

#grl: grangesList

#gr = unlist(grl) , granges object

regrl = relist(u,grl)

The groups are back, but they are not identical:


This is good enough for me, so I consider the question solved

ADD REPLYlink modified 23 months ago • written 23 months ago by hauken_heyken60

tweak that so that unlist() has use.names=FALSE, otherwise the names of the unlisted object are those of the elements of the list, e.g.,

identical(grl, relist(unlist(grl, use.names=FALSE), grl))    # TRUE
ADD REPLYlink written 23 months ago by Martin Morgan ♦♦ 23k

If you need the data as a GRangesList, then there is a choice: are you restricting the analysis to a single chromosome? If so, this does what you want:

keepSeqlevels(grl, "chr1", pruning.mode="tidy")

Or, be explicit:

grl1 <- grl[seqnames(grl) == "chr1"]
grl1[lengths(grl1) > 0]


ADD REPLYlink written 23 months ago by Michael Lawrence11k
Please log in to add an answer.


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