Cheer up, sweet, B'Genes!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{--

Straight-up listy problems.

These next set of problems (P08-P15 of the P99 problems) are of a set-piece, 
so I've run them together as one 'problem' here. ... and I have a bit of
a heartburn with the contrived examples ... why would I want to encode or
decode random strings of atoms?

Then I recalled a bit of exploratory programming I did back in 2003:

% This system takes a pool of nucleotide snippets and combines them
% into the requested amino acid string.

Heh.

We're not going to do that today, but we'll have a bit of fun messing up
some strings of ... elements of ... 'things.' Yeah. 'Things.'

So, encode/decode, pack/unpack

So, let's say you have the data type declaration: 

--}

data Base = A | C | G | T
   deriving (Show, Eq)

-- Exercise 1:

pack :: [Base] -> [[Base]]
pack bases = undefined

{--

pack groups repeated consecutive elements into a sublist, so:
    pack [A, A, A, A, C, G, G, A, A, C, T, T, T, T] 
         ~> [[A, A, A, A], [C], [G, G], [A, A], [C], [T, T, T, T]]
 --}

-- Exercise 2: unpack.

-- unpack is an euphemism for what standard library function name?
-- (that is: unpack can be defined in one word)

-- Exercise 3:

encode :: [Base] -> [(Int, Base)]
encode bases = undefined

{--

encode gives a run-length encoding of a list of bases. Consecutive
duplicate elements are encoded as the tuple (N, E), where N is the
number of duplicates and E is the base, so:

   encode [A, A, A, A, C, G, G, A, A, C, T, T, T, T]  
         ~> [(4, A), (1, C), (2, G), (2, A), (1, C), (4, T)]

 --}

-- Exercise 4:

decode :: [(Int, Base)] -> [Base]
decode encodedBases = undefined

{--

decode takes a run-length encoded list and gives the uncompressed version, so:

   decode [(4, A), (1, C), (2, G), (2, A), (1, C), (4, T)]
          ~> [A, A, A, A, C, G, G, A, A, C, T, T, T, T]

 --}

-- Exercise 5:

duplicate :: [Base] -> [Base]
duplicate bases = undefined

{--

duplicate duplicates the bases in a list, so:

   duplicate [A, C, G, T] ~> [A, A, C, C, G, G, T, T]

 --}

-- Exercise 6:

run :: [Base] -> Int -> [Base]
run bases n = undefined

{--

run is a generalization of duplicate, repeating (some would say 'replicating,'
... hm) the bases in the list n times:

   run [G, A, T] 3 ~> [G, G, G, A, A, A, T, T, T]

 --}

-- ... ANNNNDDD we're done! Have fun!

-- Bonus:

{-- 

... ALMOST done. ALMOST done. Here are some snippets to play with.

'Pretend' you have the following snippets defined in files somewhere
in your archive of projects:

kmspdw.snippets:
----------------
ccg
ga
gg
aagatgtct

etiwitwikngfmtr.snippets:
-------------------------
aacgga
ac
acccga
ca
cgatgttggattccagattatgcta
gagacgatt
gagagaccagaggaccctcga
gaaacgatgaggggaataa
gcgggtt
gg
ggtactg
tg
tgtttg
tggata
ttggattaaa

glue.snippets:
--------------
c
t
g
a
cc
tc
ct
gc
cg
ca
ac
tt
ta
at
tg
gt
gg
ga
ag
aa

Make the data type Base a Read instance. Read in each of the .snippets files
in turn to a flat list of bases and apply each of the functions you created
above against these bases. What are the results that you get?

A solution is available at http://lpaste.net/106938 : GATTACA
 --}