package org.cpic.haplotype;

import com.google.common.base.Preconditions;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Read in haplotype definition files.
 *
 * @author Mark Woon
 */
public class DefinitionReader {
  private TreeMultimap<String, Variant> m_haplotypePositions;
  private TreeMultimap<String, Haplotype> m_haplotypes;
  
  private Set<TSVfile> s_files;


  public Multimap<String, Variant> getHaplotypePositions() {
    return m_haplotypePositions;
  }
  
  public Multimap<String, Haplotype> getHaplotypes() {
	    return m_haplotypes;
  }



  public void read(Path path) throws IOException {

    if (Files.isDirectory(path)) {
      Files.list(path)
          .filter(f -> f.toString().endsWith(".tsv"))
          .forEach(this::readFile);
    } else {
      readFile(path);
    }
  }
  
  
  
  


  private void readFile(Path file)  {

    Preconditions.checkArgument(Files.isRegularFile(file));
    System.out.println(file);
    
    TSVfile inProccessFile = new TSVfile(file.toString());
    ArrayList<Variant> variants = new ArrayList<Variant>();
    
    try (BufferedReader bufferedReader = Files.newBufferedReader(file)) {
    	
        String line = null;
   	
    	while((line = bufferedReader.readLine()) != null) {
            
            String[] fields = line.trim().split("\t");
            for (int i = 0; i < fields.length; i++){
            	fields[i] = fields[i].trim();
            }
            
            if (fields[0].equals("FormatVersion")){
            	inProccessFile.setFormatVersion(fields[1]);
            }
            else if (fields[0].equals("Gene")){
            	inProccessFile.setFormatVersion(fields[1]);
            }
            else if (fields[0].equals("ContentVersion")){
            	//inProccessFile.setFormatVersion(fields[1]);
            }
            else if (fields[0].equals("GenomeBuild")){
            	//inProccessFile.setFormatVersion(fields[1]);
            }
            else if (fields[0].equals("Chomosome")){
            	inProccessFile.setChromosomeID(fields[1]);
            	inProccessFile.setChromosome(fields[2]);
            }
            else if (fields[0].equals("Protein")){
            	//inProccessFile.setFormatVersion(fields[1]);
            }
            else if (fields[0].equals("cDNAChange")){
            	for (int i = 4; i < fields.length; i++){
                	Variant newVariant = new Variant(inProccessFile.getChromosome(),inProccessFile.getGeneName(),fields[i]);
                	variants.add(newVariant);
                }
            }
            else if (fields[0].equals("ProteinEffect")){
            	for (int i = 4; i < fields.length; i++){
                	variants.get(i-4).addProteingEffect(fields[i]);
                }
            }
            else if (fields[0].equals("ChrPosition")){
            	for (int i = 4; i < fields.length; i++){
                	variants.get(i-4).addProteingEffect(fields[i]);
                	variants.get(i-4).setStartPOS();
                }
            }
            else if (fields[0].equals("GenePosition")){
            	for (int i = 4; i < fields.length; i++){
                	variants.get(i-4).addProteingEffect(fields[i]);
                }
            }
            else if (fields[0].equals("rsID")){
            	for (int i = 4; i < fields.length; i++){
                	variants.get(i-4).set_rsID(fields[i]);
                }
            }
            
            
        } 
    	
    	s_files.add(inProccessFile);
    	
    	
    } catch (Exception ex) {
    	throw new RuntimeException(ex);
    }
  }
  
  public static void main(String[] args) {

    try {
      Path path = Paths.get(args[0]);
      DefinitionReader r = new DefinitionReader();
      r.read(path);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

@@ -100,16 +100,20 @@
 collect.
+List
 Multimap
@@ -147,20 +147,25 @@
 collect.
-Tree
+SortedSet
 Multimap
@@ -362,72 +362,8 @@
 Set;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/*
@@ -465,36 +465,41 @@
 der %7B%0A  private 
-Tree
+SortedSet
 Multimap%3CString,
@@ -544,12 +544,12 @@
 ate 
-Tree
+List
 Mult
@@ -586,18 +586,16 @@
 otypes;%0A
-  
 %0A  priva
@@ -622,32 +622,41 @@
 les;%0A%0A%0A  public 
+SortedSet
 Multimap%3CString,
@@ -724,26 +724,24 @@
 itions;%0A  %7D%0A
-  
 %0A  public Mu
@@ -738,16 +738,20 @@
  public 
+List
 Multimap
@@ -1070,19 +1070,11 @@
   %7D%0A
-  %0A  %0A  %0A  
+%0A%0A%0A
 %0A%0A%0A 
@@ -1201,20 +1201,16 @@
 (file);%0A
-    
 %0A    TSV
@@ -1321,20 +1321,16 @@
 ant%3E();%0A
-    
 %0A    try
@@ -1396,21 +1396,16 @@
 ile)) %7B%0A
-    %09
 %0A       
@@ -1425,20 +1425,16 @@
 = null;%0A
-   %09
 %0A    %09wh
@@ -1483,28 +1483,16 @@
 null) %7B%0A
-            
 %0A       
@@ -1645,36 +1645,24 @@
           %7D%0A
-            
 %0A           
@@ -3622,33 +3622,9 @@
   %7D%0A
-            %0A            
+%0A
 %0A   
@@ -3633,15 +3633,9 @@
    %7D
- %0A    %09
+%0A
 %0A   
@@ -3669,19 +3669,9 @@
 e);%0A
-    %09%0A    %09
+%0A
 %0A   
@@ -3743,18 +3743,16 @@
   %7D%0A  %7D%0A
-  
 %0A  publi

Clarify collection type.