1 # -*-Perl-*- Test Harness script for Bioperl
14 -requires_module => 'Graph::Directed'
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();
30 isa_ok( $eng, "Bio::Ontology::OntologyEngineI" );
31 is( $ont->engine, $eng );
35 -identifier => "IPR000001",
37 -definition => "Kringles are autonomous structural domains ...",
41 -identifier => "IPR000002",
42 -name => "Cdc20/Fizzy",
43 -definition => "The Cdc20/Fizzy region is almost always ...",
47 -identifier => "IPR000003",
48 -name => "Retinoid X receptor",
49 -definition => "Steroid or nuclear hormone receptors ...",
53 -identifier => "IPR000004",
55 -definition => "Test4 definition ...",
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 );
70 -object_term => $terms[0],
71 -subject_term => $terms[1],
72 -predicate_term => $rel_type,
76 -object_term => $terms[1],
77 -subject_term => $terms[2],
78 -predicate_term => $rel_type,
82 -object_term => $terms[0],
83 -subject_term => $terms[3],
84 -predicate_term => $rel_type,
88 -object_term => $terms[3],
89 -subject_term => $terms[2],
90 -predicate_term => $rel_type,
96 -object_term => undef,
97 -subject_term => $terms[2],
98 -predicate_term => $rel_type,
102 -object_term => $terms[1],
103 -subject_term => undef,
104 -predicate_term => $rel_type,
108 -object_term => $terms[1],
109 -subject_term => $terms[2],
110 -predicate_term => $rel_type,
115 $bad_rels[0] = Bio::Ontology::Relationship->new( @{ $bad_rels[0] } );
117 throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) },
118 'Bio::Root::Exception',
119 'adding a relationship with an undef object term fails');
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] } );
128 throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
129 'Bio::Root::Exception',
130 'adding a relationship with an undef subject term fails');
132 throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
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] );
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] );
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 );
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] );
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();