package net.maizegenetics.gwas.imputation;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Pattern;

import net.maizegenetics.pal.alignment.Alignment;
import net.maizegenetics.util.BitSet;

public class PopulationData {
	public String name;
	public ArrayList<String> members;
	public BitSet snpIndex;
	public String parent1;
	public String parent2;
	public double contribution1;
	public double contribution2;
	public int Fgen;
	public double inbredCoef;
	public Alignment original;
	public Alignment imputed;
	public byte[] alleleA;
	public byte[] alleleC;

	/**
	 * @param popFilename	the name of the file containing pedigree information for a group of populations
	 * @return	a HashMap of family names (keys) and associated PopulationData objects (values) containing information for the families in the pedigree file
	 */
	public static ArrayList<PopulationData> readPedigreeFile(String popFilename) {
		Pattern tab = Pattern.compile("\t");
		LinkedHashMap<String, PopulationData> familyMap = new LinkedHashMap<String, PopulationData>(); 
		try {
			BufferedReader br = new BufferedReader(new FileReader(popFilename));
			br.readLine();
			String input;
			while ((input = br.readLine()) != null) {
				String[] info = tab.split(input);
				PopulationData family = familyMap.get(info[0]);
				if (family == null) {
					family = new PopulationData ();
					family.name = info[0];
					family.members = new ArrayList<String>();
					family.members.add(info[2]);  //add parents to family members
					family.members.add(info[3]);
					family.members.add(info[1]);
					family.parent1 = info[2];
					family.parent2 = info[3];
					family.contribution1 = Double.parseDouble(info[4]);
					family.contribution2 = Double.parseDouble(info[5]);
					familyMap.put(info[0], family);
				}
				else family.members.add(info[1]);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		return new ArrayList<PopulationData>(familyMap.values());
	}
}
@@ -1203,17 +1203,37 @@
 Data%3E();
- 
+%0A%09%09String input = %22%22;
 %0A%09%09try %7B
@@ -1327,25 +1327,8 @@
 ();%0A
-%09%09%09String input;%0A
 %09%09%09w
@@ -2080,16 +2080,141 @@
 it(-1);%0A
+%09%09%7D catch (NumberFormatException nfe) %7B%0A%09%09%09System.out.println(%22at: %22 + input);%0A%09%09%09nfe.printStackTrace();%0A%09%09%09System.exit(-1);%0A
 %09%09%7D%0A%09%09re

check for NumberFormatException