2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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";
39 public static final String DNA = "dna";
42 { "A", "C", "G", "T", "-" };
44 // "C", "T", "A", "G", "-"};
46 { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
47 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-" };
77 public String astr1 = "";
79 public String astr2 = "";
106 int[][] lookup = ResidueProperties.getBLOSUM62();
108 String[] intToStr = pep;
112 StringBuffer output = new StringBuffer();
117 * Creates a new AlignSeq object.
126 public AlignSeq(SequenceI s1, SequenceI s2, String type)
128 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
133 * Creates a new AlignSeq object.
142 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
143 String string2, String type)
145 SeqInit(s1, string1, s2, string2, type);
151 * @return DOCUMENT ME!
153 public int getMaxScore()
161 * @return DOCUMENT ME!
163 public int getSeq2Start()
171 * @return DOCUMENT ME!
173 public int getSeq2End()
181 * @return DOCUMENT ME!
183 public int getSeq1Start()
191 * @return DOCUMENT ME!
193 public int getSeq1End()
201 * @return DOCUMENT ME!
203 public String getOutput()
205 return output.toString();
211 * @return DOCUMENT ME!
213 public String getAStr1()
221 * @return DOCUMENT ME!
223 public String getAStr2()
231 * @return DOCUMENT ME!
233 public int[] getASeq1()
241 * @return DOCUMENT ME!
243 public int[] getASeq2()
251 * @return DOCUMENT ME!
253 public SequenceI getS1()
261 * @return DOCUMENT ME!
263 public SequenceI getS2()
274 * string to align for sequence1
278 * string to align for sequence2
282 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
283 String string2, String type)
287 setDefaultParams(type);
288 SeqInit(string1, string2);
291 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
292 String string2, ScoreMatrix scoreMatrix)
296 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
297 lookup = scoreMatrix.getMatrix();
301 * construct score matrix for string1 and string2 (after removing any existing
307 private void SeqInit(String string1, String string2)
309 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
310 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
312 if (s1str.length() == 0 || s2str.length() == 0)
314 output.append("ALL GAPS: "
315 + (s1str.length() == 0 ? s1.getName() : " ")
316 + (s2str.length() == 0 ? s2.getName() : ""));
320 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
321 seq1 = new int[s1str.length()];
323 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
324 seq2 = new int[s2str.length()];
326 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
327 score = new int[s1str.length()][s2str.length()];
329 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
330 E = new int[s1str.length()][s2str.length()];
332 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
333 F = new int[s1str.length()][s2str.length()];
334 traceback = new int[s1str.length()][s2str.length()];
336 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
337 seq1 = stringToInt(s1str, type);
339 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
340 seq2 = stringToInt(s2str, type);
342 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
343 // long tstart = System.currentTimeMillis();
344 // calcScoreMatrix();
345 // long tend = System.currentTimeMillis();
346 // System.out.println("Time take to calculate score matrix = " +
347 // (tend-tstart) + " ms");
348 // printScoreMatrix(score);
349 // System.out.println();
350 // printScoreMatrix(traceback);
351 // System.out.println();
352 // printScoreMatrix(E);
353 // System.out.println();
354 // /printScoreMatrix(F);
355 // System.out.println();
356 // tstart = System.currentTimeMillis();
358 // tend = System.currentTimeMillis();
359 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
363 private void setDefaultParams(String type)
367 if (type.equals(AlignSeq.PEP))
369 lookup = ResidueProperties.getDefaultPeptideMatrix();
371 else if (type.equals(AlignSeq.DNA))
373 lookup = ResidueProperties.getDefaultDnaMatrix();
377 private void setType(String type2)
380 if (type.equals(AlignSeq.PEP))
385 else if (type.equals(AlignSeq.DNA))
392 output.append("Wrong type = dna or pep only");
393 throw new Error("Unknown Type " + type2
394 + " - dna or pep are the only allowed values.");
401 public void traceAlignment()
403 // Find the maximum score along the rhs or bottom row
406 for (int i = 0; i < seq1.length; i++)
408 if (score[i][seq2.length - 1] > max)
410 max = score[i][seq2.length - 1];
412 maxj = seq2.length - 1;
416 for (int j = 0; j < seq2.length; j++)
418 if (score[seq1.length - 1][j] > max)
420 max = score[seq1.length - 1][j];
421 maxi = seq1.length - 1;
426 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
430 maxscore = score[i][j] / 10;
435 aseq1 = new int[seq1.length + seq2.length];
436 aseq2 = new int[seq1.length + seq2.length];
438 count = (seq1.length + seq2.length) - 1;
440 while ((i > 0) && (j > 0))
442 if ((aseq1[count] != defInt) && (i >= 0))
444 aseq1[count] = seq1[i];
445 astr1 = s1str.charAt(i) + astr1;
448 if ((aseq2[count] != defInt) && (j > 0))
450 aseq2[count] = seq2[j];
451 astr2 = s2str.charAt(j) + astr2;
454 trace = findTrace(i, j);
464 aseq1[count] = defInt;
465 astr1 = "-" + astr1.substring(1);
467 else if (trace == -1)
470 aseq2[count] = defInt;
471 astr2 = "-" + astr2.substring(1);
480 if (aseq1[count] != defInt)
482 aseq1[count] = seq1[i];
483 astr1 = s1str.charAt(i) + astr1;
486 if (aseq2[count] != defInt)
488 aseq2[count] = seq2[j];
489 astr2 = s2str.charAt(j) + astr2;
496 public void printAlignment(java.io.PrintStream os)
498 // TODO: Use original sequence characters rather than re-translated
499 // characters in output
500 // Find the biggest id length for formatting purposes
501 int maxid = s1.getName().length();
502 if (s2.getName().length() > maxid)
504 maxid = s2.getName().length();
507 int len = 72 - maxid - 1;
508 int nochunks = ((aseq1.length - count) / len) + 1;
511 output.append("Score = " + score[maxi][maxj] + "\n");
512 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
513 output.append("Sequence ");
514 output.append(new Format("%" + maxid + "s").form(s1.getName()));
515 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
516 + " (Sequence length = " + s1str.length() + ")\n");
517 output.append("Sequence ");
518 output.append(new Format("%" + maxid + "s").form(s2.getName()));
519 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
520 + " (Sequence length = " + s2str.length() + ")\n\n");
522 for (int j = 0; j < nochunks; j++)
524 // Print the first aligned sequence
525 output.append(new Format("%" + (maxid) + "s").form(s1.getName())
528 for (int i = 0; i < len; i++)
530 if ((i + (j * len)) < astr1.length())
532 output.append(astr1.charAt(i + (j * len)));
537 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
539 // Print out the matching chars
540 for (int i = 0; i < len; i++)
542 if ((i + (j * len)) < astr1.length())
544 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
545 && !jalview.util.Comparison.isGap(astr1.charAt(i
551 else if (type.equals("pep"))
553 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
554 astr2.charAt(i + (j * len))) > 0)
570 // Now print the second aligned sequence
571 output = output.append("\n");
572 output = output.append(new Format("%" + (maxid) + "s").form(s2
576 for (int i = 0; i < len; i++)
578 if ((i + (j * len)) < astr2.length())
580 output.append(astr2.charAt(i + (j * len)));
584 output = output.append("\n\n");
587 pid = pid / (float) (aseq1.length - count) * 100;
588 output = output.append(new Format("Percentage ID = %2.2f\n\n")
593 os.print(output.toString());
594 } catch (Exception ex)
605 public void printScoreMatrix(int[][] mat)
610 for (int i = 0; i < n; i++)
612 // Print the top sequence
615 Format.print(System.out, "%8s", s2str.substring(0, 1));
617 for (int jj = 1; jj < m; jj++)
619 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
622 System.out.println();
625 for (int j = 0; j < m; j++)
629 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
632 Format.print(System.out, "%3d ", mat[i][j] / 10);
635 System.out.println();
647 * @return DOCUMENT ME!
649 public int findTrace(int i, int j)
652 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
659 else if (F[i][j] == max)
673 else if (E[i][j] == max)
690 public void calcScoreMatrix()
695 // top left hand element
696 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
697 E[0][0] = -gapExtend;
700 // Calculate the top row first
701 for (int j = 1; j < m; j++)
703 // What should these values be? 0 maybe
704 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
705 F[0][j] = -gapExtend;
707 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
712 // Now do the left hand column
713 for (int i = 1; i < n; i++)
716 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
718 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
719 traceback[i][0] = -1;
722 // Now do all the other rows
723 for (int i = 1; i < n; i++)
725 for (int j = 1; j < m; j++)
727 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
728 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
730 score[i][j] = max(score[i - 1][j - 1]
731 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
732 traceback[i][j] = findTrace(i, j);
745 * @return DOCUMENT ME!
747 public static String extractGaps(String gapChar, String seq)
749 StringTokenizer str = new StringTokenizer(seq, gapChar);
750 StringBuffer newString = new StringBuffer();
752 while (str.hasMoreTokens())
754 newString.append(str.nextToken());
757 return newString.toString();
770 * @return DOCUMENT ME!
772 public int max(int i1, int i2, int i3)
797 * @return DOCUMENT ME!
799 public int max(int i1, int i2)
819 * @return DOCUMENT ME!
821 public int[] stringToInt(String s, String type)
823 int[] seq1 = new int[s.length()];
825 for (int i = 0; i < s.length(); i++)
827 // String ss = s.substring(i, i + 1).toUpperCase();
828 char c = s.charAt(i);
829 if ('a' <= c && c <= 'z')
837 if (type.equals("pep"))
839 seq1[i] = ResidueProperties.aaIndex[c];
845 else if (type.equals("dna"))
847 seq1[i] = ResidueProperties.nucleotideIndex[c];
854 } catch (Exception e)
856 if (type.equals("dna"))
884 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
890 for (int i = 0; i < n; i++)
892 for (int j = 0; j < m; j++)
894 if (mat[i][j] >= max)
899 if (mat[i][j] <= min)
906 System.out.println(max + " " + min);
908 for (int i = 0; i < n; i++)
910 for (int j = 0; j < m; j++)
915 // System.out.println(mat[i][j]);
916 float score = (float) (mat[i][j] - min) / (float) (max - min);
917 g.setColor(new Color(score, 0, 0));
918 g.fillRect(x, y, psize, psize);
920 // System.out.println(x + " " + y + " " + score);