Beispiel #1
0
def set_main_units(model, main_units):
    """ Set main units information on model.

    :param model: Model
    :param main_units: units info
    """
    factory.set_model_units(model, main_units)
Beispiel #2
0
def set_main_units(model, main_units):
    """ Set main units information on model.

    :param model: Model
    :param main_units: units info
    """
    factory.set_model_units(model, main_units)
Beispiel #3
0
    def create_sbml(self, sbml_level=SBML_LEVEL, sbml_version=SBML_VERSION):
        """ Create the SBML model

        :return:
        :rtype:
        """
        from sbmlutils.validation import check

        logging.info('*'*40)
        logging.info(self.model_id)
        logging.info('*' * 40)

        # create core model
        sbmlns = libsbml.SBMLNamespaces(sbml_level, sbml_version)

        # add all the packages
        # FIXME: only add packages which are required for the model

        sbmlns.addPackageNamespace("fbc", 2)
        sbmlns.addPackageNamespace("comp", 1)
        # sbmlns.addPackageNamespace("distrib", 1)

        self.doc = libsbml.SBMLDocument(sbmlns)
        self.doc.setPackageRequired("comp", True)
        self.doc.setPackageRequired("fbc", False)
        # self.doc.setPackageRequired("distrib", True)

        self.model = self.doc.createModel()
        fbc_plugin = self.model.getPlugin("fbc")
        fbc_plugin.setStrict(False)

        # name & id
        check(self.model.setId(self.model_id), 'set id')
        check(self.model.setName(self.model_id), 'set name')
        # notes
        if hasattr(self, 'notes') and self.notes is not None:
            factory.set_notes(self.model, self.notes)
        # history
        if hasattr(self, 'creators'):
            history.set_model_history(self.model, self.creators)

        # model units
        if hasattr(self, 'model_units'):
            factory.set_model_units(self.model, self.model_units)

        # lists ofs
        for attr in [
            'externalModelDefinitions',
            'submodels',
            'units',
            'functions',
            'parameters',
            'compartments',
            'species',
            'assignments',
            'rules',
            'rate_rules',
            'reactions',
            'events',
            'constraints',
            'ports',
            'replacedElements',
            'deletions',
            'objectives',
            'layouts'
        ]:
            # create the respective objects
            if hasattr(self, attr):
                objects = getattr(self, attr)
                if objects:
                    factory.create_objects(self.model, obj_iter=objects, key=attr)
                else:
                    logging.warning("Not defined: <{}> ".format(attr))
UNIT_CONCENTRATION = 'item_per_m3'
UNIT_FLUX = 'item_per_s'


# ### Model building
# Creation of FBA model using multiple packages (`comp`, `fbc`).

# In[3]:


# Create SBMLDocument with fba
doc = builder.template_doc_fba(model_id="toy")
model = doc.getModel()

factory.create_objects(model, units)
factory.set_model_units(model, model_units)

objects = [
    # compartments
    Compartment(sid='extern', value=1.0, unit=UNIT_VOLUME, constant=True, name='external compartment',
                   spatialDimensions=3),
    Compartment(sid='cell', value=1.0, unit=UNIT_VOLUME, constant=True, name='cell', spatialDimensions=3),
    Compartment(sid='membrane', value=1.0, unit=UNIT_AREA, constant=True, name='membrane', spatialDimensions=2),

    # exchange species
    Species(sid='A', name="A", initialAmount=0, substanceUnit=UNIT_AMOUNT, hasOnlySubstanceUnits=True,
               compartment="extern"),
    Species(sid='C', name="C", initialAmount=0, substanceUnit=UNIT_AMOUNT, hasOnlySubstanceUnits=True,
               compartment="extern"),

    # internal species
Beispiel #5
0
UNIT_CONCENTRATION = 'item_per_m3'
UNIT_FLUX = 'item_per_s'


# ### Model building
# Creation of FBA model using multiple packages (`comp`, `fbc`).

# In[3]:


# Create SBMLDocument with fba
doc = builder.template_doc_fba(model_id="toy")
model = doc.getModel()

factory.create_objects(model, units)
factory.set_model_units(model, model_units)

objects = [
    # compartments
    Compartment(sid='extern', value=1.0, unit=UNIT_VOLUME, constant=True, name='external compartment',
                   spatialDimensions=3),
    Compartment(sid='cell', value=1.0, unit=UNIT_VOLUME, constant=True, name='cell', spatialDimensions=3),
    Compartment(sid='membrane', value=1.0, unit=UNIT_AREA, constant=True, name='membrane', spatialDimensions=2),

    # exchange species
    Species(sid='A', name="A", initialAmount=0, substanceUnit=UNIT_AMOUNT, hasOnlySubstanceUnits=True,
               compartment="extern"),
    Species(sid='C', name="C", initialAmount=0, substanceUnit=UNIT_AMOUNT, hasOnlySubstanceUnits=True,
               compartment="extern"),

    # internal species
Beispiel #6
0
    def create_sbml(self, sbml_level=SBML_LEVEL, sbml_version=SBML_VERSION):
        """ Create the SBML model

        :return:
        :rtype:
        """
        from sbmlutils.validation import check

        logging.info('*' * 40)
        logging.info(self.model_id)
        logging.info('*' * 40)

        # create core model
        sbmlns = libsbml.SBMLNamespaces(sbml_level, sbml_version)

        # add all the packages
        # FIXME: only add packages which are required for the model

        sbmlns.addPackageNamespace("fbc", 2)
        sbmlns.addPackageNamespace("comp", 1)
        # sbmlns.addPackageNamespace("distrib", 1)

        self.doc = libsbml.SBMLDocument(sbmlns)
        self.doc.setPackageRequired("comp", True)
        self.doc.setPackageRequired("fbc", False)
        # self.doc.setPackageRequired("distrib", True)

        self.model = self.doc.createModel()
        fbc_plugin = self.model.getPlugin("fbc")
        fbc_plugin.setStrict(False)

        # name & id
        check(self.model.setId(self.model_id), 'set id')
        check(self.model.setName(self.model_id), 'set name')
        # notes
        if hasattr(self, 'notes') and self.notes is not None:
            factory.set_notes(self.model, self.notes)
        # history
        if hasattr(self, 'creators'):
            history.set_model_history(self.model, self.creators)

        # model units
        if hasattr(self, 'model_units'):
            factory.set_model_units(self.model, self.model_units)

        # lists ofs
        for attr in [
                'externalModelDefinitions', 'submodels', 'units', 'functions',
                'parameters', 'compartments', 'species', 'assignments',
                'rules', 'rate_rules', 'reactions', 'events', 'constraints',
                'ports', 'replacedElements', 'deletions', 'objectives',
                'layouts'
        ]:
            # create the respective objects
            if hasattr(self, attr):
                objects = getattr(self, attr)
                if objects:
                    factory.create_objects(self.model,
                                           obj_iter=objects,
                                           key=attr)
                else:
                    logging.info("Not defined: <{}> ".format(attr))
Beispiel #7
0
    def create_sbml(
        self, sbml_level: int = SBML_LEVEL, sbml_version: int = SBML_VERSION
    ) -> libsbml.SBMLDocument:
        """Create the SBML model

        :return:
        :rtype:
        """

        logger.info(f"create_sbml: '{self.model_id}'")

        # create core model
        sbmlns = libsbml.SBMLNamespaces(sbml_level, sbml_version)

        # add all the packages
        # FIXME: only add packages which are required for the model
        supported_packages = {"fbc", "comp", "distrib"}
        sbmlns.addPackageNamespace("comp", 1)
        for package in self.packages:
            if package not in supported_packages:
                raise ValueError(
                    f"Supported packages are: '{supported_packages}', "
                    f"but package '{package}' found."
                )
            if package == "fbc":
                sbmlns.addPackageNamespace("fbc", 2)
            if package == "distrib":
                sbmlns.addPackageNamespace("distrib", 1)

        self.doc = libsbml.SBMLDocument(sbmlns)
        self.model = self.doc.createModel()
        self.doc.setPackageRequired("comp", True)
        if "fbc" in self.packages:
            self.doc.setPackageRequired("fbc", False)
            fbc_plugin = self.model.getPlugin("fbc")
            fbc_plugin.setStrict(False)
        if "distrib" in self.packages:
            self.doc.setPackageRequired("distrib", True)

        # name & id
        if self.model_id:
            check(self.model.setId(self.model_id), "set id")
            check(self.model.setName(self.model_id), "set name")
        else:
            logger.warning("Model id 'mid' should be set on model")
        # notes
        if hasattr(self, "notes") and self.notes is not None:
            factory.set_notes(self.model, self.notes)
        # history
        if hasattr(self, "creators"):
            history.set_model_history(self.model, self.creators)

        # model units
        if hasattr(self, "model_units"):
            factory.set_model_units(self.model, self.model_units)

        # lists ofs
        for attr in [
            "externalModelDefinitions",
            "submodels",
            "units",
            "functions",
            "parameters",
            "compartments",
            "species",
            "assignments",
            "rules",
            "rate_rules",
            "reactions",
            "events",
            "constraints",
            "ports",
            "replacedElements",
            "deletions",
            "objectives",
            "layouts",
        ]:
            # create the respective objects
            if hasattr(self, attr):
                objects = getattr(self, attr)
                if objects:
                    factory.create_objects(self.model, obj_iter=objects, key=attr)
                else:
                    logger.debug(f"Not defined: <{attr}>")

        return self.doc