From 6e7fa24a178048d007dd6f96a9467be846b8066c Mon Sep 17 00:00:00 2001 From: Klaus Schliep Date: Sat, 12 Oct 2024 14:47:21 +0200 Subject: [PATCH] improved output of write.pml, allow serialization. --- R/pml_generics.R | 27 ++++++++++++++++++++++++--- man/write.pml.Rd | 5 ++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/R/pml_generics.R b/R/pml_generics.R index 9aab4a05..07d3c159 100644 --- a/R/pml_generics.R +++ b/R/pml_generics.R @@ -132,6 +132,8 @@ print.pml <- function(x, ...) { #' #' @param x an object of class ancestral. #' @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 ... 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}} @@ -141,15 +143,34 @@ print.pml <- function(x, ...) { #' write.pml(fit, "woodmouse") #' unlink(c("woodmouse_pml.txt", "woodmouse_tree.nwk")) #' @export -write.pml <- function(x, file=tempfile(), ...){ +write.pml <- function(x, file="pml", save_rds=TRUE, ...){ digits <- -1 if (hasArg("digits")) digits <- list(...)$digits write.tree(x$tree, file=paste0(file, "_tree.nwk")) - saveRDS(x, file=paste0(file, ".rds")) + if(save_rds) saveRDS(x, file=paste0(file, ".rds")) + else write.phyDat(x$data, file=paste0(file, "_align.fasta"), format="fasta") if(!is.null(x$bs)) write.nexus(x$bs, file=paste0(file, "_bs.nex"), digits=digits) sink(paste0(file, "_pml.txt")) - print.pml(x) + cat("phangorn", packageDescription("phangorn", fields = "Version"), "\n\n") + print(x) + cat("\n\n") + cat("You can (re-)create the pml object using:\n\n") + if(save_rds){ + cat("fit <- readRDS(\"", file,".rds\")", sep="") + } + else { + call <- x$call + call$data <- "align" + cat("tree <- read.tree(\"", file, "_tree.nwk\")\n", sep="") + cat("align <- read.fasta(\"", file, "_align.fasta\", format=\"fasta\")", + sep="") + cat( "\nfit <- ") + print(call) + } + cat("\n\nREFERENCES\n\n") + cat("To cite phangorn please use:\n\n") + print(citation("phangorn") [[1]], style="text") sink() invisible(x) } diff --git a/man/write.pml.Rd b/man/write.pml.Rd index a7c1a285..7c81886d 100644 --- a/man/write.pml.Rd +++ b/man/write.pml.Rd @@ -4,13 +4,16 @@ \alias{write.pml} \title{Export pml objects} \usage{ -write.pml(x, file = tempfile(), ...) +write.pml(x, file = "pml", save_rds = TRUE, ...) } \arguments{ \item{x}{an object of class ancestral.} \item{file}{a file name. File endings are added.} +\item{save_rds}{logical, if TRUE saves the pml object as a rds file, +otherwise the alignment is saved as a fasta file.} + \item{...}{Further arguments passed to or from other methods.} } \value{