我的主要目的是创造类似的东西
我目前从我的txt文件中读取2个dna序列并创建2个带有2个序列(M和N)的字符串,所以我必须创建一个M + 1和N + 1矩阵才能执行我的动态编程算法。
现在我的问题是这个!
我该如何创建这个2d数组?我的第一个维度应该使用我的第一个字符串(第1部分)的字符创建,第二个维度使用我的第二个字符串(第2部分)
我如何才能实现这一目标,然后再将其打印出来,如图所示。
谢谢你 http://i.stack.imgur.com/ViHc9.png这是我的代码
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class EditDistance {
public static void main(String[] args) {
int N = 0;
int M = 0;
// char [][] opt = new char [N+1][M+1];
java.io.File file = new java.io.File("gene57.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
String num = input.nextLine();
String[] parts = num.split(" ");
String part1 = parts[0];
N = part1.length();
String part2 = parts[1];
M = part2.length();
System.out.println(part1);
System.out.println("Number of nucleobase of Sequence 1 is=" + N);
System.out.println(part2);
System.out.println("Number of nucleobase of Sequence 2 is=" + M);
}
}
catch (FileNotFoundException e) {
System.err.format("File does not exist\n");
}
// x= n+1 , y=m+
}
}
答案 0 :(得分:2)
我使用样本输入"ATGA AGCT"
编写了这个小程序(对DNA而言可能甚至不可能)。它设置了一个名为char
的2d opt
数组,您可以随时使用0
访问当前行或列字母。
因此,如果您需要4.行中的字母,例如使用opt[4][0]
。
在java数组中,以0
而不是1
开头,但由于零始终是字母,因此您只需将行号设置为正确的值即可。
int N = 0;
int M = 0;
String part1 = null;
String part2 = null;
java.io.File file = new java.io.File("gene57.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
String num = input.nextLine();
String[] parts = num.split(" ");
part1 = parts[0];
N = part1.length();
part2 = parts[1];
M = part2.length();
System.out.println(part1);
System.out.println("Number of nucleobase of Sequence 1 is=" + N);
System.out.println(part2);
System.out.println("Number of nucleobase of Sequence 2 is=" + M);
}
}
catch (FileNotFoundException e) {
System.err.format("File does not exist\n");
}
char [][] opt = new char [N + 2][M + 2]; // + 1 for the indicator row/column + 1 for the extra field
opt[0] = part1.toCharArray();
char[] temp = part2.toCharArray();
for (int count = 1; count < M + 1; count++) {
opt[count][0] = temp[count - 1];
}
// Add '-' for the extra row at the bottom.
opt[M + 1][0] = '-';
// Here you need to do your dynamic programming using op[][] and once you're done
// you should have a 2d array of integers we'll just call it result[][] for now.
// replace with the method that computes and returns result! We'll just use these values for now.
int[][] result = {{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}, {17, 18, 19, 20, 21}};
// Printing the matrix
StringBuilder firstLine = new StringBuilder(" |");
StringBuilder secondLine = new StringBuilder(" x/y|");
StringBuilder horizontalLine = new StringBuilder("________");
int count = 0;
for (count = 0; count < N; count++) {
if (count < 9) {
// 1 digit
firstLine.append(" " + count);
secondLine.append(" " + opt[0][count]);
horizontalLine.append("___");
} else {
// 2 digits
firstLine.append(" " + count);
secondLine.append(" " + opt[0][count]);
horizontalLine.append("___");
}
}
// Add the extra column at the end for '-'.
if (count > 9) {
firstLine.append(" " + count);
secondLine.append(" -");
horizontalLine.append("___");
} else {
firstLine.append(" " + count);
secondLine.append(" -");
horizontalLine.append("___");
}
System.out.println(firstLine.toString());
System.out.println(secondLine.toString());
System.out.println(horizontalLine.toString());
for (count = 0; count < M + 1; count++) {
StringBuilder line = new StringBuilder();
// Add the indicator stuff
if (count > 9) {
line.append(" " + count + " " + opt[count + 1][0] + " |");
} else {
line.append(" " + count + " " + opt[count + 1][0] + " |");
}
for (int index = 0; index < N + 1; index++) {
// Add the results
if (result[count][index] > 9) {
line.append(" " + result[count][index]);
} else {
line.append(" " + result[count][index]);
}
}
// Print the line
System.out.println(line.toString());
}
这将打印以下输出:
ATGA
Number of nucleobase of Sequence 1 is=4
AGCT
Number of nucleobase of Sequence 2 is=4
| 0 1 2 3 4
x/y| A T G A -
_______________________
0 A | 1 2 3 4 5
1 G | 5 6 7 8 9
2 C | 9 10 11 12 13
3 T | 13 14 15 16 17
4 - | 17 18 19 20 21
如果您对算法有任何疑问,请在下面的评论中告诉我。如果result
的值超过99
(最多3位数),则必须调整if-else
结构并腾出更多空间以保持所有内容正确对齐。