Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created July 16, 2025 09:10
Show Gist options
  • Save stephenturner/d0616ffe10353a089150c4c168f69366 to your computer and use it in GitHub Desktop.
Save stephenturner/d0616ffe10353a089150c4c168f69366 to your computer and use it in GitHub Desktop.
Example code and output for reverse complementing a DNA sequence with R

Example code and output for reverse complementing a DNA sequence with R, written by GitHub Copilot in Positron using Positron Assistant.

For more details, see the blog post at:
https://blog.stephenturner.us/p/positron-assistant-copilot-chat-agent.

# R function to reverse complement a DNA sequence
reverse_complement <- function(dna_sequence) {
  # Convert the DNA sequence to uppercase
  dna_sequence <- toupper(dna_sequence)

  # Define the complement mapping
  complement <- c(A = "T", T = "A", C = "G", G = "C")

  # Replace each base with its complement
  complemented_sequence <- sapply(
    strsplit(dna_sequence, "")[[1]],
    function(base) complement[base]
  )

  # Reverse the complemented sequence
  reversed_sequence <- rev(complemented_sequence)

  # Collapse the vector back into a string
  return(paste(reversed_sequence, collapse = ""))
}

# examples
# Example usage
dna_seq <- "ATCGTA"
rev_comp_seq <- reverse_complement(dna_seq)
print(rev_comp_seq) # Output: "TACGAT"
#> [1] "TACGAT"

# More examples
dna_seq2 <- "GCTAGC"
rev_comp_seq2 <- reverse_complement(dna_seq2)
print(rev_comp_seq2) # Output: "GCTAGC" -> "GCTAGC" reversed is "CGATCG"
#> [1] "GCTAGC"

# Vectorized example
dna_seqs <- c("ATCG", "GCTA", "TACG")
rev_comp_seqs <- sapply(dna_seqs, reverse_complement)
print(rev_comp_seqs) # Output: "CGAT", "TAGC", "GCAT"
#>   ATCG   GCTA   TACG 
#> "CGAT" "TAGC" "CGTA"

Created on 2025-07-16 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment