[Phylobase-commits] r787 - in pkg: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 23 16:09:00 CEST 2010


Author: davidorme
Date: 2010-04-23 16:09:00 +0200 (Fri, 23 Apr 2010)
New Revision: 787

Modified:
   pkg/R/readNexus.R
   pkg/man/readNexus.Rd
Log:
Making readNexus more general in handling TREE and CHARACTER/DATA blocks

Modified: pkg/R/readNexus.R
===================================================================
--- pkg/R/readNexus.R	2010-04-12 18:57:09 UTC (rev 786)
+++ pkg/R/readNexus.R	2010-04-23 14:09:00 UTC (rev 787)
@@ -112,72 +112,110 @@
                                PACKAGE="phylobase")
         ## Display the string returned by NCL if quiet=FALSE
         if(!quiet) print(intreesstring)
-        intreesphylolist <- read.nexustreestring(intreesstring)
-        if (length(intreesphylolist)>1 && !simplify) {
-            trees <- list()
-            for (i in 1:length(intreesphylolist)) {
-                if(identical(check.node.labels, "asdata")) {
-                    if(is.null(intreesphylolist[[i]]$node.label)) {
-                        warning("Could not use value \"asdata\" for ",
-                                "check.node.labels because there are no ",
-                                "labels associated with the tree ", i)
-                        check.node.labels <- "drop"
-                    }
-                    trees[[i]] <- phylo4d(intreesphylolist[[i]],
-                                          check.node.labels=check.node.labels,
-                                          ...)
-                }
-                else {
-                    trees[[i]] <- phylo4(intreesphylolist[[i]],
-                                         check.node.labels=check.node.labels,
-                                         ...)
-                }
-            }
-        }
-        else {
-            if (identical(check.node.labels, "asdata")) {
-                if (is.null(intreesphylolist[[1]]$node.label)) {
-                    warning("Could not use value \"asdata\" for ",
-                            "check.node.labels because there are no ",
-                            "labels associated with the tree ", i)
-                    check.node.labels <- "drop"
-                }
-                trees <- phylo4d(intreesphylolist[[1]],
-                                 check.node.labels=check.node.labels,
-                                 ...)
-            }
-            else {
-                trees <- phylo4(intreesphylolist[[1]],
-                                check.node.labels=check.node.labels,
-                                ...)
-            }
-        }
+		if(length(intreesstring) > 0){
+	        intreesphylolist <- read.nexustreestring(intreesstring)
+	        if (length(intreesphylolist)>1 && !simplify) {
+	            trees <- list()
+	            for (i in 1:length(intreesphylolist)) {
+	                if(identical(check.node.labels, "asdata")) {
+	                    if(is.null(intreesphylolist[[i]]$node.label)) {
+	                        warning("Could not use value \"asdata\" for ",
+	                                "check.node.labels because there are no ",
+	                                "labels associated with the tree ", i)
+	                        check.node.labels <- "drop"
+	                    }
+	                    trees[[i]] <- phylo4d(intreesphylolist[[i]],
+	                                          check.node.labels=check.node.labels,
+	                                          ...)
+	                }
+	                else {
+	                    trees[[i]] <- phylo4(intreesphylolist[[i]],
+	                                         check.node.labels=check.node.labels,
+	                                         ...)
+	                }
+	            }
+	        }
+	        else {
+	            if (identical(check.node.labels, "asdata")) {
+	                if (is.null(intreesphylolist[[1]]$node.label)) {
+	                    warning("Could not use value \"asdata\" for ",
+	                            "check.node.labels because there are no ",
+	                            "labels associated with the tree ", i)
+	                    check.node.labels <- "drop"
+	                }
+	                trees <- phylo4d(intreesphylolist[[1]],
+	                                 check.node.labels=check.node.labels,
+	                                 ...)
+	            }
+	            else {
+	                trees <- phylo4(intreesphylolist[[1]],
+	                                check.node.labels=check.node.labels,
+	                                ...)
+	            }
+	        }
+		}
+		else {
+			trees <- NULL
+		}
     }
-    if (type == "tree" || (type == "all" && length(tipdata) == 0 )) {
-        output <- trees
-    }
-    else {
-        if (type == "data") {
-            output <- tipdata
-        }
-        else {
-            if (length(intreesphylolist) > 1 && !simplify) {
-                output <- list()
-                for (i in 1:length(intreesphylolist)) {
-                    output[[i]] <- phylo4d(intreesphylolist[[i]],
-                                           tip.data = tipdata,
-                                           check.node.labels=check.node.labels,
-                                           ...)
-                }
-            }
-            else {
-                output <- phylo4d(intreesphylolist[[1]],
-                                  tip.data=tipdata,
-                                  check.node.labels=check.node.labels,
-                                  ...)
-            }
-        }
-    }
+
+	## scheme of what you get back, given what you asked
+	## for and whether data or tree blocks are actually in
+	## the file
+	##                         
+	## in nexus file        type argument
+	## data     tree        all   data  trees
+	## TRUE     FALSE       df    df    NULL
+	## FALSE    TRUE        p4    NULL  p4
+	## TRUE     TRUE        p4d   df    p4
+	## FALSE    FALSE       NULL  NULL  NULL
+	
+	switch(type,
+		'data' = {
+			if(is.null(tipdata)){
+				output <- NULL
+			}
+			else {
+				output <- tipdata
+			}
+		},
+		'tree' = {
+			if(is.null(trees)){
+				output <- NULL
+			}
+			else {
+				output <- trees
+			}
+		},
+		'all' = {
+			if(is.null(tipdata) & is.null(trees)){
+				output <- NULL
+			}
+			else if (is.null(tipdata)){
+				output <- trees
+			}
+			else if (is.null(trees)){
+				output <- tipdata
+			}
+			else {
+	            if (length(intreesphylolist) > 1 && !simplify) {
+	                output <- list()
+	                for (i in 1:length(intreesphylolist)) {
+	                    output[[i]] <- phylo4d(intreesphylolist[[i]],
+	                                           tip.data = tipdata,
+	                                           check.node.labels=check.node.labels,
+	                                           ...)
+	                }
+	            }
+	            else {
+	                output <- phylo4d(intreesphylolist[[1]],
+	                                  tip.data=tipdata,
+	                                  check.node.labels=check.node.labels,
+	                                  ...)
+	            }
+			}		
+		})
+		
     output
 }
 

Modified: pkg/man/readNexus.Rd
===================================================================
--- pkg/man/readNexus.Rd	2010-04-12 18:57:09 UTC (rev 786)
+++ pkg/man/readNexus.Rd	2010-04-23 14:09:00 UTC (rev 787)
@@ -1,13 +1,13 @@
 \name{readNexus}
 \docType{methods}
 \alias{readNexus}
-\title{Create a phylo4 or a phylo4d object from a Nexus file}
+\title{Create a phylo4, phylo4d or data.frame object from a Nexus file}
 \description{
-  \code{readNexus} reads a Nexus file and outputs a \code{phylo4} or \code{phylo4d} object.
+  \code{readNexus} reads a Nexus file and outputs a \code{phylo4} or \code{phylo4d} or \code{data.frame} object.
 }
 \section{Methods}{
   \describe{
-	\item{x = "readNexus"}{creates a \linkS4class{phylo4} or \linkS4class{phylo4d} object from a Nexus file}
+	\item{x = "readNexus"}{creates a \linkS4class{phylo4}, \linkS4class{phylo4d} or \code{data.frame} object from a Nexus file}
    }
  }
  \usage{
@@ -19,7 +19,8 @@
   \item{file}{a Nexus file}
   \item{simplify}{If FALSE, returns a list. If TRUE, returns a list if
     there are multiple trees, a single object otherwise} 
-  \item{type}{Determines which type of objects to return, if present}
+  \item{type}{Determines which type of objects to return, if present (see 
+    details).}
   \item{char.all}{If TRUE, returns all characters, even those excluded
     in the NEXUS file} 
   \item{polymorphic.convert}{If TRUE, converts polymorphic DNA
@@ -38,6 +39,31 @@
     constructor (see details)}
 }
 \details{
+
+ The function extracts data held in a Nexus file, specifically from
+ DATA, CHARACTER or TREES blocks present in the file. The \code{type} 
+ argument specifies which of these is returned:
+ \describe{
+ \item{data}{will only return a \code{data.frame} of the contents of all 
+   DATA and CHARACTER blocks.}
+ \item{tree}{will only return a \code{phylo4} object of the contents of 
+   the TREES block.}
+ \item{all}{if only data or a tree are present in the file, this option 
+   will act as the options above, returning either a \code{data.frame} 
+   or a \code{phylo4} object respectively. If both are present then a
+   \code{phylo4d} object is returned containing both.}
+ }
+ The function returns \code{NULL} if the \code{type} of data requested is 
+ not present in the file, or if neither data nor tree blocks are present.
+
+ Depending on the context \code{readNexus} will call either the
+ \code{phylo4} or \code{phylo4d} constructor. In addition to with
+ \code{type="all"}, the \code{phylo4d} constructor will be used if 
+ \code{check.node.labels="asdata"}. Additional arguments can be passed
+ to the constructors such as \code{annote}, \code{missing.data} or 
+ \code{extra.data}. See the documentation of \link{phylo4-methods},
+ \link{phylo4d} and \link{formatData} for the complete list of arguments.
+ 
   The options for \code{check.node.labels} can take the values:
   \describe{
      \item{keep}{the node labels of the Nexus file will be passed as
@@ -51,20 +77,12 @@
  warning message is issued and by default \code{check.node.labels} takes
  the value \code{drop}. 
 
- Depending on the context \code{readNexus} will call either the
- \code{phylo4} or \code{phylo4d} constructor. The \code{phylo4d}
- constructor will be used if \code{check.node.labels="asdata"} or if
- \code{type="all"}; and the \code{phylo4} constructor will be used
- otherwise. Additional arguments can be passed to the constructors such
- as \code{annote}, \code{missing.data} or \code{extra.data}. See the
- documentation of \link{phylo4-methods}, \link{phylo4d} and
- \link{formatData} for the complete list of arguments.
- 
+
 }
 \value{
-  A \linkS4class{phylo4} object if there is no data in the Nexus file
-  and if the option \code{check.node.labels} is \dQuote{keep} or
-  \dQuote{drop}, or a \linkS4class{phylo4d} object otherwise.
+  Depending on the value of \code{type} and the contents of the file, 
+  one of: a \code{data.frame}, a \linkS4class{phylo4} object,  a
+  \linkS4class{phylo4d} object or \code{NULL}.
   If several trees are included in the Nexus file and the option
   \code{simplify=FALSE} a list of \linkS4class{phylo4} or
   \linkS4class{phylo4d} objects is returned.



More information about the Phylobase-commits mailing list