Skip to content

Commit

Permalink
small improvement, add unalign
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausVigo committed Dec 17, 2024
1 parent f32bb21 commit 0f944df
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions R/phyDat_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

7 changes: 5 additions & 2 deletions R/pml_generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions man/as.phyDat.Rd

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

0 comments on commit 0f944df

Please sign in to comment.