skip tests if the cloning class used happens to be Storable, see bug #3447
[bioperl-live.git] / t / Ontology / OntologyEngine.t
blob426982ccd286c321ab398fd242e42574f2835723
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 my $ERROR_CLASS;
8 BEGIN {
9     use lib '.';
10     use Bio::Root::Test;
12     test_begin(
13         -tests           => 31,
14         -requires_module => 'Graph::Directed'
15     );
16     
17     $ERROR_CLASS = eval "require Error; 1" ? 1 : 0;
19     use_ok('Bio::Ontology::Term');
20     use_ok('Bio::Ontology::Relationship');
21     use_ok('Bio::Ontology::RelationshipType');
22     use_ok('Bio::Ontology::SimpleOntologyEngine');
23     use_ok('Bio::Ontology::Ontology');
26 my $ont = Bio::Ontology::Ontology->new( -name => "My Ontology" );
28 my $eng = Bio::Ontology::SimpleOntologyEngine->new();
29 $ont->engine($eng);
30 isa_ok( $eng, "Bio::Ontology::OntologyEngineI" );
31 is( $ont->engine, $eng );
33 my @terms = (
34     [
35         -identifier => "IPR000001",
36         -name       => "Kringle",
37         -definition => "Kringles are autonomous structural domains ...",
38         -ontology   => $ont
39     ],
40     [
41         -identifier => "IPR000002",
42         -name       => "Cdc20/Fizzy",
43         -definition => "The Cdc20/Fizzy region is almost always ...",
44         -ontology   => $ont
45     ],
46     [
47         -identifier => "IPR000003",
48         -name       => "Retinoid X receptor",
49         -definition => "Steroid or nuclear hormone receptors ...",
50         -ontology   => $ont
51     ],
52     [
53         -identifier => "IPR000004",
54         -name       => "Test4",
55         -definition => "Test4 definition ...",
56         -ontology   => $ont
57     ],
60 for ( my $i = 0 ; $i < @terms ; $i++ ) {
61     $terms[$i] = Bio::Ontology::Term->new( @{ $terms[$i] } );
62     $ont->add_term( $terms[$i] );
65 my $rel_type  = Bio::Ontology::RelationshipType->get_instance( "IS_A",    $ont );
66 my $rel_type1 = Bio::Ontology::RelationshipType->get_instance( "PART_OF", $ont );
68 my @rels = (
69     [
70         -object_term    => $terms[0],
71         -subject_term   => $terms[1],
72         -predicate_term => $rel_type,
73         -ontology       => $ont,
74     ],
75     [
76         -object_term    => $terms[1],
77         -subject_term   => $terms[2],
78         -predicate_term => $rel_type,
79         -ontology       => $ont,
80     ],
81     [
82         -object_term    => $terms[0],
83         -subject_term   => $terms[3],
84         -predicate_term => $rel_type,
85         -ontology       => $ont,
86     ],
87     [
88         -object_term    => $terms[3],
89         -subject_term   => $terms[2],
90         -predicate_term => $rel_type,
91         -ontology       => $ont,
92     ],
94 my @bad_rels = (
95     [
96         -object_term    => undef,
97         -subject_term   => $terms[2],
98         -predicate_term => $rel_type,
99         -ontology       => $ont,
100     ],
101     [
102         -object_term    => $terms[1],
103         -subject_term   => undef,
104         -predicate_term => $rel_type,
105         -ontology       => $ont,
106     ],
107     [
108         -object_term    => $terms[1],
109         -subject_term   => $terms[2],
110         -predicate_term => $rel_type,
111         -ontology       => $ont,
112     ],
115 $bad_rels[0] = Bio::Ontology::Relationship->new( @{ $bad_rels[0] } );
116 if ($ERROR_CLASS) {
117     throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) }, 
118     'Bio::Root::Exception',
119     'adding a relationship with an undef object term fails');
120 } else {
121     throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) }, qr/Exception/,
122               'adding a relationship with an undef object term fails');
124 throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) }, qr/MSG: cannot add relationship, relationship has no object_term/, 'adding a relationship with an undef object term fails');
126 $bad_rels[1] = Bio::Ontology::Relationship->new( @{ $bad_rels[1] } );
127 if ($ERROR_CLASS) {
128     throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
129     'Bio::Root::Exception',
130     'adding a relationship with an undef subject term fails');
131 } else {
132     throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
133     qr/Exception/,
134     'adding a relationship with an undef subject term fails');
136 throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) }, qr/MSG: cannot add relationship, relationship has no subject_term/, 'adding a relationship with an undef subject term fails');
138 for ( my $i = 0 ; $i < @rels ; $i++ ) {
139     $rels[$i] = Bio::Ontology::Relationship->new( @{ $rels[$i] } );
140     $ont->add_relationship( $rels[$i] );
143 my @child_terms =
144     sort { $a->identifier() cmp $b->identifier(); } $ont->get_child_terms( $terms[0] );
145 is( scalar(@child_terms), 2 );
146 is( $child_terms[0],      $terms[1] );
147 my @child_terms1 =
148     sort { $a->identifier() cmp $b->identifier(); } $ont->get_child_terms( $terms[0], $rel_type );
149 is( scalar(@child_terms), 2 );
150 is( $child_terms1[0],     $terms[1] );
151 is( scalar( $ont->get_child_terms( $terms[0], $rel_type1 ) ), 0 );
153 my @descendant_terms =
154     sort { $a->identifier() cmp $b->identifier(); } $ont->get_descendant_terms( $terms[0] );
155 is( scalar(@descendant_terms), 3 );
156 is( $descendant_terms[1],      $terms[2] );
158 my @descendant_terms1 =
159     sort { $a->identifier() cmp $b->identifier(); }
160     $ont->get_descendant_terms( $terms[0], $rel_type );
161 is( $descendant_terms1[1],      $terms[2] );
162 is( scalar(@descendant_terms1), 3 );
163 is( scalar( $ont->get_descendant_terms( $terms[0], $rel_type1 ) ), 0 );
165 my @parent_terms =
166     sort { $a->identifier() cmp $b->identifier(); } $ont->get_parent_terms( $terms[1] );
167 is( scalar(@parent_terms), 1 );
168 is( $parent_terms[0],      $terms[0] );
170 my @ancestor_terms =
171     sort { $a->identifier() cmp $b->identifier(); } $ont->get_ancestor_terms( $terms[2] );
172 is( $ancestor_terms[0],      $terms[0] );
173 is( scalar(@ancestor_terms), 3 );
174 is( scalar( $ont->get_ancestor_terms( $terms[2], $rel_type ) ),  3 );
175 is( scalar( $ont->get_ancestor_terms( $terms[2], $rel_type1 ) ), 0 );
177 my @leaf_terms = $ont->get_leaf_terms();
179 # print scalar(@leaf_terms)."\n";
180 is( scalar(@leaf_terms), 1 );
181 is( $leaf_terms[0],      $terms[2] );
183 my @root_terms = $ont->get_root_terms();
185 # print scalar(@root_terms)."\n";
186 is( scalar(@root_terms), 1 );
187 is( $root_terms[0],      $terms[0] );
189 #print $ont->engine->to_string();