Skip to content

Commit 9b62653

Browse files
authored
Merge pull request #22 from virtualcell/sysbio-course-3
upgrade fvsolver and regenerate math before solving
2 parents 1755df6 + 9b225ff commit 9b62653

File tree

6 files changed

+210
-182
lines changed

6 files changed

+210
-182
lines changed

poetry.lock

Lines changed: 11 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pyvcell"
3-
version = "0.1.12"
3+
version = "0.1.13"
44
description = "This is the python wrapper for vcell modeling and simulation"
55
authors = ["Jim Schaff <[email protected]>"]
66
repository = "https://github.com/virtualcell/pyvcell"
@@ -18,7 +18,7 @@ h5py = "^3.11.0"
1818
numpy = ">=1.26.4,<3.0"
1919
orjson = "^3.10.3"
2020
vtk = "^9.3.1"
21-
pyvcell-fvsolver = "^0.1.0"
21+
pyvcell-fvsolver = "^0.1.1"
2222
typer = ">=0.12.3,<1.0.0"
2323
pydantic = "^2.10.5"
2424
requests-oauth2client = "^1.6.0"

pyvcell/vcml/__init__.py

Lines changed: 30 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import logging
2-
import tempfile
3-
from os import PathLike
4-
from pathlib import Path
5-
61
from pyvcell.vcml.models import (
72
Application,
83
Biomodel,
@@ -26,6 +21,22 @@
2621
SurfaceClass,
2722
VCMLDocument,
2823
)
24+
from pyvcell.vcml.utils import (
25+
field_data_refs,
26+
load_antimony_file,
27+
load_antimony_str,
28+
load_sbml_file,
29+
load_sbml_str,
30+
load_vcml_file,
31+
load_vcml_str,
32+
to_antimony_str,
33+
to_sbml_str,
34+
to_vcml_str,
35+
update_biomodel,
36+
write_antimony_file,
37+
write_sbml_file,
38+
write_vcml_file,
39+
)
2940
from pyvcell.vcml.vcml_reader import VcmlReader
3041
from pyvcell.vcml.vcml_writer import VcmlWriter
3142

@@ -54,139 +65,18 @@
5465
"BoundaryType",
5566
"Application",
5667
"Simulation",
68+
"update_biomodel",
69+
"field_data_refs",
70+
"to_vcml_str",
71+
"to_sbml_str",
72+
"to_antimony_str",
73+
"load_vcml_str",
74+
"load_sbml_str",
75+
"load_antimony_str",
76+
"write_vcml_file",
77+
"write_sbml_file",
78+
"write_antimony_file",
79+
"load_vcml_file",
80+
"load_sbml_file",
81+
"load_antimony_file",
5782
]
58-
59-
60-
def load_antimony_str(antimony_str: str) -> Biomodel:
61-
import antimony # type: ignore[import-untyped]
62-
63-
antimony_success = antimony.loadAntimonyString(antimony_str)
64-
if antimony_success != -1:
65-
sbml_str = antimony.getSBMLString()
66-
sbml_str = sbml_str.replace("sboTerm", "metaid")
67-
logging.info(f"Hack - introduced a metaid in place of sboTerm to SBML string:\n{sbml_str}")
68-
return load_sbml_str(sbml_str)
69-
else:
70-
raise ValueError("Error loading model:", antimony.getLastError())
71-
72-
73-
def load_antimony_file(antimony_file: PathLike[str] | str) -> Biomodel:
74-
import antimony # ignore
75-
76-
antimony_success = antimony.loadAntimonyFile(antimony_file)
77-
if antimony_success != -1:
78-
sbml_str = antimony.getSBMLString()
79-
return load_sbml_str(sbml_str)
80-
else:
81-
raise ValueError("Error loading model:", antimony.getLastError())
82-
83-
84-
def to_antimony_str(
85-
bio_model: Biomodel, application_name: str | None = None, round_trip_validation: bool = True
86-
) -> str:
87-
sbml_str = to_sbml_str(bio_model, application_name, round_trip_validation=round_trip_validation)
88-
import antimony
89-
90-
antimony_success = antimony.loadSBMLString(sbml_str)
91-
if antimony_success != -1:
92-
antimony_str = str(antimony.getAntimonyString())
93-
return antimony_str
94-
else:
95-
raise ValueError("Error converting SBML to Antimony:", antimony.getLastError())
96-
97-
98-
def write_antimony_file(bio_model: Biomodel, antimony_file: PathLike[str] | str) -> None:
99-
antimony_str = to_antimony_str(bio_model)
100-
with open(antimony_file, "w") as f:
101-
f.write(antimony_str)
102-
103-
104-
def load_vcml_str(vcml_str: str) -> Biomodel:
105-
return VcmlReader.biomodel_from_str(vcml_str)
106-
107-
108-
def load_vcml_file(vcml_file: PathLike[str] | str) -> Biomodel:
109-
return VcmlReader.biomodel_from_file(vcml_file)
110-
111-
112-
def to_vcml_str(bio_model: Biomodel) -> str:
113-
vcml_document = VCMLDocument(biomodel=bio_model)
114-
vcml_str: str = VcmlWriter().write_vcml(document=vcml_document)
115-
return vcml_str
116-
117-
118-
def write_vcml_file(bio_model: Biomodel, vcml_file: PathLike[str] | str) -> None:
119-
vcml_document = VCMLDocument(biomodel=bio_model)
120-
VcmlWriter.write_to_file(vcml_document=vcml_document, file_path=vcml_file)
121-
122-
123-
def load_sbml_str(sbml_str: str) -> Biomodel:
124-
import libvcell
125-
126-
with tempfile.TemporaryDirectory() as tempdir:
127-
vcml_path = Path(tempdir) / "model.vcml"
128-
vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path)
129-
if vc_success:
130-
return VcmlReader.biomodel_from_file(vcml_path=vcml_path)
131-
else:
132-
raise ValueError("Error loading model:", vc_errmsg)
133-
134-
135-
def load_sbml_file(sbml_file: PathLike[str] | str) -> Biomodel:
136-
import libvcell
137-
138-
with tempfile.TemporaryDirectory() as tempdir:
139-
with open(sbml_file) as f:
140-
sbml_str = f.read()
141-
vcml_path = Path(tempdir) / "model.vcml"
142-
vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path)
143-
if vc_success:
144-
return VcmlReader.biomodel_from_file(vcml_path=vcml_path)
145-
else:
146-
raise ValueError("Error loading model:", vc_errmsg)
147-
148-
149-
def to_sbml_str(bio_model: Biomodel, application_name: str | None = None, round_trip_validation: bool = True) -> str:
150-
import libvcell
151-
152-
if application_name is None:
153-
if len(bio_model.applications) == 0:
154-
raise ValueError("sbml export from biomodel needs a biomodel application")
155-
if len(bio_model.applications) > 1:
156-
raise ValueError("Application must have exactly one application")
157-
application_name = bio_model.applications[0].name
158-
elif application_name not in [app.name for app in bio_model.applications]:
159-
raise ValueError(f"Application '{application_name}' not found in biomodel")
160-
vcml_document = VCMLDocument(biomodel=bio_model)
161-
vcml_str: str = VcmlWriter().write_vcml(document=vcml_document)
162-
with tempfile.TemporaryDirectory() as tempdir:
163-
sbml_path = Path(tempdir) / "model.sbml"
164-
success, msg = libvcell.vcml_to_sbml(
165-
vcml_content=vcml_str,
166-
application_name=application_name,
167-
sbml_file_path=sbml_path,
168-
round_trip_validation=round_trip_validation,
169-
)
170-
if not success:
171-
raise ValueError("Error converting VCML to SBML:", msg)
172-
with open(sbml_path) as f:
173-
sbml_str = f.read()
174-
return sbml_str
175-
176-
177-
def write_sbml_file(
178-
bio_model: Biomodel,
179-
sbml_file: PathLike[str] | str,
180-
application_name: str | None = None,
181-
round_trip_validation: bool = True,
182-
) -> None:
183-
sbml_str = to_sbml_str(bio_model, application_name, round_trip_validation)
184-
with open(sbml_file, "w") as f:
185-
f.write(sbml_str)
186-
187-
188-
def refresh_biomodel(bio_model: Biomodel) -> Biomodel:
189-
with tempfile.TemporaryDirectory() as tempdir:
190-
vcml_path = Path(tempdir) / "model.vcml"
191-
write_vcml_file(bio_model=bio_model, vcml_file=vcml_path)
192-
return VcmlReader.biomodel_from_file(vcml_path=vcml_path)

0 commit comments

Comments
 (0)