Java Blog

 
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dynamic_prog;
import java.util.Scanner;
import java.io.*;
import java.io.FileReader;
import java.nio.file.Files;
import java.util.*;
import java.lang.String;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.Collections;
import java.lang.Exception;
/**
 *
 * @author Hira
 */
public class Dynamic_Prog {

   // Path p=Paths.get(\"seq.txt\");

//Charset charset = Charset.forName(\"US-ASCII\");
// BufferedReader reader = Files.newBufferedReader(p, charset)) {
  //  String line = null;
   // while ((line = reader.readLine()) != null) {
     //   System.out.println(line);
   // }


    private static String a=\" GGATCGA\";
    private static String b=\" GAATTCAGTTA\";
    private static List<String>trace=new ArrayList<String>();
    private static String na,nb;
    private static int[][] arr = new int[a.length()][b.length()];
    private static int match,mismatch,gap;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    System.out.print(a.charAt(0));
       Scanner in=new Scanner(System.in);
       System.out.println(\"set scores:\");
       System.out.println(\"Enter score for match:\");
       match=in.nextInt();
       System.out.println(\"Enter score for mismatch(-ive value):\");
       mismatch=in.nextInt();
       System.out.println(\"Enter gap penalty\"); 
       gap=in.nextInt();

        System.out.println(a);

       for(int i=0, j=0;j<b.length();j++)
           arr[i][j]=j*gap;



       for (int i=1,j=0;i<a.length();i++)
           arr[i][j]=gap*i;


       for (int i=0;i<a.length();i++){
          for(int j=0;j<b.length();j++)
               System.out.print(arr[i][j]+\" \");
          System.out.println();
       }

       for (int i=1;i<a.length();i++)
           for(int j=1;j<b.length();j++)
                arr[i][j]=max(i,j);
       System.out.println(\"trace list\");
       System.out.println(trace);
       // for (int i=0;i<a.length();i++){
         // for(int j=0;j<b.length();j++)
              // System.out.print(arr[i][j]+\" \");
         // System.out.println();


               traceback();


    }

   public static int max(int i,int j){
       int x,y,z;
       String p,o,t; 
       List<Integer>l=new ArrayList<Integer>();
    //   List<Integer>index=new ArrayList<Integer>();


                     if(a.charAt(i)==b.charAt(j)){
                            x=arr[i-1][j-1]+match;
                            l.add(x);
                            p=i-1+\":\"+(j-1)+\"\";

                      }
                      else{
                             x=arr[i-1][j-1]+mismatch;
                             l.add(x);
                             p=i-1+\":\"+(j-1)+\"\";
                          }
                     y=arr[i][j-1]+gap;
                     l.add(y);
                     o=i+\":\"+(j-1)+\"\";
                     z=arr[i-1][j]+gap;
                     l.add(z);
                     t=i-1+\":\"+(j)+\"\";
                     int max=Collections.max(l);    
                    // System.out.println(l);

                    // System.out.println(max);


                      if(max==y)
                     {
                         //trace.add(o);
                         trace.add(\"h\");
                         String n=max+\"\";
                        // trace.add(n);
                     }
                     else if(max==z)
                     {
                        ////trace.add(t);
                        trace.add(\"v\");
                        String n=max+\"\";
                        // trace.add(n);
                     }

                     else{
                      // trace.add(p);
                       trace.add(\"d\"); 
                       String n=max+\"\";
                       // trace.add(n);
                     }       
                trace.toString();

       return max;
           }

    public static void traceback(){

         List<String>x=new ArrayList<String>();
         List<String>y=new ArrayList<String>();
         a=a.trim();
         b=b.trim();
      //   Collections.reverse(a);
       //Collections.reverse(b);    
         System.out.println(a);
         int i=a.length()-1;
         int j=b.length()-1;
        // List<String>a1=new ArrayList<String>();
        // List<String>b1=new ArrayList<String>();
          System.out.println(i);
           System.out.println(j);
           String h;
           String   c;
           char t,r;
        //for(int i=l;i>=l;){
          // for(int j=k;j>=k;){
        //do{

               for(int w=trace.size()-1;w>=0;)
               {
                 // 
                   if(trace.get(w)==\"h\")
                   {
                        x.add(\"_\");                       
                        //trace.remove(w);
                       // a=a.substring(0, i);
                       c= b.charAt(j)+\"\";
                        y.add(c);
                        j--; 
                        w--;
                       // break;
               }
                  else if(trace.get(w)==\"v\"){
                      y.add(\"_\");
                      //trace.remove(w);
                       h=a.charAt(i)+\"\";
                       x.add(h);
                       i--;
                       w=w-b.length();
                      //a=a.substring(0, i);
                     // b=b.substring(0, j); 
                     // break;
                  } 
                  else if(trace.get(w)==\"d\")
                  {
                     // try{

                      t=a.charAt(i);
                      h=t+\"\";
                      x.add(h);
                       c=b.charAt(j)+\"\";
                     y.add(c); 
                     // trace.remove(w);
                      //a=a.substring(0, i);
                     // b=b.substring(0, j); 
                      j--;
                      i--;
                      w=w-(b.length()-1);
                     // break;
                    //  }catch(Exception e)
                     // {
                          //System.err.println(\"caught exception\");
                 // }
                  }
                     // else
    //{

   // }
        }
       // }
         //while(i>-1||j>-1);


       Collections.reverse(x);
       Collections.reverse(y);        
       // String f=x.toString();
       // String o=y+\"\";

       System.out.println(\"X\"); 
       System.out.println(x);
       System.out.println(\"y list\");
       System.out.println(y);
       System.out.println(a);
    }
}

    Related Post:
  1. Corba program of game

  2. An applet program to give demo of getDocumentBase() and getCodeBase() methods

  3. Program in CORBA which takes a long sentence from the client (at least 7 words), passes to the server, server separates out each word

  4. Program to print triangle of numbers

  5. Program to show the use of Conditional (Ternary) Operator

  6. Program to show the use of Do-While Loop

  7. Program to compare Strings by compring successive corresponding characters, starting with the first character in each String

  8. Program using Switch case to compute the net amount to be paid by a customer

  9. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  10. Develop a CORBA application that takes a string from client and returns whether it is palindrome

  11. Program to show an example of extraction of characters from a StringBuffer objects

  12. Program to show the use of operator (&lt;&lt;) Shift Left filling with zero from the right

  13. RMI based application for login validation based on data saved in text file stored on the server and will display appropriate message to the client..

  14. Program to declare, initialize and print a String object

  15. Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server

  16. Program to show the use of Logical OR Operator (|) in If Statements

  17. Program of an applet to illustrate BarChart

  18. Program of date server and client

  19. Program to show an example of creating Character Arrays from String objects

  20. File handling program to copy characters from one file to another

 
 
Didn't find what you were looking for? Find more on Biological Sequence Allignment Using Dynamic Programing