Commit 8a908bbf authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch 'refactor' into 'master'

Refactoring

issues #9, #10, #12

See merge request !3
parents d47a3508 5304f825
......@@ -28,10 +28,12 @@ import lcsb.mapviewer.annotation.services.annotators.UniprotAnnotator;
import lcsb.mapviewer.common.EventStorageLoggerAppender;
import lcsb.mapviewer.common.IProgressUpdater;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
......@@ -165,13 +167,13 @@ public class ModelAnnotator {
/**
* Performs all possible and automatic annotations on the model.
*
* @param model
* model to update
* @param project
* project to update
* @param progressUpdater
* callback function used for updating progress of the function
*/
public void performAnnotations(Model model, final IProgressUpdater progressUpdater) {
performAnnotations(model, progressUpdater, null);
public void performAnnotations(Project project, final IProgressUpdater progressUpdater) {
performAnnotations(project, progressUpdater, null);
}
/**
......@@ -180,19 +182,21 @@ public class ModelAnnotator {
* @param annotators
* this map contains lists of {@link ElementAnnotator} objects that
* should be used for a given classes
* @param model
* model to update
* @param project
* project with models to update
* @param progressUpdater
* callback function used for updating progress of the function
*/
public void performAnnotations(Model model, final IProgressUpdater progressUpdater, Map<Class<?>, List<ElementAnnotator>> annotators) {
public void performAnnotations(Project project, final IProgressUpdater progressUpdater, Map<Class<?>, List<ElementAnnotator>> annotators) {
EventStorageLoggerAppender appender = new EventStorageLoggerAppender();
try {
Logger.getRootLogger().addAppender(appender);
progressUpdater.setProgress(0);
List<Model> models = new ArrayList<Model>();
models.add(model);
models.addAll(model.getSubmodels());
List<Model> models = new ArrayList<>();
for (ModelData model: project.getModels()) {
models.add(model.getModel());
models.addAll(model.getModel().getSubmodels());
}
final double size = models.size();
double counter = 0;
for (Model m : models) {
......@@ -215,7 +219,7 @@ public class ModelAnnotator {
counter++;
}
Logger.getRootLogger().removeAppender(appender);
model.addLoggingInfo(appender);
project.addLoggingInfo(appender);
} finally {
Logger.getRootLogger().removeAppender(appender);
}
......
......@@ -29,6 +29,7 @@ import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.annotation.services.annotators.ElementAnnotator;
import lcsb.mapviewer.annotation.services.annotators.ReconAnnotator;
import lcsb.mapviewer.common.IProgressUpdater;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType;
......@@ -311,7 +312,9 @@ public class ModelAnnotatorTest extends AnnotationTestFunctions {
final MutableDouble maxProgress = new MutableDouble(0.0);
modelAnnotator.performAnnotations(model, new IProgressUpdater() {
Project project = new Project();
project.addModel(model);
modelAnnotator.performAnnotations(project, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
maxProgress.setValue(Math.max(progress, maxProgress.getValue()));
......
......@@ -5,7 +5,8 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ CompositeStrokeTest.class, //
@SuiteClasses({ ColorParserTest.class, //
CompositeStrokeTest.class, //
EllipseTransformationTest.class, //
LineTransformationTest.class, //
PointTransformationTest.class, //
......
......@@ -7,6 +7,7 @@ import lcsb.mapviewer.annotation.services.ModelAnnotator;
import lcsb.mapviewer.common.IProgressUpdater;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.persist.ApplicationContextLoader;
......@@ -26,25 +27,25 @@ public class BellExport {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(BellExport.class.getName());
private static Logger logger = Logger.getLogger(BellExport.class.getName());
/**
* Object that performs different types of export transformation.
*/
@Autowired
private IExporterService exporter;
private IExporterService exporter;
/**
* Object used to annotate the model.
*/
@Autowired
private ModelAnnotator modelAnnotator;
private ModelAnnotator modelAnnotator;
/**
* Object used to annotate the model.
*/
@Autowired
private DbUtils dbUtils;
private DbUtils dbUtils;
/**
* Static main method used to run this stand alone code.
......@@ -76,23 +77,22 @@ public class BellExport {
try {
String filename = PdMapAnnotations.getLastPdFilename();
String version = PdMapAnnotations.getLastPdVersion();
//filename = "testFiles/other_full/ASTHMAP18000X8000V1_38.xml";
//version = "1.1.38";
// filename = "testFiles/other_full/ASTHMAP18000X8000V1_38.xml";
// version = "1.1.38";
dbUtils.createSessionForCurrentThread();
Model model = p.createModel(new ConverterParams().filename(filename));
Project project = new Project();
project.addModel(model);
modelAnnotator.performAnnotations(model, new IProgressUpdater() {
modelAnnotator.performAnnotations(project, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
// TODO Auto-generated method stub
}
});
ExporterParameters params = new IExporterService.ExporterParameters().model(model).//
fileName("out/bell/" + version + "-reactions.txt").//
fileName("out/bell/" + version + "-reactions.txt").//
fileType(ExportFileType.TAB_SEPARATED).//
type(Species.class).//
moleculeEdges(false);
......@@ -100,15 +100,15 @@ public class BellExport {
exporter.exportReactions(params);
params = new IExporterService.ExporterParameters().model(model).//
fileName("out/bell/" + version + "-species.txt").//
fileName("out/bell/" + version + "-species.txt").//
fileType(ExportFileType.TAB_SEPARATED).//
type(Species.class).//
// column(ExportColumn.FULL).//
// column(ExportColumn.FULL).//
moleculeEdges(false);
exporter.exportSpecies(params);
params = new IExporterService.ExporterParameters().model(model).//
fileName("out/bell/" + version + "-compartments.txt").//
fileName("out/bell/" + version + "-compartments.txt").//
fileType(ExportFileType.TAB_SEPARATED).//
type(Object.class); //
// column(ExportColumn.FULL);
......
......@@ -20,13 +20,14 @@ import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.IProgressUpdater;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.persist.ApplicationContextLoader;
import lcsb.mapviewer.persist.DbUtils;
......@@ -112,8 +113,11 @@ public class Statistics {
try {
// String modelName = PdMapAnnotations.getLastPdFilename();
CellDesignerXmlParser parser = new CellDesignerXmlParser();
Project project = new Project();
for (String name : fileNames) {
models.add(parser.createModel(new ConverterParams().filename(name)));
Model model = parser.createModel(new ConverterParams().filename(name));
models.add(model);
project.addModel(model);
}
IProgressUpdater updater = new IProgressUpdater() {
......@@ -122,9 +126,7 @@ public class Statistics {
logger.debug("Progress: " + progress);
}
};
for (Model model : models) {
modelAnnotator.performAnnotations(model, updater);
}
modelAnnotator.performAnnotations(project, updater);
// modelAnnotator.removeIncorrectAnnotations(model, updater);
printStatistics(models);
......
......@@ -18,7 +18,6 @@ import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import lcsb.mapviewer.common.EventStorageLoggerAppender;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.XmlParser;
......@@ -77,7 +76,6 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(CellDesignerXmlParser.class.getName());
/**
......@@ -107,153 +105,143 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
@Override
public Model createModel(ConverterParams params) throws InvalidInputDataExecption {
EventStorageLoggerAppender appender = new EventStorageLoggerAppender();
try {
Logger.getRootLogger().addAppender(appender);
CellDesignerElementCollection elements = new CellDesignerElementCollection();
CellDesignerElementCollection elements = new CellDesignerElementCollection();
Model model = new ModelFullIndexed(null);
Model model = new ModelFullIndexed(null);
model.setSbgnFormat(params.isSbgnFormat());
if (params.getFilename() != null) {
model.setName(FilenameUtils.getBaseName(params.getFilename()));
}
speciesSbmlParser = new SpeciesCollectionXmlParser(elements);
aliasCollectionParser = new AliasCollectionXmlParser(elements, model);
compartmentCollectionXmlParser = new CompartmentCollectionXmlParser(elements);
if (params.getFilename() != null) {
model.setName(FilenameUtils.getBaseName(params.getFilename()));
}
speciesSbmlParser = new SpeciesCollectionXmlParser(elements);
aliasCollectionParser = new AliasCollectionXmlParser(elements, model);
compartmentCollectionXmlParser = new CompartmentCollectionXmlParser(elements);
DOMParser parser = new DOMParser();
try {
parser.parse(params.getSource());
} catch (IOException e) {
throw new InvalidInputDataExecption("IO Problem with a file: " + params.getSource().getSystemId(), e);
}
Document doc = parser.getDocument();
try {
DOMParser parser = new DOMParser();
try {
parser.parse(params.getSource());
} catch (IOException e) {
throw new InvalidInputDataExecption("IO Problem with a file: " + params.getSource().getSystemId(), e);
}
Document doc = parser.getDocument();
try {
// Get the document's root XML node
NodeList root = doc.getChildNodes();
// Get the document's root XML node
NodeList root = doc.getChildNodes();
// Navigate down the hierarchy to get to the CEO node
Node sbmlNode = getNode("SBML", root);
if (sbmlNode == null) {
throw new InvalidInputDataExecption("No SBML node");
}
// Navigate down the hierarchy to get to the CEO node
Node sbmlNode = getNode("SBML", root);
if (sbmlNode == null) {
throw new InvalidInputDataExecption("No SBML node");
}
Node modelNode = getNode("model", sbmlNode.getChildNodes());
if (modelNode == null) {
throw new InvalidInputDataExecption("No model node in SBML");
}
// we ignore metaid - it's useless and obstruct data model
// model.setMetaId(getNodeAttr("metaId", modelNode));
model.setIdModel(getNodeAttr("id", modelNode));
Node compartmentNode = getNode("listOfCompartments", modelNode.getChildNodes());
if (compartmentNode != null) {
List<CellDesignerCompartment> compartments = compartmentCollectionXmlParser.parseXmlCompartmentCollection(compartmentNode);
elements.addElements(compartments);
}
Node modelNode = getNode("model", sbmlNode.getChildNodes());
if (modelNode == null) {
throw new InvalidInputDataExecption("No model node in SBML");
}
// we ignore metaid - it's useless and obstruct data model
// model.setMetaId(getNodeAttr("metaId", modelNode));
model.setIdModel(getNodeAttr("id", modelNode));
Node compartmentNode = getNode("listOfCompartments", modelNode.getChildNodes());
if (compartmentNode != null) {
List<CellDesignerCompartment> compartments = compartmentCollectionXmlParser.parseXmlCompartmentCollection(compartmentNode);
elements.addElements(compartments);
}
InternalModelSpeciesData modelData = new InternalModelSpeciesData();
InternalModelSpeciesData modelData = new InternalModelSpeciesData();
Node speciesNode = getNode("listOfSpecies", modelNode.getChildNodes());
if (speciesNode != null) {
List<Pair<String, ? extends CellDesignerSpecies<?>>> species = speciesSbmlParser.parseSbmlSpeciesCollection(speciesNode);
modelData.updateSpecies(species);
}
Node reactionsNode = null;
NodeList nodes = modelNode.getChildNodes();
for (int x = 0; x < nodes.getLength(); x++) {
Node node = nodes.item(x);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equalsIgnoreCase("annotation")) {
continue;
} else if (node.getNodeName().equalsIgnoreCase("listOfSpecies")) {
continue;
} else if (node.getNodeName().equalsIgnoreCase("listOfReactions")) {
reactionsNode = node;
} else if (node.getNodeName().equalsIgnoreCase("listOfCompartments")) {
// we already parsed compartemnts
continue;
} else if (node.getNodeName().equalsIgnoreCase("notes")) {
String notes = rap.getNotes(node);
if (notes != null) {
notes = StringEscapeUtils.unescapeHtml4(notes);
}
model.setNotes(notes);
} else if (node.getNodeName().equalsIgnoreCase("listOfUnitDefinitions")) {
continue; // we can ignore unit definitions
} else {
throw new InvalidInputDataExecption("Unknown element of model: " + node.getNodeName());
Node speciesNode = getNode("listOfSpecies", modelNode.getChildNodes());
if (speciesNode != null) {
List<Pair<String, ? extends CellDesignerSpecies<?>>> species = speciesSbmlParser.parseSbmlSpeciesCollection(speciesNode);
modelData.updateSpecies(species);
}
Node reactionsNode = null;
NodeList nodes = modelNode.getChildNodes();
for (int x = 0; x < nodes.getLength(); x++) {
Node node = nodes.item(x);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equalsIgnoreCase("annotation")) {
continue;
} else if (node.getNodeName().equalsIgnoreCase("listOfSpecies")) {
continue;
} else if (node.getNodeName().equalsIgnoreCase("listOfReactions")) {
reactionsNode = node;
} else if (node.getNodeName().equalsIgnoreCase("listOfCompartments")) {
// we already parsed compartemnts
continue;
} else if (node.getNodeName().equalsIgnoreCase("notes")) {
String notes = rap.getNotes(node);
if (notes != null) {
notes = StringEscapeUtils.unescapeHtml4(notes);
}
model.setNotes(notes);
} else if (node.getNodeName().equalsIgnoreCase("listOfUnitDefinitions")) {
continue; // we can ignore unit definitions
} else {
throw new InvalidInputDataExecption("Unknown element of model: " + node.getNodeName());
}
}
}
Node annotationNode = getNode("annotation", modelNode.getChildNodes());
if (annotationNode == null) {
throw new InvalidInputDataExecption("No annotation node in SBML/model");
}
Node annotationNode = getNode("annotation", modelNode.getChildNodes());
if (annotationNode == null) {
throw new InvalidInputDataExecption("No annotation node in SBML/model");
}
parseAnnotation(model, annotationNode, modelData, elements);
parseAnnotation(model, annotationNode, modelData, elements);
if (speciesNode != null) {
List<Pair<String, ? extends CellDesignerSpecies<?>>> species = speciesSbmlParser.parseSbmlSpeciesCollection(speciesNode);
modelData.updateSpecies(species);
}
if (speciesNode != null) {
List<Pair<String, ? extends CellDesignerSpecies<?>>> species = speciesSbmlParser.parseSbmlSpeciesCollection(speciesNode);
modelData.updateSpecies(species);
}
if (reactionsNode != null) {
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements);
List<Reaction> reactions = reactionCollectionXmlParser.parseXmlReactionCollection(reactionsNode);
model.addReactions(reactions);
}
if (reactionsNode != null) {
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements, params.isSbgnFormat());
List<Reaction> reactions = reactionCollectionXmlParser.parseXmlReactionCollection(reactionsNode);
model.addReactions(reactions);
}
if (params.isSizeAutoAdjust()) {
Rectangle2D bound = getModelBound(model);
double width = bound.getWidth() + 2 * (Math.max(0, bound.getX()));
double height = bound.getHeight() + 2 * (Math.max(0, bound.getY()));
if (params.isSizeAutoAdjust()) {
Rectangle2D bound = getModelBound(model);
double width = bound.getWidth() + 2 * (Math.max(0, bound.getX()));
double height = bound.getHeight() + 2 * (Math.max(0, bound.getY()));
model.setWidth(width);
model.setHeight(height);
}
} catch (InvalidXmlSchemaException e) {
throw new InvalidInputDataExecption(e);
} catch (UnknownReactionClassException e) {
String type = e.getReactionType();
String reactionId = e.getReactionId();
String newType = null;
if ("CATALYSIS".equalsIgnoreCase(type)) {
newType = "positive influence";
} else if ("INHIBITION".equalsIgnoreCase(type)) {
newType = "negative influence";
} else if ("UNKNOWN_CATALYSIS".equalsIgnoreCase(type)) {
newType = "unknown positive influence";
} else if ("UNKNOWN_INHIBITION".equalsIgnoreCase(type)) {
newType = "unknown negative influence";
} else if ("PHYSICAL_STIMULATION".equalsIgnoreCase(type)) {
newType = "reduced physical stimulation";
} else if ("MODULATION".equalsIgnoreCase(type)) {
newType = "reduced modulation";
} else if ("TRIGGER".equalsIgnoreCase(type)) {
newType = "reduced trigger";
} else {
throw new InvalidInputDataExecption(e);
}
throw new InvalidInputDataExecption(
"Reaction type \"" + type + "\" is inappropriate for reaction " + reactionId + ". Suggested type: " + newType
+ " in the \"Reduced\" notation of CellDesigner.",
e);
} catch (CellDesignerParserException e) {
model.setWidth(width);
model.setHeight(height);
}
} catch (InvalidXmlSchemaException e) {
throw new InvalidInputDataExecption(e);
} catch (UnknownReactionClassException e) {
String type = e.getReactionType();
String reactionId = e.getReactionId();
String newType = null;
if ("CATALYSIS".equalsIgnoreCase(type)) {
newType = "positive influence";
} else if ("INHIBITION".equalsIgnoreCase(type)) {
newType = "negative influence";
} else if ("UNKNOWN_CATALYSIS".equalsIgnoreCase(type)) {
newType = "unknown positive influence";
} else if ("UNKNOWN_INHIBITION".equalsIgnoreCase(type)) {
newType = "unknown negative influence";
} else if ("PHYSICAL_STIMULATION".equalsIgnoreCase(type)) {
newType = "reduced physical stimulation";
} else if ("MODULATION".equalsIgnoreCase(type)) {
newType = "reduced modulation";
} else if ("TRIGGER".equalsIgnoreCase(type)) {
newType = "reduced trigger";
} else {
throw new InvalidInputDataExecption(e);
}
Logger.getRootLogger().removeAppender(appender);
model.addLoggingInfo(appender);
return model;
} finally {
Logger.getRootLogger().removeAppender(appender);
throw new InvalidInputDataExecption(
"Reaction type \"" + type + "\" is inappropriate for reaction " + reactionId + ". Suggested type: " + newType
+ " in the \"Reduced\" notation of CellDesigner.",
e);
} catch (CellDesignerParserException e) {
throw new InvalidInputDataExecption(e);
}
return model;
}
/**
......@@ -451,7 +439,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
if (!species.getElementId().equals("")) {
elements.addElement(species);
} else {
model.addCreationWarning(
logger.warn(
"Species (class: " + species.getClass().getName() + ", name: " + species.getName()
+ ") exists in CD file, but is never instantiated. It's CellDesigner file problem.");
}
......@@ -572,7 +560,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
CellDesignerElementCollection elements = new CellDesignerElementCollection();
SpeciesCollectionXmlParser speciesCollectionXmlParser = new SpeciesCollectionXmlParser(elements);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements, false);
aliasCollectionParser = new AliasCollectionXmlParser(elements, model);
StringBuilder result = new StringBuilder();
......
......@@ -23,7 +23,7 @@ import lcsb.mapviewer.model.map.species.Element;
* @param <T>
*/
public abstract class AbstractCellDesignerAliasConverter<T extends Element> implements ICellDesignerAliasConverter<T> {
/**
* Default class logger.
*/
......@@ -33,17 +33,17 @@ public abstract class AbstractCellDesignerAliasConverter<T extends Element> impl
* CellDesigner graphical helper with polygon transformation functions.
*/
private CellDesignerPolygonTransformation polygonTransformation = new CellDesignerPolygonTransformation();
/**
* CellDesigner graphical helper with lin transformation functions.
*/
private LineTransformation lineTransformation = new LineTransformation();
/**
* CellDesigner graphical helper with ellipse transformation functions.
*/