1 # -*-Perl-*- Test Harness script for Bioperl
10 test_begin(-tests => 148,
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();
29 my $simple = Bio::Annotation::SimpleValue->new(-tagname => 'colour',
33 isa_ok($simple, 'Bio::AnnotationI');
34 is $simple->display_text, 1;
36 is $simple->tagname, 'colour';
38 is $simple->value(0), 0;
40 is $simple->display_text, 0;
44 my $link1 = Bio::Annotation::DBLink->new(-database => 'TSC',
45 -primary_id => 'TSC0000030',
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',
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',
79 isa_ok($ref,'Bio::AnnotationI');
80 is $ref->authors, 'author line';
81 is $ref->title, 'title line';
82 is $ref->location, 'location line';
84 is $ref->database, 'MEDLINE';
85 is $ref->as_text, 'Reference: title line';
86 $ac->add_Annotation('reference', $ref);
90 foreach my $link ( $ac->get_Annotations('dblink') ) {
91 is $link->database, 'TSC';
92 is $link->tagname(), 'dblink';
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];
107 $ac->add_Annotation($link1);
110 foreach my $link ( $ac->get_Annotations('dblink') ) {
111 is $link->tagname(), 'dblink';
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");
135 foreach ($ann->get_all_values()) {
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);
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 # tests for Bio::Annotation::AnnotationFactory
177 my $factory = Bio::Annotation::AnnotationFactory->new;
178 isa_ok($factory, 'Bio::Factory::ObjectFactoryI');
180 # defaults to SimpleValue
181 $ann = $factory->create_object(-value => 'peroxisome',
182 -tagname => 'cellular component');
183 isa_ok($ann, 'Bio::Annotation::SimpleValue');
185 $factory->type('Bio::Annotation::OntologyTerm');
187 $ann = $factory->create_object(-name => 'peroxisome',
188 -tagname => 'cellular component');
190 isa_ok($ann, 'Bio::Annotation::OntologyTerm');
193 $factory->type(undef);
194 $ann = $factory->create_object(-text => 'this is a comment');
195 ok(defined $ann,'Bio::Annotation::Comment');
197 isa_ok($ann,'Bio::Annotation::Comment');
199 ok $factory->type('Bio::Annotation::Comment');
200 $ann = $factory->create_object(-text => 'this is a comment');
201 ok(defined $ann,'Bio::Annotation::Comment');
202 isa_ok($ann,'Bio::Annotation::Comment');
204 # factory guessing the type: Comment
205 $factory = Bio::Annotation::AnnotationFactory->new();
206 $ann = $factory->create_object(-text => 'this is a comment');
207 ok(defined $ann,'Bio::Annotation::Comment');
208 isa_ok($ann,'Bio::Annotation::Comment');
210 # factory guessing the type: Target
211 $factory = Bio::Annotation::AnnotationFactory->new();
212 $ann = $factory->create_object(-target_id => 'F1234',
216 isa_ok($ann,'Bio::Annotation::Target');
218 # factory guessing the type: OntologyTerm
219 $factory = Bio::Annotation::AnnotationFactory->new();
220 ok(defined ($ann = $factory->create_object(-name => 'peroxisome',
221 -tagname => 'cellular component')));
222 like(ref $ann, qr(Bio::Annotation::OntologyTerm));
225 my $tree_filename = test_input_file('longnames.dnd');
226 my $tree = Bio::TreeIO->new(-file=>$tree_filename)->next_tree();
227 my $ann_tree = Bio::Annotation::Tree->new(
232 isa_ok($ann_tree, 'Bio::AnnotationI');
233 $ann_tree->tree_id('test');
234 is $ann_tree->tree_id(), 'test', "tree_id()";
235 $ann_tree->tagname('tree');
236 is $ann_tree->tagname(), 'tree', "tagname()";
238 my $aln = Bio::AlignIO->new(-file => test_input_file('longnames.aln'),
239 -format=>'clustalw')->next_aln();
240 $ac = Bio::Annotation::Collection->new();
241 $ac->add_Annotation('tree',$ann_tree);
242 $aln->annotation($ac);
243 for my $treeblock ( $aln->annotation->get_Annotations('tree') ) {
244 my $treeref = $treeblock->tree();
245 my @nodes = sort { defined $a->id &&
247 $a->id cmp $b->id } $treeref->get_nodes();
249 is $nodes[12]->id, 'Skud_Contig1703.7', "add tree to AlignI";
251 for my $seq ($aln->each_seq_with_id($nodes[12]->id)) {
252 $str = $seq->subseq(1,20);
254 is( $str, "-------------MPFAQIV", "get seq from node id");
257 # factory guessing the type: Tree
258 $factory = Bio::Annotation::AnnotationFactory->new();
259 $ann = $factory->create_object(-tree_obj => $tree);
261 isa_ok($ann,'Bio::Annotation::Tree');
264 my $struct = [ 'genenames' => [
266 [ 'Name' => 'CALM1' ],
267 ['Synonyms'=> 'CAM1'],
268 ['Synonyms'=> 'CALM'],
269 ['Synonyms'=> 'CAM' ] ] ],
271 [ 'Name'=> 'CALM2' ],
272 [ 'Synonyms'=> 'CAM2'],
273 [ 'Synonyms'=> 'CAMB'] ] ],
275 [ 'Name'=> 'CALM3' ],
276 [ 'Synonyms'=> 'CAM3' ],
277 [ 'Synonyms'=> 'CAMC' ] ] ]
280 my $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'gn',
283 isa_ok($ann_struct, 'Bio::AnnotationI');
284 my $val = $ann_struct->value;
285 like($val, qr/Name: CALM1/,'default itext');
288 my $ann_struct2 = Bio::Annotation::TagTree->new(-tagname => 'gn',
290 is($ann_struct2->value, $val,'roundtrip');
293 like($ann_struct2->value, qr/Name: CALM1/,'itext');
294 $ann_struct2->tagformat('sxpr');
295 like($ann_struct2->value, qr/\(Name "CALM1"\)/,'spxr');
296 $ann_struct2->tagformat('indent');
297 like($ann_struct2->value, qr/Name "CALM1"/,'indent');
300 eval {require XML::Parser::PerlSAX};
301 skip ("XML::Parser::PerlSAX rquired for XML",1) if $@;
302 $ann_struct2->tagformat('xml');
303 like($ann_struct2->value, qr/<Name>CALM1<\/Name>/,'xml');
306 # grab Data::Stag nodes, use Data::Stag methods
307 my @nodes = $ann_struct2->children;
308 for my $node (@nodes) {
309 isa_ok($node, 'Data::Stag::StagI');
310 is($node->element, 'genename');
311 # add tag-value data to node
312 $node->set('foo', 'bar');
314 like($node->itext, qr/foo:\s+bar/,'child changes');
317 $ann_struct2->tagformat('itext');
318 like($ann_struct2->value, qr/foo:\s+bar/,'child changes in parent node');
320 # pass in a Data::Stag node to value()
321 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
322 like($ann_struct->value, qr/^\s+:\s+$/xms, 'no tags');
323 like($ann_struct->value, qr/^\s+:\s+$/xms,'before Stag node');
324 $ann_struct->value($nodes[0]);
325 like($ann_struct->value, qr/Name: CALM1/,'after Stag node');
326 is(ref $ann_struct->node, ref $nodes[0], 'both stag nodes');
327 isnt($ann_struct->node, $nodes[0], 'different instances');
329 # pass in another TagTree to value()
330 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
331 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
332 $ann_struct->value($ann_struct2);
333 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
334 is(ref $ann_struct->node, ref $ann_struct2->node, 'both stag nodes');
335 isnt($ann_struct->node, $ann_struct2->node, 'different instances');
337 # replace the Data::Stag node in the annotation (no copy)
338 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
339 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
340 $ann_struct->node($nodes[1]);
341 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
342 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
343 is($ann_struct->node, $nodes[1], 'same instance');
344 # replace the Data::Stag node in the annotation (use duplicate)
345 $ann_struct = Bio::Annotation::TagTree->new(-tagname => 'mytags');
346 like($ann_struct->value, qr/^\s+:\s+$/xms,'before TagTree');
347 $ann_struct->node($nodes[1],'copy');
348 like($ann_struct->value, qr/Name: CALM2/,'after TagTree');
349 is(ref $ann_struct->node, ref $ann_struct2->node, 'stag nodes');
350 isnt($ann_struct->node, $nodes[1], 'different instance');
352 #check insertion in to collection
353 $ann_struct = Bio::Annotation::TagTree->new(-value => $struct);
354 $ac = Bio::Annotation::Collection->new();
356 $ac->add_Annotation('genenames',$ann_struct);
358 for my $tagtree ( $ac->get_Annotations('genenames') ) {
359 isa_ok($tagtree, 'Bio::AnnotationI');
360 for my $node ($tagtree->children) {
361 isa_ok($node, 'Data::Stag::StagI');
362 like($node->itext, qr/Name:\s+CALM/,'child changes');
368 # factory guessing the type: TagTree
369 $factory = Bio::Annotation::AnnotationFactory->new();
370 $ann = $factory->create_object(-value => $struct);
372 isa_ok($ann,'Bio::Annotation::TagTree');