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 00ea8ca9115c4b911d8bc5070739a15b48fb2d65..46400e3d02acb02b590ace60c7d8b0a44fbfb4d3 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 db837a494569c8ea19b0a06f19b23af18b0e0754..93ab076bbb6399bd49d4d445e9bab6119d416a48 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 a38a634c097cc21692fe90c3fa9b70cc4781bc78..d71e8f17a4198b25f8e4c3f6736781876975cc8f 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 bacc86fe1bd0cb526203cd376f465c01c1198e43..ee6f7fe47e30253c0b68cdb31bfc97128c7cb43d 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;