Also note that it's not really necessary to loop here, as is often the case
with IRanges:

rd$windo <- unlist(runmean(RleList(values(rd)[,"score"])))

On Fri, Apr 23, 2010 at 6:13 AM, Michael Lawrence <michafla@gene.com> wrote:

>
>
> On Fri, Apr 23, 2010 at 1:49 AM, Yvan <yvan.strahm@uni.no> wrote:
>
>>  Hello,
>> Thank you both of you.
>>
>> I could could calculate the sliding window, but not as a Rle object, could
>> not append values for the last w-1 position in the Rle object in order to
>> take care of the size problem.
>>
>>
> Why not? Rle supports all the normal vector operations. And runsum or
> runmean will output a vector of the same size as the input, using a choice
> of two endrules. If you want 0's at the end, try something like:
>
> rle[(nrow(rd)-w+1):nrow(rd)] <- 0
>
>
>
>> So I did it like that:
>>
>> params <-RDApplyParams(rd,function(rd)
>> append((diff(c(0,cumsum(rd$score)),lag=w)/w),rep(0,each=w-1),after=(length(rd$score)-w+1)))
>>
>> But when I try to add the new values to the rangedData object I got these
>> error.
>>
>> > values(rd)[,"windo"]<-rdapply(params)
>> Error in `[<-`(`*tmp*`, , j, value = <S4 object of class "DataFrame">) :
>>   ncol(x[j]) != ncol(value)
>> In addition: Warning messages:
>> 1: In mapply(f, ..., SIMPLIFY = FALSE) :
>>   longer argument not a multiple of length of shorter
>> 2: In mapply(f, ..., SIMPLIFY = FALSE) :
>>   longer argument not a multiple of length of shorter
>>
>> But when I check the size, they are the same, here for one space
>>
>> > x<-rdapply(params)
>> > length(x$SCAFFOLD_100) == length(rd["SCAFFOLD_100"]$windo)
>> [1] TRUE
>>
>>
> It may be that that type of insertion is unsupported. Why not just do
> something like:
>
> rd$window <- unlist(x)
>
>
>>  Maybe params miss a parameter or the way I try to update the rd object is
>> wrong. Anyway form the rdapply output a vector could be created and so a new
>> rd object with the new value column.
>>
>> yvan
>>
>>
>> On 22/04/10 15:51, Michael Lawrence wrote:
>>
>>
>>
>> On Thu, Apr 22, 2010 at 5:49 AM, Michael Dondrup <Michael.Dondrup@uni.no>wrote:
>>
>>> Hi,
>>> how about funtion rdapply (not lapply) which is for that?
>>>
>>>
>> lapply() should apply per-space as well, basically providing a short-cut
>> for the more complicated rdapply().
>>
>> > lapply(rd, function(x) sum(x$score))
>> $chr1
>> [1] 3
>>
>> $chr2
>> [1] 0
>>
>> sapply() also works:
>> > sapply(rd, function(x) sum(x$score))
>> chr1 chr2
>>    3    0
>>
>> Another choice is tapply:
>> > tapply(rd$score, space(rd), sum)
>> chr1 chr2
>>    3    0
>>
>> Michael
>>
>>  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@uni.no
>>> >> <mailto:yvan.strahm@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@stat.math.ethz.ch
>>> > https://stat.ethz.ch/mailman/listinfo/bioconductor
>>> > Search the archives:
>>> http://news.gmane.org/gmane.science.biology.informatics.conductor
>>>
>>> _______________________________________________
>>> Bioconductor mailing list
>>> Bioconductor@stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/bioconductor
>>> Search the archives:
>>> http://news.gmane.org/gmane.science.biology.informatics.conductor
>>>
>>
>>
>>
>

	[[alternative HTML version deleted]]

