- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有 2 个序列,AACAGTTACC
和 TAAGGTCA
,我正试图找到一个全局序列比对。我设法创建了一个二维数组并创建了矩阵,我什至用半动态方法填充了它。
这是我填充矩阵的代码:
void process() {
for (int i = 1; i <= sequenceA.length; i++) {
for (int j = 1; j <= sequenceB.length; j++) {
int scoreDiag = opt[i-1][j-1] + equal(i, j);
int scoreLeft = opt[i][j-1] - 1;
int scoreUp = opt[i-1][j] - 1;
opt[i][j] = Math.max(Math.max(scoreDiag, scoreLeft), scoreUp);
}
}
}
private int equal(int i, int j) {
if (sequenceA[i - 1] == sequenceB[j - 1]) {
return 1;
} else {
return -1;
}
}
我的主要问题是这段代码生成了这个输出:
0 -1 -2 -3 -4 -5 -6 -7 -8
-1 -1 0 -1 -2 -3 -4 -5 -6
-2 -2 0 1 0 -1 -2 -3 -4
-3 -3 -1 0 0 -1 -2 -1 -2
-4 -4 -2 0 -1 -1 -2 -2 0
-5 -5 -3 -1 1 0 -1 -2 -1
-6 -4 -4 -2 0 0 1 0 -1
-7 -5 -5 -3 -1 -1 1 0 -1
-8 -6 -4 -4 -2 -2 0 0 1
-9 -7 -5 -5 -3 -3 -1 1 0
-10 -8 -6 -6 -4 -4 -2 0 0
但我希望它看起来像这样(我只关心图片中的数字):
我必须应用惩罚:每个不匹配 1 和每个间隙 2,如果它匹配 0。
最佳答案
有几处需要修改:
AACAGTTACC
和 TAAGGTCA
,而是 CCATTGACAA
和 ACTGGAAT
。完整的解决方案是:
// Note that these sequences are reversed!
String sequenceA ="CCATTGACAA";
String sequenceB = "ACTGGAAT";
// The penalties to apply
int gap = 2, substitution = 1, match = 0;
int[][] opt = new int[sequenceA.length() + 1][sequenceB.length() + 1];
// First of all, compute insertions and deletions at 1st row/column
for (int i = 1; i <= sequenceA.length(); i++)
opt[i][0] = opt[i - 1][0] + gap;
for (int j = 1; j <= sequenceB.length(); j++)
opt[0][j] = opt[0][j - 1] + gap;
for (int i = 1; i <= sequenceA.length(); i++) {
for (int j = 1; j <= sequenceB.length(); j++) {
int scoreDiag = opt[i - 1][j - 1] +
(sequenceA.charAt(i-1) == sequenceB.charAt(j-1) ?
match : // same symbol
substitution); // different symbol
int scoreLeft = opt[i][j - 1] + gap; // insertion
int scoreUp = opt[i - 1][j] + gap; // deletion
// we take the minimum
opt[i][j] = Math.min(Math.min(scoreDiag, scoreLeft), scoreUp);
}
}
for (int i = 0; i <= sequenceA.length(); i++) {
for (int j = 0; j <= sequenceB.length(); j++)
System.out.print(opt[i][j] + "\t");
System.out.println();
}
结果就像你给我们的例子一样(但是相反,记住!):
0 2 4 6 8 10 12 14 16
2 1 2 4 6 8 10 12 14
4 3 1 3 5 7 9 11 13
6 4 3 2 4 6 7 9 11
8 6 5 3 3 5 7 8 9
10 8 7 5 4 4 6 8 8
12 10 9 7 5 4 5 7 9
14 12 11 9 7 6 4 5 7
16 14 12 11 9 8 6 5 6
18 16 14 13 11 10 8 6 6
20 18 16 15 13 12 10 8 7
因此,最终比对得分位于 opt[sequenceA.length()][sequenceB.length()]
(7)。如果您确实需要像图像中那样显示反转矩阵,请执行以下操作:
for (int i = sequenceA.length(); i >=0; i--) {
for (int j = sequenceB.length(); j >= 0 ; j--)
System.out.print(opt[i][j] + "\t");
System.out.println();
}
关于java - 全局序列比对动态规划寻找矩阵中的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647619/
我正在创建我的第一个 WAR 文件。我一直在试验 ant buildfile 语法,我的 buildfile 的第一部分从我的 Eclipse 项目中获取内容并将其放入 /dist 文件夹中,然后将其
我是一名学习 SQL 和 PHP 的学生,我接到了一项任务,要使用 PHP 和 mySQLi 创建学生反馈表,我真的一直在思考如何为项目设计数据库! 我正在创建一个系统,用户可以在其中登录网页,如果用
这个问题在这里已经有了答案: Is it possbile to test for expected errors when the testee exits with failure using
我目前正在设计和开发一个 Web 应用程序,该应用程序有可能快速增长。我将提供一些一般信息,然后继续我的问题。我会说我是一名中级网络程序员。 以下是一些规范:MySQL - 数据库后端PHP - 用于
我不知何故无法在我的日志解析器应用程序中实现报告功能。 这是我目前所做的: 我正在编写一个应用程序,它读取日志文件并在字符串中搜索可以在用户配置文件中定义的多个正则表达式。对于从配置中解析的每个所谓的
我有兴趣学习如何在多开发团队场景中设计/规划 Web 应用程序开发。 假设“项目经理/负责人”的角色: 成功的 Web 应用程序开发需要哪些“文档”? 需要什么 UML 图,需要什么程度? 在设计/计
table a (t_a): id name last first email state country 0 sklass klass steve
我们建立了一个广泛使用 JQuery UI 的 AJAX 网站。我们有 30 多个自制的 JQuery UI 小部件(动态加载)。我们到处都使用 JQuery native 小部件:对话框、 slid
我是一名优秀的程序员,十分优秀!