/*****************************************************
 Amino Acid Preference Toolkit in Java
 Pathogen Project
 Department of Computer Science and Engineering
 University of South Carolina
 Columbia, SC 29208
 Contact Email: rose@cse.sc.edu
*****************************************************/

import java.io.*;
import java.util.*;

public class CumulateScores
{
	public static void main(String[] args) throws Exception
	{
		// args[0] = svmTypeList
		// args[1] = file with filenames of AnalysisResults to look at


		BufferedReader typeReader = new BufferedReader(new FileReader(args[0]));
		String type;
		int typeCounter = 0;
		Hashtable typeToCountTable = new Hashtable();
		Hashtable countToTypeTable = new Hashtable();

		while ((type = typeReader.readLine()) != null)
		{
			Integer typeInteger = new Integer(typeCounter);
			typeToCountTable.put(type, typeInteger);
			countToTypeTable.put(typeInteger,type);
			typeCounter++;
		}

		typeReader.close();

		BufferedReader bufferedReader = new BufferedReader(new FileReader(args[1]));
		String analysisFilename;
		BufferedReader analysisFileReader;

		int numberOfTypes = typeToCountTable.size();
		double[][] scoringMatrix = new double[numberOfTypes][numberOfTypes];
		for (int i = 0; i < numberOfTypes; i++)
		{
			for (int j = 0; j < numberOfTypes; j++)
			{
				scoringMatrix[i][j] = 0;
			}
		}

		StringTokenizer tokenizer;	
		String realClassification;
		int realClassificationInt;
		String toolClassification;
		int toolClassificationInt;
		String analysisLine;
		String toolLine;
		double classifyValue;

		while ((analysisFilename = bufferedReader.readLine()) != null)
		{
			analysisFileReader = new BufferedReader(new FileReader(analysisFilename));
			while ((analysisLine = analysisFileReader.readLine()) != null)
			{
			
				if (analysisLine.indexOf("Real Classification") >= 0)
				{
					tokenizer = new StringTokenizer(analysisLine, ":");
					tokenizer.nextToken();
					realClassification = tokenizer.nextToken().trim();
					realClassificationInt = ((Integer)typeToCountTable.get(realClassification)).intValue();
					analysisFileReader.readLine();

					for (int i = 0; i < numberOfTypes; i++)
					{
						toolLine = analysisFileReader.readLine();
						tokenizer = new StringTokenizer(toolLine);
						toolClassification = tokenizer.nextToken().trim();
						toolClassificationInt = ((Integer)typeToCountTable.get(toolClassification)).intValue();
						classifyValue = new Double(tokenizer.nextToken().trim()).doubleValue();
						scoringMatrix[realClassificationInt][toolClassificationInt] = scoringMatrix[realClassificationInt][toolClassificationInt] + classifyValue;
					}
				

				}
			}

			analysisFileReader.close();
		}


		bufferedReader.close();
		System.out.println("Table Ordering:");
		for (int i = 0; i < numberOfTypes; i++)
		{
			System.out.println("\t" + (String)countToTypeTable.get(new Integer(i)));
		}
		System.out.println("Classification Table:");	
		for (int i = 0; i < numberOfTypes; i++)
		{
			double typeSum = 0;
			for (int j = 0; j < numberOfTypes; j++)
			{
				System.out.print("\t" + scoringMatrix[i][j]);
				typeSum = typeSum + scoringMatrix[i][j];
			}
			System.out.print("\t" + typeSum);
			double percentage = scoringMatrix[i][i] / typeSum;
			System.out.println("\t" + percentage);
		}
	}
}
