package org.networklibrary.edger.parsing;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.networklibrary.core.parsing.FileBasedParser;
import org.networklibrary.core.parsing.ParsingErrorException;
import org.networklibrary.core.types.EdgeData;
import org.networklibrary.core.types.EdgeTypes;

public class DisgenetParser extends FileBasedParser<EdgeData> {

	protected static final Logger log = Logger.getLogger(DisgenetParser.class.getName());

	public static String SOURCE_NAME = "disgenet";
	public static String EDGE_TYPE = EdgeTypes.DGN_ASSOCIATION;

	private List<String> columns = null;

	protected double cutoff = 0;

	@Override
	public Collection<EdgeData> parse() throws ParsingErrorException {
		String line = readLine();
		List<EdgeData> res = null;

		if(!line.isEmpty()){
			res = new LinkedList<EdgeData>();

			String[] values = line.split("\\t",-1);

			if(values.length != columns.size()){
				System.out.println(line);
				throw new IllegalArgumentException("number of elements in row does not match number of columns " + line);
			}

			//	geneId	geneSymbol	geneName	diseaseId	diseaseName	score	NumberOfPubmeds	associationType	source

			double score = Double.valueOf(values[5]);
			if(score > cutoff){
				Map<String,Object> props = new HashMap<String,Object>();

				props.put("score",score);
				props.put(columns.get(6), Integer.valueOf(values[6]));
				props.put(columns.get(7), values[7]);
				String[] sources = values[8].split(", ",-1);

				props.put("associations", getAssociations(values[7]));
				props.put(columns.get(8), sources);
				props.put("data_source",SOURCE_NAME);

				res.add(new EdgeData(values[0],values[3],EDGE_TYPE,props));
			}

		}

		return res;
	}

	private String[] getAssociations(String disgenetType){
		return disgenetType.split(", ");
	}

//	private String getEdgeType(String disgenetType) {
//		switch(disgenetType) {
//		case "Biomarker":
//			return EdgeTypes.BIOMARKER_OF;
//		case "GeneticVariation":
//			return EdgeTypes.GENETIC_VARIATION_FOR;
//		case "PostTranslationalModification":
//			return EdgeTypes.POST_TRANSLATIONAL_MODIFICATION_IN;
//		case "AlteredExpression":
//			return EdgeTypes.ALTERED_EXPRESSION_IN;
//		case "Therapeutic":
//			return EdgeTypes.THERAPEUTIC_ROLE_IN;
//		}
//
//		log.warning("Edge type for " + disgenetType + " not found!");
//		return disgenetType;
//	}

	@Override
	public boolean hasExtraParameters() {
		return true;
	}

	@Override
	public void takeExtraParameters(List<String> extras) {
		if(extras != null){
			log.info("processing extra parameters: " + extras.toString());

			for(String extra : extras){
				String values[] = extra.split("=",-1);

				switch(values[0]) {
				case "cutoff":
					cutoff = Double.parseDouble(values[1]);
					break;
				}
			}

			log.info("using a cutoff of " + cutoff);
		}
	}

	@Override
	protected boolean hasHeader() {
		return true;
	}

	@Override
	protected void parseHeader(String header) {
		columns = Arrays.asList(header.split("\\s",-1));
	}

}

@@ -1300,16 +1300,184 @@
 source%0A%0A
+%09%09%09Map%3CString, Integer%3E columnIndices = new HashMap%3CString, Integer%3E();%0A%09%09%09for(int i = 0; i %3C columns.size(); i++) %7B%0A%09%09%09%09columnIndices.put(columns.get(i), i);%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09doubl
@@ -1505,17 +1505,34 @@
 eOf(
-values%5B5%5D
+columnIndices.get(%22score%22)
 );%0A%09
@@ -1663,22 +1663,18 @@
 put(
-columns.get(6)
+%22NofPmids%22
 , In
@@ -1698,53 +1698,120 @@
 ues%5B
-6%5D));%0A%09%09%09%09props.put(columns.get(7), values%5B7%5D
+columnIndices.get(%22NofPmids%22)%5D));%0A%09%09%09%09props.put(%22NofSnps%22, Integer.valueOf(values%5BcolumnIndices.get(%22NofSnps%22)%5D)
 );%0A%09
@@ -1839,17 +1839,43 @@
  values%5B
-8
+columnIndices.get(%22source%22)
 %5D.split(
@@ -1875,18 +1875,17 @@
 .split(%22
-, 
+;
 %22,-1);%0A%0A
@@ -1903,80 +1903,15 @@
 ut(%22
-associations%22, getAssociations(values%5B7%5D));%0A%09%09%09%09props.put(columns.get(8)
+source%22
 , so
@@ -1997,158 +1997,118 @@
 ues%5B
-0%5D,values%5B3%5D,EDGE_TYPE,props));%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%09return res;%0A%09%7D%0A%0A%09private String%5B%5D getAssociations(String disgenetType)%7B%0A%09%09return disgenetType.split(%22, %22)
+columnIndices.get(%22geneId%22)%5D,values%5BcolumnIndices.get(%22diseaseId%22)%5D,EDGE_TYPE,props));%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%09return res
 ;%0A%09%7D

fix disgenet - they changed format again :(, make it more resilient by using column headers instead of indices