def json_for_sbml(uid: str, source: Union[Path, str, bytes]) -> Dict: """Create JSON content for given SBML source. Source is either path to SBML file or SBML string. """ if isinstance(source, bytes): source = source.decode("utf-8") time_start = time.time() info = SBMLDocumentInfo.from_sbml(source=source) time_elapsed = round(time.time() - time_start, 3) debug = False if debug: console.rule("Creating JSON content") console.print(info.info) console.rule() logger.info(f"JSON created for '{uid}' in '{time_elapsed}'") return { "report": info.info, "debug": { "jsonReportTime": f"{time_elapsed} [s]", }, }
def annotate_sbml( source: Union[Path, str], annotations_path: Path, filepath: Path ) -> libsbml.SBMLDocument: """ Annotate a given SBML file with the provided annotations. :param source: SBML to annotation :param annotations_path: external file with annotations :param filepath: annotated SBML file :return: annotated SBMLDocument """ doc: libsbml.SBMLDocument = read_sbml(source=source) # annotate if not os.path.exists(str(annotations_path)): raise IOError(f"Annotation file does not exist: {annotations_path}") external_annotations = ModelAnnotator.read_annotations( annotations_path, file_format="*" ) doc = annotate_sbml_doc(doc, external_annotations) # type: ignore # write annotated sbml write_sbml(doc, filepath=filepath) console.print(f"Model annotated: file://{filepath}", style="success") return doc
def _create_biomodels_testfiles(output_dir: Path) -> None: """Download all curated biomodels and create omex files.""" # query the biomodel_ids = query_curated_biomodels() console.print(biomodel_ids) for biomodel_id in biomodel_ids: # download SBML model as omex try: download_biomodel_sbml(biomodel_id, output_dir, output_format="omex") except HTTPError as err: logger.error( f"Could not retrieve OMEX for biomodel: '{biomodel_id}'") logger.error(err)
def create_omex(tmp: bool = False) -> None: """Create omex with models.""" with tempfile.TemporaryDirectory() as tmp_dir: if tmp: output_dir = Path(tmp_dir) else: output_dir = EXAMPLES_DIR results_minimal_model = create_model( models=model_minimal, output_dir=output_dir, units_consistency=False, ) results_comp_model = create_model( models=comp_model, output_dir=output_dir, units_consistency=False, ) sbml_flat_path = ( results_comp_model.sbml_path.parent / f"{comp_model.sid}_flat.xml" ) flatten_sbml( sbml_path=results_comp_model.sbml_path, sbml_flat_path=sbml_flat_path ) sbml_paths = [ results_minimal_model.sbml_path, results_comp_model.sbml_path, sbml_flat_path, ] # Create COMBINE archive omex = Omex() for path in sbml_paths: omex.add_entry( entry_path=path, entry=ManifestEntry( format=EntryFormat.SBML_L3V1, location=f"./models/{path.name}" ), ) omex_path = output_dir / f"{comp_model.sid}.omex" omex.to_omex(omex_path) console.print(f"OMEX created: {omex_path}") console.print(omex.manifest.dict())
return ustr if __name__ == "__main__": import libsbml from sbmlutils.factory import * doc: libsbml.SBMLDocument = libsbml.SBMLDocument() model: libsbml.Model = doc.createModel() for (key, definition, _, _) in [ # ("mmole_per_min", "mmole/min", "str", "mmol/min"), # ("m3", "meter^3", "str", "m^3"), # ("m3", "meter^3/second", "str", "m^3/s"), # ("mM", "mmole/liter", "str", "mmol/l"), # ("ml_per_s_kg", "ml/s/kg", "str", "ml/s/kg"), # ("dimensionless", "dimensionless", "str", "dimensionless"), ("item", "item", "str", "item"), # ("mM", "mmole/min", "latex", "\\frac{mmol}/{min}"), ]: ud = UnitDefinition(key, definition=definition) # ud = UnitDefinition("item") udef: libsbml.UnitDefinition = ud.create_sbml(model=model) console.rule() console.print(udef) console.print(udef_to_string(udef, format="str")) console.print(udef_to_string(udef, format="latex"))
if __name__ == "__main__": from pathlib import Path from sbmlutils.console import console output_dir = Path(__file__).parent / "tests" if not output_dir.exists(): output_dir.mkdir(parents=True, exist_ok=True) from sbmlutils.resources import GLUCOSE_SBML, REPRESSILATOR_SBML for source in [ # COMP_ICG_BODY, # COMP_ICG_BODY_FLAT, # COMP_ICG_LIVER, # COMP_MODEL_DEFINITIONS_SBML, # FBC_RECON3D_SBML, # FBC_ECOLI_CORE_SBML, # DISTRIB_DISTRIBUTIONS_SBML, # DISTRIB_UNCERTAINTIES_SBML, # output_dir / "icg_body_flat_v2.xml", # GLUCOSE_SBML REPRESSILATOR_SBML, ]: info = SBMLDocumentInfo.from_sbml(source) console.rule() json_str = info.to_json() console.print(json_str) with open(output_dir / "tests.json", "w") as fout: fout.write(json_str)