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

Martin Morgan mtmorgan at fhcrc.org
Thu Jul 8 18:50:26 CEST 2010


On 07/08/2010 09:40 AM, Steve Lianoglou 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)

values(gr) is an alternative (shorter and semantically meaningful) to
elementMetadata(gr). Also ranges(gr) and strand(gr) to extract the
remaining components.

Martin

> 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]]
> 
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the Bioc-sig-sequencing mailing list