Skip to content

Instantly share code, notes, and snippets.

@acvill
Last active May 25, 2023 17:05
Show Gist options
  • Save acvill/63172ea4c028a80c6f98b02f4133784a to your computer and use it in GitHub Desktop.
Save acvill/63172ea4c028a80c6f98b02f4133784a to your computer and use it in GitHub Desktop.
given a nucleotide sequence, generate an index-color string recognized by forna
# requires the native pipe from R >= 4.1
## maps each base to a particular color
## colors can be named or hex values
## outputs a format recognized by the forna webapp's custom color editor
## http://rna.tbi.univie.ac.at/forna/forna.html
forna_colors <- function(sequence, colormap) {
inseq <- gsub(x = strsplit(toupper(as.character(sequence)),split="")[[1]],
pattern="T",
replacement="U")
index <- paste0(1:length(inseq),":")
c(rbind(index,inseq)) |>
gsub(pattern = names(colormap[1]), replacement = paste0(tolower(colormap[1])," ")) |>
gsub(pattern = names(colormap[2]), replacement = paste0(tolower(colormap[2])," ")) |>
gsub(pattern = names(colormap[3]), replacement = paste0(tolower(colormap[3])," ")) |>
gsub(pattern = names(colormap[4]), replacement = paste0(tolower(colormap[4])," ")) |>
paste0(collapse = "") |> trimws() |> noquote()
}
# example
seq <- "GGCCCCATGGTCAAGCGGTTAAGACATCGCCCTTTCACGGCGGTAACACGGGTTCAAATCCCGTTGGGGTCA"
colors <- c(A = "yellow", G = "red", C = "green", U = "blue")
colors_hex <- c(A = "#5BB57E", G = "#F8C966", C = "#668DB7", U = "#E26674")
forna_colors(seq, colors)
forna_colors(seq, colors_hex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment