|
| 1 | +import logging |
| 2 | +import tempfile |
| 3 | +from os import PathLike |
| 4 | +from pathlib import Path |
| 5 | + |
1 | 6 | from pyvcell.vcml.models import ( |
2 | 7 | Application, |
3 | 8 | Biomodel, |
|
50 | 55 | "Application", |
51 | 56 | "Simulation", |
52 | 57 | ] |
| 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]) -> 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 load_sbml_file(sbml_file: PathLike[str]) -> Biomodel: |
| 85 | + import libvcell |
| 86 | + |
| 87 | + with tempfile.TemporaryDirectory() as tempdir: |
| 88 | + with open(sbml_file) as f: |
| 89 | + sbml_str = f.read() |
| 90 | + vcml_path = Path(tempdir) / "model.vcml" |
| 91 | + vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path) |
| 92 | + if vc_success: |
| 93 | + return VcmlReader.biomodel_from_file(vcml_path=vcml_path) |
| 94 | + else: |
| 95 | + raise ValueError("Error loading model:", vc_errmsg) |
| 96 | + |
| 97 | + |
| 98 | +def load_sbml_str(sbml_str: str) -> Biomodel: |
| 99 | + import libvcell |
| 100 | + |
| 101 | + with tempfile.TemporaryDirectory() as tempdir: |
| 102 | + vcml_path = Path(tempdir) / "model.vcml" |
| 103 | + vc_success, vc_errmsg = libvcell.sbml_to_vcml(sbml_content=sbml_str, vcml_file_path=vcml_path) |
| 104 | + if vc_success: |
| 105 | + return VcmlReader.biomodel_from_file(vcml_path=vcml_path) |
| 106 | + else: |
| 107 | + raise ValueError("Error loading model:", vc_errmsg) |
| 108 | + |
| 109 | + |
| 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 | + |
| 114 | + |
| 115 | +def to_vcml_str(bio_model: Biomodel) -> str: |
| 116 | + vcml_document = VCMLDocument(biomodel=bio_model) |
| 117 | + vcml_str: str = VcmlWriter().write_vcml(document=vcml_document) |
| 118 | + return vcml_str |
| 119 | + |
| 120 | + |
| 121 | +def refresh_biomodel(bio_model: Biomodel) -> Biomodel: |
| 122 | + with tempfile.TemporaryDirectory() as tempdir: |
| 123 | + vcml_path = Path(tempdir) / "model.vcml" |
| 124 | + write_vcml_file(bio_model=bio_model, vcml_file=vcml_path) |
| 125 | + return VcmlReader.biomodel_from_file(vcml_path=vcml_path) |
0 commit comments