package net.maizegenetics.pal.ids;

import com.google.common.collect.TreeMultimap;
import java.io.BufferedReader;
import java.util.ArrayList;
import net.maizegenetics.util.Utils;

/**
 * Utilities for reading and writing IdGroup and PedigreeIdGroups.
 * 
 * @author Ed Buckler
 */
public class IdGroupIOUtils {

    private IdGroupIOUtils() {
    }
    
    public static TreeMultimap<String,String> getMapOfTextAnnotatedIds(IdGroup annoIdGroup, String annoName) {
        TreeMultimap<String,String> annoMap=TreeMultimap.create();
        for (int i = 0; i < annoIdGroup.getIdCount(); i++) {
            if(annoIdGroup.getIdentifier(i) instanceof AnnotatedIdentifier) {
                AnnotatedIdentifier ai=(AnnotatedIdentifier)annoIdGroup.getIdentifier(i);
                for (String value : ai.getTextAnnotation(annoName)) {
                    annoMap.put(value,ai.getFullName());
                } 
            }
        }
        return annoMap;
    }
    
    public static IdGroup readPedigree(String fileName) {
        BufferedReader fileIn = null;
        try {
            fileIn = Utils.getBufferedReader(fileName, 1000000);
            fileIn.mark(1<<16);
            String line;
            ArrayList<Identifier> taxaNames=new ArrayList<Identifier>();
            line=fileIn.readLine();
            int indexOfName=0;
            String[] headers=null;
            boolean[] isQuant=null;
            if(line.contains("<Name>")) {
                //parse headers
                headers=line.split("\\t");
                isQuant=new boolean[headers.length];
                for (int i = 0; i < headers.length; i++) {
                    if(headers[i].equals("<Name>")) {indexOfName=i; continue;}
                    headers[i]=headers[i].replace(">", "");
                    headers[i]=headers[i].replace("<", "");
                    if(headers[i].startsWith("#")) {
                        isQuant[i]=true;
                        headers[i]=headers[i].replace("#", "");
                    } else {
                        isQuant[i]=false;
                    }
                    
                }
            } else {
               fileIn.reset();
            }
            while((line=fileIn.readLine())!=null) {
                String[] s=line.split("\\t");
                if(headers!=null) {
                    AnnotatedIdentifier anID=new AnnotatedIdentifier(s[indexOfName]);
                    for (int i = 0; i < s.length; i++) {
                        if(i==indexOfName) continue;
                        if(isQuant[i]) {
                           // System.out.println(line);
                            
                            if(s[i].equals("NA")) {anID.addAnnotation(headers[i], Double.NaN);}
                            else {anID.addAnnotation(headers[i], Double.parseDouble(s[i]));}
                        }
                        else {anID.addAnnotation(headers[i], s[i]);}
                    }
                    taxaNames.add(anID);
                } else {
                    taxaNames.add(new Identifier(s[indexOfName]));
                }
            }
            return new SimpleIdGroup(taxaNames);
        } catch(Exception e) {
            System.err.println("Error in Reading Pedigree File:"+fileName);
            e.printStackTrace();
        }    
        return null;
        
    }
    
    
    
}

@@ -2544,80 +2544,81 @@
     
-if(isQuant%5Bi%5D) %7B%0A                           // System.out.println(line);
+String%5B%5D cs=s%5Bi%5D.split(%22;%22);%0A                        for(String ta: cs) %7B
 %0A   
@@ -2634,33 +2634,53 @@
                 
-%0A
+if(isQuant%5Bi%5D) %7B%0A    
                 
@@ -2694,20 +2694,18 @@
      if(
-s%5Bi%5D
+ta
 .equals(
@@ -2777,32 +2777,36 @@
                 
+    
 else %7BanID.addAn
@@ -2849,17 +2849,19 @@
 ble(
-s%5Bi%5D
+ta
 ));%7D%0A
+    
     
@@ -2898,32 +2898,36 @@
                 
+    
 else %7BanID.addAn
@@ -2951,14 +2951,38 @@
 i%5D, 
-s%5Bi%5D);
+ta);%7D%0A                        
 %7D%0A  

Test approach for delimiting multiple annotations for IDs