2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
21 import jalview.datamodel.*;
23 import jalview.io.NewickFile;
25 import jalview.schemes.ResidueProperties;
27 import jalview.util.*;
43 //SequenceData is a string representation of what the user
44 //sees. The display may contain hidden columns.
45 public AlignmentView seqData=null;
55 Vector groups = new Vector();
67 boolean hasDistances = true; // normal case for jalview trees
68 boolean hasBootstrap = false; // normal case for jalview trees
70 private boolean hasRootDistance = true;
73 * Create a new NJTree object with leaves associated with sequences in seqs,
74 * and original alignment data represented by Cigar strings.
75 * @param seqs SequenceI[]
76 * @param odata Cigar[]
77 * @param treefile NewickFile
79 public NJTree(SequenceI[] seqs, AlignmentView odata, NewickFile treefile) {
84 sequenceString = new String[odata.length];
85 char gapChar = jalview.util.Comparison.GapChars.charAt(0);
86 for (int i = 0; i < odata.length; i++)
88 SequenceI oseq_aligned = odata[i].getSeq(gapChar);
89 sequenceString[i] = oseq_aligned.getSequence();
94 * Creates a new NJTree object from a tree from an external source
96 * @param seqs SequenceI which should be associated with leafs of treefile
97 * @param treefile A parsed tree
99 public NJTree(SequenceI[] seqs, NewickFile treefile)
101 this.sequence = seqs;
102 top = treefile.getTree();
105 * There is no dependent alignment to be recovered from an
108 if (sequenceString == null)
110 sequenceString = new String[seqs.length];
111 for (int i = 0; i < seqs.length; i++)
113 sequenceString[i] = seqs[i].getSequence();
118 hasDistances = treefile.HasDistances();
119 hasBootstrap = treefile.HasBootstrap();
120 hasRootDistance = treefile.HasRootDistance();
122 maxheight = findHeight(top);
124 SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
126 Vector leaves = new Vector();
127 findLeaves(top, leaves);
130 int namesleft = seqs.length;
135 Vector one2many=new Vector();
137 while (i < leaves.size())
139 j = (SequenceNode) leaves.elementAt(i++);
140 realnam = j.getName();
145 nam = algnIds.findIdMatch(realnam);
151 if (one2many.contains(nam)) {
153 // if (jalview.bin.Cache.log.isDebugEnabled())
154 // jalview.bin.Cache.log.debug("One 2 many relationship for "+nam.getName());
156 one2many.addElement(nam);
162 j.setElement(new Sequence(realnam, "THISISAPLACEHLDER"));
163 j.setPlaceholder(true);
166 // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
167 // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment sequence ids (out of "+one2many.size()+" unique ids) linked to two or more leaves.");
173 * Creates a new NJTree object.
175 * @param sequence DOCUMENT ME!
176 * @param type DOCUMENT ME!
177 * @param pwtype DOCUMENT ME!
178 * @param start DOCUMENT ME!
179 * @param end DOCUMENT ME!
181 public NJTree(SequenceI[] sequence,
182 AlignmentView seqData,
187 this.sequence = sequence;
188 this.node = new Vector();
190 this.pwtype = pwtype;
192 this.seqData = seqData;
194 SeqCigar[] seqs = new SeqCigar[sequence.length];
195 for(int i=0; i<sequence.length; i++)
197 seqs[i] = new SeqCigar(sequence[i], start, end);
199 CigarArray sdata = new CigarArray(seqs);
200 sdata.addOperation(CigarArray.M, end-start+1);
201 this.seqData = new AlignmentView(sdata, start);
204 if (!(type.equals("NJ")))
209 if (!(pwtype.equals("PID")))
216 done = new int[sequence.length];
218 while ((i < sequence.length) && (sequence[i] != null))
226 distance = findDistances(this.seqData.getSequenceStrings(Comparison.GapChars.charAt(0)));
230 noClus = cluster.size();
238 * @return DOCUMENT ME!
240 public String toString()
242 jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode());
244 return fout.print(false, true); // distances only
249 * used when the alignment associated to a tree has changed.
251 * @param alignment Vector
253 public void UpdatePlaceHolders(Vector alignment)
255 Vector leaves = new Vector();
256 findLeaves(top, leaves);
258 int sz = leaves.size();
259 SequenceIdMatcher seqmatcher = null;
264 SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);
266 if (alignment.contains(leaf.element()))
268 leaf.setPlaceholder(false);
272 if (seqmatcher == null)
274 // Only create this the first time we need it
275 SequenceI[] seqs = new SequenceI[alignment.size()];
277 for (int j = 0; j < seqs.length; j++)
278 seqs[j] = (SequenceI) alignment.elementAt(j);
280 seqmatcher = new SequenceIdMatcher(seqs);
283 SequenceI nam = seqmatcher.findIdMatch(leaf.getName());
287 leaf.setPlaceholder(false);
288 leaf.setElement(nam);
292 leaf.setPlaceholder(true);
301 public void cluster()
305 if (type.equals("NJ"))
314 Cluster c = joinClusters(mini, minj);
318 cluster.setElementAt(null, minj);
319 cluster.setElementAt(c, mini);
324 boolean onefound = false;
329 for (int i = 0; i < noseqs; i++)
333 if (onefound == false)
345 joinClusters(one, two);
346 top = (SequenceNode) (node.elementAt(one));
356 * @param i DOCUMENT ME!
357 * @param j DOCUMENT ME!
359 * @return DOCUMENT ME!
361 public Cluster joinClusters(int i, int j)
363 float dist = distance[i][j];
365 int noi = ((Cluster) cluster.elementAt(i)).value.length;
366 int noj = ((Cluster) cluster.elementAt(j)).value.length;
368 int[] value = new int[noi + noj];
370 for (int ii = 0; ii < noi; ii++)
372 value[ii] = ((Cluster) cluster.elementAt(i)).value[ii];
375 for (int ii = noi; ii < (noi + noj); ii++)
377 value[ii] = ((Cluster) cluster.elementAt(j)).value[ii - noi];
380 Cluster c = new Cluster(value);
385 if (type.equals("NJ"))
387 findClusterNJDistance(i, j);
391 findClusterDistance(i, j);
394 SequenceNode sn = new SequenceNode();
396 sn.setLeft((SequenceNode) (node.elementAt(i)));
397 sn.setRight((SequenceNode) (node.elementAt(j)));
399 SequenceNode tmpi = (SequenceNode) (node.elementAt(i));
400 SequenceNode tmpj = (SequenceNode) (node.elementAt(j));
402 if (type.equals("NJ"))
404 findNewNJDistances(tmpi, tmpj, dist);
408 findNewDistances(tmpi, tmpj, dist);
414 node.setElementAt(sn, i);
422 * @param tmpi DOCUMENT ME!
423 * @param tmpj DOCUMENT ME!
424 * @param dist DOCUMENT ME!
426 public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj,
430 tmpi.dist = ((dist + ri) - rj) / 2;
431 tmpj.dist = (dist - tmpi.dist);
447 * @param tmpi DOCUMENT ME!
448 * @param tmpj DOCUMENT ME!
449 * @param dist DOCUMENT ME!
451 public void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
457 SequenceNode sni = tmpi;
458 SequenceNode snj = tmpj;
463 sni = (SequenceNode) sni.left();
469 snj = (SequenceNode) snj.left();
472 tmpi.dist = ((dist / 2) - ih);
473 tmpj.dist = ((dist / 2) - jh);
479 * @param i DOCUMENT ME!
480 * @param j DOCUMENT ME!
482 public void findClusterDistance(int i, int j)
484 int noi = ((Cluster) cluster.elementAt(i)).value.length;
485 int noj = ((Cluster) cluster.elementAt(j)).value.length;
487 // New distances from cluster to others
488 float[] newdist = new float[noseqs];
490 for (int l = 0; l < noseqs; l++)
492 if ((l != i) && (l != j))
494 newdist[l] = ((distance[i][l] * noi) + (distance[j][l] * noj)) / (noi +
503 for (int ii = 0; ii < noseqs; ii++)
505 distance[i][ii] = newdist[ii];
506 distance[ii][i] = newdist[ii];
513 * @param i DOCUMENT ME!
514 * @param j DOCUMENT ME!
516 public void findClusterNJDistance(int i, int j)
519 // New distances from cluster to others
520 float[] newdist = new float[noseqs];
522 for (int l = 0; l < noseqs; l++)
524 if ((l != i) && (l != j))
526 newdist[l] = ((distance[i][l] + distance[j][l]) -
535 for (int ii = 0; ii < noseqs; ii++)
537 distance[i][ii] = newdist[ii];
538 distance[ii][i] = newdist[ii];
545 * @param i DOCUMENT ME!
546 * @param j DOCUMENT ME!
548 * @return DOCUMENT ME!
550 public float findr(int i, int j)
554 for (int k = 0; k < noseqs; k++)
556 if ((k != i) && (k != j) && (done[k] != 1))
558 tmp = tmp + distance[i][k];
564 tmp = tmp / (noClus - 2);
573 * @return DOCUMENT ME!
575 public float findMinNJDistance()
579 for (int i = 0; i < (noseqs - 1); i++)
581 for (int j = i + 1; j < noseqs; j++)
583 if ((done[i] != 1) && (done[j] != 1))
585 float tmp = distance[i][j] - (findr(i, j) + findr(j, i));
604 * @return DOCUMENT ME!
606 public float findMinDistance()
610 for (int i = 0; i < (noseqs - 1); i++)
612 for (int j = i + 1; j < noseqs; j++)
614 if ((done[i] != 1) && (done[j] != 1))
616 if (distance[i][j] < min)
621 min = distance[i][j];
633 * @return DOCUMENT ME!
635 public float[][] findDistances(String[] sequenceString)
637 float[][] distance = new float[noseqs][noseqs];
639 if (pwtype.equals("PID"))
641 for (int i = 0; i < (noseqs - 1); i++)
643 for (int j = i; j < noseqs; j++)
651 distance[i][j] = 100 -
652 Comparison.PID(sequenceString[i], sequenceString[j]);
654 distance[j][i] = distance[i][j];
659 else if (pwtype.equals("BL"))
662 int end = sequenceString[0].length();
663 for (int i = 0; i < (noseqs - 1); i++)
665 for (int j = i; j < noseqs; j++)
669 for (int k = 0; k < end; k++)
673 score += ResidueProperties.getBLOSUM62(
674 sequenceString[i].substring(k, k + 1),
675 sequenceString[j].substring(k, k + 1));
679 System.err.println("err creating BLOSUM62 tree");
680 ex.printStackTrace();
684 distance[i][j] = (float) score;
686 if (score > maxscore)
693 for (int i = 0; i < (noseqs - 1); i++)
695 for (int j = i; j < noseqs; j++)
697 distance[i][j] = (float) maxscore - distance[i][j];
698 distance[j][i] = distance[i][j];
702 /* else if (pwtype.equals("SW"))
706 for (int i = 0; i < (noseqs - 1); i++)
708 for (int j = i; j < noseqs; j++)
710 AlignSeq as = new AlignSeq(sequence[i], sequence[j], "pep");
711 as.calcScoreMatrix();
713 as.printAlignment(System.out);
714 distance[i][j] = (float) as.maxscore;
716 if (max < distance[i][j])
718 max = distance[i][j];
723 for (int i = 0; i < (noseqs - 1); i++)
725 for (int j = i; j < noseqs; j++)
727 distance[i][j] = max - distance[i][j];
728 distance[j][i] = distance[i][j];
739 public void makeLeaves()
741 cluster = new Vector();
743 for (int i = 0; i < noseqs; i++)
745 SequenceNode sn = new SequenceNode();
747 sn.setElement(sequence[i]);
748 sn.setName(sequence[i].getName());
751 int[] value = new int[1];
754 Cluster c = new Cluster(value);
755 cluster.addElement(c);
762 * @param node DOCUMENT ME!
763 * @param leaves DOCUMENT ME!
765 * @return DOCUMENT ME!
767 public Vector findLeaves(SequenceNode node, Vector leaves)
774 if ((node.left() == null) && (node.right() == null))
776 leaves.addElement(node);
782 findLeaves((SequenceNode) node.left(), leaves);
783 findLeaves((SequenceNode) node.right(), leaves);
792 * @param node DOCUMENT ME!
793 * @param count DOCUMENT ME!
795 * @return DOCUMENT ME!
797 public Object findLeaf(SequenceNode node, int count)
799 found = _findLeaf(node, count);
807 * @param node DOCUMENT ME!
808 * @param count DOCUMENT ME!
810 * @return DOCUMENT ME!
812 public Object _findLeaf(SequenceNode node, int count)
819 if (node.ycount == count)
821 found = node.element();
827 _findLeaf((SequenceNode) node.left(), count);
828 _findLeaf((SequenceNode) node.right(), count);
835 * printNode is mainly for debugging purposes.
837 * @param node SequenceNode
839 public void printNode(SequenceNode node)
846 if ((node.left() == null) && (node.right() == null))
848 System.out.println("Leaf = " +
849 ((SequenceI) node.element()).getName());
850 System.out.println("Dist " + ((SequenceNode) node).dist);
851 System.out.println("Boot " + node.getBootstrap());
855 System.out.println("Dist " + ((SequenceNode) node).dist);
856 printNode((SequenceNode) node.left());
857 printNode((SequenceNode) node.right());
864 * @param node DOCUMENT ME!
866 public void findMaxDist(SequenceNode node)
873 if ((node.left() == null) && (node.right() == null))
875 float dist = ((SequenceNode) node).dist;
877 if (dist > maxDistValue)
879 maxdist = (SequenceNode) node;
885 findMaxDist((SequenceNode) node.left());
886 findMaxDist((SequenceNode) node.right());
893 * @return DOCUMENT ME!
895 public Vector getGroups()
903 * @return DOCUMENT ME!
905 public float getMaxHeight()
913 * @param node DOCUMENT ME!
914 * @param threshold DOCUMENT ME!
916 public void groupNodes(SequenceNode node, float threshold)
923 if ((node.height / maxheight) > threshold)
925 groups.addElement(node);
929 groupNodes((SequenceNode) node.left(), threshold);
930 groupNodes((SequenceNode) node.right(), threshold);
937 * @param node DOCUMENT ME!
939 * @return DOCUMENT ME!
941 public float findHeight(SequenceNode node)
948 if ((node.left() == null) && (node.right() == null))
950 node.height = ((SequenceNode) node.parent()).height + node.dist;
952 if (node.height > maxheight)
963 if (node.parent() != null)
965 node.height = ((SequenceNode) node.parent()).height +
971 node.height = (float) 0.0;
974 maxheight = findHeight((SequenceNode) (node.left()));
975 maxheight = findHeight((SequenceNode) (node.right()));
984 * @return DOCUMENT ME!
986 public SequenceNode reRoot()
992 float tmpdist = maxdist.dist;
995 SequenceNode sn = new SequenceNode();
998 // New right hand of top
999 SequenceNode snr = (SequenceNode) maxdist.parent();
1000 changeDirection(snr, maxdist);
1001 System.out.println("Printing reversed tree");
1003 snr.dist = tmpdist / 2;
1004 maxdist.dist = tmpdist / 2;
1007 maxdist.setParent(sn);
1010 sn.setLeft(maxdist);
1023 * @return true if original sequence data can be recovered
1025 public boolean hasOriginalSequenceData() {
1026 return seqData!=null;
1029 * Returns original alignment data used for calculation - or null where
1032 * @return null or cut'n'pasteable alignment
1034 public String printOriginalSequenceData(char gapChar)
1039 StringBuffer sb = new StringBuffer();
1040 String[] seqdatas = seqData.getSequenceStrings(gapChar);
1041 for(int i=0; i<seqdatas.length; i++)
1043 sb.append(new jalview.util.Format("%-" + 15 + "s").form(
1044 sequence[i].getName()));
1045 sb.append(" "+seqdatas[i]+"\n");
1047 return sb.toString();
1052 * @param node DOCUMENT ME!
1054 public void printN(SequenceNode node)
1061 if ((node.left() != null) && (node.right() != null))
1063 printN((SequenceNode) node.left());
1064 printN((SequenceNode) node.right());
1068 System.out.println(" name = " +
1069 ((SequenceI) node.element()).getName());
1072 System.out.println(" dist = " + ((SequenceNode) node).dist + " " +
1073 ((SequenceNode) node).count + " " + ((SequenceNode) node).height);
1079 * @param node DOCUMENT ME!
1081 public void reCount(SequenceNode node)
1090 * @param node DOCUMENT ME!
1092 public void _reCount(SequenceNode node)
1099 if ((node.left() != null) && (node.right() != null))
1101 _reCount((SequenceNode) node.left());
1102 _reCount((SequenceNode) node.right());
1104 SequenceNode l = (SequenceNode) node.left();
1105 SequenceNode r = (SequenceNode) node.right();
1107 ((SequenceNode) node).count = l.count + r.count;
1108 ((SequenceNode) node).ycount = (l.ycount + r.ycount) / 2;
1112 ((SequenceNode) node).count = 1;
1113 ((SequenceNode) node).ycount = ycount++;
1120 * @param node DOCUMENT ME!
1122 public void swapNodes(SequenceNode node)
1129 SequenceNode tmp = (SequenceNode) node.left();
1131 node.setLeft(node.right());
1138 * @param node DOCUMENT ME!
1139 * @param dir DOCUMENT ME!
1141 public void changeDirection(SequenceNode node, SequenceNode dir)
1148 if (node.parent() != top)
1150 changeDirection((SequenceNode) node.parent(), node);
1152 SequenceNode tmp = (SequenceNode) node.parent();
1154 if (dir == node.left())
1156 node.setParent(dir);
1159 else if (dir == node.right())
1161 node.setParent(dir);
1167 if (dir == node.left())
1169 node.setParent(node.left());
1171 if (top.left() == node)
1173 node.setRight(top.right());
1177 node.setRight(top.left());
1182 node.setParent(node.right());
1184 if (top.left() == node)
1186 node.setLeft(top.right());
1190 node.setLeft(top.left());
1200 * @return DOCUMENT ME!
1202 public SequenceNode getMaxDist()
1210 * @return DOCUMENT ME!
1212 public SequenceNode getTopNode()
1218 * @return true if tree has real distances
1220 public boolean isHasDistances() {
1221 return hasDistances;
1226 * @return true if tree has real bootstrap values
1228 public boolean isHasBootstrap() {
1229 return hasBootstrap;
1232 public boolean isHasRootDistance()
1234 return hasRootDistance;
1244 * @version $Revision$
1251 * Creates a new Cluster object.
1253 * @param value DOCUMENT ME!
1255 public Cluster(int[] value)