Commit 0aaf789c authored by Piotr Gawron's avatar Piotr Gawron
Browse files

sbml converter package

basic parsing of compartments
parent 60cffa1d
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="MapViewer-converter-CellDesigner">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>converter-CellDesigner</artifactId>
<name>CellDesigner converter</name>
<description>CellDesigner converter for model</description>
<dependencies>
<!-- dependency from the MapViewer model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter</artifactId>
<version>1.0</version>
</dependency>
<!-- Log4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- mockito used for testing -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>converter-CellDesigner</artifactId>
<name>CellDesigner converter</name>
<description>CellDesigner converter for model</description>
<dependencies>
<!-- dependency from the MapViewer model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter</artifactId>
<version>1.0</version>
</dependency>
<!-- Log4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- mockito used for testing -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>converter-sbml</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>converter-sbml</artifactId>
<name>converter-sbml</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
</configuration>
</plugin>
<dependencies>
<!-- dependency from the minerva model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter</artifactId>
<version>1.0</version>
</dependency>
<!-- Log4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- mockito used for testing -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sbml.jsbml</groupId>
<artifactId>jsbml</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter-graphics</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter-CellDesigner</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package lcsb.mapviewer.converter.model.sbml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.Annotation;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.ext.layout.CompartmentGlyph;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.util.NotImplementedException;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.compartment.SquareCompartment;
public class SbmlCompartmentParser {
Logger logger = Logger.getLogger(SbmlCompartmentParser.class);
Layout layout;
public SbmlCompartmentParser(Layout layout) {
this.layout = layout;
}
public List<Compartment> parseList(Model sbmlModel) throws InvalidInputDataExecption {
List<Compartment> result = new ArrayList<>();
for (org.sbml.jsbml.Compartment compartment : sbmlModel.getListOfCompartments()) {
result.add(parse(compartment));
}
if (layout != null) {
return mergeLayout(result, layout);
} else {
return result;
}
}
private List<Compartment> mergeLayout(List<Compartment> compartments, Layout sbmlLayout)
throws InvalidInputDataExecption {
Set<Compartment> used = new HashSet<>();
Map<String, Compartment> compartmentById = new HashMap<>();
for (Compartment compartment : compartments) {
if (compartmentById.get(compartment.getElementId()) != null) {
throw new InvalidInputDataExecption("Duplicated element id: " + compartment.getElementId());
}
compartmentById.put(compartment.getElementId(), compartment);
}
List<Compartment> result = new ArrayList<>();
for (CompartmentGlyph glyph : sbmlLayout.getListOfCompartmentGlyphs()) {
Compartment source = compartmentById.get(glyph.getCompartment());
if (source==null) {
throw new InvalidInputDataExecption("Layout contains invalid compartment id: "+glyph.getCompartment());
}
used.add(source);
Compartment compartmentWithLayout = new SquareCompartment(source);
compartmentWithLayout.setElementId(glyph.getId());
compartmentWithLayout.setX(glyph.getBoundingBox().getPosition().getX());
compartmentWithLayout.setY(glyph.getBoundingBox().getPosition().getY());
compartmentWithLayout.setWidth(glyph.getBoundingBox().getDimensions().getWidth());
compartmentWithLayout.setHeight(glyph.getBoundingBox().getDimensions().getHeight());
result.add(compartmentWithLayout);
}
for (Compartment compartment : compartments) {
if (!used.contains(compartment)) {
logger.warn("Layout doesn't contain information about compartment: "+compartment.getElementId());
result.add(compartment);
}
}
return result;
}
private Compartment parse(org.sbml.jsbml.Compartment compartment) throws InvalidInputDataExecption {
Compartment result = new Compartment(compartment.getId());
result.setMiriamData(parseAnnotation(compartment.getAnnotation()));
result.setName(compartment.getName());
try {
result.setNotes(compartment.getNotesString());
} catch (XMLStreamException e) {
throw new InvalidInputDataExecption(compartment.getId() + " Invalid compartment notes", e);
}
return result;
}
protected Set<MiriamData> parseAnnotation(Annotation annotation) {
if (annotation.getCVTermCount() > 0) {
throw new NotImplementedException();
}
Set<MiriamData> result = new HashSet<>();
return result;
}
}
package lcsb.mapviewer.converter.model.sbml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.SBMLReader;
import org.sbml.jsbml.ext.SBasePlugin;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.ext.layout.LayoutModelPlugin;
import org.sbml.jsbml.util.NotImplementedException;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.IConverter;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.species.Element;
public class SbmlParser implements IConverter {
/**
* Default class logger.
*/
Logger logger = Logger.getLogger(SbmlParser.class);
@Override
public Model createModel(ConverterParams params) throws InvalidInputDataExecption {
try {
Model model = new ModelFullIndexed(null);
if (params.getFilename() != null) {
model.setName(FilenameUtils.getBaseName(params.getFilename()));
}
SBMLDocument sbmlDocument = SBMLReader.read(params.getInputStream());
org.sbml.jsbml.Model sbmlModel = sbmlDocument.getModel();
model.setIdModel(sbmlModel.getId());
Layout layout = getSbmlLayout(sbmlModel);
SbmlCompartmentParser compartmentParser = new SbmlCompartmentParser(layout);
Set<MiriamData> annotations = compartmentParser.parseAnnotation(sbmlModel.getAnnotation());
if (annotations.size() > 0) {
throw new NotImplementedException("Annotations not implemented for model");
}
model.addElements(compartmentParser.parseList(sbmlModel));
if (sbmlModel.getConstraintCount() > 0) {
throw new NotImplementedException("Constraints not implemented for model");
}
if (sbmlModel.getConversionFactorInstance() != null) {
throw new NotImplementedException("ConversionFactor not implemented for model");
}
if (sbmlModel.getCVTermCount() > 0) {
throw new NotImplementedException("CVTerms not implemented for model");
}
if (sbmlModel.getEventCount() > 0) {
throw new NotImplementedException("Events not implemented for model");
}
if (sbmlModel.getEventAssignmentCount() > 0) {
throw new NotImplementedException("EventAssignemnts not implemented for model");
}
if (sbmlModel.getFunctionDefinitionCount() > 0) {
throw new NotImplementedException("FunctionDefinition not implemented for model");
}
if (sbmlModel.getInitialAssignmentCount() > 0) {
throw new NotImplementedException("InitialAssignment not implemented for model");
}
if (sbmlModel.getKineticLawCount() > 0) {
throw new NotImplementedException("KineticLaw not implemented for model");
}
if (sbmlModel.getParameterCount() > 0) {
throw new NotImplementedException("Parameter not implemented for model");
}
if (sbmlModel.getModifierSpeciesReferenceCount() > 0) {
throw new NotImplementedException("ModifierSpeciesReference not implemented for model");
}
if (sbmlModel.getReactionCount() > 0) {
throw new NotImplementedException("Reaction not implemented for model");
}
if (sbmlModel.getRuleCount() > 0) {
throw new NotImplementedException("Rule not implemented for model");
}
if (sbmlModel.getSpeciesCount() > 0) {
throw new NotImplementedException("Species not implemented for model");
}
if (sbmlModel.getSpeciesReferenceCount() > 0) {
throw new NotImplementedException("SpeciesReference not implemented for model");
}
if (sbmlModel.getUnitDefinitionCount() > 0) {
logger.warn("Units are not supported");
}
createLayout(model, layout);
return model;
} catch (XMLStreamException e) {
throw new InvalidInputDataExecption(e);
}
}
private void createLayout(Model model, Layout layout) {
double width;
double height;
if (layout != null) {
width = layout.getDimensions().getWidth();
height = layout.getDimensions().getHeight();
} else {
width = 100;
height = 100;
double minX = Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
for (Element element : model.getElements()) {
if (element.getX() == null) {
throw new NotImplementedException("Element without layout not implemented");
}
width = Math.max(width, element.getX() + element.getWidth());
height = Math.max(height, element.getY() + element.getHeight());
minX = Math.min(minX, element.getX());
minY = Math.min(minY, element.getY());
}
if (minX != Double.MAX_VALUE) {
minX = Math.max(0, minX);
minY = Math.max(0, minY);
}
width += minX;
height += minY;
}
model.setWidth(width);
model.setHeight(height);
}
private Layout getSbmlLayout(org.sbml.jsbml.Model sbmlModel) {
Layout layout = null;
if (sbmlModel.getExtensionCount() > 0) {
for (SBasePlugin plugin : sbmlModel.getExtensionPackages().values()) {
if (plugin.getClass().equals(org.sbml.jsbml.ext.layout.LayoutModelPlugin.class)) {
LayoutModelPlugin layoutPlugin = (LayoutModelPlugin) plugin;
if (layoutPlugin.getLayoutCount() == 0) {
logger.warn("Layout plugin available but not layouts defined");
} else if (layoutPlugin.getLayoutCount() > 1) {
logger.warn(layoutPlugin.getLayoutCount() + " layouts defined. Using first one.");
layout = layoutPlugin.getLayout(0);
} else {
layout = layoutPlugin.getLayout(0);
}
} else {
logger.warn("Unknown sbml plugin: " + plugin);
}
}
}
return layout;
}
@Override
public InputStream exportModelToInputStream(Model model) throws ConverterException, InconsistentModelException {
// TODO Auto-generated method stub
return null;
}
@Override
public File exportModelToFile(Model model, String filePath)
throws ConverterException, InconsistentModelException, IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public String getCommonName() {
// TODO Auto-generated method stub
return null;
}
@Override
public MimeType getMimeType() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getFileExtension() {
// TODO Auto-generated method stub
return null;
}
}
#Set root logger 's level and its appender to an appender called CONSOLE which is defined below.
log4j.rootLogger=info, SBML_CONSOLE
#Set the behavior of the CONSOLE appender
log4j.appender.SBML_CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.SBML_CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.SBML_CONSOLE.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=%m%n
#Set the behavior of the FILE appender
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=${catalina.home}/logs/MapViewer.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
log4j.logger.org.springframework=warn
log4j.logger.org.hibernate=warn
log4j.logger.lcsb=debug
log4j.logger.lcsb.mapviewer.common.comparator=info