[Bioc-devel] names, mcols and metadata are not changed in "[<-" operator of class Vector

Sebastian Gibb sgibb.debian at gmail.com
Thu May 8 01:10:47 CEST 2014


Hi Hervé,

thanks for the explanation. I never recognized that this happens with ordinary
vectors, too.

Of course, I see that it would be a bad style to overload "[<-" for my own
class to behave like I want to.

So I need to update the metadata manually.

Kind regards,

Sebastian


On 2014-05-07 20:18:58, hpages at fhcrc.org wrote:
> Hi Sebastian,
>
> This is the expected behavior and it's motivated by how [<- behaves on
> ordinary vectors:
>
>    x <- setNames(1:3, LETTERS[1:3])
>
> Then:
>
>    > x
>    A B C
>    1 2 3
>
> Replacing the first two elements:
>
>    x[2:1] <- x[1:2]
>
> Then:
>
>    > x
>    A B C
>    2 1 3
>
> The names are preserved.
>
> So we need to think of [<- as an operator that touches the values of
> a vector-like object without touching its structure or its attributes.
> People sometimes rely on this behavior to stuff the entire object with
> a given value:
>
>    x[] <- some_value
>
> They wouldn't expect this to destroy the names or metadata cols that
> are on 'x'.
>
> Hope that makes sense,
>
> Cheers,
> H.
>
>
> On 05/07/2014 11:59 AM, Sebastian Gibb wrote:
> > 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
> >
> > _______________________________________________
> > Bioc-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/bioc-devel
> >
>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: hpages at fhcrc.org
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319



More information about the Bioc-devel mailing list