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