Exemplo n.º 1
0
 def test_single_gene_deletion_moma_benchmark(self, model, benchmark):
     try:
         sutil.get_solver_name(qp=True)
     except sutil.SolverNotFound:
         pytest.skip("no qp support")
     genes = ['b0008', 'b0114', 'b2276', 'b1779']
     benchmark(single_gene_deletion, model, gene_list=genes, method="moma")
Exemplo n.º 2
0
    def test_single_gene_deletion_moma(self, model):
        try:
            sutil.get_solver_name(qp=True)
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {
            "b0008": 0.87,
            "b0114": 0.71,
            "b0116": 0.56,
            "b2276": 0.11,
            "b1779": 0.00
        }

        df = single_gene_deletion(model,
                                  gene_list=growth_dict.keys(),
                                  method="moma")
        assert numpy.all([df.status == 'optimal'])
        assert all(
            abs(df.flux[gene] - expected) < 0.01
            for gene, expected in iteritems(growth_dict))
        with model:
            add_moma(model)
            with pytest.raises(ValueError):
                add_moma(model)
Exemplo n.º 3
0
    def test_moma_sanity(self, model):
        """Test optimization criterion and optimality."""
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        sol = model.optimize()
        with model:
            model.reactions.PFK.knock_out()
            knock_sol = model.optimize()
            ssq = (knock_sol.fluxes - sol.fluxes).pow(2).sum()

        with model:
            add_moma(model)
            model.reactions.PFK.knock_out()
            moma_sol = model.optimize()
            moma_ssq = (moma_sol.fluxes - sol.fluxes).pow(2).sum()

        # Use normal FBA as reference solution.
        with model:
            add_moma(model, solution=sol)
            model.reactions.PFK.knock_out()
            moma_ref_sol = model.optimize()
            moma_ref_ssq = (moma_ref_sol.fluxes - sol.fluxes).pow(2).sum()

        assert numpy.isclose(moma_sol.objective_value, moma_ssq)
        assert moma_ssq < ssq
        assert numpy.isclose(moma_sol.objective_value,
                             moma_ref_sol.objective_value)
        assert numpy.isclose(moma_ssq, moma_ref_ssq)
Exemplo n.º 4
0
    def test_single_gene_deletion_linear_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {
            "b0008": 0.87,
            "b0114": 0.76,
            "b0116": 0.65,
            "b2276": 0.08,
            "b1779": 0.00
        }

        result = single_gene_deletion(model,
                                      gene_list=growth_dict.keys(),
                                      method="linear moma")['growth']
        assert all(
            abs(result[frozenset([gene])] - expected) < 0.01
            for gene, expected in iteritems(growth_dict))
        with model:
            add_moma(model, linear=True)
            with pytest.raises(ValueError):
                add_moma(model)
Exemplo n.º 5
0
    def test_moma_sanity(self, model):
        """Test optimization criterion and optimality."""
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        sol = model.optimize()
        with model:
            model.reactions.PFK.knock_out()
            knock_sol = model.optimize()
            ssq = (knock_sol.fluxes - sol.fluxes).pow(2).sum()

        with model:
            add_moma(model)
            model.reactions.PFK.knock_out()
            moma_sol = model.optimize()
            moma_ssq = (moma_sol.fluxes - sol.fluxes).pow(2).sum()

        # Use normal FBA as reference solution.
        with model:
            add_moma(model, solution=sol)
            model.reactions.PFK.knock_out()
            moma_ref_sol = model.optimize()
            moma_ref_ssq = (moma_ref_sol.fluxes - sol.fluxes).pow(2).sum()

        assert numpy.isclose(moma_sol.objective_value, moma_ssq)
        assert moma_ssq < ssq
        assert numpy.isclose(moma_sol.objective_value,
                             moma_ref_sol.objective_value)
        assert numpy.isclose(moma_ssq, moma_ref_ssq)
Exemplo n.º 6
0
    def __init__(self, id_or_model=None, name=None):
        if isinstance(id_or_model, Model):
            Object.__init__(self, name=name)
            self.__setstate__(id_or_model.__dict__)
            if not hasattr(self, "name"):
                self.name = None
            self._solver = id_or_model.solver
        else:
            Object.__init__(self, id_or_model, name=name)
            self._trimmed = False
            self._trimmed_genes = []
            self._trimmed_reactions = {}
            self.genes = DictList()
            self.reactions = DictList()  # A list of cobra.Reactions
            self.metabolites = DictList()  # A list of cobra.Metabolites
            # genes based on their ids {Gene.id: Gene}
            self._compartments = dict()
            self._contexts = []

            # from cameo ...

            # if not hasattr(self, '_solver'):  # backwards compatibility
            # with older cobrapy pickles?
            interface = solvers[get_solver_name()]
            self._solver = interface.Model()
            self._solver.objective = interface.Objective(Zero)
            self._populate_solver(self.reactions, self.metabolites)
Exemplo n.º 7
0
    def __init__(self, id_or_model=None, name=None):
        if isinstance(id_or_model, Model):
            Object.__init__(self, name=name)
            self.__setstate__(id_or_model.__dict__)
            if not hasattr(self, "name"):
                self.name = None
            self._solver = id_or_model.solver
        else:
            Object.__init__(self, id_or_model, name=name)
            self._trimmed = False
            self._trimmed_genes = []
            self._trimmed_reactions = {}
            self.genes = DictList()
            self.reactions = DictList()  # A list of cobra.Reactions
            self.metabolites = DictList()  # A list of cobra.Metabolites
            # genes based on their ids {Gene.id: Gene}
            self.compartments = dict()
            self._contexts = []

            # from cameo ...

            # if not hasattr(self, '_solver'):  # backwards compatibility
            # with older cobrapy pickles?
            interface = solvers[get_solver_name()]
            self._solver = interface.Model()
            self._solver.objective = interface.Objective(S.Zero)
            self._populate_solver(self.reactions, self.metabolites)
Exemplo n.º 8
0
    def test_single_deletion_linear_moma_benchmark(self, model, benchmark):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        genes = ['b0008', 'b0114', 'b2276', 'b1779']
        benchmark(single_gene_deletion, model, gene_list=genes,
                  method="linear moma")
Exemplo n.º 9
0
    def test_single_gene_deletion_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {"b0008": 0.87, "b0114": 0.71, "b0116": 0.56,
                       "b2276": 0.11, "b1779": 0.00}

        result = single_gene_deletion(
            model, gene_list=growth_dict.keys(), method="moma")["growth"]
        for gene, expected_value in iteritems(growth_dict):
            assert abs(result[frozenset([gene])] - expected_value) < 0.01
Exemplo n.º 10
0
    def test_single_gene_deletion_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {"b0008": 0.87, "b0114": 0.71, "b0116": 0.56,
                       "b2276": 0.11, "b1779": 0.00}

        result = single_gene_deletion(
            model=model,
            gene_list=list(growth_dict),
            method="moma",
            processes=1
        )["growth"]
        for gene, value in iteritems(growth_dict):
            assert numpy.isclose(result[frozenset([gene])], value,
                                 atol=1E-02)
Exemplo n.º 11
0
    def test_single_gene_deletion_linear_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {"b0008": 0.87, "b0114": 0.76, "b0116": 0.65,
                       "b2276": 0.08, "b1779": 0.00}

        result = single_gene_deletion(
            model, gene_list=growth_dict.keys(),
            method="linear moma")['growth']
        assert all(abs(result[frozenset([gene])] - expected) < 0.01
                   for gene, expected in iteritems(growth_dict))
        with model:
            add_moma(model, linear=True)
            with pytest.raises(ValueError):
                add_moma(model)
Exemplo n.º 12
0
    def test_single_gene_deletion_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {
            "b0008": 0.87,
            "b0114": 0.71,
            "b0116": 0.56,
            "b2276": 0.11,
            "b1779": 0.00
        }

        result = single_gene_deletion(model,
                                      gene_list=growth_dict.keys(),
                                      method="moma")["growth"]
        for gene, expected_value in iteritems(growth_dict):
            assert abs(result[frozenset([gene])] - expected_value) < 0.01
Exemplo n.º 13
0
    def test_single_gene_deletion_moma(self, model):
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        # expected knockouts for textbook model
        growth_dict = {
            "b0008": 0.87,
            "b0114": 0.71,
            "b0116": 0.56,
            "b2276": 0.11,
            "b1779": 0.00
        }

        result = single_gene_deletion(model=model,
                                      gene_list=list(growth_dict),
                                      method="moma",
                                      processes=1)["growth"]
        for gene, value in iteritems(growth_dict):
            assert numpy.isclose(result[frozenset([gene])], value, atol=1E-02)
Exemplo n.º 14
0
    def test_linear_moma_sanity(self, model):
        """Test optimization criterion and optimality."""
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        sol = model.optimize()
        with model:
            model.reactions.PFK.knock_out()
            knock_sol = model.optimize()
            sabs = (knock_sol.fluxes - sol.fluxes).abs().sum()

        with model:
            add_moma(model, linear=True)
            model.reactions.PFK.knock_out()
            moma_sol = model.optimize()
            moma_sabs = (moma_sol.fluxes - sol.fluxes).abs().sum()

        assert numpy.allclose(moma_sol.objective_value, moma_sabs)
        assert moma_sabs < sabs
Exemplo n.º 15
0
    def test_linear_moma_sanity(self, model):
        """Test optimization criterion and optimality."""
        try:
            solver = sutil.get_solver_name(qp=True)
            model.solver = solver
        except sutil.SolverNotFound:
            pytest.skip("no qp support")

        sol = model.optimize()
        with model:
            model.reactions.PFK.knock_out()
            knock_sol = model.optimize()
            sabs = (knock_sol.fluxes - sol.fluxes).abs().sum()

        with model:
            add_moma(model, linear=True)
            model.reactions.PFK.knock_out()
            moma_sol = model.optimize()
            moma_sabs = (moma_sol.fluxes - sol.fluxes).abs().sum()

        assert numpy.allclose(moma_sol.objective_value, moma_sabs)
        assert moma_sabs < sabs
Exemplo n.º 16
0
 def test_solver_name(self):
     assert su.get_solver_name() == "glpk"
Exemplo n.º 17
0
def test_solver_name() -> None:
    """Test that the default LP solver name is GLPK."""
    assert su.get_solver_name() == "glpk"
Exemplo n.º 18
0
def test_solver_name():
    assert su.get_solver_name() == "glpk"