diff --git a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java index 451deb265f68c530d378655394d234e67e1cec2a..d7ac52a2907e84754893e53ebc8f4148760bbd47 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java +++ b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java @@ -41,325 +41,338 @@ import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; * */ public class XmlParser { - /** - * Base of the hex representation. - */ - private static final int HEX_BASE = 16; + /** + * Base of the hex representation. + */ + private static final int HEX_BASE = 16; - /** - * {@link DocumentBuilderFactory} used to create {@link DocumentBuilder} - * objects that will manipulate xml nodes. - */ - private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + /** + * {@link DocumentBuilderFactory} used to create {@link DocumentBuilder} objects + * that will manipulate xml nodes. + */ + private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - /** - * Default class logger. - */ - private Logger logger = Logger.getLogger(XmlParser.class.getName()); + /** + * Default class logger. + */ + private Logger logger = Logger.getLogger(XmlParser.class.getName()); - /** - * DOM document builder used for xml transformations. - */ - private DocumentBuilder db; + /** + * DOM document builder used for xml transformations. + */ + private DocumentBuilder db; - /** - * Default constructor that prevents from instatiation of the class and - * initializes fields. - */ - protected XmlParser() { - try { - db = documentBuilderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new InvalidStateException("Problem with xml parser"); - } + /** + * Default constructor that prevents from instatiation of the class and + * initializes fields. + */ + protected XmlParser() { + try { + db = documentBuilderFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new InvalidStateException("Problem with xml parser"); + } - } + } - /** - * Method returns the node of xml nodelist 'nodes' with 'tagName' name. If - * node could not be found then null is returned. - * - * @param tagName - * name of node to look for - * @param nodes - * list of nodes - * @return node from nodes list with the tagName name, <b>null</b> if such - * node doesn't exist - */ - protected Node getNode(final String tagName, final NodeList nodes) { - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase(tagName)) { - return node; - } - } - } - return null; - } + /** + * Method returns the node of xml nodelist 'nodes' with 'tagName' name. If node + * could not be found then null is returned. + * + * @param tagName + * name of node to look for + * @param nodes + * list of nodes + * @return node from nodes list with the tagName name, <b>null</b> if such node + * doesn't exist + */ + protected Node getNode(final String tagName, final NodeList nodes) { + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase(tagName)) { + return node; + } + } + } + return null; + } - /** - * Method returns the child node of xml 'parentNode' with 'tagName' name. If - * node could not be found then null is returned. - * - * @param tagName - * name of node to look for - * @param parentNode - * parent node - * @return node from nodes list with the tagName name, <b>null</b> if such - * node doesn't exist - */ - protected Node getNode(final String tagName, final Node parentNode) { - return getNode(tagName, parentNode.getChildNodes()); - } + /** + * Method returns the child node of xml 'parentNode' with 'tagName' name. If + * node could not be found then null is returned. + * + * @param tagName + * name of node to look for + * @param parentNode + * parent node + * @return node from nodes list with the tagName name, <b>null</b> if such node + * doesn't exist + */ + protected Node getNode(final String tagName, final Node parentNode) { + return getNode(tagName, parentNode.getChildNodes()); + } - /** - * Method returns list of nodes with 'tagName' name. If node could not be - * found then empty list is returned. - * - * @param tagName - * name of node to look for - * @param nodes - * list of input nodes - * @return list of nodes with 'tagName' name - */ - protected List<Node> getNodes(final String tagName, final NodeList nodes) { - List<Node> result = new ArrayList<Node>(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeName().equalsIgnoreCase(tagName)) { - result.add(node); - } - } - return result; - } + /** + * Method returns list of nodes with 'tagName' name. If node could not be found + * then empty list is returned. + * + * @param tagName + * name of node to look for + * @param nodes + * list of input nodes + * @return list of nodes with 'tagName' name + */ + protected List<Node> getNodes(final String tagName, final NodeList nodes) { + List<Node> result = new ArrayList<Node>(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeName().equalsIgnoreCase(tagName)) { + result.add(node); + } + } + return result; + } - /** - * Method returns the value of node attribute. If attribute could not be found - * then "" is returned. - * - * @param attrName - * name of the attribute to look for - * @param node - * a node - * @return the value of node attribute, empty string("") if attribute doesn't - * exist - */ - protected String getNodeAttr(final String attrName, final Node node) { - NamedNodeMap attrs = node.getAttributes(); - for (int y = 0; y < attrs.getLength(); y++) { - Node attr = attrs.item(y); - if (attr.getNodeName().equalsIgnoreCase(attrName)) { - return attr.getNodeValue(); - } - } - return ""; - } + /** + * Method returns the value of node attribute. If attribute could not be found + * then "" is returned. + * + * @param attrName + * name of the attribute to look for + * @param node + * a node + * @return the value of node attribute, empty string("") if attribute doesn't + * exist + */ + protected String getNodeAttr(final String attrName, final Node node) { + NamedNodeMap attrs = node.getAttributes(); + for (int y = 0; y < attrs.getLength(); y++) { + Node attr = attrs.item(y); + if (attr.getNodeName().equalsIgnoreCase(attrName)) { + return attr.getNodeValue(); + } + } + return ""; + } - /** - * Method returns the text value of node. If text could not be found then "" - * is returned. - * - * @param node - * a node - * @return the text value of node or empty string ("") if the text could be - * found. - */ - protected String getNodeValue(final Node node) { - if (node == null) { - return ""; - } - NodeList childNodes = node.getChildNodes(); - for (int x = 0; x < childNodes.getLength(); x++) { - Node data = childNodes.item(x); - if (data.getNodeType() == Node.TEXT_NODE) { - return data.getNodeValue(); - } - } - return ""; - } + /** + * Method returns the text value of node. If text could not be found then "" is + * returned. + * + * @param node + * a node + * @return the text value of node or empty string ("") if the text could be + * found. + */ + protected String getNodeValue(final Node node) { + if (node == null) { + return ""; + } + NodeList childNodes = node.getChildNodes(); + for (int x = 0; x < childNodes.getLength(); x++) { + Node data = childNodes.item(x); + if (data.getNodeType() == Node.TEXT_NODE) { + return data.getNodeValue(); + } + } + return ""; + } - /** - * Method returns the xml Document from input source given as a parameter. - * - * @param stream - * input stream with xml document - * @return Document node for the input stream - * @throws InvalidXmlSchemaException - * thrown when there is a problem with xml - */ - protected Document getXmlDocumentFromInputSource(final InputSource stream) throws InvalidXmlSchemaException { - try { - synchronized (db) { // DocumentBuilder cannot parse few objects at the - // same time - return db.parse(stream); - } - } catch (SAXException e) { - throw new InvalidXmlSchemaException("Problem with xml parser", e); - } catch (IOException e) { - throw new InvalidXmlSchemaException("Problem with xml parser", e); - } - } + /** + * Method returns the xml Document from input source given as a parameter. + * + * @param stream + * input stream with xml document + * @return Document node for the input stream + * @throws InvalidXmlSchemaException + * thrown when there is a problem with xml + */ + protected Document getXmlDocumentFromInputSource(final InputSource stream) throws InvalidXmlSchemaException { + try { + synchronized (db) { // DocumentBuilder cannot parse few objects at the + // same time + return db.parse(stream); + } + } catch (SAXException e) { + throw new InvalidXmlSchemaException("Problem with xml parser", e); + } catch (IOException e) { + throw new InvalidXmlSchemaException("Problem with xml parser", e); + } + } - /** - * Method returns the xml Document from text given as a source. - * - * @param text - * string representing xml document - * @return Document for the xml document given in the input - * @throws InvalidXmlSchemaException - * thrown when there is a problem with xml - */ - protected Document getXmlDocumentFromString(final String text) throws InvalidXmlSchemaException { - InputSource is = new InputSource(); - is.setCharacterStream(new StringReader(text)); - try { - return getXmlDocumentFromInputSource(is); - } catch (NullPointerException e) { - logger.error("Problem with input xml: " + text); - throw new InvalidXmlSchemaException(e); - } - } + /** + * Method returns the xml Document from text given as a source. + * + * @param text + * string representing xml document + * @return Document for the xml document given in the input + * @throws InvalidXmlSchemaException + * thrown when there is a problem with xml + */ + protected Document getXmlDocumentFromString(final String text) throws InvalidXmlSchemaException { + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader(text)); + try { + return getXmlDocumentFromInputSource(is); + } catch (NullPointerException e) { + logger.error("Problem with input xml: " + text); + throw new InvalidXmlSchemaException(e); + } + } - /** - * Transforms node into string xml format. - * - * @param node - * node that should be transformed into xml string - * @return string representation of the xml node - */ - protected String nodeToString(final Node node) { - return nodeToString(node, false); - } + /** + * Transforms node into string xml format. + * + * @param node + * node that should be transformed into xml string + * @return string representation of the xml node + */ + protected String nodeToString(final Node node) { + return nodeToString(node, false); + } - /** - * Transforms node into string xml format. - * - * @param node - * node that should be transformed into xml string - * @param includeHeadNode - * should the top level node exist in the output - * @return string representation of the xml node - */ - protected String nodeToString(final Node node, final boolean includeHeadNode) { - if (node == null) { - return null; - } - StringWriter sw = new StringWriter(); - try { - Transformer t = TransformerFactory.newInstance().newTransformer(); - t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - t.setOutputProperty(OutputKeys.INDENT, "yes"); - t.setOutputProperty(OutputKeys.METHOD, "xml"); + /** + * Transforms node into string xml format. + * + * @param node + * node that should be transformed into xml string + * @param includeHeadNode + * should the top level node exist in the output + * @return string representation of the xml node + */ + protected String nodeToString(final Node node, final boolean includeHeadNode) { + if (node == null) { + return null; + } + StringWriter sw = new StringWriter(); + try { + Transformer t = TransformerFactory.newInstance().newTransformer(); + t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + t.setOutputProperty(OutputKeys.INDENT, "yes"); + t.setOutputProperty(OutputKeys.METHOD, "xml"); - NodeList list = node.getChildNodes(); - for (int i = 0; i < list.getLength(); i++) { - Node element = list.item(i); - t.transform(new DOMSource(element), new StreamResult(sw)); - } - } catch (TransformerException te) { - logger.debug("nodeToString Transformer Exception"); - } - if (includeHeadNode) { - return "<" + node.getNodeName() + ">" + sw.toString() + "</" + node.getNodeName() + ">"; - } - return sw.toString(); - } + NodeList list = node.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { + Node element = list.item(i); + t.transform(new DOMSource(element), new StreamResult(sw)); + } + } catch (TransformerException te) { + logger.debug("nodeToString Transformer Exception"); + } + if (includeHeadNode) { + return "<" + node.getNodeName() + ">" + sw.toString() + "</" + node.getNodeName() + ">"; + } + return sw.toString(); + } - /** - * This method transform color encoded in string (CellDesigner format) into - * Color. - * - * @param color - * string representing color - * @return Color object for the fiven string - */ - protected Color stringToColor(final String color) { - try { - String alpha = color.substring(0, 2); - Color tmp = new Color(hexToInteger(color.substring(2))); - return new Color(tmp.getRed(), tmp.getGreen(), tmp.getBlue(), hexToInteger(alpha)); - } catch (Exception e) { - throw new InvalidArgumentException("Invalid color string: " + color); - } - } + /** + * This method transform color encoded in string (CellDesigner format) into + * Color. + * + * @param color + * string representing color + * @return Color object for the fiven string + */ + protected Color stringToColor(final String color) { + try { + String alpha = color.substring(0, 2); + Color tmp = new Color(hexToInteger(color.substring(2))); + return new Color(tmp.getRed(), tmp.getGreen(), tmp.getBlue(), hexToInteger(alpha)); + } catch (Exception e) { + throw new InvalidArgumentException("Invalid color string: " + color); + } + } - /** - * Transforms hex string into Integer. - * - * @param hexString - * string representation in hex base - * @return Integer value of the hex string - */ - private Integer hexToInteger(String hexString) { - return Integer.valueOf(hexString, HEX_BASE); - } + /** + * Transforms hex string into Integer. + * + * @param hexString + * string representation in hex base + * @return Integer value of the hex string + */ + private Integer hexToInteger(String hexString) { + return Integer.valueOf(hexString, HEX_BASE); + } - /** - * Transforms Color object into string representing this color in RGB. - * - * @param color - * color that should be converted into string - * @return hex string representation of the color - */ - protected String colorToString(final Color color) { - return String.format("%08X", color.getRGB()); - } + /** + * Transforms Color object into string representing this color in RGB. + * + * @param color + * color that should be converted into string + * @return hex string representation of the color + */ + protected String colorToString(final Color color) { + return String.format("%08X", color.getRGB()); + } - /** - * Method that reads file and transforms it into a string. - * - * @param fileName - * path to a file - * @return string containing data from the file (default coding is used) - * @throws IOException - * thrown when there are some problems with a file - */ - protected String fileToString(final String fileName) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(fileName)); - String line = null; - StringBuilder stringBuilder = new StringBuilder(); - String ls = System.getProperty("line.separator"); + /** + * Method that reads file and transforms it into a string. + * + * @param fileName + * path to a file + * @return string containing data from the file (default coding is used) + * @throws IOException + * thrown when there are some problems with a file + */ + protected String fileToString(final String fileName) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(fileName)); + String line = null; + StringBuilder stringBuilder = new StringBuilder(); + String ls = System.getProperty("line.separator"); - while ((line = reader.readLine()) != null) { - stringBuilder.append(line); - stringBuilder.append(ls); - } - reader.close(); + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + stringBuilder.append(ls); + } + reader.close(); - return stringBuilder.toString(); - } + return stringBuilder.toString(); + } - /** - * Method that reads all data from inputstream and transform it into a string. - * UTF-8 coding is used. - * - * @param inputStream - * stream from which we read data - * @return string representing all data from input stream - * @throws IOException - * thrown if there are some problems with input stream - */ - protected String inputStreamToString(final InputStream inputStream) throws IOException { - StringWriter writer = new StringWriter(); - IOUtils.copy(inputStream, writer, "UTF-8"); - String result = writer.toString(); - return result; - } + /** + * Method that reads all data from inputstream and transform it into a string. + * UTF-8 coding is used. + * + * @param inputStream + * stream from which we read data + * @return string representing all data from input stream + * @throws IOException + * thrown if there are some problems with input stream + */ + protected String inputStreamToString(final InputStream inputStream) throws IOException { + StringWriter writer = new StringWriter(); + IOUtils.copy(inputStream, writer, "UTF-8"); + String result = writer.toString(); + return result; + } - /** - * Method that encode string into a string that can be used in xml file. - * - * @param string - * string to be escaped - * @return escaped string, ready to be used in xml - */ - protected String escapeXml(final String string) { - if (string == null) { - return null; - } - // quite expensive - return StringEscapeUtils.escapeXml(string).replaceAll("\n", " ").replace("\r", " "); - } + /** + * Method that encode string into a string that can be used in xml file. + * + * @param string + * string to be escaped + * @return escaped string, ready to be used in xml + */ + protected String escapeXml(final String string) { + if (string == null) { + return null; + } + // quite expensive + return StringEscapeUtils.escapeXml(string).replaceAll("\n", " ").replace("\r", " "); + } + + public List<Node> getAllNotNecessirellyDirectChild(String tagName, Node root) { + List<Node> result = new ArrayList<>(); + for (int x = 0; x < root.getChildNodes().getLength(); x++) { + Node node = root.getChildNodes().item(x); + if (node.getNodeName().equalsIgnoreCase(tagName)) { + result.add(node); + } else { + result.addAll(getAllNotNecessirellyDirectChild(tagName, node)); + } + } + return result; + } } diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParser.java new file mode 100644 index 0000000000000000000000000000000000000000..ef91803efe11a4e8d5195a23f5cbc7c61ea6cca6 --- /dev/null +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParser.java @@ -0,0 +1,64 @@ +package lcsb.mapviewer.converter.model.celldesigner.reaction; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.log4j.Logger; +import org.w3c.dom.Node; + +import lcsb.mapviewer.common.XmlParser; +import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; +import lcsb.mapviewer.converter.model.celldesigner.parameter.ParameterCollectionXmlParser; +import lcsb.mapviewer.model.map.kinetics.SbmlArgument; +import lcsb.mapviewer.model.map.kinetics.SbmlKinetics; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.species.Element; + +public class KineticsXmlParser extends XmlParser { + Logger logger = Logger.getLogger(KineticsXmlParser.class); + + ParameterCollectionXmlParser parameterParser; + + Model model; + + public KineticsXmlParser(Model model) { + this.model = model; + parameterParser = new ParameterCollectionXmlParser(model); + } + + public SbmlKinetics parseKinetics(Node kineticsNode, Map<String, Element> elements) throws InvalidXmlSchemaException { + SbmlKinetics result = new SbmlKinetics(); + Node mathNode = super.getNode("math", kineticsNode); + if (mathNode == null) { + throw new InvalidXmlSchemaException("kineticLaw node doesn't have math subnode"); + } + + Node parametersNode = super.getNode("listOfParameters", kineticsNode); + if (parametersNode != null) { + result.addParameters(parameterParser.parseXmlParameterCollection(parametersNode)); + } + + Set<SbmlArgument> elementsUsedInKinetics = new HashSet<>(); + for (Node ciNode : super.getAllNotNecessirellyDirectChild("ci", mathNode)) { + String id = super.getNodeValue(ciNode).trim(); + SbmlArgument element = elements.get(id); + if (element == null) { + element = result.getParameterById(id); + } + if (element == null) { + element = model.getParameterById(id); + } + if (element == null) { + element = model.getFunctionById(id); + } + if (element == null) { + throw new InvalidXmlSchemaException("Unknown symbol in kinetics: " + id); + } + elementsUsedInKinetics.add(element); + } + result.addArguments(elementsUsedInKinetics); + + return result; + } +} diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java index 2b428bfdb9d9b35db0e3be6aa438bec9f5dae9f6..735682c06731c429303b449c70524311958d42c3 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java @@ -70,1568 +70,1617 @@ import lcsb.mapviewer.model.map.species.Species; */ public class ReactionFromXml extends XmlParser { - /** - * Reactant lines in cell designer ends in the 2/5 of the center line. - */ - private static final double REACTANT_END_RATIO = 0.4; - - /** - * Product lines in cell designer starts in the 3/5 of the center line. - */ - private static final double PRODUCT_START_RATIO = 0.6; - - /** - * Stores information about {@link CellDesignerAnchor} for a node. - */ - private Map<ReactionNode, CellDesignerAnchor> anchorsByNodes = new HashMap<ReactionNode, CellDesignerAnchor>(); - - /** - * Stores information about operator type that should be used for a modifier - * node. - */ - private Map<ReactionNode, String> typeByModifier = new HashMap<ReactionNode, String>(); - - /** - * Stores information to which point on the central rectangle modifier should - * be connected. - */ - private Map<ReactionNode, String> lineTypeByModifier = new HashMap<ReactionNode, String>(); - - /** - * Stores information about list of points that create line describing - * modifier. - */ - private Map<ReactionNode, List<Point2D>> pointsByModifier = new HashMap<ReactionNode, List<Point2D>>(); - - /** - * Helps to determine if the key Modifier should be treates as part of - * NodeOperator (value in the map). - */ - private Map<Modifier, Modifier> modifierParentOperator = new HashMap<Modifier, Modifier>(); - - /** - * Identifies central line segment in {@link TwoProductReactionInterface} and - * {@link TwoReactantReactionInterface} reactions. - */ - private Map<ReactionNode, Integer> indexByComplexReaction = new HashMap<ReactionNode, Integer>(); - - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private Logger logger = Logger.getLogger(ReactionXmlParser.class.getName()); - - /** - * Xml parser used for processing notes into structurized data. - */ - private RestAnnotationParser rap = new RestAnnotationParser(); - - /** - * Helper object used for manipulation on the point coorinates in CellDesigner - * format. - */ - private CellDesignerPointTransformation pointTransformation = new CellDesignerPointTransformation(); - - /** - * Should sbgn standard be used. - */ - private boolean sbgn; - - /** - * Default constructor. - * - * @param sbgn - * Should the converter use sbgn standard - */ - public ReactionFromXml(boolean sbgn) { - this.sbgn = sbgn; - } - - /** - * Returns {@link Reaction} object from CellDesigner xml node. - * - * @param reactionNode - * xml node - * @param model - * model where the reaction is placed - * @return reactin from xml node - * @throws ReactionParserException - * thrown when the xml is invalid - * @throws InvalidXmlSchemaException - * thrown when reactionNode is invalid xml - */ - public Reaction getReaction(Node reactionNode, Model model) throws ReactionParserException { - Reaction result = new Reaction(); - // we ignore metaid - it's useless and obstruct data model - // result.setMetaId(getNodeAttr("metaid", reactionNode)); - result.setIdReaction(getNodeAttr("id", reactionNode)); - result.setName(getNodeAttr("name", reactionNode)); - // by default this value is true... - result.setReversible(!(getNodeAttr("reversible", reactionNode).equalsIgnoreCase("false"))); - - NodeList nodes = reactionNode.getChildNodes(); - Node annotationNode = null; - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("annotation")) { - annotationNode = node; - } else if (node.getNodeName().equalsIgnoreCase("listOfReactants")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("listOfProducts")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("listOfModifiers")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("notes")) { - rap.processNotes(rap.getNotes(node), result); - } else if (node.getNodeName().equalsIgnoreCase("kineticLaw")) { - // this definitelly needs to be improved!!! - // up to now we didn't do anything about kinetics - - Node kineticLaw = node; - Node mathNode = getNode("math", kineticLaw.getChildNodes()); - if (mathNode == null) { - throw new ReactionParserException("kineticLaw node doesn't have math subnode", result); - } - String xmlns = getNodeAttr("xmlns", mathNode); - if (!xmlns.equals("http://www.w3.org/1998/Math/MathML")) { - throw new ReactionParserException("invalid value in math xmlns attrib: " + xmlns, result); - } - result.setKineticLaw(true); - } else { - throw new ReactionParserException("Unknown element of reaction: " + node.getNodeName(), result); - } - } - } - if (annotationNode != null) { - result = parseReactionAnnotation(annotationNode, result, model); - } else { - throw new ReactionParserException("No annotation node in reaction", result); - } - - return result; - } - - /** - * Parses reaction annotation node and update reaction. - * - * @param annotationNode - * xml node - * @param result - * reaction to update - * @param model - * model where reaction is placed - * @return updated reaction - * @throws ReactionParserException - * thrown when the xml is invalid - * @throws InvalidXmlSchemaException - * thrown when annotationNode is invalid xml - */ - private Reaction parseReactionAnnotation(Node annotationNode, Reaction result, Model model) throws ReactionParserException { - NodeList nodes = annotationNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:extension")) { - result = parseReactionExtension(result, node, model); - } else if (node.getNodeName().equalsIgnoreCase("rdf:RDF")) { - try { - XmlAnnotationParser xmlParser = new XmlAnnotationParser(); - result.addMiriamData(xmlParser.parseRdfNode(nodes)); - } catch (InvalidXmlSchemaException e) { - throw new ReactionParserException("Problem with parsing RDF", result, e); - } - } else { - throw new ReactionParserException("Unknown element of reaction/annotation: " + node.getNodeName(), result); - } - } - } - return result; - } - - /** - * Parses CellDesigner extension to sbml reaction node and updates reaction. - * - * @param result - * reaction to update - * @param node - * xml node - * @param model - * model where reaction is placed - * @return updated reaction - * @throws ReactionParserException - * thrown when the xml is invalid and reason is more specific - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - * - */ - private Reaction parseReactionExtension(Reaction result, Node node, Model model) throws ReactionParserException { - NodeList extensionNodes = node.getChildNodes(); - LineProperties line = null; - ConnectScheme connectScheme = null; - EditPoints points = null; - Node reactantsLinkNode = null; - Node productsLinkNode = null; - Node modifiersLinkNode = null; - Node gateMembers = null; - String type = null; - for (int y = 0; y < extensionNodes.getLength(); y++) { - Node nodeReaction = extensionNodes.item(y); - if (nodeReaction.getNodeType() == Node.ELEMENT_NODE) { - if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:reactionType")) { - type = getNodeValue(nodeReaction); - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseReactants")) { - NodeList reactantsNodes = nodeReaction.getChildNodes(); - for (int z = 0; z < reactantsNodes.getLength(); z++) { - Node reactantNode = reactantsNodes.item(z); - if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { - if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseReactant")) { - parseBaseReactant(result, reactantNode, model); - } else { - throw new ReactionParserException("Unknown element of celldesigner:baseReactants: " + node.getNodeName(), result); - } - } - } - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseProducts")) { - NodeList reactantsNodes = nodeReaction.getChildNodes(); - for (int z = 0; z < reactantsNodes.getLength(); z++) { - Node reactantNode = reactantsNodes.item(z); - if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { - if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseProduct")) { - parseBaseProduct(model, result, reactantNode); - } else { - throw new ReactionParserException("Unknown element of celldesigner:baseProducts: " + node.getNodeName(), result); - } - } - } - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:line")) { - line = getLineProperties(nodeReaction); - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { - try { - connectScheme = parseConnectScheme(nodeReaction); - } catch (InvalidXmlSchemaException e) { - throw new ReactionParserException(result, e); - } - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfReactantLinks")) { - reactantsLinkNode = nodeReaction; - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfProductLinks")) { - productsLinkNode = nodeReaction; - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfModification")) { - modifiersLinkNode = nodeReaction; - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { - points = parseEditPoints(nodeReaction); - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:name")) { - result.setName(getNodeValue(nodeReaction)); - } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfGateMember")) { - gateMembers = nodeReaction; - } else { - throw new ReactionParserException("Unknown element of reaction/celldesigner:extension: " + nodeReaction.getNodeName(), result); - } - } - } - if (gateMembers != null) { - try { - points = gateMembersToPoints(gateMembers); - } catch (InvalidXmlSchemaException e) { - throw new ReactionParserException(result, e); - } - } - result = createProperTypeReaction(type, result); - if (connectScheme == null) { - throw new ReactionParserException("No connectScheme node", result); - } - if (points == null) { - points = new EditPoints(); - } - if (result instanceof SimpleReactionInterface) { - createLinesForSimpleReaction(result, points, connectScheme); - } else if (result instanceof TwoReactantReactionInterface) { - createLinesForTwoReactantReaction(result, points, gateMembers != null); - createOperatorsForTwoReactantReaction(result, gateMembers); - - } else if (result instanceof TwoProductReactionInterface) { - createLinesForTwoProductReaction(result, points); - createOperatorsForTwoProductReaction(result); - } else { - throw new ReactionParserException("Problem with parsing lines. Unknown reaction: " + type + "; " + result.getClass().getName(), result); - } - if (reactantsLinkNode != null) { - parseReactantLinks(result, reactantsLinkNode, model); - } - if (productsLinkNode != null) { - parseProductLinks(result, productsLinkNode, model); - } - - // create operators - createOperators(result); - - // now try to create modifiers (we must have set fixed layout data for the - // core of the reaction) - if (modifiersLinkNode != null) { - parseReactionModification(result, modifiersLinkNode, model); - } - for (int i = 0; i < result.getModifiers().size(); i++) { - Modifier modifier = result.getModifiers().get(i); - if (modifier.getElement() == null) { - List<Modifier> modifiers = new ArrayList<Modifier>(); - modifiers.add(modifier); - for (Modifier modifier2 : result.getModifiers()) { - if (modifierParentOperator.get(modifier2) == modifier) { - modifiers.add(modifier2); - } - } - computeLineForModification(result, modifiers); - createOperatorFromModifiers(result, modifiers); - result.removeModifier(modifier); - i--; - } else if (modifier.getLine() == null) { - createLineForModifier(result, modifier); - } - } - - if (line != null) { - for (AbstractNode rNode : result.getNodes()) { - rNode.getLine().setWidth(line.getWidth()); - rNode.getLine().setColor(line.getColor()); - } - } - if (result.isReversible()) { - for (Reactant reactant : result.getReactants()) { - reactant.getLine().getBeginAtd().setArrowType(result.getProducts().get(0).getLine().getEndAtd().getArrowType()); - } - } - - return result; - } - - /** - * Creates reaction with a new type. - * - * @param type - * CellDesigner type of a reaction - * @param result - * initial data - * @return reaction with a new type - * @throws UnknownReactionClassException - * thrown when type is invalid - */ - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - ReactionLineData rdl = ReactionLineData.getByCellDesignerString(type); - if (rdl == null) { - throw new UnknownReactionClassException("Unknown CellDesigner class type: " + type + ".", type, result.getIdReaction()); - } - return rdl.createReaction(result); - } - - /** - * Creates CellDesigner {@link EditPoints} structure for gate members node. - * - * @param gateMembers - * xml node - * @return {@link EditPoints} structure representing line information for the - * xml node - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private EditPoints gateMembersToPoints(Node gateMembers) throws InvalidXmlSchemaException { - Node lastMember = null; - EditPoints result = new EditPoints(); - Integer num0 = null; - Integer num1 = null; - Integer num2 = null; - for (int i = 0; i < gateMembers.getChildNodes().getLength(); i++) { - Node child = gateMembers.getChildNodes().item(i); - if (child.getNodeType() == Node.ELEMENT_NODE) { - - if (child.getNodeName().equalsIgnoreCase("celldesigner:GateMember")) { - String type = getNodeAttr("type", child); - if (type.startsWith(ReactionLineData.BOOLEAN_LOGIC_GATE.getCellDesignerString())) { - lastMember = child; - } else { - String pointsString = getNodeAttr("editPoints", child); - List<Point2D> points = parseEditPointsString(pointsString); - if (num0 == null) { - num0 = points.size(); - } else if (num1 == null) { - num1 = points.size(); - } else { - throw new InvalidXmlSchemaException("Too many gate members"); - } - result.getPoints().addAll(points); - } - } else { - throw new InvalidXmlSchemaException("Unknown node type: " + child.getNodeName()); - } - } - } - if (lastMember == null) { - throw new InvalidXmlSchemaException("Missing gate member connecting members"); - } else { - String pointsString = getNodeAttr("editPoints", lastMember); - List<Point2D> points = parseEditPointsString(pointsString); - num2 = points.size() - 1; - result.getPoints().addAll(points); - } - result.setNum0(num0); - result.setNum1(num1); - result.setNum2(num2); - return result; - } - - /** - * Creates line information for the modifier. - * - * @param reaction - * reaction where modifier is placed - * @param modifier - * modifier to update - */ - private void createLineForModifier(Reaction reaction, Modifier modifier) { - Element element = modifier.getElement(); - CellDesignerAliasConverter converter = new CellDesignerAliasConverter(element, sbgn); - Point2D startPoint = converter.getPointCoordinates(element, anchorsByNodes.get(modifier)); - ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); - - Point2D p = modifierTypeUtils.getAnchorPointOnReactionRect(modifier.getReaction(), lineTypeByModifier.get(modifier)); - PolylineData line = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, p, pointsByModifier.get(modifier)); - - startPoint = converter.getAnchorPointCoordinates(element, anchorsByNodes.get(modifier), line); - line.setStartPoint(startPoint); - modifier.setLine(line); - modifierTypeUtils.updateLineEndPoint(modifier); - } - - /** - * Creates operators for CellDesigner reaction that belongs to - * {@link TwoProductReactionInterface}. - * - * @param result - * reaction to be updated - * @throws ReactionParserException - * thrown when there is a problem with creating operators (input - * data is invalid) - */ - private void createOperatorsForTwoProductReaction(Reaction result) throws ReactionParserException { - int inputs = 0; - NodeOperator operator = null; - if (result.getClass() == DissociationReaction.class) { - operator = new DissociationOperator(); - } else if (result.getClass() == TruncationReaction.class) { - operator = new TruncationOperator(); - } else { - throw new ReactionParserException("Invalid reaction type", result); - } - for (AbstractNode node : result.getNodes()) { - if (node.getClass() == Product.class) { - operator.addOutput(node); - } else if (node.getClass() == Reactant.class) { - inputs++; - if (inputs > 1) { - throw new ReactionParserException("Reaction has more than one reactant", result); - } - } - } - if (operator.getOutputs().size() > 2) { - throw new ReactionParserException("Too many products: " + operator.getOutputs().size(), result); - } - - // and now we have to modify lines - - Reactant reactant = result.getReactants().get(0); - Integer index = indexByComplexReaction.get(reactant); - - Point2D p1, p2; - - p1 = reactant.getLine().getPoints().get(index); - p2 = reactant.getLine().getPoints().get(index + 1); - - p1 = new Point2D.Double(p1.getX(), p1.getY()); - p2 = new Point2D.Double(p2.getX(), p2.getY()); - - Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - operator.setLine(reactant.getLine().getSubline(0, index + 1)); - operator.getLine().addPoint(p); - - p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - - reactant.setLine(reactant.getLine().getSubline(index + 1, reactant.getLine().getPoints().size()).reverse()); - reactant.getLine().getPoints().add(p); - - reactant.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - operator.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - - result.addNode(operator); - } - - /** - * Creates general input/output operators for the model. - * - * @param result - * reaction to be updated - */ - private void createOperators(Reaction result) { - // central line points - Point2D p1 = result.getReactants().get(0).getLine().getPoints().get(result.getReactants().get(0).getLine().getPoints().size() - 2); - Point2D p2 = result.getProducts().get(0).getLine().getPoints().get(1); - Point2D tmp = result.getProducts().get(0).getLine().getPoints().get(0); - Point2D productSplitOperatorBeginPoint = new Point2D.Double(tmp.getX(), tmp.getY()); - - // where the line from reactants ends - tmp = result.getReactants().get(0).getLine().getPoints().get(result.getReactants().get(0).getLine().getPoints().size() - 1); - Point2D reactantAndOperatorEndPoint = new Point2D.Double(tmp.getX(), tmp.getY()); - - Set<AbstractNode> toExclude = new HashSet<AbstractNode>(); - Set<AbstractNode> toInclude = new HashSet<AbstractNode>(); - for (NodeOperator operator : result.getOperators()) { - toExclude.addAll(operator.getInputs()); - if (operator.isReactantOperator()) { - toInclude.add(operator); - // if we have operator in input then central line changes - p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); - tmp = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 1); - reactantAndOperatorEndPoint = new Point2D.Double(tmp.getX(), tmp.getY()); - } - if (operator.isProductOperator()) { - // if we have operator in output then central line changes - p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); - tmp = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 1); - productSplitOperatorBeginPoint = new Point2D.Double(tmp.getX(), tmp.getY()); - } - } - - double dx = p2.getX() - p1.getX(); - double dy = p2.getY() - p1.getY(); - - Point2D reactantAndOperatorBeginPoint = new Point2D.Double(p1.getX() + dx * REACTANT_END_RATIO, p1.getY() + dy * REACTANT_END_RATIO); - - PolylineData ld = new PolylineData(reactantAndOperatorBeginPoint, reactantAndOperatorEndPoint); - - LineType lineType = null; - Set<AbstractNode> nodes = new LinkedHashSet<AbstractNode>(); - for (Reactant reactant : result.getReactants()) { - if (!toExclude.contains(reactant)) { - nodes.add(reactant); - if (lineType == null) { - lineType = reactant.getLine().getType(); - } - } - } - nodes.addAll(toInclude); - nodes.removeAll(toExclude); - - // add an operator only when the number of input nodes is greater than one - if (nodes.size() > 1) { - AndOperator inputOperator = new AndOperator(); - inputOperator.setLine(ld); - inputOperator.addInputs(nodes); - for (Reactant reactant : result.getReactants()) { - if (!toExclude.contains(reactant)) { - reactant.getLine().getEndPoint().setLocation(reactantAndOperatorBeginPoint.getX(), reactantAndOperatorBeginPoint.getY()); - } - } - if (lineType != null) { - inputOperator.getLine().setType(lineType); - } - result.addNode(inputOperator); - } - - // and now we try to handle with output operators - - toExclude = new HashSet<AbstractNode>(); - toInclude = new HashSet<AbstractNode>(); - for (NodeOperator nOperator : result.getOperators()) { - toExclude.addAll(nOperator.getOutputs()); - if (nOperator.isProductOperator()) { - toInclude.add(nOperator); - } - } - - Point2D productSplitOperatorEndPoint = new Point2D.Double(p1.getX() + dx * PRODUCT_START_RATIO, p1.getY() + dy * PRODUCT_START_RATIO); - - ld = new PolylineData(productSplitOperatorEndPoint, productSplitOperatorBeginPoint); - - lineType = null; - nodes = new LinkedHashSet<AbstractNode>(); - for (Product product : result.getProducts()) { - if (!toExclude.contains(product)) { - nodes.add(product); - if (lineType == null) { - lineType = product.getLine().getType(); - } - } - } - nodes.addAll(toInclude); - nodes.removeAll(toExclude); - if (nodes.size() > 1) { - SplitOperator outputOperator = new SplitOperator(); - outputOperator.setLine(ld); - outputOperator.addOutputs(nodes); - for (Product product : result.getProducts()) { - if (!toExclude.contains(product)) { - // outputOperator.addOutput(product); - product.getLine().getPoints().get(0).setLocation(productSplitOperatorEndPoint.getX(), productSplitOperatorEndPoint.getY()); - } - } - if (lineType != null) { - outputOperator.getLine().setType(lineType); - } - result.addNode(outputOperator); - } - } - - /** - * Creates operators in modifiers. - * - * @param reaction - * reaction to update - * @param modifiers - * list of modifiers that must be put into operators - */ - private void createOperatorFromModifiers(Reaction reaction, List<Modifier> modifiers) { - OperatorTypeUtils otu = new OperatorTypeUtils(); - String type = typeByModifier.get(modifiers.get(0)); - NodeOperator operator = otu.createModifierForStringType(type); - - operator.setLine(modifiers.get(0).getLine()); - - for (int i = 1; i < modifiers.size(); i++) { - operator.addInput(modifiers.get(i)); - } - - ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); - modifierTypeUtils.updateLineEndPoint(operator); - reaction.addNode(operator); - } - - /** - * Creates operators for CellDesigner reaction that belongs to - * {@link TwoReactantReactionInterface}. - * - * @param result - * reaction to be updated - * @param gateMembers - * node representing line information for the operator - * @throws ReactionParserException - * thrown when the xml is invalid - */ - private void createOperatorsForTwoReactantReaction(Reaction result, Node gateMembers) throws ReactionParserException { - NodeOperator andOperator = null; - if (result instanceof HeterodimerAssociationReaction) { - andOperator = new AssociationOperator(); - } else if (result instanceof BooleanLogicGateReaction) { - andOperator = new AndOperator(); - } else { - throw new ReactionParserException("Unknown class type with two reactants", result); - } - int outputs = 0; - for (AbstractNode node : result.getNodes()) { - if (node.getClass() == Reactant.class) { - andOperator.addInput(node); - } else if (node.getClass() == Product.class) { - outputs++; - if (outputs > 1) { - throw new ReactionParserException("Reaction has more than one product", result); - } - } - } - if (andOperator.getInputs().size() > 2) { - throw new ReactionParserException("Too many reactants.", result); - } - - // and now we have to modify lines - - Product product = result.getProducts().get(0); - Integer index = indexByComplexReaction.get(product); - if (index != null) { - Point2D p1, p2; - - p1 = product.getLine().getPoints().get(index); - p2 = product.getLine().getPoints().get(index + 1); - - p1 = new Point2D.Double(p1.getX(), p1.getY()); - p2 = new Point2D.Double(p2.getX(), p2.getY()); - - Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - andOperator.setLine(product.getLine().getSubline(0, index + 1)); - andOperator.getLine().addPoint(p); - andOperator.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - - p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - - product.setLine(product.getLine().getSubline(index, product.getLine().getPoints().size())); - product.getLine().getPoints().set(0, p); - product.getLine().trimBegin(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - - product.getLine().getEndAtd().setArrowType(ArrowType.FULL); - - result.addNode(andOperator); - } else { - NodeOperator operator = null; - Set<String> undefinedTypes = new HashSet<String>(); - for (int i = 0; i < gateMembers.getChildNodes().getLength(); i++) { - Node child = gateMembers.getChildNodes().item(i); - if (child.getNodeType() == Node.ELEMENT_NODE) { - - if (child.getNodeName().equalsIgnoreCase("celldesigner:GateMember")) { - String type = getNodeAttr("type", child); - - if (type.equalsIgnoreCase(OperatorType.AND_OPERATOR_STRING.getStringName())) { - operator = new AndOperator(); - } else if (type.equalsIgnoreCase(OperatorType.OR_OPERATOR_STRING.getStringName())) { - operator = new OrOperator(); - } else if (type.equalsIgnoreCase(OperatorType.NAND_OPERATOR_STRING.getStringName())) { - operator = new NandOperator(); - } else if (type.equalsIgnoreCase(OperatorType.UNKNOWN_OPERATOR_STRING.getStringName())) { - operator = new UnknownOperator(); - } else { - undefinedTypes.add(type); - } - } - } - } - if (operator == null) { - String types = ""; - for (String string : undefinedTypes) { - types += string + ", "; - } - throw new ReactionParserException("Couldn't find type of BOOLEAN_LOGIC_GATE. Unknown types identified: " + types, result); - } - - operator.addInputs(andOperator.getInputs()); - operator.addOutput(product); - - // empty line - PolylineData line = new PolylineData(); - line.addPoint(product.getLine().getBeginPoint()); - line.addPoint(product.getLine().getBeginPoint()); - - operator.setLine(line); - result.addNode(operator); - } - - } - - /** - * Creates lines for reaction that belongs to - * {@link TwoProductReactionInterface}. - * - * @param reaction - * reaction to update - * @param points - * information about points - */ - private void createLinesForTwoProductReaction(Reaction reaction, EditPoints points) { - Point2D p = points.getPoints().get(points.size() - 1); - Product product1 = reaction.getProducts().get(0); - Product product2 = reaction.getProducts().get(1); - Reactant reactant = reaction.getReactants().get(0); - - CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(reactant.getElement(), sbgn); - CellDesignerAliasConverter product1Converter = new CellDesignerAliasConverter(product1.getElement(), sbgn); - CellDesignerAliasConverter product2Converter = new CellDesignerAliasConverter(product2.getElement(), sbgn); - - Point2D p1 = reactantConverter.getPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant)); - Point2D p2 = product1Converter.getPointCoordinates(product1.getElement(), anchorsByNodes.get(product1)); - Point2D p3 = product2Converter.getPointCoordinates(product2.getElement(), anchorsByNodes.get(product2)); - - Point2D centerPoint = pointTransformation - .getCoordinatesInNormalBase(product1.getElement().getCenter(), product2.getElement().getCenter(), reactant.getElement().getCenter(), p); - - int startId0 = 0; - int num0 = points.getNum0(); - int startId1 = num0; - int num1 = num0 + points.getNum1(); - int startId2 = num1; - int num2 = num1 + points.getNum2(); - - ArrayList<Point2D> linePoints1 = new ArrayList<Point2D>(points.getPoints().subList(startId0, num0)); - ArrayList<Point2D> linePoints2 = new ArrayList<Point2D>(points.getPoints().subList(startId1, num1)); - ArrayList<Point2D> linePoints3 = new ArrayList<Point2D>(points.getPoints().subList(startId2, num2)); - - PolylineData product1Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p2, linePoints2); - PolylineData product2Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p3, linePoints3); - - PolylineData reactantLine = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p1, linePoints1); - - p1 = product2Converter.getAnchorPointCoordinates(product2.getElement(), anchorsByNodes.get(product2), product2Line.reverse()); - product2Line.setEndPoint(p1); - - p1 = product1Converter.getAnchorPointCoordinates(product1.getElement(), anchorsByNodes.get(product1), product1Line.reverse()); - product1Line.setEndPoint(p1); - - p1 = reactantConverter.getAnchorPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant), reactantLine.reverse()); - reactantLine.setEndPoint(p1); - - product2Line.getEndAtd().setArrowType(ArrowType.FULL); - product1Line.getEndAtd().setArrowType(ArrowType.FULL); - product1.setLine(product1Line); - product2.setLine(product2Line); - reactant.setLine(reactantLine); - - indexByComplexReaction.put(reactant, points.getIndex()); - - } - - /** - * Creates lines for reaction that belongs to - * {@link TwoReactantReactionInterface}. - * - * @param reaction - * reaction to update - * @param points - * information about points - * @param hasGateMemebers - * does the reaction has gate members - * @throws ReactionParserException - * thrown when data for reaction is invalid - */ - private void createLinesForTwoReactantReaction(Reaction reaction, EditPoints points, boolean hasGateMemebers) throws ReactionParserException { - Point2D p = points.getPoints().get(points.size() - 1); - Product product = reaction.getProducts().get(0); - Reactant reactant1 = reaction.getReactants().get(0); - Reactant reactant2 = reaction.getReactants().get(1); - CellDesignerAliasConverter productConverter = new CellDesignerAliasConverter(product.getElement(), sbgn); - CellDesignerAliasConverter reactantConverter1 = new CellDesignerAliasConverter(reactant1.getElement(), sbgn); - CellDesignerAliasConverter reactantConverter2 = new CellDesignerAliasConverter(reactant2.getElement(), sbgn); - Point2D p1 = reactantConverter1.getPointCoordinates(reactant1.getElement(), anchorsByNodes.get(reactant1)); - Point2D p2 = reactantConverter2.getPointCoordinates(reactant2.getElement(), anchorsByNodes.get(reactant2)); - Point2D p3 = productConverter.getPointCoordinates(product.getElement(), anchorsByNodes.get(product)); - - Element alias1 = reactant1.getElement(); - Element alias2 = reactant2.getElement(); - Element alias3 = product.getElement(); - - Point2D pointA = alias2.getCenter(); - Point2D pointC = alias1.getCenter(); - Point2D pointB = alias3.getCenter(); - Point2D pointP = p; - Point2D centerPoint = null; - if (!hasGateMemebers) { - centerPoint = pointTransformation.getCoordinatesInNormalBase(pointA, pointB, pointC, pointP); - } else { - centerPoint = pointP; - } - - int startId0 = 0; - int num0 = points.getNum0(); - int startId1 = num0; - int num1 = num0 + points.getNum1(); - int startId2 = num1; - int num2 = num1 + points.getNum2(); - - List<Point2D> linePoints1 = new ArrayList<Point2D>(points.getPoints().subList(startId0, num0)); - List<Point2D> linePoints2 = new ArrayList<Point2D>(points.getPoints().subList(startId1, num1)); - List<Point2D> linePoints3 = new ArrayList<Point2D>(points.getPoints().subList(startId2, num2)); - - PolylineData reactant1Line = null; - PolylineData reactant2Line = null; - if (!hasGateMemebers) { - reactant1Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p1, linePoints1); - reactant2Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p2, linePoints2); - } else { - reactant1Line = PolylineDataFactory.createPolylineDataFromEditPoints(p1, centerPoint, linePoints1); - reactant1Line = reactant1Line.reverse(); - - reactant2Line = PolylineDataFactory.createPolylineDataFromEditPoints(p2, centerPoint, linePoints2); - reactant2Line = reactant2Line.reverse(); - } - - PolylineData productLine = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p3, linePoints3); - - p1 = reactantConverter1.getAnchorPointCoordinates(reactant1.getElement(), anchorsByNodes.get(reactant1), reactant1Line.reverse()); - reactant1Line.setEndPoint(p1); - reactant1Line = reactant1Line.reverse(); - - p1 = reactantConverter2.getAnchorPointCoordinates(reactant2.getElement(), anchorsByNodes.get(reactant2), reactant2Line.reverse()); - reactant2Line.setEndPoint(p1); - reactant2Line = reactant2Line.reverse(); - - p1 = productConverter.getAnchorPointCoordinates(product.getElement(), anchorsByNodes.get(product), productLine.reverse()); - productLine.setEndPoint(p1); - if (hasGateMemebers) { - productLine.getEndAtd().setArrowType(ArrowType.OPEN); - } - - product.setLine(productLine); - reactant1.setLine(reactant1Line); - reactant2.setLine(reactant2Line); - - indexByComplexReaction.put(product, points.getIndex()); - } - - /** - * Creates lines for reaction that belongs to {@link SimpleReactionInterface} - * (only one standard product and one standard reactant). - * - * @param reaction - * reaction to update - * @param editPoints - * information about points - * @param scheme - * some additional information about the line - */ - private void createLinesForSimpleReaction(Reaction reaction, EditPoints editPoints, ConnectScheme scheme) { - Product product = reaction.getProducts().get(0); - Reactant reactant = reaction.getReactants().get(0); - CellDesignerAliasConverter productConverter = new CellDesignerAliasConverter(product.getElement(), sbgn); - CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(reactant.getElement(), sbgn); - - Point2D endPoint = productConverter.getPointCoordinates(product.getElement(), anchorsByNodes.get(product)); - Point2D startPoint = reactantConverter.getPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant)); - - PolylineData ld = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, endPoint, editPoints.getPoints()); - - // first place where the index of rectangle is kept - Integer index = editPoints.getIndex(); - - // second place where index of rectangle is kept - if (index == null) { - index = scheme.getConnectIndex(); - if (index != null) { - // but... - // direction of the index is reversed... - index = ld.getPoints().size() - index - 2; - } - } - // but sometimes there is no information about index... - if (index == null) { - index = 0; - } - startPoint = reactantConverter.getAnchorPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant), ld); - endPoint = productConverter.getAnchorPointCoordinates(product.getElement(), anchorsByNodes.get(product), ld.reverse()); - ld.getPoints().set(0, startPoint); - ld.getPoints().set(ld.getPoints().size() - 1, endPoint); - - PolylineData line = new PolylineData(ld); - - PolylineData reactantLine = line.getSubline(0, ld.getPoints().size() - index - 1); - PolylineData productLine = line.getSubline(ld.getPoints().size() - index - 2, ld.getPoints().size()); - - Point2D p1 = ld.getPoints().get(ld.getPoints().size() - index - 2); - Point2D p2 = ld.getPoints().get(ld.getPoints().size() - index - 1); - Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - reactantLine.getPoints().add(p); - reactantLine.trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - - p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); - productLine.getPoints().set(0, p); - productLine.trimBegin(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); - - productLine.getEndAtd().setArrowType(ArrowType.FULL); - - reactant.setLine(reactantLine); - product.setLine(productLine); - - ReactionLineData rld = ReactionLineData.getByReactionType(reaction.getClass()); - reactantLine.setType(rld.getLineType()); - productLine.setType(rld.getLineType()); - productLine.getEndAtd().setArrowType(rld.getProductArrowType()); - productLine.trimEnd(rld.getProductLineTrim()); - } - - /** - * Prepares {@link EditPoints} structure from xml node. - * - * @param rootNode - * xml node - * @return {@link EditPoints} object containing CellDesigner line data - */ - private EditPoints parseEditPoints(Node rootNode) { - EditPoints result = new EditPoints(); - String num0 = getNodeAttr("num0", rootNode); - String num1 = getNodeAttr("num1", rootNode); - String num2 = getNodeAttr("num2", rootNode); - if (!num0.equals("")) { - result.setNum0(Integer.parseInt(num0)); - } - if (!num1.equals("")) { - result.setNum1(Integer.parseInt(num1)); - } - if (!num2.equals("")) { - result.setNum2(Integer.parseInt(num2)); - } - String index = getNodeAttr("tShapeIndex", rootNode); - if (!index.equals("")) { - result.setIndex(Integer.parseInt(index)); - } - - String pointsString = getNodeValue(rootNode); - result.setPoints(parseEditPointsString(pointsString)); - return result; - } - - /** - * Parses CellDesigner point by point string. - * - * @param pointsString - * String containing points - * @return list of points - */ - ArrayList<Point2D> parseEditPointsString(String pointsString) { - ArrayList<Point2D> points2 = new ArrayList<Point2D>(); - if (pointsString.isEmpty()) { - return points2; - } - String[] points = pointsString.trim().split(" "); - for (String string : points) { - String[] p = string.split(","); - if (p.length != 2) { - throw new InvalidArgumentException("Invalid editPoint string: " + string); - } else { - double posX = Double.parseDouble(p[0]); - double posY = Double.parseDouble(p[1]); - Point2D point = new Point2D.Double(posX, posY); - if (!pointTransformation.isValidPoint(point)) { - throw new InvalidArgumentException("Invalid point parsed from input string: " + string + ". Result point: " + point); - } - points2.add(point); - } - } - return points2; - } - - /** - * Parses xml node with reactantLink nodes and creates reactants in the - * reaction. - * - * @param result - * reaction to be updated - * @param rootNode - * xml node to parse - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when the xml is invalid - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private void parseReactantLinks(Reaction result, Node rootNode, Model model) throws ReactionParserException { - NodeList list = rootNode.getChildNodes(); - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:reactantLink")) { - Reactant newReactant = getReactantLink(node, model, result); - result.addReactant(newReactant); - } else { - throw new ReactionParserException("Unknown element of celldesigner:listOfReactantLinks: " + node.getNodeName(), result); - } - } - } - } - - /** - * Parses xml node for reactanLink and creates reactant from it. - * - * @param rootNode - * xml node to parse - * @param model - * model where reaction is placed - * @param reaction - * reaction that is being parsed - * @return reactant obtained from xml node - * @throws ReactionParserException - * thrown when the xml is invalid and reason is more specific - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private Reactant getReactantLink(Node rootNode, Model model, Reaction reaction) throws ReactionParserException { - String aliasId = getNodeAttr("alias", rootNode); - Species al = (Species) model.getElementByElementId(aliasId); - if (al == null) { - throw new ReactionParserException("Alias doesn't exist (id: " + aliasId + ")", reaction); - } - - Reactant result = new Reactant(al); - CellDesignerAnchor anchor = null; - EditPoints points = null; - - NodeList nodes = rootNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { - anchor = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { - points = parseEditPoints(node); - } else { - throw new ReactionParserException("Unknown element of celldesigner:reactantLink: " + node.getNodeName(), reaction); - } - } - } - - CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(al, sbgn); - Point2D additionalPoint = reactantConverter.getPointCoordinates(al, anchor); - ArrowTypeData atd = new ArrowTypeData(); - atd.setArrowType(ArrowType.NONE); - - Point2D endPoint; - - Line2D centerLine = reaction.getCenterLine(); - - double dx = centerLine.getX2() - centerLine.getX1(); - double dy = centerLine.getY2() - centerLine.getY1(); - double coef = REACTANT_END_RATIO; - endPoint = new Point2D.Double(centerLine.getX1() + dx * coef, centerLine.getY1() + dy * coef); - - PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(additionalPoint, endPoint, points); - - additionalPoint = reactantConverter.getAnchorPointCoordinates(al, anchor, polyline); - polyline.setStartPoint(additionalPoint); - result.setLine(polyline); - - return result; - } - - /** - * Parses xml node with productLink nodes and creates products in the - * reaction. - * - * @param result - * reaction to be updated - * @param rootNode - * xml node to parse - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when the xml is invalid - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private void parseProductLinks(Reaction result, Node rootNode, Model model) throws ReactionParserException { - NodeList list = rootNode.getChildNodes(); - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:productLink")) { - Product link = getProductLink(node, model, result); - result.addProduct(link); - } else { - throw new ReactionParserException("Unknown element of celldesigner:listOfProductLinks: " + node.getNodeName(), result); - } - } - } - } - - /** - * Parses xml node for productLink and creates product from it. - * - * @param rootNode - * xml node to parse - * @param model - * model where reaction is placed - * @param reaction - * reaction that is being parsed - * @return product obtained from xml node - * @throws ReactionParserException - * thrown when the xml is invalid - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private Product getProductLink(Node rootNode, Model model, Reaction reaction) throws ReactionParserException { - String aliasId = getNodeAttr("alias", rootNode); - Species al = (Species) model.getElementByElementId(aliasId); - if (al == null) { - throw new ReactionParserException("Alias doesn't exist (id: " + aliasId + ")", reaction); - } - - Product result = new Product(al); - - CellDesignerAnchor anchor = null; - EditPoints points = null; - - NodeList nodes = rootNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { - anchor = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { - points = parseEditPoints(node); - } else { - throw new ReactionParserException("Unknown element of celldesigner:reactantLink: " + node.getNodeName(), reaction); - } - } - } - - CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(al, sbgn); - Point2D additionalPoint = reactantConverter.getPointCoordinates(al, anchor); - ArrowTypeData atd = new ArrowTypeData(); - atd.setArrowType(ArrowType.NONE); - - Point2D endPoint; - - // central line points - Point2D p1 = reaction.getReactants().get(0).getLine().getPoints().get(reaction.getReactants().get(0).getLine().getPoints().size() - 2); - Point2D p2 = reaction.getProducts().get(0).getLine().getPoints().get(1); - - Set<AbstractNode> toExclude = new HashSet<AbstractNode>(); - Set<AbstractNode> toInclude = new HashSet<AbstractNode>(); - for (NodeOperator operator : reaction.getOperators()) { - toExclude.addAll(operator.getInputs()); - if (operator.isReactantOperator()) { - toInclude.add(operator); - // if we have operator in input then central line changes - p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); - } - if (operator.isProductOperator()) { - // if we have operator in output then central line changes - p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); - } - } - - double dx = p2.getX() - p1.getX(); - double dy = p2.getY() - p1.getY(); - double coef = PRODUCT_START_RATIO; - endPoint = new Point2D.Double(p1.getX() + dx * coef, p1.getY() + dy * coef); - - PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(endPoint, additionalPoint, points); - additionalPoint = reactantConverter.getAnchorPointCoordinates(al, anchor, polyline.reverse()); - polyline.setEndPoint(additionalPoint); - - polyline.getEndAtd().setArrowType(ArrowType.FULL); - result.setLine(polyline); - return result; - } - - /** - * Creates {@link LineProperties} object from the node. - * - * @param node - * xml node to parse - * @return {@link LineProperties} object - */ - private LineProperties getLineProperties(Node node) { - LineProperties line = new LineProperties(); - String tmp = getNodeAttr("width", node); - line.setWidth(Double.parseDouble(tmp)); - tmp = getNodeAttr("color", node); - line.setColor(stringToColor(tmp)); - line.setType(getNodeAttr("type", node)); - return line; - } - - /** - * PArse reaction modifiactions and add them into reaction. - * - * @param result - * reaction currently processed - * @param rootNode - * xml node to parse - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when the xml is invalid, and reason is more specific - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private void parseReactionModification(Reaction result, Node rootNode, Model model) throws ReactionParserException { - NodeList nodes = rootNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:modification")) { - parseModificationReaction(result, node, model); - } else { - throw new ReactionParserException("Unknown element of celldesigner:listOfModification: " + node.getNodeName(), result); - } - } - } - } - - /** - * PArses modification node and adds this modification to reaction. - * - * @param reaction - * reaction currently processed - * @param rootNode - * xml node - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when node cannot be parsed properly - */ - private void parseModificationReaction(Reaction reaction, Node rootNode, Model model) throws ReactionParserException { - ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); - String type = getNodeAttr("type", rootNode); - String modificationType = getNodeAttr("modificationType", rootNode); - String aliasId = getNodeAttr("aliases", rootNode); - String lineConnectionType = getNodeAttr("targetLineIndex", rootNode); - String points = getNodeAttr("editPoints", rootNode); - - List<Species> aliasList = new ArrayList<Species>(); - - String[] list = aliasId.split(","); - for (String string : list) { - Species al = (Species) model.getElementByElementId(string); - if (al != null) { - aliasList.add(al); - } else { - throw new ReactionParserException("Unknown alias: " + string, reaction); - } - } - Modifier result = null; - if (aliasList.size() > 1) { - result = new Modifier(null); - reaction.addModifier(result); - for (int i = 0; i < aliasList.size(); i++) { - Modifier mod = modifierTypeUtils.createModifierForStringType(modificationType, aliasList.get(i)); - modifierParentOperator.put(mod, result); - reaction.addModifier(mod); - } - } else { - ModifierType mType = modifierTypeUtils.getModifierTypeForStringType(type); - if (mType == null) { - String errorInfo = "[" + reaction.getClass().getSimpleName() + "\t" + reaction.getIdReaction() + "]\tUnknown modifier type: " + type; - if (ReactionLineData.getByCellDesignerString(type) != null) { - errorInfo += ".\tThis type can be applied to reaction type only, not modifier."; - } - throw new UnknownModifierClassException(errorInfo, type, reaction.getIdReaction()); - } - for (Modifier modifier : reaction.getModifiers()) { - if (modifier.getElement() == aliasList.get(0) && modifier.getClass() == mType.getClazz()) { - result = modifier; - } - } - if (result == null) { - result = modifierTypeUtils.createModifierForStringType(type, aliasList.get(0)); - reaction.addModifier(result); - } - } - pointsByModifier.put(result, parseEditPointsString(points)); - typeByModifier.put(result, type); - lineTypeByModifier.put(result, lineConnectionType); - - NodeList nodes = rootNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { - continue; - } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkTarget")) { - try { - CellDesignerAnchor anchor = getAnchorFromLinkTarget(node); - anchorsByNodes.put(result, anchor); - } catch (InvalidXmlSchemaException e) { - throw new ReactionParserException(reaction, e); - } - } else { - throw new ReactionParserException("Unknown element of celldesigner:listOfModification: " + node.getNodeName(), reaction); - } - } - - } - - } - - /** - * Creates lines for modifiers. - * - * @param reaction - * reaction where modifiers are placed - * @param modifiers - * list of modifiers for which lines should be generated - */ - private void computeLineForModification(Reaction reaction, List<Modifier> modifiers) { - ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); - Point2D p = modifierTypeUtils.getAnchorPointOnReactionRect(reaction, lineTypeByModifier.get(modifiers.get(0))); - - Point2D startPoint; - List<Point2D> subPoints; - // in case we have more then one alias in modification then we need to - // define the start point as a different one then one on the alias - Modifier modifier = modifiers.get(0); - List<Point2D> points = pointsByModifier.get(modifier); - startPoint = points.get(points.size() - 1); - subPoints = points.subList(0, points.size() - 1); - PolylineData line = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, p, subPoints); - modifier.setLine(line); - - Point2D endPoint = startPoint; - - for (int i = 1; i < modifiers.size(); i++) { - Modifier param = modifiers.get(i); - CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(param.getElement(), sbgn); - startPoint = reactantConverter.getPointCoordinates(param.getElement(), anchorsByNodes.get(param)); - - PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, endPoint, pointsByModifier.get(param)); - - startPoint = reactantConverter.getAnchorPointCoordinates(param.getElement(), anchorsByNodes.get(param), polyline); - polyline.setStartPoint(startPoint); - param.setLine(polyline); - } - } - - /** - * Returns {@link CellDesignerAnchor} point from linkAnchor xml node. - * - * @param rootNode - * xml node - * @return {@link CellDesignerAnchor} object representing anchor point - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private CellDesignerAnchor getAnchorFromLinkTarget(Node rootNode) throws InvalidXmlSchemaException { - CellDesignerAnchor result = null; - - NodeList nodes = rootNode.getChildNodes(); - for (int z = 0; z < nodes.getLength(); z++) { - Node node = nodes.item(z); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { - result = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); - } else { - throw new InvalidXmlSchemaException("Unknown element of celldesigner:connectScheme: " + node.getNodeName()); - } - } - } - return result; - } - - /** - * Creates {@link ConnectScheme} object from xml node. - * - * @param nodeReaction - * xml node to parse - * @return {@link ConnectScheme} object for given xml - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private ConnectScheme parseConnectScheme(Node nodeReaction) throws InvalidXmlSchemaException { - ConnectScheme result = new ConnectScheme(); - result.setConnectPolicy(getNodeAttr("connectPolicy", nodeReaction)); - result.setConnectIndex(getNodeAttr("rectangleIndex", nodeReaction)); - NodeList reactantsNodes = nodeReaction.getChildNodes(); - for (int z = 0; z < reactantsNodes.getLength(); z++) { - Node reactantNode = reactantsNodes.item(z); - if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { - if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:listOfLineDirection")) { - result.setLineDirections(getlineDirectionMapForReactions(reactantNode)); - } else { - throw new InvalidXmlSchemaException("Unknown element of celldesigner:connectScheme: " + nodeReaction.getNodeName()); - } - } - } - return result; - } - - /** - * Parse lineDirectionMap. This structure is nowhere used. - * - * @param reactantNode - * xml node - * @return map with directions for every line - * @throws InvalidXmlSchemaException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private Map<String, String> getlineDirectionMapForReactions(Node reactantNode) throws InvalidXmlSchemaException { - Map<String, String> result = new HashMap<String, String>(); - NodeList nodes = reactantNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:lineDirection")) { - String index = getNodeAttr("index", node); - String value = getNodeAttr("value", node); - result.put(index, value); - } else { - throw new InvalidXmlSchemaException("Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName()); - } - } - } - return result; - } - - /** - * PArses baseReactant node from CellDEsigenr xml node and adds it into - * reaction. - * - * @param result - * reaction that is processed - * @param reactantNode - * xml node to parse - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private void parseBaseReactant(Reaction result, Node reactantNode, Model model) throws ReactionParserException { - String aliasId = getNodeAttr("alias", reactantNode); - Species alias = (Species) model.getElementByElementId(aliasId); - if (alias == null) { - throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result); - } - Reactant reactant = new Reactant(alias); - result.addReactant(reactant); - NodeList nodes = reactantNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { - anchorsByNodes.put(reactant, CellDesignerAnchor.valueOf(getNodeAttr("position", node))); - } else { - throw new ReactionParserException("Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName(), result); - } - } - - } - } - - /** - * Parses baseProduct node from CellDEsigenr xml node and adds it into - * reaction. - * - * @param result - * reaction that is processed - * @param reactantNode - * xml node to parse - * @param model - * model where reaction is placed - * @throws ReactionParserException - * thrown when xml node contains data that is not supported by xml - * schema - */ - private void parseBaseProduct(Model model, Reaction result, Node reactantNode) throws ReactionParserException { - String aliasId = getNodeAttr("alias", reactantNode); - Species alias = (Species) model.getElementByElementId(aliasId); - if (alias == null) { - throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result); - } - Product product = new Product(alias); - result.addProduct(product); - NodeList nodes = reactantNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { - anchorsByNodes.put(product, CellDesignerAnchor.valueOf(getNodeAttr("position", node))); - } else { - throw new ReactionParserException("Unknown element of reaction/celldesigner:baseProduct: " + node.getNodeName(), result); - } - } - } - - } + /** + * Reactant lines in cell designer ends in the 2/5 of the center line. + */ + private static final double REACTANT_END_RATIO = 0.4; + + /** + * Product lines in cell designer starts in the 3/5 of the center line. + */ + private static final double PRODUCT_START_RATIO = 0.6; + + /** + * Stores information about {@link CellDesignerAnchor} for a node. + */ + private Map<ReactionNode, CellDesignerAnchor> anchorsByNodes = new HashMap<ReactionNode, CellDesignerAnchor>(); + + /** + * Stores information about operator type that should be used for a modifier + * node. + */ + private Map<ReactionNode, String> typeByModifier = new HashMap<ReactionNode, String>(); + + /** + * Stores information to which point on the central rectangle modifier should be + * connected. + */ + private Map<ReactionNode, String> lineTypeByModifier = new HashMap<ReactionNode, String>(); + + /** + * Stores information about list of points that create line describing modifier. + */ + private Map<ReactionNode, List<Point2D>> pointsByModifier = new HashMap<ReactionNode, List<Point2D>>(); + + /** + * Helps to determine if the key Modifier should be treats as part of + * NodeOperator (value in the map). + */ + private Map<Modifier, Modifier> modifierParentOperator = new HashMap<Modifier, Modifier>(); + + /** + * Identifies central line segment in {@link TwoProductReactionInterface} and + * {@link TwoReactantReactionInterface} reactions. + */ + private Map<ReactionNode, Integer> indexByComplexReaction = new HashMap<ReactionNode, Integer>(); + + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private Logger logger = Logger.getLogger(ReactionXmlParser.class.getName()); + + /** + * Xml parser used for processing notes into structured data. + */ + private RestAnnotationParser rap = new RestAnnotationParser(); + + /** + * Helper object used for manipulation on the point coordinates in CellDesigner + * format. + */ + private CellDesignerPointTransformation pointTransformation = new CellDesignerPointTransformation(); + + /** + * Should SBGN standard be used. + */ + private boolean sbgn; + + /** + * Default constructor. + * + * @param sbgn + * Should the converter use SBGN standard + */ + public ReactionFromXml(boolean sbgn) { + this.sbgn = sbgn; + } + + /** + * Returns {@link Reaction} object from CellDesigner xml node. + * + * @param reactionNode + * xml node + * @param model + * model where the reaction is placed + * @return reactin from xml node + * @throws ReactionParserException + * thrown when the xml is invalid + * @throws InvalidXmlSchemaException + * thrown when reactionNode is invalid xml + */ + public Reaction getReaction(Node reactionNode, Model model) throws ReactionParserException { + Reaction result = new Reaction(); + // we ignore metaid - it's useless and obstruct data model + // result.setMetaId(getNodeAttr("metaid", reactionNode)); + result.setIdReaction(getNodeAttr("id", reactionNode)); + result.setName(getNodeAttr("name", reactionNode)); + // by default this value is true... + result.setReversible(!(getNodeAttr("reversible", reactionNode).equalsIgnoreCase("false"))); + + NodeList nodes = reactionNode.getChildNodes(); + Node annotationNode = null; + Node kineticsNode = null; + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("annotation")) { + annotationNode = node; + } else if (node.getNodeName().equalsIgnoreCase("listOfReactants")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("listOfProducts")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("listOfModifiers")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("notes")) { + rap.processNotes(rap.getNotes(node), result); + } else if (node.getNodeName().equalsIgnoreCase("kineticLaw")) { + kineticsNode = node; + } else { + throw new ReactionParserException("Unknown element of reaction: " + node.getNodeName(), result); + } + } + } + if (annotationNode != null) { + result = parseReactionAnnotation(annotationNode, result, model); + } else { + throw new ReactionParserException("No annotation node in reaction", result); + } + if (kineticsNode != null) { + Map<String, Element> elements = getSpeciesIdToElementMappingFromAnnotationNode(annotationNode, model); + KineticsXmlParser kineticsParser = new KineticsXmlParser(model); + try { + result.setKinetics(kineticsParser.parseKinetics(kineticsNode, elements)); + } catch (InvalidXmlSchemaException e) { + throw new ReactionParserException(result, e); + } + } + + return result; + } + + private Map<String, Element> getSpeciesIdToElementMappingFromAnnotationNode(Node annotationNode, Model model) { + Map<String, Element> result = new HashMap<>(); + + List<Node> elementNodes = new ArrayList<>(); + elementNodes.addAll(super.getAllNotNecessirellyDirectChild("celldesigner:baseReactant", annotationNode)); + elementNodes.addAll(super.getAllNotNecessirellyDirectChild("celldesigner:baseProduct", annotationNode)); + elementNodes.addAll(super.getAllNotNecessirellyDirectChild("celldesigner:linkTarget", annotationNode)); + + for (Node node: elementNodes) { + String speciesId = super.getNodeAttr("species", node); + String aliasId = super.getNodeAttr("alias", node); + result.put(speciesId, model.getElementByElementId(aliasId)); + } + return result; + } + + /** + * Parses reaction annotation node and update reaction. + * + * @param annotationNode + * xml node + * @param result + * reaction to update + * @param model + * model where reaction is placed + * @return updated reaction + * @throws ReactionParserException + * thrown when the xml is invalid + * @throws InvalidXmlSchemaException + * thrown when annotationNode is invalid xml + */ + private Reaction parseReactionAnnotation(Node annotationNode, Reaction result, Model model) + throws ReactionParserException { + NodeList nodes = annotationNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:extension")) { + result = parseReactionExtension(result, node, model); + } else if (node.getNodeName().equalsIgnoreCase("rdf:RDF")) { + try { + XmlAnnotationParser xmlParser = new XmlAnnotationParser(); + result.addMiriamData(xmlParser.parseRdfNode(nodes)); + } catch (InvalidXmlSchemaException e) { + throw new ReactionParserException("Problem with parsing RDF", result, e); + } + } else { + throw new ReactionParserException("Unknown element of reaction/annotation: " + node.getNodeName(), result); + } + } + } + return result; + } + + /** + * Parses CellDesigner extension to sbml reaction node and updates reaction. + * + * @param result + * reaction to update + * @param node + * xml node + * @param model + * model where reaction is placed + * @return updated reaction + * @throws ReactionParserException + * thrown when the xml is invalid and reason is more specific + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + * + */ + private Reaction parseReactionExtension(Reaction result, Node node, Model model) throws ReactionParserException { + NodeList extensionNodes = node.getChildNodes(); + LineProperties line = null; + ConnectScheme connectScheme = null; + EditPoints points = null; + Node reactantsLinkNode = null; + Node productsLinkNode = null; + Node modifiersLinkNode = null; + Node gateMembers = null; + String type = null; + for (int y = 0; y < extensionNodes.getLength(); y++) { + Node nodeReaction = extensionNodes.item(y); + if (nodeReaction.getNodeType() == Node.ELEMENT_NODE) { + if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:reactionType")) { + type = getNodeValue(nodeReaction); + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseReactants")) { + NodeList reactantsNodes = nodeReaction.getChildNodes(); + for (int z = 0; z < reactantsNodes.getLength(); z++) { + Node reactantNode = reactantsNodes.item(z); + if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { + if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseReactant")) { + parseBaseReactant(result, reactantNode, model); + } else { + throw new ReactionParserException( + "Unknown element of celldesigner:baseReactants: " + node.getNodeName(), result); + } + } + } + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseProducts")) { + NodeList reactantsNodes = nodeReaction.getChildNodes(); + for (int z = 0; z < reactantsNodes.getLength(); z++) { + Node reactantNode = reactantsNodes.item(z); + if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { + if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseProduct")) { + parseBaseProduct(model, result, reactantNode); + } else { + throw new ReactionParserException("Unknown element of celldesigner:baseProducts: " + node.getNodeName(), + result); + } + } + } + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:line")) { + line = getLineProperties(nodeReaction); + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { + try { + connectScheme = parseConnectScheme(nodeReaction); + } catch (InvalidXmlSchemaException e) { + throw new ReactionParserException(result, e); + } + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfReactantLinks")) { + reactantsLinkNode = nodeReaction; + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfProductLinks")) { + productsLinkNode = nodeReaction; + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfModification")) { + modifiersLinkNode = nodeReaction; + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { + points = parseEditPoints(nodeReaction); + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:name")) { + result.setName(getNodeValue(nodeReaction)); + } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfGateMember")) { + gateMembers = nodeReaction; + } else { + throw new ReactionParserException( + "Unknown element of reaction/celldesigner:extension: " + nodeReaction.getNodeName(), result); + } + } + } + if (gateMembers != null) { + try { + points = gateMembersToPoints(gateMembers); + } catch (InvalidXmlSchemaException e) { + throw new ReactionParserException(result, e); + } + } + result = createProperTypeReaction(type, result); + if (connectScheme == null) { + throw new ReactionParserException("No connectScheme node", result); + } + if (points == null) { + points = new EditPoints(); + } + if (result instanceof SimpleReactionInterface) { + createLinesForSimpleReaction(result, points, connectScheme); + } else if (result instanceof TwoReactantReactionInterface) { + createLinesForTwoReactantReaction(result, points, gateMembers != null); + createOperatorsForTwoReactantReaction(result, gateMembers); + + } else if (result instanceof TwoProductReactionInterface) { + createLinesForTwoProductReaction(result, points); + createOperatorsForTwoProductReaction(result); + } else { + throw new ReactionParserException( + "Problem with parsing lines. Unknown reaction: " + type + "; " + result.getClass().getName(), result); + } + if (reactantsLinkNode != null) { + parseReactantLinks(result, reactantsLinkNode, model); + } + if (productsLinkNode != null) { + parseProductLinks(result, productsLinkNode, model); + } + + // create operators + createOperators(result); + + // now try to create modifiers (we must have set fixed layout data for the + // core of the reaction) + if (modifiersLinkNode != null) { + parseReactionModification(result, modifiersLinkNode, model); + } + for (int i = 0; i < result.getModifiers().size(); i++) { + Modifier modifier = result.getModifiers().get(i); + if (modifier.getElement() == null) { + List<Modifier> modifiers = new ArrayList<Modifier>(); + modifiers.add(modifier); + for (Modifier modifier2 : result.getModifiers()) { + if (modifierParentOperator.get(modifier2) == modifier) { + modifiers.add(modifier2); + } + } + computeLineForModification(result, modifiers); + createOperatorFromModifiers(result, modifiers); + result.removeModifier(modifier); + i--; + } else if (modifier.getLine() == null) { + createLineForModifier(result, modifier); + } + } + + if (line != null) { + for (AbstractNode rNode : result.getNodes()) { + rNode.getLine().setWidth(line.getWidth()); + rNode.getLine().setColor(line.getColor()); + } + } + if (result.isReversible()) { + for (Reactant reactant : result.getReactants()) { + reactant.getLine().getBeginAtd().setArrowType(result.getProducts().get(0).getLine().getEndAtd().getArrowType()); + } + } + + return result; + } + + /** + * Creates reaction with a new type. + * + * @param type + * CellDesigner type of a reaction + * @param result + * initial data + * @return reaction with a new type + * @throws UnknownReactionClassException + * thrown when type is invalid + */ + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + ReactionLineData rdl = ReactionLineData.getByCellDesignerString(type); + if (rdl == null) { + throw new UnknownReactionClassException("Unknown CellDesigner class type: " + type + ".", type, + result.getIdReaction()); + } + return rdl.createReaction(result); + } + + /** + * Creates CellDesigner {@link EditPoints} structure for gate members node. + * + * @param gateMembers + * xml node + * @return {@link EditPoints} structure representing line information for the + * xml node + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private EditPoints gateMembersToPoints(Node gateMembers) throws InvalidXmlSchemaException { + Node lastMember = null; + EditPoints result = new EditPoints(); + Integer num0 = null; + Integer num1 = null; + Integer num2 = null; + for (int i = 0; i < gateMembers.getChildNodes().getLength(); i++) { + Node child = gateMembers.getChildNodes().item(i); + if (child.getNodeType() == Node.ELEMENT_NODE) { + + if (child.getNodeName().equalsIgnoreCase("celldesigner:GateMember")) { + String type = getNodeAttr("type", child); + if (type.startsWith(ReactionLineData.BOOLEAN_LOGIC_GATE.getCellDesignerString())) { + lastMember = child; + } else { + String pointsString = getNodeAttr("editPoints", child); + List<Point2D> points = parseEditPointsString(pointsString); + if (num0 == null) { + num0 = points.size(); + } else if (num1 == null) { + num1 = points.size(); + } else { + throw new InvalidXmlSchemaException("Too many gate members"); + } + result.getPoints().addAll(points); + } + } else { + throw new InvalidXmlSchemaException("Unknown node type: " + child.getNodeName()); + } + } + } + if (lastMember == null) { + throw new InvalidXmlSchemaException("Missing gate member connecting members"); + } else { + String pointsString = getNodeAttr("editPoints", lastMember); + List<Point2D> points = parseEditPointsString(pointsString); + num2 = points.size() - 1; + result.getPoints().addAll(points); + } + result.setNum0(num0); + result.setNum1(num1); + result.setNum2(num2); + return result; + } + + /** + * Creates line information for the modifier. + * + * @param reaction + * reaction where modifier is placed + * @param modifier + * modifier to update + */ + private void createLineForModifier(Reaction reaction, Modifier modifier) { + Element element = modifier.getElement(); + CellDesignerAliasConverter converter = new CellDesignerAliasConverter(element, sbgn); + Point2D startPoint = converter.getPointCoordinates(element, anchorsByNodes.get(modifier)); + ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); + + Point2D p = modifierTypeUtils.getAnchorPointOnReactionRect(modifier.getReaction(), + lineTypeByModifier.get(modifier)); + PolylineData line = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, p, + pointsByModifier.get(modifier)); + + startPoint = converter.getAnchorPointCoordinates(element, anchorsByNodes.get(modifier), line); + line.setStartPoint(startPoint); + modifier.setLine(line); + modifierTypeUtils.updateLineEndPoint(modifier); + } + + /** + * Creates operators for CellDesigner reaction that belongs to + * {@link TwoProductReactionInterface}. + * + * @param result + * reaction to be updated + * @throws ReactionParserException + * thrown when there is a problem with creating operators (input data + * is invalid) + */ + private void createOperatorsForTwoProductReaction(Reaction result) throws ReactionParserException { + int inputs = 0; + NodeOperator operator = null; + if (result.getClass() == DissociationReaction.class) { + operator = new DissociationOperator(); + } else if (result.getClass() == TruncationReaction.class) { + operator = new TruncationOperator(); + } else { + throw new ReactionParserException("Invalid reaction type", result); + } + for (AbstractNode node : result.getNodes()) { + if (node.getClass() == Product.class) { + operator.addOutput(node); + } else if (node.getClass() == Reactant.class) { + inputs++; + if (inputs > 1) { + throw new ReactionParserException("Reaction has more than one reactant", result); + } + } + } + if (operator.getOutputs().size() > 2) { + throw new ReactionParserException("Too many products: " + operator.getOutputs().size(), result); + } + + // and now we have to modify lines + + Reactant reactant = result.getReactants().get(0); + Integer index = indexByComplexReaction.get(reactant); + + Point2D p1, p2; + + p1 = reactant.getLine().getPoints().get(index); + p2 = reactant.getLine().getPoints().get(index + 1); + + p1 = new Point2D.Double(p1.getX(), p1.getY()); + p2 = new Point2D.Double(p2.getX(), p2.getY()); + + Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + operator.setLine(reactant.getLine().getSubline(0, index + 1)); + operator.getLine().addPoint(p); + + p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + + reactant.setLine(reactant.getLine().getSubline(index + 1, reactant.getLine().getPoints().size()).reverse()); + reactant.getLine().getPoints().add(p); + + reactant.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + operator.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + + result.addNode(operator); + } + + /** + * Creates general input/output operators for the model. + * + * @param result + * reaction to be updated + */ + private void createOperators(Reaction result) { + // central line points + Point2D p1 = result.getReactants().get(0).getLine().getPoints() + .get(result.getReactants().get(0).getLine().getPoints().size() - 2); + Point2D p2 = result.getProducts().get(0).getLine().getPoints().get(1); + Point2D tmp = result.getProducts().get(0).getLine().getPoints().get(0); + Point2D productSplitOperatorBeginPoint = new Point2D.Double(tmp.getX(), tmp.getY()); + + // where the line from reactants ends + tmp = result.getReactants().get(0).getLine().getPoints() + .get(result.getReactants().get(0).getLine().getPoints().size() - 1); + Point2D reactantAndOperatorEndPoint = new Point2D.Double(tmp.getX(), tmp.getY()); + + Set<AbstractNode> toExclude = new HashSet<AbstractNode>(); + Set<AbstractNode> toInclude = new HashSet<AbstractNode>(); + for (NodeOperator operator : result.getOperators()) { + toExclude.addAll(operator.getInputs()); + if (operator.isReactantOperator()) { + toInclude.add(operator); + // if we have operator in input then central line changes + p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); + tmp = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 1); + reactantAndOperatorEndPoint = new Point2D.Double(tmp.getX(), tmp.getY()); + } + if (operator.isProductOperator()) { + // if we have operator in output then central line changes + p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); + tmp = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 1); + productSplitOperatorBeginPoint = new Point2D.Double(tmp.getX(), tmp.getY()); + } + } + + double dx = p2.getX() - p1.getX(); + double dy = p2.getY() - p1.getY(); + + Point2D reactantAndOperatorBeginPoint = new Point2D.Double(p1.getX() + dx * REACTANT_END_RATIO, + p1.getY() + dy * REACTANT_END_RATIO); + + PolylineData ld = new PolylineData(reactantAndOperatorBeginPoint, reactantAndOperatorEndPoint); + + LineType lineType = null; + Set<AbstractNode> nodes = new LinkedHashSet<AbstractNode>(); + for (Reactant reactant : result.getReactants()) { + if (!toExclude.contains(reactant)) { + nodes.add(reactant); + if (lineType == null) { + lineType = reactant.getLine().getType(); + } + } + } + nodes.addAll(toInclude); + nodes.removeAll(toExclude); + + // add an operator only when the number of input nodes is greater than one + if (nodes.size() > 1) { + AndOperator inputOperator = new AndOperator(); + inputOperator.setLine(ld); + inputOperator.addInputs(nodes); + for (Reactant reactant : result.getReactants()) { + if (!toExclude.contains(reactant)) { + reactant.getLine().getEndPoint().setLocation(reactantAndOperatorBeginPoint.getX(), + reactantAndOperatorBeginPoint.getY()); + } + } + if (lineType != null) { + inputOperator.getLine().setType(lineType); + } + result.addNode(inputOperator); + } + + // and now we try to handle with output operators + + toExclude = new HashSet<AbstractNode>(); + toInclude = new HashSet<AbstractNode>(); + for (NodeOperator nOperator : result.getOperators()) { + toExclude.addAll(nOperator.getOutputs()); + if (nOperator.isProductOperator()) { + toInclude.add(nOperator); + } + } + + Point2D productSplitOperatorEndPoint = new Point2D.Double(p1.getX() + dx * PRODUCT_START_RATIO, + p1.getY() + dy * PRODUCT_START_RATIO); + + ld = new PolylineData(productSplitOperatorEndPoint, productSplitOperatorBeginPoint); + + lineType = null; + nodes = new LinkedHashSet<AbstractNode>(); + for (Product product : result.getProducts()) { + if (!toExclude.contains(product)) { + nodes.add(product); + if (lineType == null) { + lineType = product.getLine().getType(); + } + } + } + nodes.addAll(toInclude); + nodes.removeAll(toExclude); + if (nodes.size() > 1) { + SplitOperator outputOperator = new SplitOperator(); + outputOperator.setLine(ld); + outputOperator.addOutputs(nodes); + for (Product product : result.getProducts()) { + if (!toExclude.contains(product)) { + // outputOperator.addOutput(product); + product.getLine().getPoints().get(0).setLocation(productSplitOperatorEndPoint.getX(), + productSplitOperatorEndPoint.getY()); + } + } + if (lineType != null) { + outputOperator.getLine().setType(lineType); + } + result.addNode(outputOperator); + } + } + + /** + * Creates operators in modifiers. + * + * @param reaction + * reaction to update + * @param modifiers + * list of modifiers that must be put into operators + */ + private void createOperatorFromModifiers(Reaction reaction, List<Modifier> modifiers) { + OperatorTypeUtils otu = new OperatorTypeUtils(); + String type = typeByModifier.get(modifiers.get(0)); + NodeOperator operator = otu.createModifierForStringType(type); + + operator.setLine(modifiers.get(0).getLine()); + + for (int i = 1; i < modifiers.size(); i++) { + operator.addInput(modifiers.get(i)); + } + + ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); + modifierTypeUtils.updateLineEndPoint(operator); + reaction.addNode(operator); + } + + /** + * Creates operators for CellDesigner reaction that belongs to + * {@link TwoReactantReactionInterface}. + * + * @param result + * reaction to be updated + * @param gateMembers + * node representing line information for the operator + * @throws ReactionParserException + * thrown when the xml is invalid + */ + private void createOperatorsForTwoReactantReaction(Reaction result, Node gateMembers) throws ReactionParserException { + NodeOperator andOperator = null; + if (result instanceof HeterodimerAssociationReaction) { + andOperator = new AssociationOperator(); + } else if (result instanceof BooleanLogicGateReaction) { + andOperator = new AndOperator(); + } else { + throw new ReactionParserException("Unknown class type with two reactants", result); + } + int outputs = 0; + for (AbstractNode node : result.getNodes()) { + if (node.getClass() == Reactant.class) { + andOperator.addInput(node); + } else if (node.getClass() == Product.class) { + outputs++; + if (outputs > 1) { + throw new ReactionParserException("Reaction has more than one product", result); + } + } + } + if (andOperator.getInputs().size() > 2) { + throw new ReactionParserException("Too many reactants.", result); + } + + // and now we have to modify lines + + Product product = result.getProducts().get(0); + Integer index = indexByComplexReaction.get(product); + if (index != null) { + Point2D p1, p2; + + p1 = product.getLine().getPoints().get(index); + p2 = product.getLine().getPoints().get(index + 1); + + p1 = new Point2D.Double(p1.getX(), p1.getY()); + p2 = new Point2D.Double(p2.getX(), p2.getY()); + + Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + andOperator.setLine(product.getLine().getSubline(0, index + 1)); + andOperator.getLine().addPoint(p); + andOperator.getLine().trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + + p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + + product.setLine(product.getLine().getSubline(index, product.getLine().getPoints().size())); + product.getLine().getPoints().set(0, p); + product.getLine().trimBegin(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + + product.getLine().getEndAtd().setArrowType(ArrowType.FULL); + + result.addNode(andOperator); + } else { + NodeOperator operator = null; + Set<String> undefinedTypes = new HashSet<String>(); + for (int i = 0; i < gateMembers.getChildNodes().getLength(); i++) { + Node child = gateMembers.getChildNodes().item(i); + if (child.getNodeType() == Node.ELEMENT_NODE) { + + if (child.getNodeName().equalsIgnoreCase("celldesigner:GateMember")) { + String type = getNodeAttr("type", child); + + if (type.equalsIgnoreCase(OperatorType.AND_OPERATOR_STRING.getStringName())) { + operator = new AndOperator(); + } else if (type.equalsIgnoreCase(OperatorType.OR_OPERATOR_STRING.getStringName())) { + operator = new OrOperator(); + } else if (type.equalsIgnoreCase(OperatorType.NAND_OPERATOR_STRING.getStringName())) { + operator = new NandOperator(); + } else if (type.equalsIgnoreCase(OperatorType.UNKNOWN_OPERATOR_STRING.getStringName())) { + operator = new UnknownOperator(); + } else { + undefinedTypes.add(type); + } + } + } + } + if (operator == null) { + String types = ""; + for (String string : undefinedTypes) { + types += string + ", "; + } + throw new ReactionParserException( + "Couldn't find type of BOOLEAN_LOGIC_GATE. Unknown types identified: " + types, result); + } + + operator.addInputs(andOperator.getInputs()); + operator.addOutput(product); + + // empty line + PolylineData line = new PolylineData(); + line.addPoint(product.getLine().getBeginPoint()); + line.addPoint(product.getLine().getBeginPoint()); + + operator.setLine(line); + result.addNode(operator); + } + + } + + /** + * Creates lines for reaction that belongs to + * {@link TwoProductReactionInterface}. + * + * @param reaction + * reaction to update + * @param points + * information about points + */ + private void createLinesForTwoProductReaction(Reaction reaction, EditPoints points) { + Point2D p = points.getPoints().get(points.size() - 1); + Product product1 = reaction.getProducts().get(0); + Product product2 = reaction.getProducts().get(1); + Reactant reactant = reaction.getReactants().get(0); + + CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(reactant.getElement(), sbgn); + CellDesignerAliasConverter product1Converter = new CellDesignerAliasConverter(product1.getElement(), sbgn); + CellDesignerAliasConverter product2Converter = new CellDesignerAliasConverter(product2.getElement(), sbgn); + + Point2D p1 = reactantConverter.getPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant)); + Point2D p2 = product1Converter.getPointCoordinates(product1.getElement(), anchorsByNodes.get(product1)); + Point2D p3 = product2Converter.getPointCoordinates(product2.getElement(), anchorsByNodes.get(product2)); + + Point2D centerPoint = pointTransformation.getCoordinatesInNormalBase(product1.getElement().getCenter(), + product2.getElement().getCenter(), reactant.getElement().getCenter(), p); + + int startId0 = 0; + int num0 = points.getNum0(); + int startId1 = num0; + int num1 = num0 + points.getNum1(); + int startId2 = num1; + int num2 = num1 + points.getNum2(); + + ArrayList<Point2D> linePoints1 = new ArrayList<Point2D>(points.getPoints().subList(startId0, num0)); + ArrayList<Point2D> linePoints2 = new ArrayList<Point2D>(points.getPoints().subList(startId1, num1)); + ArrayList<Point2D> linePoints3 = new ArrayList<Point2D>(points.getPoints().subList(startId2, num2)); + + PolylineData product1Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p2, linePoints2); + PolylineData product2Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p3, linePoints3); + + PolylineData reactantLine = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p1, linePoints1); + + p1 = product2Converter.getAnchorPointCoordinates(product2.getElement(), anchorsByNodes.get(product2), + product2Line.reverse()); + product2Line.setEndPoint(p1); + + p1 = product1Converter.getAnchorPointCoordinates(product1.getElement(), anchorsByNodes.get(product1), + product1Line.reverse()); + product1Line.setEndPoint(p1); + + p1 = reactantConverter.getAnchorPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant), + reactantLine.reverse()); + reactantLine.setEndPoint(p1); + + product2Line.getEndAtd().setArrowType(ArrowType.FULL); + product1Line.getEndAtd().setArrowType(ArrowType.FULL); + product1.setLine(product1Line); + product2.setLine(product2Line); + reactant.setLine(reactantLine); + + indexByComplexReaction.put(reactant, points.getIndex()); + + } + + /** + * Creates lines for reaction that belongs to + * {@link TwoReactantReactionInterface}. + * + * @param reaction + * reaction to update + * @param points + * information about points + * @param hasGateMemebers + * does the reaction has gate members + * @throws ReactionParserException + * thrown when data for reaction is invalid + */ + private void createLinesForTwoReactantReaction(Reaction reaction, EditPoints points, boolean hasGateMemebers) + throws ReactionParserException { + Point2D p = points.getPoints().get(points.size() - 1); + Product product = reaction.getProducts().get(0); + Reactant reactant1 = reaction.getReactants().get(0); + Reactant reactant2 = reaction.getReactants().get(1); + CellDesignerAliasConverter productConverter = new CellDesignerAliasConverter(product.getElement(), sbgn); + CellDesignerAliasConverter reactantConverter1 = new CellDesignerAliasConverter(reactant1.getElement(), sbgn); + CellDesignerAliasConverter reactantConverter2 = new CellDesignerAliasConverter(reactant2.getElement(), sbgn); + Point2D p1 = reactantConverter1.getPointCoordinates(reactant1.getElement(), anchorsByNodes.get(reactant1)); + Point2D p2 = reactantConverter2.getPointCoordinates(reactant2.getElement(), anchorsByNodes.get(reactant2)); + Point2D p3 = productConverter.getPointCoordinates(product.getElement(), anchorsByNodes.get(product)); + + Element alias1 = reactant1.getElement(); + Element alias2 = reactant2.getElement(); + Element alias3 = product.getElement(); + + Point2D pointA = alias2.getCenter(); + Point2D pointC = alias1.getCenter(); + Point2D pointB = alias3.getCenter(); + Point2D pointP = p; + Point2D centerPoint = null; + if (!hasGateMemebers) { + centerPoint = pointTransformation.getCoordinatesInNormalBase(pointA, pointB, pointC, pointP); + } else { + centerPoint = pointP; + } + + int startId0 = 0; + int num0 = points.getNum0(); + int startId1 = num0; + int num1 = num0 + points.getNum1(); + int startId2 = num1; + int num2 = num1 + points.getNum2(); + + List<Point2D> linePoints1 = new ArrayList<Point2D>(points.getPoints().subList(startId0, num0)); + List<Point2D> linePoints2 = new ArrayList<Point2D>(points.getPoints().subList(startId1, num1)); + List<Point2D> linePoints3 = new ArrayList<Point2D>(points.getPoints().subList(startId2, num2)); + + PolylineData reactant1Line = null; + PolylineData reactant2Line = null; + if (!hasGateMemebers) { + reactant1Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p1, linePoints1); + reactant2Line = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p2, linePoints2); + } else { + reactant1Line = PolylineDataFactory.createPolylineDataFromEditPoints(p1, centerPoint, linePoints1); + reactant1Line = reactant1Line.reverse(); + + reactant2Line = PolylineDataFactory.createPolylineDataFromEditPoints(p2, centerPoint, linePoints2); + reactant2Line = reactant2Line.reverse(); + } + + PolylineData productLine = PolylineDataFactory.createPolylineDataFromEditPoints(centerPoint, p3, linePoints3); + + p1 = reactantConverter1.getAnchorPointCoordinates(reactant1.getElement(), anchorsByNodes.get(reactant1), + reactant1Line.reverse()); + reactant1Line.setEndPoint(p1); + reactant1Line = reactant1Line.reverse(); + + p1 = reactantConverter2.getAnchorPointCoordinates(reactant2.getElement(), anchorsByNodes.get(reactant2), + reactant2Line.reverse()); + reactant2Line.setEndPoint(p1); + reactant2Line = reactant2Line.reverse(); + + p1 = productConverter.getAnchorPointCoordinates(product.getElement(), anchorsByNodes.get(product), + productLine.reverse()); + productLine.setEndPoint(p1); + if (hasGateMemebers) { + productLine.getEndAtd().setArrowType(ArrowType.OPEN); + } + + product.setLine(productLine); + reactant1.setLine(reactant1Line); + reactant2.setLine(reactant2Line); + + indexByComplexReaction.put(product, points.getIndex()); + } + + /** + * Creates lines for reaction that belongs to {@link SimpleReactionInterface} + * (only one standard product and one standard reactant). + * + * @param reaction + * reaction to update + * @param editPoints + * information about points + * @param scheme + * some additional information about the line + */ + private void createLinesForSimpleReaction(Reaction reaction, EditPoints editPoints, ConnectScheme scheme) { + Product product = reaction.getProducts().get(0); + Reactant reactant = reaction.getReactants().get(0); + CellDesignerAliasConverter productConverter = new CellDesignerAliasConverter(product.getElement(), sbgn); + CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(reactant.getElement(), sbgn); + + Point2D endPoint = productConverter.getPointCoordinates(product.getElement(), anchorsByNodes.get(product)); + Point2D startPoint = reactantConverter.getPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant)); + + PolylineData ld = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, endPoint, + editPoints.getPoints()); + + // first place where the index of rectangle is kept + Integer index = editPoints.getIndex(); + + // second place where index of rectangle is kept + if (index == null) { + index = scheme.getConnectIndex(); + if (index != null) { + // but... + // direction of the index is reversed... + index = ld.getPoints().size() - index - 2; + } + } + // but sometimes there is no information about index... + if (index == null) { + index = 0; + } + startPoint = reactantConverter.getAnchorPointCoordinates(reactant.getElement(), anchorsByNodes.get(reactant), ld); + endPoint = productConverter.getAnchorPointCoordinates(product.getElement(), anchorsByNodes.get(product), + ld.reverse()); + ld.getPoints().set(0, startPoint); + ld.getPoints().set(ld.getPoints().size() - 1, endPoint); + + PolylineData line = new PolylineData(ld); + + PolylineData reactantLine = line.getSubline(0, ld.getPoints().size() - index - 1); + PolylineData productLine = line.getSubline(ld.getPoints().size() - index - 2, ld.getPoints().size()); + + Point2D p1 = ld.getPoints().get(ld.getPoints().size() - index - 2); + Point2D p2 = ld.getPoints().get(ld.getPoints().size() - index - 1); + Point2D p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + reactantLine.getPoints().add(p); + reactantLine.trimEnd(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + + p = new Point2D.Double((p2.getX() + p1.getX()) / 2, (p2.getY() + p1.getY()) / 2); + productLine.getPoints().set(0, p); + productLine.trimBegin(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1); + + productLine.getEndAtd().setArrowType(ArrowType.FULL); + + reactant.setLine(reactantLine); + product.setLine(productLine); + + ReactionLineData rld = ReactionLineData.getByReactionType(reaction.getClass()); + reactantLine.setType(rld.getLineType()); + productLine.setType(rld.getLineType()); + productLine.getEndAtd().setArrowType(rld.getProductArrowType()); + productLine.trimEnd(rld.getProductLineTrim()); + } + + /** + * Prepares {@link EditPoints} structure from xml node. + * + * @param rootNode + * xml node + * @return {@link EditPoints} object containing CellDesigner line data + */ + private EditPoints parseEditPoints(Node rootNode) { + EditPoints result = new EditPoints(); + String num0 = getNodeAttr("num0", rootNode); + String num1 = getNodeAttr("num1", rootNode); + String num2 = getNodeAttr("num2", rootNode); + if (!num0.equals("")) { + result.setNum0(Integer.parseInt(num0)); + } + if (!num1.equals("")) { + result.setNum1(Integer.parseInt(num1)); + } + if (!num2.equals("")) { + result.setNum2(Integer.parseInt(num2)); + } + String index = getNodeAttr("tShapeIndex", rootNode); + if (!index.equals("")) { + result.setIndex(Integer.parseInt(index)); + } + + String pointsString = getNodeValue(rootNode); + result.setPoints(parseEditPointsString(pointsString)); + return result; + } + + /** + * Parses CellDesigner point by point string. + * + * @param pointsString + * String containing points + * @return list of points + */ + ArrayList<Point2D> parseEditPointsString(String pointsString) { + ArrayList<Point2D> points2 = new ArrayList<Point2D>(); + if (pointsString.isEmpty()) { + return points2; + } + String[] points = pointsString.trim().split(" "); + for (String string : points) { + String[] p = string.split(","); + if (p.length != 2) { + throw new InvalidArgumentException("Invalid editPoint string: " + string); + } else { + double posX = Double.parseDouble(p[0]); + double posY = Double.parseDouble(p[1]); + Point2D point = new Point2D.Double(posX, posY); + if (!pointTransformation.isValidPoint(point)) { + throw new InvalidArgumentException( + "Invalid point parsed from input string: " + string + ". Result point: " + point); + } + points2.add(point); + } + } + return points2; + } + + /** + * Parses xml node with reactantLink nodes and creates reactants in the + * reaction. + * + * @param result + * reaction to be updated + * @param rootNode + * xml node to parse + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when the xml is invalid + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private void parseReactantLinks(Reaction result, Node rootNode, Model model) throws ReactionParserException { + NodeList list = rootNode.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:reactantLink")) { + Reactant newReactant = getReactantLink(node, model, result); + result.addReactant(newReactant); + } else { + throw new ReactionParserException( + "Unknown element of celldesigner:listOfReactantLinks: " + node.getNodeName(), result); + } + } + } + } + + /** + * Parses xml node for reactanLink and creates reactant from it. + * + * @param rootNode + * xml node to parse + * @param model + * model where reaction is placed + * @param reaction + * reaction that is being parsed + * @return reactant obtained from xml node + * @throws ReactionParserException + * thrown when the xml is invalid and reason is more specific + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private Reactant getReactantLink(Node rootNode, Model model, Reaction reaction) throws ReactionParserException { + String aliasId = getNodeAttr("alias", rootNode); + Species al = (Species) model.getElementByElementId(aliasId); + if (al == null) { + throw new ReactionParserException("Alias doesn't exist (id: " + aliasId + ")", reaction); + } + + Reactant result = new Reactant(al); + CellDesignerAnchor anchor = null; + EditPoints points = null; + + NodeList nodes = rootNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { + anchor = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { + points = parseEditPoints(node); + } else { + throw new ReactionParserException("Unknown element of celldesigner:reactantLink: " + node.getNodeName(), + reaction); + } + } + } + + CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(al, sbgn); + Point2D additionalPoint = reactantConverter.getPointCoordinates(al, anchor); + ArrowTypeData atd = new ArrowTypeData(); + atd.setArrowType(ArrowType.NONE); + + Point2D endPoint; + + Line2D centerLine = reaction.getCenterLine(); + + double dx = centerLine.getX2() - centerLine.getX1(); + double dy = centerLine.getY2() - centerLine.getY1(); + double coef = REACTANT_END_RATIO; + endPoint = new Point2D.Double(centerLine.getX1() + dx * coef, centerLine.getY1() + dy * coef); + + PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(additionalPoint, endPoint, points); + + additionalPoint = reactantConverter.getAnchorPointCoordinates(al, anchor, polyline); + polyline.setStartPoint(additionalPoint); + result.setLine(polyline); + + return result; + } + + /** + * Parses xml node with productLink nodes and creates products in the reaction. + * + * @param result + * reaction to be updated + * @param rootNode + * xml node to parse + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when the xml is invalid + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private void parseProductLinks(Reaction result, Node rootNode, Model model) throws ReactionParserException { + NodeList list = rootNode.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:productLink")) { + Product link = getProductLink(node, model, result); + result.addProduct(link); + } else { + throw new ReactionParserException("Unknown element of celldesigner:listOfProductLinks: " + node.getNodeName(), + result); + } + } + } + } + + /** + * Parses xml node for productLink and creates product from it. + * + * @param rootNode + * xml node to parse + * @param model + * model where reaction is placed + * @param reaction + * reaction that is being parsed + * @return product obtained from xml node + * @throws ReactionParserException + * thrown when the xml is invalid + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private Product getProductLink(Node rootNode, Model model, Reaction reaction) throws ReactionParserException { + String aliasId = getNodeAttr("alias", rootNode); + Species al = (Species) model.getElementByElementId(aliasId); + if (al == null) { + throw new ReactionParserException("Alias doesn't exist (id: " + aliasId + ")", reaction); + } + + Product result = new Product(al); + + CellDesignerAnchor anchor = null; + EditPoints points = null; + + NodeList nodes = rootNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { + anchor = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) { + points = parseEditPoints(node); + } else { + throw new ReactionParserException("Unknown element of celldesigner:reactantLink: " + node.getNodeName(), + reaction); + } + } + } + + CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(al, sbgn); + Point2D additionalPoint = reactantConverter.getPointCoordinates(al, anchor); + ArrowTypeData atd = new ArrowTypeData(); + atd.setArrowType(ArrowType.NONE); + + Point2D endPoint; + + // central line points + Point2D p1 = reaction.getReactants().get(0).getLine().getPoints() + .get(reaction.getReactants().get(0).getLine().getPoints().size() - 2); + Point2D p2 = reaction.getProducts().get(0).getLine().getPoints().get(1); + + Set<AbstractNode> toExclude = new HashSet<AbstractNode>(); + Set<AbstractNode> toInclude = new HashSet<AbstractNode>(); + for (NodeOperator operator : reaction.getOperators()) { + toExclude.addAll(operator.getInputs()); + if (operator.isReactantOperator()) { + toInclude.add(operator); + // if we have operator in input then central line changes + p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); + } + if (operator.isProductOperator()) { + // if we have operator in output then central line changes + p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2); + } + } + + double dx = p2.getX() - p1.getX(); + double dy = p2.getY() - p1.getY(); + double coef = PRODUCT_START_RATIO; + endPoint = new Point2D.Double(p1.getX() + dx * coef, p1.getY() + dy * coef); + + PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(endPoint, additionalPoint, points); + additionalPoint = reactantConverter.getAnchorPointCoordinates(al, anchor, polyline.reverse()); + polyline.setEndPoint(additionalPoint); + + polyline.getEndAtd().setArrowType(ArrowType.FULL); + result.setLine(polyline); + return result; + } + + /** + * Creates {@link LineProperties} object from the node. + * + * @param node + * xml node to parse + * @return {@link LineProperties} object + */ + private LineProperties getLineProperties(Node node) { + LineProperties line = new LineProperties(); + String tmp = getNodeAttr("width", node); + line.setWidth(Double.parseDouble(tmp)); + tmp = getNodeAttr("color", node); + line.setColor(stringToColor(tmp)); + line.setType(getNodeAttr("type", node)); + return line; + } + + /** + * PArse reaction modifiactions and add them into reaction. + * + * @param result + * reaction currently processed + * @param rootNode + * xml node to parse + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when the xml is invalid, and reason is more specific + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private void parseReactionModification(Reaction result, Node rootNode, Model model) throws ReactionParserException { + NodeList nodes = rootNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:modification")) { + parseModificationReaction(result, node, model); + } else { + throw new ReactionParserException("Unknown element of celldesigner:listOfModification: " + node.getNodeName(), + result); + } + } + } + } + + /** + * PArses modification node and adds this modification to reaction. + * + * @param reaction + * reaction currently processed + * @param rootNode + * xml node + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when node cannot be parsed properly + */ + private void parseModificationReaction(Reaction reaction, Node rootNode, Model model) throws ReactionParserException { + ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); + String type = getNodeAttr("type", rootNode); + String modificationType = getNodeAttr("modificationType", rootNode); + String aliasId = getNodeAttr("aliases", rootNode); + String lineConnectionType = getNodeAttr("targetLineIndex", rootNode); + String points = getNodeAttr("editPoints", rootNode); + + List<Species> aliasList = new ArrayList<Species>(); + + String[] list = aliasId.split(","); + for (String string : list) { + Species al = (Species) model.getElementByElementId(string); + if (al != null) { + aliasList.add(al); + } else { + throw new ReactionParserException("Unknown alias: " + string, reaction); + } + } + Modifier result = null; + if (aliasList.size() > 1) { + result = new Modifier(null); + reaction.addModifier(result); + for (int i = 0; i < aliasList.size(); i++) { + Modifier mod = modifierTypeUtils.createModifierForStringType(modificationType, aliasList.get(i)); + modifierParentOperator.put(mod, result); + reaction.addModifier(mod); + } + } else { + ModifierType mType = modifierTypeUtils.getModifierTypeForStringType(type); + if (mType == null) { + String errorInfo = "[" + reaction.getClass().getSimpleName() + "\t" + reaction.getIdReaction() + + "]\tUnknown modifier type: " + type; + if (ReactionLineData.getByCellDesignerString(type) != null) { + errorInfo += ".\tThis type can be applied to reaction type only, not modifier."; + } + throw new UnknownModifierClassException(errorInfo, type, reaction.getIdReaction()); + } + for (Modifier modifier : reaction.getModifiers()) { + if (modifier.getElement() == aliasList.get(0) && modifier.getClass() == mType.getClazz()) { + result = modifier; + } + } + if (result == null) { + result = modifierTypeUtils.createModifierForStringType(type, aliasList.get(0)); + reaction.addModifier(result); + } + } + pointsByModifier.put(result, parseEditPointsString(points)); + typeByModifier.put(result, type); + lineTypeByModifier.put(result, lineConnectionType); + + NodeList nodes = rootNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:line")) { + continue; + } else if (node.getNodeName().equalsIgnoreCase("celldesigner:linkTarget")) { + try { + CellDesignerAnchor anchor = getAnchorFromLinkTarget(node); + anchorsByNodes.put(result, anchor); + } catch (InvalidXmlSchemaException e) { + throw new ReactionParserException(reaction, e); + } + } else { + throw new ReactionParserException("Unknown element of celldesigner:listOfModification: " + node.getNodeName(), + reaction); + } + } + + } + + } + + /** + * Creates lines for modifiers. + * + * @param reaction + * reaction where modifiers are placed + * @param modifiers + * list of modifiers for which lines should be generated + */ + private void computeLineForModification(Reaction reaction, List<Modifier> modifiers) { + ModifierTypeUtils modifierTypeUtils = new ModifierTypeUtils(); + Point2D p = modifierTypeUtils.getAnchorPointOnReactionRect(reaction, lineTypeByModifier.get(modifiers.get(0))); + + Point2D startPoint; + List<Point2D> subPoints; + // in case we have more then one alias in modification then we need to + // define the start point as a different one then one on the alias + Modifier modifier = modifiers.get(0); + List<Point2D> points = pointsByModifier.get(modifier); + startPoint = points.get(points.size() - 1); + subPoints = points.subList(0, points.size() - 1); + PolylineData line = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, p, subPoints); + modifier.setLine(line); + + Point2D endPoint = startPoint; + + for (int i = 1; i < modifiers.size(); i++) { + Modifier param = modifiers.get(i); + CellDesignerAliasConverter reactantConverter = new CellDesignerAliasConverter(param.getElement(), sbgn); + startPoint = reactantConverter.getPointCoordinates(param.getElement(), anchorsByNodes.get(param)); + + PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(startPoint, endPoint, + pointsByModifier.get(param)); + + startPoint = reactantConverter.getAnchorPointCoordinates(param.getElement(), anchorsByNodes.get(param), polyline); + polyline.setStartPoint(startPoint); + param.setLine(polyline); + } + } + + /** + * Returns {@link CellDesignerAnchor} point from linkAnchor xml node. + * + * @param rootNode + * xml node + * @return {@link CellDesignerAnchor} object representing anchor point + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private CellDesignerAnchor getAnchorFromLinkTarget(Node rootNode) throws InvalidXmlSchemaException { + CellDesignerAnchor result = null; + + NodeList nodes = rootNode.getChildNodes(); + for (int z = 0; z < nodes.getLength(); z++) { + Node node = nodes.item(z); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { + result = CellDesignerAnchor.valueOf(getNodeAttr("position", node).toUpperCase()); + } else { + throw new InvalidXmlSchemaException("Unknown element of celldesigner:connectScheme: " + node.getNodeName()); + } + } + } + return result; + } + + /** + * Creates {@link ConnectScheme} object from xml node. + * + * @param nodeReaction + * xml node to parse + * @return {@link ConnectScheme} object for given xml + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private ConnectScheme parseConnectScheme(Node nodeReaction) throws InvalidXmlSchemaException { + ConnectScheme result = new ConnectScheme(); + result.setConnectPolicy(getNodeAttr("connectPolicy", nodeReaction)); + result.setConnectIndex(getNodeAttr("rectangleIndex", nodeReaction)); + NodeList reactantsNodes = nodeReaction.getChildNodes(); + for (int z = 0; z < reactantsNodes.getLength(); z++) { + Node reactantNode = reactantsNodes.item(z); + if (reactantNode.getNodeType() == Node.ELEMENT_NODE) { + if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:listOfLineDirection")) { + result.setLineDirections(getlineDirectionMapForReactions(reactantNode)); + } else { + throw new InvalidXmlSchemaException( + "Unknown element of celldesigner:connectScheme: " + nodeReaction.getNodeName()); + } + } + } + return result; + } + + /** + * Parse lineDirectionMap. This structure is nowhere used. + * + * @param reactantNode + * xml node + * @return map with directions for every line + * @throws InvalidXmlSchemaException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private Map<String, String> getlineDirectionMapForReactions(Node reactantNode) throws InvalidXmlSchemaException { + Map<String, String> result = new HashMap<String, String>(); + NodeList nodes = reactantNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:lineDirection")) { + String index = getNodeAttr("index", node); + String value = getNodeAttr("value", node); + result.put(index, value); + } else { + throw new InvalidXmlSchemaException( + "Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName()); + } + } + } + return result; + } + + /** + * PArses baseReactant node from CellDEsigenr xml node and adds it into + * reaction. + * + * @param result + * reaction that is processed + * @param reactantNode + * xml node to parse + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private void parseBaseReactant(Reaction result, Node reactantNode, Model model) throws ReactionParserException { + String aliasId = getNodeAttr("alias", reactantNode); + Species alias = model.getElementByElementId(aliasId); + if (alias == null) { + throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result); + } + Reactant reactant = new Reactant(alias); + result.addReactant(reactant); + NodeList nodes = reactantNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { + anchorsByNodes.put(reactant, CellDesignerAnchor.valueOf(getNodeAttr("position", node))); + } else { + throw new ReactionParserException( + "Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName(), result); + } + } + + } + } + + /** + * Parses baseProduct node from CellDEsigenr xml node and adds it into reaction. + * + * @param result + * reaction that is processed + * @param reactantNode + * xml node to parse + * @param model + * model where reaction is placed + * @throws ReactionParserException + * thrown when xml node contains data that is not supported by xml + * schema + */ + private void parseBaseProduct(Model model, Reaction result, Node reactantNode) throws ReactionParserException { + String aliasId = getNodeAttr("alias", reactantNode); + Species alias = (Species) model.getElementByElementId(aliasId); + if (alias == null) { + throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result); + } + Product product = new Product(alias); + result.addProduct(product); + NodeList nodes = reactantNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) { + anchorsByNodes.put(product, CellDesignerAnchor.valueOf(getNodeAttr("position", node))); + } else { + throw new ReactionParserException( + "Unknown element of reaction/celldesigner:baseProduct: " + node.getNodeName(), result); + } + } + } + + } } diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/AllReactionTests.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/AllReactionTests.java index aa13beebfc86c0d909c3128cdbb85cebc6978956..5284caf287314aba6947d1f9ee7100cc0f2b44f3 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/AllReactionTests.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/AllReactionTests.java @@ -5,14 +5,15 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({ ReactionCollectionXmlParserTest.class, // - ReactionFromXmlTest.class, // - ReactionLineDataTest.class, // - ReactionParserExceptionTest.class, // - ReactionParserTests.class, // - ReactionToXmlTest.class, // - UnknownModifierClassExceptionTest.class, // - UnknownReactionClassExceptionTest.class,// +@SuiteClasses({ KineticsXmlParserTest.class, // + ReactionCollectionXmlParserTest.class, // + ReactionFromXmlTest.class, // + ReactionLineDataTest.class, // + ReactionParserExceptionTest.class, // + ReactionParserTests.class, // + ReactionToXmlTest.class, // + UnknownModifierClassExceptionTest.class, // + UnknownReactionClassExceptionTest.class,// }) public class AllReactionTests { diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParserTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParserTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e8e388baec1591aa21b17827f28411e507dabc20 --- /dev/null +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/KineticsXmlParserTest.java @@ -0,0 +1,116 @@ +package lcsb.mapviewer.converter.model.celldesigner.reaction; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.w3c.dom.Node; + +import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; +import lcsb.mapviewer.converter.model.celldesigner.CellDesignerTestFunctions; +import lcsb.mapviewer.model.map.kinetics.SbmlFunction; +import lcsb.mapviewer.model.map.kinetics.SbmlKinetics; +import lcsb.mapviewer.model.map.kinetics.SbmlParameter; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.model.ModelFullIndexed; +import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.GenericProtein; + +public class KineticsXmlParserTest extends CellDesignerTestFunctions { + + @Test + public void testParserElements() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = getTestElementMap(); + Model model = new ModelFullIndexed(null); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/simple.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(2, kinetics.getArguments().size()); + } + + @Test + public void testParserParameters() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = getTestElementMap(); + Model model = new ModelFullIndexed(null); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_parameter.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(1, kinetics.getParameters().size()); + } + + @Test + public void testParserWithUsedParameters() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = getTestElementMap(); + Model model = new ModelFullIndexed(null); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_used_parameter.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(1, kinetics.getParameters().size()); + } + + @Test + public void testParserGlobalParameters() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = getTestElementMap(); + Model model = new ModelFullIndexed(null); + model.addParameter(new SbmlParameter("k1")); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_global_parameter.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(1, kinetics.getParameters().size()); + } + + @Test + public void testParserGlobalFunction() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = getTestElementMap(); + Model model = new ModelFullIndexed(null); + model.addFunction(new SbmlFunction("fun")); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_function.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(1, kinetics.getFunctions().size()); + } + + @Test(expected = InvalidXmlSchemaException.class) + public void testParserNonExistingElements() throws InvalidXmlSchemaException, IOException { + Map<String, Element> elements = new HashMap<>(); + Model model = new ModelFullIndexed(null); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/simple.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + parser.parseKinetics(node, elements); + } + + @Test + public void testParserElementDuplicate() throws InvalidXmlSchemaException, IOException { + Model model = new ModelFullIndexed(null); + Map<String, Element> elements = getTestElementMap(); + Node node = super.getXmlDocumentFromFile("testFiles/kinetics/math_using_one_element.xml").getFirstChild(); + + KineticsXmlParser parser = new KineticsXmlParser(model); + SbmlKinetics kinetics = parser.parseKinetics(node, elements); + assertNotNull(kinetics); + assertEquals(1, kinetics.getArguments().size()); + } + + private Map<String, Element> getTestElementMap() { + Map<String, Element> elements = new HashMap<>(); + elements.put("s1", new GenericProtein("s1")); + elements.put("s2", new GenericProtein("s2")); + return elements; + } + +} diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXmlTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXmlTest.java index 1f6ca70dce3f996bd9473bf623cf958920c30ddc..6b027c8e0fb97aabc608dd348bba37106e704cac 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXmlTest.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXmlTest.java @@ -23,637 +23,645 @@ import lcsb.mapviewer.model.map.species.GenericProtein; import lcsb.mapviewer.model.map.species.Species; public class ReactionFromXmlTest extends CellDesignerTestFunctions { - ReactionXmlParser parser; - Model model = new ModelFullIndexed(null); - - CellDesignerElementCollection elements; - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - elements = new CellDesignerElementCollection(); - parser = new ReactionXmlParser(elements, false); - - Species alias = new GenericProtein("sa1"); - model.addElement(alias); - - alias = new GenericProtein("sa2"); - model.addElement(alias); - - alias = new GenericProtein("sa3"); - model.addElement(alias); - - alias = new GenericProtein("sa4"); - model.addElement(alias); - - elements.addElement(new CellDesignerGenericProtein("s1")); - elements.addElement(new CellDesignerGenericProtein("s2")); - elements.addElement(new CellDesignerGenericProtein("s3")); - elements.addElement(new CellDesignerGenericProtein("s4")); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testInvalid() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("kineticLaw node doesn't have math subnode")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid2() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction2.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("invalid value in math xmlns attrib")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid3() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction3.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown element of reaction")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid4() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction4.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("No annotation node in reaction")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid5() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction5.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Problem with parsing RDF")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid6() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction6.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown element of reaction/annotation")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid7() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction7.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown element of celldesigner:baseReactants")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid8() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction8.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown element of celldesigner:baseProducts")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid9() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction9.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:connectScheme")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid10() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction10.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown element of reaction/celldesigner:extension")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid11() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction11.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("No connectScheme node")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid12() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction12.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown node type")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid13() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction13.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Too many gate members")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid14() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction14.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Missing gate member connecting members")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid15() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction15.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Couldn't find type of BOOLEAN_LOGIC_GATE")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid16() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction16.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:listOfReactantLinks")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid17() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction17.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:reactantLink")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid19() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction19.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias doesn't exist")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid20() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction20.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:listOfProductLinks")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid21() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction21.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:reactantLink")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid23() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction23.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias doesn't exist")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid24() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction24.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:listOfModification")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid26() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction26.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown alias")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid28() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction28.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:connectScheme")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid29() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction29.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of celldesigner:listOfModification")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid30() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction30.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of reaction/celldesigner:baseReactant")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid31() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction31.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias with id")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid33() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction33.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of reaction/celldesigner:baseReactant")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid34() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction34.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias with id")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalid36() throws Exception { - try { - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction36.xml")), model); - fail("Exception expected"); - } catch (ReactionParserException e) { - assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown element of reaction/celldesigner:baseProduct")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction() throws Exception { - try { - // test situation when createProperTypeReaction returns reaction of - // unknown type - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - return result; - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_transport.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Problem with parsing lines. Unknown reaction")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction2() throws Exception { - try { - // test situation when createProperTypeReaction returns reaction of - // unknown type that implements TwoProductReactionInterface - - class NewReactionType extends Reaction implements TwoProductReactionInterface { - private static final long serialVersionUID = 1L; - - public NewReactionType(Reaction r) { - super(r); - } - } - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - return new NewReactionType(result); - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Invalid reaction type")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction3() throws Exception { - try { - // test situation when createOperatorsForTwoProductReaction encounter - // reaction with two many base reactants - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - Reaction r = super.createProperTypeReaction(type, result); - r.addReactant(new Reactant()); - return r; - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Reaction has more than one reactant")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction4() throws Exception { - try { - // test situation when createOperatorsForTwoProductReaction encounter - // reaction with two many base products - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - Reaction r = super.createProperTypeReaction(type, result); - r.addProduct(new Product()); - return r; - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Too many products")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction5() throws Exception { - try { - // test situation when createProperTypeReaction returns reaction of - // unknown type that implements TwoReactantReactionInterface - - class NewReactionType extends Reaction implements TwoReactantReactionInterface { - private static final long serialVersionUID = 1L; - - public NewReactionType(Reaction r) { - super(r); - } - } - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - return new NewReactionType(result); - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Unknown class type with two reactants")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction6() throws Exception { - try { - // test situation when createOperatorsForTwoReactantReaction encounter - // reaction with two many base reactants - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - Reaction r = super.createProperTypeReaction(type, result); - r.addReactant(new Reactant()); - return r; - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Too many reactants")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testInvalidReaction7() throws Exception { - try { - // test situation when createOperatorsForTwoReactantReaction encounter - // reaction with two many base products - - ReactionFromXml parser = new ReactionFromXml(false) { - Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { - Reaction r = super.createProperTypeReaction(type, result); - r.addProduct(new Product()); - return r; - } - }; - parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), model); - fail("Exception expected"); - - } catch (ReactionParserException e) { - assertTrue(e.getMessage().contains("Reaction has more than one product")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testParseInvalidEditPointsString() throws Exception { - try { - ReactionFromXml parser = new ReactionFromXml(false); - - parser.parseEditPointsString("1"); - fail("Exception expected"); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Invalid editPoint string")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testParseInvalidEditPointsString2() throws Exception { - try { - ReactionFromXml parser = new ReactionFromXml(false); - - parser.parseEditPointsString("1,Infinity"); - fail("Exception expected"); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Invalid point parsed from input string")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } + ReactionXmlParser parser; + Model model = new ModelFullIndexed(null); + + CellDesignerElementCollection elements; + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + elements = new CellDesignerElementCollection(); + parser = new ReactionXmlParser(elements, false); + + Species alias = new GenericProtein("sa1"); + model.addElement(alias); + + alias = new GenericProtein("sa2"); + model.addElement(alias); + + alias = new GenericProtein("sa3"); + model.addElement(alias); + + alias = new GenericProtein("sa4"); + model.addElement(alias); + + elements.addElement(new CellDesignerGenericProtein("s1")); + elements.addElement(new CellDesignerGenericProtein("s2")); + elements.addElement(new CellDesignerGenericProtein("s3")); + elements.addElement(new CellDesignerGenericProtein("s4")); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInvalid() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("kineticLaw node doesn't have math subnode")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid3() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction3.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown element of reaction")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid4() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction4.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("No annotation node in reaction")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid5() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction5.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Problem with parsing RDF")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid6() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction6.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown element of reaction/annotation")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid7() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction7.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown element of celldesigner:baseReactants")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid8() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction8.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown element of celldesigner:baseProducts")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid9() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction9.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:connectScheme")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid10() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction10.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown element of reaction/celldesigner:extension")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid11() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction11.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("No connectScheme node")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid12() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction12.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown node type")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid13() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction13.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Too many gate members")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid14() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction14.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Missing gate member connecting members")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid15() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction15.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Couldn't find type of BOOLEAN_LOGIC_GATE")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid16() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction16.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:listOfReactantLinks")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid17() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction17.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:reactantLink")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid19() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction19.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias doesn't exist")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid20() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction20.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:listOfProductLinks")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid21() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction21.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:reactantLink")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid23() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction23.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias doesn't exist")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid24() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction24.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:listOfModification")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid26() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction26.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Unknown alias")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid28() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction28.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:connectScheme")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid29() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction29.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of celldesigner:listOfModification")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid30() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction30.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of reaction/celldesigner:baseReactant")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid31() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction31.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias with id")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid33() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction33.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of reaction/celldesigner:baseReactant")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid34() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction34.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), e.getMessage().contains("Alias with id")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalid36() throws Exception { + try { + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/invalid/reaction36.xml")), model); + fail("Exception expected"); + } catch (ReactionParserException e) { + assertTrue("Invalid message: " + e.getMessage(), + e.getMessage().contains("Unknown element of reaction/celldesigner:baseProduct")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction() throws Exception { + try { + // test situation when createProperTypeReaction returns reaction of + // unknown type + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + return result; + } + }; + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_transport.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Problem with parsing lines. Unknown reaction")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction2() throws Exception { + try { + // test situation when createProperTypeReaction returns reaction of + // unknown type that implements TwoProductReactionInterface + + class NewReactionType extends Reaction implements TwoProductReactionInterface { + private static final long serialVersionUID = 1L; + + public NewReactionType(Reaction r) { + super(r); + } + } + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + return new NewReactionType(result); + } + }; + parser.getReaction( + super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Invalid reaction type")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction3() throws Exception { + try { + // test situation when createOperatorsForTwoProductReaction encounter + // reaction with two many base reactants + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + Reaction r = super.createProperTypeReaction(type, result); + r.addReactant(new Reactant()); + return r; + } + }; + parser.getReaction( + super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Reaction has more than one reactant")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction4() throws Exception { + try { + // test situation when createOperatorsForTwoProductReaction encounter + // reaction with two many base products + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + Reaction r = super.createProperTypeReaction(type, result); + r.addProduct(new Product()); + return r; + } + }; + parser.getReaction( + super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Too many products")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction5() throws Exception { + try { + // test situation when createProperTypeReaction returns reaction of + // unknown type that implements TwoReactantReactionInterface + + class NewReactionType extends Reaction implements TwoReactantReactionInterface { + private static final long serialVersionUID = 1L; + + public NewReactionType(Reaction r) { + super(r); + } + } + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + return new NewReactionType(result); + } + }; + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Unknown class type with two reactants")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction6() throws Exception { + try { + // test situation when createOperatorsForTwoReactantReaction encounter + // reaction with two many base reactants + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + Reaction r = super.createProperTypeReaction(type, result); + r.addReactant(new Reactant()); + return r; + } + }; + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Too many reactants")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testInvalidReaction7() throws Exception { + try { + // test situation when createOperatorsForTwoReactantReaction encounter + // reaction with two many base products + + ReactionFromXml parser = new ReactionFromXml(false) { + Reaction createProperTypeReaction(String type, Reaction result) throws UnknownReactionClassException { + Reaction r = super.createProperTypeReaction(type, result); + r.addProduct(new Product()); + return r; + } + }; + parser.getReaction(super.getNodeFromXmlString(readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml")), + model); + fail("Exception expected"); + + } catch (ReactionParserException e) { + assertTrue(e.getMessage().contains("Reaction has more than one product")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testParseInvalidEditPointsString() throws Exception { + try { + ReactionFromXml parser = new ReactionFromXml(false); + + parser.parseEditPointsString("1"); + fail("Exception expected"); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Invalid editPoint string")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testParseInvalidEditPointsString2() throws Exception { + try { + ReactionFromXml parser = new ReactionFromXml(false); + + parser.parseEditPointsString("1,Infinity"); + fail("Exception expected"); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Invalid point parsed from input string")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } } diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserTests.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserTests.java index 021defcdef353c794fe9846034df35ca797d097c..f9f7d558536e5d673a6e6568aee71cdd940a1563 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserTests.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserTests.java @@ -59,1739 +59,1801 @@ import lcsb.mapviewer.model.map.species.Species; public class ReactionParserTests extends CellDesignerTestFunctions { - private Logger logger = Logger.getLogger(ReactionParserTests.class.getName());; - ReactionXmlParser parser; - CellDesignerElementCollection elements; - - @Before - public void setUp() throws Exception { - elements = new CellDesignerElementCollection(); - - parser = new ReactionXmlParser(elements, false); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testColorReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/colorfull_reaction.xml"); - Reaction reaction = model.getReactionByReactionId("re1"); - PolylineData line = reaction.getReactants().get(0).getLine(); - assertFalse("000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); - line = reaction.getProducts().get(0).getLine(); - assertFalse("000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); - line = reaction.getModifiers().get(0).getLine(); - assertFalse("000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); - line = reaction.getOperators().get(0).getLine(); - assertFalse("000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testMissingLines() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/problematic/pd_map_with_problematic_reaction_line.xml"); - assertTrue(model.getElementByElementId("sa5003") instanceof GenericProtein); - Set<Reaction> list = model.getReactions(); - for (Reaction reaction : list) { - // reaction re1607 in this model was problematic, but in fact the - // problem - // can be in any reaction line - // if (reaction.getId().equals("re1607")) { - List<Line2D> lines = reaction.getLines(); - for (Line2D line2d : lines) { - assertFalse(Double.isNaN(line2d.getX1())); - assertFalse(Double.isNaN(line2d.getX2())); - assertFalse(Double.isNaN(line2d.getY1())); - assertFalse(Double.isNaN(line2d.getY2())); - } - // } - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTransitionReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transition.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof StateTransitionReaction); - assertEquals(1, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - - assertTrue(reaction.isReversible()); - assertEquals(ArrowType.FULL, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(3, reactant.getLine().getLines().size()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(2, product.getLine().getLines().size()); - - assertTrue(reactant.getLine().getEndPoint().distance(product.getLine().getPoints().get(0)) < 20); - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testCenterLineInSimpleReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transition.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - Reactant reactant = reaction.getReactants().get(0); - - Product product = reaction.getProducts().get(0); - - // center of the line should be different than edge points of - // reactant/product description - assertFalse(reaction.getCenterLine().getP1().distance(reactant.getLine().getEndPoint()) < 1e-6); - assertFalse(reaction.getCenterLine().getP2().distance(product.getLine().getBeginPoint()) < 1e-6); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTransition2() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transitionWithAdditionalNodes.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(2, reaction.getOperators().size()); - for (Reactant reactant : reaction.getReactants()) { - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - - assertTrue(reactant.getLine().getEndPoint().distance(reaction.getReactants().get(0).getLine().getEndPoint()) < 1e-6); - } - - for (Product product : reaction.getProducts()) { - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - - assertTrue(product.getLine().getBeginPoint().distance(reaction.getProducts().get(0).getLine().getBeginPoint()) < 1e-6); - } - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTransiotionOmitted() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transition_omitted.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(0, reaction.getOperators().size()); - assertEquals(KnownTransitionOmittedReaction.class, reaction.getClass()); - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testUnknownTransition() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/unknown_transition.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTranscription() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transcription.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof TranscriptionReaction); - assertEquals(1, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(LineType.DASH_DOT_DOT, reactant.getLine().getType()); - assertEquals(1, reactant.getLine().getLines().size()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(1, product.getLine().getLines().size()); - - assertTrue(reactant.getLine().getEndPoint().distance(product.getLine().getPoints().get(0)) < 20); - assertNotNull(reaction.getReactionRect()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTranscription2() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transcription_with_additions.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof TranscriptionReaction); - assertEquals(2, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(LineType.DASH_DOT_DOT, reactant.getLine().getType()); - assertEquals(1, reactant.getLine().getLines().size()); - NodeOperator operator = null; - for (NodeOperator operator2 : reaction.getOperators()) { - if (operator2 instanceof SplitOperator) - operator = operator2; - } - assertEquals(LineType.DASH_DOT_DOT, operator.getLine().getType()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(1, product.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTranslation() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/translation.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof TranslationReaction); - assertEquals(2, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(LineType.DASH_DOT, reactant.getLine().getType()); - assertEquals(1, reactant.getLine().getLines().size()); - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.DASH_DOT, operator.getLine().getType()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(1, product.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTransport() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/transport.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof TransportReaction); - assertEquals(1, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(1, reactant.getLine().getLines().size()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL_CROSSBAR, product.getLine().getEndAtd().getArrowType()); - assertEquals(1, product.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testHeterodimer() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/heterodimer.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof HeterodimerAssociationReaction); - assertEquals(2, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(2, reactant.getLine().getLines().size()); - reactant = reaction.getReactants().get(1); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(3, reactant.getLine().getLines().size()); - - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(3, product.getLine().getLines().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.SOLID, operator.getLine().getType()); - assertEquals(7, operator.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testHeterodimerWithAdditions() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/heterodimerWithAdditions.xml"); - - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof HeterodimerAssociationReaction); - assertEquals(3, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(2, reactant.getLine().getLines().size()); - reactant = reaction.getReactants().get(1); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(3, reactant.getLine().getLines().size()); - - for (Product product : reaction.getProducts()) { - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - } - - assertEquals(3, reaction.getOperators().size()); - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.SOLID, operator.getLine().getType()); - assertEquals(7, operator.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testDissociation() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/dissociation.xml"); - assertEquals(2, model.getReactions().size()); - Reaction reaction = null; - for (Reaction reaction2 : model.getReactions()) { - if (reaction2.getIdReaction().equals("re1")) - reaction = reaction2; - } - assertTrue(reaction instanceof DissociationReaction); - assertEquals(1, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(2, reactant.getLine().getLines().size()); - - assertEquals(2, reaction.getProducts().size()); - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(5, product.getLine().getLines().size()); - product = reaction.getProducts().get(1); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(4, product.getLine().getLines().size()); - - assertEquals(1, reaction.getOperators().size()); - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.SOLID, operator.getLine().getType()); - assertEquals(5, operator.getLine().getLines().size()); - - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testDissociationWithAddition() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/dissociationWithAddition.xml"); - assertEquals(2, model.getReactions().size()); - Reaction reaction = null; - for (Reaction reaction2 : model.getReactions()) { - if (reaction2.getIdReaction().equals("re1")) - reaction = reaction2; - } - assertTrue(reaction instanceof DissociationReaction); - assertEquals(2, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(2, reactant.getLine().getLines().size()); - - assertEquals(3, reaction.getProducts().size()); - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(5, product.getLine().getLines().size()); - product = reaction.getProducts().get(1); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(4, product.getLine().getLines().size()); - - assertEquals(3, reaction.getOperators().size()); - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.SOLID, operator.getLine().getType()); - assertEquals(5, operator.getLine().getLines().size()); - assertEquals(DissociationOperator.class, operator.getClass()); - - assertNotNull(reaction.getReactionRect()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTruncation() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/truncation.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof TruncationReaction); - assertEquals(1, reaction.getReactants().size()); - Reactant reactant = reaction.getReactants().get(0); - assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); - assertEquals(2, reactant.getLine().getLines().size()); - - assertEquals(2, reaction.getProducts().size()); - Product product = reaction.getProducts().get(0); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(3, product.getLine().getLines().size()); - product = reaction.getProducts().get(1); - assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); - assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); - assertEquals(4, product.getLine().getLines().size()); - - assertEquals(1, reaction.getOperators().size()); - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(LineType.SOLID, operator.getLine().getType()); - assertEquals(1, operator.getLine().getLines().size()); - assertEquals(TruncationOperator.class, operator.getClass()); - assertEquals(reaction.getReactionRect(), ReactionRect.RECT_BOLT); - - assertNotNull(reaction.getReactionRect()); - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTruncationWithModifier() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/truncationWithModifier.xml"); - Reaction reaction = model.getReactions().iterator().next(); - Modifier m = reaction.getModifiers().get(0); - assertEquals(ArrowType.CIRCLE, m.getLine().getEndAtd().getArrowType()); - - m = reaction.getModifiers().get(1); - assertEquals(ArrowType.CROSSBAR, m.getLine().getEndAtd().getArrowType()); - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexModifier1() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexModifier1.xml"); - - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(1, reaction.getOperators().size()); - assertEquals(2, reaction.getModifiers().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertTrue(operator.isModifierOperator()); - assertEquals(AndOperator.class, operator.getClass()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexModifier2() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexModifier2.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(1, reaction.getOperators().size()); - assertEquals(2, reaction.getModifiers().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertTrue(operator.isModifierOperator()); - assertEquals(OrOperator.class, operator.getClass()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexModifier3() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexModifier3.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(1, reaction.getOperators().size()); - assertEquals(2, reaction.getModifiers().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertTrue(operator.isModifierOperator()); - assertEquals(NandOperator.class, operator.getClass()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexModifier4() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexModifier4.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(1, reaction.getOperators().size()); - assertEquals(2, reaction.getModifiers().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertTrue(operator.isModifierOperator()); - assertEquals(UnknownOperator.class, operator.getClass()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexReaction.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(0, reaction.getModifiers().size()); - assertEquals(2, reaction.getOperators().size()); - assertEquals(2, reaction.getProducts().size()); - assertEquals(2, reaction.getReactants().size()); - - NodeOperator operator = reaction.getOperators().iterator().next(); - assertEquals(AndOperator.class, operator.getClass()); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0,false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testComplexModifiers5() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexModifier5.xml"); - - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(4, reaction.getModifiers().size()); - assertEquals(1, reaction.getOperators().size()); - assertEquals(2, reaction.getOperators().iterator().next().getInputs().size()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProblematicAnchors() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/problemWithAnchors2.xml"); - - Reaction reaction1 = null; - Reaction reaction2 = null; - for (Reaction reaction : model.getReactions()) { - if (reaction.getIdReaction().equals("re2")) - reaction1 = reaction; - if (reaction.getIdReaction().equals("re3")) - reaction2 = reaction; - } - Reactant reactant = reaction1.getReactants().get(0); - Element alias1 = reaction1.getReactants().get(0).getElement(); - Element alias2 = reaction1.getProducts().get(0).getElement(); - Product product = reaction1.getProducts().get(0); - Point2D point = new Point2D.Double(alias1.getX() + alias1.getWidth() / 2, alias1.getY() + alias1.getHeight()); - Point2D point2 = new Point2D.Double(alias2.getX(), alias2.getY() + alias2.getHeight() / 2); - assertTrue(point.distance(reactant.getLine().getPoints().get(0)) < 1); - assertTrue(point2.distance(product.getLine().getEndPoint()) < 1); - - reactant = reaction2.getReactants().get(0); - alias1 = reaction2.getReactants().get(0).getElement(); - alias2 = reaction2.getProducts().get(0).getElement(); - product = reaction2.getProducts().get(0); - point = new Point2D.Double(alias1.getX() + alias1.getWidth(), alias1.getY() + alias1.getHeight() / 2); - point2 = new Point2D.Double(alias2.getX(), alias2.getY() + alias2.getHeight() / 2); - assertTrue(point.distance(reactant.getLine().getPoints().get(0)) < 1); - assertTrue(point2.distance(product.getLine().getEndPoint()) < 1); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProblematicAnchors3() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/problemWithAnchors3.xml"); - Reaction reaction = null; - for (Reaction reaction2 : model.getReactions()) { - if (reaction2.getIdReaction().equals("re3")) - reaction = reaction2; - } - Point2D point = new Point2D.Double(164.85583789974368, 86.060142902597); - Point2D point2 = new Point2D.Double(397.06477630152193, 284.99999999999994); - - assertTrue(point.distance(reaction.getModifiers().get(0).getLine().getPoints().get(0)) < 1); - assertTrue(point2.distance(reaction.getModifiers().get(1).getLine().getPoints().get(0)) < 1); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testPositiveInfluence() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/positive_influence.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertNull(reaction.getReactionRect()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProblematicAnchors2() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/complexReactionWithModifier.xml"); - Reaction reaction = model.getReactions().iterator().next(); - assertEquals(reaction.getProducts().get(0).getLine().length(), reaction.getReactants().get(0).getLine().length(), 1e-6); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProblematicAnchorsWithTwoReactantReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/centeredAnchorInModifier.xml"); - Reaction reaction = model.getReactions().iterator().next(); - Reactant r = null; - for (Reactant reactant : reaction.getReactants()) { - if (reactant.getElement().getName().equals("s3")) { - r = reactant; - } - } - assertNotNull(r); - // I think there is still a bug becaue it should work with smaller delta - // :) - assertEquals(r.getLine().getPoints().get(1).getX(), r.getLine().getPoints().get(2).getX(), 1); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionWithModifiers() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/reactionWithModifiers.xml"); - Reaction reaction = model.getReactions().iterator().next(); - List<Modifier> modifiers = reaction.getModifiers(); - Modifier sa3 = null; - Modifier sa4 = null; - for (Modifier modifier : modifiers) { - if (modifier.getElement().getElementId().equals("sa3")) { - sa3 = modifier; - } - if (modifier.getElement().getElementId().equals("sa4")) { - sa4 = modifier; - } - } - assertEquals(sa3.getLine().getPoints().get(0).distance(new Point2D.Double(101.9591678008944, 68.0)), 0, EPSILON); - assertEquals(sa3.getLine().getPoints().get(1).distance(new Point2D.Double(101.86750788643532, 112.89589905362774)), 0, EPSILON); - assertEquals(sa3.getLine().getPoints().get(2).distance(new Point2D.Double(190.66666666666666, 117.66666666666667)), 0, EPSILON); - - assertEquals(sa4.getLine().getPoints().get(0).distance(new Point2D.Double(267.7075561388218, 54.00000000000001)), 0, EPSILON); - assertEquals(sa4.getLine().getPoints().get(1).distance(new Point2D.Double(298.3735877669656, 71.67109819284718)), 0, EPSILON); - assertEquals(sa4.getLine().getPoints().get(2).distance(new Point2D.Double(190.66666666666666, 117.66666666666667)), 0, EPSILON); - - // new NormalImageGenerator(1, 0, 0, 1024, 1024, model, true, false, 0, - // false).saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionOperatorsWithOperators() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); - Reaction reaction = model.getReactions().iterator().next(); - NodeOperator operator1 = reaction.getOperators().get(0); - NodeOperator operator2 = reaction.getOperators().get(1); - NodeOperator operator3 = reaction.getOperators().get(2); - - // new NormalImageGenerator(1, 0, 0, 1024, 1024, model, true, false, 0, - // false).saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - assertEquals(operator1.getLine().getPoints().get(0).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, EPSILON); - assertEquals(operator1.getLine().getPoints().get(1).distance(new Point2D.Double(287.97349260719136, 204.40292958328325)), 0, EPSILON); - assertEquals(operator1.getLine().getPoints().get(2).distance(new Point2D.Double(372.9868291110932, 203.8558964441427)), 0, EPSILON); - - assertEquals(operator2.getLine().getPoints().get(0).distance(new Point2D.Double(359.1840955643148, 203.94471254019686)), 0, EPSILON); - assertEquals(operator2.getLine().getPoints().get(1).distance(new Point2D.Double(372.9868291110932, 203.8558964441427)), 0, EPSILON); - - assertEquals(operator3.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, EPSILON); - assertEquals(operator3.getLine().getPoints().get(1).distance(new Point2D.Double(380.9866634960982, 203.80442011470782)), 0, EPSILON); - - Product product1 = reaction.getProducts().get(0); - Product product2 = reaction.getProducts().get(1); - - assertEquals(product1.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, EPSILON); - assertEquals(product1.getLine().getPoints().get(1).distance(new Point2D.Double(466.0, 203.25738697556727)), 0, EPSILON); - - assertEquals(product2.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, EPSILON); - assertEquals(product2.getLine().getPoints().get(1).distance(new Point2D.Double(452.96894321929705, 107.00000000000001)), 0, EPSILON); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionProductsWithOperators() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); - Reaction reaction = model.getReactions().iterator().next(); - - Product product1 = reaction.getProducts().get(0); - Product product2 = reaction.getProducts().get(1); - - assertEquals(product1.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, EPSILON); - assertEquals(product1.getLine().getPoints().get(1).distance(new Point2D.Double(466.0, 203.25738697556727)), 0, EPSILON); - - assertEquals(product2.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, EPSILON); - assertEquals(product2.getLine().getPoints().get(1).distance(new Point2D.Double(452.96894321929705, 107.00000000000001)), 0, EPSILON); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionReactantsWithOperators() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); - Reaction reaction = model.getReactions().iterator().next(); - - Reactant reactant1 = reaction.getReactants().get(0); - Reactant reactant2 = reaction.getReactants().get(1); - Reactant reactant3 = reaction.getReactants().get(2); - - assertEquals(reactant1.getLine().getPoints().get(0).distance(new Point2D.Double(112.53618421052632, 167.46381578947367)), 0, EPSILON); - assertEquals(reactant1.getLine().getPoints().get(1).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, EPSILON); - - assertEquals(reactant2.getLine().getPoints().get(0).distance(new Point2D.Double(121.0, 242.0)), 0, EPSILON); - assertEquals(reactant2.getLine().getPoints().get(1).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, EPSILON); - - assertEquals(reactant3.getLine().getPoints().get(0).distance(new Point2D.Double(116.65392247520951, 72.34607752479052)), 0, EPSILON); - assertEquals(reactant3.getLine().getPoints().get(1).distance(new Point2D.Double(359.1840955643148, 203.94471254019686)), 0, EPSILON); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testTransitionReactionToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transition.xml"); - - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getClass(), reaction2.getClass()); - assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); - Reactant reactant1 = reaction1.getReactants().get(0); - Reactant reactant2 = reaction2.getReactants().get(0); - assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); - assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); - assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); - - Product product1 = reaction1.getProducts().get(0); - Product product2 = reaction2.getProducts().get(0); - assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); - assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); - assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); - - assertEquals(reactant1.getLine().getEndPoint().getX(), reactant2.getLine().getEndPoint().getX(), EPSILON); - assertEquals(reactant1.getLine().getEndPoint().getY(), reactant2.getLine().getEndPoint().getY(), EPSILON); - - assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - - assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); - assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); - - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTransitionWidthAdditionalNodelReactionToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transitionWithAdditionalNodes.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getClass(), reaction2.getClass()); - assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); - Reactant reactant1 = reaction1.getReactants().get(1); - Reactant reactant2 = reaction2.getReactants().get(1); - assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); - assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); - assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); - - Product product1 = reaction1.getProducts().get(1); - Product product2 = reaction2.getProducts().get(1); - assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); - assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); - assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); - - assertTrue(reactant1.getLine().getEndPoint().distance(reactant2.getLine().getEndPoint()) < 1e-6); - assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - - assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); - assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); - - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTranslationToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_translation.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getClass(), reaction2.getClass()); - assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); - Reactant reactant1 = reaction1.getReactants().get(1); - Reactant reactant2 = reaction2.getReactants().get(1); - assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); - assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); - assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); - - Product product1 = reaction1.getProducts().get(1); - Product product2 = reaction2.getProducts().get(1); - assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); - assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); - assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); - - assertTrue(reactant1.getLine().getEndPoint().distance(reactant2.getLine().getEndPoint()) < 1e-6); - assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - - assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); - assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); - - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTransportToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transport.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - assertEquals(reaction1.getMiriamData().size(), reaction2.getMiriamData().size()); - assertEquals(reaction1.getNotes().trim(), reaction2.getNotes().trim()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTruncationToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_truncation.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTruncationWithModifierToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_truncationWithModifier.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testUnknownTransitionToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_unknown_transition.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTransitionOmittedToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transition_omitted.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTranscriptionToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transcription.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testTranscriptionWithAdditionsToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transcription_with_additions.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testReactionWithOperatorsToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_with_operators.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testReactionWithModifiersToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_with_modifiers.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testReactionPositiveInfluenceToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_positive_influence.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testReactionHeterodimerWithAdditionsToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer_with_additions.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getNodes().size(), reaction2.getNodes().size()); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testReactionHeterodimerToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getNodes().size(), reaction2.getNodes().size()); - - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testOperatorInReactionHeterodimer() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - for (NodeOperator operator : reaction1.getOperators()) { - assertTrue(operator instanceof AssociationOperator); - } - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - @Test - public void testReactionDissociationWithAdditionToXml() throws Exception { - try { - Model model = getModelFilledWithSpecies(); - String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml"); - Node node = getNodeFromXmlString(xmlString); - Reaction reaction1 = parser.getReaction(node, model); - - String xmlString2 = parser.toXml(reaction1); - assertNotNull(xmlString2); - // logger.debug(xmlString2); - Node node2 = getNodeFromXmlString(xmlString2); - Reaction reaction2 = parser.getReaction(node2, model); - - assertEquals(reaction1.getName(), reaction2.getName()); - assertNotNull(reaction1.getName()); - assertFalse(reaction1.getName().trim().equals("")); - List<Line2D> lines1 = reaction1.getLines(); - List<Line2D> lines2 = reaction2.getLines(); - - for (int i = 0; i < lines1.size(); i++) { - assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); - assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); - assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); - assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); - } - assertEquals(reaction1.getClass(), reaction2.getClass()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw e; - } - } - - private Model getModelFilledWithSpecies() { - Model model = new ModelFullIndexed(null); - - Species sa1 = new GenericProtein("sa1"); - sa1.setX(100.0); - sa1.setY(200.0); - sa1.setWidth(300.0); - sa1.setHeight(400.0); - model.addElement(sa1); - elements.addModelElement(sa1, new CellDesignerGenericProtein("s1")); - - Species sa2 = new GenericProtein("sa2"); - sa2.setX(1050.0); - sa2.setY(2050.0); - sa2.setWidth(300.0); - sa2.setHeight(450.0); - model.addElement(sa2); - elements.addModelElement(sa2, new CellDesignerGenericProtein("s2")); - - Species sa3 = new GenericProtein("sa3"); - sa3.setX(600.0); - sa3.setY(250.0); - sa3.setWidth(300.0); - sa3.setHeight(400.0); - model.addElement(sa3); - elements.addModelElement(sa3, new CellDesignerGenericProtein("s3")); - - Species sa4 = new GenericProtein("sa4"); - sa4.setX(550.0); - sa4.setY(350.0); - sa4.setWidth(300.0); - sa4.setHeight(450.0); - model.addElement(sa4); - elements.addElement(new CellDesignerGenericProtein("s4")); - - Species sa5 = new GenericProtein("sa5"); - sa5.setX(10.0); - sa5.setY(250.0); - sa5.setWidth(300.0); - sa5.setHeight(450.0); - model.addElement(sa5); - elements.addElement(new CellDesignerGenericProtein("s5")); - - Species sa6 = new GenericProtein("sa6"); - sa6.setX(10.0); - sa6.setY(250.0); - sa6.setWidth(300.0); - sa6.setHeight(450.0); - model.addElement(sa6); - - elements.addElement(new CellDesignerGenericProtein("s6")); - - Species sa10 = new GenericProtein("sa10"); - sa10.setX(210.0); - sa10.setY(220.0); - sa10.setWidth(320.0); - sa10.setHeight(250.0); - model.addElement(sa10); - elements.addElement(new CellDesignerGenericProtein("s10")); - - Species sa11 = new GenericProtein("sa11"); - sa11.setX(11.0); - sa11.setY(320.0); - sa11.setWidth(321.0); - sa11.setHeight(150.0); - model.addElement(sa11); - elements.addElement(new CellDesignerGenericProtein("s11")); - - Species sa12 = new GenericProtein("sa12"); - sa12.setX(12.0); - sa12.setY(20.0); - sa12.setWidth(321.0); - sa12.setHeight(150.0); - model.addElement(sa12); - elements.addElement(new CellDesignerGenericProtein("s12")); - - Species sa13 = new GenericProtein("sa13"); - sa13.setX(513.0); - sa13.setY(20.0); - sa13.setWidth(321.0); - sa13.setHeight(150.0); - model.addElement(sa13); - elements.addElement(new CellDesignerGenericProtein("s13")); - - Species sa14 = new GenericProtein("sa14"); - sa14.setX(14.0); - sa14.setY(820.0); - sa14.setWidth(321.0); - sa14.setHeight(150.0); - model.addElement(sa14); - elements.addElement(new CellDesignerGenericProtein("s14")); - - Species sa15 = new GenericProtein("sa15"); - sa15.setX(815.0); - sa15.setY(620.0); - sa15.setWidth(321.0); - sa15.setHeight(150.0); - model.addElement(sa15); - elements.addElement(new CellDesignerGenericProtein("s15")); - - return model; - } - - @Test - public void testLogiGateAndReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/logicGateAnd.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof BooleanLogicGateReaction); - assertEquals(1, reaction.getOperators().size()); - assertTrue(reaction.getOperators().get(0) instanceof AndOperator); - assertEquals(2, reaction.getReactants().size()); - assertEquals(1, reaction.getProducts().size()); - - assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); - - CellDesignerXmlParser cellDesignerXmlParser = new CellDesignerXmlParser(); - - model.setName(null); - String xmlString = cellDesignerXmlParser.toXml(model); - - ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); - Model model2 = cellDesignerXmlParser.createModel(new ConverterParams().inputStream(bais).sizeAutoAdjust(false)); - - ModelComparator mc = new ModelComparator(); - assertEquals("After CellDesigner xml serialization models are different", 0, mc.compare(model, model2)); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testLogiGateOrReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/logicGateOr.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof BooleanLogicGateReaction); - assertEquals(1, reaction.getOperators().size()); - assertTrue(reaction.getOperators().get(0) instanceof OrOperator); - assertEquals(2, reaction.getReactants().size()); - assertEquals(1, reaction.getProducts().size()); - - assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testLogiGateNotReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/logicGateNot.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof BooleanLogicGateReaction); - assertEquals(1, reaction.getOperators().size()); - assertTrue(reaction.getOperators().get(0) instanceof NandOperator); - assertEquals(2, reaction.getReactants().size()); - assertEquals(1, reaction.getProducts().size()); - - assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testLogiGateUnknownReaction() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/reactions/logicGateUnknown.xml"); - assertEquals(1, model.getReactions().size()); - Reaction reaction = model.getReactions().iterator().next(); - assertTrue(reaction instanceof BooleanLogicGateReaction); - assertEquals(1, reaction.getOperators().size()); - assertTrue(reaction.getOperators().get(0) instanceof UnknownOperator); - assertEquals(2, reaction.getReactants().size()); - assertEquals(1, reaction.getProducts().size()); - - assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); - assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); - - // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, - // 1024, 1024, model, true, false, 0, false); - // generator.saveToPNG("tmp.png"); - // Desktop.getDesktop().open(new File("tmp.png")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProblematicDrawing() throws Exception { - Model model; - try { - model = getModelForFile("testFiles/problematic/reaction_drawing_problem.xml"); - Reaction reaction = model.getReactionByReactionId("re1"); - assertEquals(0, reaction.getCenterPoint().distance(new Point2D.Double(258.5, 145.0)), EPSILON); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testProteinsInsideComplex() throws Exception { - try { - Model model = getModelForFile("testFiles/problematic/proteins_inside_complex.xml"); - for (Element el : model.getElements()) { - assertFalse(el.getClass().equals(Protein.class)); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } + private Logger logger = Logger.getLogger(ReactionParserTests.class.getName());; + ReactionXmlParser parser; + CellDesignerElementCollection elements; + + @Before + public void setUp() throws Exception { + elements = new CellDesignerElementCollection(); + + parser = new ReactionXmlParser(elements, false); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testColorReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/colorfull_reaction.xml"); + Reaction reaction = model.getReactionByReactionId("re1"); + PolylineData line = reaction.getReactants().get(0).getLine(); + assertFalse( + "000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); + line = reaction.getProducts().get(0).getLine(); + assertFalse( + "000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); + line = reaction.getModifiers().get(0).getLine(); + assertFalse( + "000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); + line = reaction.getOperators().get(0).getLine(); + assertFalse( + "000".equals(line.getColor().getRed() + "" + line.getColor().getGreen() + "" + line.getColor().getBlue())); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testMissingLines() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/problematic/pd_map_with_problematic_reaction_line.xml"); + assertTrue(model.getElementByElementId("sa5003") instanceof GenericProtein); + Set<Reaction> list = model.getReactions(); + for (Reaction reaction : list) { + // reaction re1607 in this model was problematic, but in fact the + // problem + // can be in any reaction line + // if (reaction.getId().equals("re1607")) { + List<Line2D> lines = reaction.getLines(); + for (Line2D line2d : lines) { + assertFalse(Double.isNaN(line2d.getX1())); + assertFalse(Double.isNaN(line2d.getX2())); + assertFalse(Double.isNaN(line2d.getY1())); + assertFalse(Double.isNaN(line2d.getY2())); + } + // } + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTransitionReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transition.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof StateTransitionReaction); + assertEquals(1, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + + assertTrue(reaction.isReversible()); + assertEquals(ArrowType.FULL, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(3, reactant.getLine().getLines().size()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(2, product.getLine().getLines().size()); + + assertTrue(reactant.getLine().getEndPoint().distance(product.getLine().getPoints().get(0)) < 20); + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testCenterLineInSimpleReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transition.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + Reactant reactant = reaction.getReactants().get(0); + + Product product = reaction.getProducts().get(0); + + // center of the line should be different than edge points of + // reactant/product description + assertFalse(reaction.getCenterLine().getP1().distance(reactant.getLine().getEndPoint()) < 1e-6); + assertFalse(reaction.getCenterLine().getP2().distance(product.getLine().getBeginPoint()) < 1e-6); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTransition2() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transitionWithAdditionalNodes.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(2, reaction.getOperators().size()); + for (Reactant reactant : reaction.getReactants()) { + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + + assertTrue( + reactant.getLine().getEndPoint().distance(reaction.getReactants().get(0).getLine().getEndPoint()) < 1e-6); + } + + for (Product product : reaction.getProducts()) { + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + + assertTrue( + product.getLine().getBeginPoint().distance(reaction.getProducts().get(0).getLine().getBeginPoint()) < 1e-6); + } + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTransiotionOmitted() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transition_omitted.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(0, reaction.getOperators().size()); + assertEquals(KnownTransitionOmittedReaction.class, reaction.getClass()); + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testUnknownTransition() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/unknown_transition.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTranscription() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transcription.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof TranscriptionReaction); + assertEquals(1, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(LineType.DASH_DOT_DOT, reactant.getLine().getType()); + assertEquals(1, reactant.getLine().getLines().size()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(1, product.getLine().getLines().size()); + + assertTrue(reactant.getLine().getEndPoint().distance(product.getLine().getPoints().get(0)) < 20); + assertNotNull(reaction.getReactionRect()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTranscription2() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transcription_with_additions.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof TranscriptionReaction); + assertEquals(2, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(LineType.DASH_DOT_DOT, reactant.getLine().getType()); + assertEquals(1, reactant.getLine().getLines().size()); + NodeOperator operator = null; + for (NodeOperator operator2 : reaction.getOperators()) { + if (operator2 instanceof SplitOperator) + operator = operator2; + } + assertEquals(LineType.DASH_DOT_DOT, operator.getLine().getType()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(1, product.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTranslation() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/translation.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof TranslationReaction); + assertEquals(2, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(LineType.DASH_DOT, reactant.getLine().getType()); + assertEquals(1, reactant.getLine().getLines().size()); + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.DASH_DOT, operator.getLine().getType()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(1, product.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTransport() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/transport.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof TransportReaction); + assertEquals(1, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(1, reactant.getLine().getLines().size()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL_CROSSBAR, product.getLine().getEndAtd().getArrowType()); + assertEquals(1, product.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testHeterodimer() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/heterodimer.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof HeterodimerAssociationReaction); + assertEquals(2, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(2, reactant.getLine().getLines().size()); + reactant = reaction.getReactants().get(1); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(3, reactant.getLine().getLines().size()); + + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(3, product.getLine().getLines().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.SOLID, operator.getLine().getType()); + assertEquals(7, operator.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testHeterodimerWithAdditions() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/heterodimerWithAdditions.xml"); + + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof HeterodimerAssociationReaction); + assertEquals(3, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(2, reactant.getLine().getLines().size()); + reactant = reaction.getReactants().get(1); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(3, reactant.getLine().getLines().size()); + + for (Product product : reaction.getProducts()) { + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + } + + assertEquals(3, reaction.getOperators().size()); + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.SOLID, operator.getLine().getType()); + assertEquals(7, operator.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testDissociation() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/dissociation.xml"); + assertEquals(2, model.getReactions().size()); + Reaction reaction = null; + for (Reaction reaction2 : model.getReactions()) { + if (reaction2.getIdReaction().equals("re1")) + reaction = reaction2; + } + assertTrue(reaction instanceof DissociationReaction); + assertEquals(1, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(2, reactant.getLine().getLines().size()); + + assertEquals(2, reaction.getProducts().size()); + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(5, product.getLine().getLines().size()); + product = reaction.getProducts().get(1); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(4, product.getLine().getLines().size()); + + assertEquals(1, reaction.getOperators().size()); + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.SOLID, operator.getLine().getType()); + assertEquals(5, operator.getLine().getLines().size()); + + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testDissociationWithAddition() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/dissociationWithAddition.xml"); + assertEquals(2, model.getReactions().size()); + Reaction reaction = null; + for (Reaction reaction2 : model.getReactions()) { + if (reaction2.getIdReaction().equals("re1")) + reaction = reaction2; + } + assertTrue(reaction instanceof DissociationReaction); + assertEquals(2, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(2, reactant.getLine().getLines().size()); + + assertEquals(3, reaction.getProducts().size()); + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(5, product.getLine().getLines().size()); + product = reaction.getProducts().get(1); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(4, product.getLine().getLines().size()); + + assertEquals(3, reaction.getOperators().size()); + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.SOLID, operator.getLine().getType()); + assertEquals(5, operator.getLine().getLines().size()); + assertEquals(DissociationOperator.class, operator.getClass()); + + assertNotNull(reaction.getReactionRect()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTruncation() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/truncation.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof TruncationReaction); + assertEquals(1, reaction.getReactants().size()); + Reactant reactant = reaction.getReactants().get(0); + assertEquals(ArrowType.NONE, reactant.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.NONE, reactant.getLine().getEndAtd().getArrowType()); + assertEquals(2, reactant.getLine().getLines().size()); + + assertEquals(2, reaction.getProducts().size()); + Product product = reaction.getProducts().get(0); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(3, product.getLine().getLines().size()); + product = reaction.getProducts().get(1); + assertEquals(ArrowType.NONE, product.getLine().getBeginAtd().getArrowType()); + assertEquals(ArrowType.FULL, product.getLine().getEndAtd().getArrowType()); + assertEquals(4, product.getLine().getLines().size()); + + assertEquals(1, reaction.getOperators().size()); + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(LineType.SOLID, operator.getLine().getType()); + assertEquals(1, operator.getLine().getLines().size()); + assertEquals(TruncationOperator.class, operator.getClass()); + assertEquals(reaction.getReactionRect(), ReactionRect.RECT_BOLT); + + assertNotNull(reaction.getReactionRect()); + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTruncationWithModifier() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/truncationWithModifier.xml"); + Reaction reaction = model.getReactions().iterator().next(); + Modifier m = reaction.getModifiers().get(0); + assertEquals(ArrowType.CIRCLE, m.getLine().getEndAtd().getArrowType()); + + m = reaction.getModifiers().get(1); + assertEquals(ArrowType.CROSSBAR, m.getLine().getEndAtd().getArrowType()); + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexModifier1() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexModifier1.xml"); + + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(1, reaction.getOperators().size()); + assertEquals(2, reaction.getModifiers().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertTrue(operator.isModifierOperator()); + assertEquals(AndOperator.class, operator.getClass()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexModifier2() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexModifier2.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(1, reaction.getOperators().size()); + assertEquals(2, reaction.getModifiers().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertTrue(operator.isModifierOperator()); + assertEquals(OrOperator.class, operator.getClass()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexModifier3() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexModifier3.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(1, reaction.getOperators().size()); + assertEquals(2, reaction.getModifiers().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertTrue(operator.isModifierOperator()); + assertEquals(NandOperator.class, operator.getClass()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexModifier4() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexModifier4.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(1, reaction.getOperators().size()); + assertEquals(2, reaction.getModifiers().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertTrue(operator.isModifierOperator()); + assertEquals(UnknownOperator.class, operator.getClass()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexReaction.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(0, reaction.getModifiers().size()); + assertEquals(2, reaction.getOperators().size()); + assertEquals(2, reaction.getProducts().size()); + assertEquals(2, reaction.getReactants().size()); + + NodeOperator operator = reaction.getOperators().iterator().next(); + assertEquals(AndOperator.class, operator.getClass()); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0,false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testComplexModifiers5() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexModifier5.xml"); + + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(4, reaction.getModifiers().size()); + assertEquals(1, reaction.getOperators().size()); + assertEquals(2, reaction.getOperators().iterator().next().getInputs().size()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProblematicAnchors() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/problemWithAnchors2.xml"); + + Reaction reaction1 = null; + Reaction reaction2 = null; + for (Reaction reaction : model.getReactions()) { + if (reaction.getIdReaction().equals("re2")) + reaction1 = reaction; + if (reaction.getIdReaction().equals("re3")) + reaction2 = reaction; + } + Reactant reactant = reaction1.getReactants().get(0); + Element alias1 = reaction1.getReactants().get(0).getElement(); + Element alias2 = reaction1.getProducts().get(0).getElement(); + Product product = reaction1.getProducts().get(0); + Point2D point = new Point2D.Double(alias1.getX() + alias1.getWidth() / 2, alias1.getY() + alias1.getHeight()); + Point2D point2 = new Point2D.Double(alias2.getX(), alias2.getY() + alias2.getHeight() / 2); + assertTrue(point.distance(reactant.getLine().getPoints().get(0)) < 1); + assertTrue(point2.distance(product.getLine().getEndPoint()) < 1); + + reactant = reaction2.getReactants().get(0); + alias1 = reaction2.getReactants().get(0).getElement(); + alias2 = reaction2.getProducts().get(0).getElement(); + product = reaction2.getProducts().get(0); + point = new Point2D.Double(alias1.getX() + alias1.getWidth(), alias1.getY() + alias1.getHeight() / 2); + point2 = new Point2D.Double(alias2.getX(), alias2.getY() + alias2.getHeight() / 2); + assertTrue(point.distance(reactant.getLine().getPoints().get(0)) < 1); + assertTrue(point2.distance(product.getLine().getEndPoint()) < 1); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProblematicAnchors3() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/problemWithAnchors3.xml"); + Reaction reaction = null; + for (Reaction reaction2 : model.getReactions()) { + if (reaction2.getIdReaction().equals("re3")) + reaction = reaction2; + } + Point2D point = new Point2D.Double(164.85583789974368, 86.060142902597); + Point2D point2 = new Point2D.Double(397.06477630152193, 284.99999999999994); + + assertTrue(point.distance(reaction.getModifiers().get(0).getLine().getPoints().get(0)) < 1); + assertTrue(point2.distance(reaction.getModifiers().get(1).getLine().getPoints().get(0)) < 1); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testPositiveInfluence() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/positive_influence.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertNull(reaction.getReactionRect()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProblematicAnchors2() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/complexReactionWithModifier.xml"); + Reaction reaction = model.getReactions().iterator().next(); + assertEquals(reaction.getProducts().get(0).getLine().length(), reaction.getReactants().get(0).getLine().length(), + 1e-6); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProblematicAnchorsWithTwoReactantReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/centeredAnchorInModifier.xml"); + Reaction reaction = model.getReactions().iterator().next(); + Reactant r = null; + for (Reactant reactant : reaction.getReactants()) { + if (reactant.getElement().getName().equals("s3")) { + r = reactant; + } + } + assertNotNull(r); + // I think there is still a bug becaue it should work with smaller delta + // :) + assertEquals(r.getLine().getPoints().get(1).getX(), r.getLine().getPoints().get(2).getX(), 1); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionWithModifiers() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/reactionWithModifiers.xml"); + Reaction reaction = model.getReactions().iterator().next(); + List<Modifier> modifiers = reaction.getModifiers(); + Modifier sa3 = null; + Modifier sa4 = null; + for (Modifier modifier : modifiers) { + if (modifier.getElement().getElementId().equals("sa3")) { + sa3 = modifier; + } + if (modifier.getElement().getElementId().equals("sa4")) { + sa4 = modifier; + } + } + assertEquals(sa3.getLine().getPoints().get(0).distance(new Point2D.Double(101.9591678008944, 68.0)), 0, EPSILON); + assertEquals( + sa3.getLine().getPoints().get(1).distance(new Point2D.Double(101.86750788643532, 112.89589905362774)), 0, + EPSILON); + assertEquals( + sa3.getLine().getPoints().get(2).distance(new Point2D.Double(190.66666666666666, 117.66666666666667)), 0, + EPSILON); + + assertEquals(sa4.getLine().getPoints().get(0).distance(new Point2D.Double(267.7075561388218, 54.00000000000001)), + 0, EPSILON); + assertEquals(sa4.getLine().getPoints().get(1).distance(new Point2D.Double(298.3735877669656, 71.67109819284718)), + 0, EPSILON); + assertEquals( + sa4.getLine().getPoints().get(2).distance(new Point2D.Double(190.66666666666666, 117.66666666666667)), 0, + EPSILON); + + // new NormalImageGenerator(1, 0, 0, 1024, 1024, model, true, false, 0, + // false).saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionOperatorsWithOperators() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); + Reaction reaction = model.getReactions().iterator().next(); + NodeOperator operator1 = reaction.getOperators().get(0); + NodeOperator operator2 = reaction.getOperators().get(1); + NodeOperator operator3 = reaction.getOperators().get(2); + + // new NormalImageGenerator(1, 0, 0, 1024, 1024, model, true, false, 0, + // false).saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + assertEquals(operator1.getLine().getPoints().get(0).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, + EPSILON); + assertEquals( + operator1.getLine().getPoints().get(1).distance(new Point2D.Double(287.97349260719136, 204.40292958328325)), + 0, EPSILON); + assertEquals( + operator1.getLine().getPoints().get(2).distance(new Point2D.Double(372.9868291110932, 203.8558964441427)), 0, + EPSILON); + + assertEquals( + operator2.getLine().getPoints().get(0).distance(new Point2D.Double(359.1840955643148, 203.94471254019686)), 0, + EPSILON); + assertEquals( + operator2.getLine().getPoints().get(1).distance(new Point2D.Double(372.9868291110932, 203.8558964441427)), 0, + EPSILON); + + assertEquals( + operator3.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), + 0, EPSILON); + assertEquals( + operator3.getLine().getPoints().get(1).distance(new Point2D.Double(380.9866634960982, 203.80442011470782)), 0, + EPSILON); + + Product product1 = reaction.getProducts().get(0); + Product product2 = reaction.getProducts().get(1); + + assertEquals( + product1.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, + EPSILON); + assertEquals(product1.getLine().getPoints().get(1).distance(new Point2D.Double(466.0, 203.25738697556727)), 0, + EPSILON); + + assertEquals( + product2.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, + EPSILON); + assertEquals( + product2.getLine().getPoints().get(1).distance(new Point2D.Double(452.96894321929705, 107.00000000000001)), 0, + EPSILON); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionProductsWithOperators() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); + Reaction reaction = model.getReactions().iterator().next(); + + Product product1 = reaction.getProducts().get(0); + Product product2 = reaction.getProducts().get(1); + + assertEquals( + product1.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, + EPSILON); + assertEquals(product1.getLine().getPoints().get(1).distance(new Point2D.Double(466.0, 203.25738697556727)), 0, + EPSILON); + + assertEquals( + product2.getLine().getPoints().get(0).distance(new Point2D.Double(394.78939704287654, 203.71560401865366)), 0, + EPSILON); + assertEquals( + product2.getLine().getPoints().get(1).distance(new Point2D.Double(452.96894321929705, 107.00000000000001)), 0, + EPSILON); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionReactantsWithOperators() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/reactionWithOperators.xml"); + Reaction reaction = model.getReactions().iterator().next(); + + Reactant reactant1 = reaction.getReactants().get(0); + Reactant reactant2 = reaction.getReactants().get(1); + Reactant reactant3 = reaction.getReactants().get(2); + + assertEquals( + reactant1.getLine().getPoints().get(0).distance(new Point2D.Double(112.53618421052632, 167.46381578947367)), + 0, EPSILON); + assertEquals(reactant1.getLine().getPoints().get(1).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, + EPSILON); + + assertEquals(reactant2.getLine().getPoints().get(0).distance(new Point2D.Double(121.0, 242.0)), 0, EPSILON); + assertEquals(reactant2.getLine().getPoints().get(1).distance(new Point2D.Double(287.0, 242.00000000000009)), 0, + EPSILON); + + assertEquals( + reactant3.getLine().getPoints().get(0).distance(new Point2D.Double(116.65392247520951, 72.34607752479052)), 0, + EPSILON); + assertEquals( + reactant3.getLine().getPoints().get(1).distance(new Point2D.Double(359.1840955643148, 203.94471254019686)), 0, + EPSILON); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testTransitionReactionToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transition.xml"); + + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getClass(), reaction2.getClass()); + assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); + Reactant reactant1 = reaction1.getReactants().get(0); + Reactant reactant2 = reaction2.getReactants().get(0); + assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); + assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); + assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); + + Product product1 = reaction1.getProducts().get(0); + Product product2 = reaction2.getProducts().get(0); + assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); + assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); + assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); + + assertEquals(reactant1.getLine().getEndPoint().getX(), reactant2.getLine().getEndPoint().getX(), EPSILON); + assertEquals(reactant1.getLine().getEndPoint().getY(), reactant2.getLine().getEndPoint().getY(), EPSILON); + + assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + + assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); + assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTransitionWidthAdditionalNodelReactionToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transitionWithAdditionalNodes.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getClass(), reaction2.getClass()); + assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); + Reactant reactant1 = reaction1.getReactants().get(1); + Reactant reactant2 = reaction2.getReactants().get(1); + assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); + assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); + assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); + + Product product1 = reaction1.getProducts().get(1); + Product product2 = reaction2.getProducts().get(1); + assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); + assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); + assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); + + assertTrue(reactant1.getLine().getEndPoint().distance(reactant2.getLine().getEndPoint()) < 1e-6); + assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + + assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); + assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTranslationToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_translation.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getClass(), reaction2.getClass()); + assertEquals(reaction1.getReactants().size(), reaction2.getReactants().size()); + Reactant reactant1 = reaction1.getReactants().get(1); + Reactant reactant2 = reaction2.getReactants().get(1); + assertEquals(reactant1.getLine().getBeginAtd().getArrowType(), reactant2.getLine().getBeginAtd().getArrowType()); + assertEquals(reactant1.getLine().getEndAtd().getArrowType(), reactant2.getLine().getEndAtd().getArrowType()); + assertEquals(reactant1.getLine().getLines().size(), reactant2.getLine().getLines().size()); + + Product product1 = reaction1.getProducts().get(1); + Product product2 = reaction2.getProducts().get(1); + assertEquals(product1.getLine().getBeginAtd().getArrowType(), product2.getLine().getBeginAtd().getArrowType()); + assertEquals(product1.getLine().getEndAtd().getArrowType(), product2.getLine().getEndAtd().getArrowType()); + assertEquals(product1.getLine().getLines().size(), product2.getLine().getLines().size()); + + assertTrue(reactant1.getLine().getEndPoint().distance(reactant2.getLine().getEndPoint()) < 1e-6); + assertEquals(reaction1.getReactionRect(), reaction2.getReactionRect()); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + + assertEquals(product1.getLine().getColor(), product2.getLine().getColor()); + assertEquals(product1.getLine().getWidth(), product2.getLine().getWidth(), 1e-6); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTransportToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transport.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + assertEquals(reaction1.getMiriamData().size(), reaction2.getMiriamData().size()); + assertEquals(reaction1.getNotes().trim(), reaction2.getNotes().trim()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTruncationToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_truncation.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTruncationWithModifierToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_truncationWithModifier.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testUnknownTransitionToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_unknown_transition.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTransitionOmittedToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transition_omitted.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTranscriptionToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transcription.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testTranscriptionWithAdditionsToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_transcription_with_additions.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testReactionWithOperatorsToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_with_operators.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testReactionWithModifiersToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_with_modifiers.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testReactionPositiveInfluenceToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_positive_influence.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testReactionHeterodimerWithAdditionsToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer_with_additions.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getNodes().size(), reaction2.getNodes().size()); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testReactionHeterodimerToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getNodes().size(), reaction2.getNodes().size()); + + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testOperatorInReactionHeterodimer() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_heterodimer.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + for (NodeOperator operator : reaction1.getOperators()) { + assertTrue(operator instanceof AssociationOperator); + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + @Test + public void testReactionDissociationWithAdditionToXml() throws Exception { + try { + Model model = getModelFilledWithSpecies(); + String xmlString = readFile("testFiles/xmlNodeTestExamples/reaction_dissociation_with_addition.xml"); + Node node = getNodeFromXmlString(xmlString); + Reaction reaction1 = parser.getReaction(node, model); + + String xmlString2 = parser.toXml(reaction1); + assertNotNull(xmlString2); + // logger.debug(xmlString2); + Node node2 = getNodeFromXmlString(xmlString2); + Reaction reaction2 = parser.getReaction(node2, model); + + assertEquals(reaction1.getName(), reaction2.getName()); + assertNotNull(reaction1.getName()); + assertFalse(reaction1.getName().trim().equals("")); + List<Line2D> lines1 = reaction1.getLines(); + List<Line2D> lines2 = reaction2.getLines(); + + for (int i = 0; i < lines1.size(); i++) { + assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6); + assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6); + assertEquals(lines1.get(i).getY1(), lines2.get(i).getY1(), 1e-6); + assertEquals(lines1.get(i).getY2(), lines2.get(i).getY2(), 1e-6); + } + assertEquals(reaction1.getClass(), reaction2.getClass()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw e; + } + } + + private Model getModelFilledWithSpecies() { + Model model = new ModelFullIndexed(null); + + Species sa1 = new GenericProtein("sa1"); + sa1.setX(100.0); + sa1.setY(200.0); + sa1.setWidth(300.0); + sa1.setHeight(400.0); + model.addElement(sa1); + elements.addModelElement(sa1, new CellDesignerGenericProtein("s1")); + + Species sa2 = new GenericProtein("sa2"); + sa2.setX(1050.0); + sa2.setY(2050.0); + sa2.setWidth(300.0); + sa2.setHeight(450.0); + model.addElement(sa2); + elements.addModelElement(sa2, new CellDesignerGenericProtein("s2")); + + Species sa3 = new GenericProtein("sa3"); + sa3.setX(600.0); + sa3.setY(250.0); + sa3.setWidth(300.0); + sa3.setHeight(400.0); + model.addElement(sa3); + elements.addModelElement(sa3, new CellDesignerGenericProtein("s3")); + + Species sa4 = new GenericProtein("sa4"); + sa4.setX(550.0); + sa4.setY(350.0); + sa4.setWidth(300.0); + sa4.setHeight(450.0); + model.addElement(sa4); + elements.addElement(new CellDesignerGenericProtein("s4")); + + Species sa5 = new GenericProtein("sa5"); + sa5.setX(10.0); + sa5.setY(250.0); + sa5.setWidth(300.0); + sa5.setHeight(450.0); + model.addElement(sa5); + elements.addElement(new CellDesignerGenericProtein("s5")); + + Species sa6 = new GenericProtein("sa6"); + sa6.setX(10.0); + sa6.setY(250.0); + sa6.setWidth(300.0); + sa6.setHeight(450.0); + model.addElement(sa6); + + elements.addElement(new CellDesignerGenericProtein("s6")); + + Species sa10 = new GenericProtein("sa10"); + sa10.setX(210.0); + sa10.setY(220.0); + sa10.setWidth(320.0); + sa10.setHeight(250.0); + model.addElement(sa10); + elements.addElement(new CellDesignerGenericProtein("s10")); + + Species sa11 = new GenericProtein("sa11"); + sa11.setX(11.0); + sa11.setY(320.0); + sa11.setWidth(321.0); + sa11.setHeight(150.0); + model.addElement(sa11); + elements.addElement(new CellDesignerGenericProtein("s11")); + + Species sa12 = new GenericProtein("sa12"); + sa12.setX(12.0); + sa12.setY(20.0); + sa12.setWidth(321.0); + sa12.setHeight(150.0); + model.addElement(sa12); + elements.addElement(new CellDesignerGenericProtein("s12")); + + Species sa13 = new GenericProtein("sa13"); + sa13.setX(513.0); + sa13.setY(20.0); + sa13.setWidth(321.0); + sa13.setHeight(150.0); + model.addElement(sa13); + elements.addElement(new CellDesignerGenericProtein("s13")); + + Species sa14 = new GenericProtein("sa14"); + sa14.setX(14.0); + sa14.setY(820.0); + sa14.setWidth(321.0); + sa14.setHeight(150.0); + model.addElement(sa14); + elements.addElement(new CellDesignerGenericProtein("s14")); + + Species sa15 = new GenericProtein("sa15"); + sa15.setX(815.0); + sa15.setY(620.0); + sa15.setWidth(321.0); + sa15.setHeight(150.0); + model.addElement(sa15); + elements.addElement(new CellDesignerGenericProtein("s15")); + + return model; + } + + @Test + public void testLogiGateAndReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/logicGateAnd.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof BooleanLogicGateReaction); + assertEquals(1, reaction.getOperators().size()); + assertTrue(reaction.getOperators().get(0) instanceof AndOperator); + assertEquals(2, reaction.getReactants().size()); + assertEquals(1, reaction.getProducts().size()); + + assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); + + CellDesignerXmlParser cellDesignerXmlParser = new CellDesignerXmlParser(); + + model.setName(null); + String xmlString = cellDesignerXmlParser.toXml(model); + + ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); + Model model2 = cellDesignerXmlParser.createModel(new ConverterParams().inputStream(bais).sizeAutoAdjust(false)); + + ModelComparator mc = new ModelComparator(); + assertEquals("After CellDesigner xml serialization models are different", 0, mc.compare(model, model2)); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testLogiGateOrReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/logicGateOr.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof BooleanLogicGateReaction); + assertEquals(1, reaction.getOperators().size()); + assertTrue(reaction.getOperators().get(0) instanceof OrOperator); + assertEquals(2, reaction.getReactants().size()); + assertEquals(1, reaction.getProducts().size()); + + assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testLogiGateNotReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/logicGateNot.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof BooleanLogicGateReaction); + assertEquals(1, reaction.getOperators().size()); + assertTrue(reaction.getOperators().get(0) instanceof NandOperator); + assertEquals(2, reaction.getReactants().size()); + assertEquals(1, reaction.getProducts().size()); + + assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testLogiGateUnknownReaction() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/reactions/logicGateUnknown.xml"); + assertEquals(1, model.getReactions().size()); + Reaction reaction = model.getReactions().iterator().next(); + assertTrue(reaction instanceof BooleanLogicGateReaction); + assertEquals(1, reaction.getOperators().size()); + assertTrue(reaction.getOperators().get(0) instanceof UnknownOperator); + assertEquals(2, reaction.getReactants().size()); + assertEquals(1, reaction.getProducts().size()); + + assertEquals(0.0, reaction.getReactants().get(0).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getReactants().get(1).getLine().getEndPoint().distance(200.0, 127.0), EPSILON); + assertEquals(0.0, reaction.getProducts().get(0).getLine().getBeginPoint().distance(200.0, 127.0), EPSILON); + + // NormalImageGenerator generator = new NormalImageGenerator(1, 0, 0, + // 1024, 1024, model, true, false, 0, false); + // generator.saveToPNG("tmp.png"); + // Desktop.getDesktop().open(new File("tmp.png")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProblematicDrawing() throws Exception { + Model model; + try { + model = getModelForFile("testFiles/problematic/reaction_drawing_problem.xml"); + Reaction reaction = model.getReactionByReactionId("re1"); + assertEquals(0, reaction.getCenterPoint().distance(new Point2D.Double(258.5, 145.0)), EPSILON); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testProteinsInsideComplex() throws Exception { + try { + Model model = getModelForFile("testFiles/problematic/proteins_inside_complex.xml"); + for (Element el : model.getElements()) { + assertFalse(el.getClass().equals(Protein.class)); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testKinetcs() throws Exception { + try { + Model model = getModelForFile("testFiles/reactions/kinetics.xml"); + Reaction reaction = model.getReactionByReactionId("re1"); + assertNotNull(reaction.getKinetics()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } } diff --git a/converter-CellDesigner/testFiles/kinetics/math_using_one_element.xml b/converter-CellDesigner/testFiles/kinetics/math_using_one_element.xml new file mode 100644 index 0000000000000000000000000000000000000000..0baf8cdf3d070a5c16b9616b0e3d7d30d1d79055 --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/math_using_one_element.xml @@ -0,0 +1,10 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> s1 </ci> +<ci> s1 </ci> +</apply> +</math> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/kinetics/simple.xml b/converter-CellDesigner/testFiles/kinetics/simple.xml new file mode 100644 index 0000000000000000000000000000000000000000..04b70999ab504be90ce14cf4a0ceea1310020ed0 --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/simple.xml @@ -0,0 +1,10 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> s1 </ci> +<ci> s2 </ci> +</apply> +</math> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/kinetics/with_function.xml b/converter-CellDesigner/testFiles/kinetics/with_function.xml new file mode 100644 index 0000000000000000000000000000000000000000..5bf367b2ce89c1a4f3a787fa8d73427bf8671bec --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/with_function.xml @@ -0,0 +1,10 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> fun </ci> +<ci> s2 </ci> +</apply> +</math> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/kinetics/with_global_parameter.xml b/converter-CellDesigner/testFiles/kinetics/with_global_parameter.xml new file mode 100644 index 0000000000000000000000000000000000000000..6d30ed0d2b54c9823e52e463825e1912bc2535ef --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/with_global_parameter.xml @@ -0,0 +1,10 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> k1 </ci> +<ci> s2 </ci> +</apply> +</math> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/kinetics/with_parameter.xml b/converter-CellDesigner/testFiles/kinetics/with_parameter.xml new file mode 100644 index 0000000000000000000000000000000000000000..a41be8921785734fc1678d55df9b9fc46fcb4d29 --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/with_parameter.xml @@ -0,0 +1,13 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> s1 </ci> +<ci> s2 </ci> +</apply> +</math> +<listOfParameters> +<parameter metaid="k2" id="k2" name="local param" value="0" units="substance"/> +</listOfParameters> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/kinetics/with_used_parameter.xml b/converter-CellDesigner/testFiles/kinetics/with_used_parameter.xml new file mode 100644 index 0000000000000000000000000000000000000000..2d6c40edec115a8f086cd550eadb825c435698ba --- /dev/null +++ b/converter-CellDesigner/testFiles/kinetics/with_used_parameter.xml @@ -0,0 +1,13 @@ +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> s1 </ci> +<ci> k2 </ci> +</apply> +</math> +<listOfParameters> +<parameter metaid="k2" id="k2" name="local param" value="0" units="substance"/> +</listOfParameters> +</kineticLaw> diff --git a/converter-CellDesigner/testFiles/reactions/kinetics.xml b/converter-CellDesigner/testFiles/reactions/kinetics.xml new file mode 100644 index 0000000000000000000000000000000000000000..6402113c49a73f3d03a1cae3412c1d173b163235 --- /dev/null +++ b/converter-CellDesigner/testFiles/reactions/kinetics.xml @@ -0,0 +1,170 @@ +<?xml version="1.0" encoding="UTF-8"?> +<sbml xmlns="http://www.sbml.org/sbml/level2/version4" xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" level="2" version="4"> +<model metaid="untitled" id="untitled"> +<annotation> +<celldesigner:extension> +<celldesigner:modelVersion>4.0</celldesigner:modelVersion> +<celldesigner:modelDisplay sizeX="600" sizeY="400"/> +<celldesigner:listOfCompartmentAliases/> +<celldesigner:listOfComplexSpeciesAliases/> +<celldesigner:listOfSpeciesAliases> +<celldesigner:speciesAlias id="sa1" species="s1"> +<celldesigner:activity>inactive</celldesigner:activity> +<celldesigner:bounds x="67.0" y="151.0" w="80.0" h="40.0"/> +<celldesigner:font size="12"/> +<celldesigner:view state="usual"/> +<celldesigner:usualView> +<celldesigner:innerPosition x="0.0" y="0.0"/> +<celldesigner:boxSize width="80.0" height="40.0"/> +<celldesigner:singleLine width="1.0"/> +<celldesigner:paint color="ffccffcc" scheme="Color"/> +</celldesigner:usualView> +<celldesigner:briefView> +<celldesigner:innerPosition x="0.0" y="0.0"/> +<celldesigner:boxSize width="80.0" height="60.0"/> +<celldesigner:singleLine width="0.0"/> +<celldesigner:paint color="3fff0000" scheme="Color"/> +</celldesigner:briefView> +<celldesigner:info state="empty" angle="-1.5707963267948966"/> +</celldesigner:speciesAlias> +<celldesigner:speciesAlias id="sa2" species="s2"> +<celldesigner:activity>inactive</celldesigner:activity> +<celldesigner:bounds x="296.0" y="174.0" w="80.0" h="40.0"/> +<celldesigner:font size="12"/> +<celldesigner:view state="usual"/> +<celldesigner:usualView> +<celldesigner:innerPosition x="0.0" y="0.0"/> +<celldesigner:boxSize width="80.0" height="40.0"/> +<celldesigner:singleLine width="1.0"/> +<celldesigner:paint color="ffccffcc" scheme="Color"/> +</celldesigner:usualView> +<celldesigner:briefView> +<celldesigner:innerPosition x="0.0" y="0.0"/> +<celldesigner:boxSize width="80.0" height="60.0"/> +<celldesigner:singleLine width="0.0"/> +<celldesigner:paint color="3fff0000" scheme="Color"/> +</celldesigner:briefView> +<celldesigner:info state="empty" angle="-1.5707963267948966"/> +</celldesigner:speciesAlias> +</celldesigner:listOfSpeciesAliases> +<celldesigner:listOfGroups/> +<celldesigner:listOfProteins> +<celldesigner:protein id="pr1" name="s1" type="GENERIC"/> +<celldesigner:protein id="pr2" name="s2" type="GENERIC"/> +</celldesigner:listOfProteins> +<celldesigner:listOfGenes/> +<celldesigner:listOfRNAs/> +<celldesigner:listOfAntisenseRNAs/> +<celldesigner:listOfLayers/> +<celldesigner:listOfBlockDiagrams/> +</celldesigner:extension> +</annotation> +<listOfUnitDefinitions> +<unitDefinition metaid="substance" id="substance" name="substance"> +<listOfUnits> +<unit metaid="CDMT00004" kind="mole"/> +</listOfUnits> +</unitDefinition> +<unitDefinition metaid="volume" id="volume" name="volume"> +<listOfUnits> +<unit metaid="CDMT00005" kind="litre"/> +</listOfUnits> +</unitDefinition> +<unitDefinition metaid="area" id="area" name="area"> +<listOfUnits> +<unit metaid="CDMT00006" kind="metre" exponent="2"/> +</listOfUnits> +</unitDefinition> +<unitDefinition metaid="length" id="length" name="length"> +<listOfUnits> +<unit metaid="CDMT00007" kind="metre"/> +</listOfUnits> +</unitDefinition> +<unitDefinition metaid="time" id="time" name="time"> +<listOfUnits> +<unit metaid="CDMT00008" kind="second"/> +</listOfUnits> +</unitDefinition> +</listOfUnitDefinitions> +<listOfCompartments> +<compartment metaid="default" id="default" size="1" units="volume"/> +</listOfCompartments> +<listOfSpecies> +<species metaid="s1" id="s1" name="s1" compartment="default" initialAmount="0"> +<annotation> +<celldesigner:extension> +<celldesigner:positionToCompartment>inside</celldesigner:positionToCompartment> +<celldesigner:speciesIdentity> +<celldesigner:class>PROTEIN</celldesigner:class> +<celldesigner:proteinReference>pr1</celldesigner:proteinReference> +</celldesigner:speciesIdentity> +</celldesigner:extension> +</annotation> +</species> +<species metaid="s2" id="s2" name="s2" compartment="default" initialAmount="0"> +<annotation> +<celldesigner:extension> +<celldesigner:positionToCompartment>inside</celldesigner:positionToCompartment> +<celldesigner:speciesIdentity> +<celldesigner:class>PROTEIN</celldesigner:class> +<celldesigner:proteinReference>pr2</celldesigner:proteinReference> +</celldesigner:speciesIdentity> +</celldesigner:extension> +</annotation> +</species> +</listOfSpecies> +<listOfReactions> +<reaction metaid="re1" id="re1" reversible="false" fast="false"> +<annotation> +<celldesigner:extension> +<celldesigner:reactionType>STATE_TRANSITION</celldesigner:reactionType> +<celldesigner:baseReactants> +<celldesigner:baseReactant species="s1" alias="sa1"> +<celldesigner:linkAnchor position="INACTIVE"/> +</celldesigner:baseReactant> +</celldesigner:baseReactants> +<celldesigner:baseProducts> +<celldesigner:baseProduct species="s2" alias="sa2"> +<celldesigner:linkAnchor position="INACTIVE"/> +</celldesigner:baseProduct> +</celldesigner:baseProducts> +<celldesigner:connectScheme connectPolicy="direct" rectangleIndex="0"> +<celldesigner:listOfLineDirection> +<celldesigner:lineDirection index="0" value="unknown"/> +</celldesigner:listOfLineDirection> +</celldesigner:connectScheme> +<celldesigner:line width="1.0" color="ff000000"/> +</celldesigner:extension> +</annotation> +<listOfReactants> +<speciesReference metaid="CDMT00001" species="s1"> +<annotation> +<celldesigner:extension> +<celldesigner:alias>sa1</celldesigner:alias> +</celldesigner:extension> +</annotation> +</speciesReference> +</listOfReactants> +<listOfProducts> +<speciesReference metaid="CDMT00002" species="s2"> +<annotation> +<celldesigner:extension> +<celldesigner:alias>sa2</celldesigner:alias> +</celldesigner:extension> +</annotation> +</speciesReference> +</listOfProducts> +<kineticLaw metaid="CDMT00003"> +<annotation/> +<math xmlns="http://www.w3.org/1998/Math/MathML"> +<apply> +<plus/> +<ci> s1 </ci> +<ci> s2 </ci> +</apply> +</math> +</kineticLaw> +</reaction> +</listOfReactions> +</model> +</sbml> diff --git a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlArgument.java b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlArgument.java new file mode 100644 index 0000000000000000000000000000000000000000..106cc227f08a6d5c2cba955902d210fe039dcfc9 --- /dev/null +++ b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlArgument.java @@ -0,0 +1,5 @@ +package lcsb.mapviewer.model.map.kinetics; + +public interface SbmlArgument { + +} diff --git a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java index f9e067bae5fa6149bd4151b29694fbfc40c80185..4b7a71ad3812c18a195dd0c6d21a8487f314ae2c 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java @@ -28,7 +28,7 @@ import org.hibernate.annotations.IndexColumn; @Table(name = "sbml_function") @org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment") @XmlRootElement -public class SbmlFunction implements Serializable { +public class SbmlFunction implements Serializable, SbmlArgument { /** * Default class logger. diff --git a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlKinetics.java b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlKinetics.java new file mode 100644 index 0000000000000000000000000000000000000000..9a4f29f710663a9d7c9a4b2ae05db811ca157649 --- /dev/null +++ b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlKinetics.java @@ -0,0 +1,145 @@ +package lcsb.mapviewer.model.map.kinetics; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.log4j.Logger; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +import lcsb.mapviewer.model.map.species.Element; + +/** + * Representation of a single SBML function + * + * @author Piotr Gawron + * + */ +@Entity +@Table(name = "sbml_kinetics") +@org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment") +@XmlRootElement +public class SbmlKinetics implements Serializable { + + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private static Logger logger = Logger.getLogger(SbmlKinetics.class); + + /** + * + */ + private static final long serialVersionUID = 1L; + + @Cascade({ CascadeType.ALL }) + @OneToMany(mappedBy = "model", orphanRemoval = true) + private Set<SbmlParameter> parameters = new HashSet<>(); + + @Cascade({ CascadeType.ALL }) + @OneToMany(mappedBy = "model", orphanRemoval = true) + private Set<SbmlFunction> functions = new HashSet<>(); + + private List<Element> elements = new ArrayList<>(); + + /** + * Unique database identifier. + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "idDb", unique = true, nullable = false) + private int id; + + @Column(columnDefinition = "TEXT") + private String definition; + + public void addParameter(SbmlParameter parameter) { + parameters.add(parameter); + } + + public Set<SbmlParameter> getParameters() { + return parameters; + } + + public List<SbmlArgument> getArguments() { + List<SbmlArgument> arguments = new ArrayList<>(); + arguments.addAll(parameters); + arguments.addAll(elements); + arguments.addAll(functions); + return arguments; + } + + public void addElement(Element element) { + this.elements.add(element); + } + + public String getDefinition() { + return definition; + } + + public void setDefinition(String definition) { + this.definition = definition; + } + + public void addElements(Set<Element> elements) { + for (Element element : elements) { + addElement(element); + } + } + + public void addParameters(Set<SbmlParameter> parameters) { + for (SbmlParameter parameter : parameters) { + addParameter(parameter); + } + + } + + public void addArguments(Set<SbmlArgument> elementsUsedInKinetics) { + for (SbmlArgument argument : elementsUsedInKinetics) { + addArgument(argument); + } + + } + + public void addArgument(SbmlArgument argument) { + if (argument instanceof SbmlParameter) { + addParameter((SbmlParameter) argument); + } else if (argument instanceof SbmlFunction) { + addFunction((SbmlFunction) argument); + } else { + addElement((Element) argument); + } + + } + + public void addFunction(SbmlFunction argument) { + functions.add(argument); + } + + public SbmlParameter getParameterById(String id) { + for (SbmlParameter parameter : parameters) { + if (parameter.getParameterId().equals(id)) { + return parameter; + } + } + return null; + } + + public Set<SbmlFunction> getFunctions() { + return functions; + } + +} diff --git a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlParameter.java b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlParameter.java index 7684fc2f29f15772d4955d440d8442f6ec79e08e..2f8bd963053c1df5867879a4a2f00971a93694ed 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlParameter.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlParameter.java @@ -22,7 +22,7 @@ import org.apache.log4j.Logger; @Table(name = "sbml_parameter") @org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment") @XmlRootElement -public class SbmlParameter implements Serializable { +public class SbmlParameter implements Serializable, SbmlArgument { /** * Default class logger. diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java b/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java index 81ac64e4b5f69d112e7e4233f69db8fa2a7485a8..8a2bb71c2e8b3afdb4e8146548fa511a5fe72864 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java @@ -10,6 +10,7 @@ import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.compartment.Compartment; import lcsb.mapviewer.model.map.graph.DataMiningSet; +import lcsb.mapviewer.model.map.kinetics.SbmlArgument; import lcsb.mapviewer.model.map.kinetics.SbmlFunction; import lcsb.mapviewer.model.map.kinetics.SbmlParameter; import lcsb.mapviewer.model.map.kinetics.SbmlUnit; @@ -607,4 +608,12 @@ public interface Model { SbmlUnit getUnitsByUnitId(String unitId); void addParameters(Collection<SbmlParameter> sbmlParamters); + + void addParameter(SbmlParameter sbmlParameter); + + SbmlParameter getParameterById(String parameterId); + + void addFunction(SbmlFunction sbmlFunction); + + SbmlArgument getFunctionById(String id); } diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelData.java b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelData.java index 3970c18f012b0cc61618d0e30c619f1d92bf95d2..f0644e224121c25051a91224fae3dec166ca94fc 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelData.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelData.java @@ -774,7 +774,15 @@ public class ModelData implements Serializable { this.units.add(unit); } - public void addParameters(Collection<SbmlParameter> sbmlParamters) { - this.parameters.addAll(sbmlParamters); + public void addParameters(Collection<SbmlParameter> sbmlParameters) { + this.parameters.addAll(sbmlParameters); + } + + public void addParameter(SbmlParameter sbmlParameter) { + this.parameters.add(sbmlParameter); + } + + public void addFunction(SbmlFunction sbmlFunction) { + this.functions.add(sbmlFunction); } } \ No newline at end of file diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java index 6cfd3c14a696d8d8821d601bd433571faa646a45..9d32b9e7ace09a41c7705284aa2c0d9c97e20a98 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java @@ -18,6 +18,7 @@ import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.compartment.Compartment; import lcsb.mapviewer.model.map.graph.DataMiningSet; +import lcsb.mapviewer.model.map.kinetics.SbmlArgument; import lcsb.mapviewer.model.map.kinetics.SbmlFunction; import lcsb.mapviewer.model.map.kinetics.SbmlParameter; import lcsb.mapviewer.model.map.kinetics.SbmlUnit; @@ -698,7 +699,37 @@ public class ModelFullIndexed implements Model { } @Override - public void addParameters(Collection<SbmlParameter> sbmlParamters) { - modelData.addParameters(sbmlParamters); + public void addParameters(Collection<SbmlParameter> sbmlParameters) { + modelData.addParameters(sbmlParameters); + } + + @Override + public void addParameter(SbmlParameter sbmlParameter) { + modelData.addParameter(sbmlParameter); + } + + @Override + public SbmlParameter getParameterById(String parameterId) { + for (SbmlParameter parameter : getParameters()) { + if (parameter.getParameterId().equals(parameterId)) { + return parameter; + } + } + return null; + } + + @Override + public void addFunction(SbmlFunction sbmlFunction) { + modelData.addFunction(sbmlFunction); + } + + @Override + public SbmlFunction getFunctionById(String functionId) { + for (SbmlFunction function : getFunctions()) { + if (function.getFunctionId().equals(functionId)) { + return function; + } + } + return null; } } diff --git a/model/src/main/java/lcsb/mapviewer/model/map/reaction/Reaction.java b/model/src/main/java/lcsb/mapviewer/model/map/reaction/Reaction.java index eb6e6ccac3b9756e27a3a55be425cc93dc8a2555..4ce26dd3aae54d82f9ed3d43a2c90e3d35679804 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/reaction/Reaction.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/reaction/Reaction.java @@ -40,6 +40,7 @@ import lcsb.mapviewer.common.exception.NotImplementedException; import lcsb.mapviewer.common.geometry.LineTransformation; import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.MiriamData; +import lcsb.mapviewer.model.map.kinetics.SbmlKinetics; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.model.ModelData; import lcsb.mapviewer.model.map.reaction.type.ReactionRect; @@ -135,11 +136,6 @@ public class Reaction implements BioEntity { */ private boolean reversible = false; - /** - * Is there a kinetic low describing this reaction. - */ - private boolean kineticLaw = false; - /** * Symbol of the reaction (RECON). */ @@ -212,6 +208,8 @@ public class Reaction implements BioEntity { @ManyToOne(fetch = FetchType.LAZY) private ModelData model; + private SbmlKinetics kinetics; + /** * Default constructor. */ @@ -236,7 +234,6 @@ public class Reaction implements BioEntity { idReaction = original.getIdReaction(); name = original.getName(); reversible = original.reversible; - kineticLaw = original.kineticLaw; miriamDataSet = new HashSet<>(); for (MiriamData md : original.getMiriamData()) { miriamDataSet.add(new MiriamData(md)); @@ -666,23 +663,6 @@ public class Reaction implements BioEntity { this.reversible = reversible; } - /** - * @return the kineticLaw - * @see #kineticLaw - */ - public boolean isKineticLaw() { - return kineticLaw; - } - - /** - * @param kineticLaw - * the kineticLaw to set - * @see #kineticLaw - */ - public void setKineticLaw(boolean kineticLaw) { - this.kineticLaw = kineticLaw; - } - @Override public Set<MiriamData> getMiriamData() { return miriamDataSet; @@ -889,4 +869,12 @@ public class Reaction implements BioEntity { } } + + public SbmlKinetics getKinetics() { + return kinetics; + } + + public void setKinetics(SbmlKinetics kinetics) { + this.kinetics = kinetics; + } } diff --git a/model/src/main/java/lcsb/mapviewer/model/map/reaction/ReactionComparator.java b/model/src/main/java/lcsb/mapviewer/model/map/reaction/ReactionComparator.java index 230b8b08dfe9d2a01175d9e1eee87f3e755027e2..78691472086ea094781e84009280198703156401 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/reaction/ReactionComparator.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/reaction/ReactionComparator.java @@ -105,11 +105,6 @@ public class ReactionComparator implements Comparator<Reaction> { return booleanComparator.compare(arg0.isReversible(), arg1.isReversible()); } - if (booleanComparator.compare(arg0.isKineticLaw(), arg1.isKineticLaw()) != 0) { - logger.debug("KineticLaw different: " + arg0.isKineticLaw() + ", " + arg1.isKineticLaw()); - return booleanComparator.compare(arg0.isKineticLaw(), arg1.isKineticLaw()); - } - if (stringComparator.compare(arg0.getSymbol(), arg1.getSymbol()) != 0) { logger.debug("Symbol different: " + arg0.getSymbol() + ", " + arg1.getSymbol()); return stringComparator.compare(arg0.getSymbol(), arg1.getSymbol()); diff --git a/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java b/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java index a7e4a20b82e8c6306d90ad3b1e7dcc8a5c8235ad..190f1c84fbddd39e0b7d18f7b9436b63713e5807 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java @@ -43,6 +43,7 @@ import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.SearchIndex; import lcsb.mapviewer.model.map.compartment.Compartment; import lcsb.mapviewer.model.map.graph.DataMining; +import lcsb.mapviewer.model.map.kinetics.SbmlArgument; import lcsb.mapviewer.model.map.layout.graphics.LayerText; import lcsb.mapviewer.model.map.model.ElementSubmodelConnection; import lcsb.mapviewer.model.map.model.Model; @@ -61,7 +62,7 @@ import lcsb.mapviewer.model.map.model.ModelData; @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "element_type_db", discriminatorType = DiscriminatorType.STRING) @DiscriminatorValue("GENERIC_ALIAS") -public abstract class Element implements BioEntity, Serializable { +public abstract class Element implements BioEntity, Serializable, SbmlArgument { /** * diff --git a/model/src/test/java/lcsb/mapviewer/model/map/kinetics/AllKineticsTests.java b/model/src/test/java/lcsb/mapviewer/model/map/kinetics/AllKineticsTests.java index b699ac87b8b6a7e439ab8ec92892b438bb1d16f2..d97dc565279aadcf3329ea4cc9361fc9c4d7d848 100644 --- a/model/src/test/java/lcsb/mapviewer/model/map/kinetics/AllKineticsTests.java +++ b/model/src/test/java/lcsb/mapviewer/model/map/kinetics/AllKineticsTests.java @@ -5,7 +5,11 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({ SbmlFunctionTest.class, SbmlParameterTest.class, SbmlUnitTest.class, SbmlUnitTypeFactorTest.class }) +@SuiteClasses({ SbmlFunctionTest.class, // + SbmlKinetics.class, // + SbmlParameterTest.class, // + SbmlUnitTest.class, // + SbmlUnitTypeFactorTest.class }) public class AllKineticsTests { } diff --git a/model/src/test/java/lcsb/mapviewer/model/map/kinetics/SbmlKineticsTest.java b/model/src/test/java/lcsb/mapviewer/model/map/kinetics/SbmlKineticsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..573f9506d1d50d302ac1e2abb31df4f159fefcb1 --- /dev/null +++ b/model/src/test/java/lcsb/mapviewer/model/map/kinetics/SbmlKineticsTest.java @@ -0,0 +1,58 @@ +package lcsb.mapviewer.model.map.kinetics; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.GenericProtein; + +public class SbmlKineticsTest { + + @Test + public void testAddParameter() { + SbmlParameter parameter = new SbmlParameter(""); + SbmlKinetics kinetics = new SbmlKinetics(); + kinetics.addParameter(parameter); + assertEquals(kinetics.getParameters().iterator().next(), parameter); + } + + @Test + public void testAddFunction() { + SbmlFunction function = new SbmlFunction(""); + SbmlKinetics kinetics = new SbmlKinetics(); + kinetics.addFunction(function); + assertEquals(1, kinetics.getFunctions().size()); + assertEquals(1, kinetics.getArguments().size()); + assertEquals(kinetics.getArguments().iterator().next(), function); + } + + @Test + public void testGetArguments() { + SbmlParameter parameter = new SbmlParameter("k1"); + SbmlKinetics kinetics = new SbmlKinetics(); + kinetics.addParameter(parameter); + assertTrue(kinetics.getArguments().contains(parameter)); + } + + @Test + public void testAddElement() { + Element protein = new GenericProtein("s1"); + + SbmlKinetics kinetics = new SbmlKinetics(); + kinetics.addElement(protein); + assertEquals(kinetics.getArguments().get(0), protein); + } + + @Test + public void testSetDefinition() { + String definition = "<lambda><bvar><ci> x </ci></bvar></lambda>"; + + SbmlKinetics kinetics = new SbmlKinetics(); + kinetics.setDefinition(definition); + assertEquals(definition, kinetics.getDefinition()); + } + + +} diff --git a/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionComparatorTest.java b/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionComparatorTest.java index fbd1794bdeb5229886309bb46c3d66ef2f21f415..5f1f3ee27934a0111f3a32147e665a079c21e756 100644 --- a/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionComparatorTest.java +++ b/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionComparatorTest.java @@ -69,13 +69,6 @@ public class ReactionComparatorTest { reaction1 = createReaction(); reaction2 = createReaction(); - reaction1.setKineticLaw(true); - assertTrue(comparator.compare(reaction1, reaction2) != 0); - assertTrue(comparator.compare(reaction2, reaction1) != 0); - - reaction1 = createReaction(); - reaction2 = createReaction(); - reaction1.setSymbol("a"); assertTrue(comparator.compare(reaction1, reaction2) != 0); assertTrue(comparator.compare(reaction2, reaction1) != 0); diff --git a/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionTest.java b/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionTest.java index ba848cd6e67581dbfadf02446e4909293ef6c887..aa53199703a30f5f0e014af4166f89aa7cbeed29 100644 --- a/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionTest.java +++ b/model/src/test/java/lcsb/mapviewer/model/map/reaction/ReactionTest.java @@ -192,8 +192,6 @@ public class ReactionTest extends ModelTestFunctions { reaction1.setId(id); reaction1.setNodes(nodes); reaction1.setNotes(notes); - reaction1.setKineticLaw(kineticLaw); - assertEquals(kineticLaw, reaction1.isKineticLaw()); reaction1.setModel(model); assertEquals(model, reaction1.getModel()); reaction1.setModelData(data); diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java b/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java index 4eb421f72825deeadc2c337314283ce342af13df..a2a369d7114ec1686ddf67be0b749ebaced353d9 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java @@ -611,8 +611,6 @@ public class ExporterService implements IExporterService { result.append("\t"); result.append("reversible=" + reaction.isReversible()); result.append("\t"); - result.append("kineticLaw=" + reaction.isKineticLaw()); - result.append("\t"); if (reaction.getMiriamData().size() > 0) { result.append("REACTION_MIRIAM_DATA"); result.append("\t");