From 0f944dfba21c6bf9cf285935b87e7b96649523f5 Mon Sep 17 00:00:00 2001 From: Klaus Schliep Date: Tue, 17 Dec 2024 13:32:50 +0100 Subject: [PATCH] small improvement, add unalign --- R/phyDat_conversion.R | 22 ++++++++++++++++++++++ R/pml_generics.R | 7 +++++-- man/as.phyDat.Rd | 7 +++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/R/phyDat_conversion.R b/R/phyDat_conversion.R index d120e406..521af8c3 100644 --- a/R/phyDat_conversion.R +++ b/R/phyDat_conversion.R @@ -12,6 +12,10 @@ #' \code{acgt2ry} converts a \code{phyDat} object of nucleotides into an binary #' ry-coded dataset. #' +#' \code{unalign} converts a \code{phyDat} object of nucleotides or amino acids +#' into a \code{DNAbin} or \code{AAbin} object in list form removing all gaps. +#' These objects can be exported using \code{\link[ape]{write.FASTA}}. +#' #' @aliases #' as.phyDat.character as.phyDat.data.frame as.phyDat.matrix #' as.MultipleAlignment as.MultipleAlignment.phyDat @@ -367,3 +371,21 @@ acgt2ry <- function(obj){ levels = c("r", "y"), ambiguity = NULL) obj } + + +#' @rdname as.phyDat +#' @export +unalign <- function(x){ + if(inherits(x, "phyDat")){ + if(attr(x, "type")=="DNA") x<- as.DNAbin(x) + if(attr(x, "type")=="AA") x<- as.AAbin(x) + } + stopifnot(inherits(x, "DNAbin") || inherits(x, "AAbin")) + x <- as.list(x) + for(i in 1:length(x)){ + tmp <- as.character(x[[i]]) + x[[i]] <- x[[i]][tmp!="-"] + } + x +} + diff --git a/R/pml_generics.R b/R/pml_generics.R index 4d93eb88..cde082e2 100644 --- a/R/pml_generics.R +++ b/R/pml_generics.R @@ -134,6 +134,8 @@ print.pml <- function(x, ...) { #' @param file a file name. File endings are added. #' @param save_rds logical, if TRUE saves the pml object as a rds file, #' otherwise the alignment is saved as a fasta file. +## @param chi_sq logical, if TRUE performs $Chi^2$-test to check if sequences have similar +## state composition. #' @param ... Further arguments passed to or from other methods. #' @returns \code{write.pml} returns the input x invisibly. #' @seealso \code{\link{ancestral.pml}}, \code{\link{plotAnc}} @@ -144,7 +146,7 @@ print.pml <- function(x, ...) { #' unlink(c("woodmouse.txt", "woodmouse_tree.nwk", "woodmouse_align.fasta")) #' @importFrom utils citation #' @export -write.pml <- function(x, file="pml", save_rds=FALSE, ...){ +write.pml <- function(x, file="pml", save_rds=FALSE,...){ digits <- -1 if (hasArg("digits")) digits <- list(...)$digits write.tree(x$tree, file=paste0(file, "_tree.nwk")) @@ -162,7 +164,8 @@ write.pml <- function(x, file="pml", save_rds=FALSE, ...){ call$data <- quote(align) call$tree <- quote(tree) cat("tree <- read.tree(\"", file, "_tree.nwk\")\n", sep="") - cat("align <- read.phyDat(\"", file, "_align.fasta\", format=\"fasta\")", + type <- attr(x$data, "type") + cat("align <- read.phyDat(\"", file, "_align.fasta\", format=\"fasta\", type=\"", type,"\")", sep="") cat( "\nfit <- ") print(call) diff --git a/man/as.phyDat.Rd b/man/as.phyDat.Rd index d8e8a9d9..c721f3a5 100644 --- a/man/as.phyDat.Rd +++ b/man/as.phyDat.Rd @@ -25,6 +25,7 @@ \alias{as.DNAbin.phyDat} \alias{as.AAbin.phyDat} \alias{genlight2phyDat} +\alias{unalign} \title{Conversion among Sequence Formats} \usage{ phyDat(data, type = "DNA", levels = NULL, return.index = TRUE, ...) @@ -64,6 +65,8 @@ as.StringSet(x, ...) genlight2phyDat(x, ambiguity = NA) acgt2ry(obj) + +unalign(x) } \arguments{ \item{data}{An object containing sequences.} @@ -101,6 +104,10 @@ example for specifying "USER" defined data formats in the vignette \code{acgt2ry} converts a \code{phyDat} object of nucleotides into an binary ry-coded dataset. + +\code{unalign} converts a \code{phyDat} object of nucleotides or amino acids +into a \code{DNAbin} or \code{AAbin} object in list form removing all gaps. +These objects can be exported using \code{\link[ape]{write.FASTA}}. } \examples{