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.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Mapping;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceI;
28 import jalview.schemes.ResidueProperties;
29 import jalview.schemes.ScoreMatrix;
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();
56 static String[] dna = { "A", "C", "G", "T", "-" };
58 // "C", "T", "A", "G", "-"};
59 static String[] pep = { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I",
60 "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-" };
90 public String astr1 = "";
92 public String astr2 = "";
101 public int seq2start;
119 int[][] lookup = ResidueProperties.getBLOSUM62();
121 String[] intToStr = pep;
125 StringBuffer output = new StringBuffer();
129 private int[] charToInt;
132 * Creates a new AlignSeq object.
141 public AlignSeq(SequenceI s1, SequenceI s2, String type)
143 SeqInit(s1, s1.getSequenceAsString(), s2, s2.getSequenceAsString(),
148 * Creates a new AlignSeq object.
157 public AlignSeq(SequenceI s1, String string1, SequenceI s2,
158 String string2, String type)
160 SeqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
166 * @return DOCUMENT ME!
168 public int getMaxScore()
176 * @return DOCUMENT ME!
178 public int getSeq2Start()
186 * @return DOCUMENT ME!
188 public int getSeq2End()
196 * @return DOCUMENT ME!
198 public int getSeq1Start()
206 * @return DOCUMENT ME!
208 public int getSeq1End()
216 * @return DOCUMENT ME!
218 public String getOutput()
220 return output.toString();
226 * @return DOCUMENT ME!
228 public String getAStr1()
236 * @return DOCUMENT ME!
238 public String getAStr2()
246 * @return DOCUMENT ME!
248 public int[] getASeq1()
256 * @return DOCUMENT ME!
258 public int[] getASeq2()
266 * @return DOCUMENT ME!
268 public SequenceI getS1()
276 * @return DOCUMENT ME!
278 public SequenceI getS2()
285 * @return aligned instance of Seq 1
287 public SequenceI getAlignedSeq1()
289 SequenceI alSeq1 = new Sequence(s1.getName(), getAStr1());
290 alSeq1.setStart(s1.getStart() + getSeq1Start() - 1);
291 alSeq1.setEnd(s1.getStart() + getSeq1End() - 1);
292 alSeq1.setDatasetSequence(s1.getDatasetSequence() == null ? s1 : s1
293 .getDatasetSequence());
299 * @return aligned instance of Seq 2
301 public SequenceI getAlignedSeq2()
303 SequenceI alSeq2 = new Sequence(s2.getName(), getAStr2());
304 alSeq2.setStart(s2.getStart() + getSeq2Start() - 1);
305 alSeq2.setEnd(s2.getStart() + getSeq2End() - 1);
306 alSeq2.setDatasetSequence(s2.getDatasetSequence() == null ? s2 : s2
307 .getDatasetSequence());
312 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
317 * - string to use for s1
321 * - string to use for s2
325 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
326 String string2, String type)
330 setDefaultParams(type);
331 SeqInit(string1, string2);
335 * Construct score matrix for sequences with custom substitution matrix
340 * - string to use for s1
344 * - string to use for s2
346 * - substitution matrix to use for alignment
348 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
349 String string2, ScoreMatrix scoreMatrix)
353 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
354 lookup = scoreMatrix.getMatrix();
358 * construct score matrix for string1 and string2 (after removing any existing
364 private void SeqInit(String string1, String string2)
366 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
367 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
369 if (s1str.length() == 0 || s2str.length() == 0)
371 output.append("ALL GAPS: "
372 + (s1str.length() == 0 ? s1.getName() : " ")
373 + (s2str.length() == 0 ? s2.getName() : ""));
377 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
378 seq1 = new int[s1str.length()];
380 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
381 seq2 = new int[s2str.length()];
383 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
384 score = new int[s1str.length()][s2str.length()];
386 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
387 E = new int[s1str.length()][s2str.length()];
389 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
390 F = new int[s1str.length()][s2str.length()];
391 traceback = new int[s1str.length()][s2str.length()];
393 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
394 seq1 = stringToInt(s1str, type);
396 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
397 seq2 = stringToInt(s2str, type);
399 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
400 // long tstart = System.currentTimeMillis();
401 // calcScoreMatrix();
402 // long tend = System.currentTimeMillis();
403 // System.out.println("Time take to calculate score matrix = " +
404 // (tend-tstart) + " ms");
405 // printScoreMatrix(score);
406 // System.out.println();
407 // printScoreMatrix(traceback);
408 // System.out.println();
409 // printScoreMatrix(E);
410 // System.out.println();
411 // /printScoreMatrix(F);
412 // System.out.println();
413 // tstart = System.currentTimeMillis();
415 // tend = System.currentTimeMillis();
416 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
420 private void setDefaultParams(String type)
424 if (type.equals(AlignSeq.PEP))
426 lookup = ResidueProperties.getDefaultPeptideMatrix();
428 else if (type.equals(AlignSeq.DNA))
430 lookup = ResidueProperties.getDefaultDnaMatrix();
434 private void setType(String type2)
437 if (type.equals(AlignSeq.PEP))
440 charToInt = ResidueProperties.aaIndex;
441 defInt = ResidueProperties.maxProteinIndex;
443 else if (type.equals(AlignSeq.DNA))
446 charToInt = ResidueProperties.nucleotideIndex;
447 defInt = ResidueProperties.maxNucleotideIndex;
451 output.append("Wrong type = dna or pep only");
452 throw new Error(MessageManager.formatMessage(
453 "error.unknown_type_dna_or_pep", new String[] { type2 }));
460 public void traceAlignment()
462 // Find the maximum score along the rhs or bottom row
465 for (int i = 0; i < seq1.length; i++)
467 if (score[i][seq2.length - 1] > max)
469 max = score[i][seq2.length - 1];
471 maxj = seq2.length - 1;
475 for (int j = 0; j < seq2.length; j++)
477 if (score[seq1.length - 1][j] > max)
479 max = score[seq1.length - 1][j];
480 maxi = seq1.length - 1;
485 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
489 maxscore = score[i][j] / 10;
494 aseq1 = new int[seq1.length + seq2.length];
495 aseq2 = new int[seq1.length + seq2.length];
497 count = (seq1.length + seq2.length) - 1;
499 while ((i > 0) && (j > 0))
501 if ((aseq1[count] != defInt) && (i >= 0))
503 aseq1[count] = seq1[i];
504 astr1 = s1str.charAt(i) + astr1;
507 if ((aseq2[count] != defInt) && (j > 0))
509 aseq2[count] = seq2[j];
510 astr2 = s2str.charAt(j) + astr2;
513 trace = findTrace(i, j);
523 aseq1[count] = defInt;
524 astr1 = "-" + astr1.substring(1);
526 else if (trace == -1)
529 aseq2[count] = defInt;
530 astr2 = "-" + astr2.substring(1);
539 if (aseq1[count] != defInt)
541 aseq1[count] = seq1[i];
542 astr1 = s1str.charAt(i) + astr1;
545 if (aseq2[count] != defInt)
547 aseq2[count] = seq2[j];
548 astr2 = s2str.charAt(j) + astr2;
555 public void printAlignment(java.io.PrintStream os)
557 // TODO: Use original sequence characters rather than re-translated
558 // characters in output
559 // Find the biggest id length for formatting purposes
560 String s1id = s1.getName(), s2id = s2.getName();
561 int maxid = s1.getName().length();
562 if (s2.getName().length() > maxid)
564 maxid = s2.getName().length();
569 // JAL-527 - truncate the sequence ids
570 if (s1.getName().length() > maxid)
572 s1id = s1.getName().substring(0, 30);
574 if (s2.getName().length() > maxid)
576 s2id = s2.getName().substring(0, 30);
579 int len = 72 - maxid - 1;
580 int nochunks = ((aseq1.length - count) / len) + 1;
583 output.append("Sequence \u27f7 Structure mapping details: ")
585 output.append(new Format("%" + maxid + "s").form(s1.getName()));
586 output.append(" : ").append(String.valueOf(s1.getStart()))
587 .append(" - ").append(String.valueOf(s1.getEnd()));
588 output.append(" (Sequence length = ")
589 .append(String.valueOf(s1str.length())).append(")")
591 // output.append("Sequence ");
592 output.append(new Format("%" + maxid + "s").form(s2.getName()));
593 output.append(" : ").append(String.valueOf(s2.getStart()))
594 .append(" - ").append(String.valueOf(s2.getEnd()));
595 output.append(" (Sequence length = ")
596 .append(String.valueOf(s2str.length())).append(")")
597 .append(NEWLINE).append(NEWLINE);
600 for (int j = 0; j < nochunks; j++)
602 // Print the first aligned sequence
603 output.append(new Format("%" + (maxid) + "s").form(s1id)).append(" ");
605 for (int i = 0; i < len; i++)
607 if ((i + (j * len)) < astr1.length())
609 output.append(astr1.charAt(i + (j * len)));
613 output.append(NEWLINE);
614 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
616 // Print out the matching chars
617 for (int i = 0; i < len; i++)
619 if ((i + (j * len)) < astr1.length())
621 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
622 && !jalview.util.Comparison.isGap(astr1.charAt(i
628 else if (type.equals("pep"))
630 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
631 astr2.charAt(i + (j * len))) > 0)
647 // Now print the second aligned sequence
648 output = output.append(NEWLINE);
649 output = output.append(new Format("%" + (maxid) + "s").form(s2id))
652 for (int i = 0; i < len; i++)
654 if ((i + (j * len)) < astr2.length())
656 output.append(astr2.charAt(i + (j * len)));
660 output.append(NEWLINE).append(NEWLINE);
663 output.append("Length of alignment = ")
664 .append(String.valueOf(aseq1.length - count)).append(NEWLINE);
665 output.append("Score = ").append(score[maxi][maxj]).append(NEWLINE);
667 pid = pid / (aseq1.length - count) * 100;
668 output = output.append(new Format("Percentage ID = %2.2f\n\n")
671 output.append(NEWLINE).append(
672 "Mapping method: Needleman & Wunsch Alignment");
675 os.print(output.toString());
676 } catch (Exception ex)
687 public void printScoreMatrix(int[][] mat)
692 for (int i = 0; i < n; i++)
694 // Print the top sequence
697 Format.print(System.out, "%8s", s2str.substring(0, 1));
699 for (int jj = 1; jj < m; jj++)
701 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
704 System.out.println();
707 for (int j = 0; j < m; j++)
711 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
714 Format.print(System.out, "%3d ", mat[i][j] / 10);
717 System.out.println();
729 * @return DOCUMENT ME!
731 public int findTrace(int i, int j)
734 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
741 else if (F[i][j] == max)
755 else if (E[i][j] == max)
772 public void calcScoreMatrix()
777 // top left hand element
778 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
779 E[0][0] = -gapExtend;
782 // Calculate the top row first
783 for (int j = 1; j < m; j++)
785 // What should these values be? 0 maybe
786 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
787 F[0][j] = -gapExtend;
789 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
794 // Now do the left hand column
795 for (int i = 1; i < n; i++)
798 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
800 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
801 traceback[i][0] = -1;
804 // Now do all the other rows
805 for (int i = 1; i < n; i++)
807 for (int j = 1; j < m; j++)
809 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
810 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
812 score[i][j] = max(score[i - 1][j - 1]
813 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
814 traceback[i][j] = findTrace(i, j);
820 * Returns the given sequence with all of the given gap characters removed.
823 * a string of characters to be treated as gaps
829 public static String extractGaps(String gapChars, String seq)
831 if (gapChars == null || seq == null)
835 StringTokenizer str = new StringTokenizer(seq, gapChars);
836 StringBuilder newString = new StringBuilder(seq.length());
838 while (str.hasMoreTokens())
840 newString.append(str.nextToken());
843 return newString.toString();
856 * @return DOCUMENT ME!
858 public int max(int i1, int i2, int i3)
883 * @return DOCUMENT ME!
885 public int max(int i1, int i2)
905 * @return DOCUMENT ME!
907 public int[] stringToInt(String s, String type)
909 int[] seq1 = new int[s.length()];
911 for (int i = 0; i < s.length(); i++)
913 // String ss = s.substring(i, i + 1).toUpperCase();
914 char c = s.charAt(i);
915 if ('a' <= c && c <= 'z')
923 seq1[i] = charToInt[c]; // set accordingly from setType
924 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
925 // peptides, or 4 for NA.
930 } catch (Exception e)
953 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
956 // TODO method dosen't seem to be referenced anywhere delete??
960 for (int i = 0; i < n; i++)
962 for (int j = 0; j < m; j++)
964 if (mat[i][j] >= max)
969 if (mat[i][j] <= min)
976 System.out.println(max + " " + min);
978 for (int i = 0; i < n; i++)
980 for (int j = 0; j < m; j++)
985 // System.out.println(mat[i][j]);
986 float score = (float) (mat[i][j] - min) / (float) (max - min);
987 g.setColor(new Color(score, 0, 0));
988 g.fillRect(x, y, psize, psize);
990 // System.out.println(x + " " + y + " " + score);
996 * Compute a globally optimal needleman and wunsch alignment between two
1002 * AlignSeq.DNA or AlignSeq.PEP
1004 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
1007 AlignSeq as = new AlignSeq(s1, s2, type);
1009 as.calcScoreMatrix();
1010 as.traceAlignment();
1016 * @return mapping from positions in S1 to corresponding positions in S2
1018 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
1020 ArrayList<Integer> as1 = new ArrayList<Integer>(), as2 = new ArrayList<Integer>();
1021 int pdbpos = s2.getStart() + getSeq2Start() - 2;
1022 int alignpos = s1.getStart() + getSeq1Start() - 2;
1023 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
1024 boolean lastmatch = false;
1025 // and now trace the alignment onto the atom set.
1026 for (int i = 0; i < astr1.length(); i++)
1028 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
1039 if (allowmismatch || c1 == c2)
1041 // extend mapping interval
1042 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)
1044 as1.add(Integer.valueOf(alignpos));
1045 as2.add(Integer.valueOf(pdbpos));
1053 // extend mapping interval
1056 as1.add(Integer.valueOf(lp1));
1057 as2.add(Integer.valueOf(lp2));
1062 // construct range pairs
1064 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)], mapseq2 = new int[as2
1065 .size() + (lastmatch ? 1 : 0)];
1067 for (Integer ip : as1)
1073 for (Integer ip : as2)
1080 mapseq1[mapseq1.length - 1] = alignpos;
1081 mapseq2[mapseq2.length - 1] = pdbpos;
1083 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
1085 jalview.datamodel.Mapping mapping = new Mapping(map);
1091 * matches ochains against al and populates seqs with the best match between
1092 * each ochain and the set in al
1096 * @param dnaOrProtein
1097 * @param removeOldAnnots
1098 * when true, old annotation is cleared before new annotation
1100 * @return List<List<SequenceI> originals, List<SequenceI> replacement,
1101 * List<AlignSeq> alignment between each>
1103 public static List<List<? extends Object>> replaceMatchingSeqsWith(
1104 List<SequenceI> seqs, List<AlignmentAnnotation> annotations,
1105 List<SequenceI> ochains, AlignmentI al, String dnaOrProtein,
1106 boolean removeOldAnnots)
1108 List<SequenceI> orig = new ArrayList<SequenceI>(), repl = new ArrayList<SequenceI>();
1109 List<AlignSeq> aligs = new ArrayList<AlignSeq>();
1110 if (al != null && al.getHeight() > 0)
1112 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
1113 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
1115 for (SequenceI sq : ochains)
1117 SequenceI bestm = null;
1118 AlignSeq bestaseq = null;
1120 for (SequenceI msq : al.getSequences())
1122 AlignSeq aseq = doGlobalNWAlignment(msq, sq, dnaOrProtein);
1123 if (bestm == null || aseq.getMaxScore() > bestscore)
1125 bestscore = aseq.getMaxScore();
1130 System.out.println("Best Score for " + (matches.size() + 1) + " :"
1133 aligns.add(bestaseq);
1134 al.deleteSequence(bestm);
1136 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
1138 SequenceI sq, sp = seqs.get(p);
1140 if ((q = ochains.indexOf(sp)) > -1)
1142 seqs.set(p, sq = matches.get(q));
1145 sq.setName(sp.getName());
1146 sq.setDescription(sp.getDescription());
1148 sq.transferAnnotation(sp,
1149 sp2sq = aligns.get(q).getMappingFromS1(false));
1150 aligs.add(aligns.get(q));
1152 for (int ap = 0; ap < annotations.size();)
1154 if (annotations.get(ap).sequenceRef == sp)
1160 if (removeOldAnnots)
1162 annotations.remove(ap);
1166 AlignmentAnnotation alan = annotations.remove(ap);
1167 alan.liftOver(sq, sp2sq);
1168 alan.setSequenceRef(sq);
1169 sq.addAlignmentAnnotation(alan);
1177 if (sq.getAnnotation() != null && sq.getAnnotation().length > 0)
1179 annotations.addAll(inspos == -1 ? annotations.size() : inspos,
1180 Arrays.asList(sq.getAnnotation()));
1185 return Arrays.asList(orig, repl, aligs);
1189 * compute the PID vector used by the redundancy filter.
1191 * @param originalSequences
1192 * - sequences in alignment that are to filtered
1194 * - null or strings to be analysed (typically, visible portion of
1195 * each sequence in alignment)
1197 * - first column in window for calculation
1199 * - last column in window for calculation
1201 * - if true then use ungapped sequence to compute PID
1202 * @return vector containing maximum PID for i-th sequence and any sequences
1203 * longer than that seuqence
1205 public static float[] computeRedundancyMatrix(
1206 SequenceI[] originalSequences, String[] omitHidden, int start,
1207 int end, boolean ungapped)
1209 int height = originalSequences.length;
1210 float[] redundancy = new float[height];
1211 int[] lngth = new int[height];
1212 for (int i = 0; i < height; i++)
1218 // long start = System.currentTimeMillis();
1222 for (int i = 0; i < height; i++)
1225 for (int j = 0; j < i; j++)
1232 if (omitHidden == null)
1234 seqi = originalSequences[i].getSequenceAsString(start, end);
1235 seqj = originalSequences[j].getSequenceAsString(start, end);
1239 seqi = omitHidden[i];
1240 seqj = omitHidden[j];
1244 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1245 lngth[i] = ug.length();
1253 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1254 lngth[j] = ug.length();
1260 pid = Comparison.PID(seqi, seqj);
1262 // use real sequence length rather than string length
1263 if (lngth[j] < lngth[i])
1265 redundancy[j] = Math.max(pid, redundancy[j]);
1269 redundancy[i] = Math.max(pid, redundancy[i]);