[BioC] IRanges - Find

Martin Morgan mtmorgan at fhcrc.org
Sat Mar 12 22:45:37 CET 2011


On 03/12/2011 06:41 AM, Fahim Mohammad wrote:
> Hi
> Is it possible to filter ranges by imposing condition on name field.  I am
> using the IRanges vignette example below:
> 
> Require(IRanges)
> ir1 <- IRanges(start = 1:10, width = 10:1)
> ir2 <- IRanges(start = 1:10, end = 11)
> ir3 <- IRanges(end = 11, width = 10:1)
> identical(ir1, ir2) & identical(ir2, ir3)
> ir <- IRanges(c(1, 8, 14, 15, 19, 34, 40),
>   width = c(12, 6, 6, 15, 6, 2, 7))
> 
> values <- rnorm(length(ir))
> rd <- RangedData(ir, name = letters[seq_len(length(ir))], values)
> rd
> 1> rd
> RangedData with 7 rows and 2 value columns across 1 space
>         space    ranges |                 name             values
>   <character> <IRanges> |     <character>  <numeric>
> 1           1        [ 1, 12] |                a            -0.8204717
> 2           1        [ 8, 13] |                b            -0.2419617
> 3           1        [14, 19] |               c           -0.7747140
> 4           1        [15, 29] |               d           -0.1103875
> 5           1        [19, 24] |               e           -0.2945884
> 6           1        [34, 35] |               f            1.8082479
> 7           1        [40, 46] |               g           -0.1854162
> 
> 
> I have another list *toFind*:
> 
> toFind = c ('a', 'c', 'd', 'f')
> 
> Is it possible to find the correponding ranges? The output in this case
> would be

Hi Fahim --

access the 'name' field rd[["name"]] and use R's %in% to see which are
in toFind; this is a logical vector that can be used to subset rd, as

> rd[rd[["name"]] %in% toFind, ]
RangedData with 4 rows and 2 value columns across 1 space
        space    ranges |        name      values
  <character> <IRanges> | <character>   <numeric>
1           1  [ 1, 12] |           a -0.02434458
2           1  [14, 19] |           c  0.43727261
3           1  [15, 29] |           d  0.92065069
4           1  [34, 35] |           f  0.66271368

> 
> 1> output
> RangedData with 4 rows and 2 value columns across 1 space
>         space    ranges |                 name             values
>   <character> <IRanges> |     <character>  <numeric>
> 1           1        [ 1, 12] |                a            -0.8204717
> 2           1        [14, 19] |               c           -0.7747140
> 3           1        [15, 29] |               d           -0.1103875
> 4           1        [34, 35] |               f            1.8082479
> 
> 
> Thanks a lot.
> Fahim
> 
> 
> 
> 


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

Location: M1-B861
Telephone: 206 667-2793



More information about the Bioconductor mailing list