From 12de5196bbb8b4a0ef06b4c5b41cdff536c6a914 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Thu, 30 Jan 2025 10:29:13 +0100
Subject: [PATCH] when creating hierarchy consider also pathways

---
 .../commands/CreateHierarchyCommand.java      | 75 +++++++++++++++----
 1 file changed, 62 insertions(+), 13 deletions(-)

diff --git a/model-command/src/main/java/lcsb/mapviewer/commands/CreateHierarchyCommand.java b/model-command/src/main/java/lcsb/mapviewer/commands/CreateHierarchyCommand.java
index 9db57b4061..cb4da52866 100644
--- a/model-command/src/main/java/lcsb/mapviewer/commands/CreateHierarchyCommand.java
+++ b/model-command/src/main/java/lcsb/mapviewer/commands/CreateHierarchyCommand.java
@@ -42,11 +42,11 @@ public class CreateHierarchyCommand extends ModelCommand {
   /**
    * Top left corner x coordinate of the text associated with compartment.
    */
-  private static final double DEFAULT_TITLE_X_COORD_IN_ARTIFITIAL_COMPARTMENT = 10;
+  private static final double DEFAULT_TITLE_X_COORD_IN_ARTIFICIAL_COMPARTMENT = 10;
   /**
    * Top left corner y coordinate of the text associated with compartment.
    */
-  private static final double DEFAULT_TITLE_Y_COORD_IN_ARTIFITIAL_COMPARTMENT = 10;
+  private static final double DEFAULT_TITLE_Y_COORD_IN_ARTIFICIAL_COMPARTMENT = 10;
   /**
    * Default class logger.
    */
@@ -168,10 +168,10 @@ public class CreateHierarchyCommand extends ModelCommand {
           if (rect.getFillColor() != null) {
             compartment.setFillColor(rect.getFillColor());
           }
-          compartment.setNameX(rect.getX() + DEFAULT_TITLE_X_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameY(rect.getY() + DEFAULT_TITLE_Y_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameWidth(rect.getWidth() - 2 * DEFAULT_TITLE_X_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameHeight(rect.getHeight() - 2 * DEFAULT_TITLE_Y_COORD_IN_ARTIFITIAL_COMPARTMENT);
+          compartment.setNameX(rect.getX() + DEFAULT_TITLE_X_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameY(rect.getY() + DEFAULT_TITLE_Y_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameWidth(rect.getWidth() - 2 * DEFAULT_TITLE_X_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameHeight(rect.getHeight() - 2 * DEFAULT_TITLE_Y_COORD_IN_ARTIFICIAL_COMPARTMENT);
           compartment.setNameHorizontalAlign(HorizontalAlign.LEFT);
           compartment.setNameVerticalAlign(VerticalAlign.TOP);
           compartment.setZ(rect.getZ());
@@ -196,10 +196,10 @@ public class CreateHierarchyCommand extends ModelCommand {
           rap.processNotes(compartment);
           text.setNotes(compartment.getName() + "\n" + compartment.getNotes());
 
-          compartment.setNameX(text.getX() + DEFAULT_TITLE_X_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameY(text.getY() + DEFAULT_TITLE_Y_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameWidth(text.getWidth() - 2 * DEFAULT_TITLE_X_COORD_IN_ARTIFITIAL_COMPARTMENT);
-          compartment.setNameHeight(text.getHeight() - 2 * DEFAULT_TITLE_Y_COORD_IN_ARTIFITIAL_COMPARTMENT);
+          compartment.setNameX(text.getX() + DEFAULT_TITLE_X_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameY(text.getY() + DEFAULT_TITLE_Y_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameWidth(text.getWidth() - 2 * DEFAULT_TITLE_X_COORD_IN_ARTIFICIAL_COMPARTMENT);
+          compartment.setNameHeight(text.getHeight() - 2 * DEFAULT_TITLE_Y_COORD_IN_ARTIFICIAL_COMPARTMENT);
           compartment.setNameHorizontalAlign(HorizontalAlign.LEFT);
           compartment.setNameVerticalAlign(VerticalAlign.TOP);
 
@@ -292,12 +292,49 @@ public class CreateHierarchyCommand extends ModelCommand {
       compartment.setTransparencyLevel(maxVisibilityLevel + "");
     }
     for (final Element child : compartment.getElements()) {
-      if (child.getVisibilityLevel() == null || child.getVisibilityLevel().isEmpty()) {
+      if (!isExcludedChildVisibility(child)) {
         child.setVisibilityLevel(compartment.getTransparencyLevel());
       }
     }
   }
 
+  private boolean isExcludedChildVisibility(final Element child) {
+    return excluded.contains(child);
+  }
+
+  private void settingTransparencyLevelForPathway(final PathwayCompartment pathway) {
+    Set<Element> pathwayChildren = getPathwayChildren(pathway);
+    if (pathway.getTransparencyLevel() == null || pathway.getTransparencyLevel().isEmpty()) {
+      double rate = computeRate(pathway, Configuration.MAX_VISIBLE_OBJECT_SIZE);
+      int maxVisibilityLevel = (int) ((int) Math.ceil(Math.log(rate)) / LOG_4);
+      for (final Element child : pathwayChildren) {
+        maxVisibilityLevel = Math.min(maxVisibilityLevel, computeVisibility(child));
+      }
+      if (maxVisibilityLevel >= zoomLevels) {
+        maxVisibilityLevel = zoomLevels;
+      }
+      if (maxVisibilityLevel <= 0) {
+        maxVisibilityLevel = 1;
+      }
+      pathway.setTransparencyLevel(maxVisibilityLevel + "");
+    }
+    for (final Element child : pathwayChildren) {
+      if (!isExcludedChildVisibility(child)) {
+        child.setVisibilityLevel(pathway.getTransparencyLevel());
+      }
+    }
+  }
+
+  private Set<Element> getPathwayChildren(final PathwayCompartment pathway) {
+    Set<Element> children = new HashSet<>();
+    for (Element element : pathway.getModel().getElements()) {
+      if (element.getPathway() == pathway) {
+        children.add(element);
+      }
+    }
+    return children;
+  }
+
   /**
    * Sets transparency level in hierarchical view for complex alias.
    *
@@ -320,20 +357,32 @@ public class CreateHierarchyCommand extends ModelCommand {
       complex.setTransparencyLevel(maxVisibilityLevel + "");
     }
     for (final Element child : complex.getElements()) {
-      if (child.getVisibilityLevel() == null || child.getVisibilityLevel().isEmpty()) {
+      if (!isExcludedChildVisibility(child)) {
         child.setVisibilityLevel(complex.getTransparencyLevel());
       }
     }
   }
 
+  private final Set<Element> excluded = new HashSet<>();
+
   /**
    * Sets transparency level in hierarchical view for all elements.
    *
    * @param sortedAliases list of aliases
    */
   private void settingOfTransparencyLevel(final List<Element> sortedAliases) {
+    excluded.clear();
+
+    for (final Element alias : sortedAliases) {
+      if (alias.getVisibilityLevel() != null && !alias.getVisibilityLevel().isEmpty()) {
+        excluded.add(alias);
+      }
+    }
+
     for (final Element alias : sortedAliases) {
-      if (alias instanceof Compartment) {
+      if (alias instanceof PathwayCompartment) {
+        settingTransparencyLevelForPathway((PathwayCompartment) alias);
+      } else if (alias instanceof Compartment) {
         settingTransparencyLevelForCompartment((Compartment) alias);
       } else if (alias instanceof Complex) {
         settingTransparencyLevelForComplex((Complex) alias);
-- 
GitLab