test commits after fsfs transfer
[bioperl-live.git] / t / simpleGOparser.t
blob367dd996d7864d68cef8ed1131c7f7c38cf80e0d
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 => 102,
11                            -requires_module => 'Graph');
12         
13         use_ok('Bio::OntologyIO');
16 my $parser = Bio::OntologyIO->new(
17                       -format    => "go",
18                       -defs_file => test_input_file('GO.defs.test'),
19                       # test using -file
20                       -file      => test_input_file('component.ontology.test'));
23 my $IS_A    = Bio::Ontology::RelationshipType->get_instance( "IS_A" );
24 my $PART_OF = Bio::Ontology::RelationshipType->get_instance( "PART_OF" );
27 my @onts = ();
28 while(my $ont = $parser->next_ontology()) {
29     push(@onts, $ont);
31 is (scalar(@onts), 1);
32 my $ont = $onts[0];
33 isa_ok $ont, "Bio::Ontology::OntologyI";
34 is ($ont->name(), "Gene Ontology");
36 my $engine = $ont->engine();
37 isa_ok $engine, "Bio::Ontology::OntologyEngineI";
39 my $term = $engine->get_terms( "GO:0018897" );
41 # note that all dblinks are now Bio::Annotation::DBLink instances and that all
42 # *dblink* methods related to Bio::Ontology::Term are deprecated; this is due to
43 # inconsistencies in those Bio::Ontology::Term methods. Use *dbxref* methods
44 # instead
46 my @dblinks = sort {$a->display_text cmp $b->display_text} ( $term->get_dbxrefs() );
47 my @synos = sort ( $term->get_synonyms() );
49 is( $dblinks[ 0 ]->display_text, "MetaCyc:PWY-681" );
50 is( $dblinks[ 1 ]->display_text, "UM-BBD_pathwayID:dbt" );
51 is( $synos[ 0 ], "murein sacculus" );
52 is( $synos[ 1 ], "peptidoglycan" );
53 is( $term->ontology()->name(), "Gene Ontology" );
54 is( $term->name(), "dibenzothiophene desulfurization" );
57 $term = $engine->get_terms( "GO:0004796" );
58 @dblinks = sort ( $term->get_dbxrefs() );
59 @synos = sort ( $term->get_synonyms() );
60 my @sec = sort ( $term->get_secondary_GO_ids() ); 
62 is( $dblinks[ 0 ]->display_text, "EC:5.3.99.5" );
63 is( $synos[ 0 ], "cytochrome P450 CYP5" );
64 is( $sec[ 0 ], "GO:0008400" );
65 is( $term->ontology()->name(), "Gene Ontology" );
66 is( $term->name(), "thromboxane-A synthase" );
68 my @parents = sort goid ( $ont->get_parent_terms( $term ) );
69 is( @parents, 2 );
70 is( $parents[ 0 ]->GO_id(), "GO:0015034" );
71 is( $parents[ 1 ]->GO_id(), "GO:0018895" );
74 @parents = sort goid ( $ont->get_parent_terms( $term, $PART_OF, $IS_A) );
76 is( @parents, 2 );
77 is( $parents[ 0 ]->GO_id(), "GO:0015034" );
78 is( $parents[ 1 ]->GO_id(), "GO:0018895" );
81 @parents = sort goid ( $ont->get_parent_terms( "GO:0004796", $IS_A ) );
82 is( @parents, 2 );
83 is( $parents[ 0 ]->GO_id(), "GO:0015034" );
84 is( $parents[ 1 ]->GO_id(), "GO:0018895" );
87 @parents = sort goid ( $ont->get_parent_terms( "GO:0004796", $PART_OF ) );
88 is( scalar(@parents), 0 );
89 my @anc = sort goid ( $ont->get_ancestor_terms( $term ) );
90 is( scalar(@anc), 3 );
91 is( $anc[ 0 ]->GO_id(), "GO:0003673" );
92 is( $anc[ 1 ]->GO_id(), "GO:0015034" );
93 is( $anc[ 2 ]->GO_id(), "GO:0018895" );
96 @anc = sort goid ( $ont->get_ancestor_terms( "GO:0004796", $IS_A ) );
97 is( scalar(@anc), 3 );
98 is( $anc[ 0 ]->GO_id(), "GO:0003673" );
99 is( $anc[ 1 ]->GO_id(), "GO:0015034" );
100 is( $anc[ 2 ]->GO_id(), "GO:0018895" );
103 @anc = sort goid ( $ont->get_ancestor_terms( "GO:0000666" ) );
104 is( @anc, 12 );
106 @anc = sort goid ( $ont->get_ancestor_terms( "GO:0000666", $IS_A ) );
107 is( @anc, 2 );
108 is( $anc[ 0 ]->GO_id(), "GO:0005811" );
109 is( $anc[ 1 ]->GO_id(), "GO:0030481" );
111 @anc = sort goid ( $ont->get_ancestor_terms( "GO:0000666", $PART_OF ) );
112 is( @anc, 6 );
113 is( $anc[ 0 ]->GO_id(), "GO:0005623" );
114 is( $anc[ 1 ]->GO_id(), "GO:0005625" );
115 is( $anc[ 2 ]->GO_id(), "GO:0005933" );
116 is( $anc[ 3 ]->GO_id(), "GO:0005935" );
117 is( $anc[ 4 ]->GO_id(), "GO:0005937" );
118 is( $anc[ 5 ]->GO_id(), "GO:0005938" );
121 my @childs = sort goid ( $ont->get_child_terms( "GO:0005625", $PART_OF ) );
122 is( @childs, 2 );
123 is( $childs[ 0 ]->GO_id(), "GO:0000666" );
124 is( $childs[ 0 ]->name(), "polarisomeX" );
125 is( $childs[ 1 ]->GO_id(), "GO:0000667" );
126 is( $childs[ 1 ]->name(), "polarisomeY" );
127 is( $childs[ 1 ]->ontology()->name(), "Gene Ontology" );
130 is( $engine->get_terms( "GO:0005625" )->name(), "soluble fraction" ); 
133 @childs = sort goid ( $ont->get_descendant_terms( "GO:0005624", $IS_A ) );
134 is( @childs, 6 );
135 is( $childs[ 0 ]->GO_id(), "GO:0000299" );
136 is( $childs[ 0 ]->name(), "integral membrane protein of membrane fraction" );
137 is( $childs[ 1 ]->GO_id(), "GO:0000300" );
138 is( $childs[ 1 ]->name(), "peripheral membrane protein of membrane fraction" );
139 is( $childs[ 2 ]->GO_id(), "GO:0005792" );
140 is( $childs[ 2 ]->name(), "microsome" );
141 is( $childs[ 3 ]->GO_id(), "GO:0019717" );
142 is( $childs[ 3 ]->name(), "synaptosome" );
143 is( $childs[ 4 ]->GO_id(), "GO:0019718" );
144 is( $childs[ 4 ]->name(), "rough microsome" );
145 is( $childs[ 5 ]->GO_id(), "GO:0019719" );
146 is( $childs[ 5 ]->name(), "smooth microsome" );
148 @childs = sort goid ( $ont->get_descendant_terms( "GO:0005625", $IS_A ) );
149 is( @childs, 0 );
152 @childs = sort goid ( $ont->get_descendant_terms( "GO:0005625", $PART_OF ) );
153 is( @childs, 2 );
155 my @rels = sort child_goid ( $ont->get_relationships( "GO:0005625" ) );
156 is( @rels, 3 );
157 is( $rels[ 0 ]->object_term()->GO_id(), "GO:0005625" );
158 is( $rels[ 0 ]->subject_term()->GO_id(), "GO:0000666" );
159 ok( $rels[ 0 ]->predicate_term()->equals( $PART_OF ) );
161 is( $rels[ 1 ]->object_term()->GO_id(), "GO:0005625" );
162 is( $rels[ 1 ]->subject_term()->GO_id(), "GO:0000667" );
163 ok( $rels[ 1 ]->predicate_term()->equals( $PART_OF ) );
165 is( $rels[ 2 ]->object_term()->GO_id(), "GO:0000267" );
166 is( $rels[ 2 ]->subject_term()->GO_id(), "GO:0005625" );
167 ok( $rels[ 2 ]->predicate_term()->equals( $IS_A ) );
169 # dbxrefs and synonyms are candidates for being falsely picked up by
170 # overly promiscuous regular expressions as related terms, so we test for
171 # that here
172 my @terms = $engine->get_terms( "EC:5.3.99.5" );
173 is (scalar(@terms), 0);
174 @terms = $engine->get_terms("MetaCyc:PWY-681","MetaCyc:PWY");
175 is (scalar(@terms), 0);
176 @terms = $engine->get_terms("UM-BBD_pathwayID:dbt","BBD_pathwayID:dbt",
177                             "UM-BBD_pathwayID:dbt2","BBD_pathwayID:dbt2");
178 is (scalar(@terms), 0);
181 ok( $engine->graph() );
183 ok( $ont->add_term( Bio::Ontology::GOterm->new(-identifier => "GO:0000000")));
185 ok( $engine->has_term( "GO:0000300" ) );
187 is( scalar $ont->get_all_terms(), 44 );
188 is( scalar $ont->get_relationship_types(), 3 );
190 ok( ! $ont->add_relationship( $rels[ 2 ] ) ); # this edge already exists, cannot add
192 $rels[ 2 ]->subject_term()->GO_id( "GO:0005938" );
193 ok( $ont->add_relationship( $rels[ 2 ] ) ); # now it's changed, can add
196 my @roots = $ont->get_root_terms();
197 is( scalar(@roots), 10 );
199 my @leafs = $ont->get_leaf_terms();
200 is( scalar(@leafs), 19 );
204 $parser = Bio::OntologyIO->new(
205                       -format    => "go",
206                       -defs_file => test_input_file('GO.defs.test2'),
207                       # test using -files
208                       -files     => test_input_file('component.ontology.test2'));
210 $ont = $parser->next_ontology();
211 ok ($ont);
213 @roots = $ont->get_root_terms();
214 is( scalar(@roots), 1 );
216 @leafs = $ont->get_leaf_terms();
217 is( scalar(@leafs), 4 );
219 $parser = Bio::OntologyIO->new(
220                       -format    => "go",
221                       -file      => test_input_file('mpath.ontology.test'));
223 ok($parser);
224 $ont = $parser->next_ontology;
225 ok($ont);
226 $engine = $ont->engine;
227 ok($engine);
228 $term = $engine->get_terms( "MPATH:30" );
229 is($term->identifier,"MPATH:30");
230 is($term->name,"cystic medial necrosis");
231 is($term->definition,undef);
232 is((sort $term->get_synonyms)[0],"erdheim disease");
233 is($ont->get_parent_terms( $term )->name,"tissue specific degenerative process");
234 is(scalar($ont->get_root_terms()),2);
235 @anc = $ont->get_ancestor_terms($term);
236 is(scalar(@anc),4);
238 #################################################################
239 # helper functions
240 #################################################################
242 sub goid { num ( $a->GO_id() ) <=> num ( $b->GO_id() ) }
244 sub child_goid { num ( $a->child_term()->GO_id() ) <=> num ( $b->child_term()->GO_id() ) }
246 sub num {
247     my $x = shift( @_ );
248     $x =~ s/\D+//g;
249     return $x;