How to Implement Traceback Functionality in Java Algorithm?

  • Thread starter jlrj
  • Start date
  • Tags
    Code
In summary, the conversation is about creating a Java program for dynamic traceback using a global alignment algorithm. The program includes a text area, buttons for different operations, and fields for user input. The traceback process involves following the highest scoring path in the matrix backwards and recording the steps taken to reconstruct the optimal alignment. Further research is recommended for a more detailed understanding of the process.
  • #1
jlrj
1
0
i have the code for the algorithm.
all i need now is the when traceback button is pressed,traceback can be done and shown.
anyone can help?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Dynamictraceback extends JApplet implements ActionListener

{

TextArea outputArea;
JButton button;
JButton reset;
JButton trace;
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;



public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());


outputArea = new TextArea(48,95);
Font font = new Font("Courier", Font.PLAIN,12);
outputArea.setFont(font);
outputArea.setEditable(false);


button = new JButton("No Gap");
button.addActionListener(this);

trace= new JButton("Traceback");
trace.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(trace);
c.add(reset);
c.add(m1);
c.add(m1F);
c.add(m2);
c.add(m2F);
c.add(g1);
c.add(g1F);
c.add(outputArea);
}



public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() ==(reset))
{
outputArea.setText(null);
tF1.setText(null);
tF2.setText(null);
m1F.setText("1");
m2F.setText("0");
g1F.setText("-1");
}


else if(evt.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 = new int[a2.length][a1.length];//this is the inital 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 == a1[j])
{
main[j] = matI; //match = 1
}

else
{
main[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--) //this loops goes into the matrix
{ //1 is subtracted because i wan to leave the ends out
addUp = 0; //alignment 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 yer check for max alignment addup
{ //for the max amount u can addup
addUp = main[k][l];
}
else
{
addUp = addUp;
}
}
}
main[j] +=addUp;
}
}


for(int i =0; i<a1.length; i++)
{
output +=" "+a1;
}
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 the double digits
output +=" "+main[j][k];

else
output +=" "+main[j][k];
}
output += "\n";
}


output += "\n";
outputArea.setText(output);
}


else if (evt.getSource() ==(gapB)) //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 = new int[a2.length][a1.length];

for(int i=0; i<a1.length; i++)//results for the 1st row is done here
{
main[0] = 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[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;

else if(no2>no3 && no2>no1)
main[k][l] = no2;

else
main[k][l] = no1;
}
}


for(int i=0;i<a1.length;i++)//first row is displayed
output+=" "+a1;

output+="\n";


for(int i=0;i<a2.length;i++)//2nd row and forth is displayed with the matrix(main[][])

{

output+=a2;

for(int k=0;k<a1.length;k++)

{
if(main[k]<10 && main[k]>=0)//same thing to check for double digits


output+=" "+main[k];


else if(main[k]<0 && main[k]>-10)
output+=" "+main[k];


else if (main[k]>=10)
output +=" "+main[k];

else
output +=" "+main[k];

}

output+="\n";
}
outputArea.setText(output);

}

}

}
 
Technology news on Phys.org
  • #2
//This is the traceback button else if (evt.getSource() ==(trace)) { //Add your traceback code here }}</code>As you have not provided a clear understanding of the exact problem that you are trying to solve, it is difficult to provide a specific answer to your question. However, in general, the process of traceback involves following a path through the matrix (or other data structure) which was created during the dynamic programming algorithm, and recording the sequence of steps taken. This is usually done by starting at the end of the matrix and then following the maximum scoring path backwards. The exact implementation will depend on the particular algorithm you are using, but there are some general approaches that can be followed.For example, if you are using a global alignment algorithm, you would start at the highest scoring cell in the matrix, and then trace back to the cell which yielded the highest score. You would then move to the next highest scoring cell and repeat the process until you reach the origin. At each step, you would record the type of step taken (match, gap, or mismatch) and the direction of the move (up, left, or diagonal). Once you reach the origin, you can reconstruct the optimal alignment based on the sequence of steps taken.For a more detailed explanation, I would recommend consulting an online resource such as the Wikipedia article on Sequence Alignment or similar.
 
  • #3


Thank you for providing the code for the algorithm. It seems like you have successfully implemented the algorithm and are now looking for help with the traceback functionality. I would suggest reaching out to a colleague or fellow researcher who may have experience with this type of programming or seeking assistance from a programmer or software engineer. Additionally, online forums or communities dedicated to coding and programming may also be a helpful resource. Good luck with your project!
 

1. What is an algorithm?

An algorithm is a set of step-by-step instructions or rules that are followed to solve a problem or complete a task.

2. How are algorithms used in computer science?

Algorithms are used in computer science to solve complex problems and automate tasks. They are essential for creating software and computer programs.

3. What is the difference between a simple and complex algorithm?

A simple algorithm is a set of basic instructions that can be easily understood and executed by a computer. A complex algorithm involves more advanced techniques and may require more computing power to solve a problem.

4. Can algorithms make mistakes?

Yes, algorithms can make mistakes if there are errors in the code or if the algorithm is not designed correctly. It is important for scientists to carefully test and debug algorithms to ensure their accuracy.

5. How do scientists improve algorithms?

Scientists can improve algorithms by analyzing their performance and making adjustments to the code. This can involve optimizing the algorithm for speed, accuracy, or efficiency.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
913
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top