Exemplo n.º 1
0
def get_submodel_frameworks(doc: libsbml.SBMLDocument) -> Dict[str, Any]:
    """Read the SBO terms of the submodels.

    These are used to distinguish the different frameworks of the submodels.
    :param doc: SBMLDocument
    :return:
    """
    frameworks = {}
    # get list of submodels
    model = doc.getModel()
    mplugin = model.getPlugin("comp")

    # model.setSBOTerm(comp.SBO.CONTINOUS_FRAMEWORK)
    submodel: libsbml.Submodel
    for submodel in mplugin.getListOfSubmodels():
        sid = submodel.getId()
        sbo = None
        if submodel.isSetSBOTerm():
            # This is the sbo which is set on the submodel element
            # not the SBO which is set on the model in listOfModels or
            # listOfExternalModels
            sbo = submodel.getSBOTerm()
        frameworks[sid] = {
            "sid": sid,
            "modelRef": submodel.getModelRef(),
            "sbo": sbo
        }

    return frameworks
Exemplo n.º 2
0
def no_boundary_conditions(doc: libsbml.SBMLDocument) -> None:
    """Set all boundaryCondition to False in the model.

    :param doc: libsbml.SBMLDocument
    :return:
    """
    model = doc.getModel()
    for s in model.species:
        if s.boundary_condition:
            warnings.warn(f"boundaryCondition changed {s}", UserWarning)
            s.setBoundaryCondition(False)
Exemplo n.º 3
0
def _create_html(
    doc: libsbml.SBMLDocument,
    basename: str,
    html_template: str = "report.html",
    math_type: str = "cmathml",
    offline: bool = True,
) -> str:
    """Create HTML from SBML.

    :param doc: SBML document for creating HTML report
    :param basename: basename of SBML file path
    :param html_template: which template file to use for rendering
    :param math_type: specifies the math rendering mode for the report
    :param offline: to specify offline report generation for appropriate linking of
                    stylesheet and script files

    :return: rendered HTML report template for the SBML document
    """
    # template environment
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(TEMPLATE_DIR),
        extensions=["jinja2.ext.autoescape"],
        trim_blocks=True,
        lstrip_blocks=True,
    )
    # additional SBML filters
    for key in sbmlfilters.filters:
        env.filters[key] = getattr(sbmlfilters, key)

    model = doc.getModel()
    context: Dict[str, Any] = {
        "offline": offline,
        "basename": basename,
    }
    if model is not None:
        try:
            template = env.get_template(html_template)
        except TemplateNotFound as err:
            logger.error(
                f"TemplateNotFound: {TEMPLATE_DIR} / {html_template}; {err}")

        model_info = SBMLModelInfo(doc=doc, model=model, math_render=math_type)
        context.update(model_info.info)
    else:
        # no model exists
        logging.error(
            f"No model in SBML file when creating model report: {doc}")
        template = env.get_template("report_no_model.html")
        context.update({
            "doc": doc,
        })
    return template.render(context)
Exemplo n.º 4
0
    def __init__(self, doc: libsbml.SBMLDocument,
                 annotations: Iterable[ExternalAnnotation]):
        """Constructor.

        :param doc: SBMLDocument
        :param annotations: iterable of ModelAnnotation
        """
        self.doc = doc
        self.model = doc.getModel()
        self.annotations = annotations

        # prepare dictionary for lookup of ids
        self.id_dict = self._get_ids_from_model()
Exemplo n.º 5
0
def promote_local_variables(doc: libsbml.SBMLDocument,
                            suffix: str = "_promoted") -> libsbml.SBMLDocument:
    """Promotes local variables in SBMLDocument.

    Manipulates SBMLDocument in place!

    :param doc: SBMLDocument
    :param suffix: str suffix for promoted SBML
    :return: SBMLDocument with promoted parameters
    """
    model: libsbml.Model = doc.getModel()
    model.setId(f"{model.id}{suffix}")

    # promote local parameters
    props = libsbml.ConversionProperties()
    props.addOption("promoteLocalParameters", True,
                    "Promotes all Local Parameters to Global ones")

    if doc.convert(props) != libsbml.LIBSBML_OPERATION_SUCCESS:
        logger.error(f"Promotion of local parameters failed: {doc}")
    else:
        logger.info(f"Promotion of local paramters successful: {doc}")
    return doc
Exemplo n.º 6
0
def add_default_flux_bounds(doc: libsbml.SBMLDocument,
                            lower: float = -100.0,
                            upper: float = 100.0) -> None:
    """Add default flux bounds to SBMLDocument.

    :param doc: SBMLDocument
    :param lower: lower flux bound
    :param upper: upper flux bound
    :return:
    """
    model = doc.getModel()

    def create_bound(sid: str, value: float) -> libsbml.Parameter:
        """Create flux bound parameter with given value.

        :param sid: id of parameter
        :param value: flux bound
        :return:
        """
        p = model.createParameter()
        p.setId(sid)
        p.setValue(value)
        p.setName("{} flux bound".format(sid))
        p.setSBOTerm("SBO:0000626")  # default flux bound
        p.setConstant(True)
        return p

    # FIXME: overwrites lower/upper parameter (you should check if existing in model)
    create_bound(sid="lower", value=lower)
    create_bound(sid="upper", value=upper)

    for r in model.reactions:
        rfbc = r.getPlugin("fbc")
        if not rfbc.isSetLowerFluxBound():
            rfbc.setLowerFluxBound("lower")
        if not rfbc.isSetUpperFluxBound():
            rfbc.setUpperFluxBound("upper")