forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalAlignment.java
121 lines (115 loc) ยท 3.65 KB
/
GlobalAlignment.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
import java.util.Scanner;
public class GlobalAlignment {
//code for global alginment matrix, and alignment
public static int max(int a,int b,int c){
return Math.max(a,Math.max(b,c));
}
public static String rev(String s){
String temp = "";
for(int i=s.length()-1;i>=0;--i){
temp+=s.charAt(i);
}
return temp;
}
public static void printmatrix(int[][] dp){
for (int[] ints : dp) {
for (int j = 0; j < dp[0].length; ++j) {
System.out.print(ints[j]);
System.out.print(" \t");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
//input are two strings for example, ATCG and ACGG
Scanner in = new Scanner(System.in);
String ref = in.next();
String obs = in.next();
int reflen = ref.length();
int obslen = obs.length();
int dp[][] = new int[obslen+1][reflen+1];
int deletiongap = -2;
int insertiongap = -2;
int match = 1;
int mismatch = -1;
//i for obs , and j will be for ref
for(int i=0;i<=reflen;++i){
dp[0][i] = i*insertiongap;
}
for(int i=0;i<=obslen;++i){
dp[i][0] = i*deletiongap;
}
for(int i=1;i<=obslen;++i){
for(int j=1;j<=reflen;++j){
int add = ref.charAt(j-1)==obs.charAt(i-1) ? match : mismatch;
dp[i][j] = max(dp[i-1][j]+deletiongap, dp[i][j-1]+insertiongap, dp[i-1][j-1]+ add);
}
}
System.out.println("GLOBAL ALIGNMENT MATRIX:");
System.out.println();
printmatrix(dp);
String a1 = "";
String a2 = "";
int j = reflen;
int i = obslen;
while (i!=0 && j!=0){
int add = ref.charAt(j-1) == obs.charAt(i-1) ? match : mismatch;
if(dp[i][j] == dp[i-1][j-1] + add){
a1 += ref.charAt(j-1);
a2 += obs.charAt(i-1);
//System.out.println("MOVE DIAGONAL");
i--;
j--;
}
else if(dp[i][j] == dp[i-1][j] + deletiongap){
a2 += obs.charAt(i-1);
a1 += "-";
i--;
//System.out.println("MOVE UP");
}
else if(dp[i][j] == dp[i][j-1] + insertiongap){
a1 += ref.charAt(j-1);
a2 += "-";
j--;
//System.out.println("MOVE LEFT");
}
}
if(i == 0){
while (j != 0){
a2 += "-";
a1 += ref.charAt(j-1);
//System.out.println("MOVE LEFT");
j--;
}
}
if(j == 0) {
while (i != 0) {
a1 += "-";
a2 += obs.charAt(i - 1);
//System.out.println("MOVE UP");
i--;
}
}
System.out.println();
System.out.println("ALGINMENT FINAL:");
System.out.println();
a1 = rev(a1);
a2 = rev(a2);
String str = "";
for(int x=0;x<a1.length();++x){
if(a1.charAt(x) == '-' || a2.charAt(x) == '-'){
str+=" ";
}
else if(a1.charAt(x) == a2.charAt(x)){
str+="|";
}
else{
str+=" ";
}
}
System.out.println(a1);
System.out.println(str);
System.out.println(a2);
}
}