1 package jalview.analysis;
\r
3 import jalview.datamodel.*;
\r
4 import jalview.util.*;
\r
5 import jalview.schemes.ResidueProperties;
\r
8 import jalview.io.NewickFile;
\r
10 public class NJTree {
\r
13 SequenceI[] sequence;
\r
26 Vector groups = new Vector();
\r
27 SequenceNode maxdist;
\r
40 Object found = null;
\r
41 Object leaves = null;
\r
46 public NJTree(SequenceNode node) {
\r
48 maxheight = findHeight(top);
\r
51 public String toString()
\r
53 jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode());
\r
54 return fout.print(false,true); // distances only
\r
57 public NJTree(SequenceI[] seqs, NewickFile treefile) {
\r
58 top = treefile.getTree();
\r
59 maxheight = findHeight(top);
\r
60 SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
\r
62 Vector leaves = new Vector();
\r
63 findLeaves(top, leaves);
\r
66 int namesleft = seqs.length;
\r
71 while (i < leaves.size())
\r
73 j = (SequenceNode) leaves.elementAt(i++);
\r
74 realnam = j.getName();
\r
77 nam = algnIds.findIdMatch(realnam);
\r
82 j.setElement(new Sequence(realnam, "THISISAPLACEHLDER"));
\r
83 j.setPlaceholder(true);
\r
89 public NJTree(SequenceI[] sequence,int start, int end) {
\r
90 this(sequence,"NJ","BL",start,end);
\r
93 public NJTree(SequenceI[] sequence,String type,String pwtype,int start, int end ) {
\r
95 this.sequence = sequence;
\r
96 this.node = new Vector();
\r
98 this.pwtype = pwtype;
\r
102 if (!(type.equals("NJ"))) {
\r
106 if (!(pwtype.equals("PID"))) {
\r
112 done = new int[sequence.length];
\r
115 while (i < sequence.length && sequence[i] != null) {
\r
122 distance = findDistances();
\r
126 noClus = cluster.size();
\r
133 public void cluster() {
\r
135 while (noClus > 2) {
\r
136 if (type.equals("NJ")) {
\r
137 float mind = findMinNJDistance();
\r
139 float mind = findMinDistance();
\r
142 Cluster c = joinClusters(mini,minj);
\r
147 cluster.setElementAt(null,minj);
\r
148 cluster.setElementAt(c,mini);
\r
153 boolean onefound = false;
\r
158 for (int i=0; i < noseqs; i++) {
\r
159 if (done[i] != 1) {
\r
160 if (onefound == false) {
\r
169 Cluster c = joinClusters(one,two);
\r
170 top = (SequenceNode)(node.elementAt(one));
\r
178 public Cluster joinClusters(int i, int j) {
\r
180 float dist = distance[i][j];
\r
182 int noi = ((Cluster)cluster.elementAt(i)).value.length;
\r
183 int noj = ((Cluster)cluster.elementAt(j)).value.length;
\r
185 int[] value = new int[noi + noj];
\r
187 for (int ii = 0; ii < noi;ii++) {
\r
188 value[ii] = ((Cluster)cluster.elementAt(i)).value[ii];
\r
191 for (int ii = noi; ii < noi+ noj;ii++) {
\r
192 value[ii] = ((Cluster)cluster.elementAt(j)).value[ii-noi];
\r
195 Cluster c = new Cluster(value);
\r
200 if (type.equals("NJ")) {
\r
201 findClusterNJDistance(i,j);
\r
203 findClusterDistance(i,j);
\r
206 SequenceNode sn = new SequenceNode();
\r
208 sn.setLeft((SequenceNode)(node.elementAt(i)));
\r
209 sn.setRight((SequenceNode)(node.elementAt(j)));
\r
211 SequenceNode tmpi = (SequenceNode)(node.elementAt(i));
\r
212 SequenceNode tmpj = (SequenceNode)(node.elementAt(j));
\r
214 if (type.equals("NJ")) {
\r
215 findNewNJDistances(tmpi,tmpj,dist);
\r
217 findNewDistances(tmpi,tmpj,dist);
\r
220 tmpi.setParent(sn);
\r
221 tmpj.setParent(sn);
\r
223 node.setElementAt(sn,i);
\r
227 public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, float dist) {
\r
232 SequenceNode sni = tmpi;
\r
233 SequenceNode snj = tmpj;
\r
235 tmpi.dist = (dist + ri - rj)/2;
\r
236 tmpj.dist = (dist - tmpi.dist);
\r
238 if (tmpi.dist < 0) {
\r
241 if (tmpj.dist < 0) {
\r
246 public void findNewDistances(SequenceNode tmpi,SequenceNode tmpj,float dist) {
\r
251 SequenceNode sni = tmpi;
\r
252 SequenceNode snj = tmpj;
\r
254 while (sni != null) {
\r
255 ih = ih + sni.dist;
\r
256 sni = (SequenceNode)sni.left();
\r
259 while (snj != null) {
\r
260 jh = jh + snj.dist;
\r
261 snj = (SequenceNode)snj.left();
\r
264 tmpi.dist = (dist/2 - ih);
\r
265 tmpj.dist = (dist/2 - jh);
\r
270 public void findClusterDistance(int i, int j) {
\r
272 int noi = ((Cluster)cluster.elementAt(i)).value.length;
\r
273 int noj = ((Cluster)cluster.elementAt(j)).value.length;
\r
275 // New distances from cluster to others
\r
276 float[] newdist = new float[noseqs];
\r
278 for (int l = 0; l < noseqs; l++) {
\r
279 if ( l != i && l != j) {
\r
280 newdist[l] = (distance[i][l] * noi + distance[j][l] * noj)/(noi + noj);
\r
286 for (int ii=0; ii < noseqs;ii++) {
\r
287 distance[i][ii] = newdist[ii];
\r
288 distance[ii][i] = newdist[ii];
\r
292 public void findClusterNJDistance(int i, int j) {
\r
294 int noi = ((Cluster)cluster.elementAt(i)).value.length;
\r
295 int noj = ((Cluster)cluster.elementAt(j)).value.length;
\r
297 // New distances from cluster to others
\r
298 float[] newdist = new float[noseqs];
\r
300 for (int l = 0; l < noseqs; l++) {
\r
301 if ( l != i && l != j) {
\r
302 newdist[l] = (distance[i][l] + distance[j][l] - distance[i][j])/2;
\r
308 for (int ii=0; ii < noseqs;ii++) {
\r
309 distance[i][ii] = newdist[ii];
\r
310 distance[ii][i] = newdist[ii];
\r
314 public float findr(int i, int j) {
\r
317 for (int k=0; k < noseqs;k++) {
\r
318 if (k!= i && k!= j && done[k] != 1) {
\r
319 tmp = tmp + distance[i][k];
\r
324 tmp = tmp/(noClus - 2);
\r
330 public float findMinNJDistance() {
\r
332 float min = 100000;
\r
334 for (int i=0; i < noseqs-1; i++) {
\r
335 for (int j=i+1;j < noseqs;j++) {
\r
336 if (done[i] != 1 && done[j] != 1) {
\r
337 float tmp = distance[i][j] - (findr(i,j) + findr(j,i));
\r
352 public float findMinDistance() {
\r
354 float min = 100000;
\r
356 for (int i=0; i < noseqs-1;i++) {
\r
357 for (int j = i+1; j < noseqs;j++) {
\r
358 if (done[i] != 1 && done[j] != 1) {
\r
359 if (distance[i][j] < min) {
\r
363 min = distance[i][j];
\r
371 public float[][] findDistances() {
\r
373 float[][] distance = new float[noseqs][noseqs];
\r
374 if (pwtype.equals("PID")) {
\r
375 for (int i = 0; i < noseqs-1; i++) {
\r
376 for (int j = i; j < noseqs; j++) {
\r
378 distance[i][i] = 0;
\r
380 distance[i][j] = 100-Comparison.PID(sequence[i], sequence[j]);
\r
381 distance[j][i] = distance[i][j];
\r
385 } else if (pwtype.equals("BL")) {
\r
388 for (int i = 0; i < noseqs-1; i++) {
\r
389 for (int j = i; j < noseqs; j++) {
\r
391 for (int k=0; k < sequence[i].getLength(); k++) {
\r
394 ResidueProperties.getBLOSUM62(sequence[i].getSequence(k,
\r
396 sequence[j].getSequence(k,
\r
398 }catch(Exception ex){System.out.println("err creating BLOSUM62 tree");}
\r
400 distance[i][j] = (float)score;
\r
401 if (score > maxscore) {
\r
406 for (int i = 0; i < noseqs-1; i++) {
\r
407 for (int j = i; j < noseqs; j++) {
\r
408 distance[i][j] = (float)maxscore - distance[i][j];
\r
409 distance[j][i] = distance[i][j];
\r
412 } else if (pwtype.equals("SW")) {
\r
414 for (int i = 0; i < noseqs-1; i++) {
\r
415 for (int j = i; j < noseqs; j++) {
\r
416 AlignSeq as = new AlignSeq(sequence[i],sequence[j],"pep");
\r
417 as.calcScoreMatrix();
\r
418 as.traceAlignment();
\r
419 as.printAlignment();
\r
420 distance[i][j] = (float)as.maxscore;
\r
421 if (max < distance[i][j]) {
\r
422 max = distance[i][j];
\r
426 for (int i = 0; i < noseqs-1; i++) {
\r
427 for (int j = i; j < noseqs; j++) {
\r
428 distance[i][j] = max - distance[i][j];
\r
429 distance[j][i] = distance[i][j];
\r
437 public void makeLeaves() {
\r
438 cluster = new Vector();
\r
440 for (int i=0; i < noseqs; i++) {
\r
441 SequenceNode sn = new SequenceNode();
\r
443 sn.setElement(sequence[i]);
\r
444 sn.setName(sequence[i].getName());
\r
445 node.addElement(sn);
\r
447 int[] value = new int[1];
\r
450 Cluster c = new Cluster(value);
\r
451 cluster.addElement(c);
\r
455 public Vector findLeaves(SequenceNode node, Vector leaves) {
\r
456 if (node == null) {
\r
460 if (node.left() == null && node.right() == null) {
\r
461 leaves.addElement(node);
\r
464 findLeaves((SequenceNode)node.left(),leaves);
\r
465 findLeaves((SequenceNode)node.right(),leaves);
\r
470 public Object findLeaf(SequenceNode node, int count) {
\r
471 found = _findLeaf(node,count);
\r
475 public Object _findLeaf(SequenceNode node,int count) {
\r
476 if (node == null) {
\r
479 if (node.ycount == count) {
\r
480 found = node.element();
\r
483 _findLeaf((SequenceNode)node.left(),count);
\r
484 _findLeaf((SequenceNode)node.right(),count);
\r
490 public void printNode(SequenceNode node) {
\r
491 if (node == null) {
\r
494 if (node.left() == null && node.right() == null) {
\r
495 System.out.println("Leaf = " + ((SequenceI)node.element()).getName());
\r
496 System.out.println("Dist " + ((SequenceNode)node).dist);
\r
497 System.out.println("Boot " + node.getBootstrap());
\r
499 System.out.println("Dist " + ((SequenceNode)node).dist);
\r
500 printNode((SequenceNode)node.left());
\r
501 printNode((SequenceNode)node.right());
\r
504 public void findMaxDist(SequenceNode node) {
\r
505 if (node == null) {
\r
508 if (node.left() == null && node.right() == null) {
\r
510 float dist = ((SequenceNode)node).dist;
\r
511 if (dist > maxDistValue) {
\r
512 maxdist = (SequenceNode)node;
\r
513 maxDistValue = dist;
\r
516 findMaxDist((SequenceNode)node.left());
\r
517 findMaxDist((SequenceNode)node.right());
\r
520 public Vector getGroups() {
\r
523 public float getMaxHeight() {
\r
526 public void groupNodes(SequenceNode node, float threshold) {
\r
527 if (node == null) {
\r
531 if (node.height/maxheight > threshold) {
\r
532 groups.addElement(node);
\r
534 groupNodes((SequenceNode)node.left(),threshold);
\r
535 groupNodes((SequenceNode)node.right(),threshold);
\r
539 public float findHeight(SequenceNode node) {
\r
541 if (node == null) {
\r
545 if (node.left() == null && node.right() == null) {
\r
546 node.height = ((SequenceNode)node.parent()).height + node.dist;
\r
548 if (node.height > maxheight) {
\r
549 return node.height;
\r
554 if (node.parent() != null) {
\r
555 node.height = ((SequenceNode)node.parent()).height + node.dist;
\r
558 node.height = (float)0.0;
\r
561 maxheight = findHeight((SequenceNode)(node.left()));
\r
562 maxheight = findHeight((SequenceNode)(node.right()));
\r
566 public SequenceNode reRoot() {
\r
567 if (maxdist != null) {
\r
569 float tmpdist = maxdist.dist;
\r
572 SequenceNode sn = new SequenceNode();
\r
573 sn.setParent(null);
\r
575 // New right hand of top
\r
576 SequenceNode snr = (SequenceNode)maxdist.parent();
\r
577 changeDirection(snr,maxdist);
\r
578 System.out.println("Printing reversed tree");
\r
580 snr.dist = tmpdist/2;
\r
581 maxdist.dist = tmpdist/2;
\r
584 maxdist.setParent(sn);
\r
587 sn.setLeft(maxdist);
\r
598 public static void printN(SequenceNode node) {
\r
599 if (node == null) {
\r
603 if (node.left() != null && node.right() != null) {
\r
604 printN((SequenceNode)node.left());
\r
605 printN((SequenceNode)node.right());
\r
607 System.out.println(" name = " + ((SequenceI)node.element()).getName());
\r
609 System.out.println(" dist = " + ((SequenceNode)node).dist + " " + ((SequenceNode)node).count + " " + ((SequenceNode)node).height);
\r
612 public void reCount(SequenceNode node) {
\r
616 public void _reCount(SequenceNode node) {
\r
617 if (node == null) {
\r
621 if (node.left() != null && node.right() != null) {
\r
622 _reCount((SequenceNode)node.left());
\r
623 _reCount((SequenceNode)node.right());
\r
625 SequenceNode l = (SequenceNode)node.left();
\r
626 SequenceNode r = (SequenceNode)node.right();
\r
628 ((SequenceNode)node).count = l.count + r.count;
\r
629 ((SequenceNode)node).ycount = (l.ycount + r.ycount)/2;
\r
632 ((SequenceNode)node).count = 1;
\r
633 ((SequenceNode)node).ycount = ycount++;
\r
637 public void swapNodes(SequenceNode node) {
\r
638 if (node == null) {
\r
641 SequenceNode tmp = (SequenceNode)node.left();
\r
643 node.setLeft(node.right());
\r
644 node.setRight(tmp);
\r
646 public void changeDirection(SequenceNode node, SequenceNode dir) {
\r
647 if (node == null) {
\r
650 if (node.parent() != top) {
\r
651 changeDirection((SequenceNode)node.parent(), node);
\r
653 SequenceNode tmp = (SequenceNode)node.parent();
\r
655 if (dir == node.left()) {
\r
656 node.setParent(dir);
\r
658 } else if (dir == node.right()) {
\r
659 node.setParent(dir);
\r
660 node.setRight(tmp);
\r
664 if (dir == node.left()) {
\r
665 node.setParent(node.left());
\r
667 if (top.left() == node) {
\r
668 node.setRight(top.right());
\r
670 node.setRight(top.left());
\r
673 node.setParent(node.right());
\r
675 if (top.left() == node) {
\r
676 node.setLeft(top.right());
\r
678 node.setLeft(top.left());
\r
683 public void setMaxDist(SequenceNode node) {
\r
684 this.maxdist = maxdist;
\r
686 public SequenceNode getMaxDist() {
\r
689 public SequenceNode getTopNode() {
\r
701 public Cluster(int[] value) {
\r
702 this.value = value;
\r