characterOrderIn  = "ACDEFGHIKLMNPQRSTVWY";


SetDialogPrompt ("Protein data file:");
DataSet ds1 = ReadDataFile (PROMPT_FOR_FILE);


while (1)
{
	fprintf (stdout, "1-based site to map (-1 to stop:):");
	fscanf  (stdin, "Number", siteToMap);
	if (siteToMap < 0)
	{
		break;
	}
	mapSite (siteToMap);
}

function mapSite (aSite)
{
	DataSetFilter 		f1 = CreateFilter (ds1, 1, siteIndex == (aSite-1));
	HarvestFrequencies (freqs,f1,1,1,1);
	freqs = freqs * f1.species;
	
	rankedMatrix = {20,3}["freqs[_MATRIX_ELEMENT_ROW_]*(_MATRIX_ELEMENT_COLUMN_==0)+(_MATRIX_ELEMENT_COLUMN_==1)*_MATRIX_ELEMENT_ROW_"];
	for (k=0; k<20; k=k+1)
	{
		rankedMatrix[k][2] = rankedMatrix[k][1]/40+rankedMatrix[k][0];
	}
	rankedMatrix = rankedMatrix%2;
	
	fprintf (stdout, "$");
	for (k=19; k>=0; k=k-1)
	{
		if (rankedMatrix[k][0]<1)
		{
			break;
		}
		fprintf (stdout, characterOrderIn[rankedMatrix[k][1]], "_{", rankedMatrix[k][0]$1, "}");
	}
	fprintf (stdout, "$\n");
	return 0;	
}
