JAL-1807 test
[jalviewjs.git] / bin / jalview / analysis / NJTree.js
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 () {
4 this.$cluster = null;
5 this.sequence = null;
6 this.seqData = null;
7 this.done = null;
8 this.noseqs = 0;
9 this.noClus = 0;
10 this.distance = null;
11 this.mini = 0;
12 this.minj = 0;
13 this.ri = 0;
14 this.rj = 0;
15 this.groups = null;
16 this.maxdist = null;
17 this.top = null;
18 this.maxDistValue = 0;
19 this.maxheight = 0;
20 this.ycount = 0;
21 this.node = null;
22 this.type = null;
23 this.pwtype = null;
24 this.found = null;
25 this.leaves = null;
26 this.hasDistances = true;
27 this.hasBootstrap = false;
28 this.hasRootDistance = true;
29 this._lycount = 0;
30 this._lylimit = 0;
31 Clazz.instantialize (this, arguments);
32 }, jalview.analysis, "NJTree");
33 Clazz.prepareFields (c$, function () {
34 this.groups =  new java.util.Vector ();
35 });
36 Clazz.makeConstructor (c$, 
37 function (seqs, odata, treefile) {
38 this.construct (seqs, treefile);
39 if (odata != null) {
40 this.seqData = odata;
41 }}, "~A,jalview.datamodel.AlignmentView,jalview.io.NewickFile");
42 Clazz.makeConstructor (c$, 
43 function (seqs, treefile) {
44 this.sequence = seqs;
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);
53 var i = 0;
54 var namesleft = seqs.length;
55 var j;
56 var nam;
57 var realnam;
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 ();
63 nam = null;
64 if (namesleft > -1) {
65 nam = algnIds.findIdMatch (realnam);
66 }if (nam != null) {
67 j.setElement (nam);
68 if (one2many.contains (nam)) {
69 countOne2Many++;
70 } else {
71 one2many.addElement (nam);
72 namesleft--;
73 }} else {
74 j.setElement ( new jalview.datamodel.Sequence (realnam, "THISISAPLACEHLDER"));
75 j.setPlaceholder (true);
76 }}
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 ();
82 this.type = type;
83 this.pwtype = pwtype;
84 if (seqData != null) {
85 this.seqData = seqData;
86 } else {
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);
90 }
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"))) {
95 type = "AV";
96 }if (sm == null && !(pwtype.equals ("PID"))) {
97 if (jalview.schemes.ResidueProperties.getScoreMatrix (pwtype) == null) {
98 pwtype = "BLOSUM62";
99 }}var i = 0;
100 this.done =  Clazz.newIntArray (sequence.length, 0);
101 while ((i < sequence.length) && (sequence[i] != null)) {
102 this.done[i] = 0;
103 i++;
104 }
105 this.noseqs = i++;
106 this.distance = this.findDistances (sm);
107 this.makeLeaves ();
108 this.noClus = this.$cluster.size ();
109 this.cluster ();
110 }, "~A,jalview.datamodel.AlignmentView,~S,~S,jalview.api.analysis.ScoreModelI,~N,~N");
111 Clazz.overrideMethod (c$, "toString", 
112 function () {
113 var fout =  new jalview.io.NewickFile (this.getTopNode ());
114 return fout.print (this.isHasBootstrap (), this.isHasDistances (), this.isHasRootDistance ());
115 });
116 Clazz.defineMethod (c$, "UpdatePlaceHolders", 
117 function (list) {
118 var leaves =  new java.util.Vector ();
119 this.findLeaves (this.top, leaves);
120 var sz = leaves.size ();
121 var seqmatcher = null;
122 var i = 0;
123 while (i < sz) {
124 var leaf = leaves.elementAt (i++);
125 if (list.contains (leaf.element ())) {
126 leaf.setPlaceholder (false);
127 } else {
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);
132 }
133 seqmatcher =  new jalview.analysis.SequenceIdMatcher (seqs);
134 }var nam = seqmatcher.findIdMatch (leaf.getName ());
135 if (nam != null) {
136 if (!leaf.isPlaceholder ()) {
137 }leaf.setPlaceholder (false);
138 leaf.setElement (nam);
139 } else {
140 if (!leaf.isPlaceholder ()) {
141 leaf.setElement ( new jalview.datamodel.Sequence (leaf.getName (), "THISISAPLACEHLDER"));
142 }leaf.setPlaceholder (true);
143 }}}
144 }, "java.util.List");
145 Clazz.defineMethod (c$, "renameAssociatedNodes", 
146 function () {
147 this.applyToNodes (((Clazz.isClassDefined ("jalview.analysis.NJTree$1") ? 0 : jalview.analysis.NJTree.$NJTree$1$ ()), Clazz.innerTypeInstance (jalview.analysis.NJTree$1, this, null)));
148 });
149 Clazz.defineMethod (c$, "cluster", 
150 function () {
151 while (this.noClus > 2) {
152 if (this.type.equals ("NJ")) {
153 this.findMinNJDistance ();
154 } else {
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);
160 this.noClus--;
161 }
162 var onefound = false;
163 var one = -1;
164 var two = -1;
165 for (var i = 0; i < this.noseqs; i++) {
166 if (this.done[i] != 1) {
167 if (onefound == false) {
168 two = i;
169 onefound = true;
170 } else {
171 one = i;
172 }}}
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);
178 });
179 Clazz.defineMethod (c$, "joinClusters", 
180 function (i, j) {
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];
187 }
188 for (var ii = noi; ii < (noi + noj); ii++) {
189 value[ii] = (this.$cluster.elementAt (j)).value[ii - noi];
190 }
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);
196 } else {
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);
205 } else {
206 this.findNewDistances (tmpi, tmpj, dist);
207 }tmpi.setParent (sn);
208 tmpj.setParent (sn);
209 this.node.setElementAt (sn, i);
210 return c;
211 }, "~N,~N");
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);
216 if (tmpi.dist < 0) {
217 tmpi.dist = 0;
218 }if (tmpj.dist < 0) {
219 tmpj.dist = 0;
220 }}, "jalview.datamodel.SequenceNode,jalview.datamodel.SequenceNode,~N");
221 Clazz.defineMethod (c$, "findNewDistances", 
222 function (tmpi, tmpj, dist) {
223 var ih = 0;
224 var jh = 0;
225 var sni = tmpi;
226 var snj = tmpj;
227 while (sni != null) {
228 ih = ih + sni.dist;
229 sni = sni.left ();
230 }
231 while (snj != null) {
232 jh = jh + snj.dist;
233 snj = snj.left ();
234 }
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", 
239 function (i, j) {
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);
246 } else {
247 newdist[l] = 0;
248 }}
249 for (var ii = 0; ii < this.noseqs; ii++) {
250 this.distance[i][ii] = newdist[ii];
251 this.distance[ii][i] = newdist[ii];
252 }
253 }, "~N,~N");
254 Clazz.defineMethod (c$, "findClusterNJDistance", 
255 function (i, j) {
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;
260 } else {
261 newdist[l] = 0;
262 }}
263 for (var ii = 0; ii < this.noseqs; ii++) {
264 this.distance[i][ii] = newdist[ii];
265 this.distance[ii][i] = newdist[ii];
266 }
267 }, "~N,~N");
268 Clazz.defineMethod (c$, "findr", 
269 function (i, j) {
270 var tmp = 1;
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];
274 }}
275 if (this.noClus > 2) {
276 tmp = tmp / (this.noClus - 2);
277 }return tmp;
278 }, "~N,~N");
279 Clazz.defineMethod (c$, "findMinNJDistance", 
280 function () {
281 var min = 100000;
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));
286 if (tmp < min) {
287 this.mini = i;
288 this.minj = j;
289 min = tmp;
290 }}}
291 }
292 return min;
293 });
294 Clazz.defineMethod (c$, "findMinDistance", 
295 function () {
296 var min = 100000;
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) {
301 this.mini = i;
302 this.minj = j;
303 min = this.distance[i][j];
304 }}}
305 }
306 return min;
307 });
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);
316 return distance;
317 }, "jalview.api.analysis.ScoreModelI");
318 Clazz.defineMethod (c$, "makeLeaves", 
319 function () {
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);
327 value[0] = i;
328 var c =  new jalview.analysis.Cluster (value);
329 this.$cluster.addElement (c);
330 }
331 });
332 Clazz.defineMethod (c$, "findLeaves", 
333 function (node, leaves) {
334 if (node == null) {
335 return leaves;
336 }if ((node.left () == null) && (node.right () == null)) {
337 leaves.addElement (node);
338 return leaves;
339 } else {
340 this.findLeaves (node.left (), leaves);
341 this.findLeaves (node.right (), leaves);
342 }return leaves;
343 }, "jalview.datamodel.SequenceNode,java.util.Vector");
344 Clazz.defineMethod (c$, "findLeaf", 
345 function (node, count) {
346 this.found = this._findLeaf (node, count);
347 return this.found;
348 }, "jalview.datamodel.SequenceNode,~N");
349 Clazz.defineMethod (c$, "_findLeaf", 
350 function (node, count) {
351 if (node == null) {
352 return null;
353 }if (node.ycount == count) {
354 this.found = node.element ();
355 return this.found;
356 } else {
357 this._findLeaf (node.left (), count);
358 this._findLeaf (node.right (), count);
359 }return this.found;
360 }, "jalview.datamodel.SequenceNode,~N");
361 Clazz.defineMethod (c$, "printNode", 
362 function (node) {
363 if (node == null) {
364 return;
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 ());
369 } else {
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", 
375 function (node) {
376 if (node == null) {
377 return;
378 }if ((node.left () == null) && (node.right () == null)) {
379 var dist = node.dist;
380 if (dist > this.maxDistValue) {
381 this.maxdist = node;
382 this.maxDistValue = dist;
383 }} else {
384 this.findMaxDist (node.left ());
385 this.findMaxDist (node.right ());
386 }}, "jalview.datamodel.SequenceNode");
387 Clazz.defineMethod (c$, "getGroups", 
388 function () {
389 return this.groups;
390 });
391 Clazz.defineMethod (c$, "getMaxHeight", 
392 function () {
393 return this.maxheight;
394 });
395 Clazz.defineMethod (c$, "groupNodes", 
396 function (node, threshold) {
397 if (node == null) {
398 return;
399 }if ((node.height / this.maxheight) > threshold) {
400 this.groups.addElement (node);
401 } else {
402 this.groupNodes (node.left (), threshold);
403 this.groupNodes (node.right (), threshold);
404 }}, "jalview.datamodel.SequenceNode,~N");
405 Clazz.defineMethod (c$, "findHeight", 
406 function (node) {
407 if (node == null) {
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) {
412 return node.height;
413 } else {
414 return this.maxheight;
415 }} else {
416 if (node.parent () != null) {
417 node.height = (node.parent ()).height + node.dist;
418 } else {
419 this.maxheight = 0;
420 node.height = 0.0;
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", 
426 function () {
427 if (this.maxdist != null) {
428 this.ycount = 0;
429 var tmpdist = this.maxdist.dist;
430 var sn =  new jalview.datamodel.SequenceNode ();
431 sn.setParent (null);
432 var snr = this.maxdist.parent ();
433 this.changeDirection (snr, this.maxdist);
434 System.out.println ("Printing reversed tree");
435 this.printN (snr);
436 snr.dist = tmpdist / 2;
437 this.maxdist.dist = tmpdist / 2;
438 snr.setParent (sn);
439 this.maxdist.setParent (sn);
440 sn.setRight (snr);
441 sn.setLeft (this.maxdist);
442 this.top = sn;
443 this.ycount = 0;
444 this.reCount (this.top);
445 this.findHeight (this.top);
446 }return this.top;
447 });
448 Clazz.defineMethod (c$, "hasOriginalSequenceData", 
449 function () {
450 return this.seqData != null;
451 });
452 Clazz.defineMethod (c$, "printOriginalSequenceData", 
453 function (gapChar) {
454 if (this.seqData == null) {
455 return 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");
461 }
462 return sb.toString ();
463 }, "~S");
464 Clazz.defineMethod (c$, "printN", 
465 function (node) {
466 if (node == null) {
467 return;
468 }if ((node.left () != null) && (node.right () != null)) {
469 this.printN (node.left ());
470 this.printN (node.right ());
471 } else {
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", 
476 function (node) {
477 this.ycount = 0;
478 this._lycount = 0;
479 this._reCount (node);
480 }, "jalview.datamodel.SequenceNode");
481 Clazz.defineMethod (c$, "_reCount", 
482 function (node) {
483 if (node == null) {
484 return;
485 }this._lycount++;
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;
493 } else {
494 node.count = 1;
495 node.ycount = this.ycount++;
496 }this._lycount--;
497 }, "jalview.datamodel.SequenceNode");
498 Clazz.defineMethod (c$, "swapNodes", 
499 function (node) {
500 if (node == null) {
501 return;
502 }var tmp = node.left ();
503 node.setLeft (node.right ());
504 node.setRight (tmp);
505 }, "jalview.datamodel.SequenceNode");
506 Clazz.defineMethod (c$, "changeDirection", 
507 function (node, dir) {
508 if (node == null) {
509 return;
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);
515 node.setLeft (tmp);
516 } else if (dir === node.right ()) {
517 node.setParent (dir);
518 node.setRight (tmp);
519 }} else {
520 if (dir === node.left ()) {
521 node.setParent (node.left ());
522 if (this.top.left () === node) {
523 node.setRight (this.top.right ());
524 } else {
525 node.setRight (this.top.left ());
526 }} else {
527 node.setParent (node.right ());
528 if (this.top.left () === node) {
529 node.setLeft (this.top.right ());
530 } else {
531 node.setLeft (this.top.left ());
532 }}}}, "jalview.datamodel.SequenceNode,jalview.datamodel.SequenceNode");
533 Clazz.defineMethod (c$, "getMaxDist", 
534 function () {
535 return this.maxdist;
536 });
537 Clazz.defineMethod (c$, "getTopNode", 
538 function () {
539 return this.top;
540 });
541 Clazz.defineMethod (c$, "isHasDistances", 
542 function () {
543 return this.hasDistances;
544 });
545 Clazz.defineMethod (c$, "isHasBootstrap", 
546 function () {
547 return this.hasBootstrap;
548 });
549 Clazz.defineMethod (c$, "isHasRootDistance", 
550 function () {
551 return this.hasRootDistance;
552 });
553 Clazz.defineMethod (c$, "applyToNodes", 
554 function (nodeTransformI) {
555 for (var nodes = this.node.elements (); nodes.hasMoreElements (); nodeTransformI.transform (nodes.nextElement ())) {
556 ;}
557 }, "jalview.datamodel.NodeTransformI");
558 c$.$NJTree$1$ = function () {
559 Clazz.pu$h ();
560 c$ = Clazz.declareAnonymous (jalview.analysis, "NJTree$1", null, jalview.datamodel.NodeTransformI);
561 Clazz.defineMethod (c$, "transform", 
562 function (node) {
563 var el = node.element ();
564 if (el != null && Clazz.instanceOf (el, jalview.datamodel.SequenceI)) {
565 node.setName ((el).getName ());
566 }}, "jalview.datamodel.BinaryNode");
567 c$ = Clazz.p0p ();
568 };
569 c$ = Clazz.decorateAsClass (function () {
570 this.value = null;
571 Clazz.instantialize (this, arguments);
572 }, jalview.analysis, "Cluster");
573 Clazz.makeConstructor (c$, 
574 function (value) {
575 this.value = value;
576 }, "~A");
577 });