[adegenet-commits] r254 - in pkg: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 27 17:49:38 CET 2009


Author: jombart
Date: 2009-01-27 17:49:38 +0100 (Tue, 27 Jan 2009)
New Revision: 254

Modified:
   pkg/R/auxil.R
   pkg/R/handling.R
   pkg/man/accessors.Rd
   pkg/man/genind.Rd
   pkg/man/genpop.Rd
Log:
added pop generic & method; added selpopsize


Modified: pkg/R/auxil.R
===================================================================
--- pkg/R/auxil.R	2009-01-27 12:53:49 UTC (rev 253)
+++ pkg/R/auxil.R	2009-01-27 16:49:38 UTC (rev 254)
@@ -114,7 +114,7 @@
 
     ## names of functions which are ok for dominant markers
     PAOk <- c("genind","genpop","genind2genpop","summary",
-                 "truenames","seppop","na.replace","nLoc","scaleGen","spca")
+                 "truenames","seppop","na.replace","nLoc","scaleGen","spca","selpop")
 
     PAWarn <- c("df2genind")
 

Modified: pkg/R/handling.R
===================================================================
--- pkg/R/handling.R	2009-01-27 12:53:49 UTC (rev 253)
+++ pkg/R/handling.R	2009-01-27 16:49:38 UTC (rev 254)
@@ -305,11 +305,13 @@
     ## misc checks
     if(!is.genind(x)) stop("x is not a valid genind object")
     if(is.null(pop)) { # pop taken from @pop
+        if(is.null(x at pop)) stop("pop not provided and x at pop is empty")
         pop <- x at pop
         levels(pop) <- x at pop.names
+    } else{
+        pop <- factor(pop)
     }
 
-    if(is.null(pop)) stop("pop not provided and x at pop is empty")
 
     res.type <- match.arg(res.type)
     if(res.type=="genind") { truenames <- TRUE }
@@ -475,11 +477,48 @@
 
 
 
+
+#############
+# selpopsize
+#############
+setGeneric("selpop", function(x, ...) standardGeneric("selpop"))
+
+## genind method ##
+setMethod("selpop", signature(x="genind"), function(x,pop=NULL,nMin=10){
+
+    ## misc checks
+    checkType(x)
+    if(!is.genind(x)) stop("x is not a valid genind object")
+    if(is.null(pop)) { # pop taken from @pop
+        if(is.null(x at pop)) stop("pop not provided and x at pop is empty")
+        pop <- x at pop
+        levels(pop) <- x at pop.names
+    } else{
+        pop <- factor(pop)
+    }
+
+    ## select retained individuals
+    effPop <- table(pop)
+    popOk <- names(effPop)[effPop >= nMin]
+    toKeep <- pop %in% popOk
+
+    ## build result
+    res <- x[toKeep]
+    pop(res) <- pop[toKeep]
+
+    return(res)
+}) # end selpop
+
+
+
+
+
+
+
 ######################
 ## miscellanous utils
 ######################
 
-
 #######
 # nLoc
 #######
@@ -499,3 +538,49 @@
     return(length(x at loc.names))
 })
 
+
+
+
+
+######
+# pop
+######
+setGeneric("pop", function(x) {
+  standardGeneric("pop")
+})
+
+
+
+setGeneric("pop<-",
+           function(x, value) {
+               standardGeneric("pop<-")
+           })
+
+
+
+setMethod("pop","genind", function(x){
+    if(is.null(x at pop)) return(NULL)
+    res <- x at pop
+    levels(res) <- x at pop.names
+    return(res)
+})
+
+
+
+setReplaceMethod("pop", "genind", function(x, value) {
+    if(length(value) != nrow(x$tab)) stop("wrong length for population factor")
+
+    ## coerce to factor (put levels in their order of appearance)
+    newPop <- as.character(value)
+    newPop <- factor(newPop, levels=unique(newPop))
+
+    ## generic labels
+    newPop.lab <- .genlab("P",length(levels(newPop)) )
+
+    ## construct output
+    x$pop.names <- levels(newPop)
+    levels(newPop) <- newPop.lab
+    x$pop <- newPop
+
+    return(x)
+})

Modified: pkg/man/accessors.Rd
===================================================================
--- pkg/man/accessors.Rd	2009-01-27 12:53:49 UTC (rev 253)
+++ pkg/man/accessors.Rd	2009-01-27 16:49:38 UTC (rev 254)
@@ -7,9 +7,17 @@
 \alias{$<-,genpop-method}
 \alias{[,genind-method}
 \alias{[,genpop-method}
+\alias{nLoc}
+\alias{nLoc,genind-method}
+\alias{nLoc,genpop-method}
+\alias{pop}
+\alias{pop<-}
+\alias{pop,genind-method}
+\alias{pop<-,genind-method}
 \title{ Accessors for adegenet objects}
 \description{
-  Several accessors for \linkS4class{genind} or
+  An accessor is a function that allows to interact with slots of an
+  object in a convenient way. Several accessors are available for \linkS4class{genind} or
   \linkS4class{genpop} objects. The operator "\$" and "\$<-" are used to
   access the slots, being equivalent to "@" and "@<-".\cr
 
@@ -30,7 +38,14 @@
   The argument \code{treatOther} handles the treatment of objects in the
   \code{@other} slot (see details).
 }
-\usage{
+\section{Methods}{
+  \describe{
+    \item{nLoc}{returns the number of loci of the object}
+    \item{pop}{returns the population factor of the object, using true
+      (as opposed to generic) levels.}
+    \item{pop<-}{replacement method for the \code{@pop} slot of an
+  object. The content of \code{@pop} and \code{@pop.names} is updated automatically.}
+  }
 }
 \value{
   A \linkS4class{genind} or \linkS4class{genpop} object.
@@ -82,5 +97,16 @@
 colonies123 <- colonies[1:3]
 colonies
 colonies at other$xy
+
+# illustrate pop
+obj <- nancycats[sample(1:100,10)]
+obj$pop
+obj$pop.names
+pop(obj)
+pop(obj) <- rep(c('b','a'), each=5)
+obj$pop
+obj$pop.names
+pop(obj)
+
 }
 \keyword{manip}
\ No newline at end of file

Modified: pkg/man/genind.Rd
===================================================================
--- pkg/man/genind.Rd	2009-01-27 12:53:49 UTC (rev 253)
+++ pkg/man/genind.Rd	2009-01-27 16:49:38 UTC (rev 254)
@@ -5,8 +5,6 @@
 \alias{show,genind-method}
 \alias{names,genind-method}
 \alias{summary,genind-method}
-\alias{nLoc}
-\alias{nLoc,genind-method}
 \alias{.valid.genind}
 \title{adegenet formal class (S4) for individual genotypes}
 \description{The S4 class \code{genind} is used to store individual genotypes.\cr
@@ -60,9 +58,6 @@
       object (same as print)}
     \item{summary}{\code{signature(object = "genind")}: summarizes a
       genind object, invisibly returning its content}
-    \item{nLoc}{\code{signature(object = "genind")}: returns the number
-      of loci of the object}
-    
   }
 }
 \seealso{\code{\link{as.genind}}, \code{\link{is.genind}}, \code{\link{genind2genpop}},

Modified: pkg/man/genpop.Rd
===================================================================
--- pkg/man/genpop.Rd	2009-01-27 12:53:49 UTC (rev 253)
+++ pkg/man/genpop.Rd	2009-01-27 16:49:38 UTC (rev 254)
@@ -5,7 +5,6 @@
 \alias{names,genpop-method}
 \alias{show,genpop-method}
 \alias{summary,genpop-method}
-\alias{nLoc,genpop-method}
 \title{adegenet formal class (S4) for allele counts in populations}
 \description{An object of class \code{genpop} contain alleles counts
   for several loci.\cr
@@ -48,8 +47,6 @@
       object (same as print)}
     \item{summary}{\code{signature(object = "genpop")}: summarizes a
       genpop object, invisibly returning its content}
-    \item{nLoc}{\code{signature(object = "genpop")}: returns the number
-      of loci of the object}
   }
 }
 \references{}



More information about the adegenet-commits mailing list