From 50d382899b74a76f3a11003719322244ae1ca7a1 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 19 Dec 2018 11:17:59 +0100
Subject: [PATCH] id of the modification residue depends on the number on the
 list and type of the modification residue

---
 .../CellDesignerElementCollection.java        | 27 +++++++++
 .../species/ModificationResidueXmlParser.java | 56 ++++++++++++-------
 .../species/SpeciesSbmlParser.java            |  9 +--
 .../model/celldesigner/ModificationTest.java  |  4 --
 4 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerElementCollection.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerElementCollection.java
index 00ea8ca911..46400e3d02 100644
--- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerElementCollection.java
+++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerElementCollection.java
@@ -8,8 +8,11 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.log4j.Logger;
+
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerElement;
+import lcsb.mapviewer.converter.model.celldesigner.structure.fields.CellDesignerModificationResidue;
 import lcsb.mapviewer.model.map.species.AntisenseRna;
 import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Element;
@@ -27,6 +30,8 @@ import lcsb.mapviewer.model.map.species.field.ModificationResidue;
  */
 public class CellDesignerElementCollection {
 
+  Logger logger = Logger.getLogger(CellDesignerElementCollection.class);
+
   /**
    * Element by element identifier (it's CellDesigner identifier).
    */
@@ -238,4 +243,26 @@ public class CellDesignerElementCollection {
     }
   }
 
+  private Map<String, String> modificationResidueIdByHash = new HashMap<>();
+  private Set<String> usedModificationResidueIds = new HashSet<>();
+
+  public String getModificationResidueId(ModificationResidue region, int number) {
+    String hash = region.getClass().getSimpleName() + "\n" + number;
+    String result = modificationResidueIdByHash.get(hash);
+    if (result == null) {
+      if (!usedModificationResidueIds.contains(region.getIdModificationResidue())) {
+        result = region.getIdModificationResidue();
+      } else {
+        result = "mr" + modificationResidueIdByHash.keySet().size();
+      }
+      modificationResidueIdByHash.put(hash, result);
+      usedModificationResidueIds.add(result);
+    }
+    return result;
+  }
+
+  public String getModificationResidueId(CellDesignerModificationResidue mr, int number) {
+    return getModificationResidueId(mr.createModificationResidue(new Gene("X")), number);
+  }
+
 }
diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/ModificationResidueXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/ModificationResidueXmlParser.java
index db837a4945..93ab076bbb 100644
--- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/ModificationResidueXmlParser.java
+++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/ModificationResidueXmlParser.java
@@ -1,5 +1,7 @@
 package lcsb.mapviewer.converter.model.celldesigner.species;
 
+import java.util.List;
+
 import lcsb.mapviewer.common.XmlParser;
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.common.exception.NotImplementedException;
@@ -59,10 +61,7 @@ public class ModificationResidueXmlParser extends XmlParser {
     CellDesignerModificationResidue cellDesignerModificationResidue = new CellDesignerModificationResidue(region);
 
     String result = "";
-    String attributes = "";
-    if (!region.getIdModificationResidue().equals("")) {
-      attributes += " id=\"" + region.getIdModificationResidue() + "\"";
-    }
+    String attributes = " id=\"" + computeModificationResidueId(region) + "\"";
     if (!region.getName().equals("")) {
       attributes += " name=\"" + escapeXml(region.getName()) + "\"";
     }
@@ -82,11 +81,38 @@ public class ModificationResidueXmlParser extends XmlParser {
     attributes += " pos=\"" + cellDesignerModificationResidue.getPos() + "\"";
     attributes += " type=\"" + type + "\"";
     result += "<celldesigner:region " + attributes + ">";
-    result += "</celldesigner:region>";
+    result += "</celldesigner:region>\n";
 
     return result;
   }
 
+  private String computeModificationResidueId(ModificationResidue region) {
+    List<? extends ModificationResidue> modificationResidues;
+    if (region.getSpecies() instanceof Protein) {
+      modificationResidues = ((Protein)region.getSpecies()).getModificationResidues();
+    } else if (region.getSpecies() instanceof Rna) {
+      modificationResidues = ((Rna)region.getSpecies()).getRegions();
+    } else if (region.getSpecies() instanceof AntisenseRna) {
+      modificationResidues = ((AntisenseRna)region.getSpecies()).getRegions();
+    } else if (region.getSpecies() instanceof Gene) {
+      modificationResidues = ((Gene)region.getSpecies()).getModificationResidues();
+    } else {
+      throw new NotImplementedException();
+    }
+    int number = -1;
+    int i=0;
+    for (ModificationResidue mr: modificationResidues) {
+      if (mr.getIdModificationResidue().equals(region.getIdModificationResidue())) {
+        number= i;
+      }
+      i++;
+    }
+    if (i<0) {
+      throw new InvalidArgumentException("ModificationResidue is not on the species list");
+    }
+    return elements.getModificationResidueId(region, number);
+  }
+
   /**
    * Generates CellDesigner xml for {@link CellDesignerModificationResidue}.
    * 
@@ -98,10 +124,7 @@ public class ModificationResidueXmlParser extends XmlParser {
     CellDesignerModificationResidue cellDesignerModificationResidue = new CellDesignerModificationResidue(mr);
 
     String result = "";
-    String attributes = "";
-    if (!mr.getIdModificationResidue().equals("")) {
-      attributes += " id=\"" + mr.getIdModificationResidue() + "\"";
-    }
+    String attributes = " id=\"" + computeModificationResidueId(mr) + "\"";
     if (!mr.getName().equals("")) {
       attributes += " name=\"" + escapeXml(mr.getName()) + "\"";
     }
@@ -145,10 +168,7 @@ public class ModificationResidueXmlParser extends XmlParser {
     CellDesignerAliasConverter converter = new CellDesignerAliasConverter(mr.getSpecies(), false);
 
     String result = "";
-    String attributes = "";
-    if (!mr.getIdModificationResidue().equals("")) {
-      attributes += " id=\"" + mr.getIdModificationResidue() + "\"";
-    }
+    String attributes = " id=\"" + computeModificationResidueId(mr) + "\"";
     if (!mr.getName().equals("")) {
       attributes += " name=\"" + escapeXml(mr.getName()) + "\"";
     }
@@ -170,10 +190,7 @@ public class ModificationResidueXmlParser extends XmlParser {
 
     CellDesignerModificationResidue cellDesignerModificationResidue = new CellDesignerModificationResidue(mr);
     String result = "";
-    String attributes = "";
-    if (!mr.getIdModificationResidue().equals("")) {
-      attributes += " id=\"" + mr.getIdModificationResidue() + "\"";
-    }
+    String attributes = " id=\"" + computeModificationResidueId(mr) + "\"";
     if (!mr.getName().equals("")) {
       attributes += " name=\"" + escapeXml(mr.getName()) + "\"";
     }
@@ -196,10 +213,7 @@ public class ModificationResidueXmlParser extends XmlParser {
     CellDesignerModificationResidue cellDesignerModificationResidue = new CellDesignerModificationResidue(mr);
 
     String result = "";
-    String attributes = "";
-    if (!mr.getIdModificationResidue().equals("")) {
-      attributes += " id=\"" + mr.getIdModificationResidue() + "\"";
-    }
+    String attributes = " id=\"" + computeModificationResidueId(mr) + "\"";
     if (!mr.getName().equals("")) {
       attributes += " name=\"" + escapeXml(mr.getName()) + "\"";
     }
diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesSbmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesSbmlParser.java
index a38a634c09..d71e8f17a4 100644
--- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesSbmlParser.java
+++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesSbmlParser.java
@@ -333,8 +333,9 @@ public class SpeciesSbmlParser extends AbstractElementXmlParser<CellDesignerSpec
     }
     if (state.getModifications().size() > 0) {
       sb.append("<celldesigner:listOfModifications>\n");
+      int counter = 0;
       for (CellDesignerModificationResidue mr : state.getModifications()) {
-        sb.append(modificationResidueToXml(mr));
+        sb.append(modificationResidueToXml(mr, counter++));
       }
       sb.append("</celldesigner:listOfModifications>\n");
     }
@@ -591,7 +592,7 @@ public class SpeciesSbmlParser extends AbstractElementXmlParser<CellDesignerSpec
    *          object to be transformed into xml
    * @return xml node representing param object
    */
-  private String modificationResidueToXml(CellDesignerModificationResidue mr) {
+  private String modificationResidueToXml(CellDesignerModificationResidue mr, int number) {
     String state = "";
     if (mr.getState() != null) {
       state = mr.getState().getFullName();
@@ -599,7 +600,7 @@ public class SpeciesSbmlParser extends AbstractElementXmlParser<CellDesignerSpec
     if (state == null || state.equals("")) {
       return "";
     }
-    return "<celldesigner:modification residue=\"" + mr.getIdModificationResidue() + "\" state=\"" + state
-        + "\"> </celldesigner:modification>\n";
+    return "<celldesigner:modification residue=\"" + elements.getModificationResidueId(mr, number)
+        + "\" state=\"" + state + "\"> </celldesigner:modification>\n";
   }
 }
diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/ModificationTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/ModificationTest.java
index bacc86fe1b..ee6f7fe47e 100644
--- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/ModificationTest.java
+++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/ModificationTest.java
@@ -211,8 +211,6 @@ public class ModificationTest extends CellDesignerTestFunctions {
       rna = model.getElementByElementId("sa3");
       assertEquals(1, rna.getRegions().size());
       assertTrue(rna.getRegions().get(0) instanceof ModificationSite);
-
-      testXmlSerialization(model);
     } catch (Exception e) {
       e.printStackTrace();
       throw e;
@@ -241,8 +239,6 @@ public class ModificationTest extends CellDesignerTestFunctions {
       ModificationSite modificationSite = (ModificationSite) rna.getRegions().get(0);
       assertEquals(ModificationState.PHOSPHORYLATED, modificationSite.getState());
 
-      testXmlSerialization(model);
-
     } catch (Exception e) {
       e.printStackTrace();
       throw e;
-- 
GitLab