/* 
   Author: 
   Description:
   
   input file:
   dnaLoci.txt which has the database of dna typing
   each line has an ID followed by dna information at the loci

   samples.txt which has dna typing of a few samples
   each line has an ID followed by dna information at the loci

   output:
   match or no match; probability if there is a match
   
 
*/

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



public class HW6
{
    private static int NUM_PEOPLE = 622;
    private static int NUM_DNA_LOCI = 40;
    private static final Person[] db = new Person[NUM_PEOPLE];  // database of people
    private static final Scanner keyboard = new Scanner(System.in);



    // return true if there is a match in all loci in dna1 and dna2
    // don't check all loci if not necessary
    public static boolean matchDNA(String[] dna1, String[] dna2)
    {
	boolean match = false;









	return match;
    }

    // return the joint probability--assuming independence,
    // product of individual probabilities of values at the loci
    // 
    public static double getProbabilityOfProfile(String[] dna)
    {
	// getProbability(int locus, String value) gets the probability
	// of value at a locus
	double probability = 0.5;





	
	return probability;
    }



    // check if a sample matches an entry in the database (DB)
    // print the entry and its probability, and skip the rest if there is a match
    // print does not match if there is not a match
    private static void checkDB(Person sample)
    {
	// modify the following
	int entry = 0;       // index of an entry in the DB
	double prob = 0.5;   // probability of the entry or sample


	matchDNA(sample.dna, db[entry].dna);


	System.out.println("Sample " + sample.id + 
			   " matches " + db[entry].id + ".");
        prob = getProbabilityOfProfile(sample.dna);

        System.out.printf("Estimated probability of the DNA profile is %8.2e\n", prob);
        System.out.printf("or approximately one in %8.2e people has the same DNA profile.\n", 1/prob);


    }





    private static String DNA_FNAME = "dnaLoci.txt";
    private static String SAMPLE_FNAME = "samples.txt";
    public static class Person
    {
	public String   id;
	public String[] dna;

	public Person()
	{
	    dna = new String[NUM_DNA_LOCI];
	}
    }


    public static void main(String[] arg)
    {
	// read dna info from input file
	readDNAdb();

	// check if each sample matches an entry in the database
	checkSamples();
    }


    // read DNA database info from input file
    private static void readDNAdb()
    {
	try
	    {
		System.out.println("Building the DNA database...");

		Scanner dnaFile = new Scanner(new File(DNA_FNAME));

		for (int i = 0; i < NUM_PEOPLE; i++)
		    {
			db[i] = new Person();
			readPerson(db[i], dnaFile);
		    }
		
		dnaFile.close();

		estimateProbability();
	    }
	catch (FileNotFoundException e)
	    {
		System.err.println(e.getMessage());
		System.exit(-1);
	    }

    }

    // check samples
    private static void checkSamples()
    {
	try
	    {
		Scanner sampleFile = new Scanner(new File(SAMPLE_FNAME));

		int numSamples = sampleFile.nextInt();

		for (int i = 0; i < numSamples; i++)
		    {
			Person sample = new Person();
			readPerson(sample, sampleFile);
			checkDB(sample);
		    }
		
		sampleFile.close();
	    }
	catch (FileNotFoundException e)
	    {
		System.err.println(e.getMessage());
		System.exit(-1);
	    }

    }


    // read one person from input file
    private static void readPerson(Person person,  Scanner file)
    {
	person.id = file.next();
	for (int i = 0; i < NUM_DNA_LOCI; i++)
	    {
		person.dna[i] = file.next();
	    }
    }


    private static ArrayList<HashMap<String, Integer>> dnaProb = new ArrayList<HashMap<String, Integer>>(NUM_DNA_LOCI);

    // probabilities of each value at each locus
    // each locus has a hashmap
    // each value at a locus is the key to the hashmap, count of the value is the value 
    //   in the hashmap
    // probability is calculated when getProbability is called
    private static void estimateProbability()
    {
	// initialize the arraylist with hashmaps
	for (int i = 0; i < NUM_DNA_LOCI; i++)
	    {
		dnaProb.add(i, new HashMap<String,Integer>());
	    }

	// use hash maps to keep count of each value at each locus
	Integer value;
	for (int i = 0; i < db.length; i++)
	    {
		Person person = db[i];
		for (int j = 0; j < NUM_DNA_LOCI; j++)
		    {
			value = dnaProb.get(j).get(person.dna[j]);  
			if (value != null)  // increment the count
			    {
				dnaProb.get(j).put(person.dna[j], new Integer(value.intValue() + 1));
			    }
			else // no entry
			    {
				dnaProb.get(j).put(person.dna[j], new Integer(1));
			    }

		    }
	    }
	//printDNAprob();
    }

    private static void printDNAprob()
    {
	for (int j = 0; j < NUM_DNA_LOCI; j++)
	    {
		System.out.println(dnaProb.get(j));
	    }
    }

    // returns the probability of the value at a dna locus
    private static double getProbability(int dnaLocus, String valueAtLocus)
    {
	return dnaProb.get(dnaLocus).get(valueAtLocus).intValue()/(double)NUM_PEOPLE;
    }

}