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())
def create(tmp: bool = False) -> None: """Create model.""" if tmp: tmp_dir = tempfile.mkdtemp() output_dir = Path(tmp_dir) else: output_dir = EXAMPLES_DIR sbml_path_flat = output_dir / "distrib_comp_flat.xml" result = create_model( models=_m, output_dir=output_dir, ) flatten_sbml(result.sbml_path, sbml_flat_path=sbml_path_flat) # create model report # sbmlreport.create_report(sbml_path_flat) if tmp: shutil.rmtree(tmp_dir)
def merge_models( model_paths: Dict[str, Path], output_dir: Path, merged_id: str = "merged", flatten: bool = True, validate: bool = True, validate_input: bool = True, units_consistency: bool = False, modeling_practice: bool = False, sbml_level: int = 3, sbml_version: int = 1, ) -> libsbml.SBMLDocument: """Merge SBML models. Merges SBML models given in `model_paths` in the `output_dir`. Models are provided as dictionary { 'model1_id': model1_path, 'model2_id': model2_path, ... } The model ids are used as ids for the ExternalModelDefinitions. Relative paths are set in the merged models. The created model is either in SBML L3V1 (default) or SBML L3V2. :param model_paths: absolute paths to models :param output_dir: output directory for merged model :param merged_id: model id of the merged model :param flatten: flattens the merged model :param validate: boolean flag to validate the merged model :param validate_input: boolean flag to validate the input models :param units_consistency: boolean flag to check units consistency :param modeling_practice: boolean flag to check modeling practise :param sbml_level: SBML Level of the merged model in [3] :param sbml_version: SBML Version of the merged model in [1, 2] :return: SBMLDocument of the merged models """ # necessary to convert models to SBML L3V1 if isinstance(output_dir, str): logger.warning( f"'output_dir' should be a Path but: '{type(output_dir)}'") output_dir = Path(output_dir) if not output_dir.exists(): raise IOError(f"'output_dir' does not exist: {output_dir}") validate_kwargs: Dict[str, bool] = { "units_consistency": units_consistency, "modeling_practice": modeling_practice, } for model_id, path in model_paths.items(): if not path.exists(): raise IOError(f"Path for SBML file does not exist: {path}") if isinstance(path, str): path = Path(path) # convert to L3V1 path_L3: Path = output_dir / f"{model_id}_L3.xml" doc = read_sbml(path) doc.setLevelAndVersion(sbml_level, sbml_version) write_sbml(doc, path_L3) model_paths[model_id] = path_L3 if validate_input: validate_sbml( source=path_L3, name=str(path), **validate_kwargs, ) # create comp model cur_dir = os.getcwd() os.chdir(str(output_dir)) merged_doc: libsbml.SBMLDocument = _create_merged_doc(model_paths, merged_id=merged_id) os.chdir(cur_dir) # write merged doc merged_path = output_dir / f"{merged_id}.xml" write_sbml(merged_doc, filepath=merged_path) if validate: validate_sbml(merged_path, name=str(merged_path), **validate_kwargs) if flatten: flat_path = output_dir / f"{merged_id}_flat.xml" flatten_sbml(sbml_path=merged_path, sbml_flat_path=flat_path) if validate: validate_sbml(flat_path, name=str(flat_path), **validate_kwargs) return merged_doc
# dictionary of ids & paths of models which should be combined # here we just bring together the first Biomodels model_ids = ["BIOMD000000000{}".format(k) for k in range(1, 5)] model_paths = dict( zip(model_ids, [os.path.join(merge_dir, "{}.xml".format(mid)) for mid in model_ids])) pprint(model_paths) # create merged model output_dir = os.path.join(merge_dir, 'output') doc = manipulation.merge_models(model_paths, out_dir=output_dir, validate=False) # validate Nall, Nerr, Nwarn = validation.validate_doc(doc, units_consistency=False) assert Nerr == 0 assert Nwarn == 0 assert Nall == 0 # write the merged model print(libsbml.writeSBMLToString(doc)) libsbml.writeSBMLToFile(doc, os.path.join(output_dir, "merged.xml")) # flatten the merged model doc_flat = comp.flatten_sbml(doc) Nall, Nerr, Nwarn = validation.validate_doc(doc_flat, units_consistency=False) libsbml.writeSBMLToFile(doc_flat, os.path.join(output_dir, "merged_flat.xml")) # In[ ]:
elementRef=f"S{k}", submodelRef=f"submodel{k}", portRef=f"S1{PORT_SUFFIX}", ), ]) # ------------------------------------------------------------------------------------- def create(tmp: bool = False) -> FactoryResult: """Create model.""" return create_model( models=_m, output_dir=EXAMPLES_DIR, units_consistency=False, tmp=tmp, ) if __name__ == "__main__": from sbmlutils.comp import flatten_sbml from sbmlutils.cytoscape import visualize_sbml fac_result = create() sbml_path_flat = Path(__file__).parent / "comp_model_flat.xml" # flatten SBML model flatten_sbml(fac_result.sbml_path, sbml_flat_path=sbml_path_flat) # visualize_sbml(sbml_path=fac_result.sbml_path) visualize_sbml(sbml_path=sbml_path_flat)