2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.analysis.scoremodels.ScoreMatrix;
24 import jalview.analysis.scoremodels.ScoreModels;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
30 import jalview.schemes.ResidueProperties;
31 import jalview.util.Comparison;
32 import jalview.util.Format;
33 import jalview.util.MapList;
34 import jalview.util.MessageManager;
36 import java.awt.Color;
37 import java.awt.Graphics;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.StringTokenizer;
51 public static final String PEP = "pep";
53 public static final String DNA = "dna";
55 private static final String NEWLINE = System.lineSeparator();
85 public String astr1 = "";
87 public String astr2 = "";
104 public float maxscore;
114 float[][] lookup = ScoreModels.getInstance().getBlosum62().getMatrix();
118 StringBuffer output = new StringBuffer();
122 private int[] charToInt;
125 * Creates a new AlignSeq object.
134 public AlignSeq(SequenceI s1, SequenceI s2, String type)
136 seqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
141 * Creates a new AlignSeq object.
150 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
151 String string2, String type)
153 seqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
159 * @return DOCUMENT ME!
161 public float getMaxScore()
169 * @return DOCUMENT ME!
171 public int getSeq2Start()
179 * @return DOCUMENT ME!
181 public int getSeq2End()
189 * @return DOCUMENT ME!
191 public int getSeq1Start()
199 * @return DOCUMENT ME!
201 public int getSeq1End()
209 * @return DOCUMENT ME!
211 public String getOutput()
213 return output.toString();
219 * @return DOCUMENT ME!
221 public String getAStr1()
229 * @return DOCUMENT ME!
231 public String getAStr2()
239 * @return DOCUMENT ME!
241 public int[] getASeq1()
249 * @return DOCUMENT ME!
251 public int[] getASeq2()
258 * @return aligned instance of Seq 1
260 public SequenceI getAlignedSeq1()
262 SequenceI alSeq1 = new Sequence(s1.getName(), getAStr1());
263 alSeq1.setStart(s1.getStart() + getSeq1Start() - 1);
264 alSeq1.setEnd(s1.getStart() + getSeq1End() - 1);
265 alSeq1.setDatasetSequence(s1.getDatasetSequence() == null ? s1 : s1
266 .getDatasetSequence());
272 * @return aligned instance of Seq 2
274 public SequenceI getAlignedSeq2()
276 SequenceI alSeq2 = new Sequence(s2.getName(), getAStr2());
277 alSeq2.setStart(s2.getStart() + getSeq2Start() - 1);
278 alSeq2.setEnd(s2.getStart() + getSeq2End() - 1);
279 alSeq2.setDatasetSequence(s2.getDatasetSequence() == null ? s2 : s2
280 .getDatasetSequence());
285 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
290 * - string to use for s1
294 * - string to use for s2
298 public void seqInit(SequenceI s1, String string1, SequenceI s2,
299 String string2, String type)
303 setDefaultParams(type);
304 seqInit(string1, string2);
308 * construct score matrix for string1 and string2 (after removing any existing
314 private void seqInit(String string1, String string2)
316 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
317 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
319 if (s1str.length() == 0 || s2str.length() == 0)
321 output.append("ALL GAPS: "
322 + (s1str.length() == 0 ? s1.getName() : " ")
323 + (s2str.length() == 0 ? s2.getName() : ""));
327 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
328 seq1 = new int[s1str.length()];
330 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
331 seq2 = new int[s2str.length()];
333 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
334 score = new float[s1str.length()][s2str.length()];
336 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
337 E = new float[s1str.length()][s2str.length()];
339 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
340 F = new float[s1str.length()][s2str.length()];
341 traceback = new int[s1str.length()][s2str.length()];
343 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
344 seq1 = stringToInt(s1str, type);
346 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
347 seq2 = stringToInt(s2str, type);
349 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
350 // long tstart = System.currentTimeMillis();
351 // calcScoreMatrix();
352 // long tend = System.currentTimeMillis();
353 // System.out.println("Time take to calculate score matrix = " +
354 // (tend-tstart) + " ms");
355 // printScoreMatrix(score);
356 // System.out.println();
357 // printScoreMatrix(traceback);
358 // System.out.println();
359 // printScoreMatrix(E);
360 // System.out.println();
361 // /printScoreMatrix(F);
362 // System.out.println();
363 // tstart = System.currentTimeMillis();
365 // tend = System.currentTimeMillis();
366 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
370 private void setDefaultParams(String type)
374 if (type.equals(AlignSeq.PEP))
376 lookup = ScoreModels.getInstance().getDefaultModel(true).getMatrix();
378 else if (type.equals(AlignSeq.DNA))
380 lookup = ScoreModels.getInstance().getDefaultModel(false).getMatrix();
384 private void setType(String type2)
387 if (type.equals(AlignSeq.PEP))
389 charToInt = ResidueProperties.aaIndex;
390 defInt = ResidueProperties.maxProteinIndex;
392 else if (type.equals(AlignSeq.DNA))
394 charToInt = ResidueProperties.nucleotideIndex;
395 defInt = ResidueProperties.maxNucleotideIndex;
399 output.append("Wrong type = dna or pep only");
400 throw new Error(MessageManager.formatMessage(
401 "error.unknown_type_dna_or_pep", new String[] { type2 }));
408 public void traceAlignment()
410 // Find the maximum score along the rhs or bottom row
413 for (int i = 0; i < seq1.length; i++)
415 if (score[i][seq2.length - 1] > max)
417 max = score[i][seq2.length - 1];
419 maxj = seq2.length - 1;
423 for (int j = 0; j < seq2.length; j++)
425 if (score[seq1.length - 1][j] > max)
427 max = score[seq1.length - 1][j];
428 maxi = seq1.length - 1;
433 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
437 maxscore = score[i][j] / 10;
442 aseq1 = new int[seq1.length + seq2.length];
443 aseq2 = new int[seq1.length + seq2.length];
445 count = (seq1.length + seq2.length) - 1;
447 while ((i > 0) && (j > 0))
449 if ((aseq1[count] != defInt) && (i >= 0))
451 aseq1[count] = seq1[i];
452 astr1 = s1str.charAt(i) + astr1;
455 if ((aseq2[count] != defInt) && (j > 0))
457 aseq2[count] = seq2[j];
458 astr2 = s2str.charAt(j) + astr2;
461 trace = findTrace(i, j);
471 aseq1[count] = defInt;
472 astr1 = "-" + astr1.substring(1);
474 else if (trace == -1)
477 aseq2[count] = defInt;
478 astr2 = "-" + astr2.substring(1);
487 if (aseq1[count] != defInt)
489 aseq1[count] = seq1[i];
490 astr1 = s1str.charAt(i) + astr1;
493 if (aseq2[count] != defInt)
495 aseq2[count] = seq2[j];
496 astr2 = s2str.charAt(j) + astr2;
503 public void printAlignment(java.io.PrintStream os)
505 // TODO: Use original sequence characters rather than re-translated
506 // characters in output
507 // Find the biggest id length for formatting purposes
508 String s1id = s1.getName(), s2id = s2.getName();
509 int maxid = s1.getName().length();
510 if (s2.getName().length() > maxid)
512 maxid = s2.getName().length();
517 // JAL-527 - truncate the sequence ids
518 if (s1.getName().length() > maxid)
520 s1id = s1.getName().substring(0, 30);
522 if (s2.getName().length() > maxid)
524 s2id = s2.getName().substring(0, 30);
527 int len = 72 - maxid - 1;
528 int nochunks = ((aseq1.length - count) / len)
529 + ((aseq1.length - count) % len > 0 ? 1 : 0);
532 output.append("Score = ").append(score[maxi][maxj]).append(NEWLINE);
533 output.append("Length of alignment = ")
534 .append(String.valueOf(aseq1.length - count)).append(NEWLINE);
535 output.append("Sequence ");
536 output.append(new Format("%" + maxid + "s").form(s1.getName()));
537 output.append(" : ").append(String.valueOf(s1.getStart()))
538 .append(" - ").append(String.valueOf(s1.getEnd()));
539 output.append(" (Sequence length = ")
540 .append(String.valueOf(s1str.length())).append(")")
542 output.append("Sequence ");
543 output.append(new Format("%" + maxid + "s").form(s2.getName()));
544 output.append(" : ").append(String.valueOf(s2.getStart()))
545 .append(" - ").append(String.valueOf(s2.getEnd()));
546 output.append(" (Sequence length = ")
547 .append(String.valueOf(s2str.length())).append(")")
548 .append(NEWLINE).append(NEWLINE);
550 ScoreMatrix pam250 = ScoreModels.getInstance().getPam250();
552 for (int j = 0; j < nochunks; j++)
554 // Print the first aligned sequence
555 output.append(new Format("%" + (maxid) + "s").form(s1id)).append(" ");
557 for (int i = 0; i < len; i++)
559 if ((i + (j * len)) < astr1.length())
561 output.append(astr1.charAt(i + (j * len)));
565 output.append(NEWLINE);
566 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
569 * Print out the match symbols:
570 * | for exact match (ignoring case)
571 * . if PAM250 score is positive
574 for (int i = 0; i < len; i++)
576 if ((i + (j * len)) < astr1.length())
578 char c1 = astr1.charAt(i + (j * len));
579 char c2 = astr2.charAt(i + (j * len));
580 boolean sameChar = Comparison.isSameResidue(c1, c2, false);
581 if (sameChar && !Comparison.isGap(c1))
586 else if (type.equals("pep"))
588 if (pam250.getPairwiseScore(c1, c2) > 0)
604 // Now print the second aligned sequence
605 output = output.append(NEWLINE);
606 output = output.append(new Format("%" + (maxid) + "s").form(s2id))
609 for (int i = 0; i < len; i++)
611 if ((i + (j * len)) < astr2.length())
613 output.append(astr2.charAt(i + (j * len)));
617 output.append(NEWLINE).append(NEWLINE);
620 pid = pid / (aseq1.length - count) * 100;
621 output = output.append(new Format("Percentage ID = %2.2f\n").form(pid));
624 os.print(output.toString());
625 } catch (Exception ex)
638 * @return DOCUMENT ME!
640 public int findTrace(int i, int j)
643 float max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
650 else if (F[i][j] == max)
664 else if (E[i][j] == max)
681 public void calcScoreMatrix()
686 // top left hand element
687 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
688 E[0][0] = -gapExtend;
691 // Calculate the top row first
692 for (int j = 1; j < m; j++)
694 // What should these values be? 0 maybe
695 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
696 F[0][j] = -gapExtend;
698 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
703 // Now do the left hand column
704 for (int i = 1; i < n; i++)
707 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
709 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
710 traceback[i][0] = -1;
713 // Now do all the other rows
714 for (int i = 1; i < n; i++)
716 for (int j = 1; j < m; j++)
718 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
719 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
721 score[i][j] = max(score[i - 1][j - 1]
722 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
723 traceback[i][j] = findTrace(i, j);
729 * Returns the given sequence with all of the given gap characters removed.
732 * a string of characters to be treated as gaps
738 public static String extractGaps(String gapChars, String seq)
740 if (gapChars == null || seq == null)
744 StringTokenizer str = new StringTokenizer(seq, gapChars);
745 StringBuilder newString = new StringBuilder(seq.length());
747 while (str.hasMoreTokens())
749 newString.append(str.nextToken());
752 return newString.toString();
765 * @return DOCUMENT ME!
767 private static float max(float f1, float f2, float f3)
792 * @return DOCUMENT ME!
794 private static float max(float f1, float f2)
814 * @return DOCUMENT ME!
816 int[] stringToInt(String s, String type)
818 int[] seq1 = new int[s.length()];
820 for (int i = 0; i < s.length(); i++)
822 // String ss = s.substring(i, i + 1).toUpperCase();
823 char c = s.charAt(i);
824 if ('a' <= c && c <= 'z')
832 seq1[i] = charToInt[c]; // set accordingly from setType
833 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
834 // peptides, or 4 for NA.
839 } catch (Exception e)
862 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
865 // TODO method dosen't seem to be referenced anywhere delete??
869 for (int i = 0; i < n; i++)
871 for (int j = 0; j < m; j++)
873 if (mat[i][j] >= max)
878 if (mat[i][j] <= min)
885 System.out.println(max + " " + min);
887 for (int i = 0; i < n; i++)
889 for (int j = 0; j < m; j++)
894 // System.out.println(mat[i][j]);
895 float score = (float) (mat[i][j] - min) / (float) (max - min);
896 g.setColor(new Color(score, 0, 0));
897 g.fillRect(x, y, psize, psize);
899 // System.out.println(x + " " + y + " " + score);
905 * Compute a globally optimal needleman and wunsch alignment between two
911 * AlignSeq.DNA or AlignSeq.PEP
913 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
916 AlignSeq as = new AlignSeq(s1, s2, type);
918 as.calcScoreMatrix();
925 * @return mapping from positions in S1 to corresponding positions in S2
927 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
929 ArrayList<Integer> as1 = new ArrayList<Integer>(), as2 = new ArrayList<Integer>();
930 int pdbpos = s2.getStart() + getSeq2Start() - 2;
931 int alignpos = s1.getStart() + getSeq1Start() - 2;
932 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
933 boolean lastmatch = false;
934 // and now trace the alignment onto the atom set.
935 for (int i = 0; i < astr1.length(); i++)
937 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
948 if (allowmismatch || c1 == c2)
950 // extend mapping interval
951 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)
953 as1.add(Integer.valueOf(alignpos));
954 as2.add(Integer.valueOf(pdbpos));
962 // extend mapping interval
965 as1.add(Integer.valueOf(lp1));
966 as2.add(Integer.valueOf(lp2));
971 // construct range pairs
973 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)], mapseq2 = new int[as2
974 .size() + (lastmatch ? 1 : 0)];
976 for (Integer ip : as1)
982 for (Integer ip : as2)
989 mapseq1[mapseq1.length - 1] = alignpos;
990 mapseq2[mapseq2.length - 1] = pdbpos;
992 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
994 jalview.datamodel.Mapping mapping = new Mapping(map);
1000 * matches ochains against al and populates seqs with the best match between
1001 * each ochain and the set in al
1005 * @param dnaOrProtein
1006 * @param removeOldAnnots
1007 * when true, old annotation is cleared before new annotation
1009 * @return List<List<SequenceI> originals, List<SequenceI> replacement,
1010 * List<AlignSeq> alignment between each>
1012 public static List<List<? extends Object>> replaceMatchingSeqsWith(
1013 List<SequenceI> seqs, List<AlignmentAnnotation> annotations,
1014 List<SequenceI> ochains, AlignmentI al, String dnaOrProtein,
1015 boolean removeOldAnnots)
1017 List<SequenceI> orig = new ArrayList<SequenceI>(), repl = new ArrayList<SequenceI>();
1018 List<AlignSeq> aligs = new ArrayList<AlignSeq>();
1019 if (al != null && al.getHeight() > 0)
1021 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
1022 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
1024 for (SequenceI sq : ochains)
1026 SequenceI bestm = null;
1027 AlignSeq bestaseq = null;
1028 float bestscore = 0;
1029 for (SequenceI msq : al.getSequences())
1031 AlignSeq aseq = doGlobalNWAlignment(msq, sq, dnaOrProtein);
1032 if (bestm == null || aseq.getMaxScore() > bestscore)
1034 bestscore = aseq.getMaxScore();
1039 System.out.println("Best Score for " + (matches.size() + 1) + " :"
1042 aligns.add(bestaseq);
1043 al.deleteSequence(bestm);
1045 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
1047 SequenceI sq, sp = seqs.get(p);
1049 if ((q = ochains.indexOf(sp)) > -1)
1051 seqs.set(p, sq = matches.get(q));
1054 sq.setName(sp.getName());
1055 sq.setDescription(sp.getDescription());
1057 sq.transferAnnotation(sp,
1058 sp2sq = aligns.get(q).getMappingFromS1(false));
1059 aligs.add(aligns.get(q));
1061 for (int ap = 0; ap < annotations.size();)
1063 if (annotations.get(ap).sequenceRef == sp)
1069 if (removeOldAnnots)
1071 annotations.remove(ap);
1075 AlignmentAnnotation alan = annotations.remove(ap);
1076 alan.liftOver(sq, sp2sq);
1077 alan.setSequenceRef(sq);
1078 sq.addAlignmentAnnotation(alan);
1086 if (sq.getAnnotation() != null && sq.getAnnotation().length > 0)
1088 annotations.addAll(inspos == -1 ? annotations.size() : inspos,
1089 Arrays.asList(sq.getAnnotation()));
1094 return Arrays.asList(orig, repl, aligs);
1098 * compute the PID vector used by the redundancy filter.
1100 * @param originalSequences
1101 * - sequences in alignment that are to filtered
1103 * - null or strings to be analysed (typically, visible portion of
1104 * each sequence in alignment)
1106 * - first column in window for calculation
1108 * - last column in window for calculation
1110 * - if true then use ungapped sequence to compute PID
1111 * @return vector containing maximum PID for i-th sequence and any sequences
1112 * longer than that seuqence
1114 public static float[] computeRedundancyMatrix(
1115 SequenceI[] originalSequences, String[] omitHidden, int start,
1116 int end, boolean ungapped)
1118 int height = originalSequences.length;
1119 float[] redundancy = new float[height];
1120 int[] lngth = new int[height];
1121 for (int i = 0; i < height; i++)
1127 // long start = System.currentTimeMillis();
1131 for (int i = 0; i < height; i++)
1134 for (int j = 0; j < i; j++)
1141 if (omitHidden == null)
1143 seqi = originalSequences[i].getSequenceAsString(start, end);
1144 seqj = originalSequences[j].getSequenceAsString(start, end);
1148 seqi = omitHidden[i];
1149 seqj = omitHidden[j];
1153 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1154 lngth[i] = ug.length();
1162 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1163 lngth[j] = ug.length();
1169 pid = Comparison.PID(seqi, seqj);
1171 // use real sequence length rather than string length
1172 if (lngth[j] < lngth[i])
1174 redundancy[j] = Math.max(pid, redundancy[j]);
1178 redundancy[i] = Math.max(pid, redundancy[i]);