Skip to content
Snippets Groups Projects
Commit 14cea5ea authored by Piotr Gawron's avatar Piotr Gawron
Browse files

simple export of species implemeted

parent 154f1e68
No related branches found
No related tags found
1 merge request!186Resolve "upload of sbml"
package lcsb.mapviewer.converter.model.sbml;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.ext.layout.BoundingBox;
import org.sbml.jsbml.ext.layout.CompartmentGlyph;
import org.sbml.jsbml.ext.layout.Dimensions;
import org.sbml.jsbml.ext.layout.AbstractReferenceGlyph;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.ext.layout.Point;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.compartment.Compartment;
public class SbmlCompartmentExporter extends SbmlElementExporter<Compartment> {
public class SbmlCompartmentExporter extends SbmlElementExporter<Compartment, org.sbml.jsbml.Compartment> {
Logger logger = Logger.getLogger(SbmlCompartmentExporter.class);
Map<String, org.sbml.jsbml.Compartment> sbmlCompartmentByElementId = new HashMap<>();
Map<String, org.sbml.jsbml.Compartment> sbmlCompartmentByName = new HashMap<>();
Map<String, CompartmentGlyph> sbmlCompartmentGlyphByElementId = new HashMap<>();
public SbmlCompartmentExporter(Layout layout, lcsb.mapviewer.model.map.model.Model minervaModel) {
super(layout, minervaModel);
}
@Override
public void exportElements(Model model) throws InconsistentModelException {
List<Compartment> compartments = minervaModel.getCompartments();
for (Compartment compartment : compartments) {
if (sbmlCompartmentByName.get(compartment.getName()) == null) {
org.sbml.jsbml.Compartment sbmlCompartment = model.createCompartment("comp_" + compartment.getName());
try {
sbmlCompartment.setNotes(compartment.getNotes());
} catch (XMLStreamException e) {
throw new InvalidStateException(e);
}
sbmlCompartmentByName.put(compartment.getName(), sbmlCompartment);
}
org.sbml.jsbml.Compartment sbmlCompartment = sbmlCompartmentByName.get(compartment.getName());
if (sbmlCompartmentByElementId.get(compartment.getElementId()) != null) {
throw new InconsistentModelException("More than one compartment with id: " + compartment.getElementId());
}
sbmlCompartmentByElementId.put(compartment.getElementId(), sbmlCompartment);
}
for (Compartment compartment : compartments) {
CompartmentGlyph compartmentGlyph = createCompartmentGlyph(compartment);
sbmlCompartmentGlyphByElementId.put(compartment.getElementId(), compartmentGlyph);
}
public org.sbml.jsbml.Compartment createSbmlElement(Compartment element) throws InconsistentModelException {
return sbmlModel.createCompartment("comp_" + element.getName());
}
private CompartmentGlyph createCompartmentGlyph(Compartment compartment) {
String sbmlCompartmentId = sbmlCompartmentByElementId.get(compartment.getElementId()).getId();
String glyphId = compartment.getElementId();
CompartmentGlyph compartmentGlyph = layout.createCompartmentGlyph(glyphId, sbmlCompartmentId);
BoundingBox boundingBox = new BoundingBox();
@Override
protected List<Compartment> getElementList() {
return minervaModel.getCompartments();
}
boundingBox.setPosition(new Point(compartment.getX(), compartment.getY()));
Dimensions dimensions = new Dimensions();
dimensions.setWidth(compartment.getWidth());
dimensions.setHeight(compartment.getHeight());
boundingBox.setDimensions(dimensions);
compartmentGlyph.setBoundingBox(boundingBox);
return compartmentGlyph;
@Override
protected AbstractReferenceGlyph createElementGlyph(String sbmlCompartmentId, String glyphId) {
return layout.createCompartmentGlyph(glyphId, sbmlCompartmentId);
}
}
package lcsb.mapviewer.converter.model.sbml;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.ext.layout.AbstractReferenceGlyph;
import org.sbml.jsbml.ext.layout.BoundingBox;
import org.sbml.jsbml.ext.layout.Dimensions;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.ext.layout.Point;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.species.Element;
public abstract class SbmlElementExporter<T extends Element> {
public abstract class SbmlElementExporter<T extends Element, S extends org.sbml.jsbml.Symbol> {
Logger logger = Logger.getLogger(SbmlElementExporter.class);
Layout layout;
lcsb.mapviewer.model.map.model.Model minervaModel;
Model sbmlModel;
Map<String, S> sbmlElementByElementId = new HashMap<>();
Map<String, AbstractReferenceGlyph> sbmlGlyphByElementId = new HashMap<>();
private Map<String, S> sbmlElementByElementNameAndCompartmentName = new HashMap<>();
public SbmlElementExporter(Layout sbmlLayout, lcsb.mapviewer.model.map.model.Model minervaModel) {
this.layout = sbmlLayout;
this.minervaModel = minervaModel;
}
public abstract void exportElements(Model model) throws InconsistentModelException;
public void exportElements(Model model) throws InconsistentModelException {
sbmlModel = model;
List<T> speciesList = getElementList();
for (T species : speciesList) {
S sbmlCompartment = getSbmlElement(species, null);
if (sbmlElementByElementId.get(species.getElementId()) != null) {
throw new InconsistentModelException("More than one species with id: " + species.getElementId());
}
sbmlElementByElementId.put(species.getElementId(), sbmlCompartment);
}
for (T species : speciesList) {
AbstractReferenceGlyph compartmentGlyph = createCompartmentGlyph(species);
sbmlGlyphByElementId.put(species.getElementId(), compartmentGlyph);
}
}
protected abstract List<T> getElementList();
public abstract S createSbmlElement(T element) throws InconsistentModelException;
protected S getSbmlElement(T element, String compartmentName) throws InconsistentModelException {
String mapKey = element.getName() + "\n" + compartmentName;
if (sbmlElementByElementNameAndCompartmentName.get(mapKey) == null) {
S sbmlElement = createSbmlElement(element);
sbmlElement.setName(element.getName());
try {
sbmlElement.setNotes(element.getNotes());
} catch (XMLStreamException e) {
throw new InvalidStateException(e);
}
sbmlElementByElementNameAndCompartmentName.put(mapKey, sbmlElement);
}
return sbmlElementByElementNameAndCompartmentName.get(mapKey);
}
protected void assignLayoutToGlyph(T element, AbstractReferenceGlyph compartmentGlyph) {
BoundingBox boundingBox = new BoundingBox();
boundingBox.setPosition(new Point(element.getX(), element.getY()));
Dimensions dimensions = new Dimensions();
dimensions.setWidth(element.getWidth());
dimensions.setHeight(element.getHeight());
boundingBox.setDimensions(dimensions);
compartmentGlyph.setBoundingBox(boundingBox);
}
protected AbstractReferenceGlyph createCompartmentGlyph(T compartment) {
String sbmlCompartmentId = sbmlElementByElementId.get(compartment.getElementId()).getId();
String glyphId = compartment.getElementId();
AbstractReferenceGlyph compartmentGlyph = createElementGlyph(sbmlCompartmentId, glyphId);
assignLayoutToGlyph(compartment, compartmentGlyph);
return compartmentGlyph;
}
protected abstract AbstractReferenceGlyph createElementGlyph(String sbmlCompartmentId, String glyphId);
}
......@@ -3,16 +3,10 @@ package lcsb.mapviewer.converter.model.sbml;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.sbml.jsbml.Compartment;
import org.sbml.jsbml.Creator;
import org.sbml.jsbml.History;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.Reaction;
import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.SBMLException;
import org.sbml.jsbml.SBMLWriter;
import org.sbml.jsbml.Species;
import org.sbml.jsbml.SpeciesReference;
import org.sbml.jsbml.ext.layout.Dimensions;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.ext.layout.LayoutModelPlugin;
......@@ -20,24 +14,27 @@ import org.sbml.jsbml.ext.layout.LayoutModelPlugin;
import lcsb.mapviewer.model.map.InconsistentModelException;
public class SbmlExporter {
public String toXml(lcsb.mapviewer.model.map.model.Model model) throws SBMLException, XMLStreamException, InconsistentModelException {
public String toXml(lcsb.mapviewer.model.map.model.Model model)
throws SBMLException, XMLStreamException, InconsistentModelException {
SBMLDocument doc = new SBMLDocument(3, 1);
Model result = doc.createModel(model.getName());
Layout layout = createSbmlLayout(model, result);
SbmlCompartmentExporter compartmentExporter = new SbmlCompartmentExporter(layout, model);
SbmlSpeciesExporter speciesExporter = new SbmlSpeciesExporter(layout, model, compartmentExporter);
compartmentExporter.exportElements(result);
// // Create some sample content in the SBML model.
// Species specOne = result.createSpecies("test_spec1", compartment);
// Species specTwo = result.createSpecies("test_spec2", compartment);
// Reaction sbReaction = result.createReaction("reaction_id");
//
// // Add a substrate (SBO:0000015) and product (SBO:0000011) to the reaction.
// SpeciesReference subs = sbReaction.createReactant(specOne);
// subs.setSBOTerm(15);
// SpeciesReference prod = sbReaction.createProduct(specTwo);
// prod.setSBOTerm(11);
speciesExporter.exportElements(result);
// // Create some sample content in the SBML model.
// Species specOne = result.createSpecies("test_spec1", compartment);
// Species specTwo = result.createSpecies("test_spec2", compartment);
// Reaction sbReaction = result.createReaction("reaction_id");
//
// // Add a substrate (SBO:0000015) and product (SBO:0000011) to the reaction.
// SpeciesReference subs = sbReaction.createReactant(specOne);
// subs.setSBOTerm(15);
// SpeciesReference prod = sbReaction.createProduct(specTwo);
// prod.setSBOTerm(11);
// For brevity, we omit error checking, BUT YOU SHOULD CALL
// doc.checkConsistency() and check the error log.
......
package lcsb.mapviewer.converter.model.sbml;
import java.util.List;
import org.apache.log4j.Logger;
import org.sbml.jsbml.Symbol;
import org.sbml.jsbml.ext.layout.AbstractReferenceGlyph;
import org.sbml.jsbml.ext.layout.Layout;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.species.Species;
public class SbmlSpeciesExporter extends SbmlElementExporter<Species, org.sbml.jsbml.Species> {
Logger logger = Logger.getLogger(SbmlSpeciesExporter.class);
private int idCounter = 0;
private SbmlCompartmentExporter compartmentExporter;
public SbmlSpeciesExporter(Layout layout, lcsb.mapviewer.model.map.model.Model minervaModel,
SbmlCompartmentExporter compartmentExporter) {
super(layout, minervaModel);
this.compartmentExporter = compartmentExporter;
}
@Override
public org.sbml.jsbml.Species createSbmlElement(Species element) throws InconsistentModelException {
org.sbml.jsbml.Species result = sbmlModel.createSpecies("species_" + (idCounter++));
if (element.getCompartment() != null) {
result.setCompartment(compartmentExporter.getSbmlElement(element.getCompartment(), null));
}
return result;
}
@Override
protected AbstractReferenceGlyph createElementGlyph(String sbmlElementId, String glyphId) {
AbstractReferenceGlyph speciesGlyph = layout.createSpeciesGlyph(glyphId, sbmlElementId);
return speciesGlyph;
}
@Override
protected List<Species> getElementList() {
return minervaModel.getSpeciesList();
}
}
......@@ -23,7 +23,7 @@ public class SbmlExporterTest {
SbmlExporter exporter = new SbmlExporter();
@Test
public void testParseCompartment() throws Exception {
public void testExportCompartment() throws Exception {
Model model = getModelAfterSerializing("testFiles/layoutExample/CompartmentGlyph_Example_level2_level3.xml");
assertNotNull(model);
assertEquals(1, model.getCompartments().size());
......@@ -43,12 +43,13 @@ public class SbmlExporterTest {
private Model getModelAfterSerializing(String filename) throws Exception {
Model originalModel = parser.createModel(new ConverterParams().filename(filename));
String xml = exporter.toXml(originalModel);
logger.debug(xml);
ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
return parser.createModel(new ConverterParams().inputStream(stream));
}
@Test
public void testParseSpecies() throws Exception {
public void testExportSpecies() throws Exception {
Model model = getModelAfterSerializing("testFiles/layoutExample/SpeciesGlyph_Example_level2_level3.xml");
assertNotNull(model);
assertEquals(1, model.getElements().size());
......@@ -65,7 +66,7 @@ public class SbmlExporterTest {
}
@Test
public void testParseSpeciesInCompartments() throws Exception {
public void testExportSpeciesInCompartments() throws Exception {
Model model = getModelAfterSerializing("testFiles/layoutExample/SpeciesGlyph_Example.xml");
assertNotNull(model);
assertEquals(2, model.getElements().size());
......@@ -83,7 +84,7 @@ public class SbmlExporterTest {
}
@Test
public void testParseReaction() throws Exception {
public void testExportReaction() throws Exception {
Model model = getModelAfterSerializing("testFiles/layoutExample/Complete_Example.xml");
assertNotNull(model);
assertEquals(1, model.getReactions().size());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment