REVC: Complementing a Strand of DNA | Ben Cunningham

REVC: Complementing a Strand of DNA

Problem by Rosalind · on July 1, 2012

In DNA strings, symbols ‘A’ and ‘T’ are complements of each other, as are ‘C’ and ‘G’.

The reverse complement of a DNA string is the string formed by reversing the symbols of , then taking the complement of each symbol (e.g., the reverse complement of “GTCA” is “TGAC”).

Given: A DNA string of length at most 1000 bp.

Return: The reverse complement of .

Sample Dataset

AAAACCCGGT

Sample Output

ACCGGGTTTT

R

library(dplyr)

f <- "revc.txt"

dna <- data_frame(
  s =
    readLines(f) %>%
    strsplit(split = "") %>%
    unlist(),
  c = case_when(
    s == "A" ~ "T",
    s == "C" ~ "G",
    s == "G" ~ "C",
    s == "T" ~ "A"
  )
)

rev(dna$c) %>%
  cat(sep = "")
ACCGGGTTTT