2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
21 import jalview.datamodel.SequenceI;
23 import jalview.schemes.*;
25 import jalview.util.*;
41 public static java.util.Hashtable dnaHash = new java.util.Hashtable();
45 dnaHash.put("C", new Integer(0));
46 dnaHash.put("T", new Integer(1));
47 dnaHash.put("A", new Integer(2));
48 dnaHash.put("G", new Integer(3));
49 dnaHash.put("-", new Integer(4));
52 static String[] dna = { "C", "T", "A", "G", "-" };
55 "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
56 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-"
72 public String astr1="";
73 public String astr2="";
94 int[][] lookup = ResidueProperties.getBLOSUM62();
95 String[] intToStr = pep;
97 StringBuffer output = new StringBuffer();
101 * Creates a new AlignSeq object.
103 * @param s1 DOCUMENT ME!
104 * @param s2 DOCUMENT ME!
105 * @param type DOCUMENT ME!
107 public AlignSeq(SequenceI s1, SequenceI s2, String type)
109 SeqInit(s1, s1.getSequence(), s2, s2.getSequence(), type);
113 * Creates a new AlignSeq object.
115 * @param s1 DOCUMENT ME!
116 * @param s2 DOCUMENT ME!
117 * @param type DOCUMENT ME!
119 public AlignSeq(SequenceI s1,
125 SeqInit(s1, string1, s2, string2, type);
131 * @return DOCUMENT ME!
133 public int getMaxScore()
141 * @return DOCUMENT ME!
143 public int getSeq2Start()
151 * @return DOCUMENT ME!
153 public int getSeq2End()
161 * @return DOCUMENT ME!
163 public int getSeq1Start()
171 * @return DOCUMENT ME!
173 public int getSeq1End()
181 * @return DOCUMENT ME!
183 public String getOutput()
185 return output.toString();
191 * @return DOCUMENT ME!
193 public String getAStr1()
201 * @return DOCUMENT ME!
203 public String getAStr2()
211 * @return DOCUMENT ME!
213 public int[] getASeq1()
221 * @return DOCUMENT ME!
223 public int[] getASeq2()
231 * @return DOCUMENT ME!
233 public SequenceI getS1()
241 * @return DOCUMENT ME!
243 public SequenceI getS2()
251 * @param s1 DOCUMENT ME!
252 * @param s2 DOCUMENT ME!
253 * @param type DOCUMENT ME!
255 public void SeqInit(SequenceI s1,
262 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
263 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
265 if(s1str.length()==0 || s2str.length()==0)
267 System.out.println("ALL GAPS: " +
268 (s1str.length()==0?s1.getName():" ")
269 +(s2str.length()==0?s2.getName():""));
278 if (type.equals("pep"))
280 lookup = ResidueProperties.getBLOSUM62();
284 else if (type.equals("dna"))
286 lookup = ResidueProperties.getDNA();
292 output.append("Wrong type = dna or pep only");
296 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
297 seq1 = new int[s1str.length()];
299 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
300 seq2 = new int[s2str.length()];
302 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
303 score = new int[s1str.length()][s2str.length()];
305 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
306 E = new int[s1str.length()][s2str.length()];
308 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
309 F = new int[s1str.length()][s2str.length()];
310 traceback = new int[s1str.length()][s2str.length()];
312 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
313 seq1 = stringToInt(s1str, type);
315 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
316 seq2 = stringToInt(s2str, type);
318 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
319 // long tstart = System.currentTimeMillis();
320 // calcScoreMatrix();
321 //long tend = System.currentTimeMillis();
322 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
323 // printScoreMatrix(score);
324 //System.out.println();
325 //printScoreMatrix(traceback);
326 //System.out.println();
327 // printScoreMatrix(E);
328 //System.out.println();
329 ///printScoreMatrix(F);
330 //System.out.println();
331 // tstart = System.currentTimeMillis();
333 //tend = System.currentTimeMillis();
334 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
340 public void traceAlignment()
342 // Find the maximum score along the rhs or bottom row
345 for (int i = 0; i < seq1.length; i++)
347 if (score[i][seq2.length - 1] > max)
349 max = score[i][seq2.length - 1];
351 maxj = seq2.length - 1;
355 for (int j = 0; j < seq2.length; j++)
357 if (score[seq1.length - 1][j] > max)
359 max = score[seq1.length - 1][j];
360 maxi = seq1.length - 1;
365 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
369 maxscore = score[i][j] / 10;
374 aseq1 = new int[seq1.length + seq2.length];
375 aseq2 = new int[seq1.length + seq2.length];
377 count = (seq1.length + seq2.length) - 1;
379 while ((i > 0) && (j > 0))
381 if ((aseq1[count] != defInt) && (i >= 0))
383 aseq1[count] = seq1[i];
384 astr1 = intToStr[seq1[i]] + astr1;
387 if ((aseq2[count] != defInt) && (j > 0))
389 aseq2[count] = seq2[j];
390 astr2 = intToStr[seq2[j]] + astr2;
393 trace = findTrace(i, j);
403 aseq1[count] = defInt;
404 astr1 = "-" + astr1.substring(1);
406 else if (trace == -1)
409 aseq2[count] = defInt;
410 astr2 = "-" + astr2.substring(1);
419 if (aseq1[count] != defInt)
421 aseq1[count] = seq1[i];
422 astr1 = intToStr[seq1[i]] + astr1;
425 if (aseq2[count] != defInt)
427 aseq2[count] = seq2[j];
428 astr2 = intToStr[seq2[j]] + astr2;
435 public void printAlignment(java.io.PrintStream os)
437 // Find the biggest id length for formatting purposes
438 int maxid = s1.getName().length();
440 if (s2.getName().length() > maxid)
442 maxid = s2.getName().length();
445 int len = 72 - maxid - 1;
446 int nochunks = ((aseq1.length - count) / len) + 1;
449 output.append("Score = " + score[maxi][maxj] + "\n");
450 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
451 output.append("Sequence ");
452 output.append(new Format("%" + maxid + "s").form(s1.getName()));
453 output.append(" : " + s1.getStart() + " - " + s1.getEnd() + " (Sequence length = " +
454 s1str.length() + ")\n");
455 output .append("Sequence ");
456 output.append(new Format("%" + maxid + "s").form(s2.getName()));
457 output.append(" : " + s2.getStart() + " - " + s2.getEnd() + " (Sequence length = " +
458 s2str.length() + ")\n\n");
460 for (int j = 0; j < nochunks; j++)
462 // Print the first aligned sequence
463 output.append(new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
465 for (int i = 0; i < len; i++)
467 if ((count + i + (j * len)) < aseq1.length)
469 output.append(new Format("%s").form(intToStr[aseq1[count + i +
475 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
477 // Print out the matching chars
478 for (int i = 0; i < len; i++)
480 if ((count + i + (j * len)) < aseq1.length)
482 if (intToStr[aseq1[count + i + (j * len)]].equals(
483 intToStr[aseq2[count + i + (j * len)]]) &&
484 !intToStr[aseq1[count + i + (j * len)]].equals("-"))
489 else if (type.equals("pep"))
491 if (ResidueProperties.getPAM250(
492 intToStr[aseq1[count + i + (j * len)]],
493 intToStr[aseq2[count + i + (j * len)]]) > 0)
509 // Now print the second aligned sequence
510 output = output.append("\n");
511 output = output.append(new Format("%" + (maxid) + "s").form(s2.getName()) + " ");
513 for (int i = 0; i < len; i++)
515 if ((count + i + (j * len)) < aseq1.length)
517 output .append(new Format("%s").form(intToStr[aseq2[count + i +
522 output = output .append("\n\n");
525 pid = pid / (float) (aseq1.length - count) * 100;
526 output = output.append(new Format("Percentage ID = %2.2f\n\n").form(pid));
529 os.print(output.toString());
530 }catch(Exception ex){}
536 * @param mat DOCUMENT ME!
538 public void printScoreMatrix(int[][] mat)
543 for (int i = 0; i < n; i++)
545 // Print the top sequence
548 Format.print(System.out, "%8s", s2str.substring(0, 1));
550 for (int jj = 1; jj < m; jj++)
552 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
555 System.out.println();
558 for (int j = 0; j < m; j++)
562 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
565 Format.print(System.out, "%3d ", mat[i][j] / 10);
568 System.out.println();
575 * @param i DOCUMENT ME!
576 * @param j DOCUMENT ME!
578 * @return DOCUMENT ME!
580 public int findTrace(int i, int j)
583 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
590 else if (F[i][j] == max)
604 else if (E[i][j] == max)
621 public void calcScoreMatrix()
626 // top left hand element
627 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
628 E[0][0] = -gapExtend;
631 // Calculate the top row first
632 for (int j = 1; j < m; j++)
634 // What should these values be? 0 maybe
635 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
636 F[0][j] = -gapExtend;
638 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen,
644 // Now do the left hand column
645 for (int i = 1; i < n; i++)
648 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
650 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
651 traceback[i][0] = -1;
654 // Now do all the other rows
655 for (int i = 1; i < n; i++)
657 for (int j = 1; j < m; j++)
659 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] -
661 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] -
664 score[i][j] = max(score[i - 1][j - 1] +
665 (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
666 traceback[i][j] = findTrace(i, j);
676 * @param gapChar DOCUMENT ME!
677 * @param seq DOCUMENT ME!
679 * @return DOCUMENT ME!
681 public static String extractGaps(String gapChar, String seq)
683 StringTokenizer str = new StringTokenizer(seq, gapChar);
684 StringBuffer newString = new StringBuffer();
686 while (str.hasMoreTokens())
688 newString.append( str.nextToken() );
691 return newString.toString();
697 * @param i1 DOCUMENT ME!
698 * @param i2 DOCUMENT ME!
699 * @param i3 DOCUMENT ME!
701 * @return DOCUMENT ME!
703 public int max(int i1, int i2, int i3)
723 * @param i1 DOCUMENT ME!
724 * @param i2 DOCUMENT ME!
726 * @return DOCUMENT ME!
728 public int max(int i1, int i2)
743 * @param s DOCUMENT ME!
744 * @param type DOCUMENT ME!
746 * @return DOCUMENT ME!
748 public int[] stringToInt(String s, String type)
750 int[] seq1 = new int[s.length()];
752 for (int i = 0; i < s.length(); i++)
754 String ss = s.substring(i, i + 1).toUpperCase();
758 if (type.equals("pep"))
760 seq1[i] = ((Integer) ResidueProperties.aaHash.get(ss)).intValue();
762 else if (type.equals("dna"))
764 seq1[i] = ((Integer) dnaHash.get(ss)).intValue();
774 if (type.equals("dna"))
791 * @param g DOCUMENT ME!
792 * @param mat DOCUMENT ME!
793 * @param n DOCUMENT ME!
794 * @param m DOCUMENT ME!
795 * @param psize DOCUMENT ME!
797 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
803 for (int i = 0; i < n; i++)
805 for (int j = 0; j < m; j++)
807 if (mat[i][j] >= max)
812 if (mat[i][j] <= min)
819 System.out.println(max + " " + min);
821 for (int i = 0; i < n; i++)
823 for (int j = 0; j < m; j++)
828 // System.out.println(mat[i][j]);
829 float score = (float) (mat[i][j] - min) / (float) (max - min);
830 g.setColor(new Color(score, 0, 0));
831 g.fillRect(x, y, psize, psize);
833 // System.out.println(x + " " + y + " " + score);