From 76d6bcf1ebab4076a091f675943777f37b681661 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 30 Jun 2017 11:33:47 +0200
Subject: [PATCH] compartments have parent compartments assigned

---
 .../model/sbgnml/SbgnmlXmlParser.java         | 55 ++++++++++++-------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlParser.java b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlParser.java
index bbc79aa9d9..3733c04f54 100644
--- a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlParser.java
+++ b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlParser.java
@@ -62,6 +62,7 @@ import lcsb.mapviewer.model.map.reaction.type.StateTransitionReaction;
 import lcsb.mapviewer.model.map.reaction.type.UnknownTransitionReaction;
 import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Degraded;
+import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Gene;
 import lcsb.mapviewer.model.map.species.GenericProtein;
 import lcsb.mapviewer.model.map.species.Phenotype;
@@ -622,49 +623,58 @@ public class SbgnmlXmlParser {
 			}
 		}
 
-		parseAlias(g, newSpecies, model);
+		parseSpecies(g, newSpecies, model);
 	}
 
 	/**
 	 * Method used to create a new alias from SBGN-ML glyph.
 	 * 
-	 * @param g
+	 * @param glyph
 	 *          SBGN-ML glyph representing the alias
-	 * @param newSpecies
+	 * @param species
 	 *          species of the alias
 	 * @param model
 	 *          model to be updated
 	 */
-	private void parseAlias(Glyph g, Species newSpecies, Model model) {
-		newSpecies.setHeight(new Double(g.getBbox().getH()));
-		newSpecies.setWidth(new Double(g.getBbox().getW()));
-		newSpecies.setX(new Double(g.getBbox().getX()));
-		newSpecies.setY(new Double(g.getBbox().getY()));
+	private void parseSpecies(Glyph glyph, Species species, Model model) {
+		species.setHeight(new Double(glyph.getBbox().getH()));
+		species.setWidth(new Double(glyph.getBbox().getW()));
+		species.setX(new Double(glyph.getBbox().getX()));
+		species.setY(new Double(glyph.getBbox().getY()));
 
 		Compartment parentCompartment = null;
-		if (g.getCompartmentRef() != null) {
-			Glyph compartmentGlyph = (Glyph) g.getCompartmentRef();
+		if (glyph.getCompartmentRef() != null) {
+			Glyph compartmentGlyph = (Glyph) glyph.getCompartmentRef();
 			parentCompartment = model.getElementByElementId(compartmentGlyph.getId());
-		} else if (newSpecies.getComplex() == null) {
-			// If the alias is in any compartment, assign it to that compartment
-			parentCompartment = findParentCompartment(newSpecies, model);
+		} else if (species.getComplex() == null) {
+			parentCompartment = findParentCompartment(species, model);
 		}
-		if (parentCompartment!=null) {
-			newSpecies.setCompartment(parentCompartment);
-			parentCompartment.addElement(newSpecies);
+		if (parentCompartment != null) {
+			species.setCompartment(parentCompartment);
+			parentCompartment.addElement(species);
 		}
 
 		// Parse units of information
-		for (Glyph child : g.getGlyph()) {
+		for (Glyph child : glyph.getGlyph()) {
 			if (GlyphClazz.fromClazz(child.getClazz()).equals(GlyphClazz.UNIT_OF_INFORMATION)) {
-				parseUnitOfInformation(child, newSpecies);
+				parseUnitOfInformation(child, species);
 			}
 		}
 
-		model.addElement(newSpecies);
+		model.addElement(species);
 	}
 
-	private Compartment findParentCompartment(Species child, Model model) {
+	/**
+	 * Finds a compartment where element should be located (base on the
+	 * coordinates).
+	 * 
+	 * @param child
+	 *          {@link Element} for which we want to find compartment
+	 * @param model
+	 *          {@link Model} where we look for a compartment
+	 * @return parent {@link Compartment}
+	 */
+	private Compartment findParentCompartment(Element child, Model model) {
 		Compartment nullParent = new Compartment("null");
 		nullParent.setWidth(Double.MAX_VALUE);
 		nullParent.setHeight(Double.MAX_VALUE);
@@ -1514,6 +1524,11 @@ public class SbgnmlXmlParser {
 			compartment.setNamePoint(
 					compartment.getX() + compartment.getThickness() + CONTAINER_NAME_MARGIN, compartment.getY() + compartment.getThickness() + CONTAINER_NAME_MARGIN);
 		}
+		Compartment parent = findParentCompartment(compartment, model);
+		if (parent != null) {
+			compartment.setCompartment(parent);
+			parent.addElement(compartment);
+		}
 
 		model.addElement(compartment);
 	}
-- 
GitLab