-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlinhamento_Global_S.java
More file actions
151 lines (135 loc) · 4.21 KB
/
Alinhamento_Global_S.java
File metadata and controls
151 lines (135 loc) · 4.21 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
import java.util.*;
import java.io.*;
import java.math.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Alinhamento_Global_S {
/**
* The character that indicates the row and column for insertion and deletion
* penalties in the matrix.
*/
protected static final char INDEL_CHAR = '*';
static int[][] fMatrix;
static String x, y;
public static void main(String args[]) throws Exception {
String currentDir = System.getProperty("user.dir") + "/";
Path arg1 = Paths.get( currentDir + args[0] );
Path arg2 = Paths.get( currentDir + args[1] );
x = readFile(arg1);
y = readFile(arg2);
needlemanWunsch();
}
public static String readFile(Path file){
String temp = "";
Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
String line = reader.readLine(); // ignores the first line
while ((line = reader.readLine()) != null) {
temp = temp.concat(line);
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
return temp;
}
/*********************************************
******* Needleman–Wunsch algorithm ********
*********************************************
* +1 Match: The two letters are the same
* -1 Mismatch: The two letters are differential
* -1 Indel (INsertion or DELetion) : One letter aligns to a gap in the other string.
**/
static int gap = -2;
static int mismatch = -1;
static int match = 1;
/**
* To compute an alignment that actually gives the maximum score among all possible
* alignment:
* - Start from the bottom right cell, and compare the value with the three
* possible sources (Match, Insert, and Delete above) to see which it came from.
* @throws Exception
*/
public static void needlemanWunsch() {
computeMatrix();
String alignmentA = "";
String alignmentB = "";
int i,j;
i = x.length();
j = y.length();
while(i > 0 || j > 0){
// check if it came from a MATCH
if( i>0 && j>0 && (fMatrix[i][j] == fMatrix[i-1][j-1] + scoreSubstitution(x.charAt(i-1), y.charAt(j-1)))){
alignmentA = x.charAt(i-1) + alignmentA;
alignmentB = y.charAt(j-1) + alignmentB;
i--;
j--;
}
else if(i>0 && (fMatrix[i][j] == fMatrix[i-1][j] + gap)){
alignmentA = x.charAt(i-1) + alignmentA;
alignmentB = "_" + alignmentB;
i--;
}
else{
alignmentA = "_" + alignmentA;
alignmentB = y.charAt(j-1) + alignmentB;
j--;
}
}
System.out.println("A: " + alignmentA);
System.out.println("B: " + alignmentB);
}
public static void printTable(){
int r, c, rows, cols, ins, del, sub;
rows = x.length()+1;
cols = y.length()+1;
computeMatrix();
for (c = 0; c < cols; c++){
for (r = 0; r < rows; r++)
{
System.out.print(fMatrix[r][c] + " ");
}
System.out.println();
}
}
/**
* Computes the dynamic programming matrix.
*/
public static void computeMatrix() {
int r, c, rows, cols, ins, del, sub;
rows = x.length()+1;
cols = y.length()+1;
fMatrix =new int[rows][cols];
// initiate first row
fMatrix[0][0] = 0;
for (c = 1; c < cols; c++)
fMatrix[0][c] = fMatrix[0][c-1] + scoreInsertion(y.charAt(c-1));
for (r = 1; r < rows; r++)
{
// initiate first column
fMatrix[r][0] = fMatrix[r-1][0] + scoreInsertion(x.charAt(r-1));
for (c = 1; c < cols; c++)
{
ins = fMatrix[r][c-1] + scoreInsertion(y.charAt(c-1));
sub = fMatrix[r-1][c-1] + scoreSubstitution(x.charAt(r-1),y.charAt(c-1));
del = fMatrix[r-1][c] + scoreDeletion(x.charAt(r-1));
// choose the greatest
fMatrix[r][c] = Math.max(Math.max(ins, sub), del);
}
}
}
private static int scoreSubstitution (char a, char b) {
if(a == INDEL_CHAR || b == INDEL_CHAR)
return gap;
if(a == b)
return match;
return mismatch;
}
public static int scoreInsertion(char c) {
return scoreSubstitution (INDEL_CHAR, c);
}
public static int scoreDeletion (char a) {
return scoreSubstitution (a, INDEL_CHAR);
}
}