Traceback Algorithm
Hi! I am currently working on a sequence alignment program which must be able to produce the optimal alignment as well as the traceback algorithm. I have finished doing the code and was able to compiled without any errors.
However, my program is only doing half its job as it is not giving me the optimal alignment which it should. Would appreciate it if you could kindly take a look at my code and see if i am doing anything wrong here. Thank you.
/**
* @(#)Argentina.java
*
* Sample Applet application
*
* @author
* @version 1.00 05/11/24
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass Argentinaextends JAppletimplements ActionListener
{
TextArea outputArea;
JButton button;
JButton reset;
JButton gapB;
JTextField tF1;
JTextField tF2;
JTextField m1F;
JTextField m2F;
JTextField g1F;
JLabel l1;
JLabel l2;
JLabel m1;
JLabel m2;
JLabel g1;
String s1;
String s2;
String mat;
String mis;
String gap;
int matI;
int misI;
int gapI;
int counter;
int no;
int no1;
int no2;
int no3;
String output;
char a1[];
char a2[];
int main[][];
int i,j,k,l,addUp;
publicvoid init()
{
Container c= getContentPane();
c.setLayout(new FlowLayout());
outputArea=new TextArea(38,85);
Font font=new Font("Courier", Font.PLAIN, 12);
outputArea.setFont(font);
outputArea.setEditable(false);
button=new JButton("No Gap");
button.addActionListener(this);
reset=new JButton("Reset");
reset.addActionListener(this);
gapB=new JButton("Gap");
gapB.addActionListener(this);
tF1=new JTextField(55);
tF2=new JTextField(55);
m1F=new JTextField(3);
m2F=new JTextField(3);
g1F=new JTextField(3);
m1F.setText("1");
m2F.setText("0");
g1F.setText("-1");
l1=new JLabel("Enter Seq1:");
l2=new JLabel("Enter Seq2:");
m1=new JLabel("Match Score:");
m2=new JLabel("Mismatch Score:");
g1=new JLabel("Gap Penalty:");
c.add(l1);
c.add(tF1);
c.add(l2);
c.add(tF2);
c.add(button);
c.add(gapB);
c.add(reset);
c.add(m1);
c.add(m1F);
c.add(m2F);
c.add(g1);
c.add(g1F);
c.add(outputArea);
}
publicvoid actionPerformed(ActionEvent event)//*working just fine*
{
if(event.getSource()==(reset))
{
outputArea.setText(null);
tF1.setText(null);
tF2.setText(null);
m1F.setText("1");
m2F.setText("0");
g1F.setText("-1");
}
elseif(event.getSource()==(button))
{
output="";
addUp=0;
mat=m1F.getText();
mis=m2F.getText();
matI=Integer.parseInt(mat);
misI=Integer.parseInt(mis);
s1=tF1.getText();
a1=s1.toCharArray();
s2=tF2.getText();
a2=s2.toCharArray();
main=newint[a2.length][a1.length];//this is the initial part
//where 0s and 1s are filled in
for(int i=0; i<a2.length; i++)
{
for(int j=0; j><a1.length; j++)
{
if(a2[i]==a1[j])
{
main[i][j]=matI;//match=1
}
else
{
main[i][j]=misI;//mismatch=0
}
}
}
//this paragraph basically do a down stream comparison
//and addup the max number
for(int i=a2.length-1; i>=0;i--)//the initial scores are added
{
for(int j=a1.length-1; j>=0; j--)//the loops goes into
//the matris
{
//1 is subtracted because I want to leave the ends out
addUp=0;//allignment addup is reset
for(int k=i+1; k<a2.length; k++)
//this will take u to 1 row down and 1 column right
{
for(int l=j+1;l<a1.length; l++ )
{
if(main[k][l]>addUp)
//where u can do your check for max allignment addup
{
//for the max amount u can addup
addUp=main[k][l];
}
else
{
addUp=addUp;
}
}
}
main[i][j]+=addUp;
}
}
for(int i=0; i<a1.length; i++)
{
output+=""+a1[i];
}
output+="\n";
for(int j=0;j<a2.length; j++)
{
output+=a2[j];
for(int k=0;k<a1.length;k++)
{
if(main[j][k]>9||main[j][k]<0)
//this is done for double digits
output+=""+main[j][k];
else
output+=""+main[j][k];
}
output+="\n";
}
output+="\n";
outputArea.setText(output);
}
else//button gap is pushed
{
output="";
s1="x";
s2="x";
s1+=tF1.getText();
s2+=tF2.getText();
a1=s1.toCharArray();
a2=s2.toCharArray();
gap=g1F.getText();
gapI=Integer.parseInt(gap);
mis=m2F.getText();
mat=m1F.getText();
misI=Integer.parseInt(mis);
matI=Integer.parseInt(mat);
counter=0;
main=newint[a2.length][a1.length];
for(int i=0;i<a1.length; i++)//results for the 1st row is done here
{
main[0][i]=counter;
counter+=gapI;//this is where the gap penalty comes in
//for the 1st row
}
counter=0;
for(int i=0; i><a2.length; i++)//result for the 1st column
{
main[i][0]=counter;
counter+=gapI;//same for here, gap penalty for the 1st column
}
for(int k=1; k><a2.length; k++)
{
for(int l=1; l><a1.length; l++)
{
if(a2[k]==a1[l])//the substr score is done first
no=matI;//no is for match/mismatch
else
no=misI;
//here we have the 3 diff answers that are compared
//and the max taken out
no1=main[k-1][l-1]+no;
no2=main[k][l-1]+gapI;//set gap penalty here
no3=main[k-1][l]+gapI;
if(no3>no1&&no3>no2)//mystery part
main[k][l]=no3;
elseif(no2>no3&&no2>no1)
main[k][l]=no2;
else
main[k][l]=no1;
}
output+="\n";
}
outputArea.setText(output);
for(int i=0;i<a1.length;i++)//first row is displayed
output+=" " +a1[i];
output+="\n";
for(int i=0; i><a2.length; i++)//2nd row and 4th is displayed
//with the matrix(main[][])
{
output+=a2[i];
for(int k=0;k<a1.length; k++)
{
if(main[i][k]><10&&main[i][k]>=0)//same thing, check for double digits
output+=""+main[i][k];
elseif(main[i][k]<0&&main[i][k]>-10)
output+=""+main[i][k];
elseif(main[i][k]>=10)
output+=""+main[i][k];
else
output+=""+main[i][k];
}
output+="\n";
}
outputArea.setText(output);
}
//to traceback and find the path of choices *got problem here I think*
int kmax;
int lmax;
String align1="";
String align2="";
kmax=k;
lmax=l;
int m = main[kmax][lmax];
output+="Optimal Alignment: " + m;
while (m>0)
{
int MatchScore =(s1.charAt(kmax-1) == s2.charAt(lmax-1) )? matI : misI;
if (m==(main[kmax-1][lmax-1]+MatchScore))
{
m = main[kmax-1][lmax-1];
output+=" ->* "+main[kmax-1][lmax-1];
kmax--;
lmax--;
align1=s1.charAt(kmax-1)+align1;
align2=s2.charAt(lmax-1)+align2;
}
elseif (m==(main[kmax-1][lmax]+gapI))
{
m = main[kmax-1][lmax];
output+=" ->@ "+(main[kmax-1][lmax]);
kmax--;
align1=s1.charAt(lmax-1)+ align1;
align2="_"+ align2;
}
else
{
m = main[kmax][lmax-1];
output+=" ->% "+main[kmax][lmax-1];
lmax--;
align1="_"+align1;
align2=s2.charAt(kmax-1)+align2;
}
}
output+="\n\n" +" "+align1+"\n" +" "+align2 +"\n\n\n";
outputArea.setText(output);
}
}
//end of program

