Skip to content

Instantly share code, notes, and snippets.

@KevinDaSilvaS
Last active February 1, 2023 23:35
Show Gist options
  • Save KevinDaSilvaS/fe6057b96313a6e9edc110d0ee9aa07c to your computer and use it in GitHub Desktop.
Save KevinDaSilvaS/fe6057b96313a6e9edc110d0ee9aa07c to your computer and use it in GitHub Desktop.
DNA -> RNA
Given a DNA strand, return its RNA complement (per RNA transcription).
Both DNA and RNA strands are a sequence of nucleotides.
The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).
The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).
Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:
G -> C
C -> G
T -> A
A -> U
defmodule RnaTranscription do
def to_rna(dna) do
to_string(dna)
|> String.split("", trim: true)
|> Enum.map(fn "G" -> "C"
"C" -> "G"
"T" -> "A"
"A" -> "U"
end)
|> Enum.join("")
|> to_charlist()
end
end
toRna [] = ""
toRna ('G':xs) = 'C':toRna xs
toRna ('C':xs) = 'G':toRna xs
toRna ('T':xs) = 'A':toRna xs
toRna ('A':xs) = 'U':toRna xs
function sequenceencoder(x)
if x == "G"
"C"
elseif x == "C"
"G"
elseif x == "T"
"A"
elseif x == "A"
"U"
end
end
function torna(dna)
splitedSequence = collect(eachsplit(dna, ""))
if first(splitedSequence) == ""
return ""
end
encodedseq = map(sequenceencoder, splitedSequence)
join(encodedseq)
end
const toRna = dna => {
const encoder = {
"G": "C",
"C": "G",
"T": "A",
"A": "U"
}
return dna.split("")
.map(x => encoder[x])
.reduce((acc, x) => `${acc}${x}`, "")
}
console.log(toRna("") == "")
console.log(toRna("A") == "U")
console.log(toRna("ACGTGGTCTTAA") == "UGCACCAGAAUU")
fn to_rna(dna string) string {
runes := dna.runes()
return runes.map(fn (x rune) string {
return match x {
`G` { "C" }
`C` { "G" }
`T` { "A" }
`A` { "U" }
else { "" }
}
}).join("")
}
function torna(dna)
encoder = Dict(
"G"=>"C",
"C"=>"G",
"T"=>"A",
"A"=>"U"
)
splitedSequence = collect(eachsplit(dna, ""))
encodedseq = map(x -> get(encoder, "$x", ""), splitedSequence)
join(encodedseq)
end
assert RnaTranscription.to_rna('') == ''
assert RnaTranscription.to_rna('G') == 'C'
assert RnaTranscription.to_rna('C') == 'G'
assert RnaTranscription.to_rna('T') == 'A'
assert RnaTranscription.to_rna('A') == 'U'
assert RnaTranscription.to_rna('ACGTGGTCTTAA') == 'UGCACCAGAAUU'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment