def log_sbml_errors(sbml_document: libsbml.SBMLDocument, minimum_severity=libsbml.LIBSBML_SEV_WARNING) -> None: """Log libsbml errors Arguments: sbml_document: SBML document to check minimum_severity: Minimum severity level to report (see libsbml) """ for error_idx in range(sbml_document.getNumErrors()): error = sbml_document.getError(error_idx) if error.getSeverity() >= minimum_severity: category = error.getCategoryAsString() severity = error.getSeverityAsString() message = error.getMessage() if severity == libsbml.LIBSBML_SEV_INFO: logger.info(f'libSBML {severity} ({category}): {message}') elif severity == libsbml.LIBSBML_SEV_WARNING: logger.warning(f'libSBML {severity} ({category}): {message}') else: logger.error(f'libSBML {severity} ({category}): {message}')
def flatten_sbml_doc(doc: libsbml.SBMLDocument, output_path: Path = None, leave_ports: bool = True) -> libsbml.SBMLDocument: """Flatten SBMLDocument. Validation should be performed before the flattening and is not part of the flattening routine. If an output path is provided the file is written to the output path. :param doc: SBMLDocument to flatten. :param output_path: Path to write flattended SBMLDocument to :param leave_ports: flag to leave ports :return: SBMLDocument """ error_count = doc.getNumErrors() if error_count > 0: if doc.getError(0).getErrorId() == libsbml.XMLFileUnreadable: # Handle case of unreadable file here. logger.error("SBML error in doc: libsbml.XMLFileUnreadable") elif doc.getError(0).getErrorId() == libsbml.XMLFileOperationError: # Handle case of other file error here. logger.error("SBML error in doc: libsbml.XMLFileOperationError") else: # Handle other error cases here. logger.error("SBML errors in doc, see SBMLDocument error log.") # converter options libsbml.CompFlatteningConverter props = libsbml.ConversionProperties() props.addOption("flatten comp", True) # Invokes CompFlatteningConverter props.addOption("leave_ports", leave_ports) # Indicates whether to leave ports props.addOption("abortIfUnflattenable", "none") # flatten current = time.perf_counter() result = doc.convert(props) flattened_status = result == libsbml.LIBSBML_OPERATION_SUCCESS lines = [ "", "-" * 120, str(doc), "{:<25}: {}".format("flattened", str(flattened_status).upper()), "{:<25}: {:.3f}".format("flatten time (ms)", time.perf_counter() - current), "-" * 120, ] info = bcolors.BOLD + "\n".join(lines) + bcolors.ENDC if flattened_status: logger.info(bcolors.OKGREEN + info + bcolors.ENDC) else: logger.error(bcolors.FAIL + info + bcolors.ENDC) raise ValueError( "SBML could not be flattend due to errors in the SBMLDocument.") if output_path is not None: write_sbml(doc, filepath=output_path) logger.info(f"Flattened model created: '{output_path}'") return doc
def log_sbml_errors_for_doc(doc: libsbml.SBMLDocument) -> None: """Log errors of current SBMLDocument.""" for k in range(doc.getNumErrors()): log_sbml_error(error=doc.getError(k))