예제 #1
0
    def test_execute_action_deletion(self):
        action = "deletion"

        # Delete Gene
        target = Mock()
        gene = Gene()
        self.widget.execute_action((target, action, gene))
        target.remove_child.assert_called_once_with(gene)

        # Delete GeneGroup
        target = Mock()
        gene_group = GeneGroup()
        gene_group.delete_children = Mock()
        self.widget.execute_action((target, action, gene_group))
        target.remove_child.assert_called_once_with(gene_group)
        assert gene_group.delete_children.called is False

        # Check that children are deleted if genegroup has only one parent
        target = Mock()
        gene_group = GeneGroup()
        gene_group.add_parent(self.reaction)
        gene_group.delete_children = Mock()
        self.widget.execute_action((target, action, gene_group))
        target.remove_child.assert_called_once_with(gene_group)
        assert gene_group.delete_children.called is True
예제 #2
0
 def test_nested_genegroups_with_gene(self):
     genegroup1 = GeneGroup()
     genegroup2 = GeneGroup()
     gene = Gene("gene id")
     genegroup1.add_child(genegroup2)
     genegroup2.add_child(gene)
     assert genegroup1.gem_reaction_rule == gene.id
     gene2 = Gene("gene2 id")
     genegroup2.add_child(gene2)
     assert genegroup1.gem_reaction_rule == "("+gene.id+" and "+gene2.id+")"
예제 #3
0
 def test_gene_and_gene_group(self):
     gene1 = Gene("gene 1")
     gene2 = Gene("gene 2")
     gene3 = Gene("gene 3")
     genegroup1 = GeneGroup(type="and")
     genegroup2 = GeneGroup(type="or")
     reaction = Reaction()
     reaction.add_child(genegroup1)
     genegroup1.add_child(gene1)
     genegroup1.add_child(genegroup2)
     genegroup2.add_child(gene2)
     assert reaction.gene_reaction_rule == gene1.id+" and "+gene2.id
     genegroup2.add_child(gene3)
     assert reaction.gene_reaction_rule == gene1.id+" and ("+gene2.id+" or "+gene3.id+")"
예제 #4
0
def _parse_gene_xml_tree(node, reaction, genes):
    """ Parse the gene tree as saved in an sbml file with fbc package """

    # Check that
    if node.tag == fbc_geneProductAssociation:
        top_level_child = _parse_gene_xml_tree(node.getchildren()[0], reaction,
                                               genes)
        reaction.add_child(top_level_child)
    elif node.tag in (fbc_and, fbc_or):
        gene_group = GeneGroup(id=node.get(ge_id),
                               type="and" if node.tag == fbc_and else "or")
        for child in node.getchildren():
            child_object = _parse_gene_xml_tree(child, reaction, genes)
            gene_group.add_child(child_object)
        return gene_group

    elif node.tag == fbc_geneProductRef:
        gene_id = clip(node.attrib[fbc_geneProduct], "G_")
        # Resubstitute dots
        gene_id = gene_id.replace(SBML_DOT, ".")
        try:
            return genes.get_by_id(gene_id)
        except KeyError:
            new_gene = Gene(id=gene_id, name=gene_id)
            genes.append(new_gene)
            warn("Gene {0} not found in gene list. New gene created!".format(
                gene_id))
            return new_gene
    else:
        raise TypeError("Unknown node.tag {0} at line {1}".format(
            node.tag, node.sourceline))
예제 #5
0
    def test_genegroup_enabled_different_genes(self):
        group1 = GeneGroup()
        gene1 = Gene()
        gene1.functional = False
        group1.add_child(gene1)

        group1.type = "and"
        assert group1.functional is False

        group1.type = "or"
        assert group1.functional is False

        # Add second functional gene
        gene2 = Gene()
        gene2.functional = True
        group1.add_child(gene2)

        group1.type = "and"
        assert group1.functional is False

        group1.type = "or"
        assert group1.functional is True

        group1.type = "unknown"
        with pytest.raises(ValueError):
            group1.functional
예제 #6
0
    def test_reaction_is_disabled_with_gene_in_and_group(self):
        reaction = Reaction()
        genegroup = GeneGroup(type="and")
        reaction.add_child(genegroup)
        gene = Gene()
        genegroup.add_child(gene)
        gene.functional = False

        assert reaction.functional is False
예제 #7
0
    def test_removal_of_nested_or_groups(self):
        reaction = Reaction("r1")
        gene_group1 = GeneGroup(type="or")
        gene_group2 = GeneGroup(type="or")
        gene1 = Gene("g1")
        gene2 = Gene("g2")
        gene3 = Gene("g3")
        gene_group1.add_child(gene1)
        for x in (gene2, gene3):
            gene_group2.add_child(x)
        gene_group1.add_child(gene_group2)
        reaction.add_child(gene_group1)

        assert reaction.genes == set([gene1, gene2, gene3])
        prune_gene_tree(reaction)
        assert reaction.genes == set([gene1, gene2, gene3])
        assert not gene_group2._children
        assert not gene_group2._parents
예제 #8
0
 def test_two_genes_one_gene_group(self, reaction):
     gene1 = Gene("test 1")
     gene2 = Gene("test 2")
     group = GeneGroup(type="and")
     group.add_child(gene1)
     group.add_child(gene2)
     reaction.add_child(group)
     assert reaction.gene_reaction_rule == gene1.id + " and " + gene2.id
     group.type = "or"
     assert reaction.gene_reaction_rule == gene1.id + " or " + gene2.id
예제 #9
0
    def test_removal_empty_genegroup(self):
        reaction = Reaction("r1")
        gene_group = GeneGroup()
        reaction.add_child(gene_group)

        assert gene_group in reaction._children
        assert reaction in gene_group._parents
        prune_gene_tree(reaction)

        assert gene_group not in reaction._children
        assert reaction not in gene_group._parents
예제 #10
0
class TestGene:

    def test_default_values(self):
        gene = Gene()
        assert gene.id == ""
        assert gene.name == ""
        assert gene.genome == ""
        assert gene.annotation == set()
        assert gene.functional is True

    @pytest.mark.parametrize("child", [Gene(), Reaction(), GeneGroup()])
    def test_gene_add_child(self, child):
        gene = Gene()
        with pytest.raises(NotImplementedError):
            gene.add_child(child)

    @pytest.mark.parametrize("child", [Gene(), Reaction(), GeneGroup()])
    def test_gene_remove_child(self, child):
        gene = Gene()
        with pytest.raises(NotImplementedError):
            gene.remove_child(child)
예제 #11
0
    def add_genegroup(self):
        """ Buffer the addition of a genegroup to the genegroup and change the model"""
        # Get current item
        table_item, model_item = self.get_selected_item()

        new_group = GeneGroup()
        new_item = LinkedItem(text=str(new_group.type).upper(), link=new_group)
        new_item.setEditable(False)
        table_item.appendRow(new_item)

        # Buffer the function call for when user accepts dialog
        self.cached_actions.append((model_item, "addition", new_group))
        self.changed.emit()
예제 #12
0
 def test_multiple_nested(self):
     reaction = Reaction()
     parent = reaction
     for _ in range(5):
         genegroup = GeneGroup()
         parent.add_child(genegroup)
         parent = genegroup
     gene = Gene("Gene id")
     parent.add_child(gene)
     assert reaction.gene_reaction_rule == gene.id
     gene2 = Gene("Gene 2")
     parent.add_child(gene2)
     assert reaction.gene_reaction_rule == "(" + gene.id+" and "+gene2.id+")"
예제 #13
0
 def test_context_menu_genegroup(self):
     reaction = Reaction()
     model = Model()
     genegroup = GeneGroup()
     self.widget.set_item(reaction, model)
     self.widget.geneTable.appendRow(LinkedItem("test", genegroup))
     assert self.widget.geneTable.rowCount() == 1
     index = self.widget.geneTable.item(0, 0).index()
     self.widget.geneView.setCurrentIndex(index)
     assert self.widget.get_selected_item()[1] is genegroup
     menu = self.widget.show_gene_contextmenu(QtCore.QPoint())
     assert self.widget.delete_genegroup_action in menu.actions()
     assert self.widget.add_gene_action in menu.actions()
     assert self.widget.add_genegroup_action in menu.actions()
예제 #14
0
def merge_reactions(list_of_reactions, base_reaction):
    """ Merge duplicated reactions

    Parameters
    ----------
    list_of_reactions: list, list of reactions that should be merged
    base_reaction: GEMEditor.model.classes.cobra.Reaction, Reaction to keep

    Returns
    -------

    """

    # Move all genes of the base reaction to new gene group
    temp_children = []

    base_children = extract_genes_from_reaction(base_reaction)
    if base_children is not None:
        temp_children.append(base_children)

    merged_reactions = defaultdict(list)
    for reaction in list_of_reactions:
        if reaction is not base_reaction:
            # Add annotations
            base_reaction.annotation.update(reaction.annotation)
            # Add genes
            genes = extract_genes_from_reaction(reaction)
            if genes is not None:
                temp_children.append(genes)
            # Move evidences
            for evidence in reaction.evidences:
                evidence.substitute_item(reaction, base_reaction)

            # Mark reaction for deletion
            merged_reactions[reaction.model].append(reaction)

    # Remove reactions
    for model, delete_reactions in merged_reactions.items():
        if model:
            model.gem_remove_reactions(delete_reactions)

    # Add new genes to base reaction
    if len(temp_children) > 1:
        combined_genes = GeneGroup(type="or")
        for element in temp_children:
            combined_genes.add_child(element)
        base_reaction.add_child(combined_genes)
    elif len(temp_children) == 1:
        base_reaction.add_child(temp_children[0])
예제 #15
0
    def test_removal_of_genegroup_with_one_child(self, type):
        reaction = Reaction("r1")
        gene_group = GeneGroup(type=type)
        gene = Gene()
        reaction.add_child(gene_group)
        gene_group.add_child(gene)

        assert gene_group in reaction._children
        assert reaction in gene_group._parents
        assert gene in reaction.genes
        prune_gene_tree(reaction)

        assert gene_group not in reaction._children
        assert reaction not in gene_group._parents
        assert gene in reaction.genes
예제 #16
0
    def test_execute_action_addition(self):

        action = "addition"

        # Add Gene
        target = Mock()
        gene = Gene()
        self.widget.execute_action((target, action, gene))
        target.add_child.assert_called_once_with(gene)

        # Add GeneGroup
        target = Mock()
        gene_group = GeneGroup()
        self.widget.execute_action((target, action, gene_group))
        target.add_child.assert_called_once_with(gene_group)
예제 #17
0
    def setup_complete_model(self):
        self.model = Model("model")

        # Setup reaction1
        self.metabolite = Metabolite("m1")
        self.reaction = Reaction("r1")
        self.gene = Gene("g1")
        self.reaction.add_child(self.gene)
        self.model.add_genes((self.gene,))
        self.reaction.add_metabolites({self.metabolite: -1})
        self.model.add_reactions((self.reaction,))

        # Setup reaction2
        self.metabolite2 = Metabolite("m2")
        self.reaction2 = Reaction("r2")
        self.gene2 = Gene("g2")
        self.genegroup = GeneGroup()
        self.genegroup.add_child(self.gene2)
        self.reaction2.add_child(self.genegroup)
        self.model.add_genes((self.gene2,))
        self.reaction2.add_metabolites({self.metabolite2: 1})
        self.model.add_reactions((self.reaction2,))

        # Setup evidences
        self.evidence = Evidence(assertion="Catalyzes")
        self.evidence.set_entity(self.gene)
        self.evidence.set_target(self.reaction)
        self.reference = Reference()
        self.model.add_reference(self.reference)
        self.evidence.add_reference(self.reference)
        self.model.add_evidence(self.evidence)

        # Setup test case
        self.testcase = ModelTest()
        reaction_setting = ReactionSetting(self.reaction, 1000., -1000., 0.)
        gene_setting = GeneSetting(gene=self.gene2, activity=False)
        outcome = Outcome(self.reaction2, 0., "greater")
        self.testcase.add_outcome(outcome)
        self.testcase.add_setting(gene_setting)
        self.testcase.add_setting(reaction_setting)
        self.model.add_test(self.testcase)
        self.reference2 = Reference()
        self.model.add_reference(self.reference2)
        self.testcase.add_reference(self.reference2)

        self.model.setup_tables()
예제 #18
0
    def test_delete_children(self):
        reaction = Reaction()
        reaction2 = Reaction()
        genegroup = GeneGroup()
        gene = Gene()

        reaction.add_child(genegroup)
        reaction2.add_child(genegroup)
        genegroup.add_child(gene)
        assert genegroup in reaction._children
        assert reaction in genegroup._parents
        assert genegroup in reaction2._children
        assert reaction2 in genegroup._parents
        assert gene in genegroup._children
        assert genegroup in gene._parents
        reaction.delete_children()
        assert genegroup not in reaction._children
        assert reaction not in genegroup._parents
        assert gene not in genegroup._children
        assert genegroup not in gene._parents
        assert genegroup in reaction2._children
        assert reaction2 in genegroup._parents
예제 #19
0
def extract_genes_from_reaction(reaction):
    """ Remove all genes from a reaction an get a new genegroup from it

    Parameters
    ----------
    reaction: GEMEditor.model.classes.cobra.Reaction

    Returns
    -------

    """
    if not reaction._children:
        return None
    elif len(reaction._children) == 1:
        child = list(reaction._children)[0]
        child.remove_parent(reaction, all=False)
        return child

    new_genegroup = GeneGroup(type="or")
    for element in reaction._children:
        element.remove_parent(reaction, all=False)
        element.add_parent(new_genegroup)
    return new_genegroup
예제 #20
0
class TestReaction:

    @pytest.fixture()
    def setup_item(self):
        self.test_id = "test_id"
        self.test_name = "test_name"
        self.test_upper_bound = 1000.
        self.test_lower_bound = -1000.
        self.reaction = Reaction(id=self.test_id,
                                 name=self.test_name,
                                 lower_bound=self.test_lower_bound,
                                 upper_bound=self.test_upper_bound)

    @pytest.mark.usefixtures("setup_item")
    def test_setup_item(self):
        assert self.reaction.id == self.test_id
        assert self.reaction.name == self.test_name
        assert self.reaction.upper_bound == self.test_upper_bound
        assert self.reaction.lower_bound == self.test_lower_bound
        assert self.reaction._model is None

    def test_empty_reaction_setup(self):
        reaction = Reaction()

        assert reaction.id == ""
        assert reaction.name == ""
        assert reaction.subsystem == ""
        assert reaction.lower_bound == 0.
        assert reaction.upper_bound == 1000.
        assert reaction.comment == ""
        assert reaction.annotation == set()
        assert reaction.model is None
        assert len(reaction._children) == 0

    def test_none_reaction_setup(self):
        reaction = Reaction(id=None,
                            name=None,
                            subsystem=None,
                            lower_bound=None,
                            upper_bound=None,
                            comment=None)

        assert reaction.id == ""
        assert reaction.name == ""
        assert reaction.subsystem == ""
        assert reaction.lower_bound == 0.
        assert reaction.upper_bound == 1000.
        assert reaction.comment == ""
        assert len(reaction._children) == 0

    @pytest.mark.usefixtures("setup_item")
    @pytest.mark.parametrize("lower_bound,upper_bound,objective", [(0., 1000., 0.),
                                                                   (-1000., 1000., 1.),
                                                                   (-1000., 0., 20.),
                                                                   (0., 0., 0.)])
    def test_get_setting(self, lower_bound, upper_bound, objective):
        self.reaction.upper_bound = upper_bound
        self.reaction.lower_bound = lower_bound
        setting = self.reaction.get_setting()
        assert setting.reaction is self.reaction
        assert setting.upper_bound == upper_bound
        assert setting.lower_bound == lower_bound

    def test_gene_addition(self):
        reaction = Reaction("reaction_id")
        gene = Gene("gene_id")

        reaction.add_child(gene)
        assert gene in reaction.genes
        assert reaction in gene.reactions

    def test_reaction_gene_addition(self, ex_empty_reaction):
        test_gene = Gene(id="test", name="test")

        # Check addition
        ex_empty_reaction.add_child(test_gene)
        assert test_gene in ex_empty_reaction._children
        assert ex_empty_reaction in test_gene._parents

        # Check removal
        ex_empty_reaction.remove_child(test_gene)
        assert test_gene not in ex_empty_reaction._children
        assert ex_empty_reaction not in test_gene._parents

    def test_reaction_genegroup_addition(self):
        reaction = ex_empty_reaction()
        genegroup = ex_empty_geneGroup()

        # Check addition and references
        reaction.add_child(genegroup)
        assert genegroup in reaction._children
        assert reaction in genegroup._parents

        # Check removal
        reaction.remove_child(genegroup)
        assert genegroup not in reaction._children
        assert genegroup._parents == []

    @pytest.mark.parametrize("parent", [Gene(), GeneGroup(), Reaction()])
    def test_add_parent(self, parent):
        reaction = Reaction()
        with pytest.raises(NotImplementedError):
            reaction.add_parent(parent)

    @pytest.mark.parametrize("parent", [Gene(), GeneGroup(), Reaction()])
    def test_remove_parent(self, parent):
        reaction = Reaction()
        with pytest.raises(NotImplementedError):
            reaction.remove_parent(parent)

    @pytest.mark.usefixtures("setup_item")
    def test_reaction_subsystem_setting(self):
        model = Model()

        # Test subsystem setting while reaction not in model
        new_subsystem1 = "New subsystem"
        self.reaction.subsystem = new_subsystem1
        assert self.reaction.subsystem == new_subsystem1

        # Test that the subsystem is updated on the model
        model.add_reactions([self.reaction])
        assert model is self.reaction._model
        assert len(model.subsystems) == 1
        assert new_subsystem1 in model.subsystems
        assert self.reaction in model.subsystems[new_subsystem1]

        # Test that changing the subsystem is reflected in model.subsystems
        new_subsystem2 = "Different Stubsystem"
        self.reaction.subsystem = new_subsystem2
        assert len(model.subsystems) == 1
        assert new_subsystem2 in model.subsystems
        assert self.reaction in model.subsystems[new_subsystem2]

    def test_reaction_enabled_without_genes(self):
        reaction = Reaction()
        assert reaction.functional is True

    def test_reaction_enabled_true_with_empty_genegroup(self):
        reaction = Reaction()
        genegroup = GeneGroup()
        reaction.add_child(genegroup)

        assert reaction.functional is True

    def test_reaction_is_disabled_with_disabled_gene_only(self):
        reaction = Reaction()
        gene = Gene()
        gene.functional = False
        reaction.add_child(gene)

        assert reaction.functional is False

    def test_reaction_is_disabled_with_gene_in_or_group(self):
        reaction = Reaction()
        genegroup = GeneGroup(type="or")
        reaction.add_child(genegroup)
        gene = Gene()
        genegroup.add_child(gene)
        gene.functional = False

        assert reaction.functional is False

    def test_reaction_is_disabled_with_gene_in_and_group(self):
        reaction = Reaction()
        genegroup = GeneGroup(type="and")
        reaction.add_child(genegroup)
        gene = Gene()
        genegroup.add_child(gene)
        gene.functional = False

        assert reaction.functional is False
예제 #21
0
 def genegroup(self):
     return GeneGroup()
예제 #22
0
 def test_one_gene_genegroup(self):
     gene = Gene("test 1")
     genegroup = GeneGroup()
     genegroup.add_child(gene)
     assert genegroup.gem_reaction_rule == gene.id
예제 #23
0
    def test_empty_gene_group_setup(self):
        gene_group = GeneGroup()

        assert gene_group.id
        assert gene_group.type == "and"
예제 #24
0
    def test_reaction_enabled_true_with_empty_genegroup(self):
        reaction = Reaction()
        genegroup = GeneGroup()
        reaction.add_child(genegroup)

        assert reaction.functional is True
예제 #25
0
 def test_nested_genegroups_wo_gene(self):
     genegroup1 = GeneGroup()
     genegroup2 = GeneGroup()
     genegroup1.add_child(genegroup2)
     assert genegroup1.gem_reaction_rule == ""
예제 #26
0
def ex_empty_geneGroup():
    return GeneGroup()
예제 #27
0
 def test_genegroup_enabled_for_empty_group(self):
     genegroup = GeneGroup()
     assert genegroup.functional is None