[Bioc-sig-seq] GRanges, ordering and subsetting
Ivan Gregoretti
ivangreg at gmail.com
Thu Jul 8 18:48:11 CEST 2010
Thank you Steve. That answers my questions.
Ivan
Ivan Gregoretti, PhD
National Institute of Diabetes and Digestive and Kidney Diseases
National Institutes of Health
5 Memorial Dr, Building 5, Room 205.
Bethesda, MD 20892. USA.
Phone: 1-301-496-1016 and 1-301-496-1592
Fax: 1-301-496-9878
On Thu, Jul 8, 2010 at 12:40 PM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
> Hi Ivan,
>
> On Thu, Jul 8, 2010 at 11:48 AM, Ivan Gregoretti <ivangreg at gmail.com> wrote:
>> Can anybody show an example of how to sort GRanges by score?
>>
>> Lets say I have this GRanges instance
>>
>> library(GenomicRanges)
>>
>> gr <- GRanges(seqnames = Rle(c("chr1", "chr2", "chr1", "chr3"), c(1,
>> 3, 2, 4)), ranges = IRanges(1:10, end = 7:16, names = head(letters,
>> 10)), strand = Rle(strand(c("-", "+", "*", "+", "-")), c(1, 2, 2, 3,
>> 2)), score = 1:10, GC = seq(1, 0, length = 10))
>>
>> gr
>> GRanges with 10 ranges and 2 elementMetadata values
>> seqnames ranges strand | score GC
>> <Rle> <IRanges> <Rle> | <integer> <numeric>
>> a chr1 [ 1, 7] - | 1 1.0000000
>> b chr2 [ 2, 8] + | 2 0.8888889
>> c chr2 [ 3, 9] + | 3 0.7777778
>> d chr2 [ 4, 10] * | 4 0.6666667
>> e chr1 [ 5, 11] * | 5 0.5555556
>> f chr1 [ 6, 12] + | 6 0.4444444
>> g chr3 [ 7, 13] + | 7 0.3333333
>> h chr3 [ 8, 14] + | 8 0.2222222
>> i chr3 [ 9, 15] - | 9 0.1111111
>> j chr3 [10, 16] - | 10 0.0000000
>>
>> seqlengths
>> chr1 chr2 chr3
>> NA NA NA
>>
>> How do I re-order the rows in descending score values?
>
> How about:
> R> o <- order(elementMetadata(gr)$score, decreasing=TRUE)
> R> gr <- gr[o]
>
>> How do I subset the top 3 score ranges?
> If you did the above move:
> R> gr[1:3]
>
> If you didn't reorder gr based on o, you could do:
>
> R> gr[o[1:3]]
>
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
> | Memorial Sloan-Kettering Cancer Center
> | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
More information about the Bioc-sig-sequencing
mailing list