R/vcf2SNAPP.R

Defines functions vcf2SNAPP

Documented in vcf2SNAPP

#' @title Convert vcfR object to SNAPP format data..
#' @description The function converts vcfR format data to SNAPP nexus format.
#' @param vcf a vcfR object.
#' @param outfile name of output file.
#' @return SNAPP nexus format file.
#'
#' @details
#' This function converts the vcfR object to a SNAPP nexus format file.
#'
#'
#' @export

vcf2SNAPP <- function(vcf, file = "snapp.nex") {

  vcf <- extract.indels(vcf)
  vcf <- vcf[is.biallelic(vcf),]

  gt.filtered <- extract.gt(vcf, element = "GT", as.numeric = T, convertNA = T)

  gt.filtered[is.na(gt.filtered)] <- "?"

  gt.filtered <- t(gt.filtered)

  ape::write.nexus.data(gt.filtered, file)

  snapp.file <- scan(file, what = "character", sep = "\n",
                     quiet = TRUE)

  bgn <- grep("BEGIN", snapp.file)
  snapp.file[bgn] <- "BEGIN CHARACTERS;"
  fmt <- grep("FORMAT", snapp.file)
  snapp.file[fmt] <- "  FORMAT DATATYPE=INTEGER MISSING=? GAP=- SYMBOLS=\"012\" LABELS=LEFT TRANSPOSE=NO INTERLEAVE=NO;"

  #return(snapp.file)
  write(snapp.file, file)

}
shankarkshakya/mypackage documentation built on March 12, 2021, 5:54 p.m.