Hi, I have this program that part of it is hard coded. Instead of hard coding it, I need to do a file read instead. Meaning, the hard coded part must be changed to reading the info I need from a txt file. May I know how to do that?

// Compile with:
//      javac Match2.java
// Run with:
//      java Match2 HEAGAWGHEE PAWHEAE

//  Class hierarchies
//  -----------------
//       Align              general pairwise alignment
//     AlignSimple          alignment with simple gap costs
//        NW                global alignment with simple gap costs
//        SW                local alignment with simple gap costs
//  Traceback               traceback pointers
//     Traceback2           traceback for simple gap costs
//     Traceback3           traceback for affine gap costs
//  Substitution            substitution matrices with fast lookup
//     Blosum50             the BLOSUM50 substitution matrix
//  Output                  general text output
//     SystemOut            output to the console (in the application)
//     TextAreaOut          output to a TextArea (in the applet)

// Notational conventions:
//   i in {0..n} indexes columns and sequence seq1
//   j in {0..m} indexes rows    and sequence seq2
//   k in {0..2} indexes states (in affine alignment)


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

class MatchApplet extends JApplet {


//  TextField seq1in = new TextField("HEAGAWGHEE");
//  TextField seq2in = new TextField("PAWHEAE");

	TextArea outputArea;
	JButton button;
	JButton reset;

	JTextField tF1;
	JTextField tF2;

	JLabel l1;
	JLabel l2;

	String s1;
	String s2;
	
	BufferedReader input, input2, in1, in2;	
	

	public static void main(String[] args)
	{
		//Application for program
  		Frame f = new Frame();
  		f.addWindowListener(new java.awt.event.WindowAdapter()
  		{
       	public void windowClosing(java.awt.event.WindowEvent e)
       	{
       	System.exit(0);
      	};
     	});

  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program
		
	}//end of public static void main(String[] args)  	
  	


  	public void init() 
	{	
  	
  		String display1="";
  		String display2="";
  			
		try
		{
       	    BufferedReader in1 = new BufferedReader(new FileReader("D:\\project1\\Dynamic Programming\\Alignment Algorithms\\Sequence Testing\\1a00(50).txt"));	//reading files in specified directory	
  			BufferedReader in2 = new BufferedReader(new FileReader("D:\\project1\\Dynamic Programming\\Alignment Algorithms\\Sequence Testing\\1a0a(50).txt"));	//reading files in specified directory	
  			
  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
  				display1 = str1;
  				System.out.print(display1);			
  			}
  			in1.close();
  			
  			System.out.println("");
  			
			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
  				display2 = str2;
  				System.out.print(display2);			
  			}
			in2.close();
			
			System.out.println("");
			

		Container c = getContentPane();
		c.setLayout(new FlowLayout());

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

		tF1 = new JTextField(display1);		
		tF2 = new JTextField(display2);		
		
		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);


	final Substitution sub = new Blosum50();

		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }

	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }

	  		public void println()
	  		{ outputArea.append("\n"); }
		};

	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");
      
      
      }catch( IOException ioException ) {}
      
  }//end of init()
  
}//end of class




// The class of substitution (scoring) matrices

abstract class Substitution {
  public int[][] score;

  void buildscore(String residues, int[][] residuescores) {
    // Allow lowercase and uppercase residues (ASCII code <= 127):
    score = new int[127][127];
    for (int i=0; i<residues.length(); i++) {
      char res1 = residues.charAt(i);
      for (int j=0; j<=i; j++) {
        char res2 = residues.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
      }
    }
  }

  abstract public String getResidues();
}


// The BLOSUM50 substitution matrix for amino acids (Durbin et al, p 16)

class Blosum50 extends Substitution {

  [B]private String residues = "ARNDCQEGHILKMFPSTWYV";

  public String getResidues()
  { return residues; }

  private int[][] residuescores =
            /* A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V */
  { /* A */ {  5                                                          },
    /* R */ { -2, 7                                                       },
    /* N */ { -1,-1, 7                                                    },
    /* D */ { -2,-2, 2, 8                                                 },
    /* C */ { -1,-4,-2,-4,13                                              },
    /* Q */ { -1, 1, 0, 0,-3, 7                                           },
    /* E */ { -1, 0, 0, 2,-3, 2, 6                                        },
    /* G */ {  0,-3, 0,-1,-3,-2,-3, 8                                     },
    /* H */ { -2, 0, 1,-1,-3, 1, 0,-2,10                                  },
    /* I */ { -1,-4,-3,-4,-2,-3,-4,-4,-4, 5                               },
    /* L */ { -2,-3,-4,-4,-2,-2,-3,-4,-3, 2, 5                            },
    /* K */ { -1, 3, 0,-1,-3, 2, 1,-2, 0,-3,-3, 6                         },
    /* M */ { -1,-2,-2,-4,-2, 0,-2,-3,-1, 2, 3,-2, 7                      },
    /* F */ { -3,-3,-4,-5,-2,-4,-3,-4,-1, 0, 1,-4, 0, 8                   },
    /* P */ { -1,-3,-2,-1,-4,-1,-1,-2,-2,-3,-4,-1,-3,-4,10                },
    /* S */ {  1,-1, 1, 0,-1, 0,-1, 0,-1,-3,-3, 0,-2,-3,-1, 5             },
    /* T */ {  0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 2, 5          },
    /* W */ { -3,-3,-4,-5,-5,-1,-3,-3,-3,-3,-2,-3,-1, 1,-4,-4,-3,15       },
    /* Y */ { -2,-1,-2,-3,-3,-1,-2,-3, 2,-1,-1,-2, 0, 4,-3,-2,-2, 2, 8    },
    /* V */ {  0,-3,-3,-4,-1,-3,-3,-4,-4, 4, 1,-3, 1,-1,-3,-2, 0,-3,-1, 5 }
            /* A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V */
  };[/B]
  public Blosum50()
  { buildscore(residues, residuescores); }
}



// Pairwise sequence alignment

abstract class Align {
  Substitution sub;             // substitution matrix
  int d;                        // gap cost
  String seq1, seq2;            // the sequences
  int n, m;                     // their lengths
  Traceback B0;                 // the starting point of the traceback

  final static int NegInf = Integer.MIN_VALUE/2; // negative infinity

  public Align(Substitution sub, int d, String seq1, String seq2) {
    this.sub = sub;
    this.seq1 = strip(seq1); this.seq2 = strip(seq2);
    this.d = d;
    this.n = this.seq1.length(); this.m = this.seq2.length();
  }

  public String strip(String s) {
    boolean[] valid = new boolean[127];
    String residues = sub.getResidues();
    for (int i=0; i<residues.length(); i++) {
      char c = residues.charAt(i);
      if (c < 96)
	valid[c] = valid[c+32] = true;
      else
	valid[c-32] = valid[c] = true;
    }
    StringBuffer res = new StringBuffer(s.length());
    for (int i=0; i<s.length(); i++)
      if (valid[s.charAt(i)])
	res.append(s.charAt(i));
    return res.toString();
  }

  // Return two-element array containing an alignment with maximal score

  public String[] getMatch() {
    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    Traceback tb = B0;
    int i = tb.i, j = tb.j;
    while ((tb = next(tb)) != null) {
      if (i == tb.i)
        res1.append('-');
      else
        res1.append(seq1.charAt(i-1));
      if (j == tb.j)
        res2.append('-');
      else
        res2.append(seq2.charAt(j-1));
      i = tb.i; j = tb.j;
    }
    String[] res = { res1.reverse().toString(), res2.reverse().toString() };
    return res;
  }

  public String fmtscore(int val) {
    if (val < NegInf/2)
      return "-Inf";
    else
      return Integer.toString(val);
  }

  // Print the score, the F matrix, and the alignment
  public void domatch(Output out, String msg, boolean udskrivF) {
    out.println(msg + ":");
    out.println("Score = " + getScore());
    if (udskrivF) {
      out.println("The F matrix:");
      printf(out);
    }
    out.println("An optimal alignment:");
    String[] match = getMatch();
    out.println(match[0]);
    out.println(match[1]);
    out.println();
  }

  public void domatch(Output out, String msg)
  { domatch(out, msg, true); }

  // Get the next state in the traceback
  public Traceback next(Traceback tb)
  { return tb; }                // dummy implementation for the `smart' algs.

  // Return the score of the best alignment
  public abstract int getScore();

  // Print the matrix (matrices) used to compute the alignment
  public abstract void printf(Output out);

  // Auxiliary functions
  static int max(int x1, int x2)
  { return (x1 > x2 ? x1 : x2); }

  static int max(int x1, int x2, int x3)
  { return max(x1, max(x2, x3)); }

  static int max(int x1, int x2, int x3, int x4)
  { return max(max(x1, x2), max(x3, x4)); }

  static String padLeft(String s, int width) {
    int filler = width - s.length();
    if (filler > 0) {           // and therefore width > 0
      StringBuffer res = new StringBuffer(width);
      for (int i=0; i<filler; i++)
        res.append(' ');
      return res.append(s).toString();
    } else
      return s;
  }
}


// Alignment with simple gap costs

abstract class AlignSimple extends Align {
  int[][] F;                    // the matrix used to compute the alignment
  Traceback2[][] B;             // the traceback matrix

  public AlignSimple(Substitution sub, int d, String seq1, String seq2) {
    super(sub, d, seq1, seq2);
    F = new int[n+1][m+1];
    B = new Traceback2[n+1][m+1];
  }

  public Traceback next(Traceback tb) {
    Traceback2 tb2 = (Traceback2)tb;
    return B[tb2.i][tb2.j];
  }

  public int getScore()
  { return F[B0.i][B0.j]; }

  public void printf(Output out) {
    for (int j=0; j<=m; j++) {
      for (int i=0; i<F.length; i++)
	out.print(padLeft(fmtscore(F[i][j]), 5));
      out.println();
    }
  }
}


// Traceback objects

abstract class Traceback {
  int i, j;                     // absolute coordinates
}


// Traceback2 objects for simple gap costs

class Traceback2 extends Traceback {
  public Traceback2(int i, int j)
  { this.i = i; this.j = j; }
}


// Auxiliary classes for output

abstract class Output {
  public abstract void print(String s);
  public abstract void println(String s);
  public abstract void println();
}

class SystemOut extends Output {
  public void print(String s)
  { System.out.print(s); }

  public void println(String s)
  { System.out.println(s); }

  public void println()
  { System.out.println(); }
}


// Global alignment with the Needleman-Wunsch algorithm (simple gap costs)

class NW extends AlignSimple {

  public NW(Substitution sub, int d, String sq1, String sq2) {
    super(sub, d, sq1, sq2);
    int n = this.n, m = this.m;
    int[][] score = sub.score;
    for (int i=1; i<=n; i++) {
      F[i][0] = -d * i;
      B[i][0] = new Traceback2(i-1, 0);
    }
    for (int j=1; j<=m; j++) {
      F[0][j] = -d * j;
      B[0][j] = new Traceback2(0, j-1);
    }
    for (int i=1; i<=n; i++)
      for (int j=1; j<=m; j++) {
        int s = score[seq1.charAt(i-1)][seq2.charAt(j-1)];
        int val = max(F[i-1][j-1]+s, F[i-1][j]-d, F[i][j-1]-d);
        F[i][j] = val;
        if (val == F[i-1][j-1]+s)
          B[i][j] = new Traceback2(i-1, j-1);
        else if (val == F[i-1][j]-d)
          B[i][j] = new Traceback2(i-1, j);
        else if (val == F[i][j-1]-d)
          B[i][j] = new Traceback2(i, j-1);
        else
          throw new Error("NW 1");
      }
    B0 = new Traceback2(n, m); 
  }
}


// Local alignment with the Smith-Waterman algorithm (simple gap costs)

class SW extends AlignSimple {
  public SW(Substitution sub, int d, String sq1, String sq2) {
    super(sub, d, sq1, sq2);
    int n = this.n, m = this.m;
    int[][] score = sub.score;
    int maxi = n, maxj = m;
    int maxval = NegInf;
    for (int i=1; i<=n; i++)
      for (int j=1; j<=m; j++) {
        int s = score[seq1.charAt(i-1)][seq2.charAt(j-1)];
        int val = max(0, F[i-1][j-1]+s, F[i-1][j]-d, F[i][j-1]-d);
        F[i][j] = val;
        if (val == 0)
          B[i][j] = null;
        else if (val == F[i-1][j-1]+s)
          B[i][j] = new Traceback2(i-1, j-1);
        else if (val == F[i-1][j]-d)
          B[i][j] = new Traceback2(i-1, j);
        else if (val == F[i][j-1]-d)
          B[i][j] = new Traceback2(i, j-1);
        else
          throw new Error("SW 1");
        if (val > maxval) {
          maxval = val;
          maxi = i; maxj = j;
        }
      }
    B0 = new Traceback2(maxi, maxj);
  }
}

The bolded part is the part I need to file read it instead of hard coding it. Can someone help me with it?

I start by making a small program that only reads the data into an array and then prints out the contents of the array. For printing the array see the Arrays.toString() method.

The Scanner class can be easy to use to read in lines of data.
Then use the String split method to parse the "words" on each line into an array and then the Integer parseInt method to create ints.

I agree. You don't even need to make a separate program. You can make a method in your current program that opens a file and returns an array. Make a little driver program whose only purpose is to call that method and print out the array.

When it looks like you've got something that works (when the output is correct) you can just replace your current array assignment with a call to that method, and it should work slick as you please.

The only caution I see is that you're dealing with a 2D array, so you'll have a bit of complication in reading the array. Basically you're going to do as Norm says, but here's how I would describe it:

Your data file will have a series of rows, each row represents a row of your array.
So it'll look like this, based on your posting:

5
-2, 7
-1,-1, 7
-2,-2, 2, 8
-1,-4,-2,-4,13
....
0,-3,-3,-4,-1,-3,-3,-4,-4, 4, 1,-3, 1,-1,-3,-2, 0,-3,-1, 5

You'll start by declaring an int[][] array. Your method will read all of the lines of the file (ie, while (hasNext()) and split each string into an array. The arrays will be assigned into the rows of your 2D array. When you've read all of the rows, return the 2D array.

I hope this is clear. Good luck.

Edit:

Just to make sure you follow me, I'm suggesting that you make a method in your Substitution class, which will look like this:

public int[][] loadArrayFromFile(String file)      // get file name as parameter
{
  // you write the file loading code!
}

and then you call that from a little class called LoadArrayDriver.java, which looks something like this:

public class LoadArrayDriver
{
  public static void main(String[] args)
  {
    Substitution b = new Blosum50();
    int[][] theArray = b.loadArrayFromFile("filename");
    //print the file
  }
}

Blosum50 will have access to the loadArray method, so I think this should work. When your test driver prints out something that looks like it's right, just replace the assignment in the real class with a call to the loadArray with the correct filename.

Thanks for the suggestion! I will try it out first and see if it works. I will come back here for more help when I need. (:

abstract class Substitution {
	
  String scoringmatrix;
  	
  public int[][] score;

  void buildscore(String residues, int[][] residuescores) {
    // Allow lowercase and uppercase residues (ASCII code <= 127):
    score = new int[127][127];
    for (int i=0; i<residues.length(); i++) {
      char res1 = residues.charAt(i);
      for (int j=0; j<=i; j++) {
        char res2 = residues.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
      }
    }
  }

  abstract public String getResidues();
  
	public int[][] loadArrayFromFile(String file)      // get file name as parameter
	{
		try
		{
			BufferedReader blosum = new BufferedReader(new FileReader("D:\\project1\\Dynamic Programming\\Alignment Algorithms\\Blosum50.txt"));	//reading files in specified directory	
			
			String input;
			while ((input = blosum.readLine()) != null)	//file reading
			{
  				scoringmatrix = input;
  				System.out.print(scoringmatrix);			
  			}
  			blosum.close();
			
			
		}catch( IOException ioException ) {}	
			
		[B]return ;	[/B]	}

  
}

Hi all, this is my codes so far, for the file loading code. However, they said I need a return type. May I know which one do I return? I tried 'scoringmatrix', 'input', 'blosum', but I get an error saying "incompatible types".

You need to return something which matches the return type you declared for the method. In this case, you need a 2D array of int. You haven't got one yet, so you're going to have to build one.

I did build one already right? Erm, this.
public int[][] loadArrayFromFile(String file)

No, that's a part of the method signature. That's where you promise the compiler that you're going to give it an array of arrays if integers at the end of this method.

Like this:

public int returnsAnInteger(float f, int p)  
{
  int theIntToReturn;   // here's an integer variable
  theIntToReturn = (f - Math.floor(Math.abs(f))) * Math.pow(10,p));
  return Math.floor(theIntToReturn) % Math.pow(10,p);
}

The code in there is just some silliness - it'll probably extract the pth decimal place of f, unless I made a mistake. But look at the method signature:

public int returnsAnInteger(float f, int p)

and the last line of the method:

return Math.floor(theIntToReturn) % Math.pow(10,p);


The signature promises an int, the return delivers on that promise.

So in your method, you've promised an int[][]. You have to deliver one.

Okay I get what you mean now. Thank you!

No sweat. Have a good evening. (I think you're about 12 hours out of sync with east coast of US, right?)

Hey guys, now I have another whole new problem.
Instead of using the substitution matrix Blosum50, I need to use another substitution matrix stored in a txt file. Thus, I need to read the file and just directly pluck in the values in the file. The values in the file has decimal points. I have attached the substitution matrix scores that I need to read from, and then pluck in all the values from the txt file to my program itself. May I know how to do that? I guess the bold part of the codes are the part that I need to do some changes.

// Compile with:
//      javac Match2.java
// Run with:
//      java Match2 HEAGAWGHEE PAWHEAE

//  Class hierarchies
//  -----------------
//       Align              general pairwise alignment
//     AlignSimple          alignment with simple gap costs
//        NW                global alignment with simple gap costs
//        SW                local alignment with simple gap costs
//  Traceback               traceback pointers
//     Traceback2           traceback for simple gap costs
//     Traceback3           traceback for affine gap costs
//  Substitution            substitution matrices with fast lookup
//     Blosum50             the BLOSUM50 substitution matrix
//  Output                  general text output
//     SystemOut            output to the console (in the application)
//     TextAreaOut          output to a TextArea (in the applet)

// Notational conventions:
//   i in {0..n} indexes columns and sequence seq1
//   j in {0..m} indexes rows    and sequence seq2
//   k in {0..2} indexes states (in affine alignment)


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

class MatchApplet extends JApplet {


	TextArea outputArea;
	JButton button;
	JButton reset;

	JTextField tF1;
	JTextField tF2;

	JLabel l1;
	JLabel l2;

	String s1;
	String s2;	
	

	public static void main(String[] args)
	{
		//Application for program
  		Frame f = new Frame();
  		f.addWindowListener(new java.awt.event.WindowAdapter()
  		{
       	public void windowClosing(java.awt.event.WindowEvent e)
       	{
       	System.exit(0);
      	};
     	});

  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program
		
	}//end of public static void main(String[] args)  	
  	


  	public void init() 
	{	
  	
  		String display1="";
  		String display2="";
  			
		try
		{
       	    BufferedReader in1 = new BufferedReader(new FileReader("D:\\project1\\Dynamic Programming\\Alignment Algorithms\\Sequence Testing\\1a00(50).txt"));	//reading files in specified directory	
  			BufferedReader in2 = new BufferedReader(new FileReader("D:\\project1\\Dynamic Programming\\Alignment Algorithms\\Sequence Testing\\1a0a(50).txt"));	//reading files in specified directory	
  			
  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
  				display1 = str1;
  				System.out.print(display1);			
  			}
  			in1.close();
  			
  			System.out.println("");
  			
			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
  				display2 = str2;
  				System.out.print(display2);			
  			}
			in2.close();
			
			System.out.println("");
			

		Container c = getContentPane();
		c.setLayout(new FlowLayout());

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

		tF1 = new JTextField(display1);		
		tF2 = new JTextField(display2);		
		
		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);


	final Substitution sub = new Blosum50();

		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }

	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }

	  		public void println()
	  		{ outputArea.append("\n"); }
		};

	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");
      
      
      }catch( IOException ioException ) {}
      
  }//end of init()
  
}//end of class




[B]// The class of substitution (scoring) matrices

abstract class Substitution {
	
  String scoringmatrix;
  	
  public int[][] score;

  void buildscore(String residues, int[][] residuescores) {
    // Allow lowercase and uppercase residues (ASCII code <= 127):
    score = new int[127][127];
    for (int i=0; i<residues.length(); i++) {
      char res1 = residues.charAt(i);
      for (int j=0; j<=i; j++) {
        char res2 = residues.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
      }
    }
  }

  abstract public String getResidues();

  
}


// The BLOSUM50 substitution matrix for amino acids (Durbin et al, p 16)

class Blosum50 extends Substitution {

  private String residues = "ARNDCQEGHILKMFPSTWYV";

  public String getResidues()
  { return residues; }

  private int[][] residuescores =
            /* A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V */
  { /* A */ {  5                                                          },
    /* R */ { -2, 7                                                       },
    /* N */ { -1,-1, 7                                                    },
    /* D */ { -2,-2, 2, 8                                                 },
    /* C */ { -1,-4,-2,-4,13                                              },
    /* Q */ { -1, 1, 0, 0,-3, 7                                           },
    /* E */ { -1, 0, 0, 2,-3, 2, 6                                        },
    /* G */ {  0,-3, 0,-1,-3,-2,-3, 8                                     },
    /* H */ { -2, 0, 1,-1,-3, 1, 0,-2,10                                  },
    /* I */ { -1,-4,-3,-4,-2,-3,-4,-4,-4, 5                               },
    /* L */ { -2,-3,-4,-4,-2,-2,-3,-4,-3, 2, 5                            },
    /* K */ { -1, 3, 0,-1,-3, 2, 1,-2, 0,-3,-3, 6                         },
    /* M */ { -1,-2,-2,-4,-2, 0,-2,-3,-1, 2, 3,-2, 7                      },
    /* F */ { -3,-3,-4,-5,-2,-4,-3,-4,-1, 0, 1,-4, 0, 8                   },
    /* P */ { -1,-3,-2,-1,-4,-1,-1,-2,-2,-3,-4,-1,-3,-4,10                },
    /* S */ {  1,-1, 1, 0,-1, 0,-1, 0,-1,-3,-3, 0,-2,-3,-1, 5             },
    /* T */ {  0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 2, 5          },
    /* W */ { -3,-3,-4,-5,-5,-1,-3,-3,-3,-3,-2,-3,-1, 1,-4,-4,-3,15       },
    /* Y */ { -2,-1,-2,-3,-3,-1,-2,-3, 2,-1,-1,-2, 0, 4,-3,-2,-2, 2, 8    },
    /* V */ {  0,-3,-3,-4,-1,-3,-3,-4,-4, 4, 1,-3, 1,-1,-3,-2, 0,-3,-1, 5 }
            /* A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V */
  };

  public Blosum50()
  { buildscore(residues, residuescores); }
}[/B]
// Pairwise sequence alignment

abstract class Align {
  Substitution sub;             // substitution matrix
  int d;                        // gap cost
  String seq1, seq2;            // the sequences
  int n, m;                     // their lengths
  Traceback B0;                 // the starting point of the traceback

  final static int NegInf = Integer.MIN_VALUE/2; // negative infinity

  public Align(Substitution sub, int d, String seq1, String seq2) {
    this.sub = sub;
    this.seq1 = strip(seq1); this.seq2 = strip(seq2);
    this.d = d;
    this.n = this.seq1.length(); this.m = this.seq2.length();
  }

  public String strip(String s) {
    boolean[] valid = new boolean[127];
    String residues = sub.getResidues();
    for (int i=0; i<residues.length(); i++) {
      char c = residues.charAt(i);
      if (c < 96)
	valid[c] = valid[c+32] = true;
      else
	valid[c-32] = valid[c] = true;
    }
    StringBuffer res = new StringBuffer(s.length());
    for (int i=0; i<s.length(); i++)
      if (valid[s.charAt(i)])
	res.append(s.charAt(i));
    return res.toString();
  }

  // Return two-element array containing an alignment with maximal score

  public String[] getMatch() {
    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    Traceback tb = B0;
    int i = tb.i, j = tb.j;
    while ((tb = next(tb)) != null) {
      if (i == tb.i)
        res1.append('-');
      else
        res1.append(seq1.charAt(i-1));
      if (j == tb.j)
        res2.append('-');
      else
        res2.append(seq2.charAt(j-1));
      i = tb.i; j = tb.j;
    }
    String[] res = { res1.reverse().toString(), res2.reverse().toString() };
    return res;
  }

  public String fmtscore(int val) {
    if (val < NegInf/2)
      return "-Inf";
    else
      return Integer.toString(val);
  }

  // Print the score, the F matrix, and the alignment
  public void domatch(Output out, String msg, boolean udskrivF) {
    out.println(msg + ":");
    out.println("Score = " + getScore());
    if (udskrivF) {
      out.println("The F matrix:");
      printf(out);
    }
    out.println("An optimal alignment:");
    String[] match = getMatch();
    out.println(match[0]);
    out.println(match[1]);
    out.println();
  }

  public void domatch(Output out, String msg)
  { domatch(out, msg, true); }

  // Get the next state in the traceback
  public Traceback next(Traceback tb)
  { return tb; }                // dummy implementation for the `smart' algs.

  // Return the score of the best alignment
  public abstract int getScore();

  // Print the matrix (matrices) used to compute the alignment
  public abstract void printf(Output out);

  // Auxiliary functions
  static int max(int x1, int x2)
  { return (x1 > x2 ? x1 : x2); }

  static int max(int x1, int x2, int x3)
  { return max(x1, max(x2, x3)); }

  static int max(int x1, int x2, int x3, int x4)
  { return max(max(x1, x2), max(x3, x4)); }

  static String padLeft(String s, int width) {
    int filler = width - s.length();
    if (filler > 0) {           // and therefore width > 0
      StringBuffer res = new StringBuffer(width);
      for (int i=0; i<filler; i++)
        res.append(' ');
      return res.append(s).toString();
    } else
      return s;
  }
}


// Alignment with simple gap costs

abstract class AlignSimple extends Align {
  int[][] F;                    // the matrix used to compute the alignment
  Traceback2[][] B;             // the traceback matrix

  public AlignSimple(Substitution sub, int d, String seq1, String seq2) {
    super(sub, d, seq1, seq2);
    F = new int[n+1][m+1];
    B = new Traceback2[n+1][m+1];
  }

  public Traceback next(Traceback tb) {
    Traceback2 tb2 = (Traceback2)tb;
    return B[tb2.i][tb2.j];
  }

  public int getScore()
  { return F[B0.i][B0.j]; }

  public void printf(Output out) {
    for (int j=0; j<=m; j++) {
      for (int i=0; i<F.length; i++)
	out.print(padLeft(fmtscore(F[i][j]), 5));
      out.println();
    }
  }
}


// Traceback objects

abstract class Traceback {
  int i, j;                     // absolute coordinates
}


// Traceback2 objects for simple gap costs

class Traceback2 extends Traceback {
  public Traceback2(int i, int j)
  { this.i = i; this.j = j; }
}


// Auxiliary classes for output

abstract class Output {
  public abstract void print(String s);
  public abstract void println(String s);
  public abstract void println();
}

class SystemOut extends Output {
  public void print(String s)
  { System.out.print(s); }

  public void println(String s)
  { System.out.println(s); }

  public void println()
  { System.out.println(); }
}


// Global alignment with the Needleman-Wunsch algorithm (simple gap costs)

class NW extends AlignSimple {

  public NW(Substitution sub, int d, String sq1, String sq2) {
    super(sub, d, sq1, sq2);
    int n = this.n, m = this.m;
    int[][] score = sub.score;
    for (int i=1; i<=n; i++) {
      F[i][0] = -d * i;
      B[i][0] = new Traceback2(i-1, 0);
    }
    for (int j=1; j<=m; j++) {
      F[0][j] = -d * j;
      B[0][j] = new Traceback2(0, j-1);
    }
    for (int i=1; i<=n; i++)
      for (int j=1; j<=m; j++) {
        int s = score[seq1.charAt(i-1)][seq2.charAt(j-1)];
        int val = max(F[i-1][j-1]+s, F[i-1][j]-d, F[i][j-1]-d);
        F[i][j] = val;
        if (val == F[i-1][j-1]+s)
          B[i][j] = new Traceback2(i-1, j-1);
        else if (val == F[i-1][j]-d)
          B[i][j] = new Traceback2(i-1, j);
        else if (val == F[i][j-1]-d)
          B[i][j] = new Traceback2(i, j-1);
        else
          throw new Error("NW 1");
      }
    B0 = new Traceback2(n, m); 
  }
}


// Local alignment with the Smith-Waterman algorithm (simple gap costs)

class SW extends AlignSimple {
  public SW(Substitution sub, int d, String sq1, String sq2) {
    super(sub, d, sq1, sq2);
    int n = this.n, m = this.m;
    int[][] score = sub.score;
    int maxi = n, maxj = m;
    int maxval = NegInf;
    for (int i=1; i<=n; i++)
      for (int j=1; j<=m; j++) {
        int s = score[seq1.charAt(i-1)][seq2.charAt(j-1)];
        int val = max(0, F[i-1][j-1]+s, F[i-1][j]-d, F[i][j-1]-d);
        F[i][j] = val;
        if (val == 0)
          B[i][j] = null;
        else if (val == F[i-1][j-1]+s)
          B[i][j] = new Traceback2(i-1, j-1);
        else if (val == F[i-1][j]-d)
          B[i][j] = new Traceback2(i-1, j);
        else if (val == F[i][j-1]-d)
          B[i][j] = new Traceback2(i, j-1);
        else
          throw new Error("SW 1");
        if (val > maxval) {
          maxval = val;
          maxi = i; maxj = j;
        }
      }
    B0 = new Traceback2(maxi, maxj);
  }

the substitution matrix scores that I need to read from, and then pluck in all the values from the txt file to my program itself.

Can you describe what the problem is with your current code?

Can you reduce the code for your current problem to a small program that demonstrates the problem and that can be compiled and executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.