Sorting data frame derived from GRanges
1
0
Entering edit mode
igor ▴ 40
@igor
Last seen 10 months ago
United States

I had a GRanges object that I converted to a data frame. I am then sorting it by chr and start position:

df[order(df$seqnames, df$start),]

However, the resulting data frame starts with chr17. All chromosomes are present, including chr1 and chr10, which should come up before chr17. Is there some additional metadata that I am not aware of?

granges data.frame sort • 1.4k views
ADD COMMENT
2
Entering edit mode
@michael-lawrence-3846
Last seen 2.4 years ago
United States

I'm guessing that the levels of the factor are in the wrong order. This should come directly from the order of the seqlevels in your original GRanges. You can fix your seqlevels automatically using sortSeqlevels(). Btw, sorting the GRanges before conversion to data.frame might be easier, since it is basically just sort(gr),  but you will need to fix their order first.

ADD COMMENT
0
Entering edit mode

I forgot that factors have their own order. I converted seqnames to characters. I think that's even easier.

ADD REPLY
1
Entering edit mode

Perhaps. But you lose the information that the factor provides. For example, if you plot the data by chromosome, they will likely be rendered in a non-natural order.

ADD REPLY

Login before adding your answer.

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