js DNA动态序列比对代码
动态序列比对
function pairaln(seq1, seq2){
// scoring scheme
var MATCH = 1; // +1 for letters that match
var MISMATCH = -1; // -1 for letters that mismatch
var GAP = -1; // -1 for any gap
// initialization
var score=[[]];
var pointer=[[]];
score[0][0] = 0;
pointer[0][0] = 0;
var l1=seq1.length
var l2=seq2.length
for(var j = 1; j = up_score) {
if (diagonal_score >= left_score) {
score[i][j] = diagonal_score;
pointer[i][j] = 1;
}
else {
score[i][j] = left_score;
pointer[i][j] = 2;
}
} else {
if (up_score >= left_score) {
score[i][j] = up_score;
pointer[i][j] = 3;
}
else {
score[i][j] = left_score;
pointer[i][j] = 2;
}
}
}
}
// trace-back
var align1 = [];
var align2 = [];
// start at last cell of matrix
var j = l1;
var i = l2;
while (1) {
if(pointer[i][j] == 0)break // ends at first cell of matrix
if (pointer[i][j] == 1) {
align1[align1.length] = seq1.charAt(j-1);
align2[align2.length] = seq2.charAt(i-1);
i--;
j--;
}
if (pointer[i][j] == 2) {
align1[align1.length] = seq1.charAt(j-1);
align2[align2.length] = "-";
j--;
}
if (pointer[i][j] == 3) {
align1[align1.length] = "-";
align2[align2.length] = seq2.charAt(i-1);
i--;
}
}
align1 = align1.reverse();
align2 = align2.reverse();
return "序列:
"+seq1+"
"+seq2+"
比对结果:
"+align1.join(" | ")+" |
"+align2.join(" | ")+" |
"
}
document.write(pairaln('CTGGGCTGACTGA', 'GACTAGCTAGACTGA'))
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]