Skip to content

Commit 8755b96

Browse files
committed
X and Y to S and T
1 parent 0a73054 commit 8755b96

File tree

2 files changed

+120
-15
lines changed

2 files changed

+120
-15
lines changed

lectures/DNA-alignment.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,111 @@ score for the rest, then add in the score for the current choice.
146146

147147
Return the max of all the choices.
148148

149+
150+
```
151+
Result align(String S, String T) {
152+
if (S.length() == 0 && T.length() == 0) {
153+
return Result(0);
154+
} else if (S.length() == 0) {
155+
rest = align(S, T.substring(0, T.length()-1));
156+
return Result(rest.score - 1, Direction.LEFT)
157+
} else if (T.length() == 0) {
158+
rest = align(S.substring(0, S.length()-1), T);
159+
return Result(rest.score - 1, Direction.UP)
160+
} else {
161+
char s = S.charAt(S.length() - 1);
162+
char t = T.charAt(T.length() - 1);
163+
// option 1: line up
164+
rest1 = align(S.substring(0, S.length()-1),
165+
T.substring(0, T.length()-1));
166+
option1 = rest1.score + match(s, t);
167+
best_score = option1;
168+
best_direction = DIAGONAL;
169+
// option 2: gap on top (S)
170+
rest2 = align(S, T.substring(0, T.length()-1));
171+
option2 = rest2.score - 1;
172+
if (option2 > best_score) {
173+
best_score = option2;
174+
best_direction = LEFT;
175+
}
176+
// option 3: gap on bottom (T)
177+
rest3 = align(S.substring(0, S.length()-1), T);
178+
option3 = rest3.score - 1;
179+
if (option3 > best_score) {
180+
best_score = option3;
181+
best_direction = UP;
182+
}
183+
return Result(best_score, best_direction);
184+
}
185+
}
186+
```
187+
188+
Memoize
189+
```
190+
void put_2D(HashMap<String, HashMap<String, Result> > R,
191+
String S, String T) {
192+
if (R.contains(S)) {
193+
R.get(S).put(T, result);
194+
} else {
195+
R.put(S, new HashMap<String,Result>());
196+
R.get(S).put(T, result);
197+
}
198+
}
199+
200+
201+
Result align(String S, String T,
202+
HashMap<String, HashMap<String, Result> > R)
203+
{
204+
if (R.contains(S) and R.get(S).contains(T)) {
205+
return R.get(S).get(T);
206+
}
207+
208+
if (S.length() == 0 && T.length() == 0) {
209+
result = Result(0);
210+
put_2D(R, S, T);
211+
return result;
212+
} else if (S.length() == 0) {
213+
rest = align(S, T.substring(0, T.length()-1), R)
214+
result = Result(rest.score - 1, Direction.LEFT);
215+
put_2D(R, S, T);
216+
return result;
217+
} else if (T.length() == 0) {
218+
rest = align(S.substring(0, S.length()-1), T, R);
219+
result = Result(rest.score - 1, Direction.UP)
220+
put_2D(R, S, T);
221+
return result;
222+
} else {
223+
char s = S.charAt(S.length() - 1);
224+
char t = T.charAt(T.length() - 1);
225+
// option 1: line up
226+
rest1 = align(S.substring(0, S.length()-1),
227+
T.substring(0, T.length()-1), R);
228+
option1 = rest1.score + match(s, t);
229+
best_score = option1;
230+
best_direction = DIAGONAL;
231+
// option 2: gap on top (S)
232+
rest2 = align(S, T.substring(0, T.length()-1), R);
233+
option2 = rest2.score - 1;
234+
if (option2 > best_score) {
235+
best_score = option2;
236+
best_direction = LEFT;
237+
}
238+
// option 3: gap on bottom (T)
239+
rest3 = align(S.substring(0, S.length()-1), T, R);
240+
option3 = rest3.score - 1;
241+
if (option3 > best_score) {
242+
best_score = option3;
243+
best_direction = UP;
244+
}
245+
result = Result(best_score, best_direction);
246+
put_2D(R, S, T);
247+
return result;
248+
}
249+
}
250+
```
251+
252+
253+
149254
## Subproblem identification
150255

151256
Instead of using the entire rest of S and T as the inputs to the

lectures/more-dynamic-programming.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Here's the resulting table, which contains the solution for how to
1212
best align the two strings and every pair of prefixes of them.
1313

1414

15-
Y = G G A
15+
T = G G A
1616
j = 0 | 1 | 2 | 3
1717
--------------------------
18-
X i = 0 | 0 |I:-1 |I:-2 |I:-3
18+
S i = 0 | 0 |I:-1 |I:-2 |I:-3
1919
G 1 |D:-1 |M:2 |M:1 |I:0
2020
A 2 |D:-2 |D:1 |M:0 |M:3
2121
A 3 |D:-3 |D:0 |I:-1 |D:2
@@ -28,11 +28,11 @@ To obtain the best alignment, start at the bottom-right corner
2828
(row 3, column 3) and consult the choice that was made.
2929

3030
The choice was D (delete), so that corresponds to aligning
31-
the last letter in X with a gap:
31+
the last letter in S with a gap:
3232

3333
Partial Solution Remaining Strings
34-
X A GA
35-
Y _ GGA
34+
S A GA
35+
T _ GGA
3636

3737
To get the rest of the solution, we move up one cell because the
3838
choice was D (delete).
@@ -42,33 +42,33 @@ corresponds to aligning the last letter of each of the remaining
4242
strings (A and A).
4343

4444
Partial Solution Remaining Strings
45-
X AA G
46-
Y A_ GG
45+
S AA G
46+
T A_ GG
4747

4848
To get the rest of then solution, we move diagonally up and left
4949
because the choice was M (match/mismatch).
5050

5151
The choice in row 1, column 2 was M (match/mismatch).
5252

5353
Partial Solution Remaining Strings
54-
X GAA
55-
Y GA_ G
54+
S GAA
55+
T GA_ G
5656

5757
Again we move diagonally up and left.
5858

5959
The choice in row 0, column 1 is I (insert), so we align
60-
the letter G from Y with a gap.
60+
the letter G from T with a gap.
6161

6262
Partial Solution Remaining Strings
63-
X _GAA
64-
Y GGA_
63+
S _GAA
64+
T GGA_
6565

6666
Because we did an insert, we move to the left one cell.
6767

68-
So the best alignment of the strings X and Y is:
68+
So the best alignment of the strings S and T is:
6969

70-
X _GAA
71-
Y GGA_
70+
S _GAA
71+
T GGA_
7272
-1 2 2 -1 = 2
7373

7474
Summary:

0 commit comments

Comments
 (0)