25
\$\begingroup\$

Given a sequence of the bases Adenine, Cytosine, Guanine and Thymine (encoded as ACGT), you're to produce an ASCII art representation of a corresponding double strand of DNA.

The strand will extend vertically. The left-hand strand is the one you are given as input. The right-hand strand will be its complement. For those not familiar with DNA, A is paired with T and C is paired with G. Furthermore, there is backbone structure on either side of the double strand which is identical for all bases. So if you were given the input TAGCAT the large-scale structure of the ASCII art would be:

BTAB
BATB
BGCB
BCGB
BATB
BTAB

where B represents the backbone. Now each of these letters stands for an entire molecule and you're to reproduce the actual molecular structure.

The Bases

Use the following templates1 for each of the bases (each one is displayed together with its complementary base and the two backbone molecules):

1Credits to Peter Taylor for helping out with the ASCII layout.

Adenine

 O    O
  \\ /
    P
   / \
--O   O
     /                                         |
    <            N      NH2 ..... O      *     |
     \         // \    /           \\   /      |
      +--O    //   ----             ----       |
      |   \   |   //  \\           /   \\      |
      |    >--N--<      N ...... HN      >  ---+
      |   /       \    /           \    /  /   |
      +---         N===             ---N--<    |
      |                            //      \   |
      |                           O         O--+
      |                                         \
      |                                          >
      |                                         /
                                               O   O--
                                                \ /
                                                 P
                                                / \\
                                               O    O

Cytosine

 O    O
  \\ /
    P
   / \
--O   O            NH2 ..... O      N
     /            /           \\   / \\        |
    <         ----             ----   \\    ---+
     \       //  \\           /   \\   |   /   |
      +--O  <      N ...... HN      >--N--<    |
      |   \  \    /           \    /       \   |
      |    >--N---             ===N         O--+
      |   /      \\           /                 \
      +---         O ..... H2N                   >
      |                                         /
                                               O   O--
                                                \ /
                                                 P
                                                / \\
                                               O    O

Guanine

 O    O
  \\ /
    P
   / \
--O   O
     /                                         |
    <            N      O ..... H2N            |
     \         // \   //           \           |
      +--O    //   ----             ----       |
      |   \   |   //   \           //  \\      |
      |    >--N--<      NH ...... N      >  ---+
      |   /       \    /           \    /  /   |
      +---         N===             ---N--<    |
      |                \           //      \   |
      |                 NH2 ..... O         O--+
      |                                         \
      |                                          >
      |                                         /
                                               O   O--
                                                \ /
                                                 P
                                                / \\
                                               O    O

Thymine

 O    O
  \\ /
    P
   / \
--O   O     *      O ..... H2N      N
     /       \   //           \    / \\        |
    <         ----             ----   \\    ---+
     \       //   \           //  \\   |   /   |
      +--O  <      NH ...... N      >--N--<    |
      |   \  \    /           \    /       \   |
      |    >--N---             ===N         O--+
      |   /      \\                             \
      +---         O                             >
      |                                         /
                                               O   O--
                                                \ /
                                                 P
                                                / \\
                                               O    O

Constructing the Double Strand

These repeat vertically, such that there are no gaps in the backbone structure. This means that the bounding boxes of these four templates will overlap.

The bottom end of the left and top end of the right backbone will connect to the O of an OH.

The free O at the top end of the left and bottom end of the right backbone will have a free bond going inwards, indicated by --.

Example ATG

 O    O--
  \\ /
    P
   / \
--O   O                                        OH
     /                                         |
    <            N      NH2 ..... O      *     |
     \         // \    /           \\   /      |
      +--O    //   ----             ----       |
      |   \   |   //  \\           /   \\      |
      |    >--N--<      N ...... HN      >  ---+
      |   /       \    /           \    /  /   |
      +---         N===             ---N--<    |
      |                            //      \   |
      |                           O         O--+
      |                                         \
      |                                          >
      |                                         /
 O    O                                        O   O--
  \\ /                                          \ /
    P                                            P
   / \                                          / \\
--O   O     *      O ..... H2N      N          O    O
     /       \   //           \    / \\        |
    <         ----             ----   \\    ---+
     \       //   \           //  \\   |   /   |
      +--O  <      NH ...... N      >--N--<    |
      |   \  \    /           \    /       \   |
      |    >--N---             ===N         O--+
      |   /      \\                             \
      +---         O                             >
      |                                         /
 O    O                                        O   O--
  \\ /                                          \ /
    P                                            P
   / \                                          / \\
--O   O                                        O    O
     /                                         |
    <            N      O ..... H2N            |
     \         // \   //           \           |
      +--O    //   ----             ----       |
      |   \   |   //   \           //  \\      |
      |    >--N--<      NH ...... N      >  ---+
      |   /       \    /           \    /  /   |
      +---         N===             ---N--<    |
      |                \           //      \   |
      |                 NH2 ..... O         O--+
      |                                         \
      |                                          >
      |                                         /
      OH                                       O   O--
                                                \ /
                                                 P
                                                / \\
                                             --O    O

More Examples:

Here are the MD5 hashes of several more examples (without extraneous leading or trailing spaces):

ATG      2e4a906c44a96fe84134bf4346adf11c (this is the above example)
C        e3648b8960967463784818c3eee57246
TTT      6028a90b05775905ef1a00e7a45463c5
TAGCAT   3b834d2b7b9adc4113ffabd52d354c41
GATTACA  a19463f965c641d071e07da59d64a418

Let me know if you think any of these are wrong.

If you're unsure how to check your results' hashes reliably, try this online MD5 generator. Make sure that there is no trailing line break.

Further Notes

You may use leading or trailing spaces as you see fit. Of course, if you use leading spaces it has to be the same amount in each line.

If I have made any mistakes in copying the chemical structure, the above templates are still normative for the purposes of this challenge.

You may write a function or a program which takes the input string as a parameter, command-line argument via STDIN or expects it to be stored in a variable. Write the resulting ASCII art to STDOUT.

This is code golf, so the shortest answer (in bytes) wins.

\$\endgroup\$
8
  • \$\begingroup\$ Amazing challenge! \$\endgroup\$
    – Ray
    Commented Aug 14, 2014 at 15:20
  • 9
    \$\begingroup\$ GATTACA should be required \$\endgroup\$
    – Kevin
    Commented Aug 14, 2014 at 17:14
  • 1
    \$\begingroup\$ @Kevin I even thought about that before, so I added it now. ;) I also fixed the hash for TTT because the string contained a trailing newline. \$\endgroup\$ Commented Aug 14, 2014 at 17:23
  • \$\begingroup\$ How do you get the md5 checksum ? I copied your ATG output and got different checksum. And different OS will get different checksums. You can try these with unix2dos, unix2mac.... \$\endgroup\$
    – Ray
    Commented Aug 14, 2014 at 19:54
  • \$\begingroup\$ @Ray I got them using Ruby's Digest::MD5.hexdigest() with Unix-style line endings. Also, none of them have a trailing new line. Paste it here - this online MD5 generator agrees with my hashes. \$\endgroup\$ Commented Aug 14, 2014 at 19:56

2 Answers 2

5
\$\begingroup\$

Python 3, 1008 bytes

Decomposite to smaller blocks and then compress using python's zlib and encode the binary data with asii85 encoding. Before compressing, the size is 629 and after compression and encoding, the size is 260.

Smaller blocks:

    N      NH2            NH2 ...   *      O .....      N      O ..     O    O              ---+        
  // \    /              /           \   //           // \   //          \\ /              /   |        
 //   ----           ----             ----           //   ----             P            --<    |        
 |   //  \\         //  \\           //   \          |   //   \           / \              \   |        
 N--<      N ..    <      N .....   <      NH ....   N--<      NH .    --O   O              O--+        
     \    /         \    /           \    /              \    /             /                   \       
      N===           N---             N---                N===             <                     >      
                        \\               \\                   \             \                   /       
                          O .....          O                   NH2           +--O              O   O--  
                                                                             |   \              \ /     
                                                                             |    >--            P      
                                                                             |   /              / \\    
                                                                             +---              O    O   

The program reads from STDIN. It may have trailing spaces at the end of each line and may have empty lines at the end.

import zlib,base64
D=input()
B=b'GasbU3tfFR$q,H5@dA9CAl@Bd?/ZcRUN$!!66MJ3&IN+RbL6[r)7K,3-8;3.^a\'7XZD_Lh;(7`.g>%[1,o(<9L\\neaPK"9^lCg7teknAd\\HXFNbL!)l/pG]YNpRS-C]sXR5A[A[#C&pnT;I-Q$Bj@n$L"ODZk8M_YcM#\\5PaLq3@UfJmfm[)$+#H,A\\B+b`mL9^OQ/cET-@`YRD_DJ6mXMD">9HHep\\%LnL8&\\G?fDdbs20%[J\'jMG[Qp'
B=[b.split('\n')for b in zlib.decompress(base64.a85decode(B)).decode().split('\n\n')]
C=dict(zip('/\\<>', '\\/><'))
a=[(6,14),(6,28),(10,40)]
b=[(4,12),(4,26),(6,40)]
P=[a,b,b,a]
H=[18,14,14,18]
J=''.join
R=range
L=len
F=[[' ']*54 for _ in R(5+18*L(D))]
e=enumerate
y=0
def t(b,p):
 for i,r in e(b):
  for j,c in e(r):
   if' '!=c:F[y+p[0]+i][p[1]+j]=c
O=['OH']
U=['--']
t(O,(4,47))
t(U,(0,7))
for i in map('ACTG'.index,D):
 a,b,c=P[i];t(B[4],(0,0));t(B[i],a);q=B[(i+2)%4];t([J(C.get(z,z)for z in l[::-1]).replace('2HN', 'H2N')for l in[r+' '*(max(map(L,q))-L(r))for r in q]],b);t(B[5],c)
 for j in R(y+13,y+H[i]):F[j][6]='|'
 for j in R(y+5,y+c[0]):F[j][47]='|'
 y+=H[i]
t(O,(0,6))
t(U,(4,45))
for l in F:print(J(l))

Checksum matched using this script

And here's the ungolfed version:

import zlib, base64

flip_char_map = dict(zip('/\\<>', '\\/><'))

def flip_char(c):
    return flip_char_map.get(c, c)

def pad(block):
    w = max(map(len, block))
    return [line + ' ' * (w - len(line)) for line in block]

def flip(block):
    return [''.join(map(flip_char, line[::-1])).replace('2HN', 'H2N') for line in pad(block)]

blocks = b'GasbU3tfFR$q,H5@dA9CAl@Bd?/ZcRUN$!!66MJ3&IN+RbL6[r)7K,3-8;3.^a\'7XZD_Lh;(7`.g>%[1,o(<9L\\neaPK"9^lCg7teknAd\\HXFNbL!)l/pG]YNpRS-C]sXR5A[A[#C&pnT;I-Q$Bj@n$L"ODZk8M_YcM#\\5PaLq3@UfJmfm[)$+#H,A\\B+b`mL9^OQ/cET-@`YRD_DJ6mXMD">9HHep\\%LnL8&\\G?fDdbs20%[J\'jMG[Qp'
blocks = [b.split('\n') for b in zlib.decompress(base64.a85decode(blocks)).decode().split('\n\n')]

poss = [
    [(6, 14), (6, 28), (10, 40)],
    [(4, 12), (4, 26), (6, 40)],
    [(4, 12), (4, 26), (6, 40)],
    [(6, 14), (6, 28), (10, 40)],
]

heights = [18, 14, 14, 18]

get_id = 'ACTG'.index
dna = input()
height = sum(heights[get_id(x)] for x in dna)
field = [[' '] * 54 for _ in range(height + 5)]

def put(block, pos):
    i, j = pos
    for di, row in enumerate(block):
        for dj, c in enumerate(row):
            if c != ' ': field[y + i + di][j + dj] = c

y = 0
put(['OH'], (4, 47))
put(['--'], (0, 7))
for p in dna:
    i = get_id(p)
    h = heights[i]
    pos = poss[i]
    put(blocks[4], (0, 0))
    put(blocks[i], pos[0])
    put(flip(blocks[(i + 2) % 4]), pos[1])
    put(blocks[5], pos[2])
    for j in range(y + 13, y + h):
        field[j][6] = '|'
    for j in range(y + 5, y + pos[2][0]):
        field[j][47] = '|'
    y += h
put(['OH'], (0, 6))
put(['--'], (4, 45))

for line in field: print(''.join(line).rstrip())

from hashlib import md5
import sys
result = '\n'.join(map(str.rstrip, map(''.join, field))).encode()
print(md5(result).hexdigest(), file=sys.stderr)
\$\endgroup\$
2
\$\begingroup\$

Perl 5 (510)

Perl's fine with null bytes, so please use the hexdump provided for running this.

This works by printing out different parts of the DNA strand, with parts being one or more lines. An O or H is appended to the top line of each component to ensure valid output.

Assumes input is in the variable $_.

Golfed version:

use Compress::Zlib;@f=(eval uncompress q+xœÍUËnà ¼ó{³””¬”slqäù |xšÅ<
MÔv¤(1b¼;Ì„]8× `?CÐË×ÂÜ/¥À—¼ÃÍ"6p!ÇvDÿ@k
‘®^ÝÀ
²>ÈB$ì¡ÌHBýi`ã\qþˆRn^‚¢6WéJ±íRCXÀ\Cj[­ëzÖKg-µ€˜*Rt®abš3‘ª¤°È†"]ÖSX‰G2ôœÂV<<#9_ÐŽG´8Oa3uE'ÇC%…¹—tLºšÂBCQ‹¡NÈ»*/
V×AÛVÔÖAr©KûÊhát°DÃÁZÿÁ>;       kM‚2(9áýIP
t'A”¿žg
q­= Š[~y̲ùNÇsNsŽŽGל:IÏqŠÙl)˜ùð
ì…çÎÁ«:eôu?<(-  é æ ŽRxNSÜAM1• —)š—%+);s/./$y.=$f[$u?8:9].$f[$q=(-65+ord$&)%15].($u++?"    O":H).$f[$q+1]/ge;($y.=$f[10])=~s/@/\\/g;print$y

Ungolfed version:

use Compress::Zlib;
# load parts to @f; see below for list of parts
@f=(eval uncompress q+ .... +);
# for each chracater in input ($_)
s/./
    # append either the one or two backbone molecules
    # then append most of the first line of the base, followed by an O or H
    # then append the rest of the base
    $y .= $f[$u?8:9] . $f[$q = (-65+ord$&)%15] . ($u++?"    O":H) . $f[$q+1]
/ge;
# append the last backbone molecule, then replace
#  @ with \
($y .= $f[10])=~s/@/\\/g;
print $y

(-65+ord$&)%15 conveniently results in A=>0, C=>2, T=>4, G=>6, which is perfect as the program needs two elements in the array for each letter.

The center part, top part, and bottom part are stored in indices 8-10 in that order.

List of Parts (uses @ instead of \ to avoid a ton of escaping):

'--O   O                                        O',
'
     /                                         |
    <            N      NH2 ..... O      *     |
     @         // @    /           @@   /      |
      +--O    //   ----             ----       |
      |   @   |   //  @@           /   @@      |
      |    >--N--<      N ...... HN      >  ---+
      |   /       @    /           @    /  /   |
      +---         N===             ---N--<    |
      |                            //      @   |
      |                           O         O--+
      |                                         @
      |                                          >
',
'--O   O            NH2 ..... O      N          O',
'
     /            /           @@   / @@        |
    <         ----             ----   @@    ---+
     @       //  @@           /   @@   |   /   |
      +--O  <      N ...... HN      >--N--<    |
      |   @  @    /           @    /       @   |
      |    >--N---             ===N         O--+
      |   /      @@           /                 @
      +---         O ..... H2N                   >
',
'--O   O     *      O ..... H2N      N          O',
'
     /       @   //           @    / @@        |
    <         ----             ----   @@    ---+
     @       //   @           //  @@   |   /   |
      +--O  <      NH ...... N      >--N--<    |
      |   @  @    /           @    /       @   |
      |    >--N---             ===N         O--+
      |   /      @@                             @
      +---         O                             >
',
'--O   O                                        O',
'
     /                                         |
    <            N      O ..... H2N            |
     @         // @   //           @           |
      +--O    //   ----             ----       |
      |   @   |   //   @           //  @@      |
      |    >--N--<      NH ...... N      >  ---+
      |   /       @    /           @    /  /   |
      +---         N===             ---N--<    |
      |                @           //      @   |
      |                 NH2 ..... O         O--+
      |                                         @
      |                                          >
',
'      |                                         /
 O    O                                        O   O--
  @@ /                                          @ /
    P                                            P
   / @                                          / @@
',
' O    O--
  @@ /
    P
   / @
',
'      |                                         /
      OH                                       O   O--
                                                @ /
                                                 P
                                                / @@
                                             --O    O'

Hexdump:

0000000 7375 2065 6f43 706d 6572 7373 3a3a 6c5a
0000010 6269 403b 3d66 6528 6176 206c 6e75 6f63
0000020 706d 6572 7373 7120 782b cd9c cb55 c36e
0000030 1020 f3bc 7b15 94b3 ac94 7394 716c 04e4
0000040 20f9 7c7f 9a78 3cc5 4d0a 76d4 28a4 6231
0000050 3bbc 84cc 385d 00d7 3f60 d043 d7cb dcc2
0000060 1c2f c0a5 08ee ba0f c2a4 bc97 cdc3 3622
0000070 2170 1004 76c7 ff44 6b40 910a 0216 ae01
0000080 8d5e 0fdd 0dc0 3eb2 42c8 ec24 cca1 481e
0000090 9042 08fd 8169 6012 5ce3 fe71 5288 5e6e
00000a0 0c82 36a2 e957 084a edb1 4352 c058 435c
00000b0 5b6a ebad d67a 4b10 2d67 80b5 2a98 108d
00000c0 1052 ae74 0c61 9a62 1b01 9133 a4aa c8b0
00000d0 2286 d65d 5853 4789 f432 c29c 3c56 233c
00000e0 1839 d05f 478e 38b4 614f 1e33 0775 2745
00000f0 c71e 2543 b985 1306 7497 ba4c c29a 4342
0000100 510e a18b 4e0e bbc8 082a 0715 0d2f d756
0000110 db41 5616 7fd4 41d6 a972 4b10 0efb 68ca
0000120 7fe1 b074 c344 1ec1 ff5a 1ec1 3b3e 1a09
0000130 6b09 824d 2832 e139 49fd 0f50 740a 4127
0000140 9411 9ebf 6704 710d 3dad 8a09 175b 797e
0000150 cc12 90b2 4ef9 73c7 4e0b 7311 8e8e d747
0000160 1b9c 493a 71cf d98a 296c f998 0df0 85ec
0000170 cee7 abc1 1a3a f465 3f75 283c 2d07 0907
0000180 a0e9 20e6 528e 7813 534e 41dc 314d 95c2
0000190 97a0 2917 979a 2b25 3b29 2f73 2f2e 7924
00001a0 3d2e 6624 245b 3f75 3a38 5d39 242e 5b66
00001b0 7124 283d 362d 2b35 726f 2464 2926 3125
00001c0 5d35 282e 7524 2b2b 223f 2020 2020 224f
00001d0 483a 2e29 6624 245b 2b71 5d31 672f 3b65
00001e0 2428 2e79 243d 5b66 3031 295d 7e3d 2f73
00001f0 2f40 5c5c 672f 703b 6972 746e 7924
00001fe
\$\endgroup\$
2
  • \$\begingroup\$ Can you just replace all the back slashes with the escaped version in one go? It's hard to judge the output when it is full of @. \$\endgroup\$ Commented Aug 15, 2014 at 4:27
  • \$\begingroup\$ @githubphagocyte s/@/\\/g does exactly that, prior to printing. The list of parts is merely present to show what the compressed data is. \$\endgroup\$
    – es1024
    Commented Aug 15, 2014 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.