I have an RleList
I want to apply some functions on.
A small reprex
of the object I need
IRanges::RleList(
chr1 = 1:20,
chr2 = 20:1
) -> cvg
GenomicRanges::GRanges(
S4Vectors::Rle(c("chr1", "chr2"), c(2, 1)),
IRanges::IRanges(c(1, 4, 2), width=c(4, 8, 6), names=head(letters, 3)),
S4Vectors::Rle(GenomicRanges::strand(c("-", "+", "-")), c(1, 1, 1))
) -> gr
(v <- IRanges::Views(cvg, gr))
RleViewsList object of length 2:
$chr1
Views on a 20-length Rle subject
views:
start end width
[1] 1 4 4 [1 2 3 4]
[2] 4 11 8 [ 4 5 6 7 8 9 10 11]
$chr2
Views on a 20-length Rle subject
views:
start end width
[1] 2 7 6 [19 18 17 16 15 14]
On this object we would like to apply some functions, but I can't understand the behavior. Sometimes it returns an error I can't identify.
For once, the mean
function doesn't work on the list ...
sapply(X = v, FUN = mean)
Warning in mean.default(x) :
argument is not numeric or logical: returning NA
Warning in mean.default(x) :
argument is not numeric or logical: returning NA
chr1 chr2
NA NA
but the sum
function does
sapply(X = v, FUN = sum)
$chr1
a b
10 60
$chr2
c
99
Any ideas why there is this difference?
The main difficulty I encounter is, that I try to bin the single elements within each of the lists inside the RleList
object using the cut
function
For that i have created a function
my_cut <- function(x) {
cut(1:IRanges::width(x), 3)
}
But when I try to use the function, it only takes into account only the first element in each of the groups (hence the warning).
sapply(v, my_cut)
Warning in 1:IRanges::width(x) :
numerical expression has 2 elements: only the first used
$chr1
[1] (0.997,2] (0.997,2] (2,3] (3,4]
Levels: (0.997,2] (2,3] (3,4]
$chr2
[1] (0.995,2.67] (0.995,2.67] (2.67,4.33] (2.67,4.33] (4.33,6] (4.33,6]
Levels: (0.995,2.67] (2.67,4.33] (4.33,6]
I would appreciate the help for this two problems/question
thanks in advance
Assa
What is your ultimate goal? It looks like you might be trying to generate window summaries within each view? You may be looking for functions like
viewSums()
andviewMeans()
, which can operate directly on the RleViewsList, returning another list.