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

kinetics parser added

parent 6ca6a3f5
No related branches found
No related tags found
1 merge request!186Resolve "upload of sbml"
Showing
with 4954 additions and 4238 deletions
This diff is collapsed.
package lcsb.mapviewer.converter.model.celldesigner.reaction;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.w3c.dom.Node;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.parameter.ParameterCollectionXmlParser;
import lcsb.mapviewer.model.map.kinetics.SbmlArgument;
import lcsb.mapviewer.model.map.kinetics.SbmlKinetics;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.species.Element;
public class KineticsXmlParser extends XmlParser {
Logger logger = Logger.getLogger(KineticsXmlParser.class);
ParameterCollectionXmlParser parameterParser;
Model model;
public KineticsXmlParser(Model model) {
this.model = model;
parameterParser = new ParameterCollectionXmlParser(model);
}
public SbmlKinetics parseKinetics(Node kineticsNode, Map<String, Element> elements) throws InvalidXmlSchemaException {
SbmlKinetics result = new SbmlKinetics();
Node mathNode = super.getNode("math", kineticsNode);
if (mathNode == null) {
throw new InvalidXmlSchemaException("kineticLaw node doesn't have math subnode");
}
Node parametersNode = super.getNode("listOfParameters", kineticsNode);
if (parametersNode != null) {
result.addParameters(parameterParser.parseXmlParameterCollection(parametersNode));
}
Set<SbmlArgument> elementsUsedInKinetics = new HashSet<>();
for (Node ciNode : super.getAllNotNecessirellyDirectChild("ci", mathNode)) {
String id = super.getNodeValue(ciNode).trim();
SbmlArgument element = elements.get(id);
if (element == null) {
element = result.getParameterById(id);
}
if (element == null) {
element = model.getParameterById(id);
}
if (element == null) {
element = model.getFunctionById(id);
}
if (element == null) {
throw new InvalidXmlSchemaException("Unknown symbol in kinetics: " + id);
}
elementsUsedInKinetics.add(element);
}
result.addArguments(elementsUsedInKinetics);
return result;
}
}
...@@ -5,14 +5,15 @@ import org.junit.runners.Suite; ...@@ -5,14 +5,15 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ ReactionCollectionXmlParserTest.class, // @SuiteClasses({ KineticsXmlParserTest.class, //
ReactionFromXmlTest.class, // ReactionCollectionXmlParserTest.class, //
ReactionLineDataTest.class, // ReactionFromXmlTest.class, //
ReactionParserExceptionTest.class, // ReactionLineDataTest.class, //
ReactionParserTests.class, // ReactionParserExceptionTest.class, //
ReactionToXmlTest.class, // ReactionParserTests.class, //
UnknownModifierClassExceptionTest.class, // ReactionToXmlTest.class, //
UnknownReactionClassExceptionTest.class,// UnknownModifierClassExceptionTest.class, //
UnknownReactionClassExceptionTest.class,//
}) })
public class AllReactionTests { public class AllReactionTests {
......
package lcsb.mapviewer.converter.model.celldesigner.reaction;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.w3c.dom.Node;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerTestFunctions;
import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
import lcsb.mapviewer.model.map.kinetics.SbmlKinetics;
import lcsb.mapviewer.model.map.kinetics.SbmlParameter;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.GenericProtein;
public class KineticsXmlParserTest extends CellDesignerTestFunctions {
@Test
public void testParserElements() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = getTestElementMap();
Model model = new ModelFullIndexed(null);
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/simple.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(2, kinetics.getArguments().size());
}
@Test
public void testParserParameters() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = getTestElementMap();
Model model = new ModelFullIndexed(null);
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_parameter.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(1, kinetics.getParameters().size());
}
@Test
public void testParserWithUsedParameters() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = getTestElementMap();
Model model = new ModelFullIndexed(null);
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_used_parameter.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(1, kinetics.getParameters().size());
}
@Test
public void testParserGlobalParameters() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = getTestElementMap();
Model model = new ModelFullIndexed(null);
model.addParameter(new SbmlParameter("k1"));
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_global_parameter.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(1, kinetics.getParameters().size());
}
@Test
public void testParserGlobalFunction() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = getTestElementMap();
Model model = new ModelFullIndexed(null);
model.addFunction(new SbmlFunction("fun"));
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/with_function.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(1, kinetics.getFunctions().size());
}
@Test(expected = InvalidXmlSchemaException.class)
public void testParserNonExistingElements() throws InvalidXmlSchemaException, IOException {
Map<String, Element> elements = new HashMap<>();
Model model = new ModelFullIndexed(null);
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/simple.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
parser.parseKinetics(node, elements);
}
@Test
public void testParserElementDuplicate() throws InvalidXmlSchemaException, IOException {
Model model = new ModelFullIndexed(null);
Map<String, Element> elements = getTestElementMap();
Node node = super.getXmlDocumentFromFile("testFiles/kinetics/math_using_one_element.xml").getFirstChild();
KineticsXmlParser parser = new KineticsXmlParser(model);
SbmlKinetics kinetics = parser.parseKinetics(node, elements);
assertNotNull(kinetics);
assertEquals(1, kinetics.getArguments().size());
}
private Map<String, Element> getTestElementMap() {
Map<String, Element> elements = new HashMap<>();
elements.put("s1", new GenericProtein("s1"));
elements.put("s2", new GenericProtein("s2"));
return elements;
}
}
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> s1 </ci>
<ci> s1 </ci>
</apply>
</math>
</kineticLaw>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> s1 </ci>
<ci> s2 </ci>
</apply>
</math>
</kineticLaw>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> fun </ci>
<ci> s2 </ci>
</apply>
</math>
</kineticLaw>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> k1 </ci>
<ci> s2 </ci>
</apply>
</math>
</kineticLaw>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> s1 </ci>
<ci> s2 </ci>
</apply>
</math>
<listOfParameters>
<parameter metaid="k2" id="k2" name="local param" value="0" units="substance"/>
</listOfParameters>
</kineticLaw>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> s1 </ci>
<ci> k2 </ci>
</apply>
</math>
<listOfParameters>
<parameter metaid="k2" id="k2" name="local param" value="0" units="substance"/>
</listOfParameters>
</kineticLaw>
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level2/version4" xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" level="2" version="4">
<model metaid="untitled" id="untitled">
<annotation>
<celldesigner:extension>
<celldesigner:modelVersion>4.0</celldesigner:modelVersion>
<celldesigner:modelDisplay sizeX="600" sizeY="400"/>
<celldesigner:listOfCompartmentAliases/>
<celldesigner:listOfComplexSpeciesAliases/>
<celldesigner:listOfSpeciesAliases>
<celldesigner:speciesAlias id="sa1" species="s1">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="67.0" y="151.0" w="80.0" h="40.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="40.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffccffcc" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="0.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa2" species="s2">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="296.0" y="174.0" w="80.0" h="40.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="40.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffccffcc" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="0.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
</celldesigner:listOfSpeciesAliases>
<celldesigner:listOfGroups/>
<celldesigner:listOfProteins>
<celldesigner:protein id="pr1" name="s1" type="GENERIC"/>
<celldesigner:protein id="pr2" name="s2" type="GENERIC"/>
</celldesigner:listOfProteins>
<celldesigner:listOfGenes/>
<celldesigner:listOfRNAs/>
<celldesigner:listOfAntisenseRNAs/>
<celldesigner:listOfLayers/>
<celldesigner:listOfBlockDiagrams/>
</celldesigner:extension>
</annotation>
<listOfUnitDefinitions>
<unitDefinition metaid="substance" id="substance" name="substance">
<listOfUnits>
<unit metaid="CDMT00004" kind="mole"/>
</listOfUnits>
</unitDefinition>
<unitDefinition metaid="volume" id="volume" name="volume">
<listOfUnits>
<unit metaid="CDMT00005" kind="litre"/>
</listOfUnits>
</unitDefinition>
<unitDefinition metaid="area" id="area" name="area">
<listOfUnits>
<unit metaid="CDMT00006" kind="metre" exponent="2"/>
</listOfUnits>
</unitDefinition>
<unitDefinition metaid="length" id="length" name="length">
<listOfUnits>
<unit metaid="CDMT00007" kind="metre"/>
</listOfUnits>
</unitDefinition>
<unitDefinition metaid="time" id="time" name="time">
<listOfUnits>
<unit metaid="CDMT00008" kind="second"/>
</listOfUnits>
</unitDefinition>
</listOfUnitDefinitions>
<listOfCompartments>
<compartment metaid="default" id="default" size="1" units="volume"/>
</listOfCompartments>
<listOfSpecies>
<species metaid="s1" id="s1" name="s1" compartment="default" initialAmount="0">
<annotation>
<celldesigner:extension>
<celldesigner:positionToCompartment>inside</celldesigner:positionToCompartment>
<celldesigner:speciesIdentity>
<celldesigner:class>PROTEIN</celldesigner:class>
<celldesigner:proteinReference>pr1</celldesigner:proteinReference>
</celldesigner:speciesIdentity>
</celldesigner:extension>
</annotation>
</species>
<species metaid="s2" id="s2" name="s2" compartment="default" initialAmount="0">
<annotation>
<celldesigner:extension>
<celldesigner:positionToCompartment>inside</celldesigner:positionToCompartment>
<celldesigner:speciesIdentity>
<celldesigner:class>PROTEIN</celldesigner:class>
<celldesigner:proteinReference>pr2</celldesigner:proteinReference>
</celldesigner:speciesIdentity>
</celldesigner:extension>
</annotation>
</species>
</listOfSpecies>
<listOfReactions>
<reaction metaid="re1" id="re1" reversible="false" fast="false">
<annotation>
<celldesigner:extension>
<celldesigner:reactionType>STATE_TRANSITION</celldesigner:reactionType>
<celldesigner:baseReactants>
<celldesigner:baseReactant species="s1" alias="sa1">
<celldesigner:linkAnchor position="INACTIVE"/>
</celldesigner:baseReactant>
</celldesigner:baseReactants>
<celldesigner:baseProducts>
<celldesigner:baseProduct species="s2" alias="sa2">
<celldesigner:linkAnchor position="INACTIVE"/>
</celldesigner:baseProduct>
</celldesigner:baseProducts>
<celldesigner:connectScheme connectPolicy="direct" rectangleIndex="0">
<celldesigner:listOfLineDirection>
<celldesigner:lineDirection index="0" value="unknown"/>
</celldesigner:listOfLineDirection>
</celldesigner:connectScheme>
<celldesigner:line width="1.0" color="ff000000"/>
</celldesigner:extension>
</annotation>
<listOfReactants>
<speciesReference metaid="CDMT00001" species="s1">
<annotation>
<celldesigner:extension>
<celldesigner:alias>sa1</celldesigner:alias>
</celldesigner:extension>
</annotation>
</speciesReference>
</listOfReactants>
<listOfProducts>
<speciesReference metaid="CDMT00002" species="s2">
<annotation>
<celldesigner:extension>
<celldesigner:alias>sa2</celldesigner:alias>
</celldesigner:extension>
</annotation>
</speciesReference>
</listOfProducts>
<kineticLaw metaid="CDMT00003">
<annotation/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> s1 </ci>
<ci> s2 </ci>
</apply>
</math>
</kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>
package lcsb.mapviewer.model.map.kinetics;
public interface SbmlArgument {
}
...@@ -28,7 +28,7 @@ import org.hibernate.annotations.IndexColumn; ...@@ -28,7 +28,7 @@ import org.hibernate.annotations.IndexColumn;
@Table(name = "sbml_function") @Table(name = "sbml_function")
@org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment") @org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment")
@XmlRootElement @XmlRootElement
public class SbmlFunction implements Serializable { public class SbmlFunction implements Serializable, SbmlArgument {
/** /**
* Default class logger. * Default class logger.
......
package lcsb.mapviewer.model.map.kinetics;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import lcsb.mapviewer.model.map.species.Element;
/**
* Representation of a single SBML function
*
* @author Piotr Gawron
*
*/
@Entity
@Table(name = "sbml_kinetics")
@org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment")
@XmlRootElement
public class SbmlKinetics implements Serializable {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(SbmlKinetics.class);
/**
*
*/
private static final long serialVersionUID = 1L;
@Cascade({ CascadeType.ALL })
@OneToMany(mappedBy = "model", orphanRemoval = true)
private Set<SbmlParameter> parameters = new HashSet<>();
@Cascade({ CascadeType.ALL })
@OneToMany(mappedBy = "model", orphanRemoval = true)
private Set<SbmlFunction> functions = new HashSet<>();
private List<Element> elements = new ArrayList<>();
/**
* Unique database identifier.
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idDb", unique = true, nullable = false)
private int id;
@Column(columnDefinition = "TEXT")
private String definition;
public void addParameter(SbmlParameter parameter) {
parameters.add(parameter);
}
public Set<SbmlParameter> getParameters() {
return parameters;
}
public List<SbmlArgument> getArguments() {
List<SbmlArgument> arguments = new ArrayList<>();
arguments.addAll(parameters);
arguments.addAll(elements);
arguments.addAll(functions);
return arguments;
}
public void addElement(Element element) {
this.elements.add(element);
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
public void addElements(Set<Element> elements) {
for (Element element : elements) {
addElement(element);
}
}
public void addParameters(Set<SbmlParameter> parameters) {
for (SbmlParameter parameter : parameters) {
addParameter(parameter);
}
}
public void addArguments(Set<SbmlArgument> elementsUsedInKinetics) {
for (SbmlArgument argument : elementsUsedInKinetics) {
addArgument(argument);
}
}
public void addArgument(SbmlArgument argument) {
if (argument instanceof SbmlParameter) {
addParameter((SbmlParameter) argument);
} else if (argument instanceof SbmlFunction) {
addFunction((SbmlFunction) argument);
} else {
addElement((Element) argument);
}
}
public void addFunction(SbmlFunction argument) {
functions.add(argument);
}
public SbmlParameter getParameterById(String id) {
for (SbmlParameter parameter : parameters) {
if (parameter.getParameterId().equals(id)) {
return parameter;
}
}
return null;
}
public Set<SbmlFunction> getFunctions() {
return functions;
}
}
...@@ -22,7 +22,7 @@ import org.apache.log4j.Logger; ...@@ -22,7 +22,7 @@ import org.apache.log4j.Logger;
@Table(name = "sbml_parameter") @Table(name = "sbml_parameter")
@org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment") @org.hibernate.annotations.GenericGenerator(name = "test-increment-strategy", strategy = "increment")
@XmlRootElement @XmlRootElement
public class SbmlParameter implements Serializable { public class SbmlParameter implements Serializable, SbmlArgument {
/** /**
* Default class logger. * Default class logger.
......
...@@ -10,6 +10,7 @@ import lcsb.mapviewer.model.map.BioEntity; ...@@ -10,6 +10,7 @@ import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.compartment.Compartment; import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.graph.DataMiningSet; import lcsb.mapviewer.model.map.graph.DataMiningSet;
import lcsb.mapviewer.model.map.kinetics.SbmlArgument;
import lcsb.mapviewer.model.map.kinetics.SbmlFunction; import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
import lcsb.mapviewer.model.map.kinetics.SbmlParameter; import lcsb.mapviewer.model.map.kinetics.SbmlParameter;
import lcsb.mapviewer.model.map.kinetics.SbmlUnit; import lcsb.mapviewer.model.map.kinetics.SbmlUnit;
...@@ -607,4 +608,12 @@ public interface Model { ...@@ -607,4 +608,12 @@ public interface Model {
SbmlUnit getUnitsByUnitId(String unitId); SbmlUnit getUnitsByUnitId(String unitId);
void addParameters(Collection<SbmlParameter> sbmlParamters); void addParameters(Collection<SbmlParameter> sbmlParamters);
void addParameter(SbmlParameter sbmlParameter);
SbmlParameter getParameterById(String parameterId);
void addFunction(SbmlFunction sbmlFunction);
SbmlArgument getFunctionById(String id);
} }
...@@ -774,7 +774,15 @@ public class ModelData implements Serializable { ...@@ -774,7 +774,15 @@ public class ModelData implements Serializable {
this.units.add(unit); this.units.add(unit);
} }
public void addParameters(Collection<SbmlParameter> sbmlParamters) { public void addParameters(Collection<SbmlParameter> sbmlParameters) {
this.parameters.addAll(sbmlParamters); this.parameters.addAll(sbmlParameters);
}
public void addParameter(SbmlParameter sbmlParameter) {
this.parameters.add(sbmlParameter);
}
public void addFunction(SbmlFunction sbmlFunction) {
this.functions.add(sbmlFunction);
} }
} }
\ No newline at end of file
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