layers.metabarcoding.R 3 KB
Newer Older
Eric Coissac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#' @include 02_class_metabarcoding.data.R
NULL

#
#
# Managment of layers
#
# Layers a matrix or factors with the same dimension
# than the read matrix
#

# get motus data.frames

#' @export
setGeneric("layer.names", function(obj) {
  return(standardGeneric("layer.names"))
})

#' Returns the names of all the layers 
#' 
#' \code{layer.names} extracts the list of all the layer
#' names attached to a \code{\link{metabarcoding.data}} instance.
#' 
#' @param   obj a \code{\link{metabarcoding.data}} instance
#' @return  a vector of type \code{character} containing the
#'          list of all the layer names.
#' 
#' @docType methods
#' @rdname layer.names-methods
#' @aliases layer.names-methods,metabarcoding.data
#' 
setMethod("layer.names", "metabarcoding.data", function(obj) {
  return(names(obj@layers))
})


#' Returns the a layer associated to a \code{\link{metabarcoding.data}}
#' 
#' [[ operator Extracts a layer
#' attached to a \code{\link{metabarcoding.data}} instance.
#' 
#' @usage \method{[[}{unmutable}(x,i)
#'
#' @param   x a \code{\link{metabarcoding.data}} instance
#' @return  matrix or a factor.
#' 
#' @docType methods
#' @rdname double-open-brace-methods
#' @aliases double-open-brace-methods,metabarcoding.data
#' @method [[
#' @export
#' 
setMethod("[[", "metabarcoding.data", 
          function(x, i, j, ...) {
            
            if (! is.character(i))
              stop('Just named index must be used')  
            
            if (i=="reads")
              return(x@reads)
            
            if (i=="samples")
              return(x@samples)
            
            if (i=="motus")
              return(x@motus)
            
            if (i=="reads")
              return(x@reads)
            
            return(x@layers[[i,exact=TRUE]])
          })

#' @method $
#' @export
setMethod("$", "metabarcoding.data", 
          function(x, name) {
            return(x[[name]])
          })


# set one data layer data.frames

#' @method [[<-
#' @export
setMethod("[[<-","metabarcoding.data", 
          function(x, i, j, ...,value) {
            
            if (any(dim(value)!=c(x@scount,x@mcount)))
              stop("data dimmension are not coherent with this metabarcoding.data")
            
            if (hasArg('j'))
              stop('Just one dimension must be specified')
            
            if (! is.character(i))
              stop('Just named index must be used')
            
            if (i=='reads')
              stop('you cannot change the reads layer by this way')
            
            if (i=='motus' | i=='samples')
              stop('layers cannot be names motus or samples')
            
            value = as.factor.or.matrix(value)
            rownames(value)=rownames(x@reads)
            colnames(value)=colnames(x@reads)
            x@layers[[i]]=value
            
            return(x)
          })

#' @method $<-
#' @export
setMethod("$<-","metabarcoding.data", 
          function(x, name, value) {
            
            x[[name]]=value
            return(x)
          })