From fc0310de9834647a6f6bfa9c3f2230063f204136 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 20 Nov 2017 16:12:08 +0100 Subject: [PATCH] string<->species mapping is case insensitive this is used by ColorSchemaReader --- .../celldesigner/species/SpeciesMapping.java | 414 +++++++++--------- .../species/SpeciesMappingTest.java | 167 +++---- 2 files changed, 294 insertions(+), 287 deletions(-) diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMapping.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMapping.java index 933c6486fd..20a0223130 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMapping.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMapping.java @@ -1,207 +1,207 @@ -package lcsb.mapviewer.converter.model.celldesigner.species; - -import lcsb.mapviewer.common.exception.InvalidStateException; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerAntisenseRna; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerComplexSpecies; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerDegraded; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerDrug; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerGene; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerIon; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerPhenotype; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerProtein; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerRna; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSimpleMolecule; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSpecies; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerUnknown; -import lcsb.mapviewer.model.map.species.AntisenseRna; -import lcsb.mapviewer.model.map.species.Complex; -import lcsb.mapviewer.model.map.species.Degraded; -import lcsb.mapviewer.model.map.species.Drug; -import lcsb.mapviewer.model.map.species.Element; -import lcsb.mapviewer.model.map.species.Gene; -import lcsb.mapviewer.model.map.species.Ion; -import lcsb.mapviewer.model.map.species.Phenotype; -import lcsb.mapviewer.model.map.species.Protein; -import lcsb.mapviewer.model.map.species.Rna; -import lcsb.mapviewer.model.map.species.SimpleMolecule; -import lcsb.mapviewer.model.map.species.Unknown; - -/** - * This enum contains information about mapping between CellDesigner xml nodes - * and classes that extend {@link CellDesignerSpecies}. - * - * @author Piotr Gawron - * - */ -public enum SpeciesMapping { - /** - * {@link Protein}. - */ - PROTEIN(CellDesignerProtein.class, Protein.class, "PROTEIN"), // - - /** - * {@link Gene}. - */ - GENE(CellDesignerGene.class, Gene.class, "GENE"), // - - /** - * {@link ComplexSpecies}. - */ - COMPLEX(CellDesignerComplexSpecies.class, Complex.class, "COMPLEX"), // - - /** - * {@link SimpleMolecule}. - */ - SIMPLE_MOLECULE(CellDesignerSimpleMolecule.class, SimpleMolecule.class, "SIMPLE_MOLECULE"), // - - /** - * {@link Ion}. - */ - ION(CellDesignerIon.class, Ion.class, "ION"), // - - /** - * {@link Phenotype}. - */ - PHENOTYPE(CellDesignerPhenotype.class, Phenotype.class, "PHENOTYPE"), // - - /** - * {@link Drug}. - */ - DRUG(CellDesignerDrug.class, Drug.class, "DRUG"), // - - /** - * {@link Rna}. - */ - RNA(CellDesignerRna.class, Rna.class, "RNA"), // - - /** - * {@link AntisenseRna}. - */ - ANTISENSE_RNA(CellDesignerAntisenseRna.class, AntisenseRna.class, "ANTISENSE_RNA"), // - - /** - * {@link Unknown}. - */ - UNKNOWN(CellDesignerUnknown.class, Unknown.class, "UNKNOWN"), // - - /** - * {@link Degraded}. - */ - DEGRADED(CellDesignerDegraded.class, Degraded.class, "DEGRADED"); - - /** - * CellDesigner xml node type. - */ - private String cellDesignerString; - - /** - * Class that should be used to represent cell designer node. - */ - private Class<? extends CellDesignerSpecies<?>> cellDesignerClazz; - - /** - * Model class that represent the species. - */ - private Class<? extends Element> modelClazz; - - /** - * Default constructor. - * - * @param cellDesignerClazz - * {@link #clazz} - * @param modelClazz - * {@link #clazz class} in model corresponding to celldesigner object - * @param cellDesignerString - * {@link #cellDesignerString} - */ - SpeciesMapping(Class<?> cellDesignerClazz, Class<? extends Element> modelClazz, String cellDesignerString) { - this.cellDesignerString = cellDesignerString; - this.setCellDesignerClazz(cellDesignerClazz); - this.modelClazz = modelClazz; - } - - /** - * Sets new cellDesignerClazz value. - * - * @param cellDesignerClazz2 - * new cellDesignerClazz value - */ - @SuppressWarnings("unchecked") - private void setCellDesignerClazz(Class<?> cellDesignerClazz2) { - cellDesignerClazz = (Class<? extends CellDesignerSpecies<?>>) cellDesignerClazz2; - } - - /** - * @return the cellDesignerString - * @see #cellDesignerString - */ - public String getCellDesignerString() { - return cellDesignerString; - } - - /** - * @return the clazz - * @see #clazz - */ - public Class<? extends CellDesignerSpecies<?>> getCellDesignerClazz() { - return cellDesignerClazz; - } - - /** - * Returns model class that represent the species. - * - * @return model class that represent the species - */ - public Class<? extends Element> getModelClazz() { - return modelClazz; - } - - /** - * Creates instance of {@link CellDesignerSpecies} specific for this - * {@link SpeciesMapping}. - * - * @param result - * parameter that should be passed to the constructor - * @return new instance of {@link CellDesignerSpecies} specific for this - * {@link SpeciesMapping}. - */ - public CellDesignerSpecies<?> createSpecies(CellDesignerSpecies<?> result) { - try { - return cellDesignerClazz.getConstructor(result.getClass()).newInstance(result); - } catch (Exception e) { - throw new InvalidStateException(e); - } - } - - /** - * Returns {@link SpeciesMapping} for given CellDesigner xml node name. - * - * @param string - * CellDesigner xml node name - * @return {@link SpeciesMapping} for given CellDesigner xml node name - */ - public static SpeciesMapping getMappingByString(String string) { - for (SpeciesMapping mapping : SpeciesMapping.values()) { - if (mapping.getCellDesignerString().equals(string)) { - return mapping; - } - } - return null; - } - - /** - * Returns {@link SpeciesMapping} for given {@link CellDesignerSpecies} class. - * - * @param searchClazz - * {@link CellDesignerSpecies} class - * @return {@link SpeciesMapping} for given {@link CellDesignerSpecies} class - */ - public static SpeciesMapping getMappingByModelClass(Class<? extends Element> searchClazz) { - for (SpeciesMapping mapping : SpeciesMapping.values()) { - if (mapping.getModelClazz().isAssignableFrom(searchClazz)) { - return mapping; - } - } - return null; - } -} +package lcsb.mapviewer.converter.model.celldesigner.species; + +import lcsb.mapviewer.common.exception.InvalidStateException; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerAntisenseRna; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerComplexSpecies; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerDegraded; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerDrug; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerGene; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerIon; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerPhenotype; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerProtein; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerRna; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSimpleMolecule; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSpecies; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerUnknown; +import lcsb.mapviewer.model.map.species.AntisenseRna; +import lcsb.mapviewer.model.map.species.Complex; +import lcsb.mapviewer.model.map.species.Degraded; +import lcsb.mapviewer.model.map.species.Drug; +import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.Gene; +import lcsb.mapviewer.model.map.species.Ion; +import lcsb.mapviewer.model.map.species.Phenotype; +import lcsb.mapviewer.model.map.species.Protein; +import lcsb.mapviewer.model.map.species.Rna; +import lcsb.mapviewer.model.map.species.SimpleMolecule; +import lcsb.mapviewer.model.map.species.Unknown; + +/** + * This enum contains information about mapping between CellDesigner xml nodes + * and classes that extend {@link CellDesignerSpecies}. + * + * @author Piotr Gawron + * + */ +public enum SpeciesMapping { + /** + * {@link Protein}. + */ + PROTEIN(CellDesignerProtein.class, Protein.class, "PROTEIN"), // + + /** + * {@link Gene}. + */ + GENE(CellDesignerGene.class, Gene.class, "GENE"), // + + /** + * {@link ComplexSpecies}. + */ + COMPLEX(CellDesignerComplexSpecies.class, Complex.class, "COMPLEX"), // + + /** + * {@link SimpleMolecule}. + */ + SIMPLE_MOLECULE(CellDesignerSimpleMolecule.class, SimpleMolecule.class, "SIMPLE_MOLECULE"), // + + /** + * {@link Ion}. + */ + ION(CellDesignerIon.class, Ion.class, "ION"), // + + /** + * {@link Phenotype}. + */ + PHENOTYPE(CellDesignerPhenotype.class, Phenotype.class, "PHENOTYPE"), // + + /** + * {@link Drug}. + */ + DRUG(CellDesignerDrug.class, Drug.class, "DRUG"), // + + /** + * {@link Rna}. + */ + RNA(CellDesignerRna.class, Rna.class, "RNA"), // + + /** + * {@link AntisenseRna}. + */ + ANTISENSE_RNA(CellDesignerAntisenseRna.class, AntisenseRna.class, "ANTISENSE_RNA"), // + + /** + * {@link Unknown}. + */ + UNKNOWN(CellDesignerUnknown.class, Unknown.class, "UNKNOWN"), // + + /** + * {@link Degraded}. + */ + DEGRADED(CellDesignerDegraded.class, Degraded.class, "DEGRADED"); + + /** + * CellDesigner xml node type. + */ + private String cellDesignerString; + + /** + * Class that should be used to represent cell designer node. + */ + private Class<? extends CellDesignerSpecies<?>> cellDesignerClazz; + + /** + * Model class that represent the species. + */ + private Class<? extends Element> modelClazz; + + /** + * Default constructor. + * + * @param cellDesignerClazz + * {@link #clazz} + * @param modelClazz + * {@link #clazz class} in model corresponding to CellDesigner object + * @param cellDesignerString + * {@link #cellDesignerString} + */ + SpeciesMapping(Class<?> cellDesignerClazz, Class<? extends Element> modelClazz, String cellDesignerString) { + this.cellDesignerString = cellDesignerString; + this.setCellDesignerClazz(cellDesignerClazz); + this.modelClazz = modelClazz; + } + + /** + * Sets new cellDesignerClazz value. + * + * @param cellDesignerClazz2 + * new cellDesignerClazz value + */ + @SuppressWarnings("unchecked") + private void setCellDesignerClazz(Class<?> cellDesignerClazz2) { + cellDesignerClazz = (Class<? extends CellDesignerSpecies<?>>) cellDesignerClazz2; + } + + /** + * @return the cellDesignerString + * @see #cellDesignerString + */ + public String getCellDesignerString() { + return cellDesignerString; + } + + /** + * @return the clazz + * @see #clazz + */ + public Class<? extends CellDesignerSpecies<?>> getCellDesignerClazz() { + return cellDesignerClazz; + } + + /** + * Returns model class that represent the species. + * + * @return model class that represent the species + */ + public Class<? extends Element> getModelClazz() { + return modelClazz; + } + + /** + * Creates instance of {@link CellDesignerSpecies} specific for this + * {@link SpeciesMapping}. + * + * @param result + * parameter that should be passed to the constructor + * @return new instance of {@link CellDesignerSpecies} specific for this + * {@link SpeciesMapping}. + */ + public CellDesignerSpecies<?> createSpecies(CellDesignerSpecies<?> result) { + try { + return cellDesignerClazz.getConstructor(result.getClass()).newInstance(result); + } catch (Exception e) { + throw new InvalidStateException(e); + } + } + + /** + * Returns {@link SpeciesMapping} for given CellDesigner xml node name. + * + * @param string + * CellDesigner xml node name + * @return {@link SpeciesMapping} for given CellDesigner xml node name + */ + public static SpeciesMapping getMappingByString(String string) { + for (SpeciesMapping mapping : SpeciesMapping.values()) { + if (mapping.getCellDesignerString().equalsIgnoreCase(string)) { + return mapping; + } + } + return null; + } + + /** + * Returns {@link SpeciesMapping} for given {@link CellDesignerSpecies} class. + * + * @param searchClazz + * {@link CellDesignerSpecies} class + * @return {@link SpeciesMapping} for given {@link CellDesignerSpecies} class + */ + public static SpeciesMapping getMappingByModelClass(Class<? extends Element> searchClazz) { + for (SpeciesMapping mapping : SpeciesMapping.values()) { + if (mapping.getModelClazz().isAssignableFrom(searchClazz)) { + return mapping; + } + } + return null; + } +} diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMappingTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMappingTest.java index 2b76440dfc..75f64f4d76 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMappingTest.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/species/SpeciesMappingTest.java @@ -1,80 +1,87 @@ -package lcsb.mapviewer.converter.model.celldesigner.species; - -import static org.junit.Assert.*; - -import java.lang.reflect.Field; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; - -import lcsb.mapviewer.common.exception.InvalidStateException; -import lcsb.mapviewer.common.exception.NotImplementedException; -import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSpecies; -import lcsb.mapviewer.model.map.species.Protein; - -public class SpeciesMappingTest { - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - - @Test - public void testAllValues() { - for (SpeciesMapping value : SpeciesMapping.values()) { - assertNotNull(SpeciesMapping.valueOf(value.toString())); - } - } - - @Test - public void testCreateInvalidSpeciesImpl() throws Exception { - // artificial implementation of Species that is invalid - class InvalidSpecies extends CellDesignerSpecies<Protein> { - private static final long serialVersionUID = 1L; - - @SuppressWarnings("unused") - public InvalidSpecies() { - throw new NotImplementedException(); - } - } - - // mopdify one of the elements of SpeciesMapping so it will have invalid - // implementation - SpeciesMapping typeToModify = SpeciesMapping.ANTISENSE_RNA; - Class<? extends CellDesignerSpecies<?>> clazz = typeToModify.getCellDesignerClazz(); - - try { - Field field = typeToModify.getClass().getDeclaredField("cellDesignerClazz"); - field.setAccessible(true); - field.set(typeToModify, InvalidSpecies.class); - - // and check if we catch properly information about problematic - // implementation - typeToModify.createSpecies(new CellDesignerSpecies<Protein>()); - fail("Exceptione expected"); - } catch (InvalidStateException e) { - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - // restore correct values for the modified type (if not then other test - // might fail...) - Field field = typeToModify.getClass().getDeclaredField("cellDesignerClazz"); - field.setAccessible(true); - field.set(typeToModify, clazz); - } - - } - - -} +package lcsb.mapviewer.converter.model.celldesigner.species; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; + +import lcsb.mapviewer.common.exception.InvalidStateException; +import lcsb.mapviewer.common.exception.NotImplementedException; +import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerSpecies; +import lcsb.mapviewer.model.map.species.Protein; + +public class SpeciesMappingTest { + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAllValues() { + for (SpeciesMapping value : SpeciesMapping.values()) { + assertNotNull(SpeciesMapping.valueOf(value.toString())); + } + } + + @Test + public void testGetMappingByStringCaseInsensitive() throws Exception { + SpeciesMapping mapping = SpeciesMapping.getMappingByString("PHENOTYPE"); + assertNotNull(mapping); + mapping = SpeciesMapping.getMappingByString("phenotype"); + assertNotNull(mapping); + } + + @Test + public void testCreateInvalidSpeciesImpl() throws Exception { + // artificial implementation of Species that is invalid + class InvalidSpecies extends CellDesignerSpecies<Protein> { + private static final long serialVersionUID = 1L; + + @SuppressWarnings("unused") + public InvalidSpecies() { + throw new NotImplementedException(); + } + } + + // modify one of the elements of SpeciesMapping so it will have invalid + // implementation + SpeciesMapping typeToModify = SpeciesMapping.ANTISENSE_RNA; + Class<? extends CellDesignerSpecies<?>> clazz = typeToModify.getCellDesignerClazz(); + + try { + Field field = typeToModify.getClass().getDeclaredField("cellDesignerClazz"); + field.setAccessible(true); + field.set(typeToModify, InvalidSpecies.class); + + // and check if we catch properly information about problematic + // implementation + typeToModify.createSpecies(new CellDesignerSpecies<Protein>()); + fail("Exceptione expected"); + } catch (InvalidStateException e) { + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + // restore correct values for the modified type (if not then other test + // might fail...) + Field field = typeToModify.getClass().getDeclaredField("cellDesignerClazz"); + field.setAccessible(true); + field.set(typeToModify, clazz); + } + + } + +} -- GitLab