[Bioc-sig-seq] GRanges, ordering and subsetting

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Jul 8 18:40:06 CEST 2010


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