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;
......@@ -77,17 +78,16 @@ public class BellExport {
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
}
});
......@@ -103,7 +103,7 @@ public class BellExport {
fileName("out/bell/" + version + "-species.txt").//
fileType(ExportFileType.TAB_SEPARATED).//
type(Species.class).//
// column(ExportColumn.FULL).//
// column(ExportColumn.FULL).//
moleculeEdges(false);
exporter.exportSpecies(params);
......
......@@ -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,14 +105,9 @@ 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();
Model model = new ModelFullIndexed(null);
model.setSbgnFormat(params.isSbgnFormat());
if (params.getFilename() != null) {
model.setName(FilenameUtils.getBaseName(params.getFilename()));
......@@ -204,7 +197,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
}
if (reactionsNode != null) {
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements);
ReactionCollectionXmlParser reactionCollectionXmlParser = new ReactionCollectionXmlParser(model, elements, params.isSbgnFormat());
List<Reaction> reactions = reactionCollectionXmlParser.parseXmlReactionCollection(reactionsNode);
model.addReactions(reactions);
}
......@@ -248,12 +241,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter {
throw new InvalidInputDataExecption(e);
}
Logger.getRootLogger().removeAppender(appender);
model.addLoggingInfo(appender);
return model;
} finally {
Logger.getRootLogger().removeAppender(appender);
}
}
/**
......@@ -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();
......
......@@ -54,10 +54,19 @@ public abstract class AbstractCellDesignerAliasConverter<T extends Element> impl
*/
public static final int HOMODIMER_OFFSET = 6;
/**
* Should the converter use sbgn standard.
*/
private boolean sbgn;
/**
* Default constructor that prevents from instatiation of the class.
*
* @param sbgn
* Should the converter use sbgn standard
*/
protected AbstractCellDesignerAliasConverter() {
protected AbstractCellDesignerAliasConverter(boolean sbgn) {
this.sbgn = sbgn;
};
@Override
......@@ -215,4 +224,12 @@ public abstract class AbstractCellDesignerAliasConverter<T extends Element> impl
this.rectangleTransformation = rectangleTransformation;
}
/**
* @return the sbgn
* @see #sbgn
*/
protected boolean isSbgn() {
return sbgn;
}
}
......@@ -20,7 +20,18 @@ import lcsb.mapviewer.model.map.species.Species;
public class AntisenseRnaCellDesignerAliasConverter extends AbstractCellDesignerAliasConverter<Species> {
/**
* How big should be the arc in rectangle for nucleic acid feature representation.
* Default constructor.
*
* @param sbgn
* Should the converter use sbgn standard
*/
protected AntisenseRnaCellDesignerAliasConverter(boolean sbgn) {
super(sbgn);
}
/**
* How big should be the arc in rectangle for nucleic acid feature
* representation.
*/
private static final int RECTANGLE_CORNER_ARC_SIZE = 5;
......@@ -29,9 +40,8 @@ public class AntisenseRnaCellDesignerAliasConverter extends AbstractCellDesigner
if (invalidAnchorPosition(alias, anchor)) {
return alias.getCenter();
}
if (alias.getModel().isSbgnFormat()) {
return getRectangleTransformation().getPointOnRectangleByAnchor(alias.getX(), alias.getY(),
alias.getWidth(), alias.getHeight(), anchor);
if (isSbgn()) {
return getRectangleTransformation().getPointOnRectangleByAnchor(alias.getX(), alias.getY(), alias.getWidth(), alias.getHeight(), anchor);
}
ArrayList<Point2D> points = getPoints(alias);
return getPolygonTransformation().getPointOnPolygonByAnchor(points, anchor);
......@@ -79,7 +89,7 @@ public class AntisenseRnaCellDesignerAliasConverter extends AbstractCellDesigner
private GeneralPath getAntisebseRnaPath(Element alias) {
// CHECKSTYLE:OFF
GeneralPath path;
if (!alias.getModel().isSbgnFormat()) {
if (!isSbgn()) {
path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 4);
path.moveTo(alias.getX(), alias.getY());
path.lineTo(alias.getX() + alias.getWidth() * 3 / 4, alias.getY());
......@@ -92,13 +102,9 @@ public class AntisenseRnaCellDesignerAliasConverter extends AbstractCellDesigner
path.moveTo(x, y);
path.lineTo(x, y + height - RECTANGLE_CORNER_ARC_SIZE);
path.curveTo(x, y + height,
x + RECTANGLE_CORNER_ARC_SIZE, y + height,
x + RECTANGLE_CORNER_ARC_SIZE, y + height);
path.curveTo(x, y + height, x + RECTANGLE_CORNER_ARC_SIZE, y + height, x + RECTANGLE_CORNER_ARC_SIZE, y + height);
path.lineTo(x + width - RECTANGLE_CORNER_ARC_SIZE, y + height);
path.curveTo(x + width, y + height,
x + width, y + height - RECTANGLE_CORNER_ARC_SIZE,
x + width, y + height - RECTANGLE_CORNER_ARC_SIZE);
path.curveTo(x + width, y + height, x + width, y + height - RECTANGLE_CORNER_ARC_SIZE, x + width, y + height - RECTANGLE_CORNER_ARC_SIZE);
path.lineTo(x + width, y);
path.closePath();
}
......
package lcsb.mapviewer.converter.model.celldesigner.geometry;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.converter.model.celldesigner.geometry.helper.CellDesignerAnchor;
import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.AntisenseRna;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Degraded;
import lcsb.mapviewer.model.map.species.Drug;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.Ion;
import lcsb.mapviewer.model.map.species.IonChannelProtein;
import lcsb.mapviewer.model.map.species.Phenotype;
import lcsb.mapviewer.model.map.species.ReceptorProtein;
import lcsb.mapviewer.model.map.species.Protein;
import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.model.map.species.SimpleMolecule;
import lcsb.mapviewer.model.map.species.TruncatedProtein;
import lcsb.mapviewer.model.map.species.Unknown;
import lcsb.mapviewer.modelutils.map.ElementUtils;
/**
* This class is designed to obtain CellDesigner specific data from alias. It
* contains static instances of converter for every type and use one that is
* needed for currently processed alias.
* This class is designed to obtain CellDesigner specific data from
* {@link Element}.
*
* @author Piotr Gawron
*
......@@ -35,83 +30,54 @@ import lcsb.mapviewer.modelutils.map.ElementUtils;
public class CellDesignerAliasConverter implements ICellDesignerAliasConverter<Element> {
// *****************************************************************
//
// STATIC PART
//
// *****************************************************************
/**
* Static map of alias converters used for conversion of aliases. Key in this
* map is a class of element to which alias reference to. Value is instance of
* a converter that can process the alias with the specific element type
* reference.
*/
private static Map<Class<? extends Element>, ICellDesignerAliasConverter<?>> elementConverters;
/**
* Class helping with transforming objectcs into meaningfull identifiers.
*/
private static ElementUtils eu = new ElementUtils();
static {
elementConverters = new HashMap<>();
addElementConverter(GenericProtein.class, new ProteinCellDesignerAliasConverter());
addElementConverter(TruncatedProtein.class, new ProteinCellDesignerAliasConverter());
addElementConverter(ReceptorProtein.class, new ProteinCellDesignerAliasConverter());
addElementConverter(IonChannelProtein.class, new ProteinCellDesignerAliasConverter());
addElementConverter(Degraded.class, new DegradedCellDesignerAliasConverter());
addElementConverter(Complex.class, new ComplexCellDesignerAliasConverter());
addElementConverter(SimpleMolecule.class, new SimpleMoleculeCellDesignerAliasConverter());
addElementConverter(Drug.class, new DrugCellDesignerAliasConverter());
addElementConverter(Ion.class, new IonCellDesignerAliasConverter());
addElementConverter(Phenotype.class, new PhenotypeCellDesignerAliasConverter());
addElementConverter(Rna.class, new RnaCellDesignerAliasConverter());
addElementConverter(AntisenseRna.class, new AntisenseRnaCellDesignerAliasConverter());
addElementConverter(Gene.class, new GeneCellDesignerAliasConverter());
addElementConverter(Unknown.class, new UnknownCellDesignerAliasConverter());
}
/**
* Method that set a converter for element referenced by alias to process.
* Returns a converter for given {@link Element}. If converter doesn't exist
* exception is thrown.
*
* @param clazz
* class type of element referenced by alias
* @param converter
* converter that should be used for conversion of this alias
* @param element
* element for which we are loooking for a converter
* @param sbgn
* Should the converter use sbgn standard
* @return converter that can be applied for the given element
*/
protected static void addElementConverter(Class<? extends Element> clazz, ICellDesignerAliasConverter<?> converter) {
elementConverters.put(clazz, converter);
private ICellDesignerAliasConverter<? extends Element> getConverterForAlias(Element element, boolean sbgn) {
if (element == null) {
throw new InvalidArgumentException("element cannot be null");
}
/**
* Returns a converter for given alias. If converter doesn't exist then null
* is returned.
*
* @param alias
* alias for which we are loooking for a converter
* @return converter that can be applied for the given alias; null if such
* converter doesn't exist
*/
private static ICellDesignerAliasConverter<? extends Element> getConverterForAlias(Element alias) {
if (alias == null) {
throw new InvalidArgumentException("alias cannot be null");
if (element instanceof Protein) {
return new ProteinCellDesignerAliasConverter(sbgn);
} else if (element instanceof Degraded) {
return new DegradedCellDesignerAliasConverter(sbgn);
} else if (element instanceof Complex) {
return new ComplexCellDesignerAliasConverter(sbgn);
} else if (element instanceof SimpleMolecule) {
return new SimpleMoleculeCellDesignerAliasConverter(sbgn);
} else if (element instanceof Drug) {
return new DrugCellDesignerAliasConverter(sbgn);
} else if (element instanceof Ion) {
return new IonCellDesignerAliasConverter(sbgn);
} else if (element instanceof Phenotype) {
return new PhenotypeCellDesignerAliasConverter(sbgn);
} else if (element instanceof Rna) {
return new RnaCellDesignerAliasConverter(sbgn);
} else if (element instanceof AntisenseRna) {
return new AntisenseRnaCellDesignerAliasConverter(sbgn);
} else if (element instanceof Gene) {
return new GeneCellDesignerAliasConverter(sbgn);
} else if (element instanceof Unknown) {
return new UnknownCellDesignerAliasConverter(sbgn);
} else {
throw new NotImplementedException(eu.getElementTag(element) + "Unknown converter for class");
}
ICellDesignerAliasConverter<?> result = elementConverters.get(alias.getClass());
return result;
}
// *******************************************************
//
// NON-STATIC CLASS DEFINITION
//
// *******************************************************
/**
* Converter used for operations on the alias given in constructor.
* Converter used for operations on the {@link Element} given in constructor.
*/
@SuppressWarnings("rawtypes")
private ICellDesignerAliasConverter converter = null;
......@@ -119,32 +85,30 @@ public class CellDesignerAliasConverter implements ICellDesignerAliasConverter<E
/**
* Default constructor.
*
* @param alias
* alias for which this converter will be used
* @param sbgn
* Should the converter use sbgn standard
* @param element
* element for which this converter will be used
*/
public CellDesignerAliasConverter(Element alias) {
converter = getConverterForAlias(alias);
// if we don't know which converter to use then throw an exception
if (converter == null) {
throw new InvalidArgumentException(eu.getElementTag(alias) + "Unknown converter for class");
}
public CellDesignerAliasConverter(Element element, boolean sbgn) {
converter = getConverterForAlias(element, sbgn);
}
@SuppressWarnings("unchecked")
@Override
public CellDesignerAnchor getAnchorForCoordinates(Element alias, Point2D point) {
return converter.getAnchorForCoordinates(alias, point);
public CellDesignerAnchor getAnchorForCoordinates(Element element, Point2D point) {
return converter.getAnchorForCoordinates(element, point);
}
@SuppressWarnings("unchecked")
@Override
public Point2D getPointCoordinates(Element alias, CellDesignerAnchor anchor) {
return converter.getPointCoordinates(alias, anchor);
public Point2D getPointCoordinates(Element element, CellDesignerAnchor anchor) {
return converter.getPointCoordinates(element, anchor);
}
@SuppressWarnings("unchecked")
@Override
public Point2D getAnchorPointCoordinates(Element alias, CellDesignerAnchor anchor, PolylineData line) {
return converter.getAnchorPointCoordinates(alias, anchor, line);
public Point2D getAnchorPointCoordinates(Element element, CellDesignerAnchor anchor, PolylineData line) {
return converter.getAnchorPointCoordinates(element, anchor, line);
}
}
......@@ -18,6 +18,16 @@ import lcsb.mapviewer.model.map.species.Complex;
*/
public class ComplexCellDesignerAliasConverter extends AbstractCellDesignerAliasConverter<Complex> {
/**
* Default constructor.
*
* @param sbgn
* Should the converter use sbgn standard
*/
protected ComplexCellDesignerAliasConverter(boolean sbgn) {
super(sbgn);
}
/**
* How big is the triangle trimmed part of the complex.
*/
......