treeCount = Rows ("Tree");

if (treeCount > 0)
{
	treeChoices = {treeCount,2};
	for (k=0; k<treeCount; k=k+1)
	{
		GetString (treeID, Tree, k);
		treeChoices [k][0] = treeID;
		treeChoices [k][1] = "Tree " + treeID;
	}
	
	chosenTree = 0;
	if (treeCount > 1)
	{
		ChoiceList (chosenTree, "Which tree?", 1, SKIP_NONE, treeChoices);
		if (chosenTree < 0)
		{
			return 0;
		}
	}
	
	treeName 			= treeChoices[chosenTree][0];
	ExecuteCommands 	("branchNames = BranchName(" + treeName + ", -1);"); 
	branchCount 		= Columns (branchNames)-1;
	fprintf 			(stdout, branchCount, " nodes\n");
	
	dNdSMatrix	= {branchCount,3};
	maxdS = 0;
	for (bid = 0; bid < branchCount; bid = bid + 1)
	{
		isI = ((branchNames[bid]$"Node")[0]>=0);
		ExecuteCommands ("dNdSMatrix[bid][0] = " + treeName + "." + branchNames[bid] + ".synRate;");
		ExecuteCommands ("dNdSMatrix[bid][1] = " + treeName + "." + branchNames[bid] + ".nonSynRate;");
		maxdS = Max (maxdS,dNdSMatrix[bid][0]);
		if (isI)
		{
			dNdSMatrix[bid][2] = dNdSMatrix[bid][1];
			dNdSMatrix[bid][1] = -1;
		}
		else
		{
			dNdSMatrix[bid][2] = -1;
		}
	}
	
	columnHeaders = {{"dS","dN Leaf","dN Internal"}};
	OpenWindow (CHARTWINDOW,{{"dS and dN by branch"}
		{"columnHeaders"}
		{"dNdSMatrix"}
		{"Scatterplot"}
		{"dS"}
		{"dN Leaf;dN Internal"}
		{""}
		{""}
		{""}
		{"3"}
		{"_x_"}
		{"-1;-1"}
		{"10;1.309;0.785398"}
		{"Times:12:0;Times:10:0;Times:12:2"}
		{"0;0;16777215;11776947;0;0;6579300;11842740;13158600;14474460;0;3947580;16777215;15670812;6845928;16771158;2984993;9199669;7018159;1460610;16748822;11184810;14173291"}
		{"16,0,"+maxdS}
		},
		"SCREEN_HEIGHT*2/3;SCREEN_HEIGHT*2/3+120;70;70");
}

