jigcell.xpp
Class SBMLToXPPTranslator

java.lang.Object
  extended by jigcell.xpp.SBMLToXPPTranslator
Direct Known Subclasses:
XPPLauncher

public class SBMLToXPPTranslator
extends java.lang.Object

Translates an SBML model to ODEs. This code is licensed under the DARPA BioCOMP Open Source License. See LICENSE for more details.

Author:
Nicholas Allen, Ranjit Randhawa, Marc Vass

Field Summary
private static java.util.regex.Pattern checkIfExpression
           
private static java.util.regex.Pattern checkNumber
           
private static java.util.regex.Pattern cleanName
           
private  java.util.Set compartmentIDs
           
private  java.util.List differentialIDs
           
private  jigcell.sbml2.jep.JEP expressionParser
           
private static java.util.regex.Pattern getBvarsFromFunction
           
private static java.util.regex.Pattern getMassActionParams
           
private static java.util.regex.Pattern getMichaelieMentenParams
           
private static java.util.regex.Pattern getSpeciesFromFunction
           
private static java.util.regex.Pattern getStochMassActionParams
           
private static java.util.regex.Pattern getStochMichaelieMentenParams
           
private  java.util.Set hasNoDifferentialEquationSet
           
private  java.util.Map idToOriginalValue
           
private  java.util.Map idToXPPID
           
private  int maximumIdentifierLength
           
private  jigcell.sbml2.Model model
           
private  java.util.List nonDifferentialIDs
           
private  java.lang.String odeData
           
private static boolean orderAlphbeticalSettingsList
           
private  java.util.Set parameterIDs
           
private static java.util.regex.Pattern removeSpaces
           
private  java.util.Set speciesIDs
           
private  java.util.Map speciesIDToCompartment
           
private  java.util.Map speciesIDToEquation
           
private  java.util.Map speciesToCompartmentMap
           
private  java.util.Set speciesWithInitialAmountIDs
           
private  jigcell.sbml2.math.SymbolTable symbolTable
           
private  java.io.File targetFile
           
private  java.util.Set usedFunctionNames
           
private  java.util.Map variableToEquationMap
           
private static int XPP_LINELENGTH
           
 
Constructor Summary
SBMLToXPPTranslator(jigcell.simulation.Simulator simulator, jigcell.sbml2.Model model, java.io.File targetFile, int maximumIdentifierLength)
          Creates a new translator for SBML files.
 
Method Summary
private  void applyVelocityTerm(java.lang.String velocity, boolean positive, jigcell.sbml2.SpeciesReference reference)
          Adds a single velocity term to an equation.
private static java.lang.String breakEquation(java.lang.String text)
          Breaks an equation along '+', '-', '*', '/', ',', or ' ' in order to fit on an XPP line.
private  void buildSymbolTable(jigcell.sbml2.Model model)
          Adds the name and id of all tracked SBML elements in a model to a symbol table.
private static void buildSymbolTablePump(jigcell.sbml2.math.SymbolTable table, java.util.Collection collection)
          Takes SBML elements from a collection and adds their name and id to a symbol table.
private  void calculateAllEquationsInternal()
           
 void calculateEquationsFromReactions()
           
protected  java.util.List computeSelectionList(java.util.List returnData)
          Computes the list of species to return from simulation.
private  void computeVelocities()
          Computes the velocity terms that make up the species equations.
private  java.lang.String convertCapabilitiesToXPP(jigcell.simulation.Capabilities capabilities)
          Constructs a simulator setting string that can be passed to XPP.
private  java.lang.String convertIDToXPPID(java.lang.String id, int unique)
          Converts an SBML id to an XPP id.
protected  java.lang.String convertMathToInfix(java.lang.String math, boolean rateLaw)
           
private  java.lang.String fixFunctionNames(java.lang.String text)
          Replaces function calls that use different builtin names between SBML and XPP.
private  java.lang.String fixIDs(java.lang.String text)
          Replaces ids that differ between SBML and XPP.
static void generateODEFromModelBuilder(jigcell.sbml2.Model model, java.io.File odeFile, boolean sortAlphabetically)
          Converts an SBML file to an ODE file from the ModelBuilder
private  java.lang.String generateVelocityFromMath(java.lang.String math)
          Generates the XPP velocity from a MathML string.
 java.util.List getAvailableSelections()
          A List[String] of tokens for each measurable element in the model.
 java.lang.String getConcentration(jigcell.sbml2.Species species)
           
 java.lang.String getEquation(java.lang.String math, java.lang.String equation)
           
 double getOriginalValue(java.lang.String id)
          The current value of a modifiable element in the model.
protected  java.lang.String getProgramDirectory()
          The location where XPP is installed.
 java.util.List getSettableNames()
          The name or id of every settable quantity.
static void main(java.lang.String[] args)
          Converts an SBML file to an ODE file.
 java.lang.String printParseTreeMassAction(jigcell.sbml2.math.Node curr)
           
 java.lang.String printParseTreeMichaelisMenten(jigcell.sbml2.math.Node curr)
           
 java.lang.String printParseTreeStochMassAction(jigcell.sbml2.math.Node curr, java.lang.String equation)
           
 java.lang.String printParseTreeStochMichaelisMenten(jigcell.sbml2.math.Node curr)
           
private  void registerIDs()
          Identifies all of the SBML ids used in a model and constructs acceptable XPP ids for each.
private static java.lang.String replaceFunctionName(java.lang.String text, java.lang.String oldName, java.lang.String newName)
          Replaces a function call with a function of another name.
protected  java.lang.String replaceIdWithName(java.lang.String equation)
           
 double setValue(jigcell.simulation.Simulator simulator, java.lang.String id, double value)
          Sets the value of a modifiable element in the model.
 jigcell.compare.IDataElement simulate(jigcell.simulation.Simulator simulator)
          Performs a simulation using the current model and settings.
protected  jigcell.compare.IDataElement simulate(jigcell.simulation.Simulator simulator, java.io.File targetFile, java.io.File outputFile)
          Performs a simulation using the current model and settings.
private  java.lang.String translate()
          Writes the model as an ODE file.
private  void translateCompartments(java.io.Writer writer)
          Writes equations for compartments controlled by rules to an ODE file.
private  void translateFunctions(java.io.Writer writer)
          Writes the function definitions in the model to an ODE file.
private  void translateGlobals(java.io.Writer writer)
          Writes the events in the model to an ODE file.
private  void translateParameters(java.io.Writer writer)
          Writes parameters that have been converted to equations in the model to an ODE file.
private  void translateSpecies(java.io.Writer writer)
          Writes the species defined by equations in the model to an ODE file.
private  void translateVariables(java.io.Writer writer)
          Writes the variables defined by rules in the model to an ODE file.
private  void writeSettingsAndValues(jigcell.simulation.Simulator simulator)
          Writes the parameter and initial condition settings to the ODE file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XPP_LINELENGTH

private static final int XPP_LINELENGTH
See Also:
Constant Field Values

getSpeciesFromFunction

private static final java.util.regex.Pattern getSpeciesFromFunction

getMassActionParams

private static final java.util.regex.Pattern getMassActionParams

getStochMassActionParams

private static final java.util.regex.Pattern getStochMassActionParams

getMichaelieMentenParams

private static final java.util.regex.Pattern getMichaelieMentenParams

getStochMichaelieMentenParams

private static final java.util.regex.Pattern getStochMichaelieMentenParams

getBvarsFromFunction

private static final java.util.regex.Pattern getBvarsFromFunction

checkIfExpression

private static final java.util.regex.Pattern checkIfExpression

checkNumber

private static final java.util.regex.Pattern checkNumber

cleanName

private static final java.util.regex.Pattern cleanName

removeSpaces

private static final java.util.regex.Pattern removeSpaces

orderAlphbeticalSettingsList

private static boolean orderAlphbeticalSettingsList

maximumIdentifierLength

private final int maximumIdentifierLength

targetFile

private java.io.File targetFile

differentialIDs

private java.util.List differentialIDs

nonDifferentialIDs

private java.util.List nonDifferentialIDs

idToOriginalValue

private java.util.Map idToOriginalValue

idToXPPID

private java.util.Map idToXPPID

speciesIDToCompartment

private java.util.Map speciesIDToCompartment

speciesIDToEquation

private java.util.Map speciesIDToEquation

model

private jigcell.sbml2.Model model

compartmentIDs

private java.util.Set compartmentIDs

parameterIDs

private java.util.Set parameterIDs

speciesIDs

private java.util.Set speciesIDs

speciesWithInitialAmountIDs

private java.util.Set speciesWithInitialAmountIDs

usedFunctionNames

private java.util.Set usedFunctionNames

odeData

private final java.lang.String odeData

symbolTable

private jigcell.sbml2.math.SymbolTable symbolTable

expressionParser

private jigcell.sbml2.jep.JEP expressionParser

variableToEquationMap

private java.util.Map variableToEquationMap

speciesToCompartmentMap

private java.util.Map speciesToCompartmentMap

hasNoDifferentialEquationSet

private java.util.Set hasNoDifferentialEquationSet
Constructor Detail

SBMLToXPPTranslator

public SBMLToXPPTranslator(jigcell.simulation.Simulator simulator,
                           jigcell.sbml2.Model model,
                           java.io.File targetFile,
                           int maximumIdentifierLength)
                    throws java.lang.Exception
Creates a new translator for SBML files.

Parameters:
simulator - Simulator
targetFile - File to use for the ODEs. If this is null a temporary file is created.
model - SBML model
maximumIdentifierLength - Longest permitted length for an identifier
Throws:
java.lang.Exception
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Converts an SBML file to an ODE file.

Parameters:
args - Name of SBML and ODE files
Throws:
java.lang.Exception

breakEquation

private static java.lang.String breakEquation(java.lang.String text)
Breaks an equation along '+', '-', '*', '/', ',', or ' ' in order to fit on an XPP line.


buildSymbolTablePump

private static void buildSymbolTablePump(jigcell.sbml2.math.SymbolTable table,
                                         java.util.Collection collection)
                                  throws java.lang.Exception
Takes SBML elements from a collection and adds their name and id to a symbol table.

Throws:
java.lang.Exception

replaceFunctionName

private static java.lang.String replaceFunctionName(java.lang.String text,
                                                    java.lang.String oldName,
                                                    java.lang.String newName)
Replaces a function call with a function of another name.


getAvailableSelections

public java.util.List getAvailableSelections()
A List[String] of tokens for each measurable element in the model.


getSettableNames

public java.util.List getSettableNames()
The name or id of every settable quantity.


getOriginalValue

public double getOriginalValue(java.lang.String id)
The current value of a modifiable element in the model.

Parameters:
id - SBML id

setValue

public double setValue(jigcell.simulation.Simulator simulator,
                       java.lang.String id,
                       double value)
Sets the value of a modifiable element in the model.

Parameters:
id - SBML id
value - New element value

simulate

public jigcell.compare.IDataElement simulate(jigcell.simulation.Simulator simulator)
                                      throws java.lang.Exception
Performs a simulation using the current model and settings.

Parameters:
simulator - Simulator
Throws:
java.lang.Exception

computeSelectionList

protected java.util.List computeSelectionList(java.util.List returnData)
Computes the list of species to return from simulation.

Parameters:
returnData - List[String] of columns to return when simulating

simulate

protected jigcell.compare.IDataElement simulate(jigcell.simulation.Simulator simulator,
                                                java.io.File targetFile,
                                                java.io.File outputFile)
                                         throws java.lang.Exception
Performs a simulation using the current model and settings.

Parameters:
simulator - Simulator
targetFile - ODE file
outputFile - File that will contain the simulator output
Throws:
java.lang.Exception

applyVelocityTerm

private void applyVelocityTerm(java.lang.String velocity,
                               boolean positive,
                               jigcell.sbml2.SpeciesReference reference)
                        throws java.lang.Exception
Adds a single velocity term to an equation.

Throws:
java.lang.Exception

buildSymbolTable

private void buildSymbolTable(jigcell.sbml2.Model model)
                       throws java.lang.Exception
Adds the name and id of all tracked SBML elements in a model to a symbol table.

Throws:
java.lang.Exception

computeVelocities

private void computeVelocities()
                        throws java.lang.Exception
Computes the velocity terms that make up the species equations.

Throws:
java.lang.Exception

convertCapabilitiesToXPP

private java.lang.String convertCapabilitiesToXPP(jigcell.simulation.Capabilities capabilities)
Constructs a simulator setting string that can be passed to XPP.

Parameters:
capabilities - Simulator capabilities

convertIDToXPPID

private java.lang.String convertIDToXPPID(java.lang.String id,
                                          int unique)
Converts an SBML id to an XPP id. The conversion should avoid ids differing only by case but currently does not, bug #285.


fixFunctionNames

private java.lang.String fixFunctionNames(java.lang.String text)
Replaces function calls that use different builtin names between SBML and XPP.


fixIDs

private java.lang.String fixIDs(java.lang.String text)
Replaces ids that differ between SBML and XPP.


generateVelocityFromMath

private java.lang.String generateVelocityFromMath(java.lang.String math)
                                           throws java.lang.Exception
Generates the XPP velocity from a MathML string.

Throws:
java.lang.Exception

getProgramDirectory

protected java.lang.String getProgramDirectory()
                                        throws java.lang.Exception
The location where XPP is installed.

Throws:
java.lang.Exception

registerIDs

private void registerIDs()
                  throws java.lang.Exception
Identifies all of the SBML ids used in a model and constructs acceptable XPP ids for each.

Throws:
java.lang.Exception

translate

private java.lang.String translate()
                            throws java.lang.Exception
Writes the model as an ODE file.

Throws:
java.lang.Exception

translateCompartments

private void translateCompartments(java.io.Writer writer)
                            throws java.lang.Exception
Writes equations for compartments controlled by rules to an ODE file.

Throws:
java.lang.Exception

translateFunctions

private void translateFunctions(java.io.Writer writer)
                         throws java.lang.Exception
Writes the function definitions in the model to an ODE file.

Throws:
java.lang.Exception

translateGlobals

private void translateGlobals(java.io.Writer writer)
                       throws java.lang.Exception
Writes the events in the model to an ODE file.

Throws:
java.lang.Exception

translateParameters

private void translateParameters(java.io.Writer writer)
                          throws java.lang.Exception
Writes parameters that have been converted to equations in the model to an ODE file.

Throws:
java.lang.Exception

translateSpecies

private void translateSpecies(java.io.Writer writer)
                       throws java.lang.Exception
Writes the species defined by equations in the model to an ODE file.

Throws:
java.lang.Exception

translateVariables

private void translateVariables(java.io.Writer writer)
                         throws java.lang.Exception
Writes the variables defined by rules in the model to an ODE file.

Throws:
java.lang.Exception

writeSettingsAndValues

private void writeSettingsAndValues(jigcell.simulation.Simulator simulator)
                             throws java.io.IOException
Writes the parameter and initial condition settings to the ODE file. This should be called again if those values are later changed.

Parameters:
simulator - Simulator
Throws:
java.io.IOException

generateODEFromModelBuilder

public static void generateODEFromModelBuilder(jigcell.sbml2.Model model,
                                               java.io.File odeFile,
                                               boolean sortAlphabetically)
                                        throws java.lang.Exception
Converts an SBML file to an ODE file from the ModelBuilder

Parameters:
odeFile - ODE File to save to
model - SBML model
Throws:
java.lang.Exception

calculateAllEquationsInternal

private void calculateAllEquationsInternal()

getConcentration

public java.lang.String getConcentration(jigcell.sbml2.Species species)

calculateEquationsFromReactions

public void calculateEquationsFromReactions()

convertMathToInfix

protected java.lang.String convertMathToInfix(java.lang.String math,
                                              boolean rateLaw)

getEquation

public java.lang.String getEquation(java.lang.String math,
                                    java.lang.String equation)

printParseTreeMassAction

public java.lang.String printParseTreeMassAction(jigcell.sbml2.math.Node curr)

printParseTreeStochMassAction

public java.lang.String printParseTreeStochMassAction(jigcell.sbml2.math.Node curr,
                                                      java.lang.String equation)

printParseTreeMichaelisMenten

public java.lang.String printParseTreeMichaelisMenten(jigcell.sbml2.math.Node curr)

printParseTreeStochMichaelisMenten

public java.lang.String printParseTreeStochMichaelisMenten(jigcell.sbml2.math.Node curr)

replaceIdWithName

protected java.lang.String replaceIdWithName(java.lang.String equation)