1 # -*-Perl-*- Test Harness script for Bioperl
13 -requires_module => 'Graph::Directed'
16 $ERROR_CLASS = eval "require Error; 1" ? 1 : 0;
18 use_ok('Bio::Ontology::Term');
19 use_ok('Bio::Ontology::Relationship');
20 use_ok('Bio::Ontology::RelationshipType');
21 use_ok('Bio::Ontology::SimpleOntologyEngine');
22 use_ok('Bio::Ontology::Ontology');
25 my $ont = Bio::Ontology::Ontology->new( -name => "My Ontology" );
27 my $eng = Bio::Ontology::SimpleOntologyEngine->new();
29 isa_ok( $eng, "Bio::Ontology::OntologyEngineI" );
30 is( $ont->engine, $eng );
34 -identifier => "IPR000001",
36 -definition => "Kringles are autonomous structural domains ...",
40 -identifier => "IPR000002",
41 -name => "Cdc20/Fizzy",
42 -definition => "The Cdc20/Fizzy region is almost always ...",
46 -identifier => "IPR000003",
47 -name => "Retinoid X receptor",
48 -definition => "Steroid or nuclear hormone receptors ...",
52 -identifier => "IPR000004",
54 -definition => "Test4 definition ...",
59 for ( my $i = 0 ; $i < @terms ; $i++ ) {
60 $terms[$i] = Bio::Ontology::Term->new( @{ $terms[$i] } );
61 $ont->add_term( $terms[$i] );
64 my $rel_type = Bio::Ontology::RelationshipType->get_instance( "IS_A", $ont );
65 my $rel_type1 = Bio::Ontology::RelationshipType->get_instance( "PART_OF", $ont );
69 -object_term => $terms[0],
70 -subject_term => $terms[1],
71 -predicate_term => $rel_type,
75 -object_term => $terms[1],
76 -subject_term => $terms[2],
77 -predicate_term => $rel_type,
81 -object_term => $terms[0],
82 -subject_term => $terms[3],
83 -predicate_term => $rel_type,
87 -object_term => $terms[3],
88 -subject_term => $terms[2],
89 -predicate_term => $rel_type,
95 -object_term => undef,
96 -subject_term => $terms[2],
97 -predicate_term => $rel_type,
101 -object_term => $terms[1],
102 -subject_term => undef,
103 -predicate_term => $rel_type,
107 -object_term => $terms[1],
108 -subject_term => $terms[2],
109 -predicate_term => $rel_type,
114 $bad_rels[0] = Bio::Ontology::Relationship->new( @{ $bad_rels[0] } );
116 throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) },
117 'Bio::Root::Exception',
118 'adding a relationship with an undef object term fails');
120 throws_ok( sub { $ont->add_relationship( $bad_rels[0] ) }, qr/Exception/,
121 'adding a relationship with an undef object term fails');
123 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');
125 $bad_rels[1] = Bio::Ontology::Relationship->new( @{ $bad_rels[1] } );
127 throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
128 'Bio::Root::Exception',
129 'adding a relationship with an undef subject term fails');
131 throws_ok( sub { $ont->add_relationship( $bad_rels[1] ) },
133 'adding a relationship with an undef subject term fails');
135 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');
137 for ( my $i = 0 ; $i < @rels ; $i++ ) {
138 $rels[$i] = Bio::Ontology::Relationship->new( @{ $rels[$i] } );
139 $ont->add_relationship( $rels[$i] );
143 sort { $a->identifier() cmp $b->identifier(); } $ont->get_child_terms( $terms[0] );
144 is( scalar(@child_terms), 2 );
145 is( $child_terms[0], $terms[1] );
147 sort { $a->identifier() cmp $b->identifier(); } $ont->get_child_terms( $terms[0], $rel_type );
148 is( scalar(@child_terms), 2 );
149 is( $child_terms1[0], $terms[1] );
150 is( scalar( $ont->get_child_terms( $terms[0], $rel_type1 ) ), 0 );
152 my @descendant_terms =
153 sort { $a->identifier() cmp $b->identifier(); } $ont->get_descendant_terms( $terms[0] );
154 is( scalar(@descendant_terms), 3 );
155 is( $descendant_terms[1], $terms[2] );
157 my @descendant_terms1 =
158 sort { $a->identifier() cmp $b->identifier(); }
159 $ont->get_descendant_terms( $terms[0], $rel_type );
160 is( $descendant_terms1[1], $terms[2] );
161 is( scalar(@descendant_terms1), 3 );
162 is( scalar( $ont->get_descendant_terms( $terms[0], $rel_type1 ) ), 0 );
165 sort { $a->identifier() cmp $b->identifier(); } $ont->get_parent_terms( $terms[1] );
166 is( scalar(@parent_terms), 1 );
167 is( $parent_terms[0], $terms[0] );
170 sort { $a->identifier() cmp $b->identifier(); } $ont->get_ancestor_terms( $terms[2] );
171 is( $ancestor_terms[0], $terms[0] );
172 is( scalar(@ancestor_terms), 3 );
173 is( scalar( $ont->get_ancestor_terms( $terms[2], $rel_type ) ), 3 );
174 is( scalar( $ont->get_ancestor_terms( $terms[2], $rel_type1 ) ), 0 );
176 my @leaf_terms = $ont->get_leaf_terms();
178 # print scalar(@leaf_terms)."\n";
179 is( scalar(@leaf_terms), 1 );
180 is( $leaf_terms[0], $terms[2] );
182 my @root_terms = $ont->get_root_terms();
184 # print scalar(@root_terms)."\n";
185 is( scalar(@root_terms), 1 );
186 is( $root_terms[0], $terms[0] );
188 #print $ont->engine->to_string();