2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.analysis;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.io.NewickFile;
\r
25 import jalview.schemes.ResidueProperties;
\r
27 import jalview.util.*;
\r
36 * @version $Revision$
\r
41 SequenceI[] sequence;
\r
43 //SequenceData is a string representation of what the user
\r
44 //sees. The display may contain hidden columns.
\r
45 public AlignmentView seqData=null;
\r
55 Vector groups = new Vector();
\r
56 SequenceNode maxdist;
\r
64 Object found = null;
\r
65 Object leaves = null;
\r
67 boolean hasDistances = true; // normal case for jalview trees
\r
68 boolean hasBootstrap = false; // normal case for jalview trees
\r
70 private boolean hasRootDistance = true;
\r
73 * Create a new NJTree object with leaves associated with sequences in seqs,
\r
74 * and original alignment data represented by Cigar strings.
\r
75 * @param seqs SequenceI[]
\r
76 * @param odata Cigar[]
\r
77 * @param treefile NewickFile
\r
79 public NJTree(SequenceI[] seqs, AlignmentView odata, NewickFile treefile) {
\r
80 this(seqs, treefile);
\r
84 sequenceString = new String[odata.length];
\r
85 char gapChar = jalview.util.Comparison.GapChars.charAt(0);
\r
86 for (int i = 0; i < odata.length; i++)
\r
88 SequenceI oseq_aligned = odata[i].getSeq(gapChar);
\r
89 sequenceString[i] = oseq_aligned.getSequence();
\r
94 * Creates a new NJTree object from a tree from an external source
\r
96 * @param seqs SequenceI which should be associated with leafs of treefile
\r
97 * @param treefile A parsed tree
\r
99 public NJTree(SequenceI[] seqs, NewickFile treefile)
\r
101 this.sequence = seqs;
\r
102 top = treefile.getTree();
\r
105 * There is no dependent alignment to be recovered from an
\r
108 if (sequenceString == null)
\r
110 sequenceString = new String[seqs.length];
\r
111 for (int i = 0; i < seqs.length; i++)
\r
113 sequenceString[i] = seqs[i].getSequence();
\r
118 hasDistances = treefile.HasDistances();
\r
119 hasBootstrap = treefile.HasBootstrap();
\r
120 hasRootDistance = treefile.HasRootDistance();
\r
122 maxheight = findHeight(top);
\r
124 SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
\r
126 Vector leaves = new Vector();
\r
127 findLeaves(top, leaves);
\r
130 int namesleft = seqs.length;
\r
136 while (i < leaves.size())
\r
138 j = (SequenceNode) leaves.elementAt(i++);
\r
139 realnam = j.getName();
\r
142 if (namesleft > -1)
\r
144 nam = algnIds.findIdMatch(realnam);
\r
154 j.setElement(new Sequence(realnam, "THISISAPLACEHLDER"));
\r
155 j.setPlaceholder(true);
\r
161 * Creates a new NJTree object.
\r
163 * @param sequence DOCUMENT ME!
\r
164 * @param type DOCUMENT ME!
\r
165 * @param pwtype DOCUMENT ME!
\r
166 * @param start DOCUMENT ME!
\r
167 * @param end DOCUMENT ME!
\r
169 public NJTree(SequenceI[] sequence,
\r
170 AlignmentView seqData,
\r
173 int start, int end)
\r
175 this.sequence = sequence;
\r
176 this.node = new Vector();
\r
178 this.pwtype = pwtype;
\r
179 if (seqData!=null) {
\r
180 this.seqData = seqData;
\r
182 SeqCigar[] seqs = new SeqCigar[sequence.length];
\r
183 for(int i=0; i<sequence.length; i++)
\r
185 seqs[i] = new SeqCigar(sequence[i], start, end);
\r
187 CigarArray sdata = new CigarArray(seqs);
\r
188 sdata.addOperation(CigarArray.M, end-start+1);
\r
189 this.seqData = new AlignmentView(sdata);
\r
192 if (!(type.equals("NJ")))
\r
197 if (!(pwtype.equals("PID")))
\r
204 done = new int[sequence.length];
\r
206 while ((i < sequence.length) && (sequence[i] != null))
\r
214 distance = findDistances(this.seqData.getSequenceStrings(Comparison.GapChars.charAt(0)));
\r
218 noClus = cluster.size();
\r
226 * @return DOCUMENT ME!
\r
228 public String toString()
\r
230 jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode());
\r
232 return fout.print(false, true); // distances only
\r
237 * used when the alignment associated to a tree has changed.
\r
239 * @param alignment Vector
\r
241 public void UpdatePlaceHolders(Vector alignment)
\r
243 Vector leaves = new Vector();
\r
244 findLeaves(top, leaves);
\r
246 int sz = leaves.size();
\r
247 SequenceIdMatcher seqmatcher = null;
\r
252 SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);
\r
254 if (alignment.contains(leaf.element()))
\r
256 leaf.setPlaceholder(false);
\r
260 if (seqmatcher == null)
\r
262 // Only create this the first time we need it
\r
263 SequenceI[] seqs = new SequenceI[alignment.size()];
\r
265 for (int j = 0; j < seqs.length; j++)
\r
266 seqs[j] = (SequenceI) alignment.elementAt(j);
\r
268 seqmatcher = new SequenceIdMatcher(seqs);
\r
271 SequenceI nam = seqmatcher.findIdMatch(leaf.getName());
\r
275 leaf.setPlaceholder(false);
\r
276 leaf.setElement(nam);
\r
280 leaf.setPlaceholder(true);
\r
289 public void cluster()
\r
293 if (type.equals("NJ"))
\r
295 findMinNJDistance();
\r
302 Cluster c = joinClusters(mini, minj);
\r
306 cluster.setElementAt(null, minj);
\r
307 cluster.setElementAt(c, mini);
\r
312 boolean onefound = false;
\r
317 for (int i = 0; i < noseqs; i++)
\r
321 if (onefound == false)
\r
333 joinClusters(one, two);
\r
334 top = (SequenceNode) (node.elementAt(one));
\r
344 * @param i DOCUMENT ME!
\r
345 * @param j DOCUMENT ME!
\r
347 * @return DOCUMENT ME!
\r
349 public Cluster joinClusters(int i, int j)
\r
351 float dist = distance[i][j];
\r
353 int noi = ((Cluster) cluster.elementAt(i)).value.length;
\r
354 int noj = ((Cluster) cluster.elementAt(j)).value.length;
\r
356 int[] value = new int[noi + noj];
\r
358 for (int ii = 0; ii < noi; ii++)
\r
360 value[ii] = ((Cluster) cluster.elementAt(i)).value[ii];
\r
363 for (int ii = noi; ii < (noi + noj); ii++)
\r
365 value[ii] = ((Cluster) cluster.elementAt(j)).value[ii - noi];
\r
368 Cluster c = new Cluster(value);
\r
373 if (type.equals("NJ"))
\r
375 findClusterNJDistance(i, j);
\r
379 findClusterDistance(i, j);
\r
382 SequenceNode sn = new SequenceNode();
\r
384 sn.setLeft((SequenceNode) (node.elementAt(i)));
\r
385 sn.setRight((SequenceNode) (node.elementAt(j)));
\r
387 SequenceNode tmpi = (SequenceNode) (node.elementAt(i));
\r
388 SequenceNode tmpj = (SequenceNode) (node.elementAt(j));
\r
390 if (type.equals("NJ"))
\r
392 findNewNJDistances(tmpi, tmpj, dist);
\r
396 findNewDistances(tmpi, tmpj, dist);
\r
399 tmpi.setParent(sn);
\r
400 tmpj.setParent(sn);
\r
402 node.setElementAt(sn, i);
\r
410 * @param tmpi DOCUMENT ME!
\r
411 * @param tmpj DOCUMENT ME!
\r
412 * @param dist DOCUMENT ME!
\r
414 public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj,
\r
418 tmpi.dist = ((dist + ri) - rj) / 2;
\r
419 tmpj.dist = (dist - tmpi.dist);
\r
435 * @param tmpi DOCUMENT ME!
\r
436 * @param tmpj DOCUMENT ME!
\r
437 * @param dist DOCUMENT ME!
\r
439 public void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
\r
445 SequenceNode sni = tmpi;
\r
446 SequenceNode snj = tmpj;
\r
448 while (sni != null)
\r
450 ih = ih + sni.dist;
\r
451 sni = (SequenceNode) sni.left();
\r
454 while (snj != null)
\r
456 jh = jh + snj.dist;
\r
457 snj = (SequenceNode) snj.left();
\r
460 tmpi.dist = ((dist / 2) - ih);
\r
461 tmpj.dist = ((dist / 2) - jh);
\r
467 * @param i DOCUMENT ME!
\r
468 * @param j DOCUMENT ME!
\r
470 public void findClusterDistance(int i, int j)
\r
472 int noi = ((Cluster) cluster.elementAt(i)).value.length;
\r
473 int noj = ((Cluster) cluster.elementAt(j)).value.length;
\r
475 // New distances from cluster to others
\r
476 float[] newdist = new float[noseqs];
\r
478 for (int l = 0; l < noseqs; l++)
\r
480 if ((l != i) && (l != j))
\r
482 newdist[l] = ((distance[i][l] * noi) + (distance[j][l] * noj)) / (noi +
\r
491 for (int ii = 0; ii < noseqs; ii++)
\r
493 distance[i][ii] = newdist[ii];
\r
494 distance[ii][i] = newdist[ii];
\r
501 * @param i DOCUMENT ME!
\r
502 * @param j DOCUMENT ME!
\r
504 public void findClusterNJDistance(int i, int j)
\r
507 // New distances from cluster to others
\r
508 float[] newdist = new float[noseqs];
\r
510 for (int l = 0; l < noseqs; l++)
\r
512 if ((l != i) && (l != j))
\r
514 newdist[l] = ((distance[i][l] + distance[j][l]) -
\r
515 distance[i][j]) / 2;
\r
523 for (int ii = 0; ii < noseqs; ii++)
\r
525 distance[i][ii] = newdist[ii];
\r
526 distance[ii][i] = newdist[ii];
\r
533 * @param i DOCUMENT ME!
\r
534 * @param j DOCUMENT ME!
\r
536 * @return DOCUMENT ME!
\r
538 public float findr(int i, int j)
\r
542 for (int k = 0; k < noseqs; k++)
\r
544 if ((k != i) && (k != j) && (done[k] != 1))
\r
546 tmp = tmp + distance[i][k];
\r
552 tmp = tmp / (noClus - 2);
\r
561 * @return DOCUMENT ME!
\r
563 public float findMinNJDistance()
\r
565 float min = 100000;
\r
567 for (int i = 0; i < (noseqs - 1); i++)
\r
569 for (int j = i + 1; j < noseqs; j++)
\r
571 if ((done[i] != 1) && (done[j] != 1))
\r
573 float tmp = distance[i][j] - (findr(i, j) + findr(j, i));
\r
592 * @return DOCUMENT ME!
\r
594 public float findMinDistance()
\r
596 float min = 100000;
\r
598 for (int i = 0; i < (noseqs - 1); i++)
\r
600 for (int j = i + 1; j < noseqs; j++)
\r
602 if ((done[i] != 1) && (done[j] != 1))
\r
604 if (distance[i][j] < min)
\r
609 min = distance[i][j];
\r
621 * @return DOCUMENT ME!
\r
623 public float[][] findDistances(String[] sequenceString)
\r
625 float[][] distance = new float[noseqs][noseqs];
\r
627 if (pwtype.equals("PID"))
\r
629 for (int i = 0; i < (noseqs - 1); i++)
\r
631 for (int j = i; j < noseqs; j++)
\r
635 distance[i][i] = 0;
\r
639 distance[i][j] = 100 -
\r
640 Comparison.PID(sequenceString[i], sequenceString[j]);
\r
642 distance[j][i] = distance[i][j];
\r
647 else if (pwtype.equals("BL"))
\r
650 int end = sequenceString[0].length();
\r
651 for (int i = 0; i < (noseqs - 1); i++)
\r
653 for (int j = i; j < noseqs; j++)
\r
657 for (int k = 0; k < end; k++)
\r
661 score += ResidueProperties.getBLOSUM62(
\r
662 sequenceString[i].substring(k, k + 1),
\r
663 sequenceString[j].substring(k, k + 1));
\r
665 catch (Exception ex)
\r
667 System.err.println("err creating BLOSUM62 tree");
\r
668 ex.printStackTrace();
\r
672 distance[i][j] = (float) score;
\r
674 if (score > maxscore)
\r
681 for (int i = 0; i < (noseqs - 1); i++)
\r
683 for (int j = i; j < noseqs; j++)
\r
685 distance[i][j] = (float) maxscore - distance[i][j];
\r
686 distance[j][i] = distance[i][j];
\r
690 /* else if (pwtype.equals("SW"))
\r
694 for (int i = 0; i < (noseqs - 1); i++)
\r
696 for (int j = i; j < noseqs; j++)
\r
698 AlignSeq as = new AlignSeq(sequence[i], sequence[j], "pep");
\r
699 as.calcScoreMatrix();
\r
700 as.traceAlignment();
\r
701 as.printAlignment(System.out);
\r
702 distance[i][j] = (float) as.maxscore;
\r
704 if (max < distance[i][j])
\r
706 max = distance[i][j];
\r
711 for (int i = 0; i < (noseqs - 1); i++)
\r
713 for (int j = i; j < noseqs; j++)
\r
715 distance[i][j] = max - distance[i][j];
\r
716 distance[j][i] = distance[i][j];
\r
727 public void makeLeaves()
\r
729 cluster = new Vector();
\r
731 for (int i = 0; i < noseqs; i++)
\r
733 SequenceNode sn = new SequenceNode();
\r
735 sn.setElement(sequence[i]);
\r
736 sn.setName(sequence[i].getName());
\r
737 node.addElement(sn);
\r
739 int[] value = new int[1];
\r
742 Cluster c = new Cluster(value);
\r
743 cluster.addElement(c);
\r
750 * @param node DOCUMENT ME!
\r
751 * @param leaves DOCUMENT ME!
\r
753 * @return DOCUMENT ME!
\r
755 public Vector findLeaves(SequenceNode node, Vector leaves)
\r
762 if ((node.left() == null) && (node.right() == null))
\r
764 leaves.addElement(node);
\r
770 findLeaves((SequenceNode) node.left(), leaves);
\r
771 findLeaves((SequenceNode) node.right(), leaves);
\r
780 * @param node DOCUMENT ME!
\r
781 * @param count DOCUMENT ME!
\r
783 * @return DOCUMENT ME!
\r
785 public Object findLeaf(SequenceNode node, int count)
\r
787 found = _findLeaf(node, count);
\r
795 * @param node DOCUMENT ME!
\r
796 * @param count DOCUMENT ME!
\r
798 * @return DOCUMENT ME!
\r
800 public Object _findLeaf(SequenceNode node, int count)
\r
807 if (node.ycount == count)
\r
809 found = node.element();
\r
815 _findLeaf((SequenceNode) node.left(), count);
\r
816 _findLeaf((SequenceNode) node.right(), count);
\r
823 * printNode is mainly for debugging purposes.
\r
825 * @param node SequenceNode
\r
827 public void printNode(SequenceNode node)
\r
834 if ((node.left() == null) && (node.right() == null))
\r
836 System.out.println("Leaf = " +
\r
837 ((SequenceI) node.element()).getName());
\r
838 System.out.println("Dist " + ((SequenceNode) node).dist);
\r
839 System.out.println("Boot " + node.getBootstrap());
\r
843 System.out.println("Dist " + ((SequenceNode) node).dist);
\r
844 printNode((SequenceNode) node.left());
\r
845 printNode((SequenceNode) node.right());
\r
852 * @param node DOCUMENT ME!
\r
854 public void findMaxDist(SequenceNode node)
\r
861 if ((node.left() == null) && (node.right() == null))
\r
863 float dist = ((SequenceNode) node).dist;
\r
865 if (dist > maxDistValue)
\r
867 maxdist = (SequenceNode) node;
\r
868 maxDistValue = dist;
\r
873 findMaxDist((SequenceNode) node.left());
\r
874 findMaxDist((SequenceNode) node.right());
\r
881 * @return DOCUMENT ME!
\r
883 public Vector getGroups()
\r
891 * @return DOCUMENT ME!
\r
893 public float getMaxHeight()
\r
901 * @param node DOCUMENT ME!
\r
902 * @param threshold DOCUMENT ME!
\r
904 public void groupNodes(SequenceNode node, float threshold)
\r
911 if ((node.height / maxheight) > threshold)
\r
913 groups.addElement(node);
\r
917 groupNodes((SequenceNode) node.left(), threshold);
\r
918 groupNodes((SequenceNode) node.right(), threshold);
\r
925 * @param node DOCUMENT ME!
\r
927 * @return DOCUMENT ME!
\r
929 public float findHeight(SequenceNode node)
\r
936 if ((node.left() == null) && (node.right() == null))
\r
938 node.height = ((SequenceNode) node.parent()).height + node.dist;
\r
940 if (node.height > maxheight)
\r
942 return node.height;
\r
951 if (node.parent() != null)
\r
953 node.height = ((SequenceNode) node.parent()).height +
\r
959 node.height = (float) 0.0;
\r
962 maxheight = findHeight((SequenceNode) (node.left()));
\r
963 maxheight = findHeight((SequenceNode) (node.right()));
\r
972 * @return DOCUMENT ME!
\r
974 public SequenceNode reRoot()
\r
976 if (maxdist != null)
\r
980 float tmpdist = maxdist.dist;
\r
983 SequenceNode sn = new SequenceNode();
\r
984 sn.setParent(null);
\r
986 // New right hand of top
\r
987 SequenceNode snr = (SequenceNode) maxdist.parent();
\r
988 changeDirection(snr, maxdist);
\r
989 System.out.println("Printing reversed tree");
\r
991 snr.dist = tmpdist / 2;
\r
992 maxdist.dist = tmpdist / 2;
\r
995 maxdist.setParent(sn);
\r
998 sn.setLeft(maxdist);
\r
1011 * @return true if original sequence data can be recovered
\r
1013 public boolean hasOriginalSequenceData() {
\r
1014 return seqData!=null;
\r
1017 * Returns original alignment data used for calculation - or null where
\r
1020 * @return null or cut'n'pasteable alignment
\r
1022 public String printOriginalSequenceData(char gapChar)
\r
1024 if (seqData==null)
\r
1027 StringBuffer sb = new StringBuffer();
\r
1028 String[] seqdatas = seqData.getSequenceStrings(gapChar);
\r
1029 for(int i=0; i<seqdatas.length; i++)
\r
1031 sb.append(new jalview.util.Format("%-" + 15 + "s").form(
\r
1032 sequence[i].getName()));
\r
1033 sb.append(" "+seqdatas[i]+"\n");
\r
1035 return sb.toString();
\r
1040 * @param node DOCUMENT ME!
\r
1042 public void printN(SequenceNode node)
\r
1049 if ((node.left() != null) && (node.right() != null))
\r
1051 printN((SequenceNode) node.left());
\r
1052 printN((SequenceNode) node.right());
\r
1056 System.out.println(" name = " +
\r
1057 ((SequenceI) node.element()).getName());
\r
1060 System.out.println(" dist = " + ((SequenceNode) node).dist + " " +
\r
1061 ((SequenceNode) node).count + " " + ((SequenceNode) node).height);
\r
1067 * @param node DOCUMENT ME!
\r
1069 public void reCount(SequenceNode node)
\r
1078 * @param node DOCUMENT ME!
\r
1080 public void _reCount(SequenceNode node)
\r
1087 if ((node.left() != null) && (node.right() != null))
\r
1089 _reCount((SequenceNode) node.left());
\r
1090 _reCount((SequenceNode) node.right());
\r
1092 SequenceNode l = (SequenceNode) node.left();
\r
1093 SequenceNode r = (SequenceNode) node.right();
\r
1095 ((SequenceNode) node).count = l.count + r.count;
\r
1096 ((SequenceNode) node).ycount = (l.ycount + r.ycount) / 2;
\r
1100 ((SequenceNode) node).count = 1;
\r
1101 ((SequenceNode) node).ycount = ycount++;
\r
1108 * @param node DOCUMENT ME!
\r
1110 public void swapNodes(SequenceNode node)
\r
1117 SequenceNode tmp = (SequenceNode) node.left();
\r
1119 node.setLeft(node.right());
\r
1120 node.setRight(tmp);
\r
1126 * @param node DOCUMENT ME!
\r
1127 * @param dir DOCUMENT ME!
\r
1129 public void changeDirection(SequenceNode node, SequenceNode dir)
\r
1136 if (node.parent() != top)
\r
1138 changeDirection((SequenceNode) node.parent(), node);
\r
1140 SequenceNode tmp = (SequenceNode) node.parent();
\r
1142 if (dir == node.left())
\r
1144 node.setParent(dir);
\r
1145 node.setLeft(tmp);
\r
1147 else if (dir == node.right())
\r
1149 node.setParent(dir);
\r
1150 node.setRight(tmp);
\r
1155 if (dir == node.left())
\r
1157 node.setParent(node.left());
\r
1159 if (top.left() == node)
\r
1161 node.setRight(top.right());
\r
1165 node.setRight(top.left());
\r
1170 node.setParent(node.right());
\r
1172 if (top.left() == node)
\r
1174 node.setLeft(top.right());
\r
1178 node.setLeft(top.left());
\r
1188 * @return DOCUMENT ME!
\r
1190 public SequenceNode getMaxDist()
\r
1198 * @return DOCUMENT ME!
\r
1200 public SequenceNode getTopNode()
\r
1206 * @return true if tree has real distances
\r
1208 public boolean isHasDistances() {
\r
1209 return hasDistances;
\r
1214 * @return true if tree has real bootstrap values
\r
1216 public boolean isHasBootstrap() {
\r
1217 return hasBootstrap;
\r
1220 public boolean isHasRootDistance()
\r
1222 return hasRootDistance;
\r
1231 * @author $author$
\r
1232 * @version $Revision$
\r
1239 * Creates a new Cluster object.
\r
1241 * @param value DOCUMENT ME!
\r
1243 public Cluster(int[] value)
\r
1245 this.value = value;
\r