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.util.Comparison;
31 import jalview.util.Format;
32 import jalview.util.MapList;
33 import jalview.util.MessageManager;
35 import java.awt.Color;
36 import java.awt.Graphics;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.StringTokenizer;
50 public static final String PEP = "pep";
52 public static final String DNA = "dna";
54 private static final String NEWLINE = System.lineSeparator();
84 public String astr1 = "";
86 public String astr2 = "";
103 public float maxscore;
117 StringBuffer output = new StringBuffer();
119 String type; // AlignSeq.PEP or AlignSeq.DNA
121 private ScoreMatrix scoreModel;
124 * Creates a new AlignSeq object.
126 * @param s1 first sequence for alignment
127 * @param s2 second sequence for alignment
128 * @param type molecule type, either AlignSeq.PEP or AlignSeq.DNA
130 public AlignSeq(SequenceI s1, SequenceI s2, String type)
132 seqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
137 * Creates a new AlignSeq object.
146 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
147 String string2, String type)
149 seqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
155 * @return DOCUMENT ME!
157 public float getMaxScore()
165 * @return DOCUMENT ME!
167 public int getSeq2Start()
175 * @return DOCUMENT ME!
177 public int getSeq2End()
185 * @return DOCUMENT ME!
187 public int getSeq1Start()
195 * @return DOCUMENT ME!
197 public int getSeq1End()
205 * @return DOCUMENT ME!
207 public String getOutput()
209 return output.toString();
215 * @return DOCUMENT ME!
217 public String getAStr1()
225 * @return DOCUMENT ME!
227 public String getAStr2()
235 * @return DOCUMENT ME!
237 public int[] getASeq1()
245 * @return DOCUMENT ME!
247 public int[] getASeq2()
254 * @return aligned instance of Seq 1
256 public SequenceI getAlignedSeq1()
258 SequenceI alSeq1 = new Sequence(s1.getName(), getAStr1());
259 alSeq1.setStart(s1.getStart() + getSeq1Start() - 1);
260 alSeq1.setEnd(s1.getStart() + getSeq1End() - 1);
261 alSeq1.setDatasetSequence(s1.getDatasetSequence() == null ? s1 : s1
262 .getDatasetSequence());
268 * @return aligned instance of Seq 2
270 public SequenceI getAlignedSeq2()
272 SequenceI alSeq2 = new Sequence(s2.getName(), getAStr2());
273 alSeq2.setStart(s2.getStart() + getSeq2Start() - 1);
274 alSeq2.setEnd(s2.getStart() + getSeq2End() - 1);
275 alSeq2.setDatasetSequence(s2.getDatasetSequence() == null ? s2 : s2
276 .getDatasetSequence());
281 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
286 * - string to use for s1
290 * - string to use for s2
294 public void seqInit(SequenceI s1, String string1, SequenceI s2,
295 String string2, String type)
299 setDefaultParams(type);
300 seqInit(string1, string2);
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 seq1 = new int[s1str.length()];
325 seq2 = new int[s2str.length()];
327 score = new float[s1str.length()][s2str.length()];
329 E = new float[s1str.length()][s2str.length()];
331 F = new float[s1str.length()][s2str.length()];
332 traceback = new int[s1str.length()][s2str.length()];
334 seq1 = indexEncode(s1str);
336 seq2 = indexEncode(s2str);
339 private void setDefaultParams(String moleculeType)
341 if (!PEP.equals(moleculeType) && !DNA.equals(moleculeType))
343 output.append("Wrong type = dna or pep only");
344 throw new Error(MessageManager.formatMessage(
345 "error.unknown_type_dna_or_pep",
346 new String[] { moleculeType }));
350 scoreModel = ScoreModels.getInstance().getDefaultModel(
352 lookup = scoreModel.getMatrix();
353 gapIndex = scoreModel.getMatrixIndex(' ');
359 public void traceAlignment()
361 // Find the maximum score along the rhs or bottom row
362 float max = -Float.MAX_VALUE;
364 for (int i = 0; i < seq1.length; i++)
366 if (score[i][seq2.length - 1] > max)
368 max = score[i][seq2.length - 1];
370 maxj = seq2.length - 1;
374 for (int j = 0; j < seq2.length; j++)
376 if (score[seq1.length - 1][j] > max)
378 max = score[seq1.length - 1][j];
379 maxi = seq1.length - 1;
384 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
388 maxscore = score[i][j] / 10;
393 aseq1 = new int[seq1.length + seq2.length];
394 aseq2 = new int[seq1.length + seq2.length];
396 StringBuilder sb1 = new StringBuilder(aseq1.length);
397 StringBuilder sb2 = new StringBuilder(aseq2.length);
399 count = (seq1.length + seq2.length) - 1;
401 while (i > 0 && j > 0)
403 aseq1[count] = seq1[i];
404 sb1.append(s1str.charAt(i));
405 aseq2[count] = seq2[j];
406 sb2.append(s2str.charAt(j));
408 trace = findTrace(i, j);
418 aseq1[count] = gapIndex;
419 sb1.replace(sb1.length() - 1, sb1.length(), "-");
421 else if (trace == -1)
424 aseq2[count] = gapIndex;
425 sb2.replace(sb2.length() - 1, sb2.length(), "-");
434 if (aseq1[count] != gapIndex)
436 aseq1[count] = seq1[i];
437 sb1.append(s1str.charAt(i));
440 if (aseq2[count] != gapIndex)
442 aseq2[count] = seq2[j];
443 sb2.append(s2str.charAt(j));
447 * we built the character strings backwards, so now
448 * reverse them to convert to sequence strings
450 astr1 = sb1.reverse().toString();
451 astr2 = sb2.reverse().toString();
457 public void printAlignment(java.io.PrintStream os)
459 // TODO: Use original sequence characters rather than re-translated
460 // characters in output
461 // Find the biggest id length for formatting purposes
462 String s1id = s1.getName(), s2id = s2.getName();
463 int maxid = s1.getName().length();
464 if (s2.getName().length() > maxid)
466 maxid = s2.getName().length();
471 // JAL-527 - truncate the sequence ids
472 if (s1.getName().length() > maxid)
474 s1id = s1.getName().substring(0, 30);
476 if (s2.getName().length() > maxid)
478 s2id = s2.getName().substring(0, 30);
481 int len = 72 - maxid - 1;
482 int nochunks = ((aseq1.length - count) / len)
483 + ((aseq1.length - count) % len > 0 ? 1 : 0);
486 output.append("Score = ").append(score[maxi][maxj]).append(NEWLINE);
487 output.append("Length of alignment = ")
488 .append(String.valueOf(aseq1.length - count)).append(NEWLINE);
489 output.append("Sequence ");
490 output.append(new Format("%" + maxid + "s").form(s1.getName()));
491 output.append(" : ").append(String.valueOf(s1.getStart()))
492 .append(" - ").append(String.valueOf(s1.getEnd()));
493 output.append(" (Sequence length = ")
494 .append(String.valueOf(s1str.length())).append(")")
496 output.append("Sequence ");
497 output.append(new Format("%" + maxid + "s").form(s2.getName()));
498 output.append(" : ").append(String.valueOf(s2.getStart()))
499 .append(" - ").append(String.valueOf(s2.getEnd()));
500 output.append(" (Sequence length = ")
501 .append(String.valueOf(s2str.length())).append(")")
502 .append(NEWLINE).append(NEWLINE);
504 ScoreMatrix pam250 = ScoreModels.getInstance().getPam250();
506 for (int j = 0; j < nochunks; j++)
508 // Print the first aligned sequence
509 output.append(new Format("%" + (maxid) + "s").form(s1id)).append(" ");
511 for (int i = 0; i < len; i++)
513 if ((i + (j * len)) < astr1.length())
515 output.append(astr1.charAt(i + (j * len)));
519 output.append(NEWLINE);
520 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
523 * Print out the match symbols:
524 * | for exact match (ignoring case)
525 * . if PAM250 score is positive
528 for (int i = 0; i < len; i++)
530 if ((i + (j * len)) < astr1.length())
532 char c1 = astr1.charAt(i + (j * len));
533 char c2 = astr2.charAt(i + (j * len));
534 boolean sameChar = Comparison.isSameResidue(c1, c2, false);
535 if (sameChar && !Comparison.isGap(c1))
540 else if (type.equals("pep"))
542 if (pam250.getPairwiseScore(c1, c2) > 0)
558 // Now print the second aligned sequence
559 output = output.append(NEWLINE);
560 output = output.append(new Format("%" + (maxid) + "s").form(s2id))
563 for (int i = 0; i < len; i++)
565 if ((i + (j * len)) < astr2.length())
567 output.append(astr2.charAt(i + (j * len)));
571 output.append(NEWLINE).append(NEWLINE);
574 pid = pid / (aseq1.length - count) * 100;
575 output = output.append(new Format("Percentage ID = %2.2f\n").form(pid));
578 os.print(output.toString());
579 } catch (Exception ex)
592 * @return DOCUMENT ME!
594 public int findTrace(int i, int j)
597 float max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
604 else if (F[i][j] == max)
618 else if (E[i][j] == max)
635 public void calcScoreMatrix()
640 // top left hand element
641 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
642 E[0][0] = -gapExtend;
645 // Calculate the top row first
646 for (int j = 1; j < m; j++)
648 // What should these values be? 0 maybe
649 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
650 F[0][j] = -gapExtend;
652 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
657 // Now do the left hand column
658 for (int i = 1; i < n; i++)
661 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
663 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
664 traceback[i][0] = -1;
667 // Now do all the other rows
668 for (int i = 1; i < n; i++)
670 for (int j = 1; j < m; j++)
672 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
673 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
675 score[i][j] = max(score[i - 1][j - 1]
676 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
677 traceback[i][j] = findTrace(i, j);
683 * Returns the given sequence with all of the given gap characters removed.
686 * a string of characters to be treated as gaps
692 public static String extractGaps(String gapChars, String seq)
694 if (gapChars == null || seq == null)
698 StringTokenizer str = new StringTokenizer(seq, gapChars);
699 StringBuilder newString = new StringBuilder(seq.length());
701 while (str.hasMoreTokens())
703 newString.append(str.nextToken());
706 return newString.toString();
719 * @return DOCUMENT ME!
721 private static float max(float f1, float f2, float f3)
746 * @return DOCUMENT ME!
748 private static float max(float f1, float f2)
761 * Converts the character string to an array of integers which are the
762 * corresponding indices to the characters in the score matrix
768 int[] indexEncode(String s)
770 int[] encoded = new int[s.length()];
772 for (int i = 0; i < s.length(); i++)
774 char c = s.charAt(i);
775 encoded[i] = scoreModel.getMatrixIndex(c);
795 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
798 // TODO method dosen't seem to be referenced anywhere delete??
802 for (int i = 0; i < n; i++)
804 for (int j = 0; j < m; j++)
806 if (mat[i][j] >= max)
811 if (mat[i][j] <= min)
818 System.out.println(max + " " + min);
820 for (int i = 0; i < n; i++)
822 for (int j = 0; j < m; j++)
827 // System.out.println(mat[i][j]);
828 float score = (float) (mat[i][j] - min) / (float) (max - min);
829 g.setColor(new Color(score, 0, 0));
830 g.fillRect(x, y, psize, psize);
832 // System.out.println(x + " " + y + " " + score);
838 * Compute a globally optimal needleman and wunsch alignment between two
844 * AlignSeq.DNA or AlignSeq.PEP
846 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
849 AlignSeq as = new AlignSeq(s1, s2, type);
851 as.calcScoreMatrix();
858 * @return mapping from positions in S1 to corresponding positions in S2
860 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
862 ArrayList<Integer> as1 = new ArrayList<Integer>(), as2 = new ArrayList<Integer>();
863 int pdbpos = s2.getStart() + getSeq2Start() - 2;
864 int alignpos = s1.getStart() + getSeq1Start() - 2;
865 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
866 boolean lastmatch = false;
867 // and now trace the alignment onto the atom set.
868 for (int i = 0; i < astr1.length(); i++)
870 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
881 if (allowmismatch || c1 == c2)
883 // extend mapping interval
884 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)
886 as1.add(Integer.valueOf(alignpos));
887 as2.add(Integer.valueOf(pdbpos));
895 // extend mapping interval
898 as1.add(Integer.valueOf(lp1));
899 as2.add(Integer.valueOf(lp2));
904 // construct range pairs
906 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)], mapseq2 = new int[as2
907 .size() + (lastmatch ? 1 : 0)];
909 for (Integer ip : as1)
915 for (Integer ip : as2)
922 mapseq1[mapseq1.length - 1] = alignpos;
923 mapseq2[mapseq2.length - 1] = pdbpos;
925 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
927 jalview.datamodel.Mapping mapping = new Mapping(map);
933 * matches ochains against al and populates seqs with the best match between
934 * each ochain and the set in al
938 * @param dnaOrProtein
939 * @param removeOldAnnots
940 * when true, old annotation is cleared before new annotation
942 * @return List<List<SequenceI> originals, List<SequenceI> replacement,
943 * List<AlignSeq> alignment between each>
945 public static List<List<? extends Object>> replaceMatchingSeqsWith(
946 List<SequenceI> seqs, List<AlignmentAnnotation> annotations,
947 List<SequenceI> ochains, AlignmentI al, String dnaOrProtein,
948 boolean removeOldAnnots)
950 List<SequenceI> orig = new ArrayList<SequenceI>(), repl = new ArrayList<SequenceI>();
951 List<AlignSeq> aligs = new ArrayList<AlignSeq>();
952 if (al != null && al.getHeight() > 0)
954 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
955 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
957 for (SequenceI sq : ochains)
959 SequenceI bestm = null;
960 AlignSeq bestaseq = null;
962 for (SequenceI msq : al.getSequences())
964 AlignSeq aseq = doGlobalNWAlignment(msq, sq, dnaOrProtein);
965 if (bestm == null || aseq.getMaxScore() > bestscore)
967 bestscore = aseq.getMaxScore();
972 System.out.println("Best Score for " + (matches.size() + 1) + " :"
975 aligns.add(bestaseq);
976 al.deleteSequence(bestm);
978 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
980 SequenceI sq, sp = seqs.get(p);
982 if ((q = ochains.indexOf(sp)) > -1)
984 seqs.set(p, sq = matches.get(q));
987 sq.setName(sp.getName());
988 sq.setDescription(sp.getDescription());
990 sq.transferAnnotation(sp,
991 sp2sq = aligns.get(q).getMappingFromS1(false));
992 aligs.add(aligns.get(q));
994 for (int ap = 0; ap < annotations.size();)
996 if (annotations.get(ap).sequenceRef == sp)
1002 if (removeOldAnnots)
1004 annotations.remove(ap);
1008 AlignmentAnnotation alan = annotations.remove(ap);
1009 alan.liftOver(sq, sp2sq);
1010 alan.setSequenceRef(sq);
1011 sq.addAlignmentAnnotation(alan);
1019 if (sq.getAnnotation() != null && sq.getAnnotation().length > 0)
1021 annotations.addAll(inspos == -1 ? annotations.size() : inspos,
1022 Arrays.asList(sq.getAnnotation()));
1027 return Arrays.asList(orig, repl, aligs);
1031 * compute the PID vector used by the redundancy filter.
1033 * @param originalSequences
1034 * - sequences in alignment that are to filtered
1036 * - null or strings to be analysed (typically, visible portion of
1037 * each sequence in alignment)
1039 * - first column in window for calculation
1041 * - last column in window for calculation
1043 * - if true then use ungapped sequence to compute PID
1044 * @return vector containing maximum PID for i-th sequence and any sequences
1045 * longer than that seuqence
1047 public static float[] computeRedundancyMatrix(
1048 SequenceI[] originalSequences, String[] omitHidden, int start,
1049 int end, boolean ungapped)
1051 int height = originalSequences.length;
1052 float[] redundancy = new float[height];
1053 int[] lngth = new int[height];
1054 for (int i = 0; i < height; i++)
1060 // long start = System.currentTimeMillis();
1064 for (int i = 0; i < height; i++)
1067 for (int j = 0; j < i; j++)
1074 if (omitHidden == null)
1076 seqi = originalSequences[i].getSequenceAsString(start, end);
1077 seqj = originalSequences[j].getSequenceAsString(start, end);
1081 seqi = omitHidden[i];
1082 seqj = omitHidden[j];
1086 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1087 lngth[i] = ug.length();
1095 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1096 lngth[j] = ug.length();
1102 pid = Comparison.PID(seqi, seqj);
1104 // use real sequence length rather than string length
1105 if (lngth[j] < lngth[i])
1107 redundancy[j] = Math.max(pid, redundancy[j]);
1111 redundancy[i] = Math.max(pid, redundancy[i]);