-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmithWaterman.java
103 lines (86 loc) · 2.93 KB
/
SmithWaterman.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package smithwaterman;
import java.util.*;
/**
*
* @author FC
*/
class localAlignment{
int [][] alignment;
char [] ls, rs;
void initialization(int row, int col)
{
alignment = new int [row][col];
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
alignment[i][j] = 0;
}
}
}
void calculateAlignmentScore(int row, int col, int match, int mismatch, int gap)
{
int horizontal=0, vertical=0, diagonal=0, as = 0;
for(int i=1; i<row; i++)
{
for(int j=1; j<col; j++)
{
//System.out.print(alignment[i][j] + " ");
horizontal = alignment[i][j-1] + gap;
if(horizontal < 0) horizontal = 0;
vertical = alignment[i-1][j] + gap;
if(vertical < 0) vertical = 0;
//check diagonal
alignment[i][j] = alignment[i-1][j-1];
if(ls[i-1] == rs[j-1])
{
diagonal = alignment[i][j] + match;
System.out.print(ls[i-1] + " " + rs[j-1] + " " + diagonal + "\n");
}
else
{
diagonal = alignment[i][j] + mismatch;
}
if(diagonal < 0) diagonal = 0;
alignment[i][j] = Math.max(horizontal, vertical);
alignment[i][j] = Math.max(alignment[i][j], diagonal);
as = Math.max(as, alignment[i][j]);
//System.out.println("");
}
}
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
System.out.print(alignment[i][j] + " ");
}
System.out.println("");
}
System.out.println("Alignment Score is = " + as);
}
}
public class SmithWaterman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
localAlignment la = new localAlignment();
Scanner sc = new Scanner(System.in);
String LS = "CTCGTC";
String RS = "AGCGTAG";
int m = LS.length()+1, n = RS.length()+1;
//int [][] alignment = new int [m][n];
int match = 10, mismatch = -5, gap = -7;
//initialization
la.ls = LS.toCharArray();
la.rs = RS.toCharArray();
la.initialization(m, n);
la.calculateAlignmentScore(m, n, match, mismatch, gap);
}
}