def test_handle_relationships(self): # Tests the insertion, update and deletion of relationships between CV terms # Populate the database with essentials other_dbxref = general.DbxRef(db_id=self.default_db.db_id, accession="otheraccession") self.client.add_and_flush(other_dbxref) other_cvterm = cv.CvTerm(cv_id=self.default_cv.cv_id, dbxref_id=other_dbxref.dbxref_id, name="otherterm") self.client.add_and_flush(other_cvterm) default_id = ontology.create_dbxref(self.default_db.name, self.default_dbxref.accession) other_id = ontology.create_dbxref(self.default_db.name, other_dbxref.accession) all_cvterms = {default_id: self.default_cvterm, other_id: other_cvterm} # Check that an ontology term without relationships will not create a cvterm_relationship entry term = pronto.Term("defaultdb:defaultaccession") first_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms) self.assertEqual(len(first_relationships), 0) # Insert a relationship term.relations = {pronto.Relationship("part_of"): [pronto.Term(other_id, other_cvterm.name)]} second_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms) self.assertEqual(len(second_relationships), 1) self.assertIsNotNone(second_relationships[0].cvterm_relationship_id) self.assertEqual(second_relationships[0].subject_id, self.default_cvterm.cvterm_id) self.assertEqual(second_relationships[0].object_id, other_cvterm.cvterm_id) self.assertEqual(second_relationships[0].type_id, self.client._relationship_terms["part_of"].cvterm_id) # Insert a new relationship between the same terms term.relations.clear() term.relations = {pronto.Relationship("is_a"): [pronto.Term(other_id, other_cvterm.name)]} third_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms) self.assertEqual(len(third_relationships), 1) self.assertEqual(third_relationships[0].type_id, self.client._relationship_terms["is_a"].cvterm_id)
def test_with_custom_typedef(self): """Try to import an obo ontology containing custom typedefs """ obo = pronto.Ontology("tests/resources/elo.obo") self.check_ontology(obo) self.assertIn("has_written", pronto.Relationship._instances) self.assertIn(pronto.Relationship("written_by"), pronto.Relationship._instances.values()) self.assertIn(pronto.Relationship('written_by'), obo['ELO:0130001'].relations) self.assertIn(pronto.Relationship('has_written'), obo['ELO:0330001'].relations)
def test_handle_typedef(self): # Tests the insertion of relationship-type CV terms # Insert a new entry relationship = pronto.Relationship("new_relationship") first_typedef = self.client._handle_typedef(relationship, self.default_db, self.default_cv) self.assertEqual(first_typedef.name, "new_relationship") self.assertEqual(first_typedef.cv_id, self.default_cv.cv_id) self.assertTrue(first_typedef.is_relationshiptype) self.assertIsNotNone(first_typedef.cvterm_id) # Insert a further entry with a different name relationship = pronto.Relationship("another_relationship") second_typedef = self.client._handle_typedef(relationship, self.default_db, self.default_cv) self.assertNotEqual(first_typedef.cvterm_id, second_typedef.cvterm_id) # Try to insert a further entry with the same authority, and check that the function returns the existing entry third_typedef = self.client._handle_typedef(relationship, self.default_db, self.default_cv) self.assertEqual(third_typedef.cvterm_id, second_typedef.cvterm_id)
def onto_rel_graph(onto, rel_type = pronto.Relationship('is_a')): obo_is_a = nx.DiGraph() nodes = {u for u,term in onto.terms.items() if not is_obsolete(term)} obo_is_a.add_nodes_from(nodes) rels = [(u,vs.id) for u in obo_is_a.nodes() for rel, vs in onto.terms[u].relations.items() if rel == rel_type] edges = ((u,v) for u, vs in rels for v in vs if v in nodes) obo_is_a.add_edges_from(edges) return obo_is_a