Used quoted strings in Bio::Species to avoid a 'Invalid [] range in regex' error...
[bioperl-live.git] / t / Tree / TreeIO / phyloxml.t
blob5aaaa9b682fe969ab646fe712aac7c8e38b8cdb6
1 # -*-Perl-*- Test Harness script for Bioperl
3 use strict;
4 use warnings;
6 BEGIN {
7         use lib '.';
8         use Bio::Root::Test;
9   
10         test_begin(-tests => 98,
11                            -requires_modules => [qw(XML::LibXML XML::LibXML::Reader)]);
12         
13         use_ok('Bio::TreeIO');
16 my $verbose = test_debug();
18 SKIP: {
19   if (1000*$] < 5008) {
20     skip("Reader interface only supported in Perl >= 5.8",96);
21   } elsif (XML::LibXML::LIBXML_VERSION() <= 20620) {
22     skip("Reader not supported for libxml2 <= 2.6.20",96);
23   }
24   use_ok('Bio::TreeIO::phyloxml');
25   
26   if ($verbose) {
27         diag("libxml version: ", XML::LibXML::LIBXML_VERSION()); 
28   }
29   
30   ok my $treeio = Bio::TreeIO->new(
31                          -verbose => $verbose,
32                                    -format => 'phyloxml',
33                                    -file   => test_input_file('phyloxml_examples.xml'));
34   
35   # tree1: clade and attribute
36   # <phylogeny> <clade> <name>
37   {
38         if ($verbose > 0) {
39           diag("\ntree1: clade and attribute");
40         }
41         my $tree = $treeio->next_tree;
42         isa_ok($tree, 'Bio::Tree::TreeI');
43         is($tree->id, 'example from Prof. Joe Felsenstein\'s book "Inferring Phylogenies"');
44         is($tree->get_tag_values('description'), 'phyloXML allows to use either a "branch_length" attribute or element to indicate branch lengths.');
45         if ($verbose > 0) {
46           diag("tree id: ",$tree->id);
47           diag("tree description: ", $tree->get_tag_values('description'));
48         }
49         is($tree->get_tag_values('rooted'), 'true');
50         my @nodes = $tree->get_nodes;
51         is(@nodes, 5);
52         my ($A) = $tree->find_node('A');
53         ok($A);
54         is($A->branch_length, '0.102');
55         if ($verbose > 0) {
56           diag("node A: branch_length ", $A->branch_length);
57         }
58         is($A->ancestor->id, '');
59         is($A->ancestor->branch_length, '0.06');
60         my $leaves_string = $tree->simplify_to_leaves_string();
61         if ($verbose > 0) {
62           diag($leaves_string);
63         }
64         is($leaves_string, '((A,B),C)');
65   
66   # write_tree
67         if ($verbose > 0) {
68           diag("\ntest write_tree");
69         }
70         my $FILE1 = test_output_file();
71         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
72                 -format => 'phyloxml',
73                 -file   => ">$FILE1");
74         $treeout->write_tree($tree);
75         ok -s $FILE1;
76         if ($verbose > 0) {
77           diag(`cat $FILE1`);
78         }
79   }
80   
81   # tree2: branch_length
82   # <branch_length>
83   {
84         if ($verbose > 0) {
85           diag("\ntree2: branch_length");
86         }
87         my $tree = $treeio->next_tree;
88         isa_ok($tree, 'Bio::Tree::TreeI');
89         if ($verbose > 0) {
90           diag("tree id: ",$tree->id);
91         }
92         my @nodes = $tree->get_nodes;
93         is(@nodes, 5);
94         my $A = $tree->find_node('A');
95         ok($A);
96         is($A->branch_length, '0.102');
97         if ($verbose > 0) {
98           diag("node A: branch_length ", $A->branch_length);
99         }
100         is($A->ancestor->id, '');
101         is($A->ancestor->branch_length, '0.06');
102         my $leaves_string = $tree->simplify_to_leaves_string();
103         if ($verbose > 0) {
104           diag($leaves_string);
105         }
106         is($leaves_string, '((A,B),C)');
107   
108   # write_tree
109         if ($verbose > 0) {
110           diag("\ntest write_tree");
111         }
112         my $FILE1 = test_output_file();
113         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
114                 -format => 'phyloxml',
115                 -file   => ">$FILE1");
116         $treeout->write_tree($tree);
117         ok -s $FILE1;
118         if ($verbose > 0) {
119           diag(`cat $FILE1`);
120         }
121   }
122   
123   # tree3: confidence (bootstrap)
124   # <confidence>
125   {
126         if ($verbose > 0) {
127           diag("\ntree3: confidence (bootstrap)");
128         }
129         my $tree = $treeio->next_tree;
130         isa_ok($tree, 'Bio::Tree::TreeI');
131         if ($verbose > 0) {
132           diag("tree id: ",$tree->id);
133         }
134         my $AB = $tree->find_node('AB');
135         ok($AB);
136         is($AB->bootstrap, '89');
137         if ($verbose > 0) {
138           diag("node AB: bootstrap ", $AB->bootstrap);
139         }
140         my $leaves_string = $tree->simplify_to_leaves_string();
141         if ($verbose > 0) {
142           diag($leaves_string);
143         }
144         is($leaves_string, '((A,B),C)');
145   
146   # write_tree
147         if ($verbose > 0) {
148           diag("\ntest write_tree");
149         }
150         my $FILE1 = test_output_file();
151         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
152                 -format => 'phyloxml',
153                 -file   => ">$FILE1");
154         $treeout->write_tree($tree);
155         ok -s $FILE1;
156         if ($verbose > 0) {
157           diag(`cat $FILE1`);
158         }
159   }
160   
161   # tree4: species and sequence
162   # <taxonomy> <scientific_name> <sequence> <annotation>
163   {
164         if ($verbose > 0) {
165           diag("\ntree4: taxonomy and sequence");
166         }
167         my $tree = $treeio->next_tree;
168         isa_ok($tree, 'Bio::Tree::TreeI');
169         if ($verbose > 0) {
170           diag("tree id: ",$tree->id);
171         }
172         my $C = $tree->find_node('C');
173         my ($ac) = $C->annotation->get_Annotations('taxonomy');
174         isa_ok( $ac, 'Bio::Annotation::Collection');
175         my ($ac2) = $ac->get_Annotations('scientific_name');
176         isa_ok( $ac2, 'Bio::Annotation::Collection');
177         my ($scientificname) = $ac2->get_Annotations('_text');
178         is($scientificname->value, 'C. elegans');
179         if ($verbose > 0) {
180           diag( "Node C Scientific Name: ",$scientificname->value);
181         }
182         my ($ac3) = $C->annotation->get_nested_Annotations(-keys=>['scientific_name'], -recursive=>1);
183         isa_ok( $ac3, 'Bio::Annotation::Collection');
184         ($scientificname) = $ac2->get_Annotations('_text');
185         is($scientificname->value, 'C. elegans');
186         if ($verbose > 0) {
187           diag( "Node C Scientific Name: ",$scientificname->value);
188         }
189         my ($seq) = @{$C->sequence};
190         isa_ok( $seq, 'Bio::SeqI');
191         my ($seqac) = $seq->annotation;
192         isa_ok( $seqac, 'Bio::Annotation::Collection');
193         my ($descac) = $seqac->get_nested_Annotations(-keys=>['desc'], -recursive=>1);
194         my ($desc) = $descac->get_Annotations('_text');
195         is($desc->value, 'alcohol dehydrogenase');
196         if ($verbose > 0) {
197           diag( "Node C Sequence description: ",$desc->value);
198         }
199         ($descac) = $seqac->get_nested_Annotations(-keys=>['desc'], -recursive=>1);
200         
201   # write_tree
202         if ($verbose > 0) {
203           diag("\ntest write_tree");
204         }
205         my $FILE1 = test_output_file();
206         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
207                 -format => 'phyloxml',
208                 -file   => ">$FILE1");
209         $treeout->write_tree($tree);
210         ok -s $FILE1;
211         if ($verbose > 0) {
212           diag(`cat $FILE1`);
213         }
214   }
215   
216   # tree5: homolog relationship and sequence relationship
217   # <events> <speciations> <duplications> <symbol> <accession> 
218   # <sequence_relation> 
219   {
220         if ($verbose > 0) {
221           diag("\ntree5: events and relations");
222         }
223         my $tree = $treeio->next_tree;
224         isa_ok($tree, 'Bio::Tree::TreeI');
225         if ($verbose > 0) {
226           diag("tree id: ",$tree->id);
227         }
228         my $node = $tree->get_root_node;
229         my ($speciationsac) = $node->annotation->get_nested_Annotations(-keys=>['speciations'], -recursive=>1);
230         my ($speciationval) = $speciationsac->get_Annotations('_text');
231         is($speciationval->value, '1');
232         if ($verbose > 0) {
233           diag("root node speciation event: ", $speciationval->value);
234         }
235         my @children = ($node);
236         for (@children) {
237           push @children, $_->each_Descendent();
238         }
239         my @leaves = ();
240         for (@children) {
241           push @leaves, $_ if $_->is_Leaf;
242         }
243         my ($z) = $leaves[0];
244         my $z_seq = $z->sequence->[0];
245         isa_ok ($z_seq, 'Bio::SeqI');
246         my ($z_id) = $z_seq->annotation->get_nested_Annotations('-keys'=>['id_source'], '-recursive'=>1);
247         my ($z_id_text) = $z_id->value;
248         my @seq_rels = $z_seq->annotation->get_nested_Annotations('-keys'=>['sequence_relation'], '-recursive'=>1);
249         foreach my $rel (@seq_rels) {
250           isa_ok($rel, 'Bio::Annotation::Relation');
251           is ($rel->tagname, 'sequence_relation');
252           my $seqto = $rel->to;
253           isa_ok ($seqto, 'Bio::SeqI');
254           my ($seqto_id) = $seqto->annotation->get_nested_Annotations('-keys'=>['id_source'], '-recursive'=>1);
255           my $seqto_text = $seqto_id->value;
256           if ($verbose > 0) {
257                 diag( "node ", $z_id_text, " has ", $rel->type, " relation to ", $seqto_text);
258           }
259         }
260         my ($x) = $leaves[1];
261         
262   
263   # write_tree
264         if ($verbose > 0) {
265           diag("\ntest write_tree");
266         }
267         my $FILE1 = test_output_file();
268         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
269                 -format => 'phyloxml',
270                 -file   => ">$FILE1");
271         $treeout->write_tree($tree);
272         ok -s $FILE1;
273         if ($verbose > 0) {
274           diag(`cat $FILE1`);
275         }
276   }
277   
278   # tree6: detailed sequence data
279   # <mol_seq> <annotation> <code>
280   {
281         if ($verbose > 0) {
282           diag("\ntree6: detailed sequence annotation");
283         }
284         my $tree = $treeio->next_tree;
285         isa_ok($tree, 'Bio::Tree::TreeI');
286         if ($verbose > 0) {
287           diag("tree id: ",$tree->id);
288         }
289         my @children = ($tree->get_root_node);
290         for (@children) {
291           push @children, $_->each_Descendent();
292         }
293         my @leaves = ();
294         for (@children) {
295           push @leaves, $_ if $_->is_Leaf;
296         }
297         my ($z) = $leaves[0];
298         my $z_seq = $z->sequence->[0];
299         isa_ok ($z_seq, 'Bio::SeqI');
300         my ($z_seqname) = $z_seq->annotation->get_nested_Annotations('-keys'=>['name'], '-recursive'=>1); 
301         my ($z_seqname_text) = $z_seqname->get_Annotations('_text');
302         is ($z_seqname_text->value, 'NADH-dependent butanol dehydrogenase B');
303         my ($z_molseq) = $z_seq->seq;
304         is ($z_molseq, 'MVDFEYSIPTRIFFGKDKINVLGRELKKYGSKVLIVYGGGSIKRNGIYDK');
305         if ($verbose > 0) {
306           diag("Sequence ", $z_seqname_text->value, " is ", $z_molseq);
307         }
308         my ($z_seqname_text2) = $treeio->read_annotation('-obj'=>$z_seq, '-path'=>'name');
309         is ($z_seqname_text->value, $z_seqname_text2);
310         my ($y) = $leaves[1];
311         my $y_seq = $y->sequence->[0];
312         isa_ok ($y_seq, 'Bio::SeqI');
314   # add attribute id_source
315   $treeio->add_attribute(
316         '-obj' => $z_seq,
317         '-attr' => "id_source = \"A\""
318         );
319   $treeio->add_attribute(
320         '-obj' => $y_seq,
321         '-attr' => "id_source = \"B\""
322         );
324   # add sequence relation
325   $treeio->add_phyloXML_annotation(
326           '-obj'=>$tree,
327           '-xml'=>'<sequence_relation id_ref_0="A" id_ref_1="B" type="orthology"/>'
328           );      
330   # write_tree
331         if ($verbose > 0) {
332           diag("\ntest write_tree");
333         }
334         my $FILE1 = test_output_file();
335         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
336                 -format => 'phyloxml',
337                 -file   => ">$FILE1");
338         $treeout->write_tree($tree);
339         ok -s $FILE1;
340         if ($verbose > 0) {
341           diag(`cat $FILE1`);
342         }
343   }
344   
345   # tree7: network
346   # <clade_relation> @id_source & @id_ref
347   {
348         if ($verbose > 0) {
349           diag("\ntree7: network using id_source/id_ref");
350         }
351         my $tree = $treeio->next_tree;
352         isa_ok($tree, 'Bio::Tree::TreeI');
353         if ($verbose > 0) {
354           diag("tree id: ",$tree->id);
355         }
356         my @children = ($tree->get_root_node);
357         for (@children) {
358           push @children, $_->each_Descendent();
359         }
360         my @leaves = ();
361         for (@children) {
362           push @leaves, $_ if $_->is_Leaf;
363         }
364         my ($c) = $leaves[0];
365         my ($c_id) = $c->annotation->get_nested_Annotations('-keys'=>['id_source'], '-recursive'=>1);
366         my @clade_rels = $c->annotation->get_nested_Annotations('-keys'=>['clade_relation'], '-recursive'=>1);
367         foreach my $rel (@clade_rels) {
368           isa_ok($rel, 'Bio::Annotation::Relation');
369           is ($rel->tagname, 'clade_relation');
370           my $nodeto = $rel->to;
371           isa_ok ($nodeto, 'Bio::Tree::AnnotatableNode');
372           my ($nodeto_id) = $nodeto->annotation->get_nested_Annotations('-keys'=>['id_source'], '-recursive'=>1);
373           is ($nodeto_id->value, 'b');
374           my ($nodeto_id2) = $treeio->read_annotation('-obj'=>$nodeto, '-path'=>'id_source', '-attr'=>1);
375           is ($nodeto_id->value, $nodeto_id2);
376           if ($verbose > 0) {
377                 diag( "node ", $c_id->value, " has ", $rel->type, " relation to ", $nodeto_id->value);
378           }
379         }
380   
381   # write_tree
382         if ($verbose > 0) {
383           diag("\ntest write_tree");
384         }
385         my $FILE1 = test_output_file();
386         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
387                 -format => 'phyloxml',
388                 -file   => ">$FILE1");
389         $treeout->write_tree($tree);
390         ok -s $FILE1;
391         if ($verbose > 0) {
392           diag(`cat $FILE1`);
393         }
394   }
395   
396   # tree8: property elements
397   # <property>
398   {
399         if ($verbose > 0) {
400           diag("\ntree8: property");
401         }
402         my $tree = $treeio->next_tree;
403         isa_ok($tree, 'Bio::Tree::TreeI');
404         if ($verbose > 0) {
405           diag("tree id: ",$tree->id);
406         }
407         my ($A) = $tree->find_node('A');
408         isa_ok($A, 'Bio::Tree::AnnotatableNode');
409         my ($ac) = $A->annotation();
410         isa_ok($ac, 'Bio::AnnotationCollectionI');
411         my (@annotations) = $ac->get_Annotations('property');
412         isa_ok( $annotations[0], 'Bio::Annotation::Collection');
413         diag("property:",$annotations[0]) if $verbose;
414         my (@keys) = $annotations[0]->get_all_annotation_keys();
415         diag("keys:",@keys) if $verbose;
416         my (@value) = $annotations[0]->get_Annotations('_text');
417         is($value[0]->value, ' 1200 ');
418         if ($verbose > 0) {
419           diag( "Annotation NOAA:depth ",$value[0]->value);
420         }
421         my $leaves_string = $tree->simplify_to_leaves_string();
422         if ($verbose > 0) {
423           diag($leaves_string);
424         }
425         is($leaves_string, '((A,B),C)');
426   
427   # write_tree
428         if ($verbose > 0) {
429           diag("\ntest write_tree");
430         }
431         my $FILE1 = test_output_file();
432         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
433                 -format => 'phyloxml',
434                 -file   => ">$FILE1");
435         $treeout->write_tree($tree);
436         ok -s $FILE1;
437         if ($verbose > 0) {
438           diag(`cat $FILE1`);
439         }
440   }
441   
442   # tree9: property outside tree topology using id refs
443   # <property> @id_source @id_ref
444   {
445         if ($verbose > 0) {
446           diag("\ntree9: property using id_source/id_ref");
447         }
448         my $tree = $treeio->next_tree;
449         isa_ok($tree, 'Bio::Tree::TreeI');
450         if ($verbose > 0) {
451           diag("tree id: ",$tree->id);
452         }
453         my $A = $tree->find_node('A');
454         isa_ok($A, 'Bio::Tree::AnnotatableNode');
455         my $ac = $A->annotation();
456         isa_ok($ac, 'Bio::AnnotationCollectionI');
457         my @annotations = $ac->get_Annotations('property');
458         isa_ok( $annotations[0], 'Bio::Annotation::Collection');
459         my @value = $annotations[0]->get_Annotations('_text');
460         is($value[0]->value, ' 1200 ');
461         if ($verbose > 0) {
462           diag( "Annotation NOAA:depth ",$value[0]->value);
463         }
464         my $leaves_string = $tree->simplify_to_leaves_string();
465         if ($verbose > 0) {
466           diag($leaves_string);
467         }
468         is($leaves_string, '((A,B),C)');
469   
470   # write_tree
471         if ($verbose > 0) {
472           diag("\ntest write_tree");
473         }
474         my $FILE1 = test_output_file();
475         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
476                 -format => 'phyloxml',
477                 -file   => ">$FILE1");
478         $treeout->write_tree($tree);
479         ok -s $FILE1;
480         if ($verbose > 0) {
481           diag(`cat $FILE1`);
482         }
483   }
484   
485   # tree10: detailed taxonomy and distribution
486   # <id> <rank> <uri> <common_name> <distribution>
487   {
488         if ($verbose > 0) {
489           diag("\ntree10: taxonomy and distribution");
490         }
491         my $tree = $treeio->next_tree;
492         isa_ok($tree, 'Bio::Tree::TreeI');
493         if ($verbose > 0) {
494           diag("tree id: ",$tree->id);
495         }
496         my $node = $tree->get_root_node;
497         my @leaves;
498         my @children = ($node);
499         for (@children) {
500           push @children, $_->each_Descendent();
501         }
502         for (@children) {
503           push @leaves, $_ if $_->is_Leaf;
504         }
505         my ($A) = $leaves[0];
506         my ($scientificname) = $A->annotation->get_nested_Annotations('-keys'=>['scientific_name'], '-recursive'=>1);
507         my ($scientificname_text) = $scientificname->get_Annotations('_text');
508         my ($commonname) = $A->annotation->get_nested_Annotations('-keys'=>['common_name'], '-recursive'=>1); 
509         my ($commonname_text) = $commonname->get_Annotations('_text');
510         my ($rank) = $A->annotation->get_nested_Annotations('-keys'=>['rank'], '-recursive'=>1); 
511         my ($rank_text) = $rank->get_Annotations('_text');
512         if ($verbose > 0) {
513           diag("node rank is ", $rank_text->value);
514           diag("node scientific name is ", $scientificname_text->value);
515           diag("node common name is ", $commonname_text->value);
516         }
517         my ($distribution) = $A->annotation->get_nested_Annotations('-keys'=>['distribution'], '-recursive'=>1);
518         my ($desc) = $distribution->get_Annotations('desc');
519         my ($desc_text) = $desc->get_Annotations('_text');
520         if ($verbose > 0) {
521           diag("node distribution is ", $desc_text->value);
522         } 
523         
524   # write_tree
525         if ($verbose > 0) {
526           diag("\ntest write_tree");
527         }
528         my $FILE1 = test_output_file();
529         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
530                 -format => 'phyloxml',
531                 -file   => ">$FILE1");
532         $treeout->write_tree($tree);
533         ok -s $FILE1;
534         if ($verbose > 0) {
535           diag(`cat $FILE1`);
536         }
537   }
538   
539   # tree11: phylogeographic information
540   # <distribution> <point> <lat> <long> <alt>
541   {
542         if ($verbose > 0) {
543           diag("\ntree11: phylogenographic information");
544         }
545         my $tree = $treeio->next_tree;
546         isa_ok($tree, 'Bio::Tree::TreeI');
547         if ($verbose > 0) {
548           diag("tree id: ",$tree->id);
549         }
550         my $node = $tree->get_root_node;
551         my @leaves;
552         my @children = ($node);
553         for (@children) {
554           push @children, $_->each_Descendent();
555         }
556         for (@children) {
557           push @leaves, $_ if $_->is_Leaf;
558         }
559         my ($D) = $leaves[0];
560         my ($point) = $treeio->read_annotation('-obj'=>$D, '-path'=>'distribution/point/geodetic_datum', '-attr'=>1);
561         is ($point, 'WGS84');
562         my ($lat) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'distribution/point/lat');
563         my ($long) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'distribution/point/long');
564         my ($alt) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'distribution/point/alt');
565         is ($lat, '32.880933');
566         is ($long, '-117.217543');
567         is ($alt, '104');
568         if ($verbose > 0) {
569           diag("node distribution lat: $lat long $long alt $alt");
570         }
571         
572         
573   # write_tree
574         if ($verbose > 0) {
575           diag("\ntest write_tree");
576         }
577         my $FILE1 = test_output_file();
578         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
579                 -format => 'phyloxml',
580                 -file   => ">$FILE1");
581         $treeout->write_tree($tree);
582         ok -s $FILE1;
583         if ($verbose > 0) {
584           diag(`cat $FILE1`);
585         }
586   }
587   
588   # tree12: date information
589   # <date> <desc> <value>
590   {
591         if ($verbose > 0) {
592           diag("\ntree12: date");
593         }
594         my $tree = $treeio->next_tree;
595         isa_ok($tree, 'Bio::Tree::TreeI');
596         if ($verbose > 0) {
597           diag("tree id: ",$tree->id);
598         }
599         my $node = $tree->get_root_node;
600         my @leaves;
601         my @children = ($node);
602         for (@children) {
603           push @children, $_->each_Descendent();
604         }
605         for (@children) {
606           push @leaves, $_ if $_->is_Leaf;
607         }
608   my ($D) = $tree->find_node(-id => 'A');
609         my ($dateunit) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/unit', '-attr'=>1);
610         my ($datemin) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/minimum' );
611         my ($datemax) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/maximum' );
612         my ($datevalue) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/value');
613         is ($dateunit, 'mya');
614         is ($datemin, '416.0');
615         is ($datemax, '443.7');
616         is ($datevalue, '425');
617         if ($verbose > 0) {
618           diag("node date unit: $dateunit min $datemin max $datemax value $datevalue");
619         }
620   
621   # write_tree
622         if ($verbose > 0) {
623           diag("\ntest write_tree");
624         }
625         my $FILE1 = test_output_file();
626         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
627                 -format => 'phyloxml',
628                 -file   => ">$FILE1");
629         $treeout->write_tree($tree);
630         ok -s $FILE1;
631         if ($verbose > 0) {
632           diag(`cat $FILE1`);
633         }
634   }
635   
636   # tree13: alignment outside <phylogeny>
637   # <align:alignment> <seq> 
638   {
639         if ($verbose > 0) {
640           diag("\ntree13: alignment outside  <phylogeny>");
641         }
642         my $tree = $treeio->next_tree;
643         isa_ok($tree, 'Bio::Tree::TreeI');
644         if ($verbose > 0) {
645           diag("tree id: ",$tree->id);
646         }
647         my $leaves_string = $tree->simplify_to_leaves_string();
648         if ($verbose > 0) {
649           diag($leaves_string);
650         }
651         is($leaves_string, '((A,B),C)');
652   
653   # add annotation in phyloxml
654         if ($verbose > 0) {
655           diag("test add annotation in phyloXML format");
656         }
657         my $node = $tree->get_root_node;
658         my @leaves;
659         my @children = ($node);
660         for (@children) {
661           push @children, $_->each_Descendent();
662         }
663         for (@children) {
664           push @leaves, $_ if $_->is_Leaf;
665         }
666         my ($D) = $leaves[0];
667         isa_ok($D, 'Bio::Tree::AnnotatableNode');
668         $treeio->add_phyloXML_annotation(
669                           -obj => $D, 
670                           -xml => "            <name>D</name>
671                           <date unit=\"mya\">
672                                  <desc>my date</desc>
673                                  <value>manymany million years</value>
674                           </date>
675   "
676                           );
677         my ($dateunit) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/unit', '-attr'=>1);
678         my ($datevalue) =  $treeio->read_annotation('-obj'=>$D, '-path'=>'date/value');
679         is ($dateunit, 'mya');
680         is ($datevalue, 'manymany million years');
681   
682   # write_tree
683         if ($verbose > 0) {
684           diag("\ntest write_tree");
685         }
686         my $FILE1 = test_output_file();
687         my $treeout = Bio::TreeIO->new(-verbose => $verbose,
688                 -format => 'phyloxml',
689                 -file   => ">$FILE1");
690         $treeout->write_tree($tree);
691         ok -s $FILE1;
692         if ($verbose > 0) {
693           diag(`cat $FILE1`);
694         }
695   }
696   
697   
698   # tree14: convert between nhx and phyloxml
699   # convert between nhx-phyloxml
700   {
701         if ($verbose > 0) {
702           diag("\n test translation between nhx and phyloxml");
703         }
704         ok my $nhxio = Bio::TreeIO->new(
705                 -verbose => $verbose,
706                 -format => 'nhx',
707                 -file   => test_input_file('test.nhx'));
708         my $tree = $nhxio->next_tree;
709         isa_ok($tree, 'Bio::Tree::TreeI');
710         my $FILE1 = test_output_file();
711         my $phyloxmlio = Bio::TreeIO->new(-verbose => $verbose,
712                 -format => 'phyloxml',
713                 -file   => ">$FILE1");
714         $phyloxmlio->write_tree($tree);
715         ok -s $FILE1;
716         if ($verbose > 0) {
717           diag(`cat $FILE1`);
718         }
719   }
721   # to-do 1. validation
722 TODO: {
723   local $TODO = 'validation not implemented yet';
725   my $xsd = "http://www.phyloxml.org/1.10/phyloxml.xsd";
726 #  for my $XSD ($xsd, XML::LibXML::Schema->new(location => $xsd)) {
727 #    {
728 #      my $reader = new XML::LibXML::Reader(
729 #  location => "test/schema/demo.xml",
730 #  Schema => $XSD,
731 #       );
732 #      ok($reader->finish, "validate using ".(ref($XSD) ? 'XML::LibXML::Schema' : 'Schema file'));
733 #    }
734 #    {
735 #      my $reader = new XML::LibXML::Reader(
736 #  location => "test/schema/invaliddemo.xml",
737 #  Schema => $XSD,
738 #       );
739 #      eval { $reader->finish };
740 #      ok($@, "catch validation error for ".(ref($XSD) ? 'XML::LibXML::Schema' : 'Schema file'));
741 #    }
743 #  }
746