R/NucleoTranslate.R

#' Nucleotide Sequence Translation
#' Translate nucleic acid sequence to corresponding peptide sequences.
#'
#' @docType methods
#' @name NucleoTranslate
#' @rdname NucleoTranslate
#'
#' @param sequence A character, specifying the nucleic acid sequence.
#'
#' @return A character of peptide sequences, in which 'X' means stop codon.
#'
#' @examples
#' sequence = "AATGAGGTTGGTGCGGAGTTTCATCCTACTATGTTG"
#' NucleoTranslate(sequence)
#' @author Wubing Zhang
#' @export

NucleoTranslate <- function(sequence){
  # Read DNA codon table
  CodonPath = file.path(system.file("extdata", package = "PhageR"),
                        "DNACodonTable.gz")
  CodonFile = gzfile(CodonPath)
  CodonTable = read.table(CodonFile, sep = "\t", header = TRUE,
                          row.names = 1, stringsAsFactors = FALSE)

  # Separate DNA codons from sequence and translate
  sequence = gsub("(.{3})", "\\1 ", sequence)
  codons <- unlist(strsplit(sequence, " "))
  Peptide = matrix(CodonTable[codons, "SLC"], nrow = length(sequence), byrow = TRUE)
  Peptide[Peptide=="Stop"] = "X"
  Peptide = do.call(paste0, as.data.frame(Peptide))
  return(Peptide)
}
WubingZhang/PhageR documentation built on July 2, 2019, 9:03 p.m.