[adegenet-commits] r82 - in pkg: . R data man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 31 12:43:44 CEST 2008


Author: jombart
Date: 2008-03-31 12:43:44 +0200 (Mon, 31 Mar 2008)
New Revision: 82

Modified:
   pkg/R/auxil.R
   pkg/TODO
   pkg/data/sim2pop.rda
   pkg/man/accessors.Rd
Log:
Minor fix in sim2pop.rda.
The "[" operator now handles the @other slot as well.


Modified: pkg/R/auxil.R
===================================================================
--- pkg/R/auxil.R	2008-03-28 16:46:12 UTC (rev 81)
+++ pkg/R/auxil.R	2008-03-31 10:43:44 UTC (rev 82)
@@ -192,8 +192,8 @@
 # '[' operator
 ###############
 ## genind
-setMethod("[","genind", 
-          function(x, i, j, ..., drop=FALSE) {
+setMethod("[","genind",
+          function(x, i, j, ..., treatOther=TRUE, drop=FALSE) {
 
               if (missing(i)) i <- TRUE
               if (missing(j)) j <- TRUE
@@ -211,6 +211,27 @@
               tab <- tab[i, j, ...,drop=FALSE]
               
               res <- genind(tab,pop=pop,prevcall=prevcall)
+
+              ## handle 'other' slot
+              nOther <- length(x at other)
+              namesOther <- names(x at other)
+              counter <- 0
+              if(treatOther){
+                  f1 <- function(obj,n=nrow(x at tab)){
+                      counter <<- counter+1
+                      if(!is.null(dim(obj)) && nrow(obj)==n) { # if the element is a matrix-like obj
+                          obj <- obj[i,,drop=FALSE]
+                      } else if(length(obj) == n) { # if the element is not a matrix but has a length == n
+                          obj <- obj[i]
+                          if(is.factor(obj)) {obj <- factor(obj)}
+                      } else {warning(paste("cannot treat the object",namesOther[counter]))}
+
+                      return(obj)
+                  } # end f1
+
+                  res at other <- lapply(x at other, f1) # treat all elements
+                  
+              } # end treatOther
               
               return(res)
           })
@@ -218,7 +239,7 @@
 
 ## genpop
 setMethod("[","genpop", 
-          function(x, i, j, ..., drop=FALSE) {
+          function(x, i, j, ..., treatOther=TRUE, drop=FALSE) {
 
               if (missing(i)) i <- TRUE
               if (missing(j)) j <- TRUE
@@ -229,6 +250,28 @@
               tab <- tab[i, j, ...,drop=FALSE]
               
               res <- genpop(tab,prevcall=prevcall)
+
+              ## handle 'other' slot
+              nOther <- length(x at other)
+              namesOther <- names(x at other)
+              counter <- 0
+              if(treatOther){
+                  f1 <- function(obj,n=nrow(x at tab)){
+                      counter <<- counter+1
+                      if(!is.null(dim(obj)) && nrow(obj)==n) { # if the element is a matrix-like obj
+                          obj <- obj[i,,drop=FALSE]
+                      } else if(length(obj) == n) { # if the element is not a matrix but has a length == n
+                          obj <- obj[i]
+                          if(is.factor(obj)) {obj <- factor(obj)}
+                      } else {warning(paste("cannot treat the object",namesOther[counter]))}
+                      
+                      return(obj)
+                  } # end f1
+                  
+                  res at other <- lapply(x at other, f1) # treat all elements
+                  
+              } # end treatOther
+             
               
               return(res)
           })

Modified: pkg/TODO
===================================================================
--- pkg/TODO	2008-03-28 16:46:12 UTC (rev 81)
+++ pkg/TODO	2008-03-31 10:43:44 UTC (rev 82)
@@ -39,7 +39,8 @@
 * Implement spatial weights derived from inverse spatial distances in chooseCN -- done (TJ)
 * Build accessors for marker names, indiv names, pop names, spatial coords, ...
 * Implement a Fst wrapper for genind objects -- done (TJ)
-* 
+* Proceed wisely the elements of @other when subsetting objects using the "[" operator. --done (TJ)
+*
 
 # TESTING:
 ==========

Modified: pkg/data/sim2pop.rda
===================================================================
(Binary files differ)

Modified: pkg/man/accessors.Rd
===================================================================
--- pkg/man/accessors.Rd	2008-03-28 16:46:12 UTC (rev 81)
+++ pkg/man/accessors.Rd	2008-03-31 10:43:44 UTC (rev 82)
@@ -11,7 +11,7 @@
 \description{
   Several accessors for \linkS4class{genind} or
   \linkS4class{genpop} objects. The operator "\$" and "\$<-" are used to
-  access the slots, being equivalent to "@" and "@<-".
+  access the slots, being equivalent to "@" and "@<-".\cr
 
   The operator "[" can be used to access components of the matrix slot
   "@tab", returning a \linkS4class{genind} or \linkS4class{genpop}
@@ -21,8 +21,9 @@
   "obj" is a \linkS4class{genind}) or the first 10 populations (if
   "obj" is a \linkS4class{genpop}) of "obj" \cr
   - "obj[1:10, 5:10]" returns an object keeping the first 10 entities and
-  the alleles 5 to 10. 
-  
+  the alleles 5 to 10.\cr
+  The argument \code{treatOther} handles the treatment of objects in the
+  \code{@other} slot (see details).
 }
 \usage{
 }
@@ -30,6 +31,18 @@
   A \linkS4class{genind} or \linkS4class{genpop} object.
 }
 \author{Thibaut Jombart \email{jombart at biomserv.univ-lyon1.fr} }
+\details{
+  The "[" operator can treat elements in the \code{@other} slot as
+  well. For instance, if \code{obj at other$xy} contains spatial
+  coordinates, the \code{obj[1:3,]@other$xy} will contain the spatial
+  coordinates of the genotypes (or population) 1,2 and 3. This is
+  handled through the argument \code{treatOther}, a logical defaulting
+  to TRUE. If set to FALSE, the \code{@other} component is not
+  returned.\cr
+  Note that only matrix-like, vector-like and lists can be proceeded in
+  \code{@other}. Other kind of objects will issue a warning an be
+  returned as they are.\cr
+}
 \examples{
 data(nancycats)
 nancycats
@@ -52,5 +65,12 @@
 
 obj$loc.fac 
 obj$loc.names
+
+# illustrate how other slot is handled
+colonies <- genind2genpop(nancycats)
+colonies at other$aChar <- "This will not be proceeded"
+colonies123 <- colonies[1:3]
+colonies
+colonies at other$xy
 }
 \keyword{manip}
\ No newline at end of file



More information about the adegenet-commits mailing list