def test_get_items(self): evidence1 = Evidence(entity=Gene("g1")) evidence2 = Evidence(entity=Gene("g2")) self.table.update_row_from_item(evidence1) self.table.update_row_from_item(evidence2) items = self.table.get_items() assert len(items) == 2 assert items[0] is evidence1 assert items[1] is evidence2
def test_saving_changes(self): reaction = Reaction() reference = Reference() model = Model() evidence = Evidence(entity=reaction) evidence.add_reference(reference) widget = EvidenceDisplayWidget() model.all_evidences[evidence.internal_id] = evidence assert evidence in reaction.evidences assert evidence.internal_id in model.all_evidences assert reference in evidence.references # Set reaction and model to widget widget.set_item(reaction, model) assert widget.dataTable.rowCount() == 1 # Remove evidence copy from data table widget.dataTable.setRowCount(0) # Setup new evidence item new_evidence = Evidence() new_evidence.assertion = "Catalyzed by" new_evidence.entity = reaction reference = Reference() new_evidence.add_reference(reference, reciprocal=False) # Test the setup i.e. that items are only linked in a one way (evidence -> item) fashion assert new_evidence not in reaction.evidences assert new_evidence not in reference.linked_items # Add new reference to widget table widget.dataTable.update_row_from_item(new_evidence) assert widget.dataTable.rowCount() == 1 assert widget.dataTable.item(0).link is new_evidence # Action - Save evidences widget.save_state() # Check that old instance is detached from all links assert evidence not in reaction.evidences assert len(evidence.references) == 0 # Old evidence still kept alive by this test old_id = evidence.internal_id assert old_id in model.all_evidences evidence = None gc.collect() assert old_id not in model.all_evidences # Check that new evidence is linked properly assert new_evidence in reaction.evidences assert new_evidence.internal_id in model.all_evidences
def test_changes_are_saved(self): dialog = self.dialog evidence = Evidence(entity=self.entity) dialog.set_evidence(evidence, None) # Generate new values assert self.dialog.combo_assertion.currentIndex() == -1 new_assertion = self.dialog.combo_assertion.itemText(1) new_target = Reaction("r2") new_comment = "Comment" new_eco = "ECO:0000000" # Set new values self.dialog.set_assertion(new_assertion) self.dialog.set_eco(new_eco) self.dialog.set_target(new_target) self.dialog.set_comment(new_comment) self.dialog.referenceWidget.dataTable.update_row_from_item( self.reference) # Action self.dialog.save_state() # Check updates assert evidence.target is new_target assert evidence in new_target.evidences assert evidence.comment == new_comment assert evidence.eco == new_eco assert evidence.assertion == new_assertion assert self.reference in evidence.references assert evidence in self.reference.linked_items
def test_copy_evidence(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) # Create copy copy = evidence.copy() # Check the correctness of the copy assert evidence.entity is copy.entity assert evidence.target is copy.target assert evidence.eco == copy.eco assert evidence.assertion == copy.assertion assert evidence.comment == copy.comment assert evidence.references == copy.references assert evidence.references is not copy.references # Check that the copy is not linked assert copy not in entity.evidences assert copy not in reference.linked_items assert copy not in target.evidences
def save_state(self): genes_added = 0 for gene_id in self.plainTextEdit.toPlainText().split("\n"): try: gene = self.model.genes.get_by_id(gene_id.strip()) except KeyError: if self.comboBox_missing.currentText() == "Add": gene = Gene(gene_id.strip()) self.model.add_gene(gene) self.model.QtGeneTable.update_row_from_item(gene) genes_added += 1 else: continue # Generate an evidence item for each gene evidence = Evidence(entity=gene, eco=self.evidence.eco, assertion=self.evidence.assertion, comment=self.evidence.comment, target=self.evidence.target) # Add reference for reference in self.evidence.references: evidence.add_reference(reference) # Add evidence to model self.model.all_evidences[evidence.internal_id] = evidence if genes_added: QMessageBox(self).information( self, "Genes added", "{} new genes added!".format(str(genes_added)))
def test_evidence_init(self): entity = Reaction() target = Gene("id2") eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) # Check proper linking assert evidence.entity is entity assert evidence in entity.evidences assert evidence.target is target assert evidence in target.evidences # Check attributes assert evidence.assertion == assertion assert evidence.eco == eco assert evidence.comment == comment # Check id is set assert type(evidence.internal_id) is str assert evidence.internal_id != "" # Check the references assert reference in evidence.references assert evidence in reference.linked_items
def complete_evidence(self): evidence = Evidence(entity=self.entity, assertion=self.assertion, target=self.target, eco=self.eco, comment=self.comment) evidence.add_reference(self.reference) return evidence
def __init__(self, model, parent=None): super(BatchEvidenceDialog, self).__init__(parent) self.setupUi(self) self.model = model self.evidence = Evidence(entity=Gene()) self.accepted.connect(self.save_state) self.pushButton.clicked.connect(self.edit_evidence)
def test_sorting_erroneous(self): evidence = Evidence() assert evidence.is_valid() is None conflicts, failing, errors = sort_evidences([evidence]) assert not conflicts assert not failing assert evidence in errors
def test_sorting_conflicting(self): reaction = Reaction() gene = Gene() reaction.add_child(gene) evidence1 = Evidence(entity=gene, target=reaction, assertion="Catalyzing reaction") evidence2 = Evidence(entity=gene, target=reaction, assertion="Not catalyzing reaction") assert evidence1.is_valid() is True assert evidence2.is_valid() is False conflicts, failing, errors = sort_evidences([evidence1, evidence2]) assert len(conflicts) == 1 assert set(conflicts[0]) == set([evidence1, evidence2]) assert not failing assert not errors
def add_item(self): new_evidence = Evidence() # Set base_item externally in order to avoid linkage from base_item to # evidence new_evidence.entity = self.item dialog = EditEvidenceDialog(self.window(), self.model, evidence=new_evidence) if dialog.exec_(): self.dataTable.update_row_from_item(new_evidence)
def setup_empty_dialog(self): self.model = Model() self.evidence = Evidence() self.entity = Gene("g1") self.assertion = "Catalyzing reaction" self.target = Reaction("r1") self.eco = "ECO:0000250" self.comment = "Similarity to XY" self.reference = Reference() self.dialog = EditEvidenceDialog()
def test_entity_needed(self): evidence = Evidence(assertion="Localization") assert evidence.is_valid() is None evidence.set_entity(self.gene) assert evidence.is_valid() is None evidence.set_target(self.compartment) assert evidence.is_valid() is not None
def test_sorting_failing(self): reaction = Reaction() gene = Gene() evidence = Evidence(entity=gene, target=reaction, assertion="Catalyzing reaction") assert evidence.is_valid() is False conflicts, failing, errors = sort_evidences([evidence]) assert not conflicts assert [evidence] in failing assert not errors
def test_set_item(self): widget = EvidenceDisplayWidget() evidence = Evidence() reaction = Reaction() model = Model() evidence.set_entity(reaction) widget.set_item(reaction, model) # Check that a copy of the evidence has been added to the table assert widget.dataTable.rowCount() == 1 assert widget.dataTable.item(0).link is not evidence assert widget.dataTable.item( 0).link.internal_id == evidence.internal_id
def parse_evidences_from_xml(model_node, model, progress=None): list_of_evidences_node = model_node.find(ge_listOfEvidences) if list_of_evidences_node is None: return elif progress is None: pass elif not progress.wasCanceled(): progress.setLabelText("Reading evidences...") progress.setRange(0, len(list_of_evidences_node)) else: return for i, evidence_node in enumerate( list_of_evidences_node.iterfind(ge_evidence)): if progress is None: pass elif not progress.wasCanceled(): progress.setValue(i) QApplication.processEvents() else: return new_evidence = Evidence(internal_id=evidence_node.get("id"), assertion=evidence_node.get("assertion"), comment=evidence_node.get("comment"), eco=evidence_node.get("eco")) entity_id = evidence_node.get("entity_id") entity_type = evidence_node.get("entity_type") entity = get_item_from_model(entity_type, entity_id, model) new_evidence.set_entity(entity) # Add target item if present target_id = evidence_node.get("target_id") target_type = evidence_node.get("target_type") if target_id and target_type: target = get_item_from_model(target_type, target_id, model) new_evidence.set_target(target) # Add all references if present reference_list_node = evidence_node.find(ge_listOfReferenceLinks) if reference_list_node is not None: for refLink_node in reference_list_node.iterfind(ge_referenceLink): new_evidence.add_reference( model.references[refLink_node.get("id")]) model.all_evidences[new_evidence.internal_id] = new_evidence
def test_sorting_valid(self): reaction = Reaction() gene = Gene() evidence = Evidence(entity=gene, target=reaction, assertion="Catalyzing reaction") reaction.add_child(gene) assert evidence.is_valid() is True conflicts, failing, errors = sort_evidences([evidence]) assert not conflicts assert not failing assert not errors
def test_equality(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) assert evidence.copy() == evidence
def setup_items(self): self.gene = Gene("g1") self.compartment = Compartment("c") self.metabolite1 = Metabolite("m1") self.metabolite2 = Metabolite("m2") self.reaction = Reaction("r1") self.reaction.add_metabolites({ self.metabolite1: -1, self.metabolite2: 1 }) self.reaction.add_child(self.gene) self.evidence = Evidence(entity=self.gene, assertion="Localization", target=self.compartment)
def test_changed_triggered_deletion(self): widget = EvidenceDisplayWidget() evidence = Evidence() reaction = Reaction() model = Model() evidence.set_entity(reaction) widget.set_item(reaction, model) mock = Mock() widget.changed.connect(mock.test) assert mock.test.called is False assert widget.dataTable.rowCount() == 1 widget.dataView.selectRow(0) QtTest.QTest.mouseClick(widget.delete_button, QtCore.Qt.LeftButton) assert widget.dataTable.rowCount() == 0 assert mock.test.called is 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_changed_triggered_edition(self): widget = EvidenceDisplayWidget() evidence = Evidence() reaction = Reaction() model = Model() evidence.set_entity(reaction) reaction.add_evidence(evidence) widget.set_item(reaction, model) mock = Mock() widget.changed.connect(mock.test) assert mock.test.called is False assert widget.dataTable.rowCount() == 1 widget.dataView.selectRow(0) widget.edit_item() assert widget.dataTable.rowCount() == 1 assert mock.test.called is True
def test_inequality(self, attribute, new_value): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) new_copy = evidence.copy() setattr(new_copy, attribute, new_value) assert new_copy != evidence assert not new_copy == evidence
def test_row_from_item(self): gene1 = Gene("test id1") reaction1 = Reaction("react1") evidence = Evidence(entity=gene1, target=reaction1, assertion="Catalyze reaction", eco="ECO:001") return_values = self.table.row_from_item(evidence) assert len(return_values) == len(self.table.header) assert isinstance(return_values[0], LinkedItem) assert return_values[0].link is evidence assert return_values[0].text() == gene1.id assert return_values[1].text() == evidence.assertion assert return_values[2].text() == evidence.target.id assert return_values[3].text() == evidence.eco assert return_values[4].text() == evidence.comment assert return_values[5].text() == ""
def test_override_entity(self): entity = Reaction() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment) evidence.add_reference(reference) new_reaction = Reaction() evidence.set_entity(new_reaction) # Check disconnection of old reaction assert evidence.entity is new_reaction assert evidence not in entity.evidences
def test_inequality_reference(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) new_copy = evidence.copy() new_copy.remove_all_references() assert new_copy != evidence assert not new_copy == evidence
def test_override_target(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) new_gene = Gene() evidence.set_target(new_gene) # Check disconnection of old reaction assert evidence.target is new_gene assert evidence not in target.evidences
def test_delete_links(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence(entity=entity, eco=eco, assertion=assertion, comment=comment, target=target) evidence.add_reference(reference) evidence.delete_links() assert evidence.entity is None assert len(evidence.references) == 0 assert evidence not in entity.evidences assert evidence not in target.evidences
def setup_evidence(self): self.reaction = Reaction("r_id") self.gene = Gene("g_id") self.target = Gene("g_id2") self.model = Model("test id") self.reference = Reference() self.model.add_gene(self.gene) self.model.add_gene(self.target) self.model.add_reactions([self.reaction]) self.model.add_reference(self.reference) # Set type self.assertion = "Catalyzed by" self.eco = "ECO:0000000" self.comment = "test" self.evidence = Evidence(entity=self.reaction, eco=self.eco, assertion=self.assertion, comment=self.comment, target=self.target) self.evidence.add_reference(self.reference) self.model.all_evidences[self.evidence.internal_id] = self.evidence self.root = Element("root", nsmap={None: ge_ns})
def test_setup_links(self): entity = Reaction() target = Gene() eco = "ECO:000000" assertion = "Presence" comment = "test comment" reference = Reference() evidence = Evidence() evidence.entity = entity evidence.target = target evidence.eco = eco evidence.assertion = assertion evidence.comment = comment evidence.add_reference(reference, reciprocal=False) assert evidence not in entity.evidences assert evidence not in reference.linked_items assert evidence not in target.evidences evidence.setup_links() assert evidence in entity.evidences assert evidence in reference.linked_items assert evidence in target.evidences