This file is indexed.

/usr/share/hyphy/TemplateBatchFiles/TreeTools.ibf is in hyphy-common 2.2.7+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*************************************************************************************/

function PostOrderAVL2StringDL (theAVL, doLengths)
{
	return PostOrderAVL2StringAnnotate (theAVL, doLengths, "");
}

/*************************************************************************************/

function PostOrderAVL2StringAnnotate (theAVL, doLengths,label)
{
	return PostOrderAVL2StringAnnotateAux (theAVL, doLengths, label, "[]");
}

/*************************************************************************************/

function PostOrderAVL2StringAnnotateAux (theAVL, doLengths, label, chars)
{
	_ost = "";
	_ost * 256;
	
	lastLevel = 0;
	treeSize  = Abs(theAVL);
	treeInfo  = theAVL[0];
	rootIndex = treeInfo["Root"];
	
	for (nodeIndex = 1; nodeIndex < treeSize; nodeIndex = nodeIndex + 1)
	{
        nodeInfo = theAVL[nodeIndex];
        myDepth = nodeInfo["Depth"];
        if (lastDepth < myDepth)
        {
            if (lastDepth)
            {
                _ost * ",";
            }
            for (pidx = lastDepth; pidx < myDepth; pidx = pidx + 1)
            {
                _ost * "(";
            }
        }
        else
        {
            if (lastDepth > myDepth)
            {
                for (pidx = myDepth; pidx < lastDepth; pidx = pidx + 1)
                {
                    _ost * ")";
                }				
            }
            else
            {
                _ost * ",";
            }
        }
        
        _ost * nodeInfo["Name"];
        
        if (Abs (label))
        {
            if (Abs(nodeInfo[label]))
            {
                _ost * (chars[0] + nodeInfo[label] + chars[1]);
            }
        }

        if (doLengths)
        {
            if (nodeIndex < treeSize - 1)
            {
                _ost * ":";
                _ost * (""+nodeInfo ["Length"]); 
            }
        }
        lastDepth = myDepth;
	}
	
	_ost * 0;
	return _ost;
}


/*************************************************************************************/

function PostOrderAVL2String (theAVL)
{
	return PostOrderAVL2StringDL(theAVL, 0);
}

/*************************************************************************************/

function PostOrderAVL2StringDistances (theAVL, distAVL)
{
	_ost = "";
	_ost * 256;
	
	lastLevel = 0;
	treeSize  = Abs(theAVL);
	treeInfo  = theAVL[0];
	rootIndex = treeInfo["Root"];
	
	for (nodeIndex = 1; nodeIndex < treeSize; nodeIndex+=1)
	{
        nodeInfo = theAVL[nodeIndex];
        myDepth = nodeInfo["Depth"];
        myName 	= nodeInfo["Name"];
        
        if (lastDepth < myDepth)
        {
            if (lastDepth)
            {
                _ost * ",";
            }
            for (pidx = lastDepth; pidx < myDepth; pidx = pidx + 1)
            {
                _ost * "(";
            }
        }
        else
        {
            if (lastDepth > myDepth)
            {
                for (pidx = myDepth; pidx < lastDepth; pidx = pidx + 1)
                {
                    _ost * ")";
                }				
            }
            else
            {
                _ost * ",";
            }
        }
        if (nodeIndex != rootIndex) {
            _ost * myName;
        }
        if (nodeIndex < treeSize - 1)
        {
            _ost * ":";
            _ost * (""+distAVL [myName]); 
        }
        lastDepth = myDepth;
	}
	
	_ost * 0;
	return _ost;
}

/*************************************************************************************/

function KillInternalZeroBranchLengths (treeAVL)
{
	treeSize   = Abs(treeAVL);
	if (treeSize == 3)
	{
		return "(" + (treeAVL[0])["Name"] + "," + (treeAVL[1])["Name"] + ")";
	}
	newTreeAVL = {};
	oldIndexMap= {treeSize,1};
	index2	   = {treeSize,1};
	newDAVL	   = {};
	newTreeAVL [0] = treeAVL[0];
	allDeleted = 0;
	for (nodeIndex = 1; nodeIndex < treeSize; nodeIndex = nodeIndex + 1)
	{
		newDAVL [(treeAVL[nodeIndex])["Name"]] = (treeAVL[nodeIndex])["Length"];
		
		if (Abs((treeAVL[nodeIndex])["Children"]) && Abs((treeAVL[nodeIndex])["Length"]) <= 1e-10 && (treeAVL[nodeIndex])["Parent"]) 
			/* zero internal branch */
		{
			oldIndexMap[nodeIndex] = -(treeAVL[nodeIndex])["Parent"];
			allDeleted			 = allDeleted + 1;
		}
		else
		{
			newTreeAVL [nodeIndex-allDeleted] = treeAVL[nodeIndex];
			oldIndexMap[nodeIndex]   			= nodeIndex-allDeleted;
			index2 [nodeIndex-allDeleted]		= nodeIndex;
		}
	}
	
	if (allDeleted)
	{
		markedIndices = {};
		for (nodeIndex = treeSize-1; nodeIndex>0; nodeIndex = nodeIndex - 1)
		{
			if (oldIndexMap [nodeIndex]<0)
			{
				markedIndices[nodeIndex] = 1;
				oldIndexMap[nodeIndex] = oldIndexMap[-oldIndexMap[nodeIndex]]; 
			}
		}
		treeSize = Abs (newTreeAVL);
		for (nodeIndex = 1; nodeIndex<treeSize; nodeIndex = nodeIndex + 1)
		{
			meParent = (newTreeAVL[nodeIndex])["Parent"];
			_cc		 = Abs((newTreeAVL[nodeIndex])["Children"]);
			if (_cc > 0)
			{
				newChildrenMap = {};
				for (_cci = 0; _cci < _cc; _cci = _cci+1)
				/* map children to new indices */
				{				
					_cn = ((newTreeAVL[nodeIndex])["Children"])[_cci];
					if (markedIndices[_cn] == 0)
					{
						newChildrenMap[Abs(newChildrenMap)] = oldIndexMap[_cn];
					}
				}
				((newTreeAVL[nodeIndex])["Children"]) = newChildrenMap;
			}
					
			if (meParent > 0)
			{
				meParentOI = meParent;
				meParent = oldIndexMap[meParent];
				(newTreeAVL[nodeIndex])["Parent"] = meParent;
				if (markedIndices[meParentOI])
				{
					/*
					fprintf (stdout, "Insert ", (newTreeAVL[nodeIndex])["Name"], " as a child of ", (newTreeAVL[meParent])["Name"], " index ", index2[nodeIndex], "(", oldIndexMap[index2[nodeIndex]], ",", nodeIndex,")\n");
					*/
					((newTreeAVL[meParent])["Children"])[Abs((newTreeAVL[meParent])["Children"])] = index2[nodeIndex];
				}
			}

		}
		
		
		for (nodeIndex = treeSize-1; nodeIndex>0; nodeIndex = nodeIndex - 1)
		{
			_cc		 = Abs((newTreeAVL[nodeIndex])["Children"]);
			if (_cc > 0)
			{
				_cd = (newTreeAVL[nodeIndex])["Depth"] + 1;
				for (_cci = 0; _cci < _cc; _cci = _cci+1)
				{				
					_cn =  ((newTreeAVL[nodeIndex])["Children"])[_cci];
					/*fprintf (stdout, (newTreeAVL[_cn])["Name"], ":", (newTreeAVL[_cn])["Depth"], "=>", _cd, "\n");*/
					(newTreeAVL[_cn])["Depth"] = _cd;
				}
			}
		}

		(newTreeAVL[0])["Root"] = treeSize-1;
	}
	
	return PostOrderAVL2StringDistances (newTreeAVL, newDAVL);
}


/*************************************************************************************/

function TreeAVL2String (treeAVL)
{
	rootNode = treeAVL[0];
	rootNode = rootNode["Root"];
	return subtreeAVLStr (rootNode,0,0);
}


/*************************************************************************************/

function subtreeAVLStr (nodeIndex,k,treeString)
{
	nodeInfo = treeAVL[nodeIndex];
	k = Abs(nodeInfo["Children"])-1;
	if (k>=0)
	{
		while (k>=0)
		{
			nodeInfo = treeAVL[nodeIndex];
			cNodes = nodeInfo["Children"];
			cNodes = cNodes[k];
			if (k < Abs(nodeInfo["Children"])-1)
			{
				ExecuteCommands("treeString=subtreeAVLStr (cNodes,k,treeString)+\",\"+treeString;");
			}
			else
			{
				ExecuteCommands("treeString=subtreeAVLStr (cNodes,k,treeString)+\")\";");
			}
			k=k-1;
		}
		return "("+treeString;
	}
	else
	{
		callLevel = callLevel - 1;
		return nodeInfo["Name"];
	}
}

/*************************************************************************************/

function InsertANode (theAVL&,insertAt,newNodeName)
{
	nodeInfo = theAVL[insertAt];
	if (Abs(nodeInfo))
	{
		nparent = nodeInfo["Parent"];
		if (nparent > 0)
		{
			lastIndex = Abs(theAVL);
			myDepth = nodeInfo["Depth"];
			newParentNode = {};
			newParentNode ["Name"] = "Node"+lastIndex;
			newParentNode ["Parent"] = nparent;
			newParentNode ["Depth"] = myDepth;
			
			newChildNode = {};
			newChildNode ["Name"] = newNodeName;
			newChildNode ["Parent"] = lastIndex;
			newChildNode ["Depth"] = myDepth + 1;
			
			pChildren = {};
			pChildren [0] = insertAt;
			pChildren [1] = lastIndex+1;
			newParentNode ["Children"] = pChildren;
			
			theAVL[lastIndex] = newParentNode;
			theAVL[lastIndex+1] = newChildNode;

			/* update the parent*/

			nodeInfo ["Parent"] = lastIndex;
			theAVL[insertAt] = nodeInfo;
			
			/* update the list of children at the parent node*/
			
			parentInfo = theAVL[nparent];
			parentChildren = parentInfo["Children"];
			
			for (nic = Abs(parentChildren)-1; nic >= 0; nic = nic-1)
			{
				if (parentChildren[nic] == insertAt)
				{
					break;
				}
			}

			parentChildren[nic] = lastIndex;
			parentInfo["Children"] = parentChildren;
			theAVL[nparent] = parentInfo;
			
			/* now update the depths at new NodeName and all of its children */
			
			nodeCache    = {};
			nodeCache[0] = insertAt;
			cacheIndex   = 0;
			
			while (cacheIndex <= Abs(nodeCache))
			{
				nparent 			= nodeCache[cacheIndex];
				nodeInfo 			= theAVL[nparent];
				nodeInfo["Depth"] 	= nodeInfo["Depth"] + 1;
				theAVL[nparent] 	= nodeInfo;
				nodeChildren 		= nodeInfo["Children"];
				for (nic = Abs(nodeChildren)-1; nic >=0; nic = nic-1)
				{
					nodeCache [Abs(nodeCache)] = nodeChildren[nic];
				}
				cacheIndex = cacheIndex + 1;
			}
			
			nodeCache = 0;
		}
	}
	return 0;
}

/*************************************************************************************/

function	ModifyDepth (nIndex, modAmount)
{
	nodeInfo = theAVL[nIndex];
	nodeInfo ["Depth"] = nodeInfo ["Depth"] + modAmount;
	theAVL[nIndex] = nodeInfo;

}

/*************************************************************************************/

function	echoAVL (anAVL)
{
	for (k=1; k<Abs(anAVL); k=k+1)
	{
		nodeInfo 			= anAVL[k];
		myChildren			= nodeInfo["Children"];
		if (Abs(myChildren))
		{
			fprintf (stdout, "Index ", k, ":", nodeInfo["Name"], " : parent = ", nodeInfo["Parent"], " children:");
			for (k2 = 0; k2 < Abs(myChildren); k2=k2+1)
			{
				fprintf (stdout,"\t", myChildren[k2]);
			}
			fprintf (stdout, " depth: ", nodeInfo["Depth"], "\n");
		
		}
		else
		{
			fprintf (stdout, "Index ", k, ":", nodeInfo["Name"], " : parent = ", nodeInfo["Parent"], " children: none, depth: ", nodeInfo["Depth"], "\n");
		}
	}
	return 0;
}

/*************************************************************************************/

function	selectATreeBranch (treeID, title)
{
	ExecuteCommands ("internalNodes = BranchCount(`treeID`);");
	ExecuteCommands ("leafNodes     = TipCount(`treeID`);");

	choiceMatrix  = {internalNodes+leafNodes,2};
	
	for (bc=0; bc<internalNodes; bc=bc+1)
	{
		ExecuteCommands ("choiceMatrix[bc][0] = BranchName(`treeID`,bc);choiceMatrix[bc][1] = \"Internal Branch Rooting \" + `treeID`[bc];");
	}
	
	for (bc=0; bc<leafNodes; bc=bc+1)
	{
		ExecuteCommands ("choiceMatrix[bc+internalNodes][0] = TipName(`treeID`,bc);");
		choiceMatrix[bc+internalNodes][1] = "Terminal branch endin in " + choiceMatrix[bc+internalNodes][0];
	}
	
	mxTreeSpec = {5,1};
	mxTreeSpec [0] = treeID;
	mxTreeSpec [1] = "8240";
	mxTreeSpec [2] = "10,40,-10,-175,1";
	mxTreeSpec [3] = "";
	mxTreeSpec [4] = "";
	OpenWindow (TREEWINDOW, mxTreeSpec);
	
	ChoiceList  (bOption,title,1,NO_SKIP,choiceMatrix);
	
	if (bOption < 0)
	{
		return "";
	}
	return choiceMatrix[bOption][0];
}

/*************************************************************************************/

function	computeTreeSplits (treeID,mirror,leafMap)
{
	ExecuteCommands ("_treeAVL		= `treeID`^0;");
	ExecuteCommands ("_leafNodes     = TipCount(`treeID`);");
	_tipMap			= {};
	_splitMap		= {};
	_splitTemplate = {_leafNodes,1};
	_hasLeafMap	   = Abs (leafMap);
	
	for (_k = 1; _k < Abs(_treeAVL); _k = _k+1)
	{
		if (Abs((_treeAVL[_k])["Children"]) == 0)
		{
			if (_hasLeafMap)
			{
				_tipMap [_k] = leafMap [(_treeAVL[_k])["Name"]];
			}
			else
			{
				_tipMap [_k] = Abs(_tipMap);
			}
		}
		(_treeAVL[_k])["Split"]		   = _splitTemplate;
	}
	
	
	for (_k = 1; _k < Abs(_treeAVL); _k = _k+1)
	{
		_pc = (_treeAVL[_k])["Parent"];
		if (_pc)
		{
			_cc = Abs((_treeAVL[_k])["Children"]);
			if (_cc)
			{
				_mySplit                 = ((_treeAVL[_k])["Split"])["_MATRIX_ELEMENT_VALUE_>0"];
				(_treeAVL[_pc])["Split"] = (_treeAVL[_pc])["Split"] + _mySplit;
			}
			else
			{
				((_treeAVL[_k])["Split"])[_tipMap[_k]] = 1;
				_mySplit = (_treeAVL[_k])["Split"];
				((_treeAVL[_pc])["Split"])[_tipMap[_k]] = 1;
			}
			_splitMap[_mySplit]		  = (_treeAVL[_k])["Name"];
		}
	}
	
	
	_uniqueSplits = Rows(_splitMap);
	_splitStrings = {};
	for (_k = 0; _k < Abs(_splitMap); _k = _k+1)
	{
		_stringKey = ""; _stringKey * 128;
		_stringKey2 = ""; _stringKey2 * 128;
		ExecuteCommands ("_thisKey   = " + _uniqueSplits[_k]);
		for (_k2 = 0; _k2 < _leafNodes; _k2 = _k2 + 1)
		{
			if (_thisKey[_k2])
			{
				_stringKey * "*";
				_stringKey2 * "-";
			}
			else
			{
				_stringKey * "-";
				_stringKey2 * "*";
			}
		}
		_stringKey * 0;_stringKey2 * 0;
		_splitStrings [_stringKey] = _splitMap[_uniqueSplits[_k]];
		if (mirror)
		{
			_splitStrings [_stringKey2] = _splitStrings [_stringKey];
		}
	}
	DeleteObject (_splitMap);
	return _splitStrings;
}

/*************************************************************************************/

/* 
   return the most recent common ancestor for a group 
   of nodes in an AVL; returns the index of the MRCA
*/

function	_findMRCA (treeAVL, nodeIDList)
{
	_nodeCount	 = Rows(nodeIDList)*Columns(nodeIDList);
	
	if (_nodeCount)
	{
		nodeIDList    = nodeIDList % 0;
		_highestNode  = (treeAVL[nodeIDList[0]])["Depth"];
		_currentDepth = _highestNode;
		_lastNode	  = nodeIDList[_nodeCount-1];
		for (_nodeIterator = nodeIDList[0]+1; _nodeIterator <= _lastNode; 
											  _nodeIterator  = _nodeIterator + 1)
		{
			_currentDepth = (treeAVL[_nodeIterator])["Depth"];
			if (_currentDepth < _highestNode)
			{
				_highestNode   = _currentDepth;
			}
		}
				
		for (; _nodeIterator <= Abs(treeAVL); _nodeIterator = _nodeIterator+1)
		{
			if ((treeAVL[_nodeIterator])["Depth"] < _highestNode)
			{
				return _nodeIterator;
			}
		} 
		
	}
	
	return Abs(treeAVL)-1;
}

/*************************************************************************************/
/* 
  	use parsimony to reconstruct ancestral states based on leaf AVL labels 
  	return the number of substitutions
*/

function _parsimonyAncestralMapping (treeAVL&, label)
{
	uniqueLabels = {};
	idToLabel	 = {};
	for (_nodeIterator = 1; _nodeIterator < Abs (treeAVL); _nodeIterator = _nodeIterator + 1)
	{
		isLeaf = Abs((treeAVL[_nodeIterator])["Children"]) == 0;
		if (isLeaf)
		{
			isLeaf = (treeAVL[_nodeIterator])[label];
			if (uniqueLabels[isLeaf] == 0)
			{
				uniqueLabels [isLeaf]     = 1+Abs(uniqueLabels);
				idToLabel[Abs(idToLabel)] = isLeaf;
			}
		}
	}

	assignmentMatrices = {};
	kindCount		   = Abs (uniqueLabels);
	
	for (_nodeIterator = 1; _nodeIterator < Abs (treeAVL); _nodeIterator = _nodeIterator + 1)
	{
		aMx = {2,kindCount};
		s2  = uniqueLabels[(treeAVL[_nodeIterator])[label]]-1;
		for (k=0; k<kindCount; k=k+1)
		{
			aMx[0][k] = s2;
			aMx[1][k] = 1-(k==s2);	
		}
		assignmentMatrices [(treeAVL[_nodeIterator])["Name"]] = aMx;
	}

	for (_nodeIterator = 1; _nodeIterator < Abs (treeAVL); _nodeIterator = _nodeIterator + 1)
	{
		nodeInfo 		= treeAVL[_nodeIterator];
		nodeChildren	= nodeInfo ["Children"];
		cCount			= Abs(nodeChildren);
		
		if (cCount)
		{
			localMatrices = {};
			
			nodeName = nodeInfo["Name"];
			
			for (s1 = 0; s1<cCount; s1=s1+1)
			{
				localMatrices[s1] = assignmentMatrices[(treeAVL	 [nodeChildren[s1]])  ["Name"]];
			}
			
			twoWay = {kindCount,1};
			
			for (s2 = 0; s2 < kindCount; s2 = s2+1)
			{
				lc = 0;
				for (s3 = 0; s3<cCount; s3=s3+1)
				{
					lc  = lc + (localMatrices[s3])[1][s2];
				}
				twoWay[s2] = lc;
			}
						
			if (nodeInfo["Parent"])
			{
				aMx = {2,kindCount};
				
				for (s2 = 0; s2 < kindCount; s2 = s2+1)
				{
					minV = 1e100;
					minI = 0;
					
					for (s3 = 0; s3 < kindCount; s3 = s3+1)
					{
						aCost = (s3!=s2) + twoWay[s3];
						if (minV > aCost)
						{
							minV  = aCost;
							minI  = s3;
						}	
					}
					
					aMx[0][s2] = minI;
					aMx[1][s2] = minV;
				}
				
				assignmentMatrices [nodeName] = aMx;
			}
			else
			{
				totalCost = 1e100;
				rootState = 0;
				
				for (s2 = 0; s2 < kindCount; s2 = s2+1)
				{
					if (twoWay[s2] < totalCost)
					{
						totalCost = twoWay[s2];
						rootState = s2;
					}	
				}
				(treeAVL[_nodeIterator])[label] = idToLabel[rootState];
			}
		}
	}
	
	for (_nodeIterator = Abs (treeAVL)-1; _nodeIterator >=1 ; _nodeIterator = _nodeIterator - 1)
	{
		nodeInfo 		= treeAVL[_nodeIterator];
		nodeChildren	= nodeInfo ["Children"];
		
		if (Abs(nodeChildren))
		{			
			nodeName   = nodeInfo["Name"];
			nodeParent = nodeInfo["Parent"];
			if (nodeParent)
			{
				aMx							    = assignmentMatrices [nodeName];
				(treeAVL[_nodeIterator])[label] = idToLabel[aMx[0][uniqueLabels[(treeAVL[nodeParent])[label]]-1]];
			}
		}
	}	

	assignmentMatrices = 0;
	return totalCost;
}



/*************************************************************************************/
/* 
  	Center of Tree (COT) calculation 
*/

function ComputeCOT (_treeID, _filePath)
/* IN 	: the identifier of an existing tree variable (String)
		  _filePath : if not an empty string, write a PostScript image with this tree
		  			  to the given path
		  			  
   OUT	: an associative array with four entries
   		  "Branch" 		- the branch where the COT resides
   		  				  branch ENDS at the node whose value is returned 
   		  				  
   		  "Split"  		- how far up the branch the COT is, measured in the same units as branch lengths
   		  				  from the END of the branch (i.e node) 
   		  				  
   		  "COTTree"		- tree rerooted at the COT
   		  
   		  "Distances"	- mean squared distance from the COT to all leaves		  
*/ 
{
	_power = 2;
					 /* '2' is the power of the distance function to 
						   minimize - i.e. least squares in this case */
	ExecuteCommands (
	"_cot = Min ("+_treeID+",_power);");
	
	_returnList = {};
	_returnList ["Branch"] = _cot["COT_NODE"];
	_returnList ["Split"]  = _cot["COT_SPLIT"];
	_meanD = 0;
	_keys  = Rows(_cot["COT_TO_NODE"]); 
		/* _cot["COT_TO_NODE"] is an associative array
		   mapping tree nodes to the total distance from COT (linear distance)
		*/ 

	for (_k = 0; _k < Columns(_keys); _k=_k+1)
	{
		_meanD = _meanD + (_cot["COT_TO_NODE"])[_keys[_k]]^_power;
	}

	_returnList     ["Distances"]  = _meanD/Columns(_keys);
	/* reroot the tree at the COT branch */
	ExecuteCommands ("_rerootedTree = RerootTree ("+_treeID+", \""+_returnList ["Branch"]+"\");");
	/* hackish lines below split the root branch into appropriate bits */
	
	ACCEPT_ROOTED_TREES = 1;
	UseModel (USE_NO_MODEL);
	Tree _temp = _rerootedTree;
	_tempA = _temp^0; /* this converts the tree into a post-order list of nodes as an associative array; 
						print it to see the structure */					       
	_rootID	   = (_tempA[0])["Root"];
	_dist	   = {};
	for (_k = 1; _k < Abs (_tempA); _k = _k + 1)
	{
		if ((_tempA[_k])["Parent"] == _rootID)
		{
			if (_k == _rootID - 1)
			{
				_dist [(_tempA[_k])["Name"]] = _cot["COT_SPLIT"];
			}
			else
			{
				_dist [(_tempA[_k])["Name"]] = _cot["COT_BRANCH_LENGTH"]-_cot["COT_SPLIT"];			
			}
		}
		else
		{
			_dist [(_tempA[_k])["Name"]] = (_tempA[_k])["Length"];
		}
	}
	
	_returnList["COTTree"] = PostOrderAVL2StringDistances (_tempA,_dist);
	
	/* make a postscript file if needed */
	if (Abs(_filePath))
	{
		TREE_OUTPUT_OPTIONS = {};
		nodeSpec = {};
		nodeSpec ["TREE_OUTPUT_BRANCH_SPLIT"]		= _cot["COT_SPLIT"]/_cot["COT_BRANCH_LENGTH"];	
		TREE_OUTPUT_OPTIONS [_cot["COT_NODE"]] 		= nodeSpec;
		ExecuteCommands 							("psString = PSTreeString ("+_treeID+",\"STRING_SUPPLIED_LENGTHS\",{{-1,-1}});");
		fprintf 									(_filePath, CLEAR_FILE, psString);
	}

	return _returnList;
	
}


/*************************************************************************************/
/* 
    Computes the distance from each node to the root of the tree.
    By default, uses the "Length" key, but this can be redefined by passing an argument  
*/

function PathDistanceToRoot (_treeAVL, _distanceKey)
/* IN 	: the identifier of a post-order tree AVL (dictionary)
		  _distanceKey : if not an empty string, use this key to retrieve branch lengths
          		  			  
   OUT	: an associative array keyed on branch names with paths stored as values
                
*/ 
{
    _distances = {};
    
    if (Abs (_distanceKey) == 0)
    {
        _distanceKey = "Length";
    }
    
    for (_k = Abs (_treeAVL)-2; _k > 0; _k = _k-1)
    {
        _nodeName   = (_treeAVL[_k])["Name"];
        _distances[_nodeName] = _distances[(_treeAVL[(_treeAVL[_k])["Parent"]])["Name"]] + (_treeAVL[_k])[_distanceKey];
    }
    
    return _distances;
}