[BioC] loop over IRanges spaces

Michael Dondrup Michael.Dondrup at uni.no
Thu Apr 22 14:49:54 CEST 2010


Hi,
how about funtion rdapply (not lapply) which is for that?

The code below computes the sum score for each space in the RangedData:
# taken from the examples mostly:
> ranges <- IRanges(c(1,2,3),c(4,5,6))
>   score <- c(2L, 0L, 1L)
>   rd <- RangedData(ranges, score, space = c("chr1","chr2","chr1"))
> rd
RangedData with 3 rows and 1 value column across 2 spaces
        space    ranges |     score
  <character> <IRanges> | <integer>
1        chr1    [1, 4] |         2
2        chr1    [3, 6] |         1
3        chr2    [2, 5] |         0
> params <- RDApplyParams(rd, function(rd) sum(score(rd)))
> rdapply(params)
$chr1
[1] 3

$chr2
[1] 0


Cheers
Michael

Am Apr 22, 2010 um 1:57 PM schrieb Yvan:

> On 21/04/10 18:43, Michael Lawrence wrote:
>> 
>> 
>> On Wed, Apr 21, 2010 at 6:07 AM, Yvan <yvan.strahm at uni.no 
>> <mailto:yvan.strahm at uni.no>> wrote:
>> 
>>    Hello List,
>> 
>>    I am confused about  how to loop over a rangedData object.
>>    I have this rangedData
>> 
>>    RangedData with 61 rows and 1 value column across 3 spaces
>>            space     ranges |     score
>>    <character> <IRanges> | <numeric>
>>    1   SCAFFOLD_1 [  8,   8] |  -0.09405
>> 
>>    and the spaces are
>> 
>>    "SCAFFOLD_1"   "SCAFFOLD_10"  "SCAFFOLD_100"
>> 
>>    using aggregate it is possible to apply a function to one of the space
>> 
>>    aggregate(rd["SCAFFOLD_1"]$score, start =
>>    1:(length(rd["SCAFFOLD_1"]$score)-w+1), width = w, FUN = sum)
>> 
>>    but how can I apply the aggregate to all space without a for loop ?
>> 
>> 
>> It looks like you're attempting a running window sum of the score 
>> vector. There are more efficient ways of doing this besides 
>> aggregate(). If you convert the score into an Rle, you can use runsum().
>> 
>> Anyway, to do this over each space individually, use lapply().
>> 
>> This would come out to something like:
>> 
>> values(rd)[,"smoothScore"] <- lapply(rd, function(x) 
>> runsum(Rle(x$score), w))
>> 
>> Probably not exactly right, but it gets you in the right direction...
>> 
>> Michael
>> 
>> 
> Hello Michael,
> 
> Thanks for the answer and the tip about runsum!
> I try with lapply but could not get it working right, the main problem 
> is that the runsum is calculated on all values and not for a each 
> specific spaces.
> Sorry, I should have been more precise in the problem description.
> The runsum should be calculated in a space specific manner, let say w=2
> 
> space        score    cumsum
> 1 space1    1            3
> 2 space1    2            4
> 3 space1    2            NA
> 4 space2    10          21
> 5 space2    11          22
> 6 space2    11          NA
> 
> Is it possible to do it with lapply?
> Thanks again for your help
> cheers,
> yvan
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor



More information about the Bioconductor mailing list