Beispiel #1
0
def get_petab_problem(sbml_str: str = None,
                      condition_df: Optional[pd.DataFrame] = None,
                      measurement_df: Optional[pd.DataFrame] = None,
                      parameter_df: Optional[pd.DataFrame] = None,
                      observable_df: Optional[pd.DataFrame] = None
                      ) -> 'petab.Problem':
    """
    load petab problem.

    Arguments:
        sbml_str: PEtab SBML file
        condition_df: PEtab condition table
        measurement_df: PEtab measurement table
        parameter_df: PEtab parameter table
        observable_df: PEtab observables tables
    """

    sbml_model = sbml_document = sbml_reader = None

    if condition_df:
        condition_df = petab.conditions.get_condition_df(condition_df)

    if measurement_df:
        # TODO: If there are multiple tables, we will merge them
        measurement_df = petab.measurements.get_measurement_df(measurement_df)

    if parameter_df:
        parameter_df = petab.parameters.get_parameter_df(parameter_df)

    if sbml_str:
        sbml_reader = libsbml.SBMLReader()
        sbml_document = sbml_reader.readSBMLFromString(sbml_str)
        sbml_model = sbml_document.getModel()

    if observable_df:
        # TODO: If there are multiple tables, we will merge them
        observable_df = petab.observables.get_observable_df(observable_df)

    return petab.Problem(condition_df=condition_df,
                         measurement_df=measurement_df,
                         parameter_df=parameter_df,
                         observable_df=observable_df,
                         sbml_model=sbml_model,
                         sbml_document=sbml_document,
                         sbml_reader=sbml_reader)
Beispiel #2
0
def test_flatten_timepoint_specific_output_overrides_special_cases():
    """Test flatten_timepoint_specific_output_overrides
    for special cases:
    * no preequilibration
    * no observable parameters
    """
    observable_df = pd.DataFrame(data={
        OBSERVABLE_ID: ['obs1'],
        OBSERVABLE_FORMULA: ['species1'],
        NOISE_FORMULA: ['noiseParameter1_obs1']
    })
    observable_df.set_index(OBSERVABLE_ID, inplace=True)

    observable_df_expected = pd.DataFrame(data={
        OBSERVABLE_ID: ['obs1__noiseParOverride1__condition1',
                        'obs1__noiseParOverride2__condition1'],
        OBSERVABLE_FORMULA: [
            'species1',
            'species1'],
        NOISE_FORMULA: ['noiseParameter1_obs1__noiseParOverride1__condition1',
                        'noiseParameter1_obs1__noiseParOverride2__condition1']
    })
    observable_df_expected.set_index(OBSERVABLE_ID, inplace=True)

    # Measurement table with timepoint-specific overrides
    measurement_df = pd.DataFrame(data={
        OBSERVABLE_ID:
            ['obs1', 'obs1', 'obs1', 'obs1'],
        SIMULATION_CONDITION_ID:
            ['condition1', 'condition1', 'condition1', 'condition1'],
        TIME:
            [1.0, 1.0, 2.0, 2.0],
        MEASUREMENT:
            [.1] * 4,
        NOISE_PARAMETERS:
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2'],
    })

    measurement_df_expected = pd.DataFrame(data={
        OBSERVABLE_ID:
            ['obs1__noiseParOverride1__condition1',
             'obs1__noiseParOverride1__condition1',
             'obs1__noiseParOverride2__condition1',
             'obs1__noiseParOverride2__condition1'],
        SIMULATION_CONDITION_ID:
            ['condition1', 'condition1', 'condition1', 'condition1'],
        TIME:
            [1.0, 1.0, 2.0, 2.0],
        MEASUREMENT:
            [.1] * 4,
        NOISE_PARAMETERS:
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2'],
    })

    problem = petab.Problem(measurement_df=measurement_df,
                            observable_df=observable_df)

    assert petab.lint_problem(problem) is False

    # Ensure having timepoint-specific overrides
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        measurement_df) is True

    petab.flatten_timepoint_specific_output_overrides(problem)

    # Timepoint-specific overrides should be gone now
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        problem.measurement_df) is False

    assert problem.observable_df.equals(observable_df_expected) is True
    assert problem.measurement_df.equals(measurement_df_expected) is True

    assert petab.lint_problem(problem) is False
Beispiel #3
0
def get_problem(sbml_file, condition_file, measurement_file, parameters_file,
                observables_file):
    """
    will be removed
    :return:
    """

    if sbml_file:
        sbml_reader = libsbml.SBMLReader()
        sbml_str = str(sbml_file.stream.read(), "utf-8")
        sbml_document = sbml_reader.readSBMLFromString(sbml_str)
        sbml_model = sbml_document.getModel()
    else:
        sbml_reader = None
        sbml_document = None
        sbml_model = None

    if condition_file:
        condition_df = pd.read_csv(condition_file, sep='\t')
        try:
            condition_df.set_index([CONDITION_ID], inplace=True)
        except KeyError:
            raise KeyError(
                f'Condition table missing mandatory field {CONDITION_ID}.')
    else:
        condition_df = None

    if measurement_file:
        measurement_df = petab.measurements.get_measurement_df(
            pd.read_csv(measurement_file, sep='\t'))
    else:
        measurement_df = None

    if parameters_file:
        parameters_df = pd.read_csv(parameters_file, sep='\t')
        try:
            parameters_df.set_index([PARAMETER_ID], inplace=True)
        except KeyError:
            raise KeyError(
                f"Parameter table missing mandatory field {PARAMETER_ID}.")
    else:
        parameters_df = None

    if observables_file:
        observables_df = pd.read_csv(observables_file, sep='\t')
        try:
            observables_df.set_index([OBSERVABLE_ID], inplace=True)
        except KeyError:
            raise KeyError(
                f"Observable table missing mandatory field {OBSERVABLE_ID}.")
    else:
        observables_df = None

    petab_problem = petab.Problem(sbml_reader=sbml_reader,
                                  sbml_document=sbml_document,
                                  sbml_model=sbml_model,
                                  condition_df=condition_df,
                                  measurement_df=measurement_df,
                                  parameter_df=parameters_df,
                                  observable_df=observables_df)
    return petab_problem
Beispiel #4
0
def test_get_condition_specific_models():
    """Test for petab.sbml.get_condition_specific_models"""
    # Create test model and data files
    sbml_document = libsbml.SBMLDocument(3, 1)
    sbml_model = sbml_document.createModel()

    c = sbml_model.createCompartment()
    c.setId("compartment_1")
    c.setSize(1)

    for i in range(1, 4):
        p = sbml_model.createParameter()
        p.setId(f"parameter_{i}")
        p.setValue(i)

    for i in range(1, 4):
        s = sbml_model.createSpecies()
        s.setId(f"species_{i}")
        s.setInitialConcentration(10 * i)

    ia = sbml_model.createInitialAssignment()
    ia.setSymbol("species_2")
    ia.setMath(libsbml.parseL3Formula("25"))

    condition_df = pd.DataFrame({
        petab.CONDITION_ID: ["condition_1"],
        "parameter_3": ['parameter_2'],
        "species_1": [15],
        "species_2": [25],
        "species_3": ['parameter_1'],
        "compartment_1": [2],
    })
    condition_df.set_index([petab.CONDITION_ID], inplace=True)

    observable_df = pd.DataFrame({
        petab.OBSERVABLE_ID: ["observable_1"],
        petab.OBSERVABLE_FORMULA: ["2 * species_1"],
    })
    observable_df.set_index([petab.OBSERVABLE_ID], inplace=True)

    measurement_df = pd.DataFrame({
        petab.OBSERVABLE_ID: ["observable_1"],
        petab.SIMULATION_CONDITION_ID: ["condition_1"],
        petab.TIME: [0.0],
    })

    parameter_df = pd.DataFrame({
        petab.PARAMETER_ID: ["parameter_1", "parameter_2"],
        petab.PARAMETER_SCALE: [petab.LOG10] * 2,
        petab.NOMINAL_VALUE: [1.25, 2.25],
        petab.ESTIMATE: [0, 1],
    })
    parameter_df.set_index([petab.PARAMETER_ID], inplace=True)

    petab_problem = petab.Problem(sbml_model=sbml_model,
                                  condition_df=condition_df,
                                  observable_df=observable_df,
                                  measurement_df=measurement_df,
                                  parameter_df=parameter_df)

    # Actual test
    condition_doc, condition_model = petab.get_model_for_condition(
        petab_problem, "condition_1")

    assert condition_model.getSpecies(
        "species_1").getInitialConcentration() == 15
    assert condition_model.getSpecies(
        "species_2").getInitialConcentration() == 25
    assert condition_model.getSpecies(
        "species_3").getInitialConcentration() == 1.25
    assert len(condition_model.getListOfInitialAssignments()) == 0, \
        "InitialAssignment not removed"
    assert condition_model.getCompartment("compartment_1").getSize() == 2.0
    assert condition_model.getParameter("parameter_1").getValue() == 1.25
    assert condition_model.getParameter("parameter_2").getValue() == 2.25
    assert condition_model.getParameter("parameter_3").getValue() == 2.25
Beispiel #5
0
def test_flatten_timepoint_specific_output_overrides():
    """Test flatten_timepoint_specific_output_overrides"""
    observable_df = pd.DataFrame(data={
        OBSERVABLE_ID: ['obs1'],
        OBSERVABLE_FORMULA: [
            'observableParameter1_obs1 + observableParameter2_obs1'],
        NOISE_FORMULA: ['noiseParameter1_obs1']
    })
    observable_df.set_index(OBSERVABLE_ID, inplace=True)

    observable_df_expected = pd.DataFrame(data={
        OBSERVABLE_ID: ['obs1_1', 'obs1_2', 'obs1_3'],
        OBSERVABLE_FORMULA: [
            'observableParameter1_obs1_1 + observableParameter2_obs1_1',
            'observableParameter1_obs1_2 + observableParameter2_obs1_2',
            'observableParameter1_obs1_3 + observableParameter2_obs1_3'],
        NOISE_FORMULA: ['noiseParameter1_obs1_1',
                        'noiseParameter1_obs1_2',
                        'noiseParameter1_obs1_3']
    })
    observable_df_expected.set_index(OBSERVABLE_ID, inplace=True)

    # Measurement table with timepoint-specific overrides
    measurement_df = pd.DataFrame(data={
        OBSERVABLE_ID:
            ['obs1', 'obs1', 'obs1', 'obs1'],
        SIMULATION_CONDITION_ID:
            ['condition1', 'condition1', 'condition1', 'condition1'],
        PREEQUILIBRATION_CONDITION_ID:
            ['', '', '', ''],
        TIME:
            [1.0, 1.0, 2.0, 2.0],
        MEASUREMENT:
            [np.nan] * 4,
        OBSERVABLE_PARAMETERS:
            ['obsParOverride1;1.0', 'obsParOverride2;1.0',
             'obsParOverride2;1.0', 'obsParOverride2;1.0'],
        NOISE_PARAMETERS:
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2']
    })

    measurement_df_expected = pd.DataFrame(data={
        OBSERVABLE_ID:
            ['obs1_1', 'obs1_2', 'obs1_3', 'obs1_3'],
        SIMULATION_CONDITION_ID:
            ['condition1', 'condition1', 'condition1', 'condition1'],
        PREEQUILIBRATION_CONDITION_ID:
            ['', '', '', ''],
        TIME:
            [1.0, 1.0, 2.0, 2.0],
        MEASUREMENT:
            [np.nan] * 4,
        OBSERVABLE_PARAMETERS:
            ['obsParOverride1;1.0', 'obsParOverride2;1.0',
             'obsParOverride2;1.0', 'obsParOverride2;1.0'],
        NOISE_PARAMETERS:
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2']
    })

    problem = petab.Problem(measurement_df=measurement_df,
                            observable_df=observable_df)

    assert petab.lint_problem(problem) is False

    # Ensure having timepoint-specific overrides
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        measurement_df) is True

    petab.flatten_timepoint_specific_output_overrides(problem)

    # Timepoint-specific overrides should be gone now
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        problem.measurement_df) is False

    assert problem.observable_df.equals(observable_df_expected) is True
    assert problem.measurement_df.equals(measurement_df_expected) is True

    assert petab.lint_problem(problem) is False
Beispiel #6
0
def test_flatten_timepoint_specific_output_overrides(minimal_sbml_model):
    document, model = minimal_sbml_model
    petab.sbml.add_global_parameter(
        sbml_model=model, parameter_id='observableParameter1_obs1')
    petab.sbml.add_model_output_with_sigma(
        sbml_model=model, observable_id='obs1',
        observable_formula='observableParameter1_obs1')

    # Measurement table with timepoint-specific overrides
    measurement_df = pd.DataFrame(data={
        'observableId':
            ['obs1', 'obs1', 'obs1', 'obs1'],
        'simulationConditionId':
            ['condition1', 'condition1', 'condition1', 'condition1'],
        'preequilibrationConditionId':
            ['', '', '', ''],
        'time':
            [1.0, 1.0, 2.0, 2.0],
        'measurement':
            [np.nan] * 4,
        'observableParameters':
            ['obsParOverride1', 'obsParOverride2',
             'obsParOverride2', 'obsParOverride2'],
        'noiseParameters':
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2']
    })

    measurement_df_expected = pd.DataFrame(data={
        'observableId':
            ['obs1_1', 'obs1_2', 'obs1_3', 'obs1_3'],
        'simulationConditionId':
            ['condition1', 'condition1', 'condition1', 'condition1'],
        'preequilibrationConditionId':
            ['', '', '', ''],
        'time':
            [1.0, 1.0, 2.0, 2.0],
        'measurement':
            [np.nan] * 4,
        'observableParameters':
            ['obsParOverride1', 'obsParOverride2',
             'obsParOverride2', 'obsParOverride2'],
        'noiseParameters':
            ['noiseParOverride1', 'noiseParOverride1',
             'noiseParOverride2', 'noiseParOverride2']
    })

    problem = petab.Problem(sbml_model=model,
                            measurement_df=measurement_df)

    assert petab.lint_problem(problem) is False

    # Ensure having timepoint-specific overrides
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        measurement_df) is True

    petab.flatten_timepoint_specific_output_overrides(problem)

    # Timepoint-specific overrides should be gone now
    assert petab.lint.measurement_table_has_timepoint_specific_mappings(
        problem.measurement_df) is False

    assert problem.measurement_df.equals(measurement_df_expected) is True

    assert petab.lint_problem(problem) is False