2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29 import jalview.util.*;
39 public static final String PEP = "pep";
41 public static final String DNA = "dna";
44 { "A", "C", "G", "T", "-" };
46 // "C", "T", "A", "G", "-"};
48 { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
49 "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "-" };
79 public String astr1 = "";
81 public String astr2 = "";
108 int[][] lookup = ResidueProperties.getBLOSUM62();
110 String[] intToStr = pep;
114 StringBuffer output = new StringBuffer();
118 private int[] charToInt;
121 * Creates a new AlignSeq object.
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 int 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()
255 * @return DOCUMENT ME!
257 public SequenceI getS1()
265 * @return DOCUMENT ME!
267 public SequenceI getS2()
274 * @return aligned instance of Seq 1
276 public SequenceI getAlignedSeq1()
278 SequenceI alSeq1 = new Sequence(s1.getName(), getAStr1());
279 alSeq1.setStart(s1.getStart() + getSeq1Start() - 1);
280 alSeq1.setEnd(s1.getStart() + getSeq1End() - 1);
281 alSeq1.setDatasetSequence(s1.getDatasetSequence() == null ? s1 : s1
282 .getDatasetSequence());
288 * @return aligned instance of Seq 2
290 public SequenceI getAlignedSeq2()
292 SequenceI alSeq2 = new Sequence(s2.getName(), getAStr2());
293 alSeq2.setStart(s2.getStart() + getSeq2Start() - 1);
294 alSeq2.setEnd(s2.getStart() + getSeq2End() - 1);
295 alSeq2.setDatasetSequence(s2.getDatasetSequence() == null ? s2 : s2
296 .getDatasetSequence());
301 * Construct score matrix for sequences with standard DNA or PEPTIDE matrix
306 * - string to use for s1
310 * - string to use for s2
314 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
315 String string2, String type)
319 setDefaultParams(type);
320 SeqInit(string1, string2);
324 * Construct score matrix for sequences with custom substitution matrix
329 * - string to use for s1
333 * - string to use for s2
335 * - substitution matrix to use for alignment
337 public void SeqInit(SequenceI s1, String string1, SequenceI s2,
338 String string2, ScoreMatrix scoreMatrix)
342 setType(scoreMatrix.isDNA() ? AlignSeq.DNA : AlignSeq.PEP);
343 lookup = scoreMatrix.getMatrix();
347 * construct score matrix for string1 and string2 (after removing any existing
353 private void SeqInit(String string1, String string2)
355 s1str = extractGaps(jalview.util.Comparison.GapChars, string1);
356 s2str = extractGaps(jalview.util.Comparison.GapChars, string2);
358 if (s1str.length() == 0 || s2str.length() == 0)
360 output.append("ALL GAPS: "
361 + (s1str.length() == 0 ? s1.getName() : " ")
362 + (s2str.length() == 0 ? s2.getName() : ""));
366 // System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
367 seq1 = new int[s1str.length()];
369 // System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
370 seq2 = new int[s2str.length()];
372 // System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
373 score = new int[s1str.length()][s2str.length()];
375 // System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
376 E = new int[s1str.length()][s2str.length()];
378 // System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
379 F = new int[s1str.length()][s2str.length()];
380 traceback = new int[s1str.length()][s2str.length()];
382 // System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
383 seq1 = stringToInt(s1str, type);
385 // System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
386 seq2 = stringToInt(s2str, type);
388 // System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
389 // long tstart = System.currentTimeMillis();
390 // calcScoreMatrix();
391 // long tend = System.currentTimeMillis();
392 // System.out.println("Time take to calculate score matrix = " +
393 // (tend-tstart) + " ms");
394 // printScoreMatrix(score);
395 // System.out.println();
396 // printScoreMatrix(traceback);
397 // System.out.println();
398 // printScoreMatrix(E);
399 // System.out.println();
400 // /printScoreMatrix(F);
401 // System.out.println();
402 // tstart = System.currentTimeMillis();
404 // tend = System.currentTimeMillis();
405 // System.out.println("Time take to traceback alignment = " + (tend-tstart)
409 private void setDefaultParams(String type)
413 if (type.equals(AlignSeq.PEP))
415 lookup = ResidueProperties.getDefaultPeptideMatrix();
417 else if (type.equals(AlignSeq.DNA))
419 lookup = ResidueProperties.getDefaultDnaMatrix();
423 private void setType(String type2)
426 if (type.equals(AlignSeq.PEP))
429 charToInt = ResidueProperties.aaIndex;
430 defInt = ResidueProperties.maxProteinIndex;
432 else if (type.equals(AlignSeq.DNA))
435 charToInt = ResidueProperties.nucleotideIndex;
436 defInt = ResidueProperties.maxNucleotideIndex;
440 output.append("Wrong type = dna or pep only");
441 throw new Error(MessageManager.formatMessage("error.unknown_type_dna_or_pep", new String[]{type2}));
448 public void traceAlignment()
450 // Find the maximum score along the rhs or bottom row
453 for (int i = 0; i < seq1.length; i++)
455 if (score[i][seq2.length - 1] > max)
457 max = score[i][seq2.length - 1];
459 maxj = seq2.length - 1;
463 for (int j = 0; j < seq2.length; j++)
465 if (score[seq1.length - 1][j] > max)
467 max = score[seq1.length - 1][j];
468 maxi = seq1.length - 1;
473 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
477 maxscore = score[i][j] / 10;
482 aseq1 = new int[seq1.length + seq2.length];
483 aseq2 = new int[seq1.length + seq2.length];
485 count = (seq1.length + seq2.length) - 1;
487 while ((i > 0) && (j > 0))
489 if ((aseq1[count] != defInt) && (i >= 0))
491 aseq1[count] = seq1[i];
492 astr1 = s1str.charAt(i) + astr1;
495 if ((aseq2[count] != defInt) && (j > 0))
497 aseq2[count] = seq2[j];
498 astr2 = s2str.charAt(j) + astr2;
501 trace = findTrace(i, j);
511 aseq1[count] = defInt;
512 astr1 = "-" + astr1.substring(1);
514 else if (trace == -1)
517 aseq2[count] = defInt;
518 astr2 = "-" + astr2.substring(1);
527 if (aseq1[count] != defInt)
529 aseq1[count] = seq1[i];
530 astr1 = s1str.charAt(i) + astr1;
533 if (aseq2[count] != defInt)
535 aseq2[count] = seq2[j];
536 astr2 = s2str.charAt(j) + astr2;
543 public void printAlignment(java.io.PrintStream os)
545 // TODO: Use original sequence characters rather than re-translated
546 // characters in output
547 // Find the biggest id length for formatting purposes
548 String s1id = s1.getName(), s2id = s2.getName();
549 int maxid = s1.getName().length();
550 if (s2.getName().length() > maxid)
552 maxid = s2.getName().length();
557 // JAL-527 - truncate the sequence ids
558 if (s1.getName().length() > maxid)
560 s1id = s1.getName().substring(0, 30);
562 if (s2.getName().length() > maxid)
564 s2id = s2.getName().substring(0, 30);
567 int len = 72 - maxid - 1;
568 int nochunks = ((aseq1.length - count) / len) + 1;
571 output.append("Score = " + score[maxi][maxj] + "\n");
572 output.append("Length of alignment = " + (aseq1.length - count) + "\n");
573 output.append("Sequence ");
574 output.append(new Format("%" + maxid + "s").form(s1.getName()));
575 output.append(" : " + s1.getStart() + " - " + s1.getEnd()
576 + " (Sequence length = " + s1str.length() + ")\n");
577 output.append("Sequence ");
578 output.append(new Format("%" + maxid + "s").form(s2.getName()));
579 output.append(" : " + s2.getStart() + " - " + s2.getEnd()
580 + " (Sequence length = " + s2str.length() + ")\n\n");
582 for (int j = 0; j < nochunks; j++)
584 // Print the first aligned sequence
585 output.append(new Format("%" + (maxid) + "s").form(s1id) + " ");
587 for (int i = 0; i < len; i++)
589 if ((i + (j * len)) < astr1.length())
591 output.append(astr1.charAt(i + (j * len)));
596 output.append(new Format("%" + (maxid) + "s").form(" ") + " ");
598 // Print out the matching chars
599 for (int i = 0; i < len; i++)
601 if ((i + (j * len)) < astr1.length())
603 if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
604 && !jalview.util.Comparison.isGap(astr1.charAt(i
610 else if (type.equals("pep"))
612 if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
613 astr2.charAt(i + (j * len))) > 0)
629 // Now print the second aligned sequence
630 output = output.append("\n");
631 output = output.append(new Format("%" + (maxid) + "s").form(s2id)
634 for (int i = 0; i < len; i++)
636 if ((i + (j * len)) < astr2.length())
638 output.append(astr2.charAt(i + (j * len)));
642 output = output.append("\n\n");
645 pid = pid / (aseq1.length - count) * 100;
646 output = output.append(new Format("Percentage ID = %2.2f\n\n")
651 os.print(output.toString());
652 } catch (Exception ex)
663 public void printScoreMatrix(int[][] mat)
668 for (int i = 0; i < n; i++)
670 // Print the top sequence
673 Format.print(System.out, "%8s", s2str.substring(0, 1));
675 for (int jj = 1; jj < m; jj++)
677 Format.print(System.out, "%5s", s2str.substring(jj, jj + 1));
680 System.out.println();
683 for (int j = 0; j < m; j++)
687 Format.print(System.out, "%3s", s1str.substring(i, i + 1));
690 Format.print(System.out, "%3d ", mat[i][j] / 10);
693 System.out.println();
705 * @return DOCUMENT ME!
707 public int findTrace(int i, int j)
710 int max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
717 else if (F[i][j] == max)
731 else if (E[i][j] == max)
748 public void calcScoreMatrix()
753 // top left hand element
754 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
755 E[0][0] = -gapExtend;
758 // Calculate the top row first
759 for (int j = 1; j < m; j++)
761 // What should these values be? 0 maybe
762 E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
763 F[0][j] = -gapExtend;
765 score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
770 // Now do the left hand column
771 for (int i = 1; i < n; i++)
774 F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
776 score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
777 traceback[i][0] = -1;
780 // Now do all the other rows
781 for (int i = 1; i < n; i++)
783 for (int j = 1; j < m; j++)
785 E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
786 F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
788 score[i][j] = max(score[i - 1][j - 1]
789 + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
790 traceback[i][j] = findTrace(i, j);
803 * @return DOCUMENT ME!
805 public static String extractGaps(String gapChar, String seq)
807 StringTokenizer str = new StringTokenizer(seq, gapChar);
808 StringBuffer newString = new StringBuffer();
810 while (str.hasMoreTokens())
812 newString.append(str.nextToken());
815 return newString.toString();
828 * @return DOCUMENT ME!
830 public int max(int i1, int i2, int i3)
855 * @return DOCUMENT ME!
857 public int max(int i1, int i2)
877 * @return DOCUMENT ME!
879 public int[] stringToInt(String s, String type)
881 int[] seq1 = new int[s.length()];
883 for (int i = 0; i < s.length(); i++)
885 // String ss = s.substring(i, i + 1).toUpperCase();
886 char c = s.charAt(i);
887 if ('a' <= c && c <= 'z')
895 seq1[i] = charToInt[c]; // set accordingly from setType
896 if (seq1[i] < 0 || seq1[i] > defInt) // set from setType: 23 for
897 // peptides, or 4 for NA.
902 } catch (Exception e)
925 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
931 for (int i = 0; i < n; i++)
933 for (int j = 0; j < m; j++)
935 if (mat[i][j] >= max)
940 if (mat[i][j] <= min)
947 System.out.println(max + " " + min);
949 for (int i = 0; i < n; i++)
951 for (int j = 0; j < m; j++)
956 // System.out.println(mat[i][j]);
957 float score = (float) (mat[i][j] - min) / (float) (max - min);
958 g.setColor(new Color(score, 0, 0));
959 g.fillRect(x, y, psize, psize);
961 // System.out.println(x + " " + y + " " + score);
967 * Compute a globally optimal needleman and wunsch alignment between two
973 * AlignSeq.DNA or AlignSeq.PEP
975 public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
978 AlignSeq as = new AlignSeq(s1, s2, type);
980 as.calcScoreMatrix();
987 * @return mapping from positions in S1 to corresponding positions in S2
989 public jalview.datamodel.Mapping getMappingFromS1(boolean allowmismatch)
991 ArrayList<Integer> as1 = new ArrayList<Integer>(), as2 = new ArrayList<Integer>();
992 int pdbpos = s2.getStart() + getSeq2Start() - 2;
993 int alignpos = s1.getStart() + getSeq1Start() - 2;
994 int lp2 = pdbpos - 3, lp1 = alignpos - 3;
995 boolean lastmatch = false;
996 // and now trace the alignment onto the atom set.
997 for (int i = 0; i < astr1.length(); i++)
999 char c1 = astr1.charAt(i), c2 = astr2.charAt(i);
1010 if (allowmismatch || c1 == c2)
1013 // extend mapping interval.
1014 if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)
1016 as1.add(Integer.valueOf(alignpos));
1017 as2.add(Integer.valueOf(pdbpos));
1027 // construct range pairs
1028 int[] mapseq1 = new int[as1.size() + (lastmatch ? 1 : 0)], mapseq2 = new int[as2
1029 .size() + (lastmatch ? 1 : 0)];
1031 for (Integer ip : as1)
1037 for (Integer ip : as2)
1044 mapseq1[mapseq1.length - 1] = alignpos;
1045 mapseq2[mapseq2.length - 1] = pdbpos;
1047 MapList map = new MapList(mapseq1, mapseq2, 1, 1);
1049 jalview.datamodel.Mapping mapping = new Mapping(map);
1055 * matches ochains against al and populates seqs with the best match between
1056 * each ochain and the set in al
1060 * @param dnaOrProtein
1061 * @param removeOldAnnots when true, old annotation is cleared before new annotation transferred
1063 public static void replaceMatchingSeqsWith(List<SequenceI> seqs, List<AlignmentAnnotation> annotations, List<SequenceI> ochains,
1064 AlignmentI al, String dnaOrProtein, boolean removeOldAnnots)
1066 if (al != null && al.getHeight() > 0)
1068 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
1069 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
1071 for (SequenceI sq : ochains)
1073 SequenceI bestm = null;
1074 AlignSeq bestaseq = null;
1076 for (SequenceI msq : al.getSequences())
1078 AlignSeq aseq = doGlobalNWAlignment(msq, sq,
1080 if (bestm == null || aseq.getMaxScore() > bestscore)
1082 bestscore = aseq.getMaxScore();
1087 System.out.println("Best Score for " + (matches.size() + 1) + " :"
1090 aligns.add(bestaseq);
1091 al.deleteSequence(bestm);
1093 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
1095 SequenceI sq, sp = seqs.get(p);
1097 if ((q = ochains.indexOf(sp)) > -1)
1099 seqs.set(p, sq = matches.get(q));
1100 sq.setName(sp.getName());
1101 sq.setDescription(sp.getDescription());
1103 sq.transferAnnotation(sp, sp2sq = aligns.get(q).getMappingFromS1(false));
1105 for (int ap = 0; ap < annotations.size();)
1107 if (annotations.get(ap).sequenceRef == sp)
1113 if (removeOldAnnots) {
1114 annotations.remove(ap);
1116 AlignmentAnnotation alan = annotations.remove(ap);
1117 alan.liftOver(sq, sp2sq);
1118 alan.setSequenceRef(sq);
1119 sq.addAlignmentAnnotation(alan);
1127 if (sq.getAnnotation() != null)
1129 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
1137 * compute the PID vector used by the redundancy filter.
1139 * @param originalSequences
1140 * - sequences in alignment that are to filtered
1142 * - null or strings to be analysed (typically, visible portion of
1143 * each sequence in alignment)
1145 * - first column in window for calculation
1147 * - last column in window for calculation
1149 * - if true then use ungapped sequence to compute PID
1150 * @return vector containing maximum PID for i-th sequence and any sequences
1151 * longer than that seuqence
1153 public static float[] computeRedundancyMatrix(
1154 SequenceI[] originalSequences, String[] omitHidden, int start,
1155 int end, boolean ungapped)
1157 int height = originalSequences.length;
1158 float[] redundancy = new float[height];
1159 int[] lngth = new int[height];
1160 for (int i = 0; i < height; i++)
1166 // long start = System.currentTimeMillis();
1170 for (int i = 0; i < height; i++)
1173 for (int j = 0; j < i; j++)
1180 if (omitHidden == null)
1182 seqi = originalSequences[i].getSequenceAsString(start, end);
1183 seqj = originalSequences[j].getSequenceAsString(start, end);
1187 seqi = omitHidden[i];
1188 seqj = omitHidden[j];
1192 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqi);
1193 lngth[i] = ug.length();
1201 String ug = AlignSeq.extractGaps(Comparison.GapChars, seqj);
1202 lngth[j] = ug.length();
1208 pid = Comparison.PID(seqi, seqj);
1210 // use real sequence length rather than string length
1211 if (lngth[j] < lngth[i])
1213 redundancy[j] = Math.max(pid, redundancy[j]);
1217 redundancy[i] = Math.max(pid, redundancy[i]);