2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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.toUpperCase(), s2, string2.toUpperCase(), 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;
386 defInt = ResidueProperties.maxProteinIndex;
388 else if (type.equals(AlignSeq.DNA))
391 charToInt = ResidueProperties.nucleotideIndex;
392 defInt = ResidueProperties.maxNucleotideIndex;
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 String s1id = s1.getName(), s2id = s2.getName();
506 int maxid = s1.getName().length();
507 if (s2.getName().length() > maxid)
509 maxid = s2.getName().length();
514 // JAL-527 - truncate the sequence ids
515 if (s1.getName().length() > maxid)
517 s1id = s1.getName().substring(0, 30);
519 if (s2.getName().length() > maxid)
521 s2id = s2.getName().substring(0, 30);
524 int len = 72 - maxid - 1;
525 int nochunks = ((aseq1.length - count) / len) + 1;
528 output.append("Score = " + score[maxi][maxj] + "\n");
529 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
530 output.append("Sequence ");
531 output.append(new Format("%" + maxid + "s").form(s1.getName()));
532 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
533 + " (Sequence length = " + s1str.length() + ")\n");
534 output.append("Sequence ");
535 output.append(new Format("%" + maxid + "s").form(s2.getName()));
536 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
537 + " (Sequence length = " + s2str.length() + ")\n\n");
539 for (int j = 0; j < nochunks; j++)
541 // Print the first aligned sequence
542 output.append(new Format("%" + (maxid) + "s").form(s1id) + " ");
544 for (int i = 0; i < len; i++)
546 if ((i + (j * len)) < astr1.length())
548 output.append(astr1.charAt(i + (j * len)));
553 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
555 // Print out the matching chars
556 for (int i = 0; i < len; i++)
558 if ((i + (j * len)) < astr1.length())
560 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
561 && !jalview.util.Comparison.isGap(astr1.charAt(i
567 else if (type.equals("pep"))
569 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
570 astr2.charAt(i + (j * len))) > 0)
586 // Now print the second aligned sequence
587 output = output.append("\n");
588 output = output.append(new Format("%" + (maxid) + "s").form(s2id)
591 for (int i = 0; i < len; i++)
593 if ((i + (j * len)) < astr2.length())
595 output.append(astr2.charAt(i + (j * len)));
599 output = output.append("\n\n");
602 pid = pid / (float) (aseq1.length - count) * 100;
603 output = output.append(new Format("Percentage ID = %2.2f\n\n")
608 os.print(output.toString());
609 } catch (Exception ex)
620 public void printScoreMatrix(int[][] mat)
625 for (int i = 0; i < n; i++)
627 // Print the top sequence
630 Format.print(System.out, "%8s", s2str.substring(0, 1));
632 for (int jj = 1; jj < m; jj++)
634 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
637 System.out.println();
640 for (int j = 0; j < m; j++)
644 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
647 Format.print(System.out, "%3d ", mat[i][j] / 10);
650 System.out.println();
662 * @return DOCUMENT ME!
664 public int findTrace(int i, int j)
667 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
674 else if (F[i][j] == max)
688 else if (E[i][j] == max)
705 public void calcScoreMatrix()
710 // top left hand element
711 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
712 E[0][0] = -gapExtend;
715 // Calculate the top row first
716 for (int j = 1; j < m; j++)
718 // What should these values be? 0 maybe
719 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
720 F[0][j] = -gapExtend;
722 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
727 // Now do the left hand column
728 for (int i = 1; i < n; i++)
731 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
733 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
734 traceback[i][0] = -1;
737 // Now do all the other rows
738 for (int i = 1; i < n; i++)
740 for (int j = 1; j < m; j++)
742 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
743 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
745 score[i][j] = max(score[i - 1][j - 1]
746 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
747 traceback[i][j] = findTrace(i, j);
760 * @return DOCUMENT ME!
762 public static String extractGaps(String gapChar, String seq)
764 StringTokenizer str = new StringTokenizer(seq, gapChar);
765 StringBuffer newString = new StringBuffer();
767 while (str.hasMoreTokens())
769 newString.append(str.nextToken());
772 return newString.toString();
785 * @return DOCUMENT ME!
787 public int max(int i1, int i2, int i3)
812 * @return DOCUMENT ME!
814 public int max(int i1, int i2)
834 * @return DOCUMENT ME!
836 public int[] stringToInt(String s, String type)
838 int[] seq1 = new int[s.length()];
840 for (int i = 0; i < s.length(); i++)
842 // String ss = s.substring(i, i + 1).toUpperCase();
843 char c = s.charAt(i);
844 if ('a' <= c && c <= 'z')
852 seq1[i] = charToInt[c]; // set accordingly from setType
853 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
854 // peptides, or 4 for NA.
859 } catch (Exception e)
882 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
888 for (int i = 0; i < n; i++)
890 for (int j = 0; j < m; j++)
892 if (mat[i][j] >= max)
897 if (mat[i][j] <= min)
904 System.out.println(max + " " + min);
906 for (int i = 0; i < n; i++)
908 for (int j = 0; j < m; j++)
913 // System.out.println(mat[i][j]);
914 float score = (float) (mat[i][j] - min) / (float) (max - min);
915 g.setColor(new Color(score, 0, 0));
916 g.fillRect(x, y, psize, psize);
918 // System.out.println(x + " " + y + " " + score);
924 * compute the PID vector used by the redundancy filter.
926 * @param originalSequences
927 * - sequences in alignment that are to filtered
929 * - null or strings to be analysed (typically, visible portion of
930 * each sequence in alignment)
932 * - first column in window for calculation
934 * - last column in window for calculation
936 * - if true then use ungapped sequence to compute PID
937 * @return vector containing maximum PID for i-th sequence and any sequences
938 * longer than that seuqence
940 public static float[] computeRedundancyMatrix(
941 SequenceI[] originalSequences, String[] omitHidden, int start,
942 int end, boolean ungapped)
944 int height = originalSequences.length;
945 float[] redundancy = new float[height];
946 int[] lngth = new int[height];
947 for (int i = 0; i < height; i++)
953 // long start = System.currentTimeMillis();
957 for (int i = 0; i < height; i++)
960 for (int j = 0; j < i; j++)
967 if (omitHidden == null)
969 seqi = originalSequences[i].getSequenceAsString(start, end);
970 seqj = originalSequences[j].getSequenceAsString(start, end);
974 seqi = omitHidden[i];
975 seqj = omitHidden[j];
979 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
980 lngth[i] = ug.length();
988 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
989 lngth[j] = ug.length();
995 pid = Comparison.PID(seqi, seqj);
997 // use real sequence length rather than string length
998 if (lngth[j] < lngth[i])
1000 redundancy[j] = Math.max(pid, redundancy[j]);
1004 redundancy[i] = Math.max(pid, redundancy[i]);