-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSequenceUtils.java
More file actions
209 lines (172 loc) · 7.26 KB
/
Copy pathSequenceUtils.java
File metadata and controls
209 lines (172 loc) · 7.26 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package alg.sequence;
import alg.math.MathUtils;
import alg.misc.InterestingAlgorithm;
import alg.misc.MiscUtils;
import alg.strings.StringUtils;
import java.util.HashMap;
/**
* Created with IntelliJ IDEA.
* User: jsanchez
* Date: 12/5/14
* Time: 5:38 PM
* To change this template use File | Settings | File Templates.
*/
public class SequenceUtils {
/**
*
* TODO: Hirschberg's algorithm for optimal sequence alignment
*
*/
@InterestingAlgorithm(timeComplexity = "O(n)", spaceComplexity = "O(1)")
public static int hammingDistance (String s1, String s2) {
int s1Len = s1 == null ? 0 : s1.length();
int s2Len = s2 == null ? 0 : s2.length();
if (s1Len != s2Len)
throw new RuntimeException("Cannot compute hamming distance on different length strings: " + s1 + " and " + s2);
int distance = 0;
for (int i = 0; i < s1Len; i++) {
if (s1.charAt(i) != s2.charAt(i))
distance++;
}
return (distance);
}
public static EditDistanceCostComputer defaultCostComputer = new EditDistanceCostComputer();
public static int [] needlemanWunschScore (String x, String y) {
return (needlemanWunschScore(x, y, defaultCostComputer));
}
@InterestingAlgorithm(timeComplexity = "O(m * n)", spaceComplexity = "O(m * n)")
public static int [] needlemanWunschScore (String x, String y, EditDistanceCostComputer costComputer) {
int xLen = x == null ? 0 : x.length();
int yLen = y == null ? 0 : y.length();
// TODO: optimize to only use two vectors!
int [][] scores = new int[xLen][yLen];
scores [0][0] = 0;
// TODO: is this < or <=?
for (int j = 1; j < y.length(); j++) {
scores [0][j] = scores [0][j - 1] + costComputer.insertionCost(y.charAt(j));
}
for (int i = 1; i < x.length(); i++) {
scores [i][0] = scores [i - 1][0] + costComputer.deletionCost(x.charAt(i));
for (int j = 1; j < y.length(); j++) {
int scoreSub = scores [i -1][j - 1] + costComputer.substitutionCost(x.charAt(i), y.charAt(j));
int scoreDel = scores [i - 1][j] + costComputer.deletionCost(x.charAt(i));
int scoreIns = scores [i][j - 1] + costComputer.insertionCost(y.charAt(j));
scores [i][j] = MathUtils.max(new int [] {scoreSub, scoreDel, scoreIns});
}
}
int [] lastLine = new int[y.length()];
for (int j = 0; j < yLen; j++) {
lastLine [j] = scores [xLen - 1][j];
}
return (lastLine);
}
@InterestingAlgorithm(timeComplexity = "O(m * n)", spaceComplexity = "O(min(m, n))")
public static String [] hirschbergOptimalAlignment (String x, String y) {
String [] ret = new String [] {"", ""};
int xLen = x == null ? 0 : x.length();
int yLen = y == null ? 0 : y.length();
if (xLen == 0) {
for (int i = 0; i < yLen; i++) {
ret [0] += '-';
ret [1] += y.charAt(i);
}
}
else if (yLen == 0) {
for (int i = 0; i < xLen; i++) {
ret [0] += x.charAt(i);
ret [1] += '-';
}
}
else if (xLen == 1 && yLen == 1) {
//ret = needlemanWunsch(x, y); TODO
}
else {
int xlen = x.length();
int xmid = xlen / 2;
int ylen = y.length();
// TODO: to work space efficiently, we need to not make copies of substrings of the main strings:
String firstHalfX = x.substring(0, xmid);
int [] scoreL = needlemanWunschScore(firstHalfX, y);
String secondHalfX = x.substring(xmid);
int [] scoreR = needlemanWunschScore(StringUtils.reverse(secondHalfX), StringUtils.reverse(y));
int ymid = partition(scoreL, scoreR);
String firstHalfY = x.substring(0, ymid);
ret = hirschbergOptimalAlignment(firstHalfX, firstHalfY);
String secondHalfY = x.substring(ymid);
String [] second = hirschbergOptimalAlignment(secondHalfX, secondHalfY);
ret [0] += second [0];
ret [1] += second [1];
}
return (ret);
}
public static int partition (int [] scoreL, int [] scoreR) {
// TODO: return "arg max of scoreL + reverse(scoreR) whatever that means
int [] scoreLAndReverseOfScoreR = new int[scoreL.length];
for (int i = 0; i < scoreLAndReverseOfScoreR.length; i++) {
scoreLAndReverseOfScoreR [i] = scoreL [i] + scoreR [i];
}
// perhaps this is what "arg max" means in this context?
Integer max = null;
int maxIdx = -1;
for (int i = 0; i < scoreLAndReverseOfScoreR.length; i++) {
if (max == null || scoreLAndReverseOfScoreR [i] > max) {
max = scoreLAndReverseOfScoreR [i];
maxIdx = i;
}
}
return (maxIdx);
}
public static int levenshteinDistanceRec (String s1, String s2) {
return (levenshteinDistanceRec(s1, s1 == null ? 0 : s1.length(), s2, s2 == null ? 0 : s2.length()));
}
@InterestingAlgorithm(timeComplexity = "O(3^(m+n))", spaceComplexity = "O(m + n)")
public static int levenshteinDistanceRec (String s1, int s1Len, String s2, int s2Len) {
if (s1Len == 0)
return (s2Len);
if (s2Len == 0)
return (s1Len);
int cost;
if (s1.charAt(s1Len - 1) == s2.charAt(s2Len - 1))
cost = 0;
else
cost = 1;
return (MathUtils.min(new int[] {
levenshteinDistanceRec(s1, s1Len - 1, s2, s2Len) + 1,
levenshteinDistanceRec(s1, s1Len, s2, s2Len - 1) + 1,
levenshteinDistanceRec(s1, s1Len - 1, s2, s2Len - 1) + cost
}));
}
/**
* the iterative dynamic programming version for computing edit distance
* @param s1
* @param s2
* @return
*/
@InterestingAlgorithm(timeComplexity = "O(m * n)", spaceComplexity = "O(n)")
public static int levenshteinDistance (String s1, String s2) {
if (MiscUtils.safeEquals(s1, s2))
return (0);
int s1Len = s1 == null ? 0 : s1.length();
int s2Len = s2 == null ? 0 : s2.length();
if (s1Len == 0)
return (s2Len);
if (s2Len == 0)
return (s1Len);
int [] v0 = new int[s2.length() + 1];
int [] v1 = new int[s2.length() + 1];
for (int i = 0; i < v0.length; i++)
v0 [i] = i;
for (int i = 0; i < s1.length(); i++) {
v1 [0] = i + 1;
for (int j = 0; j < s2.length(); j++) {
int cost = s1.charAt(i) == s2.charAt(j) ? 0 : 1;
v1[j + 1] = MathUtils.min(new int[] {
v1[j] + 1, v0[j + 1] + 1, v0[j] + cost
});
}
for (int j = 0; j < v0.length; j++)
v0 [j] = v1[j];
}
return (v1[s2.length()]);
}
}