Skip to content

Commit

Permalink
Added table2nexus for a general conversion from data table into nexus.
Browse files Browse the repository at this point in the history
…Closes #6 and raises minor number
  • Loading branch information
gaballench committed Apr 17, 2024
1 parent d7c86a5 commit 6cef4e9
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tbea
Title: Tools for Pre- and Post-processing in Bayesian Evolutionary Analyses
Version: 1.2.1
Version: 1.3.0
Authors@R: c(person("Gustavo A.", "Ballen", email = "[email protected]", role = c("aut", "cre")),
person("Sandra", "Reinales", email = "[email protected]", role = c("aut")))
Description: Package for bayesian inference in phylogenetics and evolution.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(lognormalBeast)
export(measureSimil)
export(mswd.test)
export(summaryBrlen)
export(table2nexus)
export(tnt2newick)
export(topoFreq)
export(xintercept)
Expand Down
61 changes: 61 additions & 0 deletions R/table2nexus.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' table2nexus: Read a data matrix in delimited format and convert
#' into a data matrix in nexus format
#'
#' @param path a character vector of length 1 with the path to the table
#' file.
#'
#' @param datatype a character vector of length 1 with the desired datatype.
#' Possible values are STANDARD, DNA, RNA, or PROTEIN.
#' Multicharacter types such as continuous or nucleotide are not supported.
#'
#' @param header a logical vector of length 1 indicating whether the table file
#' has a header. Defaults to FALSE.
#'
#' @param sep a character vector of length 1 telling the kind of separator in
#' the table file. Defaults to comma ",".
#'
#' @param con the connection to which the matrix should be returned. Defaults
#' to stdout(), that is, return the text to the console. If writing to a file,
#' then this should be the path to the output file.
#'
#' @return This function writes to the connected required a matrix in nexus
#' format for a morphological dataset (that is, datatype=standard).
#'
#' @author Gustavo A. Ballen
#'
#' @details This function will concatenate matrices in nexus format (mandatory) and write to the disk the output and summary information on the partitions. It requires that the input matrices all share the same taxa in the same positions.
#'
#' @examples
#' \dontrun{
#' # this will return the matrix to the console rather than to a file
#' table2nexus(path="../prototipos/morpho.csv", datatype="standard", header=FALSE, sep=",")
#' }
#' @export
#' @importFrom utils read.delim

table2nexus <- function(path, datatype = c("standard", "dna", "rna", "protein"), header = FALSE, sep=",", con=stdout()) {
DATATYPE <- toupper(match.arg(datatype))
df <- read.delim(path, sep=sep, header=header)
if (header) {
colnames(df) <- NULL
}
taxa <- df[[1]]
NTAX <- length(taxa)
df <- df[, -1]
NCHAR <- ncol(df)
chars <- apply(X=df, MARGIN=1, FUN=function(x) paste(x, collapse=""))
NEXUSheader <- c("#NEXUS",
"[Data written by table2nexus, _TIMESTAMP]",
"BEGIN DATA;",
" DIMENSIONS NTAX=_NTAX NCHAR=_NCHAR",
" FORMAT DATATYPE=_DATATYPE MISSING=? GAP=- INTERLEAVE=NO;",
" MATRIX")
NEXUSheader <- gsub(pattern="_TIMESTAMP", replacement=Sys.time(), x=NEXUSheader)
NEXUSheader <- gsub(pattern="_DATATYPE", replacement=DATATYPE, x=NEXUSheader)
NEXUSheader <- gsub(pattern="_NTAX", replacement=NTAX, x=NEXUSheader)
NEXUSheader <- gsub(pattern="_NCHAR", replacement=NCHAR, x=NEXUSheader)
DATA <- paste(" ", taxa, " ", chars, sep="")
OUTPUT <- c(NEXUSheader, DATA, " ;", "END;")
writeLines(OUTPUT)
}

53 changes: 53 additions & 0 deletions man/table2nexus.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cef4e9

Please sign in to comment.