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";
42 "A", "C", "G", "T", "-"};
43 //"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", "-"
63 public String astr1 = "";
64 public String astr2 = "";
85 int[][] lookup = ResidueProperties.getBLOSUM62();
86 String[] intToStr = pep;
88 StringBuffer output = new StringBuffer();
92 * Creates a new AlignSeq object.
94 * @param s1 DOCUMENT ME!
95 * @param s2 DOCUMENT ME!
96 * @param type DOCUMENT ME!
98 public AlignSeq(SequenceI s1, SequenceI s2, String type)
100 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(), type);
104 * Creates a new AlignSeq object.
106 * @param s1 DOCUMENT ME!
107 * @param s2 DOCUMENT ME!
108 * @param type DOCUMENT ME!
110 public AlignSeq(SequenceI s1,
116 SeqInit(s1, string1, s2, string2, type);
122 * @return DOCUMENT ME!
124 public int getMaxScore()
132 * @return DOCUMENT ME!
134 public int getSeq2Start()
142 * @return DOCUMENT ME!
144 public int getSeq2End()
152 * @return DOCUMENT ME!
154 public int getSeq1Start()
162 * @return DOCUMENT ME!
164 public int getSeq1End()
172 * @return DOCUMENT ME!
174 public String getOutput()
176 return output.toString();
182 * @return DOCUMENT ME!
184 public String getAStr1()
192 * @return DOCUMENT ME!
194 public String getAStr2()
202 * @return DOCUMENT ME!
204 public int[] getASeq1()
212 * @return DOCUMENT ME!
214 public int[] getASeq2()
222 * @return DOCUMENT ME!
224 public SequenceI getS1()
232 * @return DOCUMENT ME!
234 public SequenceI getS2()
242 * @param s1 DOCUMENT ME!
243 * @param string1 - string to align for sequence1
244 * @param s2 sequence 2
245 * @param string2 - string to align for sequence2
246 * @param type DNA or PEPTIDE
248 public void SeqInit(SequenceI s1,
256 setDefaultParams(type);
257 SeqInit(string1, string2);
260 public void SeqInit(SequenceI s1,
264 ScoreMatrix scoreMatrix)
268 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
269 lookup = scoreMatrix.getMatrix();
273 * construct score matrix for string1 and string2 (after removing any existing gaps
277 private void SeqInit(String string1, String string2)
279 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
280 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
282 if (s1str.length() == 0 || s2str.length() == 0)
284 output.append("ALL GAPS: " +
285 (s1str.length() == 0 ? s1.getName() : " ")
286 + (s2str.length() == 0 ? s2.getName() : ""));
290 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
291 seq1 = new int[s1str.length()];
293 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
294 seq2 = new int[s2str.length()];
296 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
297 score = new int[s1str.length()][s2str.length()];
299 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
300 E = new int[s1str.length()][s2str.length()];
302 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
303 F = new int[s1str.length()][s2str.length()];
304 traceback = new int[s1str.length()][s2str.length()];
306 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
307 seq1 = stringToInt(s1str, type);
309 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
310 seq2 = stringToInt(s2str, type);
312 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
313 // long tstart = System.currentTimeMillis();
314 // calcScoreMatrix();
315 //long tend = System.currentTimeMillis();
316 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
317 // printScoreMatrix(score);
318 //System.out.println();
319 //printScoreMatrix(traceback);
320 //System.out.println();
321 // printScoreMatrix(E);
322 //System.out.println();
323 ///printScoreMatrix(F);
324 //System.out.println();
325 // tstart = System.currentTimeMillis();
327 //tend = System.currentTimeMillis();
328 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
331 private void setDefaultParams(String type)
335 if (type.equals(AlignSeq.PEP))
337 lookup = ResidueProperties.getDefaultPeptideMatrix();
339 else if (type.equals(AlignSeq.DNA))
341 lookup = ResidueProperties.getDefaultDnaMatrix();
345 private void setType(String type2)
348 if (type.equals(AlignSeq.PEP))
353 else if (type.equals(AlignSeq.DNA))
360 output.append("Wrong type = dna or pep only");
361 throw new Error("Unknown Type " + type2 +
362 " - dna or pep are the only allowed values.");
369 public void traceAlignment()
371 // Find the maximum score along the rhs or bottom row
374 for (int i = 0; i < seq1.length; i++)
376 if (score[i][seq2.length - 1] > max)
378 max = score[i][seq2.length - 1];
380 maxj = seq2.length - 1;
384 for (int j = 0; j < seq2.length; j++)
386 if (score[seq1.length - 1][j] > max)
388 max = score[seq1.length - 1][j];
389 maxi = seq1.length - 1;
394 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
398 maxscore = score[i][j] / 10;
403 aseq1 = new int[seq1.length + seq2.length];
404 aseq2 = new int[seq1.length + seq2.length];
406 count = (seq1.length + seq2.length) - 1;
408 while ( (i > 0) && (j > 0))
410 if ( (aseq1[count] != defInt) && (i >= 0))
412 aseq1[count] = seq1[i];
413 astr1 = s1str.charAt(i) + astr1;
416 if ( (aseq2[count] != defInt) && (j > 0))
418 aseq2[count] = seq2[j];
419 astr2 = s2str.charAt(j) + astr2;
422 trace = findTrace(i, j);
432 aseq1[count] = defInt;
433 astr1 = "-" + astr1.substring(1);
435 else if (trace == -1)
438 aseq2[count] = defInt;
439 astr2 = "-" + astr2.substring(1);
448 if (aseq1[count] != defInt)
450 aseq1[count] = seq1[i];
451 astr1 = s1str.charAt(i) + astr1;
454 if (aseq2[count] != defInt)
456 aseq2[count] = seq2[j];
457 astr2 = s2str.charAt(j) + astr2;
464 public void printAlignment(java.io.PrintStream os)
466 // TODO: Use original sequence characters rather than re-translated characters in output
467 // Find the biggest id length for formatting purposes
468 int maxid = s1.getName().length();
469 if (s2.getName().length() > maxid)
471 maxid = s2.getName().length();
474 int len = 72 - maxid - 1;
475 int nochunks = ( (aseq1.length - count) / len) + 1;
478 output.append("Score = " + score[maxi][maxj] + "\n");
479 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
480 output.append("Sequence ");
481 output.append(new Format("%" + maxid + "s").form(s1.getName()));
482 output.append(" : " + s1.getStart() + " - " + s1.getEnd() +
483 " (Sequence length = " +
484 s1str.length() + ")\n");
485 output.append("Sequence ");
486 output.append(new Format("%" + maxid + "s").form(s2.getName()));
487 output.append(" : " + s2.getStart() + " - " + s2.getEnd() +
488 " (Sequence length = " +
489 s2str.length() + ")\n\n");
491 for (int j = 0; j < nochunks; j++)
493 // Print the first aligned sequence
494 output.append(new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
496 for (int i = 0; i < len; i++)
498 if ( (i + (j * len)) < astr1.length())
500 output.append(astr1.charAt(i +
506 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
508 // Print out the matching chars
509 for (int i = 0; i < len; i++)
511 if ( (i + (j * len)) < astr1.length())
513 if (astr1.charAt(i + (j * len))==astr2.charAt(i + (j * len)) &&
514 !jalview.util.Comparison.isGap(astr1.charAt(i + (j * len))))
519 else if (type.equals("pep"))
521 if (ResidueProperties.getPAM250(
522 astr1.charAt(i + (j * len)),
523 astr2.charAt(i + (j * len)))>0)
539 // Now print the second aligned sequence
540 output = output.append("\n");
541 output = output.append(new Format("%" + (maxid) + "s").form(s2.getName()) +
544 for (int i = 0; i < len; i++)
546 if ( (i + (j * len)) < astr2.length())
548 output.append(astr2.charAt(i + (j * len)));
552 output = output.append("\n\n");
555 pid = pid / (float) (aseq1.length - count) * 100;
556 output = output.append(new Format("Percentage ID = %2.2f\n\n").form(pid));
560 os.print(output.toString());
569 * @param mat DOCUMENT ME!
571 public void printScoreMatrix(int[][] mat)
576 for (int i = 0; i < n; i++)
578 // Print the top sequence
581 Format.print(System.out, "%8s", s2str.substring(0, 1));
583 for (int jj = 1; jj < m; jj++)
585 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
588 System.out.println();
591 for (int j = 0; j < m; j++)
595 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
598 Format.print(System.out, "%3d ", mat[i][j] / 10);
601 System.out.println();
608 * @param i DOCUMENT ME!
609 * @param j DOCUMENT ME!
611 * @return DOCUMENT ME!
613 public int findTrace(int i, int j)
616 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
623 else if (F[i][j] == max)
637 else if (E[i][j] == max)
654 public void calcScoreMatrix()
659 // top left hand element
660 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
661 E[0][0] = -gapExtend;
664 // Calculate the top row first
665 for (int j = 1; j < m; j++)
667 // What should these values be? 0 maybe
668 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
669 F[0][j] = -gapExtend;
671 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen,
677 // Now do the left hand column
678 for (int i = 1; i < n; i++)
681 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
683 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
684 traceback[i][0] = -1;
687 // Now do all the other rows
688 for (int i = 1; i < n; i++)
690 for (int j = 1; j < m; j++)
692 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] -
694 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] -
697 score[i][j] = max(score[i - 1][j - 1] +
698 (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
699 traceback[i][j] = findTrace(i, j);
707 * @param gapChar DOCUMENT ME!
708 * @param seq DOCUMENT ME!
710 * @return DOCUMENT ME!
712 public static String extractGaps(String gapChar, String seq)
714 StringTokenizer str = new StringTokenizer(seq, gapChar);
715 StringBuffer newString = new StringBuffer();
717 while (str.hasMoreTokens())
719 newString.append(str.nextToken());
722 return newString.toString();
728 * @param i1 DOCUMENT ME!
729 * @param i2 DOCUMENT ME!
730 * @param i3 DOCUMENT ME!
732 * @return DOCUMENT ME!
734 public int max(int i1, int i2, int i3)
754 * @param i1 DOCUMENT ME!
755 * @param i2 DOCUMENT ME!
757 * @return DOCUMENT ME!
759 public int max(int i1, int i2)
774 * @param s DOCUMENT ME!
775 * @param type DOCUMENT ME!
777 * @return DOCUMENT ME!
779 public int[] stringToInt(String s, String type)
781 int[] seq1 = new int[s.length()];
783 for (int i = 0; i < s.length(); i++)
785 // String ss = s.substring(i, i + 1).toUpperCase();
786 char c = s.charAt(i);
787 if ('a' <= c && c <= 'z')
795 if (type.equals("pep"))
797 seq1[i] = ResidueProperties.aaIndex[c];
803 else if (type.equals("dna"))
805 seq1[i] = ResidueProperties.nucleotideIndex[c];
815 if (type.equals("dna"))
832 * @param g DOCUMENT ME!
833 * @param mat DOCUMENT ME!
834 * @param n DOCUMENT ME!
835 * @param m DOCUMENT ME!
836 * @param psize DOCUMENT ME!
838 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
844 for (int i = 0; i < n; i++)
846 for (int j = 0; j < m; j++)
848 if (mat[i][j] >= max)
853 if (mat[i][j] <= min)
860 System.out.println(max + " " + min);
862 for (int i = 0; i < n; i++)
864 for (int j = 0; j < m; j++)
869 // System.out.println(mat[i][j]);
870 float score = (float) (mat[i][j] - min) / (float) (max - min);
871 g.setColor(new Color(score, 0, 0));
872 g.fillRect(x, y, psize, psize);
874 // System.out.println(x + " " + y + " " + score);