[Bioc-devel] names, mcols and metadata are not changed in "[<-" operator of class Vector
Sebastian Gibb
sgibb.debian at gmail.com
Wed May 7 20:59:42 CEST 2014
Dear all,
today I observed an unexpected behaviour (at least for me) using the "[<-"
operator on classes that inherited "Vector".
I want to change the order of some elements inside the Vector (in my use case it
is an AAStringSet object). If I use an index in "[<-" the names, the
elementMetadata and the metadata are not replaced by their counterparts, e.g.:
###
library("IRanges")
i <- IRanges(1:3, 4:6)
names(i) <- LETTERS[1:3]
i
# IRanges of length 3
# start end width names
# [1] 1 4 4 A
# [2] 2 5 4 B
# [3] 3 6 4 C
i[2:1] <- i[1:2]
i
# IRanges of length 3
# start end width names
# [1] 2 5 4 A
# [2] 1 4 4 B
# [3] 3 6 4 C
## names should be B, A, C; the elementMetadata and metadata are in the wrong
## order, too.
## expected output:
# IRanges of length 3
# start end width names
# [1] 2 5 4 B
# [2] 1 4 4 A
# [3] 3 6 4 C
###
I tried to figure out the reason for this and ended up with the "replaceROWS"
method (in {IRanges,S4Vectors}/R/Vector-class.R).
In this method the mcols, metadata and names are just restored. To be honest I
do not really understand this function.
Why is the new value first append to the original vector (`ans <- c(x, value)`)
and subsequently extracted? Would a simple replace, e.g. `x[i] <- value` not be
enough?
In my opinion the last three lines should restore the original metadata
*and* replace the corresponding metadata by their new counterparts.
### replaceROWS from IRanges/R/Vector-class.R
setMethod("replaceROWS", "Vector",
function(x, i, value)
{
idx <- seq_along(x)
i <- extractROWS(setNames(idx, names(x)), i)
## Assuming that objects of class 'class(x)' can be combined with c().
ans <- c(x, value)
idx[i] <- length(x) + seq_len(length(value))
## Assuming that [ works on objects of class 'class(x)'.
ans <- ans[idx]
## Restore the original decoration.
metadata(ans) <- metadata(x)
names(ans) <- names(x)
mcols(ans) <- mcols(x)
ans
}
)
###
Kind regards,
Sebastian
More information about the Bioc-devel
mailing list