-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEditDistAligner.java
executable file
·163 lines (135 loc) · 5.02 KB
/
EditDistAligner.java
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
/* Copyright (C) <2013> University of Massachusetts Amherst
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author Ismet Zeki Yalniz
*/
import java.util.ArrayList;
public class EditDistAligner {
String[] reference;
String[] candidate;
int s1, e1, s2, e2;
ArrayList<AlignedSequence> alignment;
int[][] table;
int[][] trace;
final int MATCH = 4;
final int SUBSTITUTION = 3;
final int INSERTION = 2;
final int DELETION = 1;
int SUBSTITUTION_PENALTY = 2;
int INSERTION_PENALTY = 1;
int DELETION_PENALTY = 1;
public void setPenalties(int ins, int del, int sb){
SUBSTITUTION_PENALTY = sb;
INSERTION_PENALTY = ins;
DELETION_PENALTY = del;
}
// aligns subsegments of the input sequences
public ArrayList<AlignedSequence> align(String[] reference, String[] candidate, int s1, int e1, int s2, int e2) {
this.reference = reference;
this.candidate = candidate;
this.s1 = s1;
this.s2 = s2;
this.e1 = e1;
this.e2 = e2;
runEditDist();
alignment = backtrace();
return alignment;
}
private void runEditDist() {
int penalty;
int minPenalty;
int bestOp;
int m = e1-s1; // ref length
int n = e2-s2; // cand length
table = new int[m + 1][n + 1];
trace = new int[m + 1][n + 1];
table[0][0] = 0;
trace[0][0] = MATCH;
// init
for (int i = 1; i <= m; i++) {
table[i][0] = DELETION_PENALTY * i;
trace[i][0] = DELETION;
}
for (int i = 1; i <= n; i++) {
table[0][i] = INSERTION_PENALTY * i;
trace[0][i] = INSERTION;
}
// fill the dynamic programming table
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
minPenalty = table[i - 1][j] + DELETION_PENALTY;
bestOp = DELETION;
penalty = (table[i][j - 1] + INSERTION_PENALTY);
if ( minPenalty > penalty){
minPenalty = penalty;
bestOp = INSERTION;
}
if (reference[s1 + i - 1].equals(candidate[s2 + j - 1])) {
penalty = table[i - 1][j - 1];
if (penalty < minPenalty) {
minPenalty = penalty;
bestOp = MATCH;
}
} else {
penalty = table[i - 1][j - 1] + SUBSTITUTION_PENALTY;
if (penalty < minPenalty) {
minPenalty = penalty;
bestOp = SUBSTITUTION;
}
}
table[i][j] = minPenalty;
trace[i][j] = bestOp;
}
}
}
private ArrayList<AlignedSequence> backtrace() {
int i= e1-s1;
int j= e2-s2;
ArrayList<Integer> ops = new ArrayList<Integer>();
while ( (i >= 0) && (j >= 0) ) {
ops.add(trace[i][j]);
if ( trace[i][j] == MATCH || trace[i][j] == SUBSTITUTION){
i--;
j--;
}else if (trace[i][j] == INSERTION) {
j--;
}else{
i--;
}
}
// recover the alignment
ArrayList<AlignedSequence> output = new ArrayList<AlignedSequence>();
String refElement;
String candElement;
int refIndex = s1;
int candIndex = s2;
for (int m = ops.size() - 2; m >= 0; m--) {
int currentOP = (ops.get(m)).intValue();
if (currentOP != DELETION) {
candElement = candidate[candIndex];
candIndex++;
} else {
candElement = null;
}
if (currentOP != INSERTION) {
refElement = reference[refIndex];
refIndex++;
} else {
refElement = null;
}
output.add(new AlignedSequence(refElement, candElement));
}
return output;
}
}