1 Clazz.declarePackage ("jalview.analysis");
2 Clazz.load (["java.util.Vector"], ["jalview.analysis.Cluster", "$.NJTree"], ["jalview.analysis.SequenceIdMatcher", "jalview.datamodel.AlignmentView", "$.CigarArray", "$.NodeTransformI", "$.SeqCigar", "$.Sequence", "$.SequenceI", "$.SequenceNode", "jalview.io.NewickFile", "jalview.schemes.ResidueProperties", "jalview.util.Format", "java.lang.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
18 this.maxDistValue = 0;
26 this.hasDistances = true;
27 this.hasBootstrap = false;
28 this.hasRootDistance = true;
31 Clazz.instantialize (this, arguments);
32 }, jalview.analysis, "NJTree");
33 Clazz.prepareFields (c$, function () {
34 this.groups = new java.util.Vector ();
36 Clazz.makeConstructor (c$,
37 function (seqs, odata, treefile) {
38 this.construct (seqs, treefile);
41 }}, "~A,jalview.datamodel.AlignmentView,jalview.io.NewickFile");
42 Clazz.makeConstructor (c$,
43 function (seqs, treefile) {
45 this.top = treefile.getTree ();
46 this.hasDistances = treefile.HasDistances ();
47 this.hasBootstrap = treefile.HasBootstrap ();
48 this.hasRootDistance = treefile.HasRootDistance ();
49 this.maxheight = this.findHeight (this.top);
50 var algnIds = new jalview.analysis.SequenceIdMatcher (seqs);
51 var leaves = new java.util.Vector ();
52 this.findLeaves (this.top, leaves);
54 var namesleft = seqs.length;
58 var one2many = new java.util.Vector ();
59 var countOne2Many = 0;
60 while (i < leaves.size ()) {
61 j = leaves.elementAt (i++);
62 realnam = j.getName ();
65 nam = algnIds.findIdMatch (realnam);
68 if (one2many.contains (nam)) {
71 one2many.addElement (nam);
74 j.setElement ( new jalview.datamodel.Sequence (realnam, "THISISAPLACEHLDER"));
75 j.setPlaceholder (true);
77 }, "~A,jalview.io.NewickFile");
78 Clazz.makeConstructor (c$,
79 function (sequence, seqData, type, pwtype, sm, start, end) {
80 this.sequence = sequence;
81 this.node = new java.util.Vector ();
84 if (seqData != null) {
85 this.seqData = seqData;
87 var seqs = new Array (sequence.length);
88 for (var i = 0; i < sequence.length; i++) {
89 seqs[i] = new jalview.datamodel.SeqCigar (sequence[i], start, end);
91 var sdata = new jalview.datamodel.CigarArray (seqs);
92 sdata.addOperation ('M', end - start + 1);
93 this.seqData = new jalview.datamodel.AlignmentView (sdata, start);
94 }if (!(type.equals ("NJ"))) {
96 }if (sm == null && !(pwtype.equals ("PID"))) {
97 if (jalview.schemes.ResidueProperties.getScoreMatrix (pwtype) == null) {
100 this.done = Clazz.newIntArray (sequence.length, 0);
101 while ((i < sequence.length) && (sequence[i] != null)) {
106 this.distance = this.findDistances (sm);
108 this.noClus = this.$cluster.size ();
110 }, "~A,jalview.datamodel.AlignmentView,~S,~S,jalview.api.analysis.ScoreModelI,~N,~N");
111 Clazz.overrideMethod (c$, "toString",
113 var fout = new jalview.io.NewickFile (this.getTopNode ());
114 return fout.print (this.isHasBootstrap (), this.isHasDistances (), this.isHasRootDistance ());
116 Clazz.defineMethod (c$, "UpdatePlaceHolders",
118 var leaves = new java.util.Vector ();
119 this.findLeaves (this.top, leaves);
120 var sz = leaves.size ();
121 var seqmatcher = null;
124 var leaf = leaves.elementAt (i++);
125 if (list.contains (leaf.element ())) {
126 leaf.setPlaceholder (false);
128 if (seqmatcher == null) {
129 var seqs = new Array (list.size ());
130 for (var j = 0; j < seqs.length; j++) {
131 seqs[j] = list.get (j);
133 seqmatcher = new jalview.analysis.SequenceIdMatcher (seqs);
134 }var nam = seqmatcher.findIdMatch (leaf.getName ());
136 if (!leaf.isPlaceholder ()) {
137 }leaf.setPlaceholder (false);
138 leaf.setElement (nam);
140 if (!leaf.isPlaceholder ()) {
141 leaf.setElement ( new jalview.datamodel.Sequence (leaf.getName (), "THISISAPLACEHLDER"));
142 }leaf.setPlaceholder (true);
144 }, "java.util.List");
145 Clazz.defineMethod (c$, "renameAssociatedNodes",
147 this.applyToNodes (((Clazz.isClassDefined ("jalview.analysis.NJTree$1") ? 0 : jalview.analysis.NJTree.$NJTree$1$ ()), Clazz.innerTypeInstance (jalview.analysis.NJTree$1, this, null)));
149 Clazz.defineMethod (c$, "cluster",
151 while (this.noClus > 2) {
152 if (this.type.equals ("NJ")) {
153 this.findMinNJDistance ();
155 this.findMinDistance ();
156 }var c = this.joinClusters (this.mini, this.minj);
157 this.done[this.minj] = 1;
158 this.$cluster.setElementAt (null, this.minj);
159 this.$cluster.setElementAt (c, this.mini);
162 var onefound = false;
165 for (var i = 0; i < this.noseqs; i++) {
166 if (this.done[i] != 1) {
167 if (onefound == false) {
173 this.joinClusters (one, two);
174 this.top = (this.node.elementAt (one));
175 this.reCount (this.top);
176 this.findHeight (this.top);
177 this.findMaxDist (this.top);
179 Clazz.defineMethod (c$, "joinClusters",
181 var dist = this.distance[i][j];
182 var noi = (this.$cluster.elementAt (i)).value.length;
183 var noj = (this.$cluster.elementAt (j)).value.length;
184 var value = Clazz.newIntArray (noi + noj, 0);
185 for (var ii = 0; ii < noi; ii++) {
186 value[ii] = (this.$cluster.elementAt (i)).value[ii];
188 for (var ii = noi; ii < (noi + noj); ii++) {
189 value[ii] = (this.$cluster.elementAt (j)).value[ii - noi];
191 var c = new jalview.analysis.Cluster (value);
192 this.ri = this.findr (i, j);
193 this.rj = this.findr (j, i);
194 if (this.type.equals ("NJ")) {
195 this.findClusterNJDistance (i, j);
197 this.findClusterDistance (i, j);
198 }var sn = new jalview.datamodel.SequenceNode ();
199 sn.setLeft ((this.node.elementAt (i)));
200 sn.setRight ((this.node.elementAt (j)));
201 var tmpi = (this.node.elementAt (i));
202 var tmpj = (this.node.elementAt (j));
203 if (this.type.equals ("NJ")) {
204 this.findNewNJDistances (tmpi, tmpj, dist);
206 this.findNewDistances (tmpi, tmpj, dist);
207 }tmpi.setParent (sn);
209 this.node.setElementAt (sn, i);
212 Clazz.defineMethod (c$, "findNewNJDistances",
213 function (tmpi, tmpj, dist) {
214 tmpi.dist = ((dist + this.ri) - this.rj) / 2;
215 tmpj.dist = (dist - tmpi.dist);
218 }if (tmpj.dist < 0) {
220 }}, "jalview.datamodel.SequenceNode,jalview.datamodel.SequenceNode,~N");
221 Clazz.defineMethod (c$, "findNewDistances",
222 function (tmpi, tmpj, dist) {
227 while (sni != null) {
231 while (snj != null) {
235 tmpi.dist = ((dist / 2) - ih);
236 tmpj.dist = ((dist / 2) - jh);
237 }, "jalview.datamodel.SequenceNode,jalview.datamodel.SequenceNode,~N");
238 Clazz.defineMethod (c$, "findClusterDistance",
240 var noi = (this.$cluster.elementAt (i)).value.length;
241 var noj = (this.$cluster.elementAt (j)).value.length;
242 var newdist = Clazz.newFloatArray (this.noseqs, 0);
243 for (var l = 0; l < this.noseqs; l++) {
244 if ((l != i) && (l != j)) {
245 newdist[l] = ((this.distance[i][l] * noi) + (this.distance[j][l] * noj)) / (noi + noj);
249 for (var ii = 0; ii < this.noseqs; ii++) {
250 this.distance[i][ii] = newdist[ii];
251 this.distance[ii][i] = newdist[ii];
254 Clazz.defineMethod (c$, "findClusterNJDistance",
256 var newdist = Clazz.newFloatArray (this.noseqs, 0);
257 for (var l = 0; l < this.noseqs; l++) {
258 if ((l != i) && (l != j)) {
259 newdist[l] = ((this.distance[i][l] + this.distance[j][l]) - this.distance[i][j]) / 2;
263 for (var ii = 0; ii < this.noseqs; ii++) {
264 this.distance[i][ii] = newdist[ii];
265 this.distance[ii][i] = newdist[ii];
268 Clazz.defineMethod (c$, "findr",
271 for (var k = 0; k < this.noseqs; k++) {
272 if ((k != i) && (k != j) && (this.done[k] != 1)) {
273 tmp = tmp + this.distance[i][k];
275 if (this.noClus > 2) {
276 tmp = tmp / (this.noClus - 2);
279 Clazz.defineMethod (c$, "findMinNJDistance",
282 for (var i = 0; i < (this.noseqs - 1); i++) {
283 for (var j = i + 1; j < this.noseqs; j++) {
284 if ((this.done[i] != 1) && (this.done[j] != 1)) {
285 var tmp = this.distance[i][j] - (this.findr (i, j) + this.findr (j, i));
294 Clazz.defineMethod (c$, "findMinDistance",
297 for (var i = 0; i < (this.noseqs - 1); i++) {
298 for (var j = i + 1; j < this.noseqs; j++) {
299 if ((this.done[i] != 1) && (this.done[j] != 1)) {
300 if (this.distance[i][j] < min) {
303 min = this.distance[i][j];
308 Clazz.defineMethod (c$, "findDistances",
309 function (_pwmatrix) {
310 var distance = Clazz.newFloatArray (this.noseqs, this.noseqs, 0);
311 if (_pwmatrix == null) {
312 _pwmatrix = jalview.schemes.ResidueProperties.getScoreModel (this.pwtype);
313 if (_pwmatrix == null) {
314 _pwmatrix = jalview.schemes.ResidueProperties.getScoreMatrix ("BLOSUM62");
315 }}distance = _pwmatrix.findDistances (this.seqData);
317 }, "jalview.api.analysis.ScoreModelI");
318 Clazz.defineMethod (c$, "makeLeaves",
320 this.$cluster = new java.util.Vector ();
321 for (var i = 0; i < this.noseqs; i++) {
322 var sn = new jalview.datamodel.SequenceNode ();
323 sn.setElement (this.sequence[i]);
324 sn.setName (this.sequence[i].getName ());
325 this.node.addElement (sn);
326 var value = Clazz.newIntArray (1, 0);
328 var c = new jalview.analysis.Cluster (value);
329 this.$cluster.addElement (c);
332 Clazz.defineMethod (c$, "findLeaves",
333 function (node, leaves) {
336 }if ((node.left () == null) && (node.right () == null)) {
337 leaves.addElement (node);
340 this.findLeaves (node.left (), leaves);
341 this.findLeaves (node.right (), leaves);
343 }, "jalview.datamodel.SequenceNode,java.util.Vector");
344 Clazz.defineMethod (c$, "findLeaf",
345 function (node, count) {
346 this.found = this._findLeaf (node, count);
348 }, "jalview.datamodel.SequenceNode,~N");
349 Clazz.defineMethod (c$, "_findLeaf",
350 function (node, count) {
353 }if (node.ycount == count) {
354 this.found = node.element ();
357 this._findLeaf (node.left (), count);
358 this._findLeaf (node.right (), count);
360 }, "jalview.datamodel.SequenceNode,~N");
361 Clazz.defineMethod (c$, "printNode",
365 }if ((node.left () == null) && (node.right () == null)) {
366 System.out.println ("Leaf = " + (node.element ()).getName ());
367 System.out.println ("Dist " + node.dist);
368 System.out.println ("Boot " + node.getBootstrap ());
370 System.out.println ("Dist " + node.dist);
371 this.printNode (node.left ());
372 this.printNode (node.right ());
373 }}, "jalview.datamodel.SequenceNode");
374 Clazz.defineMethod (c$, "findMaxDist",
378 }if ((node.left () == null) && (node.right () == null)) {
379 var dist = node.dist;
380 if (dist > this.maxDistValue) {
382 this.maxDistValue = dist;
384 this.findMaxDist (node.left ());
385 this.findMaxDist (node.right ());
386 }}, "jalview.datamodel.SequenceNode");
387 Clazz.defineMethod (c$, "getGroups",
391 Clazz.defineMethod (c$, "getMaxHeight",
393 return this.maxheight;
395 Clazz.defineMethod (c$, "groupNodes",
396 function (node, threshold) {
399 }if ((node.height / this.maxheight) > threshold) {
400 this.groups.addElement (node);
402 this.groupNodes (node.left (), threshold);
403 this.groupNodes (node.right (), threshold);
404 }}, "jalview.datamodel.SequenceNode,~N");
405 Clazz.defineMethod (c$, "findHeight",
408 return this.maxheight;
409 }if ((node.left () == null) && (node.right () == null)) {
410 node.height = (node.parent ()).height + node.dist;
411 if (node.height > this.maxheight) {
414 return this.maxheight;
416 if (node.parent () != null) {
417 node.height = (node.parent ()).height + node.dist;
421 }this.maxheight = this.findHeight ((node.left ()));
422 this.maxheight = this.findHeight ((node.right ()));
423 }return this.maxheight;
424 }, "jalview.datamodel.SequenceNode");
425 Clazz.defineMethod (c$, "reRoot",
427 if (this.maxdist != null) {
429 var tmpdist = this.maxdist.dist;
430 var sn = new jalview.datamodel.SequenceNode ();
432 var snr = this.maxdist.parent ();
433 this.changeDirection (snr, this.maxdist);
434 System.out.println ("Printing reversed tree");
436 snr.dist = tmpdist / 2;
437 this.maxdist.dist = tmpdist / 2;
439 this.maxdist.setParent (sn);
441 sn.setLeft (this.maxdist);
444 this.reCount (this.top);
445 this.findHeight (this.top);
448 Clazz.defineMethod (c$, "hasOriginalSequenceData",
450 return this.seqData != null;
452 Clazz.defineMethod (c$, "printOriginalSequenceData",
454 if (this.seqData == null) {
456 }var sb = new StringBuffer ();
457 var seqdatas = this.seqData.getSequenceStrings (gapChar);
458 for (var i = 0; i < seqdatas.length; i++) {
459 sb.append ( new jalview.util.Format ("%-15s").form (this.sequence[i].getName ()));
460 sb.append (" " + seqdatas[i] + "\n");
462 return sb.toString ();
464 Clazz.defineMethod (c$, "printN",
468 }if ((node.left () != null) && (node.right () != null)) {
469 this.printN (node.left ());
470 this.printN (node.right ());
472 System.out.println (" name = " + (node.element ()).getName ());
473 }System.out.println (" dist = " + node.dist + " " + node.count + " " + node.height);
474 }, "jalview.datamodel.SequenceNode");
475 Clazz.defineMethod (c$, "reCount",
479 this._reCount (node);
480 }, "jalview.datamodel.SequenceNode");
481 Clazz.defineMethod (c$, "_reCount",
486 if ((node.left () != null) && (node.right () != null)) {
487 this._reCount (node.left ());
488 this._reCount (node.right ());
489 var l = node.left ();
490 var r = node.right ();
491 node.count = l.count + r.count;
492 node.ycount = (l.ycount + r.ycount) / 2;
495 node.ycount = this.ycount++;
497 }, "jalview.datamodel.SequenceNode");
498 Clazz.defineMethod (c$, "swapNodes",
502 }var tmp = node.left ();
503 node.setLeft (node.right ());
505 }, "jalview.datamodel.SequenceNode");
506 Clazz.defineMethod (c$, "changeDirection",
507 function (node, dir) {
510 }if (node.parent () !== this.top) {
511 this.changeDirection (node.parent (), node);
512 var tmp = node.parent ();
513 if (dir === node.left ()) {
514 node.setParent (dir);
516 } else if (dir === node.right ()) {
517 node.setParent (dir);
520 if (dir === node.left ()) {
521 node.setParent (node.left ());
522 if (this.top.left () === node) {
523 node.setRight (this.top.right ());
525 node.setRight (this.top.left ());
527 node.setParent (node.right ());
528 if (this.top.left () === node) {
529 node.setLeft (this.top.right ());
531 node.setLeft (this.top.left ());
532 }}}}, "jalview.datamodel.SequenceNode,jalview.datamodel.SequenceNode");
533 Clazz.defineMethod (c$, "getMaxDist",
537 Clazz.defineMethod (c$, "getTopNode",
541 Clazz.defineMethod (c$, "isHasDistances",
543 return this.hasDistances;
545 Clazz.defineMethod (c$, "isHasBootstrap",
547 return this.hasBootstrap;
549 Clazz.defineMethod (c$, "isHasRootDistance",
551 return this.hasRootDistance;
553 Clazz.defineMethod (c$, "applyToNodes",
554 function (nodeTransformI) {
555 for (var nodes = this.node.elements (); nodes.hasMoreElements (); nodeTransformI.transform (nodes.nextElement ())) {
557 }, "jalview.datamodel.NodeTransformI");
558 c$.$NJTree$1$ = function () {
560 c$ = Clazz.declareAnonymous (jalview.analysis, "NJTree$1", null, jalview.datamodel.NodeTransformI);
561 Clazz.defineMethod (c$, "transform",
563 var el = node.element ();
564 if (el != null && Clazz.instanceOf (el, jalview.datamodel.SequenceI)) {
565 node.setName ((el).getName ());
566 }}, "jalview.datamodel.BinaryNode");
569 c$ = Clazz.decorateAsClass (function () {
571 Clazz.instantialize (this, arguments);
572 }, jalview.analysis, "Cluster");
573 Clazz.makeConstructor (c$,