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

common parsing interface for bioentities exracted

parent 23a161c8
No related branches found
No related tags found
1 merge request!186Resolve "upload of sbml"
package lcsb.mapviewer.converter.model.sbml;
import java.util.HashSet;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import org.sbml.jsbml.AbstractNamedSBase;
import org.sbml.jsbml.Annotation;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.util.NotImplementedException;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData;
public class SbmlBioEntityParser {
protected Layout layout;
protected lcsb.mapviewer.model.map.model.Model minervaModel;
public SbmlBioEntityParser() {
super();
}
protected Set<MiriamData> parseAnnotation(Annotation annotation) {
if (annotation.getCVTermCount() > 0) {
throw new NotImplementedException();
}
Set<MiriamData> result = new HashSet<>();
return result;
}
protected void assignBioEntityData(AbstractNamedSBase sbmlElement, BioEntity result) throws InvalidInputDataExecption {
result.addMiriamData(parseAnnotation(sbmlElement.getAnnotation()));
result.setName(sbmlElement.getName());
if (result.getName() == null || result.getName().isEmpty()) {
result.setName(result.getElementId());
}
try {
result.setNotes(sbmlElement.getNotesString());
} catch (XMLStreamException e) {
throw new InvalidInputDataExecption(sbmlElement.getId() + " Invalid notes", e);
}
}
}
\ No newline at end of file
......@@ -3,8 +3,6 @@ package lcsb.mapviewer.converter.model.sbml;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.ListOf;
import org.sbml.jsbml.Model;
......@@ -29,16 +27,7 @@ public class SbmlCompartmentParser extends SbmlElementParser<org.sbml.jsbml.Comp
protected Compartment parse(org.sbml.jsbml.Compartment compartment, Model sbmlModel)
throws InvalidInputDataExecption {
Compartment result = new SquareCompartment(compartment.getId());
result.setMiriamData(parseAnnotation(compartment.getAnnotation()));
result.setName(compartment.getName());
if (result.getName() == null || result.getName().isEmpty()) {
result.setName(result.getElementId());
}
try {
result.setNotes(compartment.getNotesString());
} catch (XMLStreamException e) {
throw new InvalidInputDataExecption(compartment.getId() + " Invalid compartment notes", e);
}
assignBioEntityData(compartment, result);
return result;
}
......
......@@ -8,25 +8,18 @@ import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.sbml.jsbml.Annotation;
import org.sbml.jsbml.ListOf;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.ext.layout.AbstractReferenceGlyph;
import org.sbml.jsbml.ext.layout.Layout;
import org.sbml.jsbml.util.NotImplementedException;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.species.Element;
public abstract class SbmlElementParser<T extends org.sbml.jsbml.Symbol> {
public abstract class SbmlElementParser<T extends org.sbml.jsbml.Symbol> extends SbmlBioEntityParser {
Logger logger = Logger.getLogger(SbmlElementParser.class);
Layout layout;
lcsb.mapviewer.model.map.model.Model minervaModel;
public SbmlElementParser(Layout sbmlLayout, lcsb.mapviewer.model.map.model.Model minervaModel) {
this.layout = sbmlLayout;
this.minervaModel = minervaModel;
......@@ -92,12 +85,4 @@ public abstract class SbmlElementParser<T extends org.sbml.jsbml.Symbol> {
protected abstract Element parse(T species, Model sbmlModel) throws InvalidInputDataExecption;
protected Set<MiriamData> parseAnnotation(Annotation annotation) {
if (annotation.getCVTermCount() > 0) {
throw new NotImplementedException();
}
Set<MiriamData> result = new HashSet<>();
return result;
}
}
......@@ -5,6 +5,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.SimpleSpeciesReference;
import org.sbml.jsbml.Species;
......@@ -18,6 +20,7 @@ import org.sbml.jsbml.ext.layout.ReactionGlyph;
import org.sbml.jsbml.ext.layout.SpeciesReferenceGlyph;
import org.sbml.jsbml.ext.layout.SpeciesReferenceRole;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.modifier.Inhibition;
import lcsb.mapviewer.model.map.reaction.Modifier;
......@@ -40,25 +43,28 @@ public class SbmlReactionExporter extends SbmlBioEntityExporter<Reaction, org.sb
@Override
public org.sbml.jsbml.Reaction createSbmlElement(Reaction reaction) throws InconsistentModelException {
logger.debug(reaction);
org.sbml.jsbml.Reaction result = sbmlModel.createReaction("reaction_" + (getNextId()));
for (Product product : reaction.getProducts()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(product.getElement().getElementId());
SpeciesReference speciesReference = result.createProduct(sbmlSymbol);
speciesReferenceByReactionNode.put(product, speciesReference);
}
for (Reactant reactant : reaction.getReactants()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(reactant.getElement().getElementId());
SpeciesReference speciesReference = result.createReactant(sbmlSymbol);
logger.debug(reactant);
speciesReferenceByReactionNode.put(reactant, speciesReference);
}
for (Modifier modifier : reaction.getModifiers()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(modifier.getElement().getElementId());
SimpleSpeciesReference speciesReference = result.createModifier(sbmlSymbol);
speciesReferenceByReactionNode.put(modifier, speciesReference);
try {
org.sbml.jsbml.Reaction result = sbmlModel.createReaction("reaction_" + (getNextId()));
result.setNotes(reaction.getNotes());
for (Product product : reaction.getProducts()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(product.getElement().getElementId());
SpeciesReference speciesReference = result.createProduct(sbmlSymbol);
speciesReferenceByReactionNode.put(product, speciesReference);
}
for (Reactant reactant : reaction.getReactants()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(reactant.getElement().getElementId());
SpeciesReference speciesReference = result.createReactant(sbmlSymbol);
speciesReferenceByReactionNode.put(reactant, speciesReference);
}
for (Modifier modifier : reaction.getModifiers()) {
Species sbmlSymbol = speciesExporter.sbmlElementByElementId.get(modifier.getElement().getElementId());
SimpleSpeciesReference speciesReference = result.createModifier(sbmlSymbol);
speciesReferenceByReactionNode.put(modifier, speciesReference);
}
return result;
} catch (XMLStreamException e) {
throw new InvalidStateException(e);
}
return result;
}
@Override
......@@ -75,7 +81,6 @@ public class SbmlReactionExporter extends SbmlBioEntityExporter<Reaction, org.sb
@Override
protected void assignLayoutToGlyph(Reaction reaction, AbstractReferenceGlyph compartmentGlyph) {
logger.debug(reaction);
ReactionGlyph reactionGlyph = (ReactionGlyph) compartmentGlyph;
boolean firstReactant = true;
for (Reactant reactant : reaction.getReactants()) {
......@@ -112,7 +117,6 @@ public class SbmlReactionExporter extends SbmlBioEntityExporter<Reaction, org.sb
SpeciesReferenceGlyph reactantGlyph = reactionGlyph.createSpeciesReferenceGlyph("node_" + getNextId());
reactantGlyph.setSpeciesGlyph(speciesExporter.sbmlGlyphByElementId.get(node.getElement().getElementId()).getId());
reactantGlyph.setCurve(createCurve(node));
logger.debug(node);
reactantGlyph.setSpeciesReference(speciesReferenceByReactionNode.get(node));
return reactantGlyph;
}
......
......@@ -47,7 +47,7 @@ import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.modelutils.map.ElementUtils;
public class SbmlReactionParser {
public class SbmlReactionParser extends SbmlBioEntityParser {
Logger logger = Logger.getLogger(SbmlReactionParser.class);
Layout layout;
......@@ -244,6 +244,7 @@ public class SbmlReactionParser {
protected Reaction parse(org.sbml.jsbml.Reaction sbmlReaction, Model sbmlModel) throws InvalidInputDataExecption {
Reaction reaction = new StateTransitionReaction();
assignBioEntityData(sbmlReaction, reaction);
reaction.setIdReaction(sbmlReaction.getId());
reaction.setReversible(sbmlReaction.isReversible());
if (sbmlReaction.getKineticLaw() != null) {
......@@ -266,14 +267,6 @@ public class SbmlReactionParser {
return reaction;
}
protected Set<MiriamData> parseAnnotation(Annotation annotation) {
if (annotation.getCVTermCount() > 0) {
throw new NotImplementedException();
}
Set<MiriamData> result = new HashSet<>();
return result;
}
public void validateReactions(Set<Reaction> reactions) throws InvalidInputDataExecption {
for (Reaction reaction : reactions) {
if (reaction.getReactants().size() == 0) {
......
......@@ -4,8 +4,6 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.Logger;
import org.sbml.jsbml.ListOf;
import org.sbml.jsbml.Model;
......@@ -39,19 +37,9 @@ public class SbmlSpeciesParser extends SbmlElementParser<org.sbml.jsbml.Species>
if (clazz == null) {
throw new InvalidInputDataExecption("Unknown species type: " + type);
}
Species result;
try {
result = clazz.getConstructor(String.class).newInstance(species.getId());
result.setMiriamData(parseAnnotation(species.getAnnotation()));
result.setName(species.getName());
if (result.getName() == null || result.getName().isEmpty()) {
result.setName(result.getElementId());
}
try {
result.setNotes(species.getNotesString());
} catch (XMLStreamException e) {
throw new InvalidInputDataExecption(species.getId() + " Invalid Species notes", e);
}
Species result = clazz.getConstructor(String.class).newInstance(species.getId());
assignBioEntityData(species, result);
return result;
} catch (SecurityException | NoSuchMethodException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
......@@ -91,7 +79,7 @@ public class SbmlSpeciesParser extends SbmlElementParser<org.sbml.jsbml.Species>
compartment = compartment2;
}
}
}
if (compartment != null) {
compartment.addElement(element);
......
......@@ -161,6 +161,20 @@ public class SbmlExporterTest {
@Test
public void testExportReactionWithLayout() throws Exception {
Model model = createModelWithReaction();
Reaction reaction = model.getReactions().iterator().next();
Model deserializedModel = getModelAfterSerializing(model);
Reaction deserializedReaction = deserializedModel.getReactions().iterator().next();
assertEquals(reaction.getReactants().get(0).getLine().length(),
deserializedReaction.getReactants().get(0).getLine().length(), Configuration.EPSILON);
assertEquals(reaction.getProducts().get(0).getLine().length(),
deserializedReaction.getProducts().get(0).getLine().length(), Configuration.EPSILON);
}
private Model createModelWithReaction() {
Model model = new ModelFullIndexed(null);
GenericProtein p1 = new GenericProtein("s1");
p1.setWidth(100);
......@@ -188,10 +202,19 @@ public class SbmlExporterTest {
reaction.addReactant(reactant);
reaction.addProduct(product);
model.addReaction(reaction);
return model;
}
@Test
public void testExportReactionWithNotes() throws Exception {
Model model = createModelWithReaction();
Reaction reaction = model.getReactions().iterator().next();
reaction.setNotes("XYZ");
Model deserializedModel = getModelAfterSerializing(model);
Reaction deserializedReaction = deserializedModel.getReactions().iterator().next();
assertEquals(reactantLine.length(), deserializedReaction.getReactants().get(0).getLine().length(), Configuration.EPSILON);
assertEquals(productLine.length(), deserializedReaction.getProducts().get(0).getLine().length(), Configuration.EPSILON);
assertEquals(reaction.getNotes(), deserializedReaction.getNotes());
}
......
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