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
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
103 * Creates a new AlignSeq object.
\r
105 * @param s1 DOCUMENT ME!
\r
106 * @param s2 DOCUMENT ME!
\r
107 * @param type DOCUMENT ME!
\r
109 public AlignSeq(SequenceI s1, SequenceI s2, String type)
\r
111 rt = Runtime.getRuntime();
\r
112 SeqInit(s1, s2, type);
\r
118 * @return DOCUMENT ME!
\r
120 public int getMaxScore()
\r
128 * @return DOCUMENT ME!
\r
130 public int getSeq2Start()
\r
138 * @return DOCUMENT ME!
\r
140 public int getSeq2End()
\r
148 * @return DOCUMENT ME!
\r
150 public int getSeq1Start()
\r
158 * @return DOCUMENT ME!
\r
160 public int getSeq1End()
\r
168 * @return DOCUMENT ME!
\r
170 public String getOutput()
\r
178 * @return DOCUMENT ME!
\r
180 public String getAStr1()
\r
188 * @return DOCUMENT ME!
\r
190 public String getAStr2()
\r
198 * @return DOCUMENT ME!
\r
200 public int[] getASeq1()
\r
208 * @return DOCUMENT ME!
\r
210 public int[] getASeq2()
\r
218 * @return DOCUMENT ME!
\r
220 public SequenceI getS1()
\r
228 * @return DOCUMENT ME!
\r
230 public SequenceI getS2()
\r
238 * @param s1 DOCUMENT ME!
\r
239 * @param s2 DOCUMENT ME!
\r
240 * @param type DOCUMENT ME!
\r
242 public void SeqInit(SequenceI s1, SequenceI s2, String type)
\r
244 s1str = extractGaps(jalview.util.Comparison.GapChars, s1.getSequence());
\r
245 s2str = extractGaps(jalview.util.Comparison.GapChars, s2.getSequence());
\r
252 if (type.equals("pep"))
\r
254 lookup = ResidueProperties.getBLOSUM62();
\r
258 else if (type.equals("dna"))
\r
260 lookup = ResidueProperties.getDNA();
\r
266 output = output + ("Wrong type = dna or pep only");
\r
270 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
\r
271 seq1 = new int[s1str.length()];
\r
273 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
\r
274 seq2 = new int[s2str.length()];
\r
276 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
277 score = new int[s1str.length()][s2str.length()];
\r
279 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
\r
280 E = new int[s1str.length()][s2str.length()];
\r
282 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
\r
283 F = new int[s1str.length()][s2str.length()];
\r
284 traceback = new int[s1str.length()][s2str.length()];
\r
286 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
\r
287 seq1 = stringToInt(s1str, type);
\r
289 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
\r
290 seq2 = stringToInt(s2str, type);
\r
292 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
293 // long tstart = System.currentTimeMillis();
\r
294 // calcScoreMatrix();
\r
295 //long tend = System.currentTimeMillis();
\r
296 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
\r
297 // printScoreMatrix(score);
\r
298 //System.out.println();
\r
299 //printScoreMatrix(traceback);
\r
300 //System.out.println();
\r
301 // printScoreMatrix(E);
\r
302 //System.out.println();
\r
303 ///printScoreMatrix(F);
\r
304 //System.out.println();
\r
305 // tstart = System.currentTimeMillis();
\r
306 //traceAlignment();
\r
307 //tend = System.currentTimeMillis();
\r
308 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
\r
314 public void traceAlignment()
\r
316 // Find the maximum score along the rhs or bottom row
\r
319 for (int i = 0; i < seq1.length; i++)
\r
321 if (score[i][seq2.length - 1] > max)
\r
323 max = score[i][seq2.length - 1];
\r
325 maxj = seq2.length - 1;
\r
329 for (int j = 0; j < seq2.length; j++)
\r
331 if (score[seq1.length - 1][j] > max)
\r
333 max = score[seq1.length - 1][j];
\r
334 maxi = seq1.length - 1;
\r
339 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
\r
343 maxscore = score[i][j] / 10;
\r
345 seq1end = maxi + 1;
\r
346 seq2end = maxj + 1;
\r
348 aseq1 = new int[seq1.length + seq2.length];
\r
349 aseq2 = new int[seq1.length + seq2.length];
\r
351 count = (seq1.length + seq2.length) - 1;
\r
353 while ((i > 0) && (j > 0))
\r
355 if ((aseq1[count] != defInt) && (i >= 0))
\r
357 aseq1[count] = seq1[i];
\r
358 astr1 = intToStr[seq1[i]] + astr1;
\r
361 if ((aseq2[count] != defInt) && (j > 0))
\r
363 aseq2[count] = seq2[j];
\r
364 astr2 = intToStr[seq2[j]] + astr2;
\r
367 trace = findTrace(i, j);
\r
374 else if (trace == 1)
\r
377 aseq1[count] = defInt;
\r
378 astr1 = "-" + astr1.substring(1);
\r
380 else if (trace == -1)
\r
383 aseq2[count] = defInt;
\r
384 astr2 = "-" + astr2.substring(1);
\r
393 if (aseq1[count] != defInt)
\r
395 aseq1[count] = seq1[i];
\r
396 astr1 = intToStr[seq1[i]] + astr1;
\r
399 if (aseq2[count] != defInt)
\r
401 aseq2[count] = seq2[j];
\r
402 astr2 = intToStr[seq2[j]] + astr2;
\r
409 public void printAlignment()
\r
411 // Find the biggest id length for formatting purposes
\r
412 int maxid = s1.getName().length();
\r
414 if (s2.getName().length() > maxid)
\r
416 maxid = s2.getName().length();
\r
419 int len = 72 - maxid - 1;
\r
420 int nochunks = ((aseq1.length - count) / len) + 1;
\r
423 output = output + ("Score = " + score[maxi][maxj] + "\n");
\r
425 ("Length of alignment = " + (aseq1.length - count) + "\n");
\r
426 output = output + ("Sequence ");
\r
427 output = output + (new Format("%" + maxid + "s").form(s1.getName()));
\r
429 (" : " + seq1start + " - " + seq1end + " (Sequence length = " +
\r
430 s1str.length() + ")\n");
\r
431 output = output + ("Sequence ");
\r
432 output = output + (new Format("%" + maxid + "s").form(s2.getName()));
\r
434 (" : " + seq2start + " - " + seq2end + " (Sequence length = " +
\r
435 s2str.length() + ")\n\n");
\r
437 for (int j = 0; j < nochunks; j++)
\r
439 // Print the first aligned sequence
\r
441 (new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
\r
443 for (int i = 0; i < len; i++)
\r
445 if ((count + i + (j * len)) < aseq1.length)
\r
448 (new Format("%s").form(intToStr[aseq1[count + i +
\r
453 output = output + ("\n");
\r
455 (new Format("%" + (maxid) + "s").form(" ") + " ");
\r
457 // Print out the matching chars
\r
458 for (int i = 0; i < len; i++)
\r
460 if ((count + i + (j * len)) < aseq1.length)
\r
462 if (intToStr[aseq1[count + i + (j * len)]].equals(
\r
463 intToStr[aseq2[count + i + (j * len)]]) &&
\r
464 !intToStr[aseq1[count + i + (j * len)]].equals("-"))
\r
467 output = output + ("|");
\r
469 else if (type.equals("pep"))
\r
471 if (ResidueProperties.getPAM250(
\r
472 intToStr[aseq1[count + i + (j * len)]],
\r
473 intToStr[aseq2[count + i + (j * len)]]) > 0)
\r
475 output = output + (".");
\r
479 output = output + (" ");
\r
484 output = output + (" ");
\r
489 // Now print the second aligned sequence
\r
490 output = output + ("\n");
\r
492 (new Format("%" + (maxid) + "s").form(s2.getName()) + " ");
\r
494 for (int i = 0; i < len; i++)
\r
496 if ((count + i + (j * len)) < aseq1.length)
\r
499 (new Format("%s").form(intToStr[aseq2[count + i +
\r
504 output = output + ("\n\n");
\r
507 pid = pid / (float) (aseq1.length - count) * 100;
\r
508 output = output + (new Format("Percentage ID = %2.2f\n\n").form(pid));
\r
514 * @param mat DOCUMENT ME!
\r
516 public void printScoreMatrix(int[][] mat)
\r
518 int n = seq1.length;
\r
519 int m = seq2.length;
\r
521 for (int i = 0; i < n; i++)
\r
523 // Print the top sequence
\r
526 Format.print(System.out, "%8s", s2str.substring(0, 1));
\r
528 for (int jj = 1; jj < m; jj++)
\r
530 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
\r
533 System.out.println();
\r
536 for (int j = 0; j < m; j++)
\r
540 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
\r
543 Format.print(System.out, "%3d ", mat[i][j] / 10);
\r
546 System.out.println();
\r
553 * @param i DOCUMENT ME!
\r
554 * @param j DOCUMENT ME!
\r
556 * @return DOCUMENT ME!
\r
558 public int findTrace(int i, int j)
\r
561 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
\r
568 else if (F[i][j] == max)
\r
577 if (E[i][j] >= max)
\r
582 else if (E[i][j] == max)
\r
599 public void calcScoreMatrix()
\r
601 int n = seq1.length;
\r
602 int m = seq2.length;
\r
604 // top left hand element
\r
605 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
\r
606 E[0][0] = -gapExtend;
\r
609 // Calculate the top row first
\r
610 for (int j = 1; j < m; j++)
\r
612 // What should these values be? 0 maybe
\r
613 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
\r
614 F[0][j] = -gapExtend;
\r
616 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen,
\r
619 traceback[0][j] = 1;
\r
622 // Now do the left hand column
\r
623 for (int i = 1; i < n; i++)
\r
625 E[i][0] = -gapOpen;
\r
626 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
\r
628 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
\r
629 traceback[i][0] = -1;
\r
632 // Now do all the other rows
\r
633 for (int i = 1; i < n; i++)
\r
635 for (int j = 1; j < m; j++)
\r
637 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] -
\r
639 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] -
\r
642 score[i][j] = max(score[i - 1][j - 1] +
\r
643 (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
\r
644 traceback[i][j] = findTrace(i, j);
\r
654 * @param gapChar DOCUMENT ME!
\r
655 * @param seq DOCUMENT ME!
\r
657 * @return DOCUMENT ME!
\r
659 public static String extractGaps(String gapChar, String seq)
\r
661 StringTokenizer str = new StringTokenizer(seq, gapChar);
\r
662 String newString = "";
\r
664 while (str.hasMoreTokens())
\r
666 newString = newString + str.nextToken();
\r
675 * @param i1 DOCUMENT ME!
\r
676 * @param i2 DOCUMENT ME!
\r
677 * @param i3 DOCUMENT ME!
\r
679 * @return DOCUMENT ME!
\r
681 public int max(int i1, int i2, int i3)
\r
701 * @param i1 DOCUMENT ME!
\r
702 * @param i2 DOCUMENT ME!
\r
704 * @return DOCUMENT ME!
\r
706 public int max(int i1, int i2)
\r
721 * @param s DOCUMENT ME!
\r
722 * @param type DOCUMENT ME!
\r
724 * @return DOCUMENT ME!
\r
726 public int[] stringToInt(String s, String type)
\r
728 int[] seq1 = new int[s.length()];
\r
730 for (int i = 0; i < s.length(); i++)
\r
732 String ss = s.substring(i, i + 1).toUpperCase();
\r
736 if (type.equals("pep"))
\r
738 seq1[i] = ((Integer) ResidueProperties.aaHash.get(ss)).intValue();
\r
740 else if (type.equals("dna"))
\r
742 seq1[i] = ((Integer) dnaHash.get(ss)).intValue();
\r
750 catch (Exception e)
\r
752 if (type.equals("dna"))
\r
769 * @param g DOCUMENT ME!
\r
770 * @param mat DOCUMENT ME!
\r
771 * @param n DOCUMENT ME!
\r
772 * @param m DOCUMENT ME!
\r
773 * @param psize DOCUMENT ME!
\r
775 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
\r
781 for (int i = 0; i < n; i++)
\r
783 for (int j = 0; j < m; j++)
\r
785 if (mat[i][j] >= max)
\r
790 if (mat[i][j] <= min)
\r
797 System.out.println(max + " " + min);
\r
799 for (int i = 0; i < n; i++)
\r
801 for (int j = 0; j < m; j++)
\r
806 // System.out.println(mat[i][j]);
\r
807 float score = (float) (mat[i][j] - min) / (float) (max - min);
\r
808 g.setColor(new Color(score, 0, 0));
\r
809 g.fillRect(x, y, psize, psize);
\r
811 // System.out.println(x + " " + y + " " + score);
\r