Skip to content

Commit 4f1f714

Browse files
committed
load vcml or sbml from URLs to support colab use of example data
1 parent d23ef61 commit 4f1f714

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pyvcell/vcml/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
field_data_refs,
2727
load_antimony_file,
2828
load_antimony_str,
29+
load_sbml_url,
2930
load_sbml_file,
3031
load_sbml_str,
32+
load_vcml_url,
3133
load_vcml_file,
3234
load_vcml_str,
3335
to_antimony_str,
@@ -37,6 +39,8 @@
3739
write_antimony_file,
3840
write_sbml_file,
3941
write_vcml_file,
42+
suppress_stdout,
43+
restore_stdout,
4044
)
4145
from pyvcell.vcml.vcml_reader import VcmlReader
4246
from pyvcell.vcml.vcml_simulation import simulate
@@ -86,4 +90,6 @@
8690
"set_workspace_dir",
8791
"simulate",
8892
"Field",
93+
"load_vcml_url",
94+
"load_sbml_url",
8995
]

pyvcell/vcml/utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
import os
3+
import sys
24
import tempfile
35
from os import PathLike
46
from pathlib import Path
@@ -184,6 +186,32 @@ def write_antimony_file(bio_model: Biomodel, antimony_file: PathLike[str] | str)
184186
f.write(antimony_str)
185187

186188

189+
def _download_url(url: str) -> str:
190+
import requests
191+
192+
response = requests.get(url=url, timeout=10)
193+
if response.status_code == 200:
194+
return response.text
195+
else:
196+
raise ValueError(f"Failed to download file from {url}: {response.status_code}")
197+
198+
199+
def load_vcml_biomodel_id(biomodel_id: str) -> Biomodel:
200+
"""
201+
Load a VCML model from a VCell Biomodel ID.
202+
"""
203+
uri = f"https://vcell.cam.uchc.edu/api/v0/biomodel/{biomodel_id}/biomodel.vcml"
204+
return load_vcml_url(uri)
205+
206+
207+
def load_vcml_url(vcml_url: str) -> Biomodel:
208+
"""
209+
Load a VCML model from a URL.
210+
"""
211+
vcml_str = _download_url(vcml_url)
212+
return load_vcml_str(vcml_str)
213+
214+
187215
def load_vcml_str(vcml_str: str) -> Biomodel:
188216
return VcmlReader.biomodel_from_str(vcml_str)
189217

@@ -218,6 +246,14 @@ def write_vcml_file(bio_model: Biomodel, vcml_file: PathLike[str] | str, regener
218246
f.write(to_vcml_str(bio_model=bio_model, regenerate=regenerate))
219247

220248

249+
def load_sbml_url(sbml_url: str) -> Biomodel:
250+
"""
251+
Load a SBML model from a URL.
252+
"""
253+
sbml_str = _download_url(sbml_url)
254+
return load_sbml_str(sbml_str)
255+
256+
221257
def load_sbml_str(sbml_str: str) -> Biomodel:
222258
import libvcell
223259

@@ -288,3 +324,14 @@ def refresh_biomodel(bio_model: Biomodel) -> Biomodel:
288324
vcml_path = Path(tempdir) / "model.vcml"
289325
write_vcml_file(bio_model=bio_model, vcml_file=vcml_path)
290326
return VcmlReader.biomodel_from_file(vcml_path=vcml_path)
327+
328+
329+
def suppress_stdout():
330+
sys.stdout.flush() # Ensure all Python-level stdout is flushed
331+
devnull = os.open(os.devnull, os.O_WRONLY)
332+
os.dup2(devnull, sys.stdout.fileno())
333+
334+
335+
def restore_stdout():
336+
sys.stdout.flush()
337+
os.dup2(sys.__stdout__.fileno(), sys.stdout.fileno())

0 commit comments

Comments
 (0)