Ejemplo n.º 1
0
    def setUp(self):
        self._database = DictDatabase()
        self._database.set_reaction('rxn_1', parse_reaction('A[e] => B[e]'))
        self._database.set_reaction('rxn_2', parse_reaction('B[e] => C[e]'))
        self._database.set_reaction('rxn_3', parse_reaction('B[e] => D[e]'))
        self._database.set_reaction('rxn_4', parse_reaction('C[e] => E[e]'))
        self._database.set_reaction('rxn_5', parse_reaction('D[e] => E[e]'))
        self._database.set_reaction('rxn_6', parse_reaction('E[e] =>'))
        self._database.set_reaction('ex_A', parse_reaction('A[e] <=>'))

        self._mm = MetabolicModel.load_model(self._database,
                                             self._database.reactions)
        self._assoc = {
            'rxn_1': boolean.Expression('gene_1'),
            'rxn_2': boolean.Expression('gene_2 or gene_3'),
            'rxn_5': boolean.Expression('gene_3 and gene_4')
        }
        self._obj_reaction = 'rxn_6'

        try:
            self._solver = generic.Solver()
        except generic.RequirementsError:
            self.skipTest('Unable to find an LP solver for tests')

        self._prob = fluxanalysis.FluxBalanceProblem(self._mm, self._solver)
Ejemplo n.º 2
0
    def test_get_gene_association(self):
        expected_association = {
            'rxn_1': boolean.Expression('gene_1 and gene_2'),
            'rxn_2': boolean.Expression('gene_3'),
            'rxn_4': boolean.Expression('gene_4 and gene_6 and gene_5')
        }

        self.assertEqual(dict(randomsparse.get_gene_associations(self._model)),
                         expected_association)
Ejemplo n.º 3
0
    def test_get_rxn_value(self):
        mm_irreversible, reversible_gene_assoc, split_rxns =\
            gimme.make_irreversible(self._mm, self._assoc,
            exclude_list=['ex_A', 'rxn_6'])
        f = ['gene\texpression', 'gene_1\t15', 'gene_2\t20',
             'gene_3\t15', 'gene_4\t25', 'gene_5\t10', 'gene_6\t25']
        d = gimme.parse_transcriptome_file(f, 20)
        root_single = gimme.get_rxn_value(boolean.Expression(
            reversible_gene_assoc['rxn_1'])._root, d)
        self.assertEqual(root_single, 5)

        root_and = gimme.get_rxn_value(boolean.Expression(
            reversible_gene_assoc['rxn_5'])._root, d)
        self.assertEqual(root_and, 10)

        root_none = gimme.get_rxn_value(boolean.Expression(
            reversible_gene_assoc['rxn_2'])._root, d)
        self.assertEqual(root_none, None)
Ejemplo n.º 4
0
    def setUp(self):
        self._database = DictDatabase()
        self._database.set_reaction('rxn_1', parse_reaction('|A| <=>'))
        self._database.set_reaction('rxn_2', parse_reaction('|A| <=> |B|'))
        self._database.set_reaction('rxn_3', parse_reaction('|C| <=> |B|'))
        self._database.set_reaction('rxn_4', parse_reaction('|C| <=>'))
        self._mm = MetabolicModel.load_model(self._database,
                                             self._database.reactions)
        self._assoc = {'rxn_2': boolean.Expression('gene_1 or gene_2')}

        self._strategy = randomsparse.GeneDeletionStrategy(
            self._mm, self._assoc)
Ejemplo n.º 5
0
    def setUp(self):
        self.dest = tempfile.mkdtemp()

        self.model = NativeModel({
            'id': 'test_mode',
            'name': 'Test model',
        })

        # Compounds
        self.model.compounds.add_entry(
            CompoundEntry({
                'id': 'cpd_1',
                'formula': Formula.parse('CO2')
            }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_2',
        }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_3',
        }))

        # Compartments
        self.model.compartments.add_entry(CompartmentEntry({'id': 'c'}))
        self.model.compartments.add_entry(CompartmentEntry({'id': 'e'}))

        # Reactions
        self.model.reactions.add_entry(
            ReactionEntry({
                'id':
                'rxn_1',
                'equation':
                parse_reaction('(2) cpd_1[c] <=> cpd_2[e] + cpd_3[c]'),
                'genes':
                boolean.Expression('g_1 and (g_2 or g_3)'),
                'subsystem':
                'Some subsystem'
            }))
        self.model.reactions.add_entry(
            ReactionEntry({
                'id':
                'rxn_2',
                'equation':
                parse_reaction(
                    '(0.234) cpd_1[c] + (1.34) cpd_2[c] => cpd_3[c]'),
                'subsystem':
                'Biomass'
            }))

        self.model.biomass_reaction = 'rxn_2'
        self.model.limits['rxn_1'] = 'rxn_1', Decimal(-42), Decimal(1000)
        self.model.exchange[Compound('cpd_2', 'e')] = (Compound('cpd_2', 'e'),
                                                       'EX_cpd_2', -10, 0)
Ejemplo n.º 6
0
def gene_deletion(model, mm, solver):
    genes = {}
    gene_assoc = {}
    for reaction in model.parse_reactions():
        if reaction.genes is None:
            continue
        expr = boolean.Expression(reaction.genes)
        gene_assoc[reaction.id] = expr
        for var in expr.variables:
            genes.setdefault(var.symbol, set()).add(reaction.id)

    p = fluxanalysis.FluxBalanceProblem(mm, solver)
    biomass = model.get_biomass_reaction()

    p.maximize(biomass)
    wt_biomass = p.get_flux(biomass)
    #print('Wildtype biomass: {}'.format(wt_biomass))

    for gene, reactions in iteritems(genes):
        #print('Deleting {}...'.format(gene))

        deleted_reactions = set()
        for reaction in reactions:
            e = gene_assoc[reaction].substitute(lambda v: False
                                                if v.symbol == gene else v)
            if e.has_value() and not e.value:
                deleted_reactions.add(reaction)

        constr = []
        for reaction in deleted_reactions:
            constr.extend(
                p.prob.add_linear_constraints(p.get_flux_var(reaction) == 0))

        try:
            p.maximize(biomass)
            yield gene, p.get_flux(biomass) / wt_biomass
        except fluxanalysis.FluxBalanceError:
            yield gene, 0.0
        finally:
            for c in constr:
                c.delete()
Ejemplo n.º 7
0
def gene_deletion(model, mm, solver, algorithm):
    genes = {} # Genes -> reaction1, reaction2
    gene_assoc = {} # Reaction -> gene1, gene2

    # Associate genes with all the reactions
    for reaction in model.parse_reactions():

        # Ignore anything that doesn't have any genes
        if reaction.genes is None:
            continue
        # Express the genes controling a reaction as the Expression datatype
        expr = boolean.Expression(reaction.genes)
        # Associates each reaction (reaction.id) with all the genes that control it (expr)
        gene_assoc[reaction.id] = expr

        # Create a dictionary of each of the genes and all the associated reactions
        for var in expr.variables:
            genes.setdefault(var.symbol, set()).add(reaction.id)

    # Set up the MOMA solver
    p = moma.MOMAProblem(mm, solver)

    # Get the equation that the solver will use for biomass
    biomass = model.get_biomass_reaction()

    # Get the wildtype biomass for comparison later
    wt_biomass = p.get_fba_biomass(biomass)

    # Get the wildtype fluxes that we are going to constrain are model with
    wt_fluxes = p.get_fba_flux(biomass)

    #wt_biomass = p.get_flux(biomass)
    print('Wildtype biomass: {}'.format(wt_biomass))
    for gene, reactions in iteritems(genes):
        #print('Deleting {}...'.format(gene))
        deleted_reactions = set()

        # Search through all the reactions and mark each reaction associated with the gene
        for reaction in reactions:
            e = gene_assoc[reaction].substitute(
                lambda v: False if v.symbol == gene else v)
            if e.has_value() and not e.value:
                deleted_reactions.add(reaction) # Add the reaction to the deleted list

        #print(deleted_reactions)
        constr = []
        # Set all the reactions assiciated to the gene to a flux state of 0
        for reaction in deleted_reactions:
            constr.extend(p.prob.add_linear_constraints(
                p.get_flux_var(reaction) == 0))
        #print(constr)

        # Test the biomass
        try:
            if algorithm == "lp2":
                p.minimize_lp2(biomass, wt_fluxes)
            elif algorithm == "qlp2":
                p.minimize_qlp2(biomass, wt_fluxes)
            elif algorithm == "lp3":
                p.minimize_l1(biomass)
            elif algorithm == "qlp3":
                p.minimize_l2(biomass)
            yield gene, p.get_flux(biomass) / wt_biomass
        except moma.MOMAError:
            yield gene, -5.0
        # Do upon exit
        finally:
            for c in constr:
                c.delete()