From 605b2066a431051895fbc51e74dfabb0fc0f31ab Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 20 Nov 2017 16:41:34 +0100 Subject: [PATCH] allow filtering by protein class (that is super class for all types of proteins) --- .../mapviewer/commands/ColorModelCommand.java | 742 +++++++++--------- .../commands/ColorModelCommandTest.java | 703 +++++++++-------- 2 files changed, 742 insertions(+), 703 deletions(-) diff --git a/model-command/src/main/java/lcsb/mapviewer/commands/ColorModelCommand.java b/model-command/src/main/java/lcsb/mapviewer/commands/ColorModelCommand.java index ceb5a31f73..534d8432b4 100644 --- a/model-command/src/main/java/lcsb/mapviewer/commands/ColorModelCommand.java +++ b/model-command/src/main/java/lcsb/mapviewer/commands/ColorModelCommand.java @@ -1,365 +1,377 @@ -package lcsb.mapviewer.commands; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; - -import lcsb.mapviewer.common.Pair; -import lcsb.mapviewer.common.exception.NotImplementedException; -import lcsb.mapviewer.model.graphics.ArrowTypeData; -import lcsb.mapviewer.model.map.BioEntity; -import lcsb.mapviewer.model.map.MiriamData; -import lcsb.mapviewer.model.map.MiriamRelationType; -import lcsb.mapviewer.model.map.MiriamType; -import lcsb.mapviewer.model.map.compartment.Compartment; -import lcsb.mapviewer.model.map.layout.ColorSchema; -import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.model.ModelSubmodelConnection; -import lcsb.mapviewer.model.map.reaction.AbstractNode; -import lcsb.mapviewer.model.map.reaction.Product; -import lcsb.mapviewer.model.map.reaction.Reactant; -import lcsb.mapviewer.model.map.reaction.Reaction; -import lcsb.mapviewer.model.map.species.Element; -import lcsb.mapviewer.model.map.species.Species; -import lcsb.mapviewer.modelutils.map.ElementUtils; - -/** - * This {@link ModelCommand} colors a model (nodes and reactions) according to - * set of {@link ColorSchema rules}. - * - * @author Piotr Gawron - * - */ -public class ColorModelCommand extends ModelCommand { - - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private Logger logger = Logger.getLogger(ColorModelCommand.class); - - /** - * Set of color schemas used in this command to color model. - */ - private Collection<ColorSchema> schemas; - - /** - * Object that helps to convert {@link ColorSchema} values into colors. - * - */ - private ColorExtractor colorExtractor; - - /** - * Util class for all {@link BioEntity} elements. - */ - private ElementUtils eu = new ElementUtils(); - - /** - * Default constructor. - * - * @param colorExtractor - * object that helps to convert {@link ColorSchema} values into - * colors - * @param model - * original model - * @param schemas - * set of color schemas used in this command to color model. - */ - public ColorModelCommand(Model model, Collection<ColorSchema> schemas, ColorExtractor colorExtractor) { - super(model); - this.schemas = schemas; - this.colorExtractor = colorExtractor; - } - - /** - * Applies color schema into the reaction. - * - * @param reaction - * object to be colored - * @param schema - * color schema to be used - * @throws InvalidColorSchemaException - * thrown when {@link Reaction} was already colored by other schema - */ - private void applyColor(Reaction reaction, ColorSchema schema) throws InvalidColorSchemaException { - if (!reaction.getReactants().get(0).getLine().getColor().equals(Color.BLACK)) { - throw new InvalidColorSchemaException("At least two rows try to set color to reaction: " + reaction.getIdReaction()); - } - - Color color = colorExtractor.getNormalizedColor(schema); - for (AbstractNode node : reaction.getNodes()) { - node.getLine().setColor(color); - if (schema.getLineWidth() != null) { - node.getLine().setWidth(schema.getLineWidth()); - } - } - if (schema.getReverseReaction() != null && schema.getReverseReaction()) { - ArrowTypeData atdBegining = reaction.getReactants().get(0).getLine().getBeginAtd(); - ArrowTypeData atdEnd = reaction.getProducts().get(0).getLine().getEndAtd(); - for (Reactant reactant : reaction.getReactants()) { - reactant.getLine().setBeginAtd(atdEnd); - } - for (Product product : reaction.getProducts()) { - product.getLine().setEndAtd(atdBegining); - } - } - } - - /** - * Checks if the coloring schema should be used for the reaction. - * - * @param reaction - * reaction to which coloring schema is checked - * @param schema - * coloring schema - * @return <code>true</code> if coloring schema should be used for - * {@link Reaction}, <code>false</code> otherwise - */ - private boolean match(Reaction reaction, ColorSchema schema) { - if (schema.getName() != null) { - return false; - } - - if (schema.getReactionIdentifier() != null && schema.getReactionIdentifier().equalsIgnoreCase(reaction.getIdReaction())) { - return true; - } - if (schema.getGeneralIdentifier() != null && !schema.getGeneralIdentifier().equals("")) { - MiriamData md = MiriamType.getMiriamDataFromIdentifier(schema.getGeneralIdentifier()); - - if (reaction.getMiriamData().contains(md)) { - return true; - } - } - for (Pair<MiriamType, String> pair : schema.getIdentifierColumns()) { - if (pair.getRight() != null && !pair.getRight().equals("")) { - - MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, pair.getLeft(), pair.getRight()); - if (reaction.getMiriamData().contains(md)) { - return true; - } - } - } - - return false; - } - - /** - * Applies color schema into the {@link Element}. - * - * @param element - * object to be colored - * @param schema - * color schema to be used - * @throws InvalidColorSchemaException - * thrown when alias was already colored by other schema - */ - private void applyColor(Element element, ColorSchema schema) throws InvalidColorSchemaException { - if (element instanceof Species) { - if (!element.getColor().equals(Color.WHITE)) { - throw new InvalidColorSchemaException("At least two rows try to set color to element: " + element.getName()); - } - element.setColor(colorExtractor.getNormalizedColor(schema)); - } - } - - /** - * Checks if the coloring schema should be used for the {@link Element}. - * - * @param element - * {@link Element} for which coloring schema is checked - * @param schema - * coloring schema - * @return <code>true</code> if coloring schema should be used for - * {@link Element}, <code>false</code> otherwise - */ - protected boolean match(Element element, ColorSchema schema) { - if (element instanceof Species) { - if (schema.getName() != null) { - if (!element.getName().equalsIgnoreCase(schema.getName())) { - return false; - } - } - if (schema.getTypes().size() > 0) { - if (!schema.getTypes().contains(element.getClass())) { - return false; - } - } - if (schema.getGeneralIdentifier() != null && !schema.getGeneralIdentifier().equals("")) { - MiriamData md = MiriamType.getMiriamDataFromIdentifier(schema.getGeneralIdentifier()); - - if (!element.getMiriamData().contains(md)) { - return false; - } - } - for (Pair<MiriamType, String> pair : schema.getIdentifierColumns()) { - if (pair.getRight() != null && !pair.getRight().equals("")) { - MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, pair.getLeft(), pair.getRight()); - if (!element.getMiriamData().contains(md)) { - return false; - } - } - } - - if (schema.getCompartments().size() > 0) { - boolean found = false; - for (Compartment compartment : element.getModelData().getModel().getCompartments()) { - for (String compartmentName : schema.getCompartments()) { - if (compartment.getName().equalsIgnoreCase(compartmentName)) { - if (compartment.cross(element)) { - found = true; - } - } - } - } - if (!found) { - return false; - } - } - - // if we have reaction id to match then reject - if (schema.getReactionIdentifier() != null) { - return false; - } - return true; - } else { - return false; - } - } - - /** - * Checks which coloring schemas weren't used in the model coloring process. - * - * @return list of schemas that weren't used during coloring - */ - public Collection<ColorSchema> getMissingSchema() { - List<ColorSchema> result = new ArrayList<ColorSchema>(); - for (ColorSchema schema : schemas) { - boolean found = false; - for (Element element : getModel().getElements()) { - if (match(element, schema)) { - found = true; - } - } - for (Reaction reaction : getModel().getReactions()) { - if (match(reaction, schema)) { - found = true; - } - } - if (!found) { - result.add(schema); - } - } - return result; - } - - /** - * Returns list of elements ({@link Reaction reactions} and {@link Element - * elements}) that are modified by the coloring command. - * - * @return {@link Map}, where key corresponds to modified {@link Reaction} or - * {@link Element} and value is a {@link ColorSchema} that should be - * used for coloring - * @throws InvalidColorSchemaException - * thrown when more than one {@link ColorSchema} match an element - */ - public Map<Object, ColorSchema> getModifiedElements() throws InvalidColorSchemaException { - Map<Object, ColorSchema> result = new HashMap<>(); - - List<Model> models = new ArrayList<>(); - models.add(getModel()); - models.addAll(getModel().getSubmodels()); - for (Model model2 : models) { - for (ColorSchema schema : schemas) { - for (Element element : model2.getElements()) { - if (match(element, schema)) { - if (result.get(element) != null && !colorExtractor.getNormalizedColor(result.get(element)).equals(Color.WHITE)) { - throw new InvalidColorSchemaException(eu.getElementTag(element) + "Element is colored by more than one rule."); - } - result.put(element, schema); - } - } - for (Reaction reaction : model2.getReactions()) { - if (match(reaction, schema)) { - if (result.get(reaction) != null && !colorExtractor.getNormalizedColor(result.get(reaction)).equals(Color.WHITE)) { - throw new InvalidColorSchemaException(eu.getElementTag(reaction) + "Reaction is colored by more than one rule."); - } - result.put(reaction, schema); - } - } - } - } - return result; - } - - @Override - protected void undoImplementation() throws CommandExecutionException { - throw new NotImplementedException(); - - } - - @Override - protected void redoImplementation() throws CommandExecutionException { - throw new NotImplementedException(); - } - - /** - * Colors parameter model using coloring for this command. This method is used - * internally to color either top {@link Model} or one of it's sumbodels. - * - * @param result - * model to color - * @param top - * is the model a top (parent) model - * @throws InvalidColorSchemaException - * thrown when set of {@link ColorSchema} is invalid - */ - private void colorModel(Model result, boolean top) throws InvalidColorSchemaException { - - for (Element element : result.getElements()) { - element.setColor(Color.WHITE); - } - for (Reaction reaction : result.getReactions()) { - for (AbstractNode node : reaction.getNodes()) { - node.getLine().setColor(Color.BLACK); - } - } - - for (ColorSchema schema : schemas) { - for (Element element : result.getElements()) { - if (match(element, schema)) { - schema.setMatches(schema.getMatches() + 1); - applyColor(element, schema); - } - } - for (Reaction reaction : result.getReactions()) { - if (match(reaction, schema)) { - schema.setMatches(schema.getMatches() + 1); - applyColor(reaction, schema); - } - } - } - - if (top) { - for (ModelSubmodelConnection connection : result.getSubmodelConnections()) { - colorModel(connection.getSubmodel().getModel(), false); - } - } - - } - - @Override - protected void executeImplementation() throws CommandExecutionException { - try { - colorModel(getModel(), true); - } catch (InvalidColorSchemaException e) { - throw new CommandExecutionException(e); - } - } - -} +package lcsb.mapviewer.commands; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import lcsb.mapviewer.common.Pair; +import lcsb.mapviewer.common.exception.NotImplementedException; +import lcsb.mapviewer.model.graphics.ArrowTypeData; +import lcsb.mapviewer.model.map.BioEntity; +import lcsb.mapviewer.model.map.MiriamData; +import lcsb.mapviewer.model.map.MiriamRelationType; +import lcsb.mapviewer.model.map.MiriamType; +import lcsb.mapviewer.model.map.compartment.Compartment; +import lcsb.mapviewer.model.map.layout.ColorSchema; +import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.model.ModelSubmodelConnection; +import lcsb.mapviewer.model.map.reaction.AbstractNode; +import lcsb.mapviewer.model.map.reaction.Product; +import lcsb.mapviewer.model.map.reaction.Reactant; +import lcsb.mapviewer.model.map.reaction.Reaction; +import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.Species; +import lcsb.mapviewer.modelutils.map.ElementUtils; + +/** + * This {@link ModelCommand} colors a model (nodes and reactions) according to + * set of {@link ColorSchema rules}. + * + * @author Piotr Gawron + * + */ +public class ColorModelCommand extends ModelCommand { + + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private Logger logger = Logger.getLogger(ColorModelCommand.class); + + /** + * Set of color schemas used in this command to color model. + */ + private Collection<ColorSchema> schemas; + + /** + * Object that helps to convert {@link ColorSchema} values into colors. + * + */ + private ColorExtractor colorExtractor; + + /** + * Util class for all {@link BioEntity} elements. + */ + private ElementUtils eu = new ElementUtils(); + + /** + * Default constructor. + * + * @param colorExtractor + * object that helps to convert {@link ColorSchema} values into colors + * @param model + * original model + * @param schemas + * set of color schemas used in this command to color model. + */ + public ColorModelCommand(Model model, Collection<ColorSchema> schemas, ColorExtractor colorExtractor) { + super(model); + this.schemas = schemas; + this.colorExtractor = colorExtractor; + } + + /** + * Applies color schema into the reaction. + * + * @param reaction + * object to be colored + * @param schema + * color schema to be used + * @throws InvalidColorSchemaException + * thrown when {@link Reaction} was already colored by other schema + */ + private void applyColor(Reaction reaction, ColorSchema schema) throws InvalidColorSchemaException { + if (!reaction.getReactants().get(0).getLine().getColor().equals(Color.BLACK)) { + throw new InvalidColorSchemaException( + "At least two rows try to set color to reaction: " + reaction.getIdReaction()); + } + + Color color = colorExtractor.getNormalizedColor(schema); + for (AbstractNode node : reaction.getNodes()) { + node.getLine().setColor(color); + if (schema.getLineWidth() != null) { + node.getLine().setWidth(schema.getLineWidth()); + } + } + if (schema.getReverseReaction() != null && schema.getReverseReaction()) { + ArrowTypeData atdBegining = reaction.getReactants().get(0).getLine().getBeginAtd(); + ArrowTypeData atdEnd = reaction.getProducts().get(0).getLine().getEndAtd(); + for (Reactant reactant : reaction.getReactants()) { + reactant.getLine().setBeginAtd(atdEnd); + } + for (Product product : reaction.getProducts()) { + product.getLine().setEndAtd(atdBegining); + } + } + } + + /** + * Checks if the coloring schema should be used for the reaction. + * + * @param reaction + * reaction to which coloring schema is checked + * @param schema + * coloring schema + * @return <code>true</code> if coloring schema should be used for + * {@link Reaction}, <code>false</code> otherwise + */ + private boolean match(Reaction reaction, ColorSchema schema) { + if (schema.getName() != null) { + return false; + } + + if (schema.getReactionIdentifier() != null + && schema.getReactionIdentifier().equalsIgnoreCase(reaction.getIdReaction())) { + return true; + } + if (schema.getGeneralIdentifier() != null && !schema.getGeneralIdentifier().equals("")) { + MiriamData md = MiriamType.getMiriamDataFromIdentifier(schema.getGeneralIdentifier()); + + if (reaction.getMiriamData().contains(md)) { + return true; + } + } + for (Pair<MiriamType, String> pair : schema.getIdentifierColumns()) { + if (pair.getRight() != null && !pair.getRight().equals("")) { + + MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, pair.getLeft(), pair.getRight()); + if (reaction.getMiriamData().contains(md)) { + return true; + } + } + } + + return false; + } + + /** + * Applies color schema into the {@link Element}. + * + * @param element + * object to be colored + * @param schema + * color schema to be used + * @throws InvalidColorSchemaException + * thrown when alias was already colored by other schema + */ + private void applyColor(Element element, ColorSchema schema) throws InvalidColorSchemaException { + if (element instanceof Species) { + if (!element.getColor().equals(Color.WHITE)) { + throw new InvalidColorSchemaException("At least two rows try to set color to element: " + element.getName()); + } + element.setColor(colorExtractor.getNormalizedColor(schema)); + } + } + + /** + * Checks if the coloring schema should be used for the {@link Element}. + * + * @param element + * {@link Element} for which coloring schema is checked + * @param schema + * coloring schema + * @return <code>true</code> if coloring schema should be used for + * {@link Element}, <code>false</code> otherwise + */ + protected boolean match(Element element, ColorSchema schema) { + if (element instanceof Species) { + if (schema.getName() != null) { + if (!element.getName().equalsIgnoreCase(schema.getName())) { + return false; + } + } + if (schema.getTypes().size() > 0) { + boolean found = false; + for (Class<?> clazz : schema.getTypes()) { + if (clazz.isAssignableFrom(element.getClass())) { + found = true; + } + } + + if (!found) { + return false; + } + } + if (schema.getGeneralIdentifier() != null && !schema.getGeneralIdentifier().equals("")) { + MiriamData md = MiriamType.getMiriamDataFromIdentifier(schema.getGeneralIdentifier()); + + if (!element.getMiriamData().contains(md)) { + return false; + } + } + for (Pair<MiriamType, String> pair : schema.getIdentifierColumns()) { + if (pair.getRight() != null && !pair.getRight().equals("")) { + MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, pair.getLeft(), pair.getRight()); + if (!element.getMiriamData().contains(md)) { + return false; + } + } + } + + if (schema.getCompartments().size() > 0) { + boolean found = false; + for (Compartment compartment : element.getModelData().getModel().getCompartments()) { + for (String compartmentName : schema.getCompartments()) { + if (compartment.getName().equalsIgnoreCase(compartmentName)) { + if (compartment.cross(element)) { + found = true; + } + } + } + } + if (!found) { + return false; + } + } + + // if we have reaction id to match then reject + if (schema.getReactionIdentifier() != null) { + return false; + } + return true; + } else { + return false; + } + } + + /** + * Checks which coloring schemas weren't used in the model coloring process. + * + * @return list of schemas that weren't used during coloring + */ + public Collection<ColorSchema> getMissingSchema() { + List<ColorSchema> result = new ArrayList<ColorSchema>(); + for (ColorSchema schema : schemas) { + boolean found = false; + for (Element element : getModel().getElements()) { + if (match(element, schema)) { + found = true; + } + } + for (Reaction reaction : getModel().getReactions()) { + if (match(reaction, schema)) { + found = true; + } + } + if (!found) { + result.add(schema); + } + } + return result; + } + + /** + * Returns list of elements ({@link Reaction reactions} and {@link Element + * elements}) that are modified by the coloring command. + * + * @return {@link Map}, where key corresponds to modified {@link Reaction} or + * {@link Element} and value is a {@link ColorSchema} that should be + * used for coloring + * @throws InvalidColorSchemaException + * thrown when more than one {@link ColorSchema} match an element + */ + public Map<Object, ColorSchema> getModifiedElements() throws InvalidColorSchemaException { + Map<Object, ColorSchema> result = new HashMap<>(); + + List<Model> models = new ArrayList<>(); + models.add(getModel()); + models.addAll(getModel().getSubmodels()); + for (Model model2 : models) { + for (ColorSchema schema : schemas) { + for (Element element : model2.getElements()) { + if (match(element, schema)) { + if (result.get(element) != null + && !colorExtractor.getNormalizedColor(result.get(element)).equals(Color.WHITE)) { + throw new InvalidColorSchemaException( + eu.getElementTag(element) + "Element is colored by more than one rule."); + } + result.put(element, schema); + } + } + for (Reaction reaction : model2.getReactions()) { + if (match(reaction, schema)) { + if (result.get(reaction) != null + && !colorExtractor.getNormalizedColor(result.get(reaction)).equals(Color.WHITE)) { + throw new InvalidColorSchemaException( + eu.getElementTag(reaction) + "Reaction is colored by more than one rule."); + } + result.put(reaction, schema); + } + } + } + } + return result; + } + + @Override + protected void undoImplementation() throws CommandExecutionException { + throw new NotImplementedException(); + + } + + @Override + protected void redoImplementation() throws CommandExecutionException { + throw new NotImplementedException(); + } + + /** + * Colors parameter model using coloring for this command. This method is used + * internally to color either top {@link Model} or one of it's sumbodels. + * + * @param result + * model to color + * @param top + * is the model a top (parent) model + * @throws InvalidColorSchemaException + * thrown when set of {@link ColorSchema} is invalid + */ + private void colorModel(Model result, boolean top) throws InvalidColorSchemaException { + + for (Element element : result.getElements()) { + element.setColor(Color.WHITE); + } + for (Reaction reaction : result.getReactions()) { + for (AbstractNode node : reaction.getNodes()) { + node.getLine().setColor(Color.BLACK); + } + } + + for (ColorSchema schema : schemas) { + for (Element element : result.getElements()) { + if (match(element, schema)) { + schema.setMatches(schema.getMatches() + 1); + applyColor(element, schema); + } + } + for (Reaction reaction : result.getReactions()) { + if (match(reaction, schema)) { + schema.setMatches(schema.getMatches() + 1); + applyColor(reaction, schema); + } + } + } + + if (top) { + for (ModelSubmodelConnection connection : result.getSubmodelConnections()) { + colorModel(connection.getSubmodel().getModel(), false); + } + } + + } + + @Override + protected void executeImplementation() throws CommandExecutionException { + try { + colorModel(getModel(), true); + } catch (InvalidColorSchemaException e) { + throw new CommandExecutionException(e); + } + } + +} diff --git a/model-command/src/test/java/lcsb/mapviewer/commands/ColorModelCommandTest.java b/model-command/src/test/java/lcsb/mapviewer/commands/ColorModelCommandTest.java index 8bc1cd05cb..6514aa1dc9 100644 --- a/model-command/src/test/java/lcsb/mapviewer/commands/ColorModelCommandTest.java +++ b/model-command/src/test/java/lcsb/mapviewer/commands/ColorModelCommandTest.java @@ -1,338 +1,365 @@ -package lcsb.mapviewer.commands; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import lcsb.mapviewer.common.Pair; -import lcsb.mapviewer.model.map.MiriamType; -import lcsb.mapviewer.model.map.layout.ColorSchema; -import lcsb.mapviewer.model.map.layout.GenericColorSchema; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.model.ModelComparator; -import lcsb.mapviewer.model.map.model.ModelFullIndexed; -import lcsb.mapviewer.model.map.model.ModelSubmodelConnection; -import lcsb.mapviewer.model.map.model.SubmodelType; -import lcsb.mapviewer.model.map.reaction.Reaction; -import lcsb.mapviewer.model.map.species.Element; -import lcsb.mapviewer.model.map.species.GenericProtein; - -public class ColorModelCommandTest extends CommandTestFunctions { - Logger logger = Logger.getLogger(ColorModelCommandTest.class); - - ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE); - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - /** - * Test if all elements were copied - if not hibernate will complain - * - * @throws Exception - * - */ - @Test - public void testColorFullModel() throws Exception { - ModelComparator comparator = new ModelComparator(); - try { - Model model = getModelForFile("testFiles/sample.xml", false); - Model model2 = getModelForFile("testFiles/sample.xml", false); - Model coloredModel = new CopyCommand(model).execute(); - - List<ColorSchema> schemas = new ArrayList<>(); - ColorSchema schema = new GenericColorSchema(); - schema.setName("CNC"); - schema.setValue(-1.0); - schemas.add(schema); - ColorModelCommand factory = new ColorModelCommand(coloredModel, schemas, colorExtractor); - - assertFalse(Color.RED.equals(coloredModel.getElementByElementId("sa14").getColor())); - - factory.execute(); - - assertEquals(0, comparator.compare(model, model2)); - - assertFalse(comparator.compare(model, coloredModel) == 0); - - assertEquals(Color.RED, coloredModel.getElementByElementId("sa14").getColor()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testColoring2() throws Exception { - try { - Model model = getModelForFile("testFiles/coloring_model.xml", true); - Collection<ColorSchema> schemas = new ArrayList<>(); - ColorSchema schema = new GenericColorSchema(); - schema.setGeneralIdentifier("HGNC:11138"); - schema.setValue(1.0); - schemas.add(schema); - schema = new GenericColorSchema(); - schema.setGeneralIdentifier("CHEBI:CHEBI:15377"); - schema.setValue(1.0); - schemas.add(schema); - schema = new GenericColorSchema(); - schema.setGeneralIdentifier("CHEBI:CHEBI:15376"); - schema.setValue(1.0); - schemas.add(schema); - - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - Collection<ColorSchema> missing = factory.getMissingSchema(); - - assertEquals(1, missing.size()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionColoring1() throws Exception { - try { - Model model = getModelForFile("testFiles/reactions_to_color.xml", false); - Reaction re4 = model.getReactionByReactionId("re4"); - Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); - - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - assertFalse(Color.BLACK.equals(re4.getNodes().get(0).getLine().getColor())); - - factory.execute(); - re4 = model.getReactionByReactionId("re4"); - - assertEquals(Color.BLACK, re4.getNodes().get(0).getLine().getColor()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionColoring2() throws Exception { - try { - Model model = getModelForFile("testFiles/reactions_to_color.xml", false); - Reaction re1 = model.getReactionByReactionId("re1"); - Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); - ColorSchema schema = new GenericColorSchema(); - schema.setReactionIdentifier("re1"); - schema.setColor(Color.RED); - schemas.add(schema); - - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - assertEquals(Color.BLACK, re1.getNodes().get(0).getLine().getColor()); - - factory.execute(); - re1 = model.getReactionByReactionId("re1"); - - assertEquals(Color.RED, re1.getNodes().get(0).getLine().getColor()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionColoring3() throws Exception { - try { - Model model = getModelForFile("testFiles/reactions_to_color.xml", false); - Reaction re2 = model.getReactionByReactionId("re2"); - Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); - ColorSchema schema = new GenericColorSchema(); - schema.setReactionIdentifier("re2"); - schema.setValue(-1.0); - schemas.add(schema); - - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - assertEquals(Color.BLACK, re2.getNodes().get(0).getLine().getColor()); - - factory.execute(); - re2 = model.getReactionByReactionId("re2"); - - assertEquals(Color.RED, re2.getNodes().get(0).getLine().getColor()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionColoring4() throws Exception { - try { - Model model = getModelForFile("testFiles/reactions_to_color.xml", false); - Reaction re3 = model.getReactionByReactionId("re3"); - Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); - ColorSchema schema = new GenericColorSchema(); - Pair<MiriamType, String> pair = new Pair<MiriamType, String>(MiriamType.PUBMED, "12345"); - List<Pair<MiriamType, String>> identifiers = new ArrayList<Pair<MiriamType, String>>(); - identifiers.add(pair); - - schema.setIdentifierColumns(identifiers); - schema.setValue(-1.0); - schemas.add(schema); - - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - - assertEquals(Color.BLACK, re3.getNodes().get(0).getLine().getColor()); - - factory.execute(); - re3 = model.getReactionByReactionId("re3"); - - assertEquals(Color.RED, re3.getNodes().get(0).getLine().getColor()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testColoringComplexModel() throws Exception { - try { - Model model = getModelForFile("testFiles/sample.xml", false); - Model model2 = getModelForFile("testFiles/sample.xml", false); - - model.addSubmodelConnection(new ModelSubmodelConnection(model2, SubmodelType.UNKNOWN, "BLA")); - - Collection<ColorSchema> schemas = new ArrayList<>(); - Model coloredModel = new CopyCommand(model).execute(); - new ColorModelCommand(coloredModel, schemas, colorExtractor).execute(); - - Model coloredModel2 = coloredModel.getSubmodelConnections().iterator().next().getSubmodel().getModel(); - Model coloredModel3 = coloredModel.getSubmodelByConnectionName("BLA"); - - assertFalse(coloredModel2.getElementByElementId("sa2").getColor().equals(model2.getElementByElementId("sa2").getColor())); - assertFalse(coloredModel3.getElementByElementId("sa2").getColor().equals(model2.getElementByElementId("sa2").getColor())); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testColoredAliases() throws Exception { - try { - Model model = getModelForFile("testFiles/sample.xml", false); - Collection<ColorSchema> schemas = new ArrayList<>(); - ColorSchema schema = new GenericColorSchema(); - schema.setName("CNC"); - schema.setColor(Color.BLUE); - schemas.add(schema); - schema = new GenericColorSchema(); - schema.setName("BDH1"); - schema.setColor(Color.BLUE); - schemas.add(schema); - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - - Map<Object, ColorSchema> map = factory.getModifiedElements(); - assertEquals(2, map.size()); - for (Map.Entry<Object, ColorSchema> entry : map.entrySet()) { - assertTrue(entry.getKey() instanceof Element); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testColoredReactions() throws Exception { - try { - Model model = getModelForFile("testFiles/sample.xml", false); - Collection<ColorSchema> schemas = new ArrayList<>(); - ColorSchema schema = new GenericColorSchema(); - schema.setReactionIdentifier("re1"); - schema.setLineWidth(3.0); - schema.setColor(Color.BLUE); - schema.setName(""); - schema.setReverseReaction(true); - schemas.add(schema); - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - - Map<Object, ColorSchema> map = factory.getModifiedElements(); - assertEquals(0, map.size()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testColoredReactions2() throws Exception { - try { - Model model = getModelForFile("testFiles/sample.xml", false); - Collection<ColorSchema> schemas = new ArrayList<>(); - ColorSchema schema = new GenericColorSchema(); - schema.setReactionIdentifier("re1"); - schema.setLineWidth(3.0); - schema.setColor(Color.BLUE); - schema.setName(null); - schema.setReverseReaction(true); - schemas.add(schema); - ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); - - Map<Object, ColorSchema> map = factory.getModifiedElements(); - assertEquals(1, map.size()); - for (Map.Entry<Object, ColorSchema> entry : map.entrySet()) { - assertTrue(entry.getKey() instanceof Reaction); - assertEquals("re1", ((Reaction) entry.getKey()).getIdReaction()); - assertEquals(entry.getValue(), schema); - } - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAliasMatchWithReactionId() throws Exception { - try { - GenericColorSchema colorSchema = new GenericColorSchema(); - colorSchema.setName(null); - colorSchema.setReactionIdentifier("1"); - - GenericProtein alias = new GenericProtein("id"); - alias.setName("test"); - - List<ColorSchema> schemas = new ArrayList<>(); - schemas.add(colorSchema); - - ColorModelCommand factory = new ColorModelCommand(new ModelFullIndexed(null), schemas, colorExtractor); - - assertFalse(factory.match(alias, colorSchema)); - - colorSchema.setReactionIdentifier(null); - assertTrue(factory.match(alias, colorSchema)); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - - } - -} +package lcsb.mapviewer.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import lcsb.mapviewer.common.Pair; +import lcsb.mapviewer.model.map.MiriamType; +import lcsb.mapviewer.model.map.layout.ColorSchema; +import lcsb.mapviewer.model.map.layout.GenericColorSchema; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.model.ModelComparator; +import lcsb.mapviewer.model.map.model.ModelFullIndexed; +import lcsb.mapviewer.model.map.model.ModelSubmodelConnection; +import lcsb.mapviewer.model.map.model.SubmodelType; +import lcsb.mapviewer.model.map.reaction.Reaction; +import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.GenericProtein; +import lcsb.mapviewer.model.map.species.Protein; + +public class ColorModelCommandTest extends CommandTestFunctions { + Logger logger = Logger.getLogger(ColorModelCommandTest.class); + + ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + /** + * Test if all elements were copied - if not hibernate will complain + * + * @throws Exception + * + */ + @Test + public void testColorFullModel() throws Exception { + ModelComparator comparator = new ModelComparator(); + try { + Model model = getModelForFile("testFiles/sample.xml", false); + Model model2 = getModelForFile("testFiles/sample.xml", false); + Model coloredModel = new CopyCommand(model).execute(); + + List<ColorSchema> schemas = new ArrayList<>(); + ColorSchema schema = new GenericColorSchema(); + schema.setName("CNC"); + schema.setValue(-1.0); + schemas.add(schema); + ColorModelCommand factory = new ColorModelCommand(coloredModel, schemas, colorExtractor); + + assertFalse(Color.RED.equals(coloredModel.getElementByElementId("sa14").getColor())); + + factory.execute(); + + assertEquals(0, comparator.compare(model, model2)); + + assertFalse(comparator.compare(model, coloredModel) == 0); + + assertEquals(Color.RED, coloredModel.getElementByElementId("sa14").getColor()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testColoring2() throws Exception { + try { + Model model = getModelForFile("testFiles/coloring_model.xml", true); + Collection<ColorSchema> schemas = new ArrayList<>(); + ColorSchema schema = new GenericColorSchema(); + schema.setGeneralIdentifier("HGNC:11138"); + schema.setValue(1.0); + schemas.add(schema); + schema = new GenericColorSchema(); + schema.setGeneralIdentifier("CHEBI:CHEBI:15377"); + schema.setValue(1.0); + schemas.add(schema); + schema = new GenericColorSchema(); + schema.setGeneralIdentifier("CHEBI:CHEBI:15376"); + schema.setValue(1.0); + schemas.add(schema); + + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + Collection<ColorSchema> missing = factory.getMissingSchema(); + + assertEquals(1, missing.size()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionColoring1() throws Exception { + try { + Model model = getModelForFile("testFiles/reactions_to_color.xml", false); + Reaction re4 = model.getReactionByReactionId("re4"); + Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); + + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + assertFalse(Color.BLACK.equals(re4.getNodes().get(0).getLine().getColor())); + + factory.execute(); + re4 = model.getReactionByReactionId("re4"); + + assertEquals(Color.BLACK, re4.getNodes().get(0).getLine().getColor()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionColoring2() throws Exception { + try { + Model model = getModelForFile("testFiles/reactions_to_color.xml", false); + Reaction re1 = model.getReactionByReactionId("re1"); + Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); + ColorSchema schema = new GenericColorSchema(); + schema.setReactionIdentifier("re1"); + schema.setColor(Color.RED); + schemas.add(schema); + + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + assertEquals(Color.BLACK, re1.getNodes().get(0).getLine().getColor()); + + factory.execute(); + re1 = model.getReactionByReactionId("re1"); + + assertEquals(Color.RED, re1.getNodes().get(0).getLine().getColor()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionColoring3() throws Exception { + try { + Model model = getModelForFile("testFiles/reactions_to_color.xml", false); + Reaction re2 = model.getReactionByReactionId("re2"); + Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); + ColorSchema schema = new GenericColorSchema(); + schema.setReactionIdentifier("re2"); + schema.setValue(-1.0); + schemas.add(schema); + + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + assertEquals(Color.BLACK, re2.getNodes().get(0).getLine().getColor()); + + factory.execute(); + re2 = model.getReactionByReactionId("re2"); + + assertEquals(Color.RED, re2.getNodes().get(0).getLine().getColor()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionColoring4() throws Exception { + try { + Model model = getModelForFile("testFiles/reactions_to_color.xml", false); + Reaction re3 = model.getReactionByReactionId("re3"); + Collection<ColorSchema> schemas = new ArrayList<ColorSchema>(); + ColorSchema schema = new GenericColorSchema(); + Pair<MiriamType, String> pair = new Pair<MiriamType, String>(MiriamType.PUBMED, "12345"); + List<Pair<MiriamType, String>> identifiers = new ArrayList<Pair<MiriamType, String>>(); + identifiers.add(pair); + + schema.setIdentifierColumns(identifiers); + schema.setValue(-1.0); + schemas.add(schema); + + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + + assertEquals(Color.BLACK, re3.getNodes().get(0).getLine().getColor()); + + factory.execute(); + re3 = model.getReactionByReactionId("re3"); + + assertEquals(Color.RED, re3.getNodes().get(0).getLine().getColor()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testColoringComplexModel() throws Exception { + try { + Model model = getModelForFile("testFiles/sample.xml", false); + Model model2 = getModelForFile("testFiles/sample.xml", false); + + model.addSubmodelConnection(new ModelSubmodelConnection(model2, SubmodelType.UNKNOWN, "BLA")); + + Collection<ColorSchema> schemas = new ArrayList<>(); + Model coloredModel = new CopyCommand(model).execute(); + new ColorModelCommand(coloredModel, schemas, colorExtractor).execute(); + + Model coloredModel2 = coloredModel.getSubmodelConnections().iterator().next().getSubmodel().getModel(); + Model coloredModel3 = coloredModel.getSubmodelByConnectionName("BLA"); + + assertFalse( + coloredModel2.getElementByElementId("sa2").getColor().equals(model2.getElementByElementId("sa2").getColor())); + assertFalse( + coloredModel3.getElementByElementId("sa2").getColor().equals(model2.getElementByElementId("sa2").getColor())); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testColoredAliases() throws Exception { + try { + Model model = getModelForFile("testFiles/sample.xml", false); + Collection<ColorSchema> schemas = new ArrayList<>(); + ColorSchema schema = new GenericColorSchema(); + schema.setName("CNC"); + schema.setColor(Color.BLUE); + schemas.add(schema); + schema = new GenericColorSchema(); + schema.setName("BDH1"); + schema.setColor(Color.BLUE); + schemas.add(schema); + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + + Map<Object, ColorSchema> map = factory.getModifiedElements(); + assertEquals(2, map.size()); + for (Map.Entry<Object, ColorSchema> entry : map.entrySet()) { + assertTrue(entry.getKey() instanceof Element); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testColoredReactions() throws Exception { + try { + Model model = getModelForFile("testFiles/sample.xml", false); + Collection<ColorSchema> schemas = new ArrayList<>(); + ColorSchema schema = new GenericColorSchema(); + schema.setReactionIdentifier("re1"); + schema.setLineWidth(3.0); + schema.setColor(Color.BLUE); + schema.setName(""); + schema.setReverseReaction(true); + schemas.add(schema); + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + + Map<Object, ColorSchema> map = factory.getModifiedElements(); + assertEquals(0, map.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testColoredReactions2() throws Exception { + try { + Model model = getModelForFile("testFiles/sample.xml", false); + Collection<ColorSchema> schemas = new ArrayList<>(); + ColorSchema schema = new GenericColorSchema(); + schema.setReactionIdentifier("re1"); + schema.setLineWidth(3.0); + schema.setColor(Color.BLUE); + schema.setName(null); + schema.setReverseReaction(true); + schemas.add(schema); + ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor); + + Map<Object, ColorSchema> map = factory.getModifiedElements(); + assertEquals(1, map.size()); + for (Map.Entry<Object, ColorSchema> entry : map.entrySet()) { + assertTrue(entry.getKey() instanceof Reaction); + assertEquals("re1", ((Reaction) entry.getKey()).getIdReaction()); + assertEquals(entry.getValue(), schema); + } + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAliasMatchWithReactionId() throws Exception { + try { + GenericColorSchema colorSchema = new GenericColorSchema(); + colorSchema.setName(null); + colorSchema.setReactionIdentifier("1"); + + GenericProtein protein = new GenericProtein("id"); + protein.setName("test"); + + List<ColorSchema> schemas = new ArrayList<>(); + schemas.add(colorSchema); + + ColorModelCommand factory = new ColorModelCommand(new ModelFullIndexed(null), schemas, colorExtractor); + + assertFalse(factory.match(protein, colorSchema)); + + colorSchema.setReactionIdentifier(null); + assertTrue(factory.match(protein, colorSchema)); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + + } + + @Test + public void testSpeciesMatchWithProteinType() throws Exception { + try { + GenericColorSchema colorSchema = new GenericColorSchema(); + colorSchema.setName("s1"); + colorSchema.addType(Protein.class); + + GenericProtein species = new GenericProtein("id"); + species.setName("s1"); + + List<ColorSchema> schemas = new ArrayList<>(); + schemas.add(colorSchema); + + ColorModelCommand factory = new ColorModelCommand(new ModelFullIndexed(null), schemas, colorExtractor); + + assertTrue(factory.match(species, colorSchema)); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + + } + +} -- GitLab