inst/extcode/getPWM.R

#' Generate position weight matrix
#'
#' @docType methods
#' @name getPWM
#' @rdname getPWM
#'
#' @param sequences A vector of sequences.
#'
#' @return PWM (position weight matrix).
#'
#' @author Wubing Zhang
#'
#' @export
getPWM <- function(sequences){
  tmp = matrix(unlist(strsplit(sequences, "")), ncol = nchar(sequences)[1], byrow = TRUE)
  AAs = unique(as.character(tmp))
  pwm = apply(tmp, 2, function(x) table(x)[AAs])
  rownames(pwm) = AAs
  colnames(pwm) = 1:nchar(sequences)[1]
  pwm[is.na(pwm)] = 0
  pwm = pwm / colSums(pwm)
  return(pwm)
}
WubingZhang/PhageR documentation built on July 2, 2019, 9:03 p.m.