-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequenceAlignment.java
More file actions
279 lines (248 loc) · 7.77 KB
/
sequenceAlignment.java
File metadata and controls
279 lines (248 loc) · 7.77 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* File: sequenceAlignment.java
*
* This is my implementation of a class to determine a best alignment of
* two sequences with the least edit distance.
* @author Steven Yee
* @version 1.0 11/23/14
*/
/*
* I imported these classes to help me determine the minimum of numbers,
* read in text files, and backtrace to return an actual best alignment.
*/
import java.lang.Math;
import java.io.*;
import java.util.Stack;
import java.util.Scanner;
public class sequenceAlignment{
protected static int Gap = 0;
protected static int sizeFirst;
protected static int sizeSecond;
protected static int[][] AlignmentMatrix;
protected static int[][] SimilarityMatrix = new int[4][4];
protected static String firstSequenceString;
protected static String secondSequenceString;
protected static char[] firstCharArray;
protected static char[] secondCharArray;
/*
* This method takes in a constant gap cost, a similarity matrix indicating costs of mismatches,
* and two sequences defined over the alphabet {A,C,G,T}. It returns a least edit distance alignment of the two sequences.
* @param file where file contains the data.
*/
static public void readData(File file){
try{
BufferedReader textFile = new BufferedReader(new FileReader(file));
try{
String line = null;
line = textFile.readLine();
Gap = Integer.parseInt(line);
for (int i = 0; i < 4; i++){
line = textFile.readLine();
createSimilarityMatrix(line, i);
}
line = textFile.readLine();
firstSequenceString = line;
if (line == null){
sizeFirst = 0;
}
else{
sizeFirst = firstSequenceString.length();
}
line = textFile.readLine();
secondSequenceString = line;
if (line == null){
sizeSecond = 0;
}
else{
sizeSecond = secondSequenceString.length();
}
createAlignmentMatrix(firstSequenceString, secondSequenceString);
System.out.println();
displayBestAlignment();
System.out.println("with the minimum edit distance of " + AlignmentMatrix[sizeSecond][sizeFirst] + ".");
System.out.println();
}
catch(IOException ex){
ex.printStackTrace();
}
}
catch(IOException ex){
ex.printStackTrace();
}
};
/*
* This method creates a similarity matrix indicating the costs of mismatches.
* @param line where line is a string containing the penalties for a single character and all other characters.
* @param row where row specifies which "single character" we are observing.
*/
public static void createSimilarityMatrix(String line, int row){
char[] lineArray = line.toCharArray();
int count = 0;
for (int i = 0; i < 7;){
SimilarityMatrix[row][count] = (int)(lineArray[i] - '0');
count++;
i = i + 2;
};
};
/*
* This method returns the cost of a mismatch between two characters.
* @param first where first is a character we are interested in.
* @param second where second is the other character we are interested in.
* @return the cost of a mismatch between the two characters based on the similarity matrix.
*/
public static int cost(char first, char second){
int firstBase = convertBase(first);
int secondBase = convertBase(second);
return SimilarityMatrix[firstBase][secondBase];
};
/*
* This method converts a character into an integer so we can easily access teh similarity matrix.
* @param letter where letter is the character we wish to convert.
* @return the integer representation of the character.
*/
public static int convertBase(char letter){
switch(letter){
case 'A': return 0;
case 'C': return 1;
case 'G': return 2;
default: return 3;
}
};
/*
* This method creates an (m+1) x (n+1) matrix where each element is the least edit distance of
* the first j characters of the second sequence and the first i characters of the first sequence.
* @param firstSeq where firstSeq is the first sequence.
* @param secondSeq where secondSeq is the second sequence.
*/
public static void createAlignmentMatrix(String firstSeq, String secondSeq){
if (firstSeq == null){
firstCharArray = null;
}
else{
firstCharArray = firstSeq.toCharArray();
}
if (secondSeq == null){
secondCharArray = null;
}
else{
secondCharArray = secondSeq.toCharArray();
}
AlignmentMatrix = new int [sizeSecond + 1][sizeFirst + 1];
for (int i = 0; i <= sizeFirst; i++){
AlignmentMatrix[0][i] = i * Gap;
}
for (int j = 0; j <= sizeSecond; j++){
AlignmentMatrix[j][0] = j * Gap;
}
for (int i = 1; i <= sizeFirst; i++){
for (int j = 1; j <= sizeSecond; j++){
char firstChar = firstCharArray[i - 1];
char secondChar = secondCharArray[j - 1];
int top = Gap + AlignmentMatrix[j - 1][i];
int left = Gap + AlignmentMatrix[j][i - 1];
int diag = cost(firstChar, secondChar) + AlignmentMatrix[j - 1][i - 1];
int least = Math.min(top, Math.min(left, diag));
AlignmentMatrix[j][i] = least;
}
}
};
/*
* This method backtraces the least cost matrix to output a best alignment of the two sequences.
*/
public static void displayBestAlignment(){
Stack firstFinal = new Stack();
Stack secondFinal = new Stack();
Stack finalCosts = new Stack();
int firstIndex = sizeFirst;
int secondIndex = sizeSecond;
if (firstIndex == 0){
for (int i = 1; i <= secondIndex; i++){
firstFinal.push('-');
secondFinal.push(secondCharArray[secondIndex - i]);
finalCosts.push(Gap);
};
secondIndex = 0;
}
if (secondIndex == 0){
for (int i = 1; i <= firstIndex; i++){
firstFinal.push(firstCharArray[firstIndex - i]);
secondFinal.push('-');
finalCosts.push(Gap);
};
firstIndex = 0;
}
while (firstIndex > 0){
if (secondIndex == 0){
for (int i = 1; i <= firstIndex; i++){
firstFinal.push(firstCharArray[firstIndex - i]);
secondFinal.push('-');
finalCosts.push(Gap);
}
firstIndex = 0;
};
while (secondIndex > 0){
if (firstIndex == 0){
for (int i = 1; i <= secondIndex; i++){
secondFinal.push(secondCharArray[secondIndex - i]);
firstFinal.push('-');
finalCosts.push(Gap);
}
secondIndex = 0;
}
else{
int top = Gap + AlignmentMatrix[secondIndex - 1][firstIndex];
int left = Gap + AlignmentMatrix[secondIndex][firstIndex - 1];
int diag = AlignmentMatrix[secondIndex - 1][firstIndex - 1] + cost(firstCharArray[firstIndex - 1], secondCharArray[secondIndex - 1]);
int least = Math.min(top, Math.min(left, diag));
if (least == diag){
firstFinal.push(firstCharArray[firstIndex - 1]);
secondFinal.push(secondCharArray[secondIndex - 1]);
finalCosts.push(cost(firstCharArray[firstIndex - 1], secondCharArray[secondIndex - 1]));
firstIndex--;
secondIndex--;
}
else if(least == top){
firstFinal.push("-");
secondFinal.push(secondCharArray[secondIndex - 1]);
finalCosts.push(Gap);
secondIndex--;
}
else{
firstFinal.push(firstCharArray[firstIndex - 1]);
secondFinal.push("-");
finalCosts.push(Gap);
firstIndex--;
}
};
}
}
System.out.println("The best alignment is");
System.out.println();
while(!firstFinal.empty()){
System.out.print(firstFinal.pop());
System.out.print(' ');
}
System.out.println();
while(!secondFinal.empty()){
System.out.print(secondFinal.pop());
System.out.print(' ');
}
System.out.println();
while(!finalCosts.empty()){
System.out.print(finalCosts.pop());
System.out.print(' ');
}
System.out.println();
System.out.println();
}
/*
* This is my main for the assignment.
*/
public static void main (String... Arguments) throws IOException{
System.out.println("Please enter the name of your test file: ");
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
File testing = new File(System.getProperty("user.dir") + '/' + fileName);
readData(testing);
};
}