2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27 import jalview.util.*;
37 public static final String PEP = "pep";
38 public static final String DNA = "dna";
40 public static java.util.Hashtable dnaHash = new java.util.Hashtable();
44 dnaHash.put("C", new Integer(0));
45 dnaHash.put("T", new Integer(1));
46 dnaHash.put("A", new Integer(2));
47 dnaHash.put("G", new Integer(3));
48 dnaHash.put("-", new Integer(4));
53 "C", "T", "A", "G", "-"};
56 "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
57 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-"
73 public String astr1 = "";
74 public String astr2 = "";
95 int[][] lookup = ResidueProperties.getBLOSUM62();
96 String[] intToStr = pep;
98 StringBuffer output = new StringBuffer();
102 * Creates a new AlignSeq object.
104 * @param s1 DOCUMENT ME!
105 * @param s2 DOCUMENT ME!
106 * @param type DOCUMENT ME!
108 public AlignSeq(SequenceI s1, SequenceI s2, String type)
110 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(), type);
114 * Creates a new AlignSeq object.
116 * @param s1 DOCUMENT ME!
117 * @param s2 DOCUMENT ME!
118 * @param type DOCUMENT ME!
120 public AlignSeq(SequenceI s1,
126 SeqInit(s1, string1, s2, string2, type);
132 * @return DOCUMENT ME!
134 public int getMaxScore()
142 * @return DOCUMENT ME!
144 public int getSeq2Start()
152 * @return DOCUMENT ME!
154 public int getSeq2End()
162 * @return DOCUMENT ME!
164 public int getSeq1Start()
172 * @return DOCUMENT ME!
174 public int getSeq1End()
182 * @return DOCUMENT ME!
184 public String getOutput()
186 return output.toString();
192 * @return DOCUMENT ME!
194 public String getAStr1()
202 * @return DOCUMENT ME!
204 public String getAStr2()
212 * @return DOCUMENT ME!
214 public int[] getASeq1()
222 * @return DOCUMENT ME!
224 public int[] getASeq2()
232 * @return DOCUMENT ME!
234 public SequenceI getS1()
242 * @return DOCUMENT ME!
244 public SequenceI getS2()
252 * @param s1 DOCUMENT ME!
253 * @param string1 - string to align for sequence1
254 * @param s2 sequence 2
255 * @param string2 - string to align for sequence2
256 * @param type DNA or PEPTIDE
258 public void SeqInit(SequenceI s1,
266 setDefaultParams(type);
267 SeqInit(string1, string2);
270 public void SeqInit(SequenceI s1,
274 ScoreMatrix scoreMatrix)
278 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
279 lookup = scoreMatrix.getMatrix();
283 * construct score matrix for string1 and string2 (after removing any existing gaps
287 private void SeqInit(String string1, String string2)
289 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
290 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
292 if (s1str.length() == 0 || s2str.length() == 0)
294 output.append("ALL GAPS: " +
295 (s1str.length() == 0 ? s1.getName() : " ")
296 + (s2str.length() == 0 ? s2.getName() : ""));
300 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
301 seq1 = new int[s1str.length()];
303 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
304 seq2 = new int[s2str.length()];
306 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
307 score = new int[s1str.length()][s2str.length()];
309 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
310 E = new int[s1str.length()][s2str.length()];
312 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
313 F = new int[s1str.length()][s2str.length()];
314 traceback = new int[s1str.length()][s2str.length()];
316 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
317 seq1 = stringToInt(s1str, type);
319 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
320 seq2 = stringToInt(s2str, type);
322 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
323 // long tstart = System.currentTimeMillis();
324 // calcScoreMatrix();
325 //long tend = System.currentTimeMillis();
326 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
327 // printScoreMatrix(score);
328 //System.out.println();
329 //printScoreMatrix(traceback);
330 //System.out.println();
331 // printScoreMatrix(E);
332 //System.out.println();
333 ///printScoreMatrix(F);
334 //System.out.println();
335 // tstart = System.currentTimeMillis();
337 //tend = System.currentTimeMillis();
338 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
341 private void setDefaultParams(String type)
345 if (type.equals(AlignSeq.PEP))
347 lookup = ResidueProperties.getDefaultPeptideMatrix();
349 else if (type.equals(AlignSeq.DNA))
351 lookup = ResidueProperties.getDefaultDnaMatrix();
355 private void setType(String type2)
358 if (type.equals(AlignSeq.PEP))
363 else if (type.equals(AlignSeq.DNA))
370 output.append("Wrong type = dna or pep only");
371 throw new Error("Unknown Type " + type2 +
372 " - dna or pep are the only allowed values.");
379 public void traceAlignment()
381 // Find the maximum score along the rhs or bottom row
384 for (int i = 0; i < seq1.length; i++)
386 if (score[i][seq2.length - 1] > max)
388 max = score[i][seq2.length - 1];
390 maxj = seq2.length - 1;
394 for (int j = 0; j < seq2.length; j++)
396 if (score[seq1.length - 1][j] > max)
398 max = score[seq1.length - 1][j];
399 maxi = seq1.length - 1;
404 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
408 maxscore = score[i][j] / 10;
413 aseq1 = new int[seq1.length + seq2.length];
414 aseq2 = new int[seq1.length + seq2.length];
416 count = (seq1.length + seq2.length) - 1;
418 while ( (i > 0) && (j > 0))
420 if ( (aseq1[count] != defInt) && (i >= 0))
422 aseq1[count] = seq1[i];
423 astr1 = intToStr[seq1[i]] + astr1;
426 if ( (aseq2[count] != defInt) && (j > 0))
428 aseq2[count] = seq2[j];
429 astr2 = intToStr[seq2[j]] + astr2;
432 trace = findTrace(i, j);
442 aseq1[count] = defInt;
443 astr1 = "-" + astr1.substring(1);
445 else if (trace == -1)
448 aseq2[count] = defInt;
449 astr2 = "-" + astr2.substring(1);
458 if (aseq1[count] != defInt)
460 aseq1[count] = seq1[i];
461 astr1 = intToStr[seq1[i]] + astr1;
464 if (aseq2[count] != defInt)
466 aseq2[count] = seq2[j];
467 astr2 = intToStr[seq2[j]] + astr2;
474 public void printAlignment(java.io.PrintStream os)
476 // Find the biggest id length for formatting purposes
477 int maxid = s1.getName().length();
479 if (s2.getName().length() > maxid)
481 maxid = s2.getName().length();
484 int len = 72 - maxid - 1;
485 int nochunks = ( (aseq1.length - count) / len) + 1;
488 output.append("Score = " + score[maxi][maxj] + "\n");
489 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
490 output.append("Sequence ");
491 output.append(new Format("%" + maxid + "s").form(s1.getName()));
492 output.append(" : " + s1.getStart() + " - " + s1.getEnd() +
493 " (Sequence length = " +
494 s1str.length() + ")\n");
495 output.append("Sequence ");
496 output.append(new Format("%" + maxid + "s").form(s2.getName()));
497 output.append(" : " + s2.getStart() + " - " + s2.getEnd() +
498 " (Sequence length = " +
499 s2str.length() + ")\n\n");
501 for (int j = 0; j < nochunks; j++)
503 // Print the first aligned sequence
504 output.append(new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
506 for (int i = 0; i < len; i++)
508 if ( (count + i + (j * len)) < aseq1.length)
510 output.append(new Format("%s").form(intToStr[aseq1[count + i +
516 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
518 // Print out the matching chars
519 for (int i = 0; i < len; i++)
521 if ( (count + i + (j * len)) < aseq1.length)
523 if (intToStr[aseq1[count + i + (j * len)]].equals(
524 intToStr[aseq2[count + i + (j * len)]]) &&
525 !intToStr[aseq1[count + i + (j * len)]].equals("-"))
530 else if (type.equals("pep"))
532 if (ResidueProperties.getPAM250(
533 intToStr[aseq1[count + i + (j * len)]],
534 intToStr[aseq2[count + i + (j * len)]]) > 0)
550 // Now print the second aligned sequence
551 output = output.append("\n");
552 output = output.append(new Format("%" + (maxid) + "s").form(s2.getName()) +
555 for (int i = 0; i < len; i++)
557 if ( (count + i + (j * len)) < aseq1.length)
559 output.append(new Format("%s").form(intToStr[aseq2[count + i +
564 output = output.append("\n\n");
567 pid = pid / (float) (aseq1.length - count) * 100;
568 output = output.append(new Format("Percentage ID = %2.2f\n\n").form(pid));
572 os.print(output.toString());
581 * @param mat DOCUMENT ME!
583 public void printScoreMatrix(int[][] mat)
588 for (int i = 0; i < n; i++)
590 // Print the top sequence
593 Format.print(System.out, "%8s", s2str.substring(0, 1));
595 for (int jj = 1; jj < m; jj++)
597 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
600 System.out.println();
603 for (int j = 0; j < m; j++)
607 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
610 Format.print(System.out, "%3d ", mat[i][j] / 10);
613 System.out.println();
620 * @param i DOCUMENT ME!
621 * @param j DOCUMENT ME!
623 * @return DOCUMENT ME!
625 public int findTrace(int i, int j)
628 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
635 else if (F[i][j] == max)
649 else if (E[i][j] == max)
666 public void calcScoreMatrix()
671 // top left hand element
672 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
673 E[0][0] = -gapExtend;
676 // Calculate the top row first
677 for (int j = 1; j < m; j++)
679 // What should these values be? 0 maybe
680 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
681 F[0][j] = -gapExtend;
683 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen,
689 // Now do the left hand column
690 for (int i = 1; i < n; i++)
693 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
695 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
696 traceback[i][0] = -1;
699 // Now do all the other rows
700 for (int i = 1; i < n; i++)
702 for (int j = 1; j < m; j++)
704 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] -
706 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] -
709 score[i][j] = max(score[i - 1][j - 1] +
710 (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
711 traceback[i][j] = findTrace(i, j);
719 * @param gapChar DOCUMENT ME!
720 * @param seq DOCUMENT ME!
722 * @return DOCUMENT ME!
724 public static String extractGaps(String gapChar, String seq)
726 StringTokenizer str = new StringTokenizer(seq, gapChar);
727 StringBuffer newString = new StringBuffer();
729 while (str.hasMoreTokens())
731 newString.append(str.nextToken());
734 return newString.toString();
740 * @param i1 DOCUMENT ME!
741 * @param i2 DOCUMENT ME!
742 * @param i3 DOCUMENT ME!
744 * @return DOCUMENT ME!
746 public int max(int i1, int i2, int i3)
766 * @param i1 DOCUMENT ME!
767 * @param i2 DOCUMENT ME!
769 * @return DOCUMENT ME!
771 public int max(int i1, int i2)
786 * @param s DOCUMENT ME!
787 * @param type DOCUMENT ME!
789 * @return DOCUMENT ME!
791 public int[] stringToInt(String s, String type)
793 int[] seq1 = new int[s.length()];
795 for (int i = 0; i < s.length(); i++)
797 // String ss = s.substring(i, i + 1).toUpperCase();
798 char c = s.charAt(i);
799 if ('a' <= c && c <= 'z')
807 if (type.equals("pep"))
809 seq1[i] = ResidueProperties.aaIndex[c];
811 else if (type.equals("dna"))
813 seq1[i] = ResidueProperties.nucleotideIndex[c];
823 if (type.equals("dna"))
840 * @param g DOCUMENT ME!
841 * @param mat DOCUMENT ME!
842 * @param n DOCUMENT ME!
843 * @param m DOCUMENT ME!
844 * @param psize DOCUMENT ME!
846 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
852 for (int i = 0; i < n; i++)
854 for (int j = 0; j < m; j++)
856 if (mat[i][j] >= max)
861 if (mat[i][j] <= min)
868 System.out.println(max + " " + min);
870 for (int i = 0; i < n; i++)
872 for (int j = 0; j < m; j++)
877 // System.out.println(mat[i][j]);
878 float score = (float) (mat[i][j] - min) / (float) (max - min);
879 g.setColor(new Color(score, 0, 0));
880 g.fillRect(x, y, psize, psize);
882 // System.out.println(x + " " + y + " " + score);