| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import shared.Tools; |
| 6 |
public class NeedlemanWunsch { |
| 7 |
|
| 8 |
|
| 9 |
public static void main(String[] args){ |
| 10 |
byte[] read=args[0].getBytes(); |
| 11 |
byte[] ref=args[1].getBytes(); |
| 12 |
NeedlemanWunsch nw=new NeedlemanWunsch(read.length, ref.length); |
| 13 |
nw.fill(read, ref, 0, ref.length-1); |
| 14 |
|
| 15 |
for(int row=0; row<nw.scores.length; row++){ |
| 16 |
System.err.println(Arrays.toString(nw.scores[row])); |
| 17 |
System.err.println(Arrays.toString(nw.pointers[row])); |
| 18 |
System.err.println(); |
| 19 |
} |
| 20 |
|
| 21 |
byte[] out=nw.traceback(read, ref, 0, ref.length-1); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
System.err.println(new String(out)); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
public NeedlemanWunsch(int maxRows_, int maxColumns_){ |
| 30 |
maxRows=maxRows_; |
| 31 |
maxColumns=maxColumns_; |
| 32 |
scores=new int[maxRows+1][maxColumns+1]; |
| 33 |
pointers=new byte[maxRows+1][maxColumns+1]; |
| 34 |
for(int i=0; i<maxColumns+1; i++){ |
| 35 |
scores[0][i]=0-i; |
| 36 |
} |
| 37 |
for(int i=0; i<maxRows+1; i++){ |
| 38 |
scores[i][0]=0-i; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public void fill(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){ |
| 50 |
rows=read.length; |
| 51 |
columns=refEndLoc-refStartLoc+1; |
| 52 |
System.err.println("rows = "+rows+", columns="+columns); |
| 53 |
|
| 54 |
for(int row=0; row<rows; row++){ |
| 55 |
for(int col=0; col<columns; col++){ |
| 56 |
System.err.println("row = "+row+", col="+col); |
| 57 |
int match=(read[row]==ref[refStartLoc+col] ? 1 : -1); |
| 58 |
int diag=match+scores[row][col]; |
| 59 |
int left=scores[row+1][col]-1; |
| 60 |
int up=scores[row][col+1]-1; |
| 61 |
if(diag>=left && diag>=up){ |
| 62 |
scores[row+1][col+1]=diag; |
| 63 |
pointers[row+1][col+1]=DIAG; |
| 64 |
}else if(left>=up){ |
| 65 |
scores[row+1][col+1]=left; |
| 66 |
pointers[row+1][col+1]=LEFT; |
| 67 |
}else{ |
| 68 |
scores[row+1][col+1]=up; |
| 69 |
pointers[row+1][col+1]=UP; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public byte[] traceback(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){ |
| 77 |
int row=read.length; |
| 78 |
int col=ref.length; |
| 79 |
|
| 80 |
byte[] out=new byte[Tools.max(row, col)]; |
| 81 |
int outPos=out.length-1; |
| 82 |
|
| 83 |
while(row>0 || col>0){ |
| 84 |
byte ptr=pointers[row][col]; |
| 85 |
if(ptr==DIAG){ |
| 86 |
out[outPos]=read[row-1]; |
| 87 |
row--; |
| 88 |
col--; |
| 89 |
outPos--; |
| 90 |
}else if(ptr==LEFT){ |
| 91 |
out[outPos]='-'; |
| 92 |
col--; |
| 93 |
outPos--; |
| 94 |
}else{ |
| 95 |
assert(ptr==UP); |
| 96 |
|
| 97 |
row--; |
| 98 |
} |
| 99 |
} |
| 100 |
return out; |
| 101 |
} |
| 102 |
|
| 103 |
public final int maxRows; |
| 104 |
public final int maxColumns; |
| 105 |
private final int[][] scores; |
| 106 |
private final byte[][] pointers; |
| 107 |
|
| 108 |
public static final byte LEFT=0, DIAG=1, UP=2; |
| 109 |
|
| 110 |
private int rows; |
| 111 |
private int columns; |
| 112 |
|
| 113 |
} |