How to properly sort GRanges?
3
@enricoferrero-6037
Last seen 3.0 years ago
Switzerland
Hi,
I have a few GRanges that I need to sort based on their chromosomes/seqnames, start and end coordinates of the intervals.
I used to be able to do it in this way:
sort(gr, by = ~ seqnames + start + end)
But now I get this error:
Error in get(nm, parent, mode = "function") :
object 'seqnames' of mode 'function' was not found
Is there anything wrong with my code?
What's the best way to reliably sort multiple GRanges objects in the same way (similar to bedtools sort or bedops sort-bed)?
Thanks!
granges
genomicranges
sort
seqnames
order
• 17k views
@danielsilvestre-6769
Last seen 9.2 years ago
Brazil
Firstly, verify that seqlevels are sorted:
gr <- sortSeqlevels(gr)
Then, just sort your GRanges object:
gr <- sort(gr)
Simple as that. You should take some time follow through the GenomicRanges vignettes. There are a lot of quite useful tricks inside
@james-w-macdonald-5106
Last seen 6 hours ago
United States
As long as your seqlevels are ordered correctly, sort() should do it.
> z <- GRanges(c("chr3","chr4","chr1"), IRanges(c(3,4,5), c(6,7,8)))
> seqlevels(z) <- sort(seqlevels(z))
> z
GRanges object with 3 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr3 [3, 6] *
[2] chr4 [4, 7] *
[3] chr1 [5, 8] *
-------
seqinfo: 3 sequences from an unspecified genome; no seqlengths
> sort(z)
GRanges object with 3 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr1 [5, 8] *
[2] chr3 [3, 6] *
[3] chr4 [4, 7] *
-------
seqinfo: 3 sequences from an unspecified genome; no seqlengths
@yinghua-7593
Last seen 9.6 years ago
United States
The error message "object 'seqnames' of mode 'function' was not found" is probably due to the broken sort function. See GRanges manual page 17,
## TODO: Broken. Please fix!
#sort(gr, by = ~ score)
Clearly, sort by mcols was known as broken.
Login before adding your answer.
Traffic: 660 users visited in the last hour
Yep. By default
sort()
will sort a GRanges object by seqnames, strand, start, and end. If you want the strand to be ignored, useignore.strand=TRUE
:H.