コード例 #1
0
    def compartments(self, model: libsbml.Model,
                     assignments: Dict[str, Dict[str, str]]) -> List[Dict]:
        """Information for Compartments.

        :return: list of info dictionaries for Compartments
        """

        compartments = []
        c: libsbml.Compartment
        for c in model.getListOfCompartments():
            d = self.sbase_dict(c)
            for key in ["spatialDimensions", "size", "constant"]:
                d[key] = _get_sbase_attribute(c, key)
            if d["size"] is not None and np.isnan(d["size"]):
                # NaN not JSON serializable
                d["size"] = "NaN"

            d["units_sid"] = c.getUnits() if c.isSetUnits() else None
            d["units"] = udef_to_string(d["units_sid"], model)
            d["derivedUnits"] = udef_to_string(c.getDerivedUnitDefinition())

            key = c.pk.split(":")[-1]
            if key in self.maps["assignments"]:
                d["assignment"] = self.maps["assignments"][key]
            if key in self.maps["ports"]:
                d["port"] = self.maps["ports"][key]

            compartments.append(d)

        return compartments
コード例 #2
0
    def rules(self, model: libsbml.Model) -> Dict:
        """Information for Rules.

        :return: list of info dictionaries for Rules
        """

        rules: Dict[str, List] = {
            "assignmentRules": [],
            "rateRules": [],
            "algebraicRules": [],
        }
        rule: libsbml.Rule
        for rule in model.getListOfRules():
            d = self.sbase_dict(rule)
            d["variable"] = self._rule_variable_to_string(rule)
            d["math"] = astnode_to_latex(
                rule.getMath()) if rule.isSetMath() else None
            d["derivedUnits"] = udef_to_string(rule.getDerivedUnitDefinition())

            type = d["sbmlType"]
            key = f"{type[0].lower()}{type[1:]}s"

            rules[key].append(d)

        return rules
コード例 #3
0
    def model(self, model: libsbml.Model) -> Dict[str, str]:
        """Info for SBML Model.

        :param model: Model
        :return: information dictionary for Model
        """
        d = self.sbase_dict(model)
        for key in [
                "substanceUnits",
                "timeUnits",
                "volumeUnits",
                "areaUnits",
                "lengthUnits",
                "extentUnits",
        ]:
            d[f"{key}_unit"] = _get_sbase_attribute(model, key)
            d[key] = udef_to_string(d[f"{key}_unit"], model)

        # FIXME: handle analoque to species
        if model.isSetConversionFactor():
            cf_sid = model.getConversionFactor()
            cf_p: libsbml.Parameter = model.getParameter(cf_sid)
            cf_value = cf_p.getValue()
            cf_units = cf_p.getUnits()

            d["conversionFactor"] = {
                "sid": cf_sid,
                "value": cf_value,
                "units": cf_units,
            }
        else:
            d["conversionFactor"] = {}

        return d
コード例 #4
0
    def parameters(self, model: libsbml.Model,
                   assignments: Dict[str, Dict[str, str]]) -> List[Dict]:
        """Information for SBML Parameters.

        :return: list of info dictionaries for Reactions
        """

        parameters = []
        p: libsbml.Parameter
        for p in model.getListOfParameters():
            d = self.sbase_dict(p)

            if p.isSetValue():
                value = p.getValue()
                if np.isnan(value):
                    value = None
            else:
                value = None

            d["value"] = value
            for key in ["value"]:
                if d[key] is not None and np.isnan(d[key]):
                    # NaN not JSON serializable
                    d[key] = "NaN"
            d["constant"] = p.getConstant() if p.isSetConstant() else None
            d["units_sid"] = p.getUnits() if p.isSetUnits() else None
            d["units"] = udef_to_string(d["units_sid"], model)
            d["derivedUnits"] = udef_to_string(p.getDerivedUnitDefinition())

            key = p.pk.split(":")[-1]
            if key in self.maps["assignments"]:
                d["assignment"] = self.maps["assignments"][key]
            if key in self.maps["ports"]:
                d["port"] = self.maps["ports"][key]

            parameters.append(d)

        return parameters
コード例 #5
0
    def initial_assignments(self, model: libsbml.Model) -> List:
        """Information for InitialAssignments.

        :return: list of info dictionaries for InitialAssignments
        """

        assignments = []
        assignment: libsbml.InitialAssignment
        for assignment in model.getListOfInitialAssignments():
            d = self.sbase_dict(assignment)
            d["symbol"] = assignment.getSymbol() if assignment.isSetSymbol(
            ) else None
            d["math"] = astnode_to_latex(assignment.getMath())
            d["derivedUnits"] = udef_to_string(
                assignment.getDerivedUnitDefinition())
            assignments.append(d)

        return assignments
コード例 #6
0
    def unit_definitions(self, model: libsbml.Model) -> List:
        """Information for UnitDefinitions.

        :return: list of info dictionaries for UnitDefinitions
        """
        unit_defs = []
        ud: libsbml.UnitDefinition
        for ud in model.getListOfUnitDefinitions():
            d = self.sbase_dict(ud)
            d["units"] = udef_to_string(ud)

            key = "units:" + ud.pk.split(":")[-1]
            if key in self.maps["assignments"]:
                d["assignment"] = self.maps["assignments"][key]
            if key in self.maps["ports"]:
                d["port"] = self.maps["ports"][key]

            unit_defs.append(d)

        return unit_defs
コード例 #7
0
    def reactions(self, model: libsbml.Model) -> List[Dict[str, Any]]:
        """Information dictionaries for ListOfReactions.

        :return: list of info dictionaries for Reactions

        -- take a look at local parameter once
        """

        reactions = []
        r: libsbml.Reaction
        for r in model.getListOfReactions():
            d = self.sbase_dict(r)
            d["reversible"] = r.getReversible() if r.isSetReversible(
            ) else None
            d["compartment"] = r.getCompartment() if r.isSetCompartment(
            ) else None
            d["listOfReactants"] = [
                self._species_reference(reac)
                for reac in r.getListOfReactants()
            ]
            d["listOfProducts"] = [
                self._species_reference(prod)
                for prod in r.getListOfProducts()
            ]
            d["listOfModifiers"] = [
                mod.getSpecies() for mod in r.getListOfModifiers()
            ]
            d["fast"] = r.getFast() if r.isSetFast() else None
            d["equation"] = self._equation_from_reaction(r)

            klaw: libsbml.KineticLaw = (r.getKineticLaw()
                                        if r.isSetKineticLaw() else None)
            if klaw:
                d_law: Dict[str, Any] = {}
                d_law["math"] = (astnode_to_latex(klaw.getMath())
                                 if klaw.isSetMath() else None)
                d_law["derivedUnits"] = udef_to_string(
                    klaw.getDerivedUnitDefinition())

                d_law["localParameters"] = []
                for i in range(len(klaw.getListOfLocalParameters())):
                    lp: libsbml.LocalParameter = klaw.getLocalParameter(i)
                    lpar_info = {
                        "id":
                        lp.getId() if lp.isSetId() else None,
                        "value":
                        lp.getValue() if lp.isSetValue() else None,
                        "units_sid":
                        lp.getUnits() if lp.isSetUnits() else None,
                        "derivedUnits":
                        udef_to_string(lp.getDerivedUnitDefinition()),
                    }
                    lpar_info["units"] = udef_to_string(
                        lpar_info["units_sid"], model)
                    d_law["localParameters"].append(lpar_info)
                d["kineticLaw"] = d_law
            else:
                d["kineticLaw"] = None

            # fbc
            rfbc = r.getPlugin("fbc")
            d["fbc"] = ({
                "bounds": self._bounds_dict_from_reaction(r, model),
                "gpa": self._gene_product_association_from_reaction(r),
            } if rfbc else None)

            key = r.pk.split(":")[-1]
            if key in self.maps["assignments"]:
                d["assignment"] = self.maps["assignments"][key]
            if key in self.maps["ports"]:
                d["port"] = self.maps["ports"][key]

            reactions.append(d)

        return reactions
コード例 #8
0
    def species(self, model: libsbml.Model,
                assignments: Dict[str, Dict[str, str]]) -> List[Dict]:
        """Information for Species.

        :return: list of info dictionaries for Species
        """

        species = []
        s: libsbml.Species
        for s in model.getListOfSpecies():
            d = self.sbase_dict(s)

            for key in [
                    "compartment",
                    "initialAmount",
                    "initialConcentration",
                    "substanceUnits",
                    "hasOnlySubstanceUnits",
                    "boundaryCondition",
                    "constant",
            ]:
                d[key] = _get_sbase_attribute(s, key)

            for key in ["initialAmount", "initialConcentration"]:
                if d[key] is not None and np.isnan(d[key]):
                    # NaN not JSON serializable
                    d[key] = "NaN"

            d["units_sid"] = s.getUnits() if s.isSetUnits() else None
            d["units"] = udef_to_string(d["units_sid"], model)
            d["derivedUnits"] = udef_to_string(s.getDerivedUnitDefinition())

            # lookup in maps (PKs are in the form <SBMLType>:<id/metaID/name/etc).
            key = s.pk.split(":")[-1]
            if key in self.maps["assignments"]:
                d["assignment"] = self.maps["assignments"][key]
            if key in self.maps["ports"]:
                d["port"] = self.maps["ports"][key]

            if s.isSetConversionFactor():
                cf_sid = s.getConversionFactor()
                cf_p: libsbml.Parameter = model.getParameter(cf_sid)
                cf_value = cf_p.getValue()
                cf_units = cf_p.getUnits()

                d["conversionFactor"] = {
                    "sid": cf_sid,
                    "value": cf_value,
                    "units": cf_units,
                }
            else:
                d["conversionFactor"] = {}

            # fbc
            sfbc = s.getPlugin("fbc")
            d["fbc"] = ({
                "formula":
                sfbc.getChemicalFormula()
                if sfbc.isSetChemicalFormula() else None,
                "charge":
                sfbc.getCharge() if
                (sfbc.isSetCharge() and sfbc.getCharge() != 0) else None,
            } if sfbc else None)

            species.append(d)

        return species