de.unirostock.sems.bives.sbml.parser.SBMLModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BiVeS-SBML Show documentation
Show all versions of BiVeS-SBML Show documentation
BiVeS - BioModel Version Control System
This package provides SBML integration for BiVeS
/**
*
*/
package de.unirostock.sems.bives.sbml.parser;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import de.binfalse.bflog.LOGGER;
import de.binfalse.bfutils.GeneralTools;
import de.unirostock.sems.bives.exception.BivesDocumentConsistencyException;
import de.unirostock.sems.bives.sbml.exception.BivesSBMLParseException;
import de.unirostock.sems.xmlutils.ds.DocumentNode;
import de.unirostock.sems.xmlutils.ds.TreeNode;
/**
* The Class SBMLModel representing a model encoded in SBML.
*
* @author Martin Scharm
*/
public class SBMLModel
extends SBMLSBase
{
/** The document. */
private SBMLDocument document;
/** The node mapper. */
private HashMap nodeMapper;
/** The meta_id mapper. */
private HashMap metaIdMapper;
/** The list of function definitions. */
private HashMap listOfFunctionDefinitions;
/** The list of unit definitions. */
private HashMap listOfUnitDefinitions;
/** The list of compartments. */
private HashMap listOfCompartments;
/** The list of compartment types. */
private HashMap listOfCompartmentTypes;
/** The list of species. */
private HashMap listOfSpecies;
/** The list of species types. */
private HashMap listOfSpeciesTypes;
/** The list of parameters. */
private HashMap listOfParameters;
/** The list of initial assignments. */
private List listOfInitialAssignments;
/** The list of rules. */
private List listOfRules;
/** The list of constraints. */
private List listOfConstraints;
/** The list of reactions. */
private HashMap listOfReactions;
/** The list of events. */
private List listOfEvents;
/** The list of species references. */
private HashMap listOfSpeciesReferences;
/** The id. */
private String id; //optional
/** The name. */
private String name; //optional
/** The substance units. */
private SBMLUnitDefinition substanceUnits; //optional
/** The time units. */
private SBMLUnitDefinition timeUnits; //optional
/** The volume units. */
private SBMLUnitDefinition volumeUnits; //optional
/** The area units. */
private SBMLUnitDefinition areaUnits; //optional
/** The length units. */
private SBMLUnitDefinition lengthUnits; //optional
/** The extent units. */
private SBMLUnitDefinition extentUnits; //optional
/** The conversion factor. */
private SBMLParameter conversionFactor; //optional
/**
* Instantiates a new SBML model.
*
* @param documentNode the document node encoding this entity in the corresponding XML tree
* @param sbmlDocument the SBML document
* @throws BivesSBMLParseException the bives sbml parse exception
* @throws BivesDocumentConsistencyException the bives document consistency exception
*/
public SBMLModel (DocumentNode documentNode, SBMLDocument sbmlDocument)
throws BivesSBMLParseException, BivesDocumentConsistencyException
{
super (documentNode, null);
sbmlModel = this;
this.document = sbmlDocument;
if (nodeMapper == null)
nodeMapper = new HashMap ();
if (metaIdMapper == null)
metaIdMapper = new HashMap ();
listOfFunctionDefinitions = new HashMap ();
listOfUnitDefinitions = new HashMap ();
listOfCompartments = new HashMap ();
listOfCompartmentTypes = new HashMap ();
listOfSpecies = new HashMap ();
listOfSpeciesTypes = new HashMap ();
listOfParameters = new HashMap ();
listOfInitialAssignments = new ArrayList ();
listOfRules = new ArrayList ();
listOfConstraints = new ArrayList ();
listOfReactions = new HashMap ();
listOfEvents = new ArrayList ();
listOfSpeciesReferences = new HashMap ();
parseTree ();
for (DocumentNode rdf : sbmlDocument.getTreeDocument ().getNodesByTag ("RDF"))
SBMLMeta.extractOntologyLinks (rdf, this);
}
/**
* Gets the ontology mappings which group entities by there pointers into ontologies.
*
* @return the ontology mappings
*/
public HashMap> getOntologyMappings ()
{
HashMap> ontomap = new HashMap> ();
for (SBMLSBase sbase : metaIdMapper.values ())
{
HashMap> links = sbase.getOntologyLinks ();
if (links.size () > 0)
{
Object [] keys = links.keySet ().toArray ();
Arrays.sort (keys);
StringBuilder sb = new StringBuilder ();
for (Object key : keys)
{
sb.append ("{").append (key).append ("=[");
List ontolinks = links.get (key);
Collections.sort (ontolinks);
for (String link : ontolinks)
sb.append (link).append (",");
}
String id = sb.toString ();
List sbases = ontomap.get (id);
if (sbases == null)
{
sbases = new ArrayList ();
ontomap.put (id, sbases);
}
sbases.add (sbase);
}
}
return ontomap;
}
/**
* Parses the tree.
*
* @throws BivesSBMLParseException the bives sbml parse exception
* @throws BivesDocumentConsistencyException the bives document consistency exception
*/
private void parseTree () throws BivesSBMLParseException, BivesDocumentConsistencyException
{
DocumentNode modelRoot = documentNode;
// sequence important!
parseFunctions (modelRoot);
parseUnits (modelRoot);
parseCompartmentTypes (modelRoot);
parseCompartments (modelRoot);
parseParameters(modelRoot);
parseSpeciesTypes (modelRoot);
parseSpecies (modelRoot);
parseReactions (modelRoot);
parseInitialAssignments (modelRoot);
parseRules (modelRoot);
parseConstraints (modelRoot);
parseEvents (modelRoot);
parseModelRoot (modelRoot);
}
/**
* Parses the model root.
*
* @param modelRoot the node rooting the model
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseModelRoot (DocumentNode modelRoot) throws BivesSBMLParseException
{
id = documentNode.getAttributeValue ("id");
name = documentNode.getAttributeValue ("name");
if (documentNode.getAttributeValue ("substanceUnits") != null)
{
String tmp = documentNode.getAttributeValue ("substanceUnits");
substanceUnits = sbmlModel.getUnitDefinition (tmp);
if (substanceUnits == null)
throw new BivesSBMLParseException ("substanceUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("timeUnits") != null)
{
String tmp = documentNode.getAttributeValue ("timeUnits");
timeUnits = sbmlModel.getUnitDefinition (tmp);
if (timeUnits == null)
throw new BivesSBMLParseException ("timeUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("volumeUnits") != null)
{
String tmp = documentNode.getAttributeValue ("volumeUnits");
volumeUnits = sbmlModel.getUnitDefinition (tmp);
if (volumeUnits == null)
throw new BivesSBMLParseException ("volumeUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("areaUnits") != null)
{
String tmp = documentNode.getAttributeValue ("areaUnits");
areaUnits = sbmlModel.getUnitDefinition (tmp);
if (areaUnits == null)
throw new BivesSBMLParseException ("areaUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("lengthUnits") != null)
{
String tmp = documentNode.getAttributeValue ("lengthUnits");
lengthUnits = sbmlModel.getUnitDefinition (tmp);
if (lengthUnits == null)
throw new BivesSBMLParseException ("lengthUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("extentUnits") != null)
{
String tmp = documentNode.getAttributeValue ("extentUnits");
extentUnits = sbmlModel.getUnitDefinition (tmp);
if (extentUnits == null)
throw new BivesSBMLParseException ("extentUnits attribute in model root not defined: " + tmp);
}
if (documentNode.getAttributeValue ("conversionFactor") != null)
{
String tmp = documentNode.getAttributeValue ("conversionFactor");
conversionFactor = sbmlModel.getParameter (tmp);
if (conversionFactor == null)
throw new BivesSBMLParseException ("conversionFactor attribute in model root not defined: " + tmp);
}
}
/**
* Parses the events.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseEvents (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfEvents");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("event");
for (int j = 0; j < node.size (); j++)
{
SBMLEvent n = new SBMLEvent ((DocumentNode) node.get (j), this);
listOfEvents.add (n);
}
}
}
/**
* Parses the reactions.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseReactions (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfReactions");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("reaction");
for (int j = 0; j < node.size (); j++)
{
SBMLReaction n = new SBMLReaction ((DocumentNode) node.get (j), this);
listOfReactions.put (n.getID (), n);
}
}
}
/**
* Parses the constraints.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseConstraints (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfConstraints");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("constraint");
for (int j = 0; j < node.size (); j++)
{
SBMLConstraint n = new SBMLConstraint ((DocumentNode) node.get (j), this);
listOfConstraints.add (n);
}
}
}
/**
* Parses the rules.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseRules (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfRules");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("algebraicRule");
for (int j = 0; j < node.size (); j++)
{
SBMLAlgebraicRule n = new SBMLAlgebraicRule ((DocumentNode) node.get (j), this);
listOfRules.add (n);
}
node = los.getChildrenWithTag ("assignmentRule");
for (int j = 0; j < node.size (); j++)
{
SBMLAssignmentRule n = new SBMLAssignmentRule ((DocumentNode) node.get (j), this);
listOfRules.add (n);
}
node = los.getChildrenWithTag ("rateRule");
for (int j = 0; j < node.size (); j++)
{
SBMLRateRule n = new SBMLRateRule ((DocumentNode) node.get (j), this);
listOfRules.add (n);
}
}
}
/**
* Parses the initial assignments.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseInitialAssignments (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfInitialAssignments");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("initialAssignment");
for (int j = 0; j < node.size (); j++)
{
SBMLInitialAssignment n = new SBMLInitialAssignment ((DocumentNode) node.get (j), this);
listOfInitialAssignments.add (n);
}
}
}
/**
* Parses the species.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseSpecies (DocumentNode root) throws BivesSBMLParseException
{
List lospeciess = root.getChildrenWithTag ("listOfSpecies");
for (int i = 0; i < lospeciess.size (); i++)
{
DocumentNode lospecies = (DocumentNode) lospeciess.get (i);
List species = lospecies.getChildrenWithTag ("species");
for (int j = 0; j < species.size (); j++)
{
SBMLSpecies s = new SBMLSpecies ((DocumentNode) species.get (j), this);
listOfSpecies.put (s.getID (), s);
}
}
}
/**
* Parses the species types.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseSpeciesTypes (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfSpeciesTypes");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("speciesType");
for (int j = 0; j < node.size (); j++)
{
SBMLSpeciesType n = new SBMLSpeciesType ((DocumentNode) node.get (j), this);
listOfSpeciesTypes.put (n.getID (), n);
}
}
}
/**
* Parses the parameters.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseParameters (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfParameters");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("parameter");
for (int j = 0; j < node.size (); j++)
{
SBMLParameter n = new SBMLParameter ((DocumentNode) node.get (j), this);
listOfParameters.put (n.getID (), n);
}
}
}
/**
* Parses the compartments.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseCompartments (DocumentNode root) throws BivesSBMLParseException
{
List locompartments = root.getChildrenWithTag ("listOfCompartments");
for (int i = 0; i < locompartments.size (); i++)
{
DocumentNode locompartment = (DocumentNode) locompartments.get (i);
List compartments = locompartment.getChildrenWithTag ("compartment");
for (int j = 0; j < compartments.size (); j++)
{
SBMLCompartment c = new SBMLCompartment ((DocumentNode) compartments.get (j), this);
listOfCompartments.put (c.getID (), c);
}
}
}
/**
* Parses the compartment types.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseCompartmentTypes (DocumentNode root) throws BivesSBMLParseException
{
List loss = root.getChildrenWithTag ("listOfCompartmentTypes");
for (int i = 0; i < loss.size (); i++)
{
DocumentNode los = (DocumentNode) loss.get (i);
List node = los.getChildrenWithTag ("compartmentType");
for (int j = 0; j < node.size (); j++)
{
SBMLCompartmentType n = new SBMLCompartmentType ((DocumentNode) node.get (j), this);
listOfCompartmentTypes.put (n.getID (), n);
}
}
}
/**
* Parses the units.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
* @throws BivesDocumentConsistencyException the bives document consistency exception
*/
private void parseUnits (DocumentNode root) throws BivesSBMLParseException, BivesDocumentConsistencyException
{
for (int i = 0; i < SBMLUnit.BASE_UNITS.length; i++)
{
SBMLUnitDefinition ud = new SBMLUnitDefinition (SBMLUnit.BASE_UNITS[i], this);
listOfUnitDefinitions.put (ud.getID (), ud);
}
List lounits = root.getChildrenWithTag ("listOfUnitDefinitions");
for (int i = 0; i < lounits.size (); i++)
{
DocumentNode lounit = (DocumentNode) lounits.get (i);
List units = lounit.getChildrenWithTag ("unitDefinition");
List problems = new ArrayList ();
boolean nextRound = true;
while (nextRound && units.size () > 0)
{
nextRound = false;
problems.clear ();
for (int j = units.size () - 1; j >= 0; j--)
{
SBMLUnitDefinition ud = null;
try
{
ud = new SBMLUnitDefinition ((DocumentNode) units.get (j), this);
String id = ud.getID ();
if (listOfUnitDefinitions.get (id) != null)
{
if (id.equals ("substance") || id.equals ("volume") || id.equals ("area") || id.equals ("length"))
LOGGER.warn ("std unit ", id, " redefined");
else
throw new BivesSBMLParseException ("Multiple definitions of unit " + ud.getID ());
}
//System.out.println ("adde unit " + id);
listOfUnitDefinitions.put (id, ud);
units.remove (j);
nextRound = true;
}
catch (BivesDocumentConsistencyException ex)
{
problems.add (ex.getMessage ());
continue;
}
}
}
if (units.size () != 0)
throw new BivesDocumentConsistencyException ("inconsistencies for "+units.size ()+" units, problems: " + problems);
}
for (int i = 0; i < SBMLUnit.PREDEFINED_UNITS.length; i++)
{
SBMLUnitDefinition ud = new SBMLUnitDefinition (SBMLUnit.PREDEFINED_UNITS[i], this);
String id = ud.getID ();
if (listOfUnitDefinitions.get (id) == null)
listOfUnitDefinitions.put (ud.getID (), ud);
}
}
/**
* Parses the functions.
*
* @param root the root
* @throws BivesSBMLParseException the bives sbml parse exception
*/
private void parseFunctions (DocumentNode root) throws BivesSBMLParseException
{
List lofunctions = root.getChildrenWithTag ("listOfFunctionDefinitions");
for (int i = 0; i < lofunctions.size (); i++)
{
DocumentNode lofunction = (DocumentNode) lofunctions.get (i);
List functions = lofunction.getChildrenWithTag ("functionDefinition");
for (int j = 0; j < functions.size (); j++)
{
SBMLFunctionDefinition fd = new SBMLFunctionDefinition ((DocumentNode) functions.get (j), this);
listOfFunctionDefinitions.put (fd.getID (), fd);
}
}
}
/**
* Gets the function definitions.
*
* @return the function definitions
*/
public HashMap getFunctionDefinitions ()
{
return listOfFunctionDefinitions;
}
/**
* Gets the unit definitions.
*
* @return the unit definitions
*/
public HashMap getUnitDefinitions ()
{
return listOfUnitDefinitions;
}
/**
* Gets the unit definition.
*
* @param kind the kind
* @return the unit definition
*/
public SBMLUnitDefinition getUnitDefinition (String kind)
{
return listOfUnitDefinitions.get (kind);
}
/**
* Gets the compartment type.
*
* @param id the id
* @return the compartment type
*/
public SBMLCompartmentType getCompartmentType (String id)
{
return listOfCompartmentTypes.get (id);
}
/**
* Gets the compartment types.
*
* @return the compartment types
*/
public HashMap getCompartmentTypes ()
{
return listOfCompartmentTypes;
}
/**
* Gets the compartments.
*
* @return the compartments
*/
public HashMap getCompartments ()
{
return listOfCompartments;
}
/**
* Gets the compartment.
*
* @param id the id
* @return the compartment
*/
public SBMLCompartment getCompartment (String id)
{
return listOfCompartments.get (id);
}
/**
* Gets the species.
*
* @param id the id
* @return the species
*/
public SBMLSpecies getSpecies (String id)
{
return listOfSpecies.get (id);
}
/**
* Gets the species.
*
* @return the species
*/
public HashMap getSpecies ()
{
return listOfSpecies;
}
/**
* Gets the species type.
*
* @param id the id
* @return the species type
*/
public SBMLSpeciesType getSpeciesType (String id)
{
return listOfSpeciesTypes.get (id);
}
/**
* Gets the species types.
*
* @return the species types
*/
public HashMap getSpeciesTypes ()
{
return listOfSpeciesTypes;
}
/**
* Gets the parameters.
*
* @return the parameters
*/
public HashMap getParameters ()
{
return listOfParameters;
}
/**
* Gets a specific parameter.
*
* @param id the id of the parameter
* @return the parameter
*/
public SBMLParameter getParameter (String id)
{
return listOfParameters.get (id);
}
/**
* Registers a species reference.
*
* @param id the id of the species reference
* @param ref the reference
*/
public void registerSpeciesReference (String id, SBMLSimpleSpeciesReference ref)
{
listOfSpeciesReferences.put (id, ref);
}
/**
* Gets the species reference.
*
* @param id the id of the species reference
* @return the species reference
*/
public SBMLSimpleSpeciesReference getSpeciesReference (String id)
{
// search for species reference w/ spec. id
return listOfSpeciesReferences.get (id);
}
/**
* Gets a specific reaction.
*
* @param id the id of the reaction
* @return the reaction
*/
public SBMLReaction getReaction (String id)
{
return listOfReactions.get (id);
}
/**
* Gets the reactions.
*
* @return the reactions
*/
public HashMap getReactions ()
{
return listOfReactions;
}
/**
* Gets the constraints.
*
* @return the constraints
*/
public List getConstraints()
{
return listOfConstraints;
}
/**
* Gets the initial assignments.
*
* @return the initial assignments
*/
public List getInitialAssignments()
{
return listOfInitialAssignments;
}
/**
* Gets the events.
*
* @return the events
*/
public List getEvents ()
{
return listOfEvents;
}
/**
* Gets the rules.
*
* @return the rules
*/
public List getRules ()
{
return listOfRules;
}
/**
* Gets the id of the model.
*
* @return the id
*/
public String getID ()
{
return id;
}
/**
* Gets the name of the model.
*
* @return the name
*/
public String getName ()
{
return name;
}
/**
* Map node model node to its entity.
*
* @param node the document node in the corresponding XML tree
* @param sbase the entity to get mapped
*/
public void mapNode (DocumentNode node, SBMLSBase sbase)
{
if (nodeMapper == null)
nodeMapper = new HashMap ();
nodeMapper.put (node, sbase);
if (metaIdMapper == null)
metaIdMapper = new HashMap ();
String metaId = sbase.getMetaId ();
if (metaId != null)
{
metaIdMapper.put (metaId, sbase);
}
}
/**
* Gets an entity given its tree node.
*
* @param node the node in the XML tree
* @return the entity registered for this node
*/
public SBMLSBase getFromNode (TreeNode node)
{
return nodeMapper.get (node);
}
/**
* Gets an entity given its meta id.
*
* @param metaId the meta id
* @return the entity registered for this meta id
*/
public SBMLSBase getFromMetaId (String metaId)
{
return metaIdMapper.get (metaId);
}
/**
* Gets the SBML document.
*
* @return the document
*/
public SBMLDocument getDocument ()
{
return document;
}
/**
* Resolve certain symbol.
*
* @param symbol the symbol
* @return the SBMLS base
* @deprecated as of version 1.3.6, replaced by
* {@link #resolveSymbol(String)}
*/
public SBMLSBase resolveSymbole (String symbol)
{
return this.resolveSymbol (symbol);
}
/**
* Resolve certain symbol.
*
* @param symbol the symbol
* @return the SBMLS base
*/
public SBMLSBase resolveSymbol (String symbol)
{
SBMLSBase entity = sbmlModel.getCompartment (symbol);
if (entity == null)
entity = sbmlModel.getSpecies (symbol);
if (entity == null)
entity = sbmlModel.getParameter (symbol);
if (entity == null)
entity = sbmlModel.getSpeciesReference (symbol);
return entity;
}
/**
* Gets the SId.
*
* @param ref the ref
* @return the sid name
*/
public static String getSidName (SBMLSBase ref)
{
if (ref instanceof SBMLParameter)
return ((SBMLParameter) ref).getNameAndId ();
if (ref instanceof SBMLSpecies)
return ((SBMLSpecies) ref).getNameAndId ();
if (ref instanceof SBMLCompartment)
return ((SBMLCompartment) ref).getNameAndId ();
if (ref instanceof SBMLSimpleSpeciesReference)
return ((SBMLSimpleSpeciesReference) ref).getSpecies ().getNameAndId ();
return null;
}
}