sys-bio / roadrunner

libRoadRunner: A high-performance SBML simulator
http://libroadrunner.org/
Other
36 stars 24 forks source link

Missing initial assignments after loading model #1189

Closed dalbabur closed 4 months ago

dalbabur commented 4 months ago

I have an Antimony model with Initial Assignments. I can do r = te.loada(myAntimonyModel) and r.getInitialAssignmentIds() and see all the assignments just fine. After saving the model to SBML or binary and reloading, r.getInitialAssignmentIds() returns nothing.

The SBML file does not contain anything under <listOfInitialAssignments/>, so maybe initial assignments are not being saved correctly?

hsauro commented 4 months ago

I just tried this small example:

import tellurium as te

r = te.loada(""" k = 1.2 + 5.6 """)

The call to r.getInitialAssignmentIds() worked ok as it did with you. When I obtained the sbml it looked ok as well. See below. There must be something about your model, any chance you could send us the model to take a look?

<listOfInitialAssignments>
  <initialAssignment symbol="k">
    <math xmlns="http://www.w3.org/1998/Math/MathML">
      <apply>
        <plus/>
        <cn> 1.2 </cn>
        <cn> 5.6 </cn>
      </apply>
    </math>
  </initialAssignment>
</listOfInitialAssignments>

On Sun, Mar 3, 2024 at 4:29 PM Diego Alba @.***> wrote:

I have an Antimony model with Initial Assignments. I can do r = te.loada(myAntimonyModel) and r.getInitialAssignmentIds() and see all the assignments just fine. After saving the model to SBML or binary and reloading, r.getInitialAssignmentIds() returns nothing.

The SBML file does not contain anything under , so maybe initial assignments are not being saved correctly?

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/sys-bio/roadrunner/issues/1189__;!!K-Hz7m0Vt54!n7BTR5LflByulLNqyrJsB3ILWqoByyHjVpR8lqgjrDpNNXkOpTrf5UWWDKDzso-zMV2nfA47-L95B_ndRF7ieTuqKZt4Lw$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AAIBSDX2MJWDVLE4SDVMURDYWO57NAVCNFSM6AAAAABEEMGT66VHI2DSMVQWIX3LMV43ASLTON2WKOZSGE3DKNRWGUZTONI__;!!K-Hz7m0Vt54!n7BTR5LflByulLNqyrJsB3ILWqoByyHjVpR8lqgjrDpNNXkOpTrf5UWWDKDzso-zMV2nfA47-L95B_ndRF7ieTvUDEg4Xw$ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Herbert Sauro, Professor Director: NIH Center for model reproducibility University of Washington, Bioengineering 206-685-2119, www.sys-bio.org, http://reproduciblebiomodels.org/ Mobile: 206-880-8093 @.*** Books: http://books.analogmachine.org/

dalbabur commented 4 months ago

I tried the simple example as well. In both cases, I see the initial assignments with r.getSBML(), but not when exported directly with r.exportToSBML() or r.saveState().

The workaround I've found is to r.getSBML() or r.saveStateS() and write the string/bytes to disk myself. I can see the initial assignments when saving and importing this way.

hsauro commented 4 months ago

Yes I can confirm what you see. Odd that exportSBML doesn't work properly. I've always used getSBML however and then used the tellurium help method saveToFile since I can never remember the python way of saving files. I actually didn't realise we had exportSBML that took a filename directly.

There should, I imagine, be an easy fix for this.

Herbert

On Mon, Mar 4, 2024 at 11:15 AM Diego Alba @.***> wrote:

I tried the simple example as well. In both cases, I see the initial assignments with r.getSBML(), but not when exported directly with r.exportToSBML() or r.saveState().

The workaround I've found is to r.getSBML() or r.saveStateS() and write the string/bytes to disk myself. I can see the initial assignments when saving and importing this way.

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/sys-bio/roadrunner/issues/1189*issuecomment-1977284544__;Iw!!K-Hz7m0Vt54!iGPPEMWb6s1HRjhEXyB2jC4CIl8q9yTptPpZzE03qcrfiz03tyNu2K0mln1gQpztH2PtpdJiYFfsKpYAfCESu2tHa_gLpw$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AAIBSDRFIZEDNFJG2CYWLP3YWTB4TAVCNFSM6AAAAABEEMGT66VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZXGI4DINJUGQ__;!!K-Hz7m0Vt54!iGPPEMWb6s1HRjhEXyB2jC4CIl8q9yTptPpZzE03qcrfiz03tyNu2K0mln1gQpztH2PtpdJiYFfsKpYAfCESu2uYWEQS2g$ . You are receiving this because you commented.Message ID: @.***>

-- Herbert Sauro, Professor Director: NIH Center for model reproducibility University of Washington, Bioengineering 206-685-2119, www.sys-bio.org, http://reproduciblebiomodels.org/ Mobile: 206-880-8093 @.*** Books: http://books.analogmachine.org/

adelhpour commented 4 months ago

My guess is the problem originates from the fact that 'exportToSBML' function has a flag called 'current', which is set to True by default, meaning the current state of the model is exported. In other words, this function calls 'getCurrentSBML' when this parameter is not passed to it. Could you just try calling this function by setting its 'current' flag to False?

dalbabur commented 4 months ago

ah yes, that is it. but why does r.getCurrentSBML() skip the initial assignments?

I have a model with many initial assignments, and many parameters with NaN values. I want to load the model, replace the NaN values, and save it again. Right now r.getSBML() gets me the initial assignments, but also NaNs since that's how the model was first loaded. With r.getCurrentSBML(), I get the new parameter values, but not the initial assignments. How can I get both?

adelhpour commented 4 months ago

I'm not sure, but @luciansmith might have some ideas on it.

luciansmith commented 4 months ago

Roadrunner does not export initial assignments to 'getCurrentSBML' because the model is not at its initial state, but some other state. If it wrote out initial assignments, those would override the actual current values. This was actually a bug we fixed relatively recently. Consider:

import tellurium as te

r = te.loada("""
      k1 = 1.2 + 5.6
      k2 = 5
""")

r.k1 = 3
r.k2 = 3

r.getCurrentSBML()

If roadrunner wrote out the old initial assignments, r.getCurrentSBML() would export a value of 3 for k2, but 1.2+5.6 for k1, which is incorrect.

It should be noted that if the value of the element hasn't changed from its initial assignment, the value will be correct. It just won't be the original formula.

Similarly 'saveState' saves the current state of roadrunner, which will be incorrect if it exports initial assignments.

As @adelhpour mentioned, you can get the initial SBML object from exportToSBML by using:

r.exportToSBML("filename.xml", current=False)

If I understand your NaN/Initial assignment question correctly, what you want is to set the initial value of your changed parameters, not the current values:

r.setValue("init(k1)", 3.2)
r.setValue("init(k2)", 2.4)

...except this doesn't work, which is definitely a bug. I'll work on that next!

dalbabur commented 4 months ago

okay that makes more sense, thank you!