|
1 |
| -import logging |
2 |
| -import tempfile |
3 |
| -from os import PathLike |
4 |
| -from pathlib import Path |
5 |
| - |
6 | 1 | from pyvcell.vcml.models import (
|
7 | 2 | Application,
|
8 | 3 | Biomodel,
|
|
26 | 21 | SurfaceClass,
|
27 | 22 | VCMLDocument,
|
28 | 23 | )
|
| 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 | +) |
29 | 40 | from pyvcell.vcml.vcml_reader import VcmlReader
|
30 | 41 | from pyvcell.vcml.vcml_writer import VcmlWriter
|
31 | 42 |
|
|
54 | 65 | "BoundaryType",
|
55 | 66 | "Application",
|
56 | 67 | "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", |
57 | 82 | ]
|
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