forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmithWaterman.java
More file actions
56 lines (47 loc) · 1.75 KB
/
SmithWaterman.java
File metadata and controls
56 lines (47 loc) · 1.75 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
package com.thealgorithms.dynamicprogramming;
/**
* Smith–Waterman algorithm for local sequence alignment.
* Finds the highest scoring local alignment between substrings of two sequences.
*
* Time Complexity: O(n * m)
* Space Complexity: O(n * m)
*/
public final class SmithWaterman {
private SmithWaterman() {
// Utility Class
}
/**
* Computes the Smith–Waterman local alignment score between two strings.
*
* @param s1 first string
* @param s2 second string
* @param matchScore score for a match
* @param mismatchPenalty penalty for mismatch (negative)
* @param gapPenalty penalty for insertion/deletion (negative)
* @return the maximum local alignment score
*/
public static int align(String s1, String s2, int matchScore, int mismatchPenalty, int gapPenalty) {
if (s1 == null || s2 == null) {
throw new IllegalArgumentException("Input strings must not be null.");
}
int n = s1.length();
int m = s2.length();
int maxScore = 0;
int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int matchOrMismatch = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? matchScore : mismatchPenalty;
dp[i][j] = Math.max(0,
Math.max(Math.max(dp[i - 1][j - 1] + matchOrMismatch, // match/mismatch
dp[i - 1][j] + gapPenalty // deletion
),
dp[i][j - 1] + gapPenalty // insertion
));
if (dp[i][j] > maxScore) {
maxScore = dp[i][j];
}
}
}
return maxScore;
}
}