Exemple #1
0
def clamp_species(r: roadrunner.RoadRunner,
                  sids,
                  boundary_condition=True) -> roadrunner.RoadRunner:
    """ Clamp/Free specie(s) via setting boundaryCondition=True/False.

    This requires changing the SBML and ODE system.

    :param r: roadrunner.RoadRunner
    :param sids: sid or iterable of sids
    :param boundary_condition: boolean flag to clamp (True) or free (False) species
    :return: modified roadrunner.RoadRunner
    """
    # get model for current SBML state
    sbml_str = r.getCurrentSBML()
    doc = libsbml.readSBMLFromString(sbml_str)  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model

    if isinstance(sids, str):
        sids = [sids]

    for sid in sids:
        # set boundary conditions
        sbase = model.getElementBySId(sid)  # type: libsbml.SBase
        if not sbase:
            logging.error("No element for SId in model: {}".format(sid))
            return None
        else:
            if sbase.getTypeCode() == libsbml.SBML_SPECIES:
                species = sbase  # type: libsbml.Species
                species.setBoundaryCondition(boundary_condition)
            else:
                logging.error(
                    "SId in clamp does not match species: {}".format(sbase))
                return None

    # create modified roadrunner instance
    sbmlmod_str = libsbml.writeSBMLToString(doc)
    rmod = load_model(sbmlmod_str)  # type: roadrunner.RoadRunner
    set_timecourse_selections(rmod, r.timeCourseSelections)

    return rmod
Exemple #2
0
def species_df(r: roadrunner.RoadRunner) -> pd.DataFrame:
    """
    Create FloatingSpecies DataFrame.
    :return: pandas DataFrame
    """
    r_model = r.model  # type: roadrunner.ExecutableModel
    sbml_str = r.getCurrentSBML()

    doc = libsbml.readSBMLFromString(sbml_str)  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model

    sids = r_model.getFloatingSpeciesIds() + r_model.getBoundarySpeciesIds()
    species = [model.getSpecies(sid)
               for sid in sids]  # type: List[libsbml.Species]

    data = {
        'sid':
        sids,
        'concentration':
        np.concatenate([
            r_model.getFloatingSpeciesConcentrations(),
            r_model.getBoundarySpeciesConcentrations()
        ],
                       axis=0),
        'amount':
        np.concatenate([
            r.model.getFloatingSpeciesAmounts(),
            r.model.getBoundarySpeciesAmounts()
        ],
                       axis=0),
        'unit': [s.getUnits() for s in species],
        'constant': [s.getConstant() for s in species],
        'boundaryCondition': [s.getBoundaryCondition() for s in species],
        'name': [s.getName() for s in species],
    }

    return pd.DataFrame(data,
                        columns=[
                            'sid', 'concentration', 'amount', 'unit',
                            'constant', 'boundaryCondition', 'species', 'name'
                        ])
Exemple #3
0
def parameter_df(r: roadrunner.RoadRunner) -> pd.DataFrame:
    """
    Create GlobalParameter DataFrame.
    :return: pandas DataFrame
    """
    r_model = r.model  # type: roadrunner.ExecutableModel
    doc = libsbml.readSBMLFromString(
        r.getCurrentSBML())  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model
    sids = r_model.getGlobalParameterIds()
    parameters = [model.getParameter(sid)
                  for sid in sids]  # type: List[libsbml.Parameter]
    data = {
        'sid': sids,
        'value': r_model.getGlobalParameterValues(),
        'unit': [p.units for p in parameters],
        'constant': [p.constant for p in parameters],
        'name': [p.name for p in parameters],
    }
    df = pd.DataFrame(data,
                      columns=['sid', 'value', 'unit', 'constant', 'name'])
    return df