squash perl 5.12 warning
[bioperl-db.git] / t / 12ontology.t
blob81186fdc1a6285ae7ffadef003cc23aa9a34cc23
1 # -*-Perl-*-
2 # $Id$
4 BEGIN {
5     use lib 't';
6     use Bio::Root::Test;
7     test_begin(-tests => 740);
8         use_ok('DBTestHarness');
9         use_ok('Bio::OntologyIO');
12 $biosql = DBTestHarness->new("biosql");
13 ok $biosql;
15 $db = $biosql->get_DBAdaptor();
16 ok $db;
18 my $ontio = Bio::OntologyIO->new(-file => test_input_file('sofa.ontology'),
19                                  -format => 'soflat');
20 ok ($ontio);
21 my $ont = $ontio->next_ontology();
22 ok ($ont);
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;
32     }
33     # make persistent and insert
34     my $prel = $db->create_persistent($rel);
35     ok ($prel->create());
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);
42 ok ($dbont);
43 ok ($dbont->primary_key());
45 # set up the query to get all relationships
46 my $queryrels = 
47     Bio::DB::Query::BioQuery->new(
48        -datacollections => ["Bio::Ontology::OntologyI=>Bio::Ontology::RelationshipI"],
49        -where => ["Bio::Ontology::OntologyI::primary_key = ".
50                   $dbont->primary_key()
51                   ]
52                                   );
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");
65 ok ($term);
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");
77 ok ($term);
78 is ($term->name, "untranslated_region");
79 my @syns = $term->get_synonyms();
80 is (scalar(@syns), 1);
81 is ($syns[0], "UTR");
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")]);
87 ok ($term->store());
88 $term = $term->adaptor->find_by_primary_key($term->primary_key);
89 ok ($term);
90 # now test
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,
109                                         -truncate => 1,
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",
120                          ],
121                -where => ["o.name = 'My Test Ontology'",
122                           "tobj.name = 'gene'",
123                           "tsubj.name = 'exon'"]
124                                        );
125 my $pathadp = $db->get_object_adaptor("Bio::Ontology::PathI");
126 $qres = $pathadp->find_by_query($query);
127 my $n = 0;
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");
133     $n++;
135 is ($n, 1);
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",
142                          ],
143                -where => ["o.name = 'My Test Ontology'",
144                           "tpred.name = 'identity'"]
145                                        );
146 $qres = $pathadp->find_by_query($query);
147 $n = 0;
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
157 is ($n, 86);
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);
168 ok ($qres);
169 $n = 0;
170 while ($qres->next_object()) {
171     $n++;
173 is ($n, 0);
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);
178 ok ($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");
184 ok ($term);
185 is ($dbterm->name, $term->name);
186 is (scalar($dbterm->get_synonyms()), scalar($term->get_synonyms()));