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();
116 private int[] charToInt;
119 * Creates a new AlignSeq object.
128 public AlignSeq(SequenceI s1, SequenceI s2, String type)
130 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
135 * Creates a new AlignSeq object.
144 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
145 String string2, String type)
147 SeqInit(s1, string1, s2, string2, type);
153 * @return DOCUMENT ME!
155 public int getMaxScore()
163 * @return DOCUMENT ME!
165 public int getSeq2Start()
173 * @return DOCUMENT ME!
175 public int getSeq2End()
183 * @return DOCUMENT ME!
185 public int getSeq1Start()
193 * @return DOCUMENT ME!
195 public int getSeq1End()
203 * @return DOCUMENT ME!
205 public String getOutput()
207 return output.toString();
213 * @return DOCUMENT ME!
215 public String getAStr1()
223 * @return DOCUMENT ME!
225 public String getAStr2()
233 * @return DOCUMENT ME!
235 public int[] getASeq1()
243 * @return DOCUMENT ME!
245 public int[] getASeq2()
253 * @return DOCUMENT ME!
255 public SequenceI getS1()
263 * @return DOCUMENT ME!
265 public SequenceI getS2()
276 * string to align for sequence1
280 * string to align for sequence2
284 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
285 String string2, String type)
289 setDefaultParams(type);
290 SeqInit(string1, string2);
293 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
294 String string2, ScoreMatrix scoreMatrix)
298 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
299 lookup = scoreMatrix.getMatrix();
303 * construct score matrix for string1 and string2 (after removing any existing
309 private void SeqInit(String string1, String string2)
311 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
312 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
314 if (s1str.length() == 0 || s2str.length() == 0)
316 output.append("ALL GAPS: "
317 + (s1str.length() == 0 ? s1.getName() : " ")
318 + (s2str.length() == 0 ? s2.getName() : ""));
322 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
323 seq1 = new int[s1str.length()];
325 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
326 seq2 = new int[s2str.length()];
328 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
329 score = new int[s1str.length()][s2str.length()];
331 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
332 E = new int[s1str.length()][s2str.length()];
334 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
335 F = new int[s1str.length()][s2str.length()];
336 traceback = new int[s1str.length()][s2str.length()];
338 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
339 seq1 = stringToInt(s1str, type);
341 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
342 seq2 = stringToInt(s2str, type);
344 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
345 // long tstart = System.currentTimeMillis();
346 // calcScoreMatrix();
347 // long tend = System.currentTimeMillis();
348 // System.out.println("Time take to calculate score matrix = " +
349 // (tend-tstart) + " ms");
350 // printScoreMatrix(score);
351 // System.out.println();
352 // printScoreMatrix(traceback);
353 // System.out.println();
354 // printScoreMatrix(E);
355 // System.out.println();
356 // /printScoreMatrix(F);
357 // System.out.println();
358 // tstart = System.currentTimeMillis();
360 // tend = System.currentTimeMillis();
361 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
365 private void setDefaultParams(String type)
369 if (type.equals(AlignSeq.PEP))
371 lookup = ResidueProperties.getDefaultPeptideMatrix();
373 else if (type.equals(AlignSeq.DNA))
375 lookup = ResidueProperties.getDefaultDnaMatrix();
379 private void setType(String type2)
382 if (type.equals(AlignSeq.PEP))
385 charToInt = ResidueProperties.aaIndex;
388 else if (type.equals(AlignSeq.DNA))
391 charToInt = ResidueProperties.nucleotideIndex;
396 output.append("Wrong type = dna or pep only");
397 throw new Error("Unknown Type " + type2
398 + " - dna or pep are the only allowed values.");
405 public void traceAlignment()
407 // Find the maximum score along the rhs or bottom row
410 for (int i = 0; i < seq1.length; i++)
412 if (score[i][seq2.length - 1] > max)
414 max = score[i][seq2.length - 1];
416 maxj = seq2.length - 1;
420 for (int j = 0; j < seq2.length; j++)
422 if (score[seq1.length - 1][j] > max)
424 max = score[seq1.length - 1][j];
425 maxi = seq1.length - 1;
430 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
434 maxscore = score[i][j] / 10;
439 aseq1 = new int[seq1.length + seq2.length];
440 aseq2 = new int[seq1.length + seq2.length];
442 count = (seq1.length + seq2.length) - 1;
444 while ((i > 0) && (j > 0))
446 if ((aseq1[count] != defInt) && (i >= 0))
448 aseq1[count] = seq1[i];
449 astr1 = s1str.charAt(i) + astr1;
452 if ((aseq2[count] != defInt) && (j > 0))
454 aseq2[count] = seq2[j];
455 astr2 = s2str.charAt(j) + astr2;
458 trace = findTrace(i, j);
468 aseq1[count] = defInt;
469 astr1 = "-" + astr1.substring(1);
471 else if (trace == -1)
474 aseq2[count] = defInt;
475 astr2 = "-" + astr2.substring(1);
484 if (aseq1[count] != defInt)
486 aseq1[count] = seq1[i];
487 astr1 = s1str.charAt(i) + astr1;
490 if (aseq2[count] != defInt)
492 aseq2[count] = seq2[j];
493 astr2 = s2str.charAt(j) + astr2;
500 public void printAlignment(java.io.PrintStream os)
502 // TODO: Use original sequence characters rather than re-translated
503 // characters in output
504 // Find the biggest id length for formatting purposes
505 int maxid = s1.getName().length();
506 if (s2.getName().length() > maxid)
508 maxid = s2.getName().length();
511 int len = 72 - maxid - 1;
512 int nochunks = ((aseq1.length - count) / len) + 1;
515 output.append("Score = " + score[maxi][maxj] + "\n");
516 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
517 output.append("Sequence ");
518 output.append(new Format("%" + maxid + "s").form(s1.getName()));
519 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
520 + " (Sequence length = " + s1str.length() + ")\n");
521 output.append("Sequence ");
522 output.append(new Format("%" + maxid + "s").form(s2.getName()));
523 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
524 + " (Sequence length = " + s2str.length() + ")\n\n");
526 for (int j = 0; j < nochunks; j++)
528 // Print the first aligned sequence
529 output.append(new Format("%" + (maxid) + "s").form(s1.getName())
532 for (int i = 0; i < len; i++)
534 if ((i + (j * len)) < astr1.length())
536 output.append(astr1.charAt(i + (j * len)));
541 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
543 // Print out the matching chars
544 for (int i = 0; i < len; i++)
546 if ((i + (j * len)) < astr1.length())
548 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
549 && !jalview.util.Comparison.isGap(astr1.charAt(i
555 else if (type.equals("pep"))
557 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
558 astr2.charAt(i + (j * len))) > 0)
574 // Now print the second aligned sequence
575 output = output.append("\n");
576 output = output.append(new Format("%" + (maxid) + "s").form(s2
580 for (int i = 0; i < len; i++)
582 if ((i + (j * len)) < astr2.length())
584 output.append(astr2.charAt(i + (j * len)));
588 output = output.append("\n\n");
591 pid = pid / (float) (aseq1.length - count) * 100;
592 output = output.append(new Format("Percentage ID = %2.2f\n\n")
597 os.print(output.toString());
598 } catch (Exception ex)
609 public void printScoreMatrix(int[][] mat)
614 for (int i = 0; i < n; i++)
616 // Print the top sequence
619 Format.print(System.out, "%8s", s2str.substring(0, 1));
621 for (int jj = 1; jj < m; jj++)
623 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
626 System.out.println();
629 for (int j = 0; j < m; j++)
633 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
636 Format.print(System.out, "%3d ", mat[i][j] / 10);
639 System.out.println();
651 * @return DOCUMENT ME!
653 public int findTrace(int i, int j)
656 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
663 else if (F[i][j] == max)
677 else if (E[i][j] == max)
694 public void calcScoreMatrix()
699 // top left hand element
700 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
701 E[0][0] = -gapExtend;
704 // Calculate the top row first
705 for (int j = 1; j < m; j++)
707 // What should these values be? 0 maybe
708 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
709 F[0][j] = -gapExtend;
711 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
716 // Now do the left hand column
717 for (int i = 1; i < n; i++)
720 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
722 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
723 traceback[i][0] = -1;
726 // Now do all the other rows
727 for (int i = 1; i < n; i++)
729 for (int j = 1; j < m; j++)
731 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
732 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
734 score[i][j] = max(score[i - 1][j - 1]
735 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
736 traceback[i][j] = findTrace(i, j);
749 * @return DOCUMENT ME!
751 public static String extractGaps(String gapChar, String seq)
753 StringTokenizer str = new StringTokenizer(seq, gapChar);
754 StringBuffer newString = new StringBuffer();
756 while (str.hasMoreTokens())
758 newString.append(str.nextToken());
761 return newString.toString();
774 * @return DOCUMENT ME!
776 public int max(int i1, int i2, int i3)
801 * @return DOCUMENT ME!
803 public int max(int i1, int i2)
823 * @return DOCUMENT ME!
825 public int[] stringToInt(String s, String type)
827 int[] seq1 = new int[s.length()];
829 for (int i = 0; i < s.length(); i++)
831 // String ss = s.substring(i, i + 1).toUpperCase();
832 char c = s.charAt(i);
833 if ('a' <= c && c <= 'z')
841 seq1[i] = charToInt[c]; // set accordingly from setType
842 if (seq1[i]<0 || seq1[i] > defInt) // set from setType: 23 for peptides, or 4 for NA.
847 } catch (Exception e)
870 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
876 for (int i = 0; i < n; i++)
878 for (int j = 0; j < m; j++)
880 if (mat[i][j] >= max)
885 if (mat[i][j] <= min)
892 System.out.println(max + " " + min);
894 for (int i = 0; i < n; i++)
896 for (int j = 0; j < m; j++)
901 // System.out.println(mat[i][j]);
902 float score = (float) (mat[i][j] - min) / (float) (max - min);
903 g.setColor(new Color(score, 0, 0));
904 g.fillRect(x, y, psize, psize);
906 // System.out.println(x + " " + y + " " + score);