|
1 | 1 | import logging |
| 2 | +import os |
| 3 | +import sys |
2 | 4 | import tempfile |
3 | 5 | from os import PathLike |
4 | 6 | from pathlib import Path |
@@ -184,6 +186,32 @@ def write_antimony_file(bio_model: Biomodel, antimony_file: PathLike[str] | str) |
184 | 186 | f.write(antimony_str) |
185 | 187 |
|
186 | 188 |
|
| 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 | + |
187 | 215 | def load_vcml_str(vcml_str: str) -> Biomodel: |
188 | 216 | return VcmlReader.biomodel_from_str(vcml_str) |
189 | 217 |
|
@@ -218,6 +246,14 @@ def write_vcml_file(bio_model: Biomodel, vcml_file: PathLike[str] | str, regener |
218 | 246 | f.write(to_vcml_str(bio_model=bio_model, regenerate=regenerate)) |
219 | 247 |
|
220 | 248 |
|
| 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 | + |
221 | 257 | def load_sbml_str(sbml_str: str) -> Biomodel: |
222 | 258 | import libvcell |
223 | 259 |
|
@@ -288,3 +324,14 @@ def refresh_biomodel(bio_model: Biomodel) -> Biomodel: |
288 | 324 | vcml_path = Path(tempdir) / "model.vcml" |
289 | 325 | write_vcml_file(bio_model=bio_model, vcml_file=vcml_path) |
290 | 326 | 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