Skip to content

Commit e6c4fc7

Browse files
committed
add from/to files and strings for VCML, SBML, Antimony
1 parent 51b3fe3 commit e6c4fc7

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

pyvcell/vcml/__init__.py

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def load_antimony_str(antimony_str: str) -> Biomodel:
7070
raise ValueError("Error loading model:", antimony.getLastError())
7171

7272

73-
def load_antimony_file(antimony_file: PathLike[str]) -> Biomodel:
73+
def load_antimony_file(antimony_file: PathLike[str] | str) -> Biomodel:
7474
import antimony # ignore
7575

7676
antimony_success = antimony.loadAntimonyFile(antimony_file)
@@ -81,12 +81,49 @@ def load_antimony_file(antimony_file: PathLike[str]) -> Biomodel:
8181
raise ValueError("Error loading model:", antimony.getLastError())
8282

8383

84-
def load_sbml_file(sbml_file: PathLike[str]) -> Biomodel:
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:
85124
import libvcell
86125

87126
with tempfile.TemporaryDirectory() as tempdir:
88-
with open(sbml_file) as f:
89-
sbml_str = f.read()
90127
vcml_path = Path(tempdir) / "model.vcml"
91128
vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path)
92129
if vc_success:
@@ -95,10 +132,12 @@ def load_sbml_file(sbml_file: PathLike[str]) -> Biomodel:
95132
raise ValueError("Error loading model:", vc_errmsg)
96133

97134

98-
def load_sbml_str(sbml_str: str) -> Biomodel:
135+
def load_sbml_file(sbml_file: PathLike[str] | str) -> Biomodel:
99136
import libvcell
100137

101138
with tempfile.TemporaryDirectory() as tempdir:
139+
with open(sbml_file) as f:
140+
sbml_str = f.read()
102141
vcml_path = Path(tempdir) / "model.vcml"
103142
vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path)
104143
if vc_success:
@@ -107,15 +146,43 @@ def load_sbml_str(sbml_str: str) -> Biomodel:
107146
raise ValueError("Error loading model:", vc_errmsg)
108147

109148

110-
def write_vcml_file(bio_model: Biomodel, vcml_file: PathLike[str]) -> None:
111-
vcml_document = VCMLDocument(biomodel=bio_model)
112-
VcmlWriter.write_to_file(vcml_document=vcml_document, file_path=vcml_file)
113-
149+
def to_sbml_str(bio_model: Biomodel, application_name: str | None = None, round_trip_validation: bool = True) -> str:
150+
import libvcell
114151

115-
def to_vcml_str(bio_model: Biomodel) -> str:
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")
116160
vcml_document = VCMLDocument(biomodel=bio_model)
117161
vcml_str: str = VcmlWriter().write_vcml(document=vcml_document)
118-
return vcml_str
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)
119186

120187

121188
def refresh_biomodel(bio_model: Biomodel) -> Biomodel:

0 commit comments

Comments
 (0)