avTreeCount = Rows ("Tree");

branchColors  = {};
branchLabels  = {};
branchRates   = {};
branchLengths = {};

branchColors [1] = {{255,0,0}}*(1/255); 		/* red */
branchColors [2] = {{64,0,128}}*(1/255); 		/* eggplant */
branchColors [0] = {{0,128,0}}*(1/255);   		/* clover */
branchColors [3] = {{240,240,240}}*(1/255);   	/* light gray */

branchLabels["CHIMP_BONOBO_GORILLA"] = "Chimpanzee/bonobo/gorilla";
branchLabels["ORANGUTAN_PPY"] 		 = "Orangutan";
branchLabels["GIBBON_GGO"] 			 = "Lar gibbon";
branchLabels["COLOBUS_CGU_CAN"] 	 = "Angolan colobus";
branchLabels["LANGUR_SEN_SVE"]		 = "Hanuman langur";
branchLabels["LANGUR_TOB_TFR"]		 = "Dusky langur";
branchLabels["DOUC_LANGUR_PNE"]		 = "Douc langur";
branchLabels["PROBISCIS_NLA"]		 = "Proboscis monkey";
branchLabels["BABOON_PCY"]		 	 = "Olive baboon";
branchLabels["MANGABEY_CAT"]		 = "Sooty mangabey";
branchLabels["RHESUS_MMU"]			 = "Rhesus macaque";
branchLabels["ALLEN_ANI"]			 = "Allen's monkey";
branchLabels["TALAPOIN_MTA"]		 = "Talapoin";
branchLabels["PATAS_EPA"]		 	 = "Patas monkey";
branchLabels["VERVET_CAE"]		 	 = "Vervet";
branchLabels["HUMAN"]		 	 	 = "Human";
branchLabels["SQUIRREL_M"]		 	 = "Squirrel monkey";
branchLabels["TAMARIN_SOE"]		 	 = "Tamarin";
branchLabels["MARMOSET_CJA"]		 = "Marmoset";

branchLabels["PNE_LANGUR"] 			 = "Douc langur";
branchLabels["CGU_CAN_COLOBUS"] 	 = "Angolan colobus";
branchLabels["MMU_RHESUS"] 			 = "Rhesus macaque";
branchLabels["HLA_GIBBON"] 	 		 = "Lar gibbon";
branchLabels["HSA_HUMAN"]		 	 = "Human";
branchLabels["SSC_SQUIRRELM"]		 = "Squirrel monkey";
branchLabels["CJA_MARMOSET"]		 = "Marmoset";


TREE_OUTPUT_OPTIONS = {};
/*TREE_OUTPUT_OPTIONS {""} = */
if (avTreeCount>=1)
{
	choices = {avTreeCount,2};
	for (k=0; k<avTreeCount;k=k+1)
	{
		GetString (tName, Tree, k);
		choices[k][0] = tName;
		choices[k][1] = "Tree "+tName;
	}
	
	ChoiceList (tIndex,"Color This Tree:",1,SKIP_NONE,choices);
	if (tIndex>=0)
	{
		SetDialogPrompt ("Over branch labels file:");
		fscanf (PROMPT_FOR_FILE,"Raw",ooo);
		ExecuteCommands (ooo);
		
		GetString (tName, Tree, tIndex);
		ExecuteCommands ("tree_bn=BranchName("+tName+",-1);tree_lng=BranchLength("+tName+",-1);");
		totalTreeLength = 0;
		iNodeCounter = 1;
		for (k=0; k<Columns(tree_bn)-1; k=k+1)
		{
			nodeSpec  = {};
			nodeName  = tree_bn [k]&&1;
			nodeLabel = branchLabels[nodeName];
			if (Abs(nodeLabel))
			{
				nodeSpec ["TREE_OUTPUT_BRANCH_LABEL"] = "__FONT_SIZE__ 2 idiv\n__FONT_SIZE__ 3 idiv\nneg\nrmoveto\n("+nodeLabel+") show";
			}
			else
			{
				if (nodeName/"NODE*")
				{
					nodeSpec ["TREE_OUTPUT_BRANCH_LABEL"] = "__FONT_SIZE__ 2 idiv\n 0 \nneg\nrmoveto\n(N"+iNodeCounter+") show";
					iNodeCounter = iNodeCounter + 1;
				}
			}
			ExecuteCommands ("stashMeValue="+tName+"."+tree_bn [k]+".synRate;"+tName+"."+tree_bn [k]+".synRate=1;nodeRate="+tName+"."+tree_bn [k]+".nonSynRate/"+tName+"."+tree_bn [k]+".synRate;"+tName+"."+tree_bn [k]+".synRate=stashMeValue");
			nodeColor = branchRates[nodeRate];
			if (Abs(nodeColor) == 0)
			{
				branchRates[nodeRate] = Abs(branchRates)+1;
			}
			nodeColor = branchRates[nodeRate]-1;
			branchLengths [nodeRate] = branchLengths [nodeRate] + tree_lng[k];
			totalTreeLength = totalTreeLength + tree_lng[k];
			nodeSpec ["TREE_OUTPUT_BRANCH_COLOR"] = branchColors[nodeColor];
			if (tree_bn[k] / "Node*")
			{
				nodeName = tree_bn[k];
			}
			if (overOneProbs[nodeName]>0.0)
			{
				nodeSpec ["TREE_OUTPUT_OVER_BRANCH"] = "0 0 0 setrgbcolor\n5 5 rmoveto\n("+Format(overOneProbs[nodeName]*100,0,1)+"\\%) show";
			}
			nodeName  = tree_bn [k];
			TREE_OUTPUT_OPTIONS[nodeName] = nodeSpec;
		}
		SetDialogPrompt ("Save resulting tree string to:");
		ChoiceList  (brOptions,"Scaling Options",1,NO_SKIP,
							 "E[Subs]","Scale on expected number of substitutions",
							 "Unscaled","Unscaled tree");
		if (brOptions)
		{
			ExecuteCommands ("psString = PSTreeString ("+tName+",\"\",{{500,500}});");
		}
		else
		{
			ExecuteCommands ("psString = PSTreeString ("+tName+",\"EXPECTED_NUMBER_OF_SUBSTITUTIONS\",{{500,500}});");		
		}
		
		psLegend = "";
		psLegend * 256;
		currentPosX = 500;
		currentPosY = 500;
		bKeys = Rows (branchRates);
		sortedKeys = {Abs(branchRates),2};
		for (k=0; k<Abs(branchRates); k=k+1)
		{
			sortedKeys[k][0] = 0+bKeys[k];
			sortedKeys[k][1] = k;
		}	
		
		sortedKeys = sortedKeys%0;
		
		for (k=0; k<Abs(branchRates); k=k+1)
		{
			ks = sortedKeys[k][1];
			theKey = bKeys[ks];
			aKey = branchRates[theKey]-1;
			colorMx = branchColors[aKey];
			psLegend * (""+colorMx[0]+" "+colorMx[1]+" "+colorMx[2]+ " setrgbcolor\n"+currentPosX+" "+currentPosY+" moveto\n");
			psLegend * ("(dN/dS = "+Format(0+bKeys[ks],6,3)+"; "+Format(branchLengths[theKey]/totalTreeLength*100,4,0)+ "\\%) show\n");
			currentPosY = currentPosY - 15;
		}
		fprintf (stdout,"\n");
		psLegend * "showpage";
		psLegend * 0;
		repMx = {{"showpage"}{psLegend}};
		psString = psString ^ repMx;
		repMx = {{"setfont"}{"setfont\n3 setlinewidth\n1 setlinecap"}};
		psString = psString ^ repMx;
		fprintf (PROMPT_FOR_FILE,CLEAR_FILE,psString);
	}

}






