Add tests for memory leaks and weaken for Issue #81
[bioperl-live.git] / t / Annotation / Annotation.t
blob03ab11d9802e0a2946415b339649c842240544d3
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
10     test_begin(-tests           => 152,
11                -requires_module => 'Data::Stag');
13     use_ok('Bio::Annotation::Collection');
14     use_ok('Bio::Annotation::DBLink');
15     use_ok('Bio::Annotation::Comment');
16     use_ok('Bio::Annotation::Reference');
17     use_ok('Bio::Annotation::SimpleValue');
18     use_ok('Bio::Annotation::Target');
19     use_ok('Bio::Annotation::AnnotationFactory');
20     use_ok('Bio::Annotation::StructuredValue');
21     use_ok('Bio::Annotation::TagTree');
22     use_ok('Bio::Annotation::Tree');
25 my $DEBUG = test_debug();
27 #simple value
29 my $simple = Bio::Annotation::SimpleValue->new(-tagname => 'colour',
30                                                -value   => '1',
31                                               );
33 isa_ok($simple, 'Bio::AnnotationI');
34 is $simple->display_text, 1;
35 is $simple->value, 1;
36 is $simple->tagname, 'colour';
38 is $simple->value(0), 0;
39 is $simple->value, 0;
40 is $simple->display_text, 0;
42 # link
44 my $link1 = Bio::Annotation::DBLink->new(-database => 'TSC',
45                                          -primary_id => 'TSC0000030',
46                                         );
47 isa_ok($link1,'Bio::AnnotationI');
48 is $link1->database(), 'TSC';
49 is $link1->primary_id(), 'TSC0000030';
50 is $link1->as_text, 'Direct database link to TSC0000030 in database TSC';
51 my $ac = Bio::Annotation::Collection->new();
52 isa_ok($ac,'Bio::AnnotationCollectionI');
54 $ac->add_Annotation('dblink',$link1);
55 $ac->add_Annotation('dblink',
56                     Bio::Annotation::DBLink->new(-database => 'TSC',
57                                                  -primary_id => 'HUM_FABV'));
59 my $comment = Bio::Annotation::Comment->new( '-text' => 'sometext');
60 is $comment->text, 'sometext';
61 is $comment->as_text, 'Comment: sometext';
62 $ac->add_Annotation('comment', $comment);
66 my $target = Bio::Annotation::Target->new(-target_id  => 'F321966.1',
67                                           -start      => 1,
68                                           -end        => 200,
69                                           -strand     => 1,
70                                          );
71 isa_ok($target,'Bio::AnnotationI');
72 ok $ac->add_Annotation('target', $target);
75 my $ref = Bio::Annotation::Reference->new( -authors  => 'author line',
76                                            -title    => 'title line',
77                                            -location => 'location line',
78                                            -start    => 12);
79 isa_ok($ref,'Bio::AnnotationI');
80 is $ref->authors, 'author line';
81 is $ref->title,  'title line';
82 is $ref->location, 'location line';
83 is $ref->start, 12;
84 is $ref->database, 'MEDLINE';
85 is $ref->as_text, 'Reference: title line';
86 $ac->add_Annotation('reference', $ref);
89 my $n = 0;
90 foreach my $link ( $ac->get_Annotations('dblink') ) {
91     is $link->database, 'TSC';
92     is $link->tagname(), 'dblink';
93     $n++;
95 is ($n, 2);
97 $n = 0;
98 my @keys = $ac->get_all_annotation_keys();
99 is (scalar(@keys), 4);
100 foreach my $ann ( $ac->get_Annotations() ) {
101     shift(@keys) if ($n > 0) && ($ann->tagname ne $keys[0]);
102     is $ann->tagname(), $keys[0];
103     $n++;
105 is ($n, 5);
107 $ac->add_Annotation($link1);
109 $n = 0;
110 foreach my $link ( $ac->get_Annotations('dblink') ) {
111     is $link->tagname(), 'dblink';
112     $n++;
114 is ($n, 3);
116 # annotation of structured simple values (like swissprot''is GN line)
117 my $ann = Bio::Annotation::StructuredValue->new();
118 isa_ok($ann, "Bio::AnnotationI");
120 $ann->add_value([-1], "val1");
121 is ($ann->value(), "val1");
122 $ann->value("compat test");
123 is ($ann->value(), "compat test");
124 $ann->add_value([-1], "val2");
125 is ($ann->value(-joins => [" AND "]), "compat test AND val2");
126 $ann->add_value([0], "val1");
127 is ($ann->value(-joins => [" AND "]), "val1 AND val2");
128 $ann->add_value([-1,-1], "val3", "val4");
129 $ann->add_value([-1,-1], "val5", "val6");
130 $ann->add_value([-1,-1], "val7");
131 is ($ann->value(-joins => [" AND "]), "val1 AND val2 AND (val3 AND val4) AND (val5 AND val6) AND val7");
132 is ($ann->value(-joins => [" AND ", " OR "]), "val1 AND val2 AND (val3 OR val4) AND (val5 OR val6) AND val7");
134 $n = 1;
135 foreach ($ann->get_all_values()) {
136     is ($_, "val".$n++);
139 # nested collections
140 my $nested_ac = Bio::Annotation::Collection->new();
141 $nested_ac->add_Annotation('nested', $ac);
143 is (scalar($nested_ac->get_Annotations()), 1);
144 ($ac) = $nested_ac->get_Annotations();
145 isa_ok($ac, "Bio::AnnotationCollectionI");
146 is (scalar($nested_ac->get_all_Annotations()), 6);
147 $nested_ac->add_Annotation('gene names', $ann);
148 is (scalar($nested_ac->get_Annotations()), 2);
149 is (scalar($nested_ac->get_all_Annotations()), 7);
150 is (scalar($nested_ac->get_Annotations('dblink')), 0);
151 my @anns = $nested_ac->get_Annotations('gene names');
152 isa_ok($anns[0], "Bio::Annotation::StructuredValue");
153 @anns = map { $_->get_Annotations('dblink');
154           } $nested_ac->get_Annotations('nested');
155 is (scalar(@anns), 3);
156 is (scalar($nested_ac->flatten_Annotations()), 2);
157 is (scalar($nested_ac->get_Annotations()), 7);
158 is (scalar($nested_ac->get_all_Annotations()), 7);
160 SKIP: {
161   test_skip(-tests => 7, -requires_modules => [qw(Bio::Annotation::OntologyTerm)]);
162   use_ok('Bio::Annotation::OntologyTerm');
163   # OntologyTerm annotation
164   my $termann = Bio::Annotation::OntologyTerm->new(-label => 'test case',
165                                                    -identifier => 'Ann:00001',
166                                                    -ontology => 'dumpster');
167   isa_ok($termann->term,'Bio::Ontology::Term');
168   is ($termann->term->name, 'test case');
169   is ($termann->term->identifier, 'Ann:00001');
170   is ($termann->tagname, 'dumpster');
171   is ($termann->ontology->name, 'dumpster');
172   is ($termann->as_text, "dumpster|test case|");
175 # AnnotatableI
176 use Bio::Seq;
177 my $seq = Bio::Seq->new();
178 SKIP: {
179         test_skip(-requires_modules => [qw(Bio::SeqFeature::Annotated URI::Escape)],
180                           -tests => 4);
181         my $fea = Bio::SeqFeature::Annotated->new();
182         isa_ok($fea, "Bio::SeqFeatureI",'isa SeqFeatureI');
183         isa_ok($fea, "Bio::AnnotatableI",'isa AnnotatableI');
184         $fea = Bio::SeqFeature::Generic->new();
185         isa_ok($fea, "Bio::SeqFeatureI",'isa SeqFeatureI');
186         isa_ok($fea, "Bio::AnnotatableI",'isa AnnotatableI');
189 # tests for Bio::Annotation::AnnotationFactory
191 my $factory = Bio::Annotation::AnnotationFactory->new;
192 isa_ok($factory, 'Bio::Factory::ObjectFactoryI');
194 # defaults to SimpleValue
195 $ann = $factory->create_object(-value => 'peroxisome',
196                                -tagname => 'cellular component');
197 isa_ok($ann, 'Bio::Annotation::SimpleValue');
199 $factory->type('Bio::Annotation::OntologyTerm');
201 $ann = $factory->create_object(-name => 'peroxisome',
202                                -tagname => 'cellular component');
203 ok(defined $ann);
204 isa_ok($ann, 'Bio::Annotation::OntologyTerm');
206 # unset type()
207 $factory->type(undef);
208 $ann = $factory->create_object(-text => 'this is a comment');
209 ok(defined $ann,'Bio::Annotation::Comment');
211 isa_ok($ann,'Bio::Annotation::Comment');
213 ok $factory->type('Bio::Annotation::Comment');
214 $ann = $factory->create_object(-text => 'this is a comment');
215 ok(defined $ann,'Bio::Annotation::Comment');
216 isa_ok($ann,'Bio::Annotation::Comment');
218 # factory guessing the type: Comment
219 $factory = Bio::Annotation::AnnotationFactory->new();
220 $ann = $factory->create_object(-text => 'this is a comment');
221 ok(defined $ann,'Bio::Annotation::Comment');
222 isa_ok($ann,'Bio::Annotation::Comment');
224 # factory guessing the type: Target
225 $factory = Bio::Annotation::AnnotationFactory->new();
226 $ann = $factory->create_object(-target_id => 'F1234',
227                                -start     => 1,
228                                -end       => 10 );
229 ok defined $ann;
230 isa_ok($ann,'Bio::Annotation::Target');
232 # factory guessing the type: OntologyTerm
233 $factory = Bio::Annotation::AnnotationFactory->new();
234 ok(defined ($ann = $factory->create_object(-name => 'peroxisome',
235                                            -tagname => 'cellular component')));
236 like(ref $ann, qr(Bio::Annotation::OntologyTerm));
238 # tree
239 my $tree_filename = test_input_file('longnames.dnd');
240 my $tree = Bio::TreeIO->new(-file=>$tree_filename)->next_tree();
241 my $ann_tree = Bio::Annotation::Tree->new(
242                                           -tagname  => 'tree',
243                                           -tree_obj => $tree,
244                                          );
246 isa_ok($ann_tree, 'Bio::AnnotationI');
247 $ann_tree->tree_id('test');
248 is $ann_tree->tree_id(), 'test', "tree_id()";
249 $ann_tree->tagname('tree');
250 is $ann_tree->tagname(), 'tree', "tagname()";
251 use Bio::AlignIO;
252 my $aln = Bio::AlignIO->new(-file  => test_input_file('longnames.aln'),
253                          -format=>'clustalw')->next_aln();
254 $ac = Bio::Annotation::Collection->new();
255 $ac->add_Annotation('tree',$ann_tree);
256 $aln->annotation($ac);
257 for my $treeblock ( $aln->annotation->get_Annotations('tree') ) {
258   my $treeref = $treeblock->tree();
259   my @nodes = sort { defined $a->id &&
260                        defined $b->id &&
261                          $a->id cmp $b->id } $treeref->get_nodes();
262   is(@nodes, 26);
263   is $nodes[12]->id, 'Skud_Contig1703.7', "add tree to AlignI";
264   my $str;
265   for my $seq ($aln->each_seq_with_id($nodes[12]->id)) {
266     $str = $seq->subseq(1,20);
267   }
268   is( $str, "-------------MPFAQIV", "get seq from node id");
271 # factory guessing the type: Tree
272 $factory = Bio::Annotation::AnnotationFactory->new();
273 $ann = $factory->create_object(-tree_obj => $tree);
274 ok defined $ann;
275 isa_ok($ann,'Bio::Annotation::Tree');
277 #tagtree
278 my $struct = [ 'genenames' => [
279                                ['genename' => [
280                                                [ 'Name' => 'CALM1' ],
281                                                ['Synonyms'=> 'CAM1'],
282                                                ['Synonyms'=> 'CALM'],
283                                                ['Synonyms'=> 'CAM' ] ] ],
284                                ['genename'=> [
285                                               [ 'Name'=> 'CALM2' ],
286                                               [ 'Synonyms'=> 'CAM2'],
287                                               [ 'Synonyms'=> 'CAMB'] ] ],
288                                [ 'genename'=> [
289                                                [ 'Name'=> 'CALM3' ],
290                                                [ 'Synonyms'=> 'CAM3' ],
291                                                [ 'Synonyms'=> 'CAMC' ] ] ]
292                               ] ];
294 my $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'gn',
295                                                -value => $struct);
297 isa_ok($ann_struct, 'Bio::AnnotationI');
298 my $val = $ann_struct->value;
299 like($val, qr/Name: CALM1/,'default itext');
301 # roundtrip
302 my $ann_struct2 = Bio::Annotation::TagTree->new(-tagname => 'gn',
303                                                 -value => $val);
304 is($ann_struct2->value, $val,'roundtrip');
306 # formats
307 like($ann_struct2->value, qr/Name: CALM1/,'itext');
308 $ann_struct2->tagformat('sxpr');
309 like($ann_struct2->value, qr/\(Name "CALM1"\)/,'spxr');
310 $ann_struct2->tagformat('indent');
311 like($ann_struct2->value, qr/Name "CALM1"/,'indent');
313 SKIP: {
314     eval {require XML::Parser::PerlSAX};
315     skip ("XML::Parser::PerlSAX rquired for XML",1) if $@;
316     $ann_struct2->tagformat('xml');
317     like($ann_struct2->value, qr/<Name>CALM1<\/Name>/,'xml');
320 # grab Data::Stag nodes, use Data::Stag methods
321 my @nodes = $ann_struct2->children;
322 for my $node (@nodes) {
323     isa_ok($node, 'Data::Stag::StagI');
324     is($node->element, 'genename');
325     # add tag-value data to node
326     $node->set('foo', 'bar');
327     # check output
328     like($node->itext, qr/foo:\s+bar/,'child changes');
331 $ann_struct2->tagformat('itext');
332 like($ann_struct2->value, qr/foo:\s+bar/,'child changes in parent node');
334 # pass in a Data::Stag node to value()
335 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
336 like($ann_struct->value, qr/^\s+:\s+$/xms, 'no tags');
337 like($ann_struct->value, qr/^\s+:\s+$/xms,'before Stag node');
338 $ann_struct->value($nodes[0]);
339 like($ann_struct->value, qr/Name: CALM1/,'after Stag node');
340 is(ref $ann_struct->node, ref $nodes[0], 'both stag nodes');
341 isnt($ann_struct->node, $nodes[0], 'different instances');
343 # pass in another TagTree to value()
344 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
345 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
346 $ann_struct->value($ann_struct2);
347 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
348 is(ref $ann_struct->node, ref $ann_struct2->node, 'both stag nodes');
349 isnt($ann_struct->node, $ann_struct2->node, 'different instances');
351 # replace the Data::Stag node in the annotation (no copy)
352 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
353 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
354 $ann_struct->node($nodes[1]);
355 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
356 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
357 is($ann_struct->node, $nodes[1], 'same instance');
358 # replace the Data::Stag node in the annotation (use duplicate)
359 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
360 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
361 $ann_struct->node($nodes[1],'copy');
362 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
363 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
364 isnt($ann_struct->node, $nodes[1], 'different instance');
366 #check insertion in to collection
367 $ann_struct = Bio::Annotation::TagTree->new(-value => $struct);
368 $ac = Bio::Annotation::Collection->new();
370 $ac->add_Annotation('genenames',$ann_struct);
371 my $ct = 0;
372 for my $tagtree ( $ac->get_Annotations('genenames') ) {
373   isa_ok($tagtree, 'Bio::AnnotationI');
374   for my $node ($tagtree->children) {
375     isa_ok($node, 'Data::Stag::StagI');
376     like($node->itext, qr/Name:\s+CALM/,'child changes');
377     $ct++;
378   }
380 is($ct,3);
382 # factory guessing the type: TagTree
383 $factory = Bio::Annotation::AnnotationFactory->new();
384 $ann = $factory->create_object(-value => $struct);
385 ok defined $ann;
386 isa_ok($ann,'Bio::Annotation::TagTree');