Golf Text into DNA
Jelly, 15 13 bytes
OVBs2UḄị“GCTA
Try it online! or verify all test cases.
How it works
OVBs2UḄị“GCTA Main link. Argument: s (string)
O Ordinal; replace each character with its code point.
V Eval. This converts the list to a string before evaluating, so it
returns the integer that results of concatenating all the digits.
B Binary; convert from integer to base 2.
s2 Split into chunks of length 2.
U Upend; reverse the digits of each chunk.
Reversing means that we would have to conditionally PREPEND a zero
to the last chunk, which makes no difference for base conversion.
Ḅ Unbinary; convert each chunk from base 2 to integer.
`UḄ' maps:
[0, 1 ] -> [1, 0] -> 2
[1, 0(?)] -> [0(?), 1] -> 1
[1, 1 ] -> [1, 1] -> 3
[0, 0(?)] -> [0(?), 0] -> 0
ị“GCTA Replace each number by the character at that index.
Indexing is 1-based, so the indices are [1, 2, 3, 0].
CJam, 24 23 bytes
Thanks to Dennis for saving 1 byte in a really clever way. :)
l:isi2b2/Wf%2fb"AGCT"f=
Test it here.
Explanation
Very direct implementation of the specification. The only interesting bit is the padding to an even number of zeros (which was actually Dennis's idea). Instead of treating the digits in each pair in the usual order, we make the second bit the most significant one. That means, ending in a single bit is identical to appending a zero to it, which means we don't have to append the zero at all.
l e# Read input.
:i e# Convert to character codes.
si e# Convert to flat string and back to integer.
2b e# Convert to binary.
2/ e# Split into pairs.
Wf% e# Reverse each pair.
2fb e# Convert each pair back from binary, to get a value in [0 1 2 3].
"AGCT"f= e# Select corresponding letter for each number.
Python 2, 109 103 bytes
lambda s,j=''.join:j('ACGT'[int(j(t),2)]for t in
zip(*[iter(bin(int(j(`ord(c)`for c in s))*2)[2:])]*2))
Test it on Ideone.