avTreeCount = Rows ("Tree");

branchDash = {};
branchLabels = {};
branchRates  = {};

branchDash	 [0] = 0; 	/* solid */
branchDash	 [1] = {{10,5,0}}; 	/* dark gray */
branchDash 	 [2] = {{5,10,0}};   /* med gray */
branchDash 	 [3] = {{15,15,0}};   	/* 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);");
		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";
			}
			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;
			nodeSpec ["TREE_OUTPUT_BRANCH_DASH"] = branchDash[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:");
		ExecuteCommands ("psString = PSTreeString ("+tName+",\"\",{{500,500}});");
		psLegend = "";
		psLegend * 256;
		currentPosX = 460;
		currentPosY = 480;
		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];
			aKey = bKeys[ks];
			aKey = branchRates[aKey]-1;
			colorMx = branchDash[aKey];
			if (Abs(colorMx))
			{
				psLegend * ("["+colorMx[0]+" "+colorMx[1]+"] "+colorMx[2]+ " setdash\n");
			}
			else
			{
				psLegend * ("[] 0 setdash\n");
			
			}
			psLegend * ("newpath\n"+currentPosX+" "+currentPosY+" moveto\n45 0 rlineto\nstroke\n");
			psLegend * ("newpath "+ (currentPosX+55) + " "+(currentPosY-4)+" moveto\n(dN/dS = "+Format(0+bKeys[ks],6,3)+") show\n");
			currentPosY = currentPosY - 15;
		}
		psLegend * "[] 0 setdash\nshowpage";
		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);
	}

}






