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 java.util.Locale;
25 import jalview.analysis.scoremodels.PIDModel;
26 import jalview.analysis.scoremodels.ScoreMatrix;
27 import jalview.analysis.scoremodels.ScoreModels;
28 import jalview.analysis.scoremodels.SimilarityParams;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.util.Comparison;
35 import jalview.util.Format;
36 import jalview.util.MapList;
37 import jalview.util.MessageManager;
39 import java.awt.Color;
40 import java.awt.Graphics;
41 import java.io.PrintStream;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.StringTokenizer;
55 private static final int MAX_NAME_LENGTH = 30;
57 private static final int DEFAULT_OPENCOST = 120;
59 private static final int DEFAULT_EXTENDCOST = 20;
61 private int GAP_OPEN_COST=DEFAULT_OPENCOST;
63 private int GAP_EXTEND_COST=DEFAULT_EXTENDCOST;
65 private static final int GAP_INDEX = -1;
67 public static final String PEP = "pep";
69 public static final String DNA = "dna";
71 private static final String NEWLINE = System.lineSeparator();
79 int[][] traceback; // todo is this actually used?
101 public String astr1 = "";
103 public String astr2 = "";
106 public int seq1start;
112 public int seq2start;
118 public float maxscore;
122 StringBuffer output = new StringBuffer();
124 String type; // AlignSeq.PEP or AlignSeq.DNA
126 private ScoreMatrix scoreMatrix;
129 * Creates a new AlignSeq object.
132 * first sequence for alignment
134 * second sequence for alignment
136 * molecule type, either AlignSeq.PEP or AlignSeq.DNA
138 public AlignSeq(SequenceI s1, SequenceI s2, String type)
140 seqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
145 * Creates a new AlignSeq object.
148 * s1 reference sequence for string1
150 * s2 reference sequence for string2
152 * molecule type, either AlignSeq.PEP or AlignSeq.DNA
154 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
155 String string2, String type)
157 seqInit(s1, string1.toUpperCase(Locale.ROOT), s2,
158 string2.toUpperCase(Locale.ROOT), type);
161 public AlignSeq(SequenceI s1, SequenceI s2, String type, int opencost,
165 GAP_OPEN_COST=opencost;
166 GAP_EXTEND_COST=extcost;
169 public AlignSeq(SequenceI s12, String string1, SequenceI s22,
170 String string2, String type2, int defaultOpencost,
171 int defaultExtendcost)
173 this(s12,string1,s22,string2,type2);
174 GAP_OPEN_COST=defaultOpencost;
175 GAP_EXTEND_COST=defaultExtendcost;
181 * @return DOCUMENT ME!
183 public float getMaxScore()
191 * @return DOCUMENT ME!
193 public int getSeq2Start()
201 * @return DOCUMENT ME!
203 public int getSeq2End()
211 * @return DOCUMENT ME!
213 public int getSeq1Start()
221 * @return DOCUMENT ME!
223 public int getSeq1End()
231 * @return DOCUMENT ME!
233 public String getOutput()
235 return output.toString();
241 * @return DOCUMENT ME!
243 public String getAStr1()
251 * @return DOCUMENT ME!
253 public String getAStr2()
261 * @return DOCUMENT ME!
263 public int[] getASeq1()
271 * @return DOCUMENT ME!
273 public int[] getASeq2()
280 * @return aligned instance of Seq 1
282 public SequenceI getAlignedSeq1()
284 SequenceI alSeq1 = new Sequence(s1.getName(), getAStr1());
285 alSeq1.setStart(s1.getStart() + getSeq1Start() - 1);
286 alSeq1.setEnd(s1.getStart() + getSeq1End() - 1);
287 alSeq1.setDatasetSequence(
288 s1.getDatasetSequence() == null ? s1 : s1.getDatasetSequence());
294 * @return aligned instance of Seq 2
296 public SequenceI getAlignedSeq2()
298 SequenceI alSeq2 = new Sequence(s2.getName(), getAStr2());
299 alSeq2.setStart(s2.getStart() + getSeq2Start() - 1);
300 alSeq2.setEnd(s2.getStart() + getSeq2End() - 1);
301 alSeq2.setDatasetSequence(
302 s2.getDatasetSequence() == null ? s2 : s2.getDatasetSequence());
307 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
312 * - string to use for s1
316 * - string to use for s2
320 public void seqInit(SequenceI s1, String string1, SequenceI s2,
321 String string2, String type)
323 seqInit(s1,string1,s2,string2,type,GAP_OPEN_COST,GAP_EXTEND_COST);
325 public void seqInit(SequenceI s1, String string1, SequenceI s2,
326 String string2, String type, int opening,int extension)
328 GAP_OPEN_COST=opening;
329 GAP_EXTEND_COST=extension;
332 setDefaultParams(type);
333 seqInit(string1, string2);
337 * construct score matrix for string1 and string2 (after removing any existing
343 private void seqInit(String string1, String string2)
345 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
346 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
348 if (s1str.length() == 0 || s2str.length() == 0)
351 "ALL GAPS: " + (s1str.length() == 0 ? s1.getName() : " ")
352 + (s2str.length() == 0 ? s2.getName() : ""));
356 score = new float[s1str.length()][s2str.length()];
358 E = new float[s1str.length()][s2str.length()];
360 F = new float[s1str.length()][s2str.length()];
361 traceback = new int[s1str.length()][s2str.length()];
363 seq1 = indexEncode(s1str);
365 seq2 = indexEncode(s2str);
368 private void setDefaultParams(String moleculeType)
370 if (!PEP.equals(moleculeType) && !DNA.equals(moleculeType))
372 output.append("Wrong type = dna or pep only");
373 throw new Error(MessageManager
374 .formatMessage("error.unknown_type_dna_or_pep", new String[]
379 scoreMatrix = ScoreModels.getInstance()
380 .getDefaultModel(PEP.equals(type));
386 public void traceAlignment()
388 // Find the maximum score along the rhs or bottom row
389 float max = -Float.MAX_VALUE;
391 for (int i = 0; i < seq1.length; i++)
393 if (score[i][seq2.length - 1] > max)
395 max = score[i][seq2.length - 1];
397 maxj = seq2.length - 1;
401 for (int j = 0; j < seq2.length; j++)
403 if (score[seq1.length - 1][j] > max)
405 max = score[seq1.length - 1][j];
406 maxi = seq1.length - 1;
414 maxscore = score[i][j] / 10f;
419 aseq1 = new int[seq1.length + seq2.length];
420 aseq2 = new int[seq1.length + seq2.length];
422 StringBuilder sb1 = new StringBuilder(aseq1.length);
423 StringBuilder sb2 = new StringBuilder(aseq2.length);
425 count = (seq1.length + seq2.length) - 1;
427 while (i > 0 && j > 0)
429 aseq1[count] = seq1[i];
430 sb1.append(s1str.charAt(i));
431 aseq2[count] = seq2[j];
432 sb2.append(s2str.charAt(j));
434 trace = findTrace(i, j);
444 aseq1[count] = GAP_INDEX;
445 sb1.replace(sb1.length() - 1, sb1.length(), "-");
447 else if (trace == -1)
450 aseq2[count] = GAP_INDEX;
451 sb2.replace(sb2.length() - 1, sb2.length(), "-");
460 if (aseq1[count] != GAP_INDEX)
462 aseq1[count] = seq1[i];
463 sb1.append(s1str.charAt(i));
466 if (aseq2[count] != GAP_INDEX)
468 aseq2[count] = seq2[j];
469 sb2.append(s2str.charAt(j));
473 * we built the character strings backwards, so now
474 * reverse them to convert to sequence strings
476 astr1 = sb1.reverse().toString();
477 astr2 = sb2.reverse().toString();
483 public void printAlignment(PrintStream os)
485 // TODO: Use original sequence characters rather than re-translated
486 // characters in output
487 // Find the biggest id length for formatting purposes
488 String s1id = getAlignedSeq1().getDisplayId(true);
489 String s2id = getAlignedSeq2().getDisplayId(true);
490 int nameLength = Math.max(s1id.length(), s2id.length());
491 if (nameLength > MAX_NAME_LENGTH)
493 int truncateBy = nameLength - MAX_NAME_LENGTH;
494 nameLength = MAX_NAME_LENGTH;
495 // JAL-527 - truncate the sequence ids
496 if (s1id.length() > nameLength)
498 int slashPos = s1id.lastIndexOf('/');
499 s1id = s1id.substring(0, slashPos - truncateBy)
500 + s1id.substring(slashPos);
502 if (s2id.length() > nameLength)
504 int slashPos = s2id.lastIndexOf('/');
505 s2id = s2id.substring(0, slashPos - truncateBy)
506 + s2id.substring(slashPos);
509 int len = 72 - nameLength - 1;
510 int nochunks = ((aseq1.length - count) / len)
511 + ((aseq1.length - count) % len > 0 ? 1 : 0);
514 output.append("Score = ").append(score[maxi][maxj]).append(NEWLINE);
515 output.append("Length of alignment = ")
516 .append(String.valueOf(aseq1.length - count)).append(NEWLINE);
517 output.append("Sequence ");
518 Format nameFormat = new Format("%" + nameLength + "s");
519 output.append(nameFormat.form(s1id));
520 output.append(" (Sequence length = ")
521 .append(String.valueOf(s1str.length())).append(")")
523 output.append("Sequence ");
524 output.append(nameFormat.form(s2id));
525 output.append(" (Sequence length = ")
526 .append(String.valueOf(s2str.length())).append(")")
527 .append(NEWLINE).append(NEWLINE);
529 ScoreMatrix pam250 = ScoreModels.getInstance().getPam250();
531 for (int j = 0; j < nochunks; j++)
533 // Print the first aligned sequence
534 output.append(nameFormat.form(s1id)).append(" ");
536 for (int i = 0; i < len; i++)
538 if ((i + (j * len)) < astr1.length())
540 output.append(astr1.charAt(i + (j * len)));
544 output.append(NEWLINE);
545 output.append(nameFormat.form(" ")).append(" ");
548 * Print out the match symbols:
549 * | for exact match (ignoring case)
550 * . if PAM250 score is positive
553 for (int i = 0; i < len; i++)
555 if ((i + (j * len)) < astr1.length())
557 char c1 = astr1.charAt(i + (j * len));
558 char c2 = astr2.charAt(i + (j * len));
559 boolean sameChar = Comparison.isSameResidue(c1, c2, false);
560 if (sameChar && !Comparison.isGap(c1))
565 else if (PEP.equals(type))
567 if (pam250.getPairwiseScore(c1, c2) > 0)
583 // Now print the second aligned sequence
584 output = output.append(NEWLINE);
585 output = output.append(nameFormat.form(s2id)).append(" ");
587 for (int i = 0; i < len; i++)
589 if ((i + (j * len)) < astr2.length())
591 output.append(astr2.charAt(i + (j * len)));
595 output.append(NEWLINE).append(NEWLINE);
598 pid = pid / (aseq1.length - count) * 100;
599 output.append(new Format("Percentage ID = %3.2f\n").form(pid));
600 output.append(NEWLINE);
603 os.print(output.toString());
604 } catch (Exception ex)
617 * @return DOCUMENT ME!
619 public int findTrace(int i, int j)
622 float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
624 float max = score[i - 1][j - 1] + (pairwiseScore * 10);
631 else if (F[i][j] == max)
645 else if (E[i][j] == max)
662 public void calcScoreMatrix()
666 final int GAP_EX_COST=GAP_EXTEND_COST;
667 final int GAP_OP_COST = GAP_OPEN_COST;
668 // top left hand element
669 score[0][0] = scoreMatrix.getPairwiseScore(s1str.charAt(0),
670 s2str.charAt(0)) * 10;
671 E[0][0] = -GAP_EX_COST;
674 // Calculate the top row first
675 for (int j = 1; j < m; j++)
677 // What should these values be? 0 maybe
678 E[0][j] = max(score[0][j - 1] - GAP_OP_COST,
679 E[0][j - 1] - GAP_EX_COST);
680 F[0][j] = -GAP_EX_COST;
682 float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(0),
684 score[0][j] = max(pairwiseScore * 10, -GAP_OP_COST,
690 // Now do the left hand column
691 for (int i = 1; i < n; i++)
693 E[i][0] = -GAP_OP_COST;
694 F[i][0] = max(score[i - 1][0] - GAP_OP_COST,
695 F[i - 1][0] - GAP_EX_COST);
697 float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
699 score[i][0] = max(pairwiseScore * 10, E[i][0], F[i][0]);
700 traceback[i][0] = -1;
703 // Now do all the other rows
704 for (int i = 1; i < n; i++)
706 for (int j = 1; j < m; j++)
708 E[i][j] = max(score[i][j - 1] - GAP_OP_COST,
709 E[i][j - 1] - GAP_EX_COST);
710 F[i][j] = max(score[i - 1][j] - GAP_OP_COST,
711 F[i - 1][j] - GAP_EX_COST);
713 float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
715 score[i][j] = max(score[i - 1][j - 1] + (pairwiseScore * 10),
717 traceback[i][j] = findTrace(i, j);
723 * Returns the given sequence with all of the given gap characters removed.
726 * a string of characters to be treated as gaps
732 public static String extractGaps(String gapChars, String seq)
734 if (gapChars == null || seq == null)
738 StringTokenizer str = new StringTokenizer(seq, gapChars);
739 StringBuilder newString = new StringBuilder(seq.length());
741 while (str.hasMoreTokens())
743 newString.append(str.nextToken());
746 return newString.toString();
759 * @return DOCUMENT ME!
761 private static float max(float f1, float f2, float f3)
786 * @return DOCUMENT ME!
788 private static float max(float f1, float f2)
801 * Converts the character string to an array of integers which are the
802 * corresponding indices to the characters in the score matrix
808 int[] indexEncode(String s)
810 int[] encoded = new int[s.length()];
812 for (int i = 0; i < s.length(); i++)
814 char c = s.charAt(i);
815 encoded[i] = scoreMatrix.getMatrixIndex(c);
835 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
838 // TODO method doesn't seem to be referenced anywhere delete??
842 for (int i = 0; i < n; i++)
844 for (int j = 0; j < m; j++)
846 if (mat[i][j] >= max)
851 if (mat[i][j] <= min)
858 System.out.println(max + " " + min);
860 for (int i = 0; i < n; i++)
862 for (int j = 0; j < m; j++)
867 // System.out.println(mat[i][j]);
868 float score = (float) (mat[i][j] - min) / (float) (max - min);
869 g.setColor(new Color(score, 0, 0));
870 g.fillRect(x, y, psize, psize);
872 // System.out.println(x + " " + y + " " + score);
878 * Compute a globally optimal needleman and wunsch alignment between two
884 * AlignSeq.DNA or AlignSeq.PEP
886 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
889 return doGlobalNWAlignment(s1, s2, type, DEFAULT_OPENCOST,DEFAULT_EXTENDCOST);
891 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
892 String type, int opencost,int extcost)
895 AlignSeq as = new AlignSeq(s1, s2, type,opencost,extcost);
897 as.calcScoreMatrix();
904 * @return mapping from positions in S1 to corresponding positions in S2
906 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
908 ArrayList<Integer> as1 = new ArrayList<Integer>(),
909 as2 = new ArrayList<Integer>();
910 int pdbpos = s2.getStart() + getSeq2Start() - 2;
911 int alignpos = s1.getStart() + getSeq1Start() - 2;
912 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
913 boolean lastmatch = false;
914 // and now trace the alignment onto the atom set.
915 for (int i = 0; i < astr1.length(); i++)
917 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
928 // ignore case differences
929 if (allowmismatch || (c1 == c2) || (Math.abs(c2-c1)==('a'-'A')))
931 // extend mapping interval
932 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)
934 as1.add(Integer.valueOf(alignpos));
935 as2.add(Integer.valueOf(pdbpos));
943 // extend mapping interval
946 as1.add(Integer.valueOf(lp1));
947 as2.add(Integer.valueOf(lp2));
952 // construct range pairs
954 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)],
955 mapseq2 = new int[as2.size() + (lastmatch ? 1 : 0)];
957 for (Integer ip : as1)
963 for (Integer ip : as2)
970 mapseq1[mapseq1.length - 1] = alignpos;
971 mapseq2[mapseq2.length - 1] = pdbpos;
973 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
975 jalview.datamodel.Mapping mapping = new Mapping(map);
981 * matches ochains against al and populates seqs with the best match between
982 * each ochain and the set in al
986 * @param dnaOrProtein
987 * @param removeOldAnnots
988 * when true, old annotation is cleared before new annotation
990 * @return List<List<SequenceI> originals, List<SequenceI> replacement,
991 * List<AlignSeq> alignment between each>
993 public static List<List<? extends Object>> replaceMatchingSeqsWith(
994 List<SequenceI> seqs, List<AlignmentAnnotation> annotations,
995 List<SequenceI> ochains, AlignmentI al, String dnaOrProtein,
996 boolean removeOldAnnots)
998 List<SequenceI> orig = new ArrayList<SequenceI>(),
999 repl = new ArrayList<SequenceI>();
1000 List<AlignSeq> aligs = new ArrayList<AlignSeq>();
1001 if (al != null && al.getHeight() > 0)
1003 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
1004 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
1006 for (SequenceI sq : ochains)
1008 SequenceI bestm = null;
1009 AlignSeq bestaseq = null;
1010 float bestscore = 0;
1011 for (SequenceI msq : al.getSequences())
1013 AlignSeq aseq = doGlobalNWAlignment(msq, sq, dnaOrProtein);
1014 if (bestm == null || aseq.getMaxScore() > bestscore)
1016 bestscore = aseq.getMaxScore();
1021 // System.out.println("Best Score for " + (matches.size() + 1) + " :"
1024 aligns.add(bestaseq);
1025 al.deleteSequence(bestm);
1027 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
1029 SequenceI sq, sp = seqs.get(p);
1031 if ((q = ochains.indexOf(sp)) > -1)
1033 seqs.set(p, sq = matches.get(q));
1036 sq.setName(sp.getName());
1037 sq.setDescription(sp.getDescription());
1039 sq.transferAnnotation(sp,
1040 sp2sq = aligns.get(q).getMappingFromS1(false));
1041 aligs.add(aligns.get(q));
1043 for (int ap = 0; ap < annotations.size();)
1045 if (annotations.get(ap).sequenceRef == sp)
1051 if (removeOldAnnots)
1053 annotations.remove(ap);
1057 AlignmentAnnotation alan = annotations.remove(ap);
1058 alan.liftOver(sq, sp2sq);
1059 alan.setSequenceRef(sq);
1060 sq.addAlignmentAnnotation(alan);
1068 if (sq.getAnnotation() != null && sq.getAnnotation().length > 0)
1070 annotations.addAll(inspos == -1 ? annotations.size() : inspos,
1071 Arrays.asList(sq.getAnnotation()));
1076 return Arrays.asList(orig, repl, aligs);
1080 * compute the PID vector used by the redundancy filter.
1082 * @param originalSequences
1083 * - sequences in alignment that are to filtered
1085 * - null or strings to be analysed (typically, visible portion of
1086 * each sequence in alignment)
1088 * - first column in window for calculation
1090 * - last column in window for calculation
1092 * - if true then use ungapped sequence to compute PID
1093 * @return vector containing maximum PID for i-th sequence and any sequences
1094 * longer than that seuqence
1096 public static float[] computeRedundancyMatrix(
1097 SequenceI[] originalSequences, String[] omitHidden, int start,
1098 int end, boolean ungapped)
1100 int height = originalSequences.length;
1101 float[] redundancy = new float[height];
1102 int[] lngth = new int[height];
1103 for (int i = 0; i < height; i++)
1109 // long start = System.currentTimeMillis();
1111 SimilarityParams pidParams = new SimilarityParams(true, true, true,
1115 for (int i = 0; i < height; i++)
1118 for (int j = 0; j < i; j++)
1125 if (omitHidden == null)
1127 seqi = originalSequences[i].getSequenceAsString(start, end);
1128 seqj = originalSequences[j].getSequenceAsString(start, end);
1132 seqi = omitHidden[i];
1133 seqj = omitHidden[j];
1137 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1138 lngth[i] = ug.length();
1146 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1147 lngth[j] = ug.length();
1153 pid = (float) PIDModel.computePID(seqi, seqj, pidParams);
1155 // use real sequence length rather than string length
1156 if (lngth[j] < lngth[i])
1158 redundancy[j] = Math.max(pid, redundancy[j]);
1162 redundancy[i] = Math.max(pid, redundancy[i]);