2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
24 import jalview.datamodel.*;
25 import jalview.schemes.*;
26 import jalview.util.*;
36 public static final String PEP = "pep";
38 public static final String DNA = "dna";
41 { "A", "C", "G", "T", "-" };
43 // "C", "T", "A", "G", "-"};
45 { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
46 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-" };
76 public String astr1 = "";
78 public String astr2 = "";
105 int[][] lookup = ResidueProperties.getBLOSUM62();
107 String[] intToStr = pep;
111 StringBuffer output = new StringBuffer();
115 private int[] charToInt;
118 * Creates a new AlignSeq object.
127 public AlignSeq(SequenceI s1, SequenceI s2, String type)
129 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
134 * Creates a new AlignSeq object.
143 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
144 String string2, String type)
146 SeqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
152 * @return DOCUMENT ME!
154 public int getMaxScore()
162 * @return DOCUMENT ME!
164 public int getSeq2Start()
172 * @return DOCUMENT ME!
174 public int getSeq2End()
182 * @return DOCUMENT ME!
184 public int getSeq1Start()
192 * @return DOCUMENT ME!
194 public int getSeq1End()
202 * @return DOCUMENT ME!
204 public String getOutput()
206 return output.toString();
212 * @return DOCUMENT ME!
214 public String getAStr1()
222 * @return DOCUMENT ME!
224 public String getAStr2()
232 * @return DOCUMENT ME!
234 public int[] getASeq1()
242 * @return DOCUMENT ME!
244 public int[] getASeq2()
252 * @return DOCUMENT ME!
254 public SequenceI getS1()
262 * @return DOCUMENT ME!
264 public SequenceI getS2()
275 * - string to align for sequence1
279 * - string to align for sequence2
283 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
284 String string2, String type)
288 setDefaultParams(type);
289 SeqInit(string1, string2);
292 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
293 String string2, ScoreMatrix scoreMatrix)
297 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
298 lookup = scoreMatrix.getMatrix();
302 * construct score matrix for string1 and string2 (after removing any existing
308 private void SeqInit(String string1, String string2)
310 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
311 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
313 if (s1str.length() == 0 || s2str.length() == 0)
315 output.append("ALL GAPS: "
316 + (s1str.length() == 0 ? s1.getName() : " ")
317 + (s2str.length() == 0 ? s2.getName() : ""));
321 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
322 seq1 = new int[s1str.length()];
324 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
325 seq2 = new int[s2str.length()];
327 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
328 score = new int[s1str.length()][s2str.length()];
330 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
331 E = new int[s1str.length()][s2str.length()];
333 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
334 F = new int[s1str.length()][s2str.length()];
335 traceback = new int[s1str.length()][s2str.length()];
337 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
338 seq1 = stringToInt(s1str, type);
340 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
341 seq2 = stringToInt(s2str, type);
343 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
344 // long tstart = System.currentTimeMillis();
345 // calcScoreMatrix();
346 // long tend = System.currentTimeMillis();
347 // System.out.println("Time take to calculate score matrix = " +
348 // (tend-tstart) + " ms");
349 // printScoreMatrix(score);
350 // System.out.println();
351 // printScoreMatrix(traceback);
352 // System.out.println();
353 // printScoreMatrix(E);
354 // System.out.println();
355 // /printScoreMatrix(F);
356 // System.out.println();
357 // tstart = System.currentTimeMillis();
359 // tend = System.currentTimeMillis();
360 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
364 private void setDefaultParams(String type)
368 if (type.equals(AlignSeq.PEP))
370 lookup = ResidueProperties.getDefaultPeptideMatrix();
372 else if (type.equals(AlignSeq.DNA))
374 lookup = ResidueProperties.getDefaultDnaMatrix();
378 private void setType(String type2)
381 if (type.equals(AlignSeq.PEP))
384 charToInt = ResidueProperties.aaIndex;
387 else if (type.equals(AlignSeq.DNA))
390 charToInt = ResidueProperties.nucleotideIndex;
395 output.append("Wrong type = dna or pep only");
396 throw new Error("Unknown Type " + type2
397 + " - dna or pep are the only allowed values.");
404 public void traceAlignment()
406 // Find the maximum score along the rhs or bottom row
409 for (int i = 0; i < seq1.length; i++)
411 if (score[i][seq2.length - 1] > max)
413 max = score[i][seq2.length - 1];
415 maxj = seq2.length - 1;
419 for (int j = 0; j < seq2.length; j++)
421 if (score[seq1.length - 1][j] > max)
423 max = score[seq1.length - 1][j];
424 maxi = seq1.length - 1;
429 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
433 maxscore = score[i][j] / 10;
438 aseq1 = new int[seq1.length + seq2.length];
439 aseq2 = new int[seq1.length + seq2.length];
441 count = (seq1.length + seq2.length) - 1;
443 while ((i > 0) && (j > 0))
445 if ((aseq1[count] != defInt) && (i >= 0))
447 aseq1[count] = seq1[i];
448 astr1 = s1str.charAt(i) + astr1;
451 if ((aseq2[count] != defInt) && (j > 0))
453 aseq2[count] = seq2[j];
454 astr2 = s2str.charAt(j) + astr2;
457 trace = findTrace(i, j);
467 aseq1[count] = defInt;
468 astr1 = "-" + astr1.substring(1);
470 else if (trace == -1)
473 aseq2[count] = defInt;
474 astr2 = "-" + astr2.substring(1);
483 if (aseq1[count] != defInt)
485 aseq1[count] = seq1[i];
486 astr1 = s1str.charAt(i) + astr1;
489 if (aseq2[count] != defInt)
491 aseq2[count] = seq2[j];
492 astr2 = s2str.charAt(j) + astr2;
499 public void printAlignment(java.io.PrintStream os)
501 // TODO: Use original sequence characters rather than re-translated
502 // characters in output
503 // Find the biggest id length for formatting purposes
504 String s1id = s1.getName(), s2id = s2.getName();
505 int maxid = s1.getName().length();
506 if (s2.getName().length() > maxid)
508 maxid = s2.getName().length();
513 // JAL-527 - truncate the sequence ids
514 if (s1.getName().length() > maxid)
516 s1id = s1.getName().substring(0, 30);
518 if (s2.getName().length() > maxid)
520 s2id = s2.getName().substring(0, 30);
523 int len = 72 - maxid - 1;
524 int nochunks = ((aseq1.length - count) / len) + 1;
527 output.append("Score = " + score[maxi][maxj] + "\n");
528 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
529 output.append("Sequence ");
530 output.append(new Format("%" + maxid + "s").form(s1.getName()));
531 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
532 + " (Sequence length = " + s1str.length() + ")\n");
533 output.append("Sequence ");
534 output.append(new Format("%" + maxid + "s").form(s2.getName()));
535 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
536 + " (Sequence length = " + s2str.length() + ")\n\n");
538 for (int j = 0; j < nochunks; j++)
540 // Print the first aligned sequence
541 output.append(new Format("%" + (maxid) + "s").form(s1id) + " ");
543 for (int i = 0; i < len; i++)
545 if ((i + (j * len)) < astr1.length())
547 output.append(astr1.charAt(i + (j * len)));
552 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
554 // Print out the matching chars
555 for (int i = 0; i < len; i++)
557 if ((i + (j * len)) < astr1.length())
559 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
560 && !jalview.util.Comparison.isGap(astr1.charAt(i
566 else if (type.equals("pep"))
568 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
569 astr2.charAt(i + (j * len))) > 0)
585 // Now print the second aligned sequence
586 output = output.append("\n");
587 output = output.append(new Format("%" + (maxid) + "s").form(s2id)
590 for (int i = 0; i < len; i++)
592 if ((i + (j * len)) < astr2.length())
594 output.append(astr2.charAt(i + (j * len)));
598 output = output.append("\n\n");
601 pid = pid / (float) (aseq1.length - count) * 100;
602 output = output.append(new Format("Percentage ID = %2.2f\n\n")
607 os.print(output.toString());
608 } catch (Exception ex)
619 public void printScoreMatrix(int[][] mat)
624 for (int i = 0; i < n; i++)
626 // Print the top sequence
629 Format.print(System.out, "%8s", s2str.substring(0, 1));
631 for (int jj = 1; jj < m; jj++)
633 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
636 System.out.println();
639 for (int j = 0; j < m; j++)
643 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
646 Format.print(System.out, "%3d ", mat[i][j] / 10);
649 System.out.println();
661 * @return DOCUMENT ME!
663 public int findTrace(int i, int j)
666 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
673 else if (F[i][j] == max)
687 else if (E[i][j] == max)
704 public void calcScoreMatrix()
709 // top left hand element
710 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
711 E[0][0] = -gapExtend;
714 // Calculate the top row first
715 for (int j = 1; j < m; j++)
717 // What should these values be? 0 maybe
718 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
719 F[0][j] = -gapExtend;
721 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
726 // Now do the left hand column
727 for (int i = 1; i < n; i++)
730 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
732 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
733 traceback[i][0] = -1;
736 // Now do all the other rows
737 for (int i = 1; i < n; i++)
739 for (int j = 1; j < m; j++)
741 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
742 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
744 score[i][j] = max(score[i - 1][j - 1]
745 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
746 traceback[i][j] = findTrace(i, j);
759 * @return DOCUMENT ME!
761 public static String extractGaps(String gapChar, String seq)
763 StringTokenizer str = new StringTokenizer(seq, gapChar);
764 StringBuffer newString = new StringBuffer();
766 while (str.hasMoreTokens())
768 newString.append(str.nextToken());
771 return newString.toString();
784 * @return DOCUMENT ME!
786 public int max(int i1, int i2, int i3)
811 * @return DOCUMENT ME!
813 public int max(int i1, int i2)
833 * @return DOCUMENT ME!
835 public int[] stringToInt(String s, String type)
837 int[] seq1 = new int[s.length()];
839 for (int i = 0; i < s.length(); i++)
841 // String ss = s.substring(i, i + 1).toUpperCase();
842 char c = s.charAt(i);
843 if ('a' <= c && c <= 'z')
851 seq1[i] = charToInt[c]; // set accordingly from setType
852 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
853 // peptides, or 4 for NA.
858 } catch (Exception e)
881 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
887 for (int i = 0; i < n; i++)
889 for (int j = 0; j < m; j++)
891 if (mat[i][j] >= max)
896 if (mat[i][j] <= min)
903 System.out.println(max + " " + min);
905 for (int i = 0; i < n; i++)
907 for (int j = 0; j < m; j++)
912 // System.out.println(mat[i][j]);
913 float score = (float) (mat[i][j] - min) / (float) (max - min);
914 g.setColor(new Color(score, 0, 0));
915 g.fillRect(x, y, psize, psize);
917 // System.out.println(x + " " + y + " " + score);