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))
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
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+")"
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
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
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])
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
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()
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
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
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)
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
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()
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+")"
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()
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)
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
def ex_empty_geneGroup(): return GeneGroup()
def test_nested_genegroups_wo_gene(self): genegroup1 = GeneGroup() genegroup2 = GeneGroup() genegroup1.add_child(genegroup2) assert genegroup1.gem_reaction_rule == ""
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
def test_reaction_enabled_true_with_empty_genegroup(self): reaction = Reaction() genegroup = GeneGroup() reaction.add_child(genegroup) assert reaction.functional is True
class TestModelDeleteItems: @pytest.fixture(autouse=True) 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() def test_setup(self): # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_deletion_metabolite(self): # Action self.model.gem_remove_metabolites((self.metabolite,)) # Test gene gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model, valid=False) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction, valid=False) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_deletion_metabolite2(self): # Action self.model.gem_remove_metabolites((self.metabolite2,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model, valid=False) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2, valid=False) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_delete_reaction1(self): #### # Delete reaction # Check: # - gene present but removed from reaction1 # - metabolite present but removed from reaction1 # - reaction deleted # - evidence deleted # - testcase deleted # - reference present but removed from evidence # - reference2 present but removed from testcase #### # Action self.model.gem_remove_reactions((self.reaction,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model, valid=False) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test model test case_in_model(self.testcase, self.model, valid=False) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence, valid=False) reference_in_item(self.reference2, self.testcase, valid=False) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction, valid=False) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 # Check metabolite is still in reaction, but reaction not in metabolite assert self.metabolite in self.reaction.metabolites assert self.reaction not in self.metabolite.reactions metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence, valid=False) item_in_evidence(self.reaction, self.evidence, valid=False) # Check evidence is removed when reference deleted del self.evidence gc.collect() assert len(self.model.all_evidences) == 0 def test_deletion_reaction2(self): # Check: # - reaction2 removed # - test case removed # - reference removed from testcase # - gene2 removed from reaction # Action self.model.gem_remove_reactions((self.reaction2,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model, valid=False) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model, valid=False) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase, valid=False) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2, valid=False) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) # Check metabolite still in reaction, but reaction not in metabolite assert self.metabolite2 in self.reaction2.metabolites assert self.reaction2 not in self.metabolite2.reactions # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_delete_gene1(self): # Test gene deletion # Check: # - Gene removed from model # - Evidence removed from model # Action self.model.gem_remove_genes((self.gene,)) # Test genes gene_in_model(self.gene, self.model, valid=False) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence, valid=False) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction, valid=False) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence, valid=False) item_in_evidence(self.reaction, self.evidence, valid=False) # Check evidence is removed when reference deleted del self.evidence gc.collect() assert len(self.model.all_evidences) == 0 def test_delete_gene2(self): # Test removal of gene2 # Check: # - Gene2 removed from model # - Gene2 removed from reaction2 # - Testcase deleted from model # - Reference2 removed from testcase # Action self.model.gem_remove_genes((self.gene2,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model, valid=False) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model, valid=False) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase, valid=False) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2, valid=False) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_delete_testcase(self): # Action self.model.gem_remove_tests((self.testcase,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model, valid=False) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase, valid=False) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_delete_referenec(self): # Action self.model.gem_remove_references((self.reference,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model, valid=False) reference_in_model(self.reference2, self.model) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence, valid=False) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence) def test_delete_reference2(self): # Action self.model.gem_remove_references((self.reference2,)) # Test genes gene_in_model(self.gene, self.model) gene_in_model(self.gene2, self.model) # Test reactions reaction_in_model(self.reaction, self.model) reaction_in_model(self.reaction2, self.model) # Test metabolites metabolite_in_model(self.metabolite, self.model) metabolite_in_model(self.metabolite2, self.model) # Test evidences evidence_in_model(self.evidence, self.model) # Test model test case_in_model(self.testcase, self.model) # Test references reference_in_model(self.reference, self.model) reference_in_model(self.reference2, self.model, valid=False) # Test connections # Reference <-> Evidence reference_in_item(self.reference, self.evidence) # Reference2 <-> Testcase reference_in_item(self.reference2, self.testcase, valid=False) # Gene1 <-> Reaction1 gene_in_reaction(self.gene, self.reaction) gene_in_reaction(self.gene2, self.reaction2) # Metabolite1 <-> Reaction1 metabolite_in_reaction(self.metabolite, self.reaction) metabolite_in_reaction(self.metabolite2, self.reaction2) # Evidence <-> Gene1/Reaction item_in_evidence(self.gene, self.evidence) item_in_evidence(self.reaction, self.evidence)
def test_empty_gene_group_setup(self): gene_group = GeneGroup() assert gene_group.id assert gene_group.type == "and"
def test_genegroup_enabled_for_empty_group(self): genegroup = GeneGroup() assert genegroup.functional is None
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
def genegroup(self): return GeneGroup()
def test_one_gene_genegroup(self): gene = Gene("test 1") genegroup = GeneGroup() genegroup.add_child(gene) assert genegroup.gem_reaction_rule == gene.id
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+")"