2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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;
23 import jalview.datamodel.*;
24 import jalview.schemes.*;
25 import jalview.util.*;
35 public static final String PEP = "pep";
37 public static final String DNA = "dna";
40 { "A", "C", "G", "T", "-" };
42 // "C", "T", "A", "G", "-"};
44 { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
45 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-" };
75 public String astr1 = "";
77 public String astr2 = "";
104 int[][] lookup = ResidueProperties.getBLOSUM62();
106 String[] intToStr = pep;
110 StringBuffer output = new StringBuffer();
114 private int[] charToInt;
117 * Creates a new AlignSeq object.
126 public AlignSeq(SequenceI s1, SequenceI s2, String type)
128 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
133 * Creates a new AlignSeq object.
142 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
143 String string2, String type)
145 SeqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
151 * @return DOCUMENT ME!
153 public int getMaxScore()
161 * @return DOCUMENT ME!
163 public int getSeq2Start()
171 * @return DOCUMENT ME!
173 public int getSeq2End()
181 * @return DOCUMENT ME!
183 public int getSeq1Start()
191 * @return DOCUMENT ME!
193 public int getSeq1End()
201 * @return DOCUMENT ME!
203 public String getOutput()
205 return output.toString();
211 * @return DOCUMENT ME!
213 public String getAStr1()
221 * @return DOCUMENT ME!
223 public String getAStr2()
231 * @return DOCUMENT ME!
233 public int[] getASeq1()
241 * @return DOCUMENT ME!
243 public int[] getASeq2()
251 * @return DOCUMENT ME!
253 public SequenceI getS1()
261 * @return DOCUMENT ME!
263 public SequenceI getS2()
269 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
270 * @param s1 - sequence 1
271 * @param string1 - string to use for s1
272 * @param s2 - sequence 2
273 * @param string2 - string to use for s2
277 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
278 String string2, String type)
282 setDefaultParams(type);
283 SeqInit(string1, string2);
287 * Construct score matrix for sequences with custom substitution matrix
288 * @param s1 - sequence 1
289 * @param string1 - string to use for s1
290 * @param s2 - sequence 2
291 * @param string2 - string to use for s2
292 * @param scoreMatrix - substitution matrix to use for alignment
294 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
295 String string2, ScoreMatrix scoreMatrix)
299 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
300 lookup = scoreMatrix.getMatrix();
304 * construct score matrix for string1 and string2 (after removing any existing
310 private void SeqInit(String string1, String string2)
312 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
313 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
315 if (s1str.length() == 0 || s2str.length() == 0)
317 output.append("ALL GAPS: "
318 + (s1str.length() == 0 ? s1.getName() : " ")
319 + (s2str.length() == 0 ? s2.getName() : ""));
323 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
324 seq1 = new int[s1str.length()];
326 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
327 seq2 = new int[s2str.length()];
329 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
330 score = new int[s1str.length()][s2str.length()];
332 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
333 E = new int[s1str.length()][s2str.length()];
335 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
336 F = new int[s1str.length()][s2str.length()];
337 traceback = new int[s1str.length()][s2str.length()];
339 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
340 seq1 = stringToInt(s1str, type);
342 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
343 seq2 = stringToInt(s2str, type);
345 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
346 // long tstart = System.currentTimeMillis();
347 // calcScoreMatrix();
348 // long tend = System.currentTimeMillis();
349 // System.out.println("Time take to calculate score matrix = " +
350 // (tend-tstart) + " ms");
351 // printScoreMatrix(score);
352 // System.out.println();
353 // printScoreMatrix(traceback);
354 // System.out.println();
355 // printScoreMatrix(E);
356 // System.out.println();
357 // /printScoreMatrix(F);
358 // System.out.println();
359 // tstart = System.currentTimeMillis();
361 // tend = System.currentTimeMillis();
362 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
366 private void setDefaultParams(String type)
370 if (type.equals(AlignSeq.PEP))
372 lookup = ResidueProperties.getDefaultPeptideMatrix();
374 else if (type.equals(AlignSeq.DNA))
376 lookup = ResidueProperties.getDefaultDnaMatrix();
380 private void setType(String type2)
383 if (type.equals(AlignSeq.PEP))
386 charToInt = ResidueProperties.aaIndex;
387 defInt = ResidueProperties.maxProteinIndex;
389 else if (type.equals(AlignSeq.DNA))
392 charToInt = ResidueProperties.nucleotideIndex;
393 defInt = ResidueProperties.maxNucleotideIndex;
397 output.append("Wrong type = dna or pep only");
398 throw new Error("Unknown Type " + type2
399 + " - dna or pep are the only allowed values.");
406 public void traceAlignment()
408 // Find the maximum score along the rhs or bottom row
411 for (int i = 0; i < seq1.length; i++)
413 if (score[i][seq2.length - 1] > max)
415 max = score[i][seq2.length - 1];
417 maxj = seq2.length - 1;
421 for (int j = 0; j < seq2.length; j++)
423 if (score[seq1.length - 1][j] > max)
425 max = score[seq1.length - 1][j];
426 maxi = seq1.length - 1;
431 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
435 maxscore = score[i][j] / 10;
440 aseq1 = new int[seq1.length + seq2.length];
441 aseq2 = new int[seq1.length + seq2.length];
443 count = (seq1.length + seq2.length) - 1;
445 while ((i > 0) && (j > 0))
447 if ((aseq1[count] != defInt) && (i >= 0))
449 aseq1[count] = seq1[i];
450 astr1 = s1str.charAt(i) + astr1;
453 if ((aseq2[count] != defInt) && (j > 0))
455 aseq2[count] = seq2[j];
456 astr2 = s2str.charAt(j) + astr2;
459 trace = findTrace(i, j);
469 aseq1[count] = defInt;
470 astr1 = "-" + astr1.substring(1);
472 else if (trace == -1)
475 aseq2[count] = defInt;
476 astr2 = "-" + astr2.substring(1);
485 if (aseq1[count] != defInt)
487 aseq1[count] = seq1[i];
488 astr1 = s1str.charAt(i) + astr1;
491 if (aseq2[count] != defInt)
493 aseq2[count] = seq2[j];
494 astr2 = s2str.charAt(j) + astr2;
501 public void printAlignment(java.io.PrintStream os)
503 // TODO: Use original sequence characters rather than re-translated
504 // characters in output
505 // Find the biggest id length for formatting purposes
506 String s1id = s1.getName(), s2id = s2.getName();
507 int maxid = s1.getName().length();
508 if (s2.getName().length() > maxid)
510 maxid = s2.getName().length();
515 // JAL-527 - truncate the sequence ids
516 if (s1.getName().length() > maxid)
518 s1id = s1.getName().substring(0, 30);
520 if (s2.getName().length() > maxid)
522 s2id = s2.getName().substring(0, 30);
525 int len = 72 - maxid - 1;
526 int nochunks = ((aseq1.length - count) / len) + 1;
529 output.append("Score = " + score[maxi][maxj] + "\n");
530 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
531 output.append("Sequence ");
532 output.append(new Format("%" + maxid + "s").form(s1.getName()));
533 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
534 + " (Sequence length = " + s1str.length() + ")\n");
535 output.append("Sequence ");
536 output.append(new Format("%" + maxid + "s").form(s2.getName()));
537 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
538 + " (Sequence length = " + s2str.length() + ")\n\n");
540 for (int j = 0; j < nochunks; j++)
542 // Print the first aligned sequence
543 output.append(new Format("%" + (maxid) + "s").form(s1id) + " ");
545 for (int i = 0; i < len; i++)
547 if ((i + (j * len)) < astr1.length())
549 output.append(astr1.charAt(i + (j * len)));
554 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
556 // Print out the matching chars
557 for (int i = 0; i < len; i++)
559 if ((i + (j * len)) < astr1.length())
561 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
562 && !jalview.util.Comparison.isGap(astr1.charAt(i
568 else if (type.equals("pep"))
570 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
571 astr2.charAt(i + (j * len))) > 0)
587 // Now print the second aligned sequence
588 output = output.append("\n");
589 output = output.append(new Format("%" + (maxid) + "s").form(s2id)
592 for (int i = 0; i < len; i++)
594 if ((i + (j * len)) < astr2.length())
596 output.append(astr2.charAt(i + (j * len)));
600 output = output.append("\n\n");
603 pid = pid / (float) (aseq1.length - count) * 100;
604 output = output.append(new Format("Percentage ID = %2.2f\n\n")
609 os.print(output.toString());
610 } catch (Exception ex)
621 public void printScoreMatrix(int[][] mat)
626 for (int i = 0; i < n; i++)
628 // Print the top sequence
631 Format.print(System.out, "%8s", s2str.substring(0, 1));
633 for (int jj = 1; jj < m; jj++)
635 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
638 System.out.println();
641 for (int j = 0; j < m; j++)
645 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
648 Format.print(System.out, "%3d ", mat[i][j] / 10);
651 System.out.println();
663 * @return DOCUMENT ME!
665 public int findTrace(int i, int j)
668 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
675 else if (F[i][j] == max)
689 else if (E[i][j] == max)
706 public void calcScoreMatrix()
711 // top left hand element
712 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
713 E[0][0] = -gapExtend;
716 // Calculate the top row first
717 for (int j = 1; j < m; j++)
719 // What should these values be? 0 maybe
720 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
721 F[0][j] = -gapExtend;
723 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
728 // Now do the left hand column
729 for (int i = 1; i < n; i++)
732 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
734 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
735 traceback[i][0] = -1;
738 // Now do all the other rows
739 for (int i = 1; i < n; i++)
741 for (int j = 1; j < m; j++)
743 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
744 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
746 score[i][j] = max(score[i - 1][j - 1]
747 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
748 traceback[i][j] = findTrace(i, j);
761 * @return DOCUMENT ME!
763 public static String extractGaps(String gapChar, String seq)
765 StringTokenizer str = new StringTokenizer(seq, gapChar);
766 StringBuffer newString = new StringBuffer();
768 while (str.hasMoreTokens())
770 newString.append(str.nextToken());
773 return newString.toString();
786 * @return DOCUMENT ME!
788 public int max(int i1, int i2, int i3)
813 * @return DOCUMENT ME!
815 public int max(int i1, int i2)
835 * @return DOCUMENT ME!
837 public int[] stringToInt(String s, String type)
839 int[] seq1 = new int[s.length()];
841 for (int i = 0; i < s.length(); i++)
843 // String ss = s.substring(i, i + 1).toUpperCase();
844 char c = s.charAt(i);
845 if ('a' <= c && c <= 'z')
853 seq1[i] = charToInt[c]; // set accordingly from setType
854 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
855 // peptides, or 4 for NA.
860 } catch (Exception e)
883 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
889 for (int i = 0; i < n; i++)
891 for (int j = 0; j < m; j++)
893 if (mat[i][j] >= max)
898 if (mat[i][j] <= min)
905 System.out.println(max + " " + min);
907 for (int i = 0; i < n; i++)
909 for (int j = 0; j < m; j++)
914 // System.out.println(mat[i][j]);
915 float score = (float) (mat[i][j] - min) / (float) (max - min);
916 g.setColor(new Color(score, 0, 0));
917 g.fillRect(x, y, psize, psize);
919 // System.out.println(x + " " + y + " " + score);
925 * Compute a globally optimal needleman and wunsch alignment between two
931 * AlignSeq.DNA or AlignSeq.PEP
933 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
936 AlignSeq as = new AlignSeq(s1, s2, type);
938 as.calcScoreMatrix();
945 * @return mapping from positions in S1 to corresponding positions in S2
947 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
949 ArrayList<Integer> as1 = new ArrayList<Integer>(), as2 = new ArrayList<Integer>();
950 int pdbpos = s2.getStart() + getSeq2Start() - 2;
951 int alignpos = s1.getStart() + getSeq1Start() - 2;
952 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
953 boolean lastmatch = false;
954 // and now trace the alignment onto the atom set.
955 for (int i = 0; i < astr1.length(); i++)
957 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
968 if (allowmismatch || c1 == c2)
971 // extend mapping interval.
972 if (lp1 + 1 != alignpos || lp2+1 !=pdbpos)
974 as1.add(Integer.valueOf(alignpos));
975 as2.add(Integer.valueOf(pdbpos));
985 // construct range pairs
986 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)], mapseq2 = new int[as2
987 .size() + (lastmatch ? 1 : 0)];
989 for (Integer ip : as1)
995 for (Integer ip : as2)
1002 mapseq1[mapseq1.length - 1] = alignpos;
1003 mapseq2[mapseq2.length - 1] = pdbpos;
1005 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
1007 jalview.datamodel.Mapping mapping = new Mapping(map);
1013 * compute the PID vector used by the redundancy filter.
1015 * @param originalSequences
1016 * - sequences in alignment that are to filtered
1018 * - null or strings to be analysed (typically, visible portion of
1019 * each sequence in alignment)
1021 * - first column in window for calculation
1023 * - last column in window for calculation
1025 * - if true then use ungapped sequence to compute PID
1026 * @return vector containing maximum PID for i-th sequence and any sequences
1027 * longer than that seuqence
1029 public static float[] computeRedundancyMatrix(
1030 SequenceI[] originalSequences, String[] omitHidden, int start,
1031 int end, boolean ungapped)
1033 int height = originalSequences.length;
1034 float[] redundancy = new float[height];
1035 int[] lngth = new int[height];
1036 for (int i = 0; i < height; i++)
1042 // long start = System.currentTimeMillis();
1046 for (int i = 0; i < height; i++)
1049 for (int j = 0; j < i; j++)
1056 if (omitHidden == null)
1058 seqi = originalSequences[i].getSequenceAsString(start, end);
1059 seqj = originalSequences[j].getSequenceAsString(start, end);
1063 seqi = omitHidden[i];
1064 seqj = omitHidden[j];
1068 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1069 lngth[i] = ug.length();
1077 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1078 lngth[j] = ug.length();
1084 pid = Comparison.PID(seqi, seqj);
1086 // use real sequence length rather than string length
1087 if (lngth[j] < lngth[i])
1089 redundancy[j] = Math.max(pid, redundancy[j]);
1093 redundancy[i] = Math.max(pid, redundancy[i]);