7 test_begin(-tests => 740);
8 use_ok('DBTestHarness');
9 use_ok('Bio::OntologyIO');
12 $biosql = DBTestHarness->new("biosql");
15 $db = $biosql->get_DBAdaptor();
18 my $ontio = Bio::OntologyIO->new(-file => test_input_file('sofa.ontology'),
21 my $ont = $ontio->next_ontology();
23 $ont->name("My Test Ontology"); # avoid clashes
25 # insert by inserting all relationships (there are no isolated terms in SOFA)
26 foreach my $rel ($ont->get_relationships()) {
27 # change ID prefix to avoid clashes
28 foreach my $term_meth ("subject_term","object_term") {
29 my $id = $rel->$term_meth->identifier();
30 next if $id =~ /^MYTO/;
31 $rel->$term_meth->identifier("MYTO:".substr($id,3)) if $id;
33 # make persistent and insert
34 my $prel = $db->create_persistent($rel);
36 ok ($prel->primary_key());
39 # now get the ontology back from the database
40 my $dbont = Bio::Ontology::Ontology->new(-name => "My Test Ontology");
41 $dbont = $db->get_object_adaptor($dbont)->find_by_unique_key($dbont);
43 ok ($dbont->primary_key());
45 # set up the query to get all relationships
47 Bio::DB::Query::BioQuery->new(
48 -datacollections => ["Bio::Ontology::OntologyI=>Bio::Ontology::RelationshipI"],
49 -where => ["Bio::Ontology::OntologyI::primary_key = ".
53 my $reladp = $db->get_object_adaptor("Bio::Ontology::RelationshipI");
54 my $qres = $reladp->find_by_query($queryrels);
55 while(my $rel = $qres->next_object()) {
56 is ($rel->ontology->name, "My Test Ontology");
57 $dbont->add_term($rel->subject_term);
58 $dbont->add_term($rel->object_term);
59 #$dbont->add_term($rel->predicate_term);
60 $dbont->add_relationship($rel);
63 # now query the ontology
64 my ($term) = $dbont->find_terms(-identifier => "MYTO:0000233");
66 is ($term->identifier, "MYTO:0000233");
67 is ($term->name, "processed_transcript");
68 @rels = $dbont->get_relationships($term);
69 is (scalar(@rels), 5);
70 @relset = grep { $_->predicate_term->name eq "IS_A"; } @rels;
71 is (scalar(@relset), 3);
72 @relset = grep { $_->object_term->identifier eq "MYTO:0000233"; } @rels;
73 is (scalar(@relset), 4);
75 # check for correct storage and retrieval of synonyms and dbxrefs
76 ($term) = $dbont->find_terms(-identifier => "MYTO:0000203");
78 is ($term->name, "untranslated_region");
79 my @syns = $term->get_synonyms();
80 is (scalar(@syns), 1);
82 # modify, update, and re-retrieve to check with multiple synonyms, and with
83 # dbxrefs (this version of SOFA doesn't come with any dbxrefs)
84 $term->add_synonym("junk DNA");
85 $term->add_dbxref(-dbxrefs => [Bio::Annotation::DBLink->new(-database => "MYDB",
86 -primary_id => "yaddayadda")]);
88 $term = $term->adaptor->find_by_primary_key($term->primary_key);
91 @syns = $term->get_synonyms();
92 is (scalar(@syns), 2);
93 is (scalar(grep { $_ eq "junk DNA"; } @syns), 1);
94 is (scalar($term->get_dbxrefs()), 1);
97 # test the transitive closure computations
99 my $ontadp = $db->get_object_adaptor("Bio::Ontology::OntologyI");
100 my $ontname = "My BioSQL Predicate Ontology";
101 my $id_pred = Bio::Ontology::Term->new(-name => "identity",
102 -ontology => $ontname);
103 my $superpred = Bio::Ontology::Term->new(-name => "PART_OF",
104 -ontology => $ontname);
105 my $subcl_pred = Bio::Ontology::Term->new(-name => "implies",
106 -ontology => $ontname);
108 ok ($ontadp->compute_transitive_closure($ont,
110 -predicate_superclass => $superpred,
111 -subclass_predicate => $subcl_pred,
112 -identity_predicate => $id_pred));
114 # now query and test the results
115 # set up the query to get all relationships
116 $query = Bio::DB::Query::BioQuery->new(
117 -datacollections => ["Bio::Ontology::OntologyI=>Bio::Ontology::PathI o",
118 "Bio::Ontology::TermI=>Bio::Ontology::PathI tsubj::subject",
119 "Bio::Ontology::TermI=>Bio::Ontology::PathI tobj::object",
121 -where => ["o.name = 'My Test Ontology'",
122 "tobj.name = 'gene'",
123 "tsubj.name = 'exon'"]
125 my $pathadp = $db->get_object_adaptor("Bio::Ontology::PathI");
126 $qres = $pathadp->find_by_query($query);
128 while(my $path = $qres->next_object()) {
129 is ($path->ontology->name, "My Test Ontology");
130 is ($path->subject_term->name, "exon");
131 is ($path->object_term->name, "gene");
132 is ($path->predicate_term->name, "PART_OF");
137 # test for distance zero paths
138 $query = Bio::DB::Query::BioQuery->new(
139 -datacollections => [
140 "Bio::Ontology::OntologyI=>Bio::Ontology::PathI o",
141 "Bio::Ontology::TermI=>Bio::Ontology::PathI tpred::predicate",
143 -where => ["o.name = 'My Test Ontology'",
144 "tpred.name = 'identity'"]
146 $qres = $pathadp->find_by_query($query);
148 while(my $path = $qres->next_object()) {
149 is ($path->ontology->name, "My Test Ontology");
150 is ($path->subject_term->name, $path->object_term->name);
151 is ($path->predicate_term->name, "identity");
152 is ($path->predicate_term->ontology->name, "My BioSQL Predicate Ontology");
153 is ($path->distance, 0);
154 $n++ if $path->subject_term->identifier; # don't count hard-coded but
155 # unused relationship types
160 # test removal of relationships
162 $ont = Bio::Ontology::Ontology->new(-name => "My Test Ontology");
163 ok ($reladp->remove_all_relationships($ont));
165 # try to find any relationships
166 $reladp = $db->get_object_adaptor("Bio::Ontology::RelationshipI");
167 $qres = $reladp->find_by_query($queryrels);
170 while ($qres->next_object()) {
175 # there should still be terms though
176 my $dbterm = Bio::Ontology::Term->new(-identifier => "MYTO:0000233");
177 $dbterm = $db->get_object_adaptor($dbterm)->find_by_unique_key($dbterm);
179 ok ($dbterm->primary_key);
180 ok ($dbterm->ontology);
181 is ($dbterm->ontology->name, "My Test Ontology");
182 is ($dbterm->identifier, "MYTO:0000233");
183 ($term) = $dbont->find_terms(-identifier => "MYTO:0000233");
185 is ($dbterm->name, $term->name);
186 is (scalar($dbterm->get_synonyms()), scalar($term->get_synonyms()));