package org.cytoscape.keggscape.internal.read.kgml;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyEdge;
import org.cytoscape.keggscape.internal.generated.Entry;
import org.cytoscape.keggscape.internal.generated.Pathway;
import org.cytoscape.keggscape.internal.generated.Relation;

public class KGMLMapper {
	
	private final Pathway pathway;
	private final CyNetwork network;
	private final String pathwayName;
	
	final Map<String, CyNode> nodeMap = new HashMap<String, CyNode>();
	
	public KGMLMapper(final Pathway pathway, final CyNetwork network) {
		this.pathway = pathway;
		this.network = network;
		this.pathwayName = pathway.getName();
	}
	
	public void doMapping() {
		mapEntries();
		mapRelations();
	}
	
	private void mapEntries() {
		final List<Entry> entries = pathway.getEntry();
		System.out.println(entries.size());
		for (final Entry entry : entries) {
			CyNode cyNode = network.addNode();
			network.getRow(cyNode).set(CyNetwork.NAME, entry.getId());
			nodeMap.put(entry.getId(), cyNode);
		}
	}
	
	private void mapRelations() {
		final List<Relation> relations = pathway.getRelation();
		System.out.println(relations.size());
		for (final Relation relation : relations) {
			final CyNode sourceNode = nodeMap.get(relation.getEntry1());
			final CyNode targetNode = nodeMap.get(relation.getEntry2());
			final CyEdge newEdge = network.addEdge(sourceNode, targetNode, true);
		} 
	}
}

@@ -392,24 +392,204 @@
 nerated.
-Relation
+Product;%0Aimport org.cytoscape.keggscape.internal.generated.Reaction;%0Aimport org.cytoscape.keggscape.internal.generated.Relation;%0Aimport org.cytoscape.keggscape.internal.generated.Substrate
 ;%0A%0Apubli
@@ -1007,24 +1007,42 @@
 elations();%0A
+%09%09mapReactions();%0A
 %09%7D%0A%09%0A%09privat
@@ -1717,14 +1717,14 @@
 e);%0A
-
 %09%09%7D %0A%09%7D%0A
 %7D%0A
@@ -1721,10 +1721,748 @@
 %09%09%7D %0A%09%7D%0A
+%09%0A%09private void mapReactions() %7B%0A%09%09final List%3CReaction%3E reactions = pathway.getReaction();%0A%09%09System.out.println(reactions.size());%0A%09%09for (Reaction reaction : reactions) %7B%0A%09%09%09final CyNode reactionNode = nodeMap.get(reaction.getId());%0A%09%09%09final List%3CSubstrate%3E substrates = reaction.getSubstrate();%0A%09%09%09for (final Substrate substrate : substrates) %7B%0A%09%09%09%09final CyNode sourceNode = nodeMap.get(substrate.getId());%0A%09%09%09%09final CyEdge newEdge = network.addEdge(sourceNode, reactionNode, true);%0A%09%09%09%7D%0A%09%09%09final List%3CProduct%3E products = reaction.getProduct();%0A%09%09%09for (final Product product : products) %7B%0A%09%09%09%09final CyNode targetNode = nodeMap.get(product.getId());%0A%09%09%09%09final CyEdge newEdge = network.addEdge(reactionNode, targetNode, true);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

create reaction edges