Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Algorithm(trackback code)

  1. Jan 21, 2009 #1
    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);

    }

    }

    }
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Algorithm(trackback code)
  1. Sudoku Algorithm (Replies: 2)

  2. VEGAS algorithm (Replies: 2)

  3. Ask an algorithm (Replies: 3)

  4. Knuth Algorithm (Replies: 3)

Loading...