2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
21 import jalview.datamodel.SequenceI;
23 import jalview.schemes.*;
25 import jalview.util.*;
40 public static final String PEP = "pep";
41 public static final String DNA = "dna";
43 public static java.util.Hashtable dnaHash = new java.util.Hashtable();
47 dnaHash.put("C", new Integer(0));
48 dnaHash.put("T", new Integer(1));
49 dnaHash.put("A", new Integer(2));
50 dnaHash.put("G", new Integer(3));
51 dnaHash.put("-", new Integer(4));
54 static String[] dna = { "C", "T", "A", "G", "-" };
57 "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
58 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-"
74 public String astr1="";
75 public String astr2="";
96 int[][] lookup = ResidueProperties.getBLOSUM62();
97 String[] intToStr = pep;
99 StringBuffer output = new StringBuffer();
103 * Creates a new AlignSeq object.
105 * @param s1 DOCUMENT ME!
106 * @param s2 DOCUMENT ME!
107 * @param type DOCUMENT ME!
109 public AlignSeq(SequenceI s1, SequenceI s2, String type)
111 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(), type);
115 * Creates a new AlignSeq object.
117 * @param s1 DOCUMENT ME!
118 * @param s2 DOCUMENT ME!
119 * @param type DOCUMENT ME!
121 public AlignSeq(SequenceI s1,
127 SeqInit(s1, string1, s2, string2, type);
133 * @return DOCUMENT ME!
135 public int getMaxScore()
143 * @return DOCUMENT ME!
145 public int getSeq2Start()
153 * @return DOCUMENT ME!
155 public int getSeq2End()
163 * @return DOCUMENT ME!
165 public int getSeq1Start()
173 * @return DOCUMENT ME!
175 public int getSeq1End()
183 * @return DOCUMENT ME!
185 public String getOutput()
187 return output.toString();
193 * @return DOCUMENT ME!
195 public String getAStr1()
203 * @return DOCUMENT ME!
205 public String getAStr2()
213 * @return DOCUMENT ME!
215 public int[] getASeq1()
223 * @return DOCUMENT ME!
225 public int[] getASeq2()
233 * @return DOCUMENT ME!
235 public SequenceI getS1()
243 * @return DOCUMENT ME!
245 public SequenceI getS2()
253 * @param s1 DOCUMENT ME!
254 * @param string1 - string to align for sequence1
255 * @param s2 sequence 2
256 * @param string2 - string to align for sequence2
257 * @param type DNA or PEPTIDE
259 public void SeqInit(SequenceI s1,
267 setDefaultParams(type);
268 SeqInit(string1,string2);
270 public void SeqInit(SequenceI s1,
274 ScoreMatrix scoreMatrix) {
277 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
278 lookup=scoreMatrix.getMatrix();
281 * construct score matrix for string1 and string2 (after removing any existing gaps
285 private void SeqInit(String string1, String string2) {
286 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
287 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
289 if(s1str.length()==0 || s2str.length()==0)
291 output.append("ALL GAPS: " +
292 (s1str.length()==0?s1.getName():" ")
293 +(s2str.length()==0?s2.getName():""));
297 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
298 seq1 = new int[s1str.length()];
300 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
301 seq2 = new int[s2str.length()];
303 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
304 score = new int[s1str.length()][s2str.length()];
306 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
307 E = new int[s1str.length()][s2str.length()];
309 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
310 F = new int[s1str.length()][s2str.length()];
311 traceback = new int[s1str.length()][s2str.length()];
313 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
314 seq1 = stringToInt(s1str, type);
316 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
317 seq2 = stringToInt(s2str, type);
319 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
320 // long tstart = System.currentTimeMillis();
321 // calcScoreMatrix();
322 //long tend = System.currentTimeMillis();
323 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
324 // printScoreMatrix(score);
325 //System.out.println();
326 //printScoreMatrix(traceback);
327 //System.out.println();
328 // printScoreMatrix(E);
329 //System.out.println();
330 ///printScoreMatrix(F);
331 //System.out.println();
332 // tstart = System.currentTimeMillis();
334 //tend = System.currentTimeMillis();
335 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
338 private void setDefaultParams(String type) {
341 if (type.equals(AlignSeq.PEP))
343 lookup = ResidueProperties.getDefaultPeptideMatrix();
345 else if (type.equals(AlignSeq.DNA))
347 lookup = ResidueProperties.getDefaultDnaMatrix();
351 private void setType(String type2) {
353 if (type.equals(AlignSeq.PEP))
358 else if (type.equals(AlignSeq.DNA))
365 output.append("Wrong type = dna or pep only");
366 throw new Error("Unknown Type "+type2+" - dna or pep are the only allowed values.");
373 public void traceAlignment()
375 // Find the maximum score along the rhs or bottom row
378 for (int i = 0; i < seq1.length; i++)
380 if (score[i][seq2.length - 1] > max)
382 max = score[i][seq2.length - 1];
384 maxj = seq2.length - 1;
388 for (int j = 0; j < seq2.length; j++)
390 if (score[seq1.length - 1][j] > max)
392 max = score[seq1.length - 1][j];
393 maxi = seq1.length - 1;
398 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
402 maxscore = score[i][j] / 10;
407 aseq1 = new int[seq1.length + seq2.length];
408 aseq2 = new int[seq1.length + seq2.length];
410 count = (seq1.length + seq2.length) - 1;
412 while ((i > 0) && (j > 0))
414 if ((aseq1[count] != defInt) && (i >= 0))
416 aseq1[count] = seq1[i];
417 astr1 = intToStr[seq1[i]] + astr1;
420 if ((aseq2[count] != defInt) && (j > 0))
422 aseq2[count] = seq2[j];
423 astr2 = intToStr[seq2[j]] + astr2;
426 trace = findTrace(i, j);
436 aseq1[count] = defInt;
437 astr1 = "-" + astr1.substring(1);
439 else if (trace == -1)
442 aseq2[count] = defInt;
443 astr2 = "-" + astr2.substring(1);
452 if (aseq1[count] != defInt)
454 aseq1[count] = seq1[i];
455 astr1 = intToStr[seq1[i]] + astr1;
458 if (aseq2[count] != defInt)
460 aseq2[count] = seq2[j];
461 astr2 = intToStr[seq2[j]] + astr2;
468 public void printAlignment(java.io.PrintStream os)
470 // Find the biggest id length for formatting purposes
471 int maxid = s1.getName().length();
473 if (s2.getName().length() > maxid)
475 maxid = s2.getName().length();
478 int len = 72 - maxid - 1;
479 int nochunks = ((aseq1.length - count) / len) + 1;
482 output.append("Score = " + score[maxi][maxj] + "\n");
483 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
484 output.append("Sequence ");
485 output.append(new Format("%" + maxid + "s").form(s1.getName()));
486 output.append(" : " + s1.getStart() + " - " + s1.getEnd() + " (Sequence length = " +
487 s1str.length() + ")\n");
488 output .append("Sequence ");
489 output.append(new Format("%" + maxid + "s").form(s2.getName()));
490 output.append(" : " + s2.getStart() + " - " + s2.getEnd() + " (Sequence length = " +
491 s2str.length() + ")\n\n");
493 for (int j = 0; j < nochunks; j++)
495 // Print the first aligned sequence
496 output.append(new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
498 for (int i = 0; i < len; i++)
500 if ((count + i + (j * len)) < aseq1.length)
502 output.append(new Format("%s").form(intToStr[aseq1[count + i +
508 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
510 // Print out the matching chars
511 for (int i = 0; i < len; i++)
513 if ((count + i + (j * len)) < aseq1.length)
515 if (intToStr[aseq1[count + i + (j * len)]].equals(
516 intToStr[aseq2[count + i + (j * len)]]) &&
517 !intToStr[aseq1[count + i + (j * len)]].equals("-"))
522 else if (type.equals("pep"))
524 if (ResidueProperties.getPAM250(
525 intToStr[aseq1[count + i + (j * len)]],
526 intToStr[aseq2[count + i + (j * len)]]) > 0)
542 // Now print the second aligned sequence
543 output = output.append("\n");
544 output = output.append(new Format("%" + (maxid) + "s").form(s2.getName()) + " ");
546 for (int i = 0; i < len; i++)
548 if ((count + i + (j * len)) < aseq1.length)
550 output .append(new Format("%s").form(intToStr[aseq2[count + i +
555 output = output .append("\n\n");
558 pid = pid / (float) (aseq1.length - count) * 100;
559 output = output.append(new Format("Percentage ID = %2.2f\n\n").form(pid));
562 os.print(output.toString());
563 }catch(Exception ex){}
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);
709 * @param gapChar DOCUMENT ME!
710 * @param seq DOCUMENT ME!
712 * @return DOCUMENT ME!
714 public static String extractGaps(String gapChar, String seq)
716 StringTokenizer str = new StringTokenizer(seq, gapChar);
717 StringBuffer newString = new StringBuffer();
719 while (str.hasMoreTokens())
721 newString.append( str.nextToken() );
724 return newString.toString();
730 * @param i1 DOCUMENT ME!
731 * @param i2 DOCUMENT ME!
732 * @param i3 DOCUMENT ME!
734 * @return DOCUMENT ME!
736 public int max(int i1, int i2, int i3)
756 * @param i1 DOCUMENT ME!
757 * @param i2 DOCUMENT ME!
759 * @return DOCUMENT ME!
761 public int max(int i1, int i2)
776 * @param s DOCUMENT ME!
777 * @param type DOCUMENT ME!
779 * @return DOCUMENT ME!
781 public int[] stringToInt(String s, String type)
783 int[] seq1 = new int[s.length()];
785 for (int i = 0; i < s.length(); i++)
787 // String ss = s.substring(i, i + 1).toUpperCase();
788 char c = s.charAt(i);
789 if ('a' <= c && c <= 'z')
798 if (type.equals("pep"))
800 seq1[i] = ResidueProperties.aaIndex[c];
802 else if (type.equals("dna"))
804 seq1[i] = ResidueProperties.nucleotideIndex[c];
814 if (type.equals("dna"))
831 * @param g DOCUMENT ME!
832 * @param mat DOCUMENT ME!
833 * @param n DOCUMENT ME!
834 * @param m DOCUMENT ME!
835 * @param psize DOCUMENT ME!
837 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
843 for (int i = 0; i < n; i++)
845 for (int j = 0; j < m; j++)
847 if (mat[i][j] >= max)
852 if (mat[i][j] <= min)
859 System.out.println(max + " " + min);
861 for (int i = 0; i < n; i++)
863 for (int j = 0; j < m; j++)
868 // System.out.println(mat[i][j]);
869 float score = (float) (mat[i][j] - min) / (float) (max - min);
870 g.setColor(new Color(score, 0, 0));
871 g.fillRect(x, y, psize, psize);
873 // System.out.println(x + " " + y + " " + score);