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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Mar 3 19:56:17 CET 2011


Author: jombart
Date: 2011-03-03 19:56:17 +0100 (Thu, 03 Mar 2011)
New Revision: 836

Modified:
   pkg/R/SNPbin.R
   pkg/R/import.R
   pkg/man/as.genlight.Rd
   pkg/man/read.PLINK.Rd
Log:
.map file can now be read in read.PLINK, and has an external procedure


Modified: pkg/R/SNPbin.R
===================================================================
--- pkg/R/SNPbin.R	2011-03-03 16:21:55 UTC (rev 835)
+++ pkg/R/SNPbin.R	2011-03-03 18:56:17 UTC (rev 836)
@@ -813,9 +813,9 @@
 })
 
 
-setAs("snp.matrix", "genlight", def=function(from){
-    return(new("genlight", from))
-})
+## setAs("snp.matrix", "genlight", def=function(from){
+##     return(new("genlight", from))
+## })
 
 
 
@@ -823,6 +823,7 @@
 setMethod("as.genlight", "matrix", function(x, ...) as(x, "genlight"))
 setMethod("as.genlight", "data.frame", function(x, ...) as(x, "genlight"))
 setMethod("as.genlight", "list", function(x, ...) as(x, "genlight"))
+## setMethod("as.genlight", "snp.matrix", function(x, ...) as(x, "genlight"))
 
 
 

Modified: pkg/R/import.R
===================================================================
--- pkg/R/import.R	2011-03-03 16:21:55 UTC (rev 835)
+++ pkg/R/import.R	2011-03-03 18:56:17 UTC (rev 836)
@@ -873,10 +873,54 @@
 
 
 
-#######################
-# Function read.PLINK
-#######################
-read.PLINK <- function(file, quiet=FALSE, chunkSize=1000, ploidy=2,
+####################
+## extract.PLINKmap
+####################
+extract.PLINKmap <- function(file, x=NULL){
+    ## CHECK EXTENSION ##
+    ext <- .readExt(file)
+    ext <- toupper(ext)
+    if(ext != "MAP") warning("wrong map.file extension - '.map' expected")
+
+
+    ## READ FILE ##
+    ## find nb of columns
+    txt <- scan(file,what="character",sep="\n",quiet=TRUE,  nlines=1)
+    nb.col <- length( unlist(strsplit(txt,"[[:blank:]]+")))
+
+    ## read file
+    txt <- scan(file,what="character",sep="\t",quiet=TRUE)
+    txt <- matrix(txt, ncol=4, byrow=TRUE)
+
+
+    ## EXTRACT INFO AND RETURN OBJECT ##
+    ## return a genlight
+    if(!is.null(x)){ 
+        ## match data
+        ord <- match(locNames(x), txt[,2]) # check that it is the 2nd column
+        if(!inherits(x, "genlight")) stop("x is not a genlight object")
+        other(x)$chromosome <- factor(txt[ord,1])
+        other(x)$position <- as.integer(txt[ord,4])
+
+        return(x)
+    }
+
+    ## return a list
+    res <- list(chromosome=factor(txt[ord,1]), position=as.integer(txt[ord,4]))
+
+    return(res)
+} # end extract.PLINKmap
+
+
+
+
+
+
+
+########################
+## Function read.PLINK
+########################
+read.PLINK <- function(file, map.file=NULL, quiet=FALSE, chunkSize=1000, ploidy=2,
                        multicore=require("multicore"), n.cores=NULL, ...){
     ## HANDLE ARGUMENTS ##
     ext <- .readExt(file)
@@ -968,6 +1012,13 @@
     misc.info$phenotype <- factor(misc.info$phenotype)
     other(res) <- misc.info
 
+
+    ## HANDLE MAP FILE INFO ##
+    if(!is.null(map.file)){
+        res <- extract.PLINKmap(map.file, res)
+    }
+
+
     ## RETURN OUTPUT ##
     if(!quiet) cat("\n...done.\n\n")
 

Modified: pkg/man/as.genlight.Rd
===================================================================
--- pkg/man/as.genlight.Rd	2011-03-03 16:21:55 UTC (rev 835)
+++ pkg/man/as.genlight.Rd	2011-03-03 18:56:17 UTC (rev 836)
@@ -2,13 +2,16 @@
 \alias{as,genlight,matrix-method}
 \alias{as,genlight,data.frame-method}
 \alias{as,genlight,list-method}
+% \alias{as,genlight,snp.matrix-method}
 \alias{as.genlight}
 \alias{as.genlight,matrix-method}
 \alias{as.genlight,data.frame-method}
 \alias{as.genlight,list-method}
+% \alias{as.genlight,snp.matrix-method}
 \alias{coerce,genlight,matrix-method}
 \alias{coerce,genlight,data.frame-method}
 \alias{coerce,genlight,list-method}
+% \alias{coerce,genlight,snp.matrix-method}
 
 % \alias{,genlight-method}
 % \alias{,genlight-method}

Modified: pkg/man/read.PLINK.Rd
===================================================================
--- pkg/man/read.PLINK.Rd	2011-03-03 16:21:55 UTC (rev 835)
+++ pkg/man/read.PLINK.Rd	2011-03-03 18:56:17 UTC (rev 836)
@@ -1,11 +1,15 @@
 \encoding{UTF-8}
 \name{read.PLINK}
 \alias{read.PLINK}
+\alias{read.plink}
+\alias{extract.PLINKmap}
 \title{ Reading PLINK Single Nucleotide Polymorphism data}
 \description{
   The function \code{read.PLINK} reads a data file exported by the PLINK
   software with extension '.raw' and converts it into a
-  \linkS4class{genlight} object.
+  \linkS4class{genlight} object. Optionally, information about SNPs can
+  be read from a ".map" file, either by specifying the argument
+  \code{map.file} in \code{read.PLINK}, or using \code{extract.PLINKmap}.
 
   The function reads data by chunks of several genomes (minimum 1, no
   maximum) at a time, which allows one to read massive datasets with
@@ -18,12 +22,20 @@
   to the '.raw' format.
 }
 \usage{
-read.PLINK(file, quiet=FALSE, chunkSize=1000, ploidy=2,
+read.PLINK(file, map.file=NULL, quiet=FALSE, chunkSize=1000, ploidy=2,
            multicore=require("multicore"), n.cores=NULL, \dots)
+
+extract.PLINKmap(file, x=NULL)
 }
 \arguments{
-  \item{file}{ a character string giving the path to the file to
-    convert, with the extension ".PLINK".}
+  \item{file}{for \code{read.PLINK} a character string giving the path
+    to the file to convert, with the extension ".raw"; for
+    \code{extract.PLINKmap}, a character string giving the path to a
+    file with extension ".map".}
+  \item{map.file}{an optional character string indicating the path to a
+    ".map" file, which contains information about the SNPs (chromosome,
+    position). If provided, this information is processed by
+    \code{extract.PLINKmap} and stored in the \code{@other} slot.}
   \item{quiet}{ logical stating whether a conversion messages should be
     printed (TRUE,default) or not (FALSE).}
   \item{chunkSize}{an integer indicating the number of genomes to be
@@ -40,6 +52,11 @@
     available on the computer is used.}
   \item{\dots}{other arguments to be passed to other functions -
     currently not used.}
+  \item{x}{an optional object of the class \linkS4class{genlight}, in
+    which the information read is stored; if provided, information is
+    matched against the names of the loci in \code{x}, as returned by
+    \code{locNames(x)}; if not provided, a list of two components is
+    returned, containing chromosome and position information.}
 }
 \details{
   === Exporting data from PLINK ===
@@ -64,7 +81,15 @@
 
   DO NOT use the version on CRAN, which is slightly outdated.
 }
-\value{an object of the class \linkS4class{genlight}}
+\value{
+  - read.PLINK: an object of the class \linkS4class{genlight}
+
+  - extract.PLINKmap: if a \linkS4class{genlight} is provided as
+  argument \code{x}, this object incorporating the new information about
+  SNPs in the \code{@other} slot (with new components 'chromosome' and
+  'position'); otherwise, a list with two components containing
+  chromosome and position information.
+}
 \seealso{
   - a description of the class \linkS4class{genlight}.
 



More information about the adegenet-commits mailing list