sys-bio / tellurium

Python Environment for Modeling and Simulating Biological Systems
http://tellurium.analogmachine.org/
Apache License 2.0
106 stars 36 forks source link

Phrasedml with urns not working any more #224

Closed matthiaskoenig closed 6 years ago

matthiaskoenig commented 7 years ago

I often use phrasedml with urns. Before the attached code worked, i.e. setting the model from urn and could run this as an experiment. This does not work anymore and can also not execute the inline_omex consisting of only phrasedml with urn model.

from __future__ import print_function
import os
import tellurium as te
from tellurium import temiriam
import phrasedml

# Get SBML from URN and set for phrasedml
urn = "urn:miriam:biomodels.db:BIOMD0000000012"
sbmlStr = temiriam.getSBMLFromBiomodelsURN(urn=urn)
phrasedml.setReferencedSBML(urn, sbmlStr)

# <SBML species>
#   PX - LacI protein
#   PY - TetR protein
#   PZ - cI protein
#   X - LacI mRNA
#   Y - TetR mRNA
#   Z - cI mRNA

# <SBML parameters>
#   ps_a - tps_active: Transcrition from free promotor in transcripts per second and promotor
#   ps_0 - tps_repr: Transcrition from fully repressed promotor in transcripts per second and promotor

phrasedml_str = """
    model1 = model "{}"
    model2 = model model1 with ps_0=1.3E-5, ps_a=0.013
    sim1 = simulate uniform(0, 1000, 1000)
    task1 = run sim1 on model1
    task2 = run sim1 on model2

    # A simple timecourse simulation
    plot "Figure 1.1 Timecourse of repressilator" task1.time vs task1.PX, task1.PZ, task1.PY

    # Applying preprocessing
    plot "Figure 1.2 Timecourse after pre-processing" task2.time vs task2.PX, task2.PZ, task2.PY

    # Applying postprocessing
    plot "Figure 1.3 Timecourse after post-processing" task1.PX/max(task1.PX) vs task1.PZ/max(task1.PZ), \
                                                       task1.PY/max(task1.PY) vs task1.PX/max(task1.PX), \
                                                       task1.PZ/max(task1.PZ) vs task1.PY/max(task1.PY)
""".format(urn)

# convert to SED-ML (this worked before, but does not anymore)
sedmlStr = phrasedml.convertString(phrasedml_str)
if sedmlStr == None:
    print(phrasedml.getLastError())
else:
    print(sedmlStr)

# execution: not possible to execute the phrasedml as inline_omex
inline_omex = phrasedml_str
te.executeInlineOmex(inline_omex)
te.exportInlineOmex(inline_omex, os.path.join('.', 'repressilator.omex'))
matthiaskoenig commented 7 years ago

Forgot the error message:

Unable to find model 'urn:miriam:biomodels.db:BIOMD0000000012', preventing phraSED-ML from creating accurate SED-ML constructs.  Try changing the working directory with 'setWorkingDirectory', or set the model directly with 'setReferencedSBML'.
Traceback (most recent call last):
  File "/home/mkoenig/git/tellurium/examples/tellurium-files/phrasedml_scripts/specificationL1V2.py", line 58, in <module>
    te.executeInlineOmex(inline_omex)
  File "/home/mkoenig/git/tellurium/tellurium/tellurium.py", line 660, in executeInlineOmex
    in_omex = teconverters.inlineOmex.fromString(inline_omex)
  File "/home/mkoenig/git/tellurium/tellurium/teconverters/inline_omex.py", line 188, in fromString
    return inlineOmex(sources)
  File "/home/mkoenig/git/tellurium/tellurium/teconverters/inline_omex.py", line 82, in __init__
    raise RuntimeError('Unable to convert PhraSEDML to SED-ML: {}'.format(phrasedml.getLastError()))
RuntimeError: Unable to convert PhraSEDML to SED-ML: Unable to find model 'urn:miriam:biomodels.db:BIOMD0000000012', preventing phraSED-ML from creating accurate SED-ML constructs.  Try changing the working directory with 'setWorkingDirectory', or set the model directly with 'setReferencedSBML'.

It seems like setting uris is not working anymore with phrasedml via

def setReferencedSBML(URI, sbmlstring):
    """
    setReferencedSBML(char const * URI, char const * sbmlstring) -> bool

    Allows phrasedml to use the given SBML document as the filename,
    instead of looking for the file on disk.  If the document is invalid
    SBML, 'false' is returned, but the document is still saved.

    Parameter 'URI' is the string that, when used in phrasedml, should
    reference the 'sbmlstring'. Parameter 'sbmlstring' is the SBML
    document string to use when the 'URI' is encountered.

    Returns a boolean indicating whether the document is valid SBML or
    not.  Either way, the document is saved as the reference document for
    the given filename string.

    """
    return _phrasedml.setReferencedSBML(URI, sbmlstring)
matthiaskoenig commented 7 years ago

This is a phrasedml issue and opened it there with code to reproduce the issue https://sourceforge.net/p/phrasedml/tickets/24/ This is some major issue, because it does not allow to write phrasedml with external model definitions via urns or links. Unfortunately there is no workaround. Converting to antimony is no solution for me because it is not completely lossless and changes the SBML structure for more complex models (with L3 packages and annotations).

0u812 commented 7 years ago

Sorry for this. When I was working on supporting inline OMEX, I made some changes to how pml handles paths which apparently broke this. Fix in phrasedml 1.0.8. Uploading to PyPI now.

The fix I made allows you to use phrasedml's convertString on this example. For executing the inlineOMEX, you need urn:miriam:biomodels.db:BIOMD0000000012 as the name of the file in the archive using the following for your inline omex string:

%model urn:miriam:biomodels.db:BIOMD0000000012
<antimony code goes here>

%tasks sedml.xml --master=True
model1 = model "urn:miriam:biomodels.db:BIOMD0000000012"
<rest of phrasedml>

If you don't actually want the SBML file to be named urn:miriam:biomodels.db:BIOMD0000000012, we might have to look into ways to support that, because inline OMEX currently requires the file name to match the model name (except possibly for a .xml extension).

matthiaskoenig commented 6 years ago

This works. Included the fix and tests for it in https://github.com/sys-bio/tellurium/pull/239

Thanks for this.