small pod fix
[bioperl-live.git] / t / obo_parser.t
blob4c317c4c7f9d3dd6bfffdf081973c9e11c0da3d4
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib 't/lib';
8     use BioperlTest;
9     
10     test_begin(-tests => 83,
11                            -requires_module => 'Graph');
12         
13         use_ok('Bio::OntologyIO');
14         use_ok('Bio::Ontology::RelationshipType');
17 my $IS_A    = Bio::Ontology::RelationshipType->get_instance( "IS_A" );
18 my $PART_OF = Bio::Ontology::RelationshipType->get_instance( "PART_OF" );
20 my $parser = Bio::OntologyIO->new(
21                       -format    => "obo",
22                       -file      => test_input_file('so.obo'));
24 my $ont = $parser->next_ontology();
25 ok ($ont);
26 is ($ont->name(), "sequence");
28 my @roots = $ont->get_root_terms();
29 is (scalar(@roots), 1);
30 is ($roots[0]->name(), "Sequence_Ontology");
31 is ($roots[0]->identifier(), "SO:0000000");
33 my @terms = $ont->get_child_terms($roots[0]);
34 is (scalar(@terms), 5);
35 my ($term) = grep { $_->name() eq "variation_operation"; } @terms;
36 ok $term;
37 ($term) = grep { $_->name() eq "sequence_attribute"; } @terms;
38 ok $term;
39 ($term) = grep { $_->name() eq "consequences_of_mutation"; } @terms;
40 ok $term;
41 ($term) = grep { $_->name() eq "chromosome_variation"; } @terms;
42 ok $term;
43 ($term) = grep { $_->name() eq "located_sequence_feature"; } @terms;
44 ok $term;
46 @terms = $ont->get_child_terms($terms[0]);
47 is (scalar(@terms), 5);
48 ($term) = grep { $_->name() eq "translocate"; } @terms;
49 ok $term;
50 ($term) = grep { $_->name() eq "delete"; } @terms;
51 ok $term;
52 ($term) = grep { $_->name() eq "insert"; } @terms;
53 ok $term;
54 ($term) = grep { $_->name() eq "substitute"; } @terms;
55 ok $term;
56 ($term) = grep { $_->name() eq "invert"; } @terms;
57 ok $term;
59 my $featterm = $terms[0];
60 @terms = $ont->get_child_terms($featterm);
61 is (scalar(@terms), 2);
63 # substitution has two parents, see whether this is handled
64 @terms = $ont->find_terms(-name => "substitution");
65 $term =  $terms[0];
66 is ($term->name(), "substitution");
68 # search using obo terms;
69 @terms = $ont->find_identically_named_terms($term);
70 is scalar @terms, 1;
71 @terms = $ont->find_identical_terms($term);
72 is scalar @terms, 1;
73 @terms = $ont->find_similar_terms($term);
74 is scalar @terms, 7;
76 @terms = $ont->get_ancestor_terms($term);
77 is (scalar(@terms), 6);
78 is (scalar(grep { $_->name() eq "region"; } @terms), 1);
79 is (scalar(grep { $_->name() eq "sequence_variant"; } @terms), 1);
81 # processed_transcript has part-of and is-a children
83 @terms = $ont->find_terms(-name => "processed_transcript");;
84 $term = $terms[0];
86 @terms = $ont->get_child_terms($term);
87 is (scalar(@terms), 5);
88 @terms = $ont->get_child_terms($term, $PART_OF);
89 is (scalar(@terms), 2);
90 @terms = $ont->get_child_terms($term, $IS_A);
91 is (scalar(@terms), 3);
92 @terms = $ont->get_child_terms($term, $PART_OF, $IS_A);
93 is (scalar(@terms), 5);
95 # TF_binding_site has 2 parents and different relationships in the two
96 # paths up (although the relationships to its two parents are of the
97 # same type, namely is-a)
98 @terms = $ont->find_terms(-name => "TF_binding_site");;
99 $term = $terms[0];
101 @terms = $ont->get_parent_terms($term);
102 is (scalar(@terms), 2);
103 my ($pterm) = grep { $_->name eq "regulatory_region"; } @terms;
104 ok $pterm;
105 @terms = $ont->get_parent_terms($term, $PART_OF);
106 is (scalar(@terms), 0);
107 @terms = $ont->get_parent_terms($term, $IS_A);
108 is (scalar(@terms), 2);
109 @terms = $ont->get_parent_terms($term, $PART_OF, $IS_A);
110 is (scalar(@terms), 2);
113 # pull out all relationships
114 my @rels = $ont->get_relationships();
115 my @relset = grep { $_->object_term->name eq "Sequence_Ontology"; } @rels;
116 is (scalar(@relset), 5);
117 @relset = grep { $_->subject_term->name eq "Sequence_Ontology"; } @rels;
118 is (scalar(@relset), 0);
120 # relationships for a specific term only
121 ($term) = $ont->find_terms(-identifier => "SO:0000082");
122 ok ($term);
123 is ($term->identifier, "SO:0000082");
124 is ($term->name, "processed_transcript_attribute");
125 @rels = $ont->get_relationships($term);
126 is (scalar(@rels), 5);
127 @relset = grep { $_->predicate_term->name eq "IS_A"; } @rels;
128 is (scalar(@relset), 5);
129 @relset = grep { $_->object_term->identifier eq "SO:0000082"; } @rels;
130 is (scalar(@relset), 4);
131 @relset = grep { $_->subject_term->identifier eq "SO:0000082"; } @rels;
132 is (scalar(@relset), 1);
137 #### --- testing obo parsers for regulates relationships
138 my $parser2 = Bio::OntologyIO->new (
139          -format => 'obo',
140          -file => test_input_file('regulation_test.obo'));
142 isa_ok($parser2,'Bio::OntologyIO', 'got a ontology IO handler');
144 my @ontologies;
145 while (my $ont = $parser2->next_ontology()) {
146          isa_ok($ont,'Bio::Ontology::Ontology','got ontology parser2');
147          isa_ok($ont->engine,'Bio::Ontology::OBOEngine','got OBO engine object');
148          push @ontologies,$ont;
151 my $molont = $ontologies[1];
152 my $bioont = $ontologies[2];
153 is($ontologies[0]->name(),'gene_ontology','Gene ontology');
154 is($bioont->name(),'biological_process','biological process');
155 is($molont->name(),'molecular_function','molecular function');
157 my ($broot) = $bioont->get_root_terms();
158 is($broot->name(),'biological_process','Got root');
161 my ($mroot) = $molont->get_root_terms();
162 is($mroot->name(),'molecular_function','Got root');
165 ## -- testing newly introduced relationships
166 is($ontologies[0]->get_relationship_type('REGULATES')->name,'REGULATES','Got regulates
167          from gene_ontology');
168 is($ontologies[0]->get_relationship_type('POSITIVELY_REGULATES')->name,'POSITIVELY_REGULATES','Got
169          positively regulates from gene_ontology');
170 is($bioont->get_relationship_type('REGULATES')->name,'REGULATES','Got
171           regulates from biological_process');
172 is($bioont->get_relationship_type('POSITIVELY_REGULATES')->name,'POSITIVELY_REGULATES','Got
173          positively regulates from biological_process');
176 ## -- getting relationships for various ontologies
177 my @onto_pred = $ontologies[0]->get_predicate_terms();
178 my @bio_pred =  $bioont->get_predicate_terms();
179 is(scalar @onto_pred,6,'Got predicates for gene_ontology');
180 is(scalar @bio_pred,2,'Got predicates for biological_process');
181 is($onto_pred[3]->name(),'REGULATES','Got regulates predicate');
182 is($bio_pred[1]->name(),'POSITIVELY_REGULATES','Got positively regulates predicate');
185 my @bio_rel = $bioont->get_relationships();
186 my @mol_rel = $molont->get_relationships();
187 is(scalar @bio_rel,11,'Got relationships for biological_process');
188 is(scalar @mol_rel,2,'Got relationships for molecular_function');
189 is($mol_rel[0]->predicate_term->name(),'IS_A','Got is a relationship from
190          molecular_function');
191 ## ----
194 ## -- testing the regulates relationships between term1s
195 my $REG = Bio::Ontology::RelationshipType->get_instance('REGULATES');
196 my $PREG = Bio::Ontology::RelationshipType->get_instance('POSITIVELY_REGULATES');
198 my ($term1) = $bioont->find_terms(-identifier => 'GO:0050790');
199 isa_ok($term1,'Bio::Ontology::Term','Got term object');
200 is($term1->identifier(),'GO:0050790', 'Got term id');
201 is($term1->name(),'regulation of catalytic activity', 'Got term name');
203 my ($parent) = $bioont->get_parent_terms($term1,$REG);
204 isa_ok($parent,'Bio::Ontology::Term','Got regulated object');
205 is($parent->identifier(),'GO:0003824','Got regulated term1 id'); 
207 ## -- now testing the other way
208 my ($child) = $bioont->get_child_terms($parent,$REG); 
209 isa_ok($child,'Bio::Ontology::Term','Got term1 object');
210 is($child->identifier(),$term1->identifier(),'Got back the child');
212 my ($term2) = $bioont->find_terms(-identifier => 'GO:0043085');
213 isa_ok($term2,'Bio::Ontology::Term','Got term object');
214 is($term2->identifier(),'GO:0043085', 'Got term id');
215 is($term2->name(),'positive regulation of catalytic activity', 'Got term name');
217 my ($parent2) = $bioont->get_parent_terms($term2,$PREG);
218 isa_ok($parent2,'Bio::Ontology::Term','Got regulated object');
219 is($parent2->identifier(),'GO:0003824','Got regulated term1 id'); 
220 is($parent->name(),$parent2->name(),'Got identical regulation');
223 my ($child2) = $bioont->get_child_terms($parent2,$PREG); 
224 isa_ok($child2,'Bio::Ontology::Term','Got term1 object');
225 is($child2->identifier(),$term2->identifier(),'Got back the child');