Commit af954491 authored by piotr.gawron's avatar piotr.gawron
Browse files

partial working new model:

model refactoried, celldesigner parser passes tests

the rest stil to be done
parent 2e02f011
......@@ -25,6 +25,7 @@ import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.layout.alias.Alias;
import lcsb.mapviewer.model.map.layout.alias.ChemicalAlias;
import lcsb.mapviewer.model.map.species.Chemical;
import lcsb.mapviewer.modelutils.map.ElementUtils;
import uk.ac.ebi.chebi.webapps.chebiWS.client.ChebiWebServiceClient;
......@@ -458,7 +459,7 @@ public class ChebiAnnotator extends ElementAnnotator implements IExternalService
chebi = getChebiElementForChebiId(md);
}
}
Chemical species = (Chemical) ((Alias) element).getElement();
ChemicalAlias species = (ChemicalAlias) element;
if (chebi != null) {
super.setFullName(species, chebi.getName(), warnPrefix);
......
package lcsb.mapviewer.converter.model.celldesigner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.Element;
import lcsb.mapviewer.model.map.agregator.Compartment;
import lcsb.mapviewer.model.map.layout.alias.Alias;
import lcsb.mapviewer.model.map.layout.alias.SpeciesAlias;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.modelutils.map.ElementUtils;
public class CellDesignerElementCollection {
ElementUtils eu = new ElementUtils();
Map<String, Element> elementById = new HashMap<>();
public Species getSpeciesBySpeciesId(String speciesId) {
return (Species) elementById.get(speciesId);
}
public Compartment getCompartmentByCompartmentId(String compartmentId) {
return (Compartment) elementById.get(compartmentId);
}
public String getElementId(Alias alias) {
// TODO
// silly implementation for now (if CD won't crash than we can keep it)
return "s_id_" + alias.getAliasId();
}
public void addElements(List<? extends Element> elements) {
for (Element element : elements) {
addElement(element);
}
}
public void addElement(Element element) {
addElement(element, element.getElementId());
}
private void addElement(Element element, String id) {
if (elementById.get(id) != null) {
throw new InvalidArgumentException(eu.getElementTag(element) + "Element with given id alread exists. ID: " + id);
}
elementById.put(id, element);
}
public void addAlias(SpeciesAlias alias, Element element) {
addElement(element, getElementId(alias));
if (getSpeciesBySpeciesId(element.getElementId()) == null) {
addElement(element);
}
}
}
......@@ -34,12 +34,15 @@ import lcsb.mapviewer.converter.model.celldesigner.reaction.UnknownReactionClass
import lcsb.mapviewer.converter.model.celldesigner.species.InternalModelSpeciesData;
import lcsb.mapviewer.converter.model.celldesigner.species.SpeciesCollectionXmlParser;
import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.map.Element;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.agregator.Compartment;
import lcsb.mapviewer.model.map.layout.alias.Alias;
import lcsb.mapviewer.model.map.layout.alias.AntisenseRnaAlias;
import lcsb.mapviewer.model.map.layout.alias.CompartmentAlias;
import lcsb.mapviewer.model.map.layout.alias.ComplexAlias;
import lcsb.mapviewer.model.map.layout.alias.GeneAlias;
import lcsb.mapviewer.model.map.layout.alias.ProteinAlias;
import lcsb.mapviewer.model.map.layout.alias.RnaAlias;
import lcsb.mapviewer.model.map.layout.alias.SpeciesAlias;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.layout.graphics.LayerOval;
......@@ -48,10 +51,6 @@ import lcsb.mapviewer.model.map.layout.graphics.LayerText;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.AntisenseRna;
import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.Protein;
import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.model.map.species.Species;
/**
......@@ -78,17 +77,17 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(CellDesignerXmlParser.class.getName());
private static Logger logger = Logger.getLogger(CellDesignerXmlParser.class.getName());
/**
* CellDesigner parser for layers.
*/
private LayerXmlParser layerParser = new LayerXmlParser();
private LayerXmlParser layerParser = new LayerXmlParser();
/**
* CellDesigner parser for compartments collections.
*/
private CompartmentCollectionXmlParser compartmentCollectionXmlParser = new CompartmentCollectionXmlParser();
private CompartmentCollectionXmlParser compartmentCollectionXmlParser;
/**
* CellDesigner parser for species collections.
......@@ -105,14 +104,18 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
EventStorageLoggerAppender appender = new EventStorageLoggerAppender();
try {
Logger.getRootLogger().addAppender(appender);
CellDesignerElementCollection elements = new CellDesignerElementCollection();
Model model = new ModelFullIndexed(null);
model.setSbgnFormat(params.isSbgnFormat());
if (params.getFilename() != null) {
model.setName(FilenameUtils.getBaseName(params.getFilename()));
}
speciesSbmlParser = new SpeciesCollectionXmlParser(model);
aliasCollectionParser = new AliasCollectionXmlParser(model);
speciesSbmlParser = new SpeciesCollectionXmlParser(model, elements);
aliasCollectionParser = new AliasCollectionXmlParser(elements, model);
compartmentCollectionXmlParser = new CompartmentCollectionXmlParser(elements);
DOMParser parser = new DOMParser();
try {
......@@ -143,7 +146,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
Node compartmentNode = getNode("listOfCompartments", modelNode.getChildNodes());
if (compartmentNode != null) {
List<Compartment> compartments = compartmentCollectionXmlParser.parseXmlCompartmentCollection(compartmentNode);
model.addElements(compartments);
elements.addElements(compartments);
}
InternalModelSpeciesData modelData = new InternalModelSpeciesData();
......@@ -188,7 +191,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
throw new InvalidInputDataExecption("No annotation node in SBML/model");
}
parseAnnotation(model, annotationNode, modelData);
parseAnnotation(model, annotationNode, modelData, elements);
if (speciesNode != null) {
List<Pair<String, ? extends Species>> species = speciesSbmlParser.parseSbmlSpeciesCollection(speciesNode);
......@@ -196,7 +199,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
}
if (reactionsNode != null) {
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements);
List<Reaction> reactions = reactionCollectionXmlParser.parseXmlReactionCollection(reactionsNode);
model.addReactions(reactions);
}
......@@ -349,8 +352,9 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
private void parseAnnotation(Model model, Node annotationNode, InternalModelSpeciesData modelData) throws InvalidXmlSchemaException {
SpeciesCollectionXmlParser parser = new SpeciesCollectionXmlParser(model);
private void parseAnnotation(Model model, Node annotationNode, InternalModelSpeciesData modelData, CellDesignerElementCollection elements)
throws InvalidXmlSchemaException {
SpeciesCollectionXmlParser parser = new SpeciesCollectionXmlParser(model, elements);
Node extensionNode = getNode("celldesigner:extension", annotationNode.getChildNodes());
if (extensionNode == null) {
......@@ -434,7 +438,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
for (Species species : modelData.getAll()) {
if (!species.getElementId().equals("")) {
model.addElement(species);
elements.addElement(species);
} else {
model.addCreationWarning(
"Species (class: " + species.getClass().getName() + ", name: " + species.getName()
......@@ -457,10 +461,6 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
parseAnnotationComplexAliasesConnections(model, getNode("celldesigner:listOfComplexSpeciesAliases", nodes));
}
if (includedSpecies != null) {
parser.addConnectionBetweenIncludedSpecies(includedSpecies);
}
if (listOfGroups != null) {
layerParser.parseGroups(model, listOfGroups);
}
......@@ -551,10 +551,11 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
* thrown when then model is invalid
*/
public String toXml(Model model) throws InconsistentModelException {
CellDesignerElementCollection elements = new CellDesignerElementCollection();
SpeciesCollectionXmlParser speciesCollectionXmlParser = new SpeciesCollectionXmlParser(model);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model);
aliasCollectionParser = new AliasCollectionXmlParser(model);
SpeciesCollectionXmlParser speciesCollectionXmlParser = new SpeciesCollectionXmlParser(model, elements);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements);
aliasCollectionParser = new AliasCollectionXmlParser(elements, model);
StringBuilder result = new StringBuilder();
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
......@@ -571,11 +572,13 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
result.append("</notes>");
}
result.append(annotationToXml(model));
result.append(annotationToXml(model, elements));
result.append(compartmentCollectionXmlParser.toXml(model.getCompartments()));
compartmentCollectionXmlParser = new CompartmentCollectionXmlParser(elements);
result.append(compartmentCollectionXmlParser.toXml(model.getCompartmentsAliases()));
result.append(speciesCollectionXmlParser.speciesCollectionToSbmlString(model.getSpecies()));
result.append(speciesCollectionXmlParser.speciesCollectionToSbmlString(model.getSpeciesAliases()));
result.append(reactionCollectionXmlParser.reactionCollectionToXmlString(model.getReactions()));
......@@ -591,8 +594,8 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
* model to transform
* @return annotation xml string for the model
*/
private String annotationToXml(Model model) {
SpeciesCollectionXmlParser speciesCollectionXmlParser = new SpeciesCollectionXmlParser(model);
private String annotationToXml(Model model, CellDesignerElementCollection elements) {
SpeciesCollectionXmlParser speciesCollectionXmlParser = new SpeciesCollectionXmlParser(model, elements);
StringBuilder result = new StringBuilder();
result.append("<annotation>\n");
......@@ -600,25 +603,25 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
result.append("<celldesigner:modelVersion>4.0</celldesigner:modelVersion>\n");
result.append("<celldesigner:modelDisplay sizeX=\"" + model.getWidth().intValue() + "\" sizeY=\"" + model.getHeight().intValue() + "\"/>\n");
result.append(speciesCollectionXmlParser.speciesCollectionToXmlIncludedString(model.getSpecies()));
result.append(speciesCollectionXmlParser.speciesCollectionToXmlIncludedString(model.getSpeciesAliases()));
result.append(aliasCollectionParser.compartmentAliasCollectionToXmlString(model.getCompartmentsAliases()));
result.append(aliasCollectionParser.complexAliasCollectionToXmlString(model.getComplexAliases()));
result.append(aliasCollectionParser.speciesAliasCollectionToXmlString(model.getSpeciesAliases()));
List<Protein> proteins = new ArrayList<Protein>();
List<Gene> genes = new ArrayList<Gene>();
List<Rna> rnas = new ArrayList<Rna>();
List<AntisenseRna> antisenseRnas = new ArrayList<AntisenseRna>();
for (Element element : model.getElements()) {
if (element instanceof Protein) {
proteins.add((Protein) element);
} else if (element instanceof Gene) {
genes.add((Gene) element);
} else if (element instanceof AntisenseRna) {
antisenseRnas.add((AntisenseRna) element);
} else if (element instanceof Rna) {
rnas.add((Rna) element);
result.append(aliasCollectionParser.speciesAliasCollectionToXmlString(model.getNotComplexSpeciesAliases()));
List<ProteinAlias> proteins = new ArrayList<>();
List<GeneAlias> genes = new ArrayList<>();
List<RnaAlias> rnas = new ArrayList<>();
List<AntisenseRnaAlias> antisenseRnas = new ArrayList<>();
for (Alias element : model.getAliases()) {
if (element instanceof ProteinAlias) {
proteins.add((ProteinAlias) element);
} else if (element instanceof GeneAlias) {
genes.add((GeneAlias) element);
} else if (element instanceof AntisenseRnaAlias) {
antisenseRnas.add((AntisenseRnaAlias) element);
} else if (element instanceof RnaAlias) {
rnas.add((RnaAlias) element);
}
}
......
package lcsb.mapviewer.converter.model.celldesigner.alias;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.CommonXmlParser;
import lcsb.mapviewer.model.map.layout.alias.Alias;
/**
* Generic abstract interface for parsing CellDesigner xml nodes with species
* definition.
*
* @author Piotr Gawron
*
* @param <T>
* type of the object to parse
*/
public abstract class AbstractAliasXmlParser<T extends Alias> extends XmlParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(AbstractAliasXmlParser.class.getName());
/**
* Set of common functions used in parsing cell designer xml.
*/
private CommonXmlParser commonParser = new CommonXmlParser();
/**
* Parse object from the xml node.
*
* @param node
* xml node to parse
*
* @return parsed object
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
abstract T parseXmlAlias(Node node) throws InvalidXmlSchemaException;
/**
* Parse object from the xml string.
*
* @param xmlString
* xml string
* @return parsed object
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
public T parseXmlAlias(String xmlString) throws InvalidXmlSchemaException {
Document doc = getXmlDocumentFromString(xmlString);
NodeList root = doc.getChildNodes();
return parseXmlAlias(root.item(0));
}
/**
* Method that transform object into CellDesigner xml.
*
* @param alias
* object to be transformed
* @return CellDesigner xml representation of the alias
*/
abstract String toXml(T alias);
/**
* @return the commonParser
*/
CommonXmlParser getCommonParser() {
return commonParser;
}
/**
* @param commonParser
* the commonParser to set
*/
void setCommonParser(CommonXmlParser commonParser) {
this.commonParser = commonParser;
}
}
package lcsb.mapviewer.converter.model.celldesigner.alias;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.CommonXmlParser;
import lcsb.mapviewer.model.map.Element;
import lcsb.mapviewer.model.map.layout.alias.Alias;
/**
* Generic abstract interface for parsing CellDesigner xml nodes with species
* definition.
*
* @author Piotr Gawron
*
* @param <T>
* type of the object to parse
*/
public abstract class AbstractAliasXmlParser<T extends Alias> extends XmlParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(AbstractAliasXmlParser.class.getName());
/**
* Set of common functions used in parsing cell designer xml.
*/
private CommonXmlParser commonParser = new CommonXmlParser();
/**
* Parse object from the xml node.
*
* @param node
* xml node to parse
*
* @return parsed object
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
abstract T parseXmlAlias(Node node) throws InvalidXmlSchemaException;
/**
* Parse object from the xml string.
*
* @param xmlString
* xml string
* @return parsed object
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
public T parseXmlAlias(String xmlString) throws InvalidXmlSchemaException {
Document doc = getXmlDocumentFromString(xmlString);
NodeList root = doc.getChildNodes();
return parseXmlAlias(root.item(0));
}
/**
* Method that transform object into CellDesigner xml.
*
* @param alias
* object to be transformed
* @return CellDesigner xml representation of the alias
*/
abstract String toXml(T alias);
/**
* @return the commonParser
*/
CommonXmlParser getCommonParser() {
return commonParser;
}
/**
* @param commonParser
* the commonParser to set
*/
void setCommonParser(CommonXmlParser commonParser) {
this.commonParser = commonParser;
}
}
package lcsb.mapviewer.converter.model.celldesigner.alias;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.model.map.layout.alias.ArtifitialCompartmentAlias;
import lcsb.mapviewer.model.map.layout.alias.CompartmentAlias;
import lcsb.mapviewer.model.map.layout.alias.ComplexAlias;
import lcsb.mapviewer.model.map.layout.alias.SpeciesAlias;
import lcsb.mapviewer.model.map.model.Model;
/**
* This class contains function to parse CellDesigner xml nodes containing
* collection of aliases. It also contains functionality to do reverse operation
* - transform set of aliases into xml node.
*
* @author Piotr Gawron
*
*/
public class AliasCollectionXmlParser extends XmlParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(AliasCollectionXmlParser.class.getName());
/**
* Single SpeciesAlias parser for CellDesigner node.
*/
private SpeciesAliasXmlParser speciesAliasParser = null;
/**
* Single ComplexAlias parser for CellDesigner node.
*/
private ComplexAliasXmlParser complexAliasParser = null;
/**
* Single CompartmentAlias parser for CellDesigner node.
*/
private CompartmentAliasXmlParser compartmentAliasParser = null;
/**
* Default parser. As an parameter model object for which parsing is done is
* required. This is due to the fact that CellDesigner model is very tangled
* and very often data are distributed in many different places and in other
* places they are missing...
*
* @param model
* map model for which parsing is performed
*/
public AliasCollectionXmlParser(Model model) {
speciesAliasParser = new SpeciesAliasXmlParser(model);
complexAliasParser = new ComplexAliasXmlParser(model);
compartmentAliasParser = new CompartmentAliasXmlParser(model);
}
/**
* Parses xml node containing list of species aliases into collection of
* SpeciesAlias.
*
* @param aliasListNode
* xml node to be parsed
* @return collection of SpeciesAlias obtained from xml node
* @throws InvalidXmlSchemaException
* thrown when there is a problem with xml
*/
public List<SpeciesAlias> parseXmlSpeciesAliasCollection(Node aliasListNode) throws InvalidXmlSchemaException {
List<SpeciesAlias> result = new ArrayList<SpeciesAlias>();
NodeList nodes = aliasListNode.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:speciesAlias")) {
SpeciesAlias alias = speciesAliasParser.parseXmlAlias(node);
result.add(alias);
} else {
throw new InvalidXmlSchemaException("Unknown element of celldesigner:listOfSpeciesAliases: " + node.getNodeName());
}
}
}
return result;
}