R/revCompl.R

revCompl <- function(DNAseq, rev = TRUE, compl = TRUE) {
  if (is.character(DNAseq)){
    resultVect <- sapply(DNAseq, (function(seqString){
      mySeq <- toupper(seqString)
      if (rev == TRUE) {
        mySeq <- paste(sapply(1:nchar(mySeq), (function(i){
          pos <- nchar(mySeq) + 1 - i
          substr(mySeq, pos, pos)
        })), collapse = "")
      }
      if (compl == TRUE) {
        mySeq <- paste(sapply(1:nchar(mySeq), (function(i){
          aBase <- substr(mySeq, i, i)
          complBase <- c("A","T","C","G","N", "A")
          names(complBase) <- c("T","A","G","C","N","U")
          returnBase <- complBase[aBase]
          if(is.na(returnBase)){ stop("Bad input")}
          returnBase
        })), collapse = "")
      }
      return(mySeq)
    }))
    names(resultVect) <- NULL
    return(resultVect)
  } else {
    warning("Bad input")
  }
}


mySequences <- c("AGGTAAC", "UUGUCCAUA", "CCCANNNNTGGG")
revCompl(mySequences)
dami82/damitools documentation built on May 14, 2019, 3:33 p.m.