[Coding Challenge] Complementary DNA
Hello community,
Yesterday was celebrated the "World Medical Ethics Day" https://www.ama.com.au/media/wma-medical-ethics-day
We could celebrate it with a programming quiz or challenge:
DESCRIPTION:
Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.If you want to know more: http://en.wikipedia.org/wiki/DNA
In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". Your function receives one side of the DNA (string, except for Haskell); you need to return the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).
More similar exercise are found here: http://rosalind.info/problems/list-view/ (source)
Example: (input --> output)
"ATTGC" --> "TAACG" "GTAT" --> "CATA"
Satement extracted from: https://www.codewars.com/kata/554e4a2f232cdd87d9000038/java
Test cases:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class DnaStrandTest {
@Test
public void test01() {
assertEquals("TTTT", DnaStrand.makeComplement("AAAA"));
}
@Test
public void test02() {
assertEquals("TAACG", DnaStrand.makeComplement("ATTGC"));
}
@Test
public void test03() {
assertEquals("CATA", DnaStrand.makeComplement("GTAT"));
}
@Test
public void test04() {
assertEquals("TTCC", DnaStrand.makeComplement("AAGG"));
}
@Test
public void test05() {
assertEquals("GCGC", DnaStrand.makeComplement("CGCG"));
}
@Test
public void test06() {
assertEquals("TAACG", DnaStrand.makeComplement("ATTGC"));
}
@Test
public void test07() {
assertEquals("CATAGCTAGCTAGCTAGCTAATATAAAAGCTGCTCTAAATTTATATATATATATGCTCTCTTATGTCTATCTGTCTAAT", DnaStrand.makeComplement("GTATCGATCGATCGATCGATTATATTTTCGACGAGATTTAAATATATATATATACGAGAGAATACAGATAGACAGATTA"));
}
}
🌐🕸📔 Would you be able to code a solution for this?
Comments
As a reference a the Java solution:
Could be:
import java.util.*;
public class DnaStrand {
static final Map<String,String> letters = Map.ofEntries(Map.entry("A","T"),Map.entry("T","A"),Map.entry("G","C"),Map.entry("C","G"));
public static String makeComplement /*🧬🔁🧬*/ (String dna) {
StringBuilder result = new StringBuilder();
for(String letter:dna.split("")){
result.append(letters.get(letter));
}
return result.toString();
}
}Would you like to improve this in ObjectScript?
Are you able?
Hmm, ... will be difficult, but I give it a try
ClassMethod DNA(dna) As%String
{
q$tr(dna,"CGAT","GCTA")
}
Hard for any language to beat:
ClassMethod makeComplement(d As %String) As %String
{
q $tr(d,"ATGC","TACG")
}ClassMethod makeComplement(dna As %String) As %String [language = python]
{
return dna.translate(dna.maketrans("CGAT","GCTA"))
}Great spot @Timothy Leavitt
Extra syntax sugar, can project as method expression to make more transparently embedded.
ClassMethod makeComplement(d As %String) As %String [CodeMode = expression]{
$tr(d,"ATGC","TACG")}Also I believe Java lacks convenience of compile time ObjectScript macros:
#define DNAComplement(%dna) $tr(%dna,"ATGC","TACG")Write $$$DNAComplement("ATC")