好的,所以我编写了这个程序,它应该使用动态编程来对 dna 字符串进行排序并插入间隙以创建最佳数量的配对。下面的代码是我写的,理论上它应该可以工作。但事实并非如此。它编译但是当它运行时它会无限运行。我试图在我看到的代码中插入检查,二维矩阵没有初始化和填充,因此卡在它将运行回溯程序的点。我可以用什么来调试它并找出代码不起作用的原因?
/*interface Score_card
{
public score(int a, int b,String i,String j);
public gap_score(int i,int j, String a, String b);
public mismatch(int a, int b,String i,String j);
}*/
class DNA //implements Score_card
{
private int row = 10;// initialize number of rows for the matrix
private int column = 10;//initialize number of columns for the matrix
private int opt_number = 0;//the placeholder for the number of matches in the optimal alignment
private String DNA1 = DNA_gen(row);// initialize a String that is the length() of row
private String DNA2 = DNA_gen(column);//initialize a String of length() column
private int[][] dnamat = dna_ini();//initialize the DNA matrix
public char[][] mat_hold = new char[row][column];
private String DNA_seq1 = null;//create and empty String to be used in the matrix building/sequencing program
private String DNA_seq2 = null;// ditto
private String DNA_align = null;//create a String to hold the vertical alignment lines
public int[][] dna_ini()
{
//initializes the array, taking into account the possibility of and advanced gap scoring algorithm
String a = DNA1;
String b = DNA2;
int[][] ini = new int[row][column];//initialize the size of the array to be the length()s of the String plus one
for(int j = 0;j < row; j++) /*for loop to run through value of j limited by size of the array*/
{
ini[j][0] = gap_score(j,0,a,b);//utilize the gap score to initialize the first row
}//end for loop
for(int i = 0; i < column;i++)//same as above
{
ini[0][i]= gap_score(0,i,a,b);//same as above
}//end for loop
return ini;
}//end dna ini
public int score(int a, int b, String i, String j)
{
String dna1 = i;//Strings passed
String dna2 = j;
int a1 = a;//integers passed
int b1 = b;
int score = 1;//holds score
if(i.charAt(a1) == j.charAt(b1))//if the two values in the two Strings are equivalent to one another
{
score = 1;
return score;// score is equal to one
}
else
{
return score;
}
}// end score
public int gap_score(int a, int b,String k, String l)
{
int penaltyscore = 0;
if(k.charAt(a) != l.charAt(b))
{
return penaltyscore;
}
else
return penaltyscore;
}
public int mismatch(int a, int b,String i,String j)
{
String dna1 = i;//Strings passed
String dna2 = j;
int a1 = a;//integers passed
int b1 = b;
int score = 0;//holds score
if(i.charAt(a1) != dna2.charAt(b1))//if the two values in the two Strings are equivalent to one another
{
score = 0;// score is equal to one
}
return score;//return the score
}// end score
public static String DNA_gen(int n)// takes an int to determine length() of String returned
{
char[] S = new char[n]; //and array of character S equals a new array of characters of size n ( parameter passed to the method
String DNAgenchar = "AGCT";//String holds all of the possible nucleotides found in a DNA sequence
for(int i = 1; i < n; i++)// for new variable i such that i equals 0, i is less than the length() desired, i is incremented
{
int r = (int)(Math.random()*4);// given variable r is equal to a random number between 0-3
S[i] = DNAgenchar.charAt(r);// for a given index ( as the array is cycled through) the random number generated
//is the index of the String from which a character is pulled and added to the array
}
S[0] = ' ';// add a space to the beginning of the array
String DNAgened = null;// initialize a String to be returned
DNAgened += S; // convert S into a String for return
return DNAgened;//return the String
}
public void Matrix_fill()
{
String dna1 = DNA1;//initialize a variable to point to the dna String
String dna2 = DNA2;
int[][] matrix = dnamat;//initialize a matrix that points to the main one
for(int j =1; j <= row-1; j++)
{
for(int i =1; i <= column-1; i++)//cycle through the matrix
{
int diag = dnamat[j-1][i-1] + score((i-1),(j-1),dna1,dna2) + mismatch((i-1),(j-1),dna1,dna2);//diagonal value
int left = dnamat[j-1][i] + gap_score((j-1),i,dna1,dna2);//left value
int top = dnamat[j][i-1] + gap_score(j,(i-1), dna1, dna2);//top value
int maxint = Math.max(Math.max(diag,left),top);//gets the maximum value of the three numbers
if(maxint == diag)//series of if statements to add to the String containing the directions taken by the matrix in the program
{
mat_hold[j][i] = 'd';
dnamat[j][i] = maxint;// if the number is taken from the diagonal
}
else if(maxint == left)
{
mat_hold[j][i] = 'l';
dnamat[j][i] = maxint;
}
else if(maxint == top)
{
mat_hold[j][i] = 't';
dnamat[j][i] = maxint;
}
}//end for loop
}//end outer for loop
//opt_number = dnamat[dna1.length()][dna2.length()];// set the optimum number variable to the last place in the array, containing the number of matches in the optimum configuration
}//ends matrix fill
public void backtrack()
{
char d = 'd';//initialize a series of characters to represent looked for directions
char l = 'l';
char t = 't';
char temp = ' ';//not sure why i put this here
String j = DNA1;// create two Strings to be used for indexing purposes
String k = DNA2;
int x = column-1;//goes to the last row
int roww = row-1;//goes to the last column
char[][] mat_hold2 = mat_hold;
char opt_path = mat_hold[x][roww];//initialize the beginning of the path to be last element in the array
while(opt_path != mat_hold2[0][0])//while the current index does not equal the first element in the array
{
if(opt_path == d)// if the element in d was obtained from the diagonal
{
DNA_seq1 = j.charAt(x) + DNA_seq1;//using the matrix location [x][e]take the element from the DNA sequence and insert it into the front of the array we are building
DNA_seq2 = k.charAt(roww) + DNA_seq2;//ditto
DNA_align = "|" + DNA_align;
mat_hold2[x][roww] = mat_hold2[x-1][roww-1];//since they are the same in this case, insert a line to connect them
opt_path = mat_hold2[x][roww];//set the opt path to the diagonal value to be searched
}//since in this case it was taken from the diagonal, that means both letters were the same and so both letters at the given
//indexes are inserted into the newly constructed sequence
else if(opt_path == l)
{
DNA_seq1 = "_" + DNA_seq1;
DNA_seq2 = k.charAt(roww) + DNA_seq2;
DNA_align = " " + DNA_align;
mat_hold2[x][roww] = mat_hold2[x][roww-1];
opt_path = mat_hold2[x][roww];
}
else if(opt_path == t)
{
DNA_seq1 = j.charAt(x) + DNA_seq1;
DNA_seq2= "_" + DNA_seq2;
DNA_align = " " + DNA_align;
mat_hold2[x][roww] = mat_hold2[x-1][roww];
opt_path = mat_hold2[x][roww];
}
}//end while
System.out.println(DNA_seq1);
System.out.println(DNA_align);
System.out.println(DNA_seq2);
}//end backtrack
public void run_DNA_seqs()
{
Matrix_fill();
//System.out.println(opt_number);
backtrack();
}
}//end class DNA
class adv_score extends DNA
{
public int score(int a, int b, String i, String j)
{
String dna1 = i;//Strings passed
String dna2 = j;
int a1 = a;//integers passed
int b1 = b;
int score = 0;//holds score
if(i.charAt(a1) == j.charAt(b1))//if the two values in the two Strings are equivalent to one another
{
score = 2;// score is equal to one
}
return score;//return the score
}// end score
public int mismatch(int a, int b,String i,String j)
{
String dna1 = i;//Strings passed
String dna2 = j;
int a1 = a;//integers passed
int b1 = b;
int score = 0;//holds score
if(i.charAt(a1) != j.charAt(b1))//if the two values in the two Strings are equivalent to one another
{
score = -1;// score is equal to one
}
return score;//return the score
}// end score
}
class adv_gap extends DNA
{
public int gap_score(int a, int b,String k, String l)
{
int penscore = -2;
if(k.charAt(a) != l.charAt(b))
return penscore;
else
penscore = 0;
return penscore;
}
}
public class run_dna
{
public static void main(String[] args)
{
DNA dna1 = new DNA();
dna1.run_DNA_seqs();
}
}