R/PhyIntoNex.R

Defines functions PhyIntoNex

Documented in PhyIntoNex PhyIntoNex

#' Utility for turning TOAST superalign.txt into Nexus file
#'
#'
#' @author Alex Dornburg, \email{dornburgalex@@gmail.com}
#' @keywords toast nexus phylip alignment
#' @param filename defaults to superalign.txt
#' @export
#' @return exports an alignment in classic phyllip format into a nexus file. This is designed for the superalign file generated by toast but will work on any alignment file formatted the same way. 
#' @examples
#' PhyIntoNex(filename = "superalign.txt")

PhyIntoNex<-function(filename="superalign.phy", outputName="superalign.nex"){
    #get the first line
    first_line <- readLines(filename,n=1)
    #get the sites
    sites<-sub(".*\t","", first_line)
    #get the taxa
    taxa<-sub("\t.*","", first_line)
    remove_first_line<-paste("sed '1d'", filename,">", sep=" ")
    newfile<-outputName
    remove_first_lineb<-paste(remove_first_line, "temp", sep=" ")
    system(remove_first_lineb)
    nexus_tax<-paste("NTAX=",taxa, sep="")
    nexus_sites<-paste("NCHAR=",sites, sep="")
    Line3 = paste("DIMENSIONS",nexus_tax,nexus_sites,";", sep=" " )
    cat("#NEXUS","BEGIN DATA;", Line3,"FORMAT DATATYPE = DNA GAP = - MISSING = ?;","MATRIX", file= newfile, sep="\n", append="TRUE")
    makenewfile<-paste("cat", "temp",">>", newfile, sep=" ")
    system(makenewfile)
    system("rm temp")
    cat(";","end;",file= newfile,sep="\n", append=TRUE)
}
carolinafishes/toast documentation built on Dec. 19, 2024, 12:47 a.m.