1 # -*-Perl-*- Test Harness script for Bioperl
9 test_begin(-tests => 148,
10 -requires_module => 'Data::Stag');
12 use_ok('Bio::Annotation::Collection');
13 use_ok('Bio::Annotation::DBLink');
14 use_ok('Bio::Annotation::Comment');
15 use_ok('Bio::Annotation::Reference');
16 use_ok('Bio::Annotation::SimpleValue');
17 use_ok('Bio::Annotation::Target');
18 use_ok('Bio::Annotation::AnnotationFactory');
19 use_ok('Bio::Annotation::StructuredValue');
20 use_ok('Bio::Annotation::TagTree');
21 use_ok('Bio::Annotation::Tree');
24 my $DEBUG = test_debug();
28 my $simple = Bio::Annotation::SimpleValue->new(-tagname => 'colour',
32 isa_ok($simple, 'Bio::AnnotationI');
33 is $simple->display_text, 1;
35 is $simple->tagname, 'colour';
37 is $simple->value(0), 0;
39 is $simple->display_text, 0;
43 my $link1 = Bio::Annotation::DBLink->new(-database => 'TSC',
44 -primary_id => 'TSC0000030',
46 isa_ok($link1,'Bio::AnnotationI');
47 is $link1->database(), 'TSC';
48 is $link1->primary_id(), 'TSC0000030';
49 is $link1->as_text, 'Direct database link to TSC0000030 in database TSC';
50 my $ac = Bio::Annotation::Collection->new();
51 isa_ok($ac,'Bio::AnnotationCollectionI');
53 $ac->add_Annotation('dblink',$link1);
54 $ac->add_Annotation('dblink',
55 Bio::Annotation::DBLink->new(-database => 'TSC',
56 -primary_id => 'HUM_FABV'));
58 my $comment = Bio::Annotation::Comment->new( '-text' => 'sometext');
59 is $comment->text, 'sometext';
60 is $comment->as_text, 'Comment: sometext';
61 $ac->add_Annotation('comment', $comment);
65 my $target = Bio::Annotation::Target->new(-target_id => 'F321966.1',
70 isa_ok($target,'Bio::AnnotationI');
71 ok $ac->add_Annotation('target', $target);
74 my $ref = Bio::Annotation::Reference->new( -authors => 'author line',
75 -title => 'title line',
76 -location => 'location line',
78 isa_ok($ref,'Bio::AnnotationI');
79 is $ref->authors, 'author line';
80 is $ref->title, 'title line';
81 is $ref->location, 'location line';
83 is $ref->database, 'MEDLINE';
84 is $ref->as_text, 'Reference: title line';
85 $ac->add_Annotation('reference', $ref);
89 foreach my $link ( $ac->get_Annotations('dblink') ) {
90 is $link->database, 'TSC';
91 is $link->tagname(), 'dblink';
97 my @keys = $ac->get_all_annotation_keys();
98 is (scalar(@keys), 4);
99 foreach my $ann ( $ac->get_Annotations() ) {
100 shift(@keys) if ($n > 0) && ($ann->tagname ne $keys[0]);
101 is $ann->tagname(), $keys[0];
106 $ac->add_Annotation($link1);
109 foreach my $link ( $ac->get_Annotations('dblink') ) {
110 is $link->tagname(), 'dblink';
115 # annotation of structured simple values (like swissprot''is GN line)
116 my $ann = Bio::Annotation::StructuredValue->new();
117 isa_ok($ann, "Bio::AnnotationI");
119 $ann->add_value([-1], "val1");
120 is ($ann->value(), "val1");
121 $ann->value("compat test");
122 is ($ann->value(), "compat test");
123 $ann->add_value([-1], "val2");
124 is ($ann->value(-joins => [" AND "]), "compat test AND val2");
125 $ann->add_value([0], "val1");
126 is ($ann->value(-joins => [" AND "]), "val1 AND val2");
127 $ann->add_value([-1,-1], "val3", "val4");
128 $ann->add_value([-1,-1], "val5", "val6");
129 $ann->add_value([-1,-1], "val7");
130 is ($ann->value(-joins => [" AND "]), "val1 AND val2 AND (val3 AND val4) AND (val5 AND val6) AND val7");
131 is ($ann->value(-joins => [" AND ", " OR "]), "val1 AND val2 AND (val3 OR val4) AND (val5 OR val6) AND val7");
134 foreach ($ann->get_all_values()) {
139 my $nested_ac = Bio::Annotation::Collection->new();
140 $nested_ac->add_Annotation('nested', $ac);
142 is (scalar($nested_ac->get_Annotations()), 1);
143 ($ac) = $nested_ac->get_Annotations();
144 isa_ok($ac, "Bio::AnnotationCollectionI");
145 is (scalar($nested_ac->get_all_Annotations()), 6);
146 $nested_ac->add_Annotation('gene names', $ann);
147 is (scalar($nested_ac->get_Annotations()), 2);
148 is (scalar($nested_ac->get_all_Annotations()), 7);
149 is (scalar($nested_ac->get_Annotations('dblink')), 0);
150 my @anns = $nested_ac->get_Annotations('gene names');
151 isa_ok($anns[0], "Bio::Annotation::StructuredValue");
152 @anns = map { $_->get_Annotations('dblink');
153 } $nested_ac->get_Annotations('nested');
154 is (scalar(@anns), 3);
155 is (scalar($nested_ac->flatten_Annotations()), 2);
156 is (scalar($nested_ac->get_Annotations()), 7);
157 is (scalar($nested_ac->get_all_Annotations()), 7);
160 test_skip(-tests => 7, -requires_modules => [qw(Bio::Annotation::OntologyTerm)]);
161 use_ok('Bio::Annotation::OntologyTerm');
162 # OntologyTerm annotation
163 my $termann = Bio::Annotation::OntologyTerm->new(-label => 'test case',
164 -identifier => 'Ann:00001',
165 -ontology => 'dumpster');
166 isa_ok($termann->term,'Bio::Ontology::Term');
167 is ($termann->term->name, 'test case');
168 is ($termann->term->identifier, 'Ann:00001');
169 is ($termann->tagname, 'dumpster');
170 is ($termann->ontology->name, 'dumpster');
171 is ($termann->as_text, "dumpster|test case|");
174 # tests for Bio::Annotation::AnnotationFactory
176 my $factory = Bio::Annotation::AnnotationFactory->new;
177 isa_ok($factory, 'Bio::Factory::ObjectFactoryI');
179 # defaults to SimpleValue
180 $ann = $factory->create_object(-value => 'peroxisome',
181 -tagname => 'cellular component');
182 isa_ok($ann, 'Bio::Annotation::SimpleValue');
184 $factory->type('Bio::Annotation::OntologyTerm');
186 $ann = $factory->create_object(-name => 'peroxisome',
187 -tagname => 'cellular component');
189 isa_ok($ann, 'Bio::Annotation::OntologyTerm');
192 $factory->type(undef);
193 $ann = $factory->create_object(-text => 'this is a comment');
194 ok(defined $ann,'Bio::Annotation::Comment');
196 isa_ok($ann,'Bio::Annotation::Comment');
198 ok $factory->type('Bio::Annotation::Comment');
199 $ann = $factory->create_object(-text => 'this is a comment');
200 ok(defined $ann,'Bio::Annotation::Comment');
201 isa_ok($ann,'Bio::Annotation::Comment');
203 # factory guessing the type: Comment
204 $factory = Bio::Annotation::AnnotationFactory->new();
205 $ann = $factory->create_object(-text => 'this is a comment');
206 ok(defined $ann,'Bio::Annotation::Comment');
207 isa_ok($ann,'Bio::Annotation::Comment');
209 # factory guessing the type: Target
210 $factory = Bio::Annotation::AnnotationFactory->new();
211 $ann = $factory->create_object(-target_id => 'F1234',
215 isa_ok($ann,'Bio::Annotation::Target');
217 # factory guessing the type: OntologyTerm
218 $factory = Bio::Annotation::AnnotationFactory->new();
219 ok(defined ($ann = $factory->create_object(-name => 'peroxisome',
220 -tagname => 'cellular component')));
221 like(ref $ann, qr(Bio::Annotation::OntologyTerm));
224 my $tree_filename = test_input_file('longnames.dnd');
225 my $tree = Bio::TreeIO->new(-file=>$tree_filename)->next_tree();
226 my $ann_tree = Bio::Annotation::Tree->new(
231 isa_ok($ann_tree, 'Bio::AnnotationI');
232 $ann_tree->tree_id('test');
233 is $ann_tree->tree_id(), 'test', "tree_id()";
234 $ann_tree->tagname('tree');
235 is $ann_tree->tagname(), 'tree', "tagname()";
237 my $aln = Bio::AlignIO->new(-file => test_input_file('longnames.aln'),
238 -format=>'clustalw')->next_aln();
239 $ac = Bio::Annotation::Collection->new();
240 $ac->add_Annotation('tree',$ann_tree);
241 $aln->annotation($ac);
242 for my $treeblock ( $aln->annotation->get_Annotations('tree') ) {
243 my $treeref = $treeblock->tree();
244 my @nodes = sort { defined $a->id &&
246 $a->id cmp $b->id } $treeref->get_nodes();
248 is $nodes[12]->id, 'Skud_Contig1703.7', "add tree to AlignI";
250 for my $seq ($aln->each_seq_with_id($nodes[12]->id)) {
251 $str = $seq->subseq(1,20);
253 is( $str, "-------------MPFAQIV", "get seq from node id");
256 # factory guessing the type: Tree
257 $factory = Bio::Annotation::AnnotationFactory->new();
258 $ann = $factory->create_object(-tree_obj => $tree);
260 isa_ok($ann,'Bio::Annotation::Tree');
263 my $struct = [ 'genenames' => [
265 [ 'Name' => 'CALM1' ],
266 ['Synonyms'=> 'CAM1'],
267 ['Synonyms'=> 'CALM'],
268 ['Synonyms'=> 'CAM' ] ] ],
270 [ 'Name'=> 'CALM2' ],
271 [ 'Synonyms'=> 'CAM2'],
272 [ 'Synonyms'=> 'CAMB'] ] ],
274 [ 'Name'=> 'CALM3' ],
275 [ 'Synonyms'=> 'CAM3' ],
276 [ 'Synonyms'=> 'CAMC' ] ] ]
279 my $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'gn',
282 isa_ok($ann_struct, 'Bio::AnnotationI');
283 my $val = $ann_struct->value;
284 like($val, qr/Name: CALM1/,'default itext');
287 my $ann_struct2 = Bio::Annotation::TagTree->new(-tagname => 'gn',
289 is($ann_struct2->value, $val,'roundtrip');
292 like($ann_struct2->value, qr/Name: CALM1/,'itext');
293 $ann_struct2->tagformat('sxpr');
294 like($ann_struct2->value, qr/\(Name "CALM1"\)/,'spxr');
295 $ann_struct2->tagformat('indent');
296 like($ann_struct2->value, qr/Name "CALM1"/,'indent');
299 eval {require XML::Parser::PerlSAX};
300 skip ("XML::Parser::PerlSAX rquired for XML",1) if $@;
301 $ann_struct2->tagformat('xml');
302 like($ann_struct2->value, qr/<Name>CALM1<\/Name>/,'xml');
305 # grab Data::Stag nodes, use Data::Stag methods
306 my @nodes = $ann_struct2->children;
307 for my $node (@nodes) {
308 isa_ok($node, 'Data::Stag::StagI');
309 is($node->element, 'genename');
310 # add tag-value data to node
311 $node->set('foo', 'bar');
313 like($node->itext, qr/foo:\s+bar/,'child changes');
316 $ann_struct2->tagformat('itext');
317 like($ann_struct2->value, qr/foo:\s+bar/,'child changes in parent node');
319 # pass in a Data::Stag node to value()
320 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
321 like($ann_struct->value, qr/^\s+:\s+$/xms, 'no tags');
322 like($ann_struct->value, qr/^\s+:\s+$/xms,'before Stag node');
323 $ann_struct->value($nodes[0]);
324 like($ann_struct->value, qr/Name: CALM1/,'after Stag node');
325 is(ref $ann_struct->node, ref $nodes[0], 'both stag nodes');
326 isnt($ann_struct->node, $nodes[0], 'different instances');
328 # pass in another TagTree to value()
329 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
330 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
331 $ann_struct->value($ann_struct2);
332 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
333 is(ref $ann_struct->node, ref $ann_struct2->node, 'both stag nodes');
334 isnt($ann_struct->node, $ann_struct2->node, 'different instances');
336 # replace the Data::Stag node in the annotation (no copy)
337 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
338 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
339 $ann_struct->node($nodes[1]);
340 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
341 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
342 is($ann_struct->node, $nodes[1], 'same instance');
343 # replace the Data::Stag node in the annotation (use duplicate)
344 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
345 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
346 $ann_struct->node($nodes[1],'copy');
347 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
348 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
349 isnt($ann_struct->node, $nodes[1], 'different instance');
351 #check insertion in to collection
352 $ann_struct = Bio::Annotation::TagTree->new(-value => $struct);
353 $ac = Bio::Annotation::Collection->new();
355 $ac->add_Annotation('genenames',$ann_struct);
357 for my $tagtree ( $ac->get_Annotations('genenames') ) {
358 isa_ok($tagtree, 'Bio::AnnotationI');
359 for my $node ($tagtree->children) {
360 isa_ok($node, 'Data::Stag::StagI');
361 like($node->itext, qr/Name:\s+CALM/,'child changes');
367 # factory guessing the type: TagTree
368 $factory = Bio::Annotation::AnnotationFactory->new();
369 $ann = $factory->create_object(-value => $struct);
371 isa_ok($ann,'Bio::Annotation::TagTree');