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>();
		String input = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(popFilename));
			br.readLine();
			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);
		} catch (NumberFormatException nfe) {
			System.out.println("at: " + input);
			nfe.printStackTrace();
			System.exit(-1);
		}
		return new ArrayList<PopulationData>(familyMap.values());
	}
}
@@ -1406,16 +1406,84 @@
 input);%0A
+%09%09%09%09if (info%5B0%5D.length() %3E 0 && !info%5B0%5D.equalsIgnoreCase(%22NA%22)) %7B%0A%09
 %09%09%09%09Popu
@@ -1522,24 +1522,25 @@
 t(info%5B0%5D);%0A
+%09
 %09%09%09%09if (fami
@@ -1554,24 +1554,25 @@
 ull) %7B%0A%09%09%09%09%09
+%09
 family = new
@@ -1587,24 +1587,25 @@
 ionData ();%0A
+%09
 %09%09%09%09%09family.
@@ -1621,24 +1621,25 @@
 fo%5B0%5D;%0A%09%09%09%09%09
+%09
 family.membe
@@ -1664,24 +1664,25 @@
 %3CString%3E();%0A
+%09
 %09%09%09%09%09family.
@@ -1736,16 +1736,17 @@
 members%0A
+%09
 %09%09%09%09%09fam
@@ -1772,24 +1772,25 @@
 o%5B3%5D);%0A%09%09%09%09%09
+%09
 family.membe
@@ -1802,24 +1802,25 @@
 d(info%5B1%5D);%0A
+%09
 %09%09%09%09%09family.
@@ -1839,24 +1839,25 @@
 fo%5B2%5D;%0A%09%09%09%09%09
+%09
 family.paren
@@ -1870,16 +1870,17 @@
 nfo%5B3%5D;%0A
+%09
 %09%09%09%09%09fam
@@ -1929,24 +1929,25 @@
 o%5B4%5D);%0A%09%09%09%09%09
+%09
 family.contr
@@ -1982,24 +1982,131 @@
 e(info%5B5%5D);%0A
+%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09family.inbredCoef = Double.parseDouble(info%5B6%5D);%0A%09%09%09%09%09%09%7D catch (Exception e) %7B%7D%0A%09%09%09%09%09%09%0A%09
 %09%09%09%09%09familyM
@@ -2130,16 +2130,17 @@
 amily);%0A
+%09
 %09%09%09%09%7D%0A%09%09
@@ -2141,16 +2141,17 @@
 %09%09%7D%0A%09%09%09%09
+%09
 else fam
@@ -2176,16 +2176,22 @@
 fo%5B1%5D);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
@@ -2230,16 +2230,55 @@
 on e) %7B%0A
+%09%09%09System.out.println(%22at: %22 + input);%0A
 %09%09%09e.pri

add code to ignore lines with family=NA