Used quoted strings in Bio::Species to avoid a 'Invalid [] range in regex' error...
[bioperl-live.git] / t / Tree / TreeIO / nhx.t
bloba95307c703a109cacd7a95788e6c0d9935ee744e
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id: TreeIO.t 14580 2008-03-01 17:01:30Z cjfields $
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
9     use File::Temp qw(tempfile);
10     
11     test_begin(-tests => 19);
12     use_ok('Bio::TreeIO');
15 my $verbose = 0; #test_debug();
17 my $treeio = Bio::TreeIO->new(
18   -format => 'nhx',
19   -verbose => $verbose,
20   -file   => test_input_file('test.nhx'),
21   );
22 my $tree;
23 ok($treeio);
24 $tree = $treeio->next_tree;
25 isa_ok($tree, 'Bio::Tree::TreeI');
27 my @nodes = $tree->get_nodes;
28 is(@nodes, 12, "Total Nodes");
29 #print STDERR "TREE: ".$tree->as_text('nhx')."\n";
31 my $adhy = $tree->find_node('ADHY');
32 is($adhy->branch_length, 0.1);
33 is(($adhy->get_tag_values('S'))[0], 'nematode');
34 is(($adhy->get_tag_values('E'))[0], '1.1.1.1');
36 test_roundtrip('((a,b),c);','simple newick');
37 test_roundtrip('((x:0.05,y:0.06),a:0.1[&&NHX:G=dummy]);','bug 1471 test');
38 test_roundtrip('((x:0.05[&&NHX:label=x],y:0.06)[&&NHX:label=int_node],a:0.1[&&NHX:label=a]);','different combinations of label, NHX, and branch length');
40 test_roundtrip('(a:1,b:2,c:3,d:4)TEST:1.2345;','doot node branch length');
41 test_roundtrip('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F;','Example from Wikipedia');
43 test_roundtrip('(((ADH2:0.1[&&NHX:E=1.1.1.1:S=human],ADH1:0.11[&&NHX:E=1.1.1.1:S=human]):0.05[&&NHX:B=100:D=Y:E=1.1.1.1:S=Primates],ADHY:0.1[&&NHX:E=1.1.1.1:S=nematode],ADHX:0.12[&&NHX:E=1.1.1.1:S=insect]):0.1[&&NHX:D=N:E=1.1.1.1:S=Metazoa],(ADH4:0.09[&&NHX:E=1.1.1.1:S=yeast],ADH3:0.13[&&NHX:E=1.1.1.1:S=yeast],ADH2:0.12[&&NHX:E=1.1.1.1:S=yeast],ADH1:0.11[&&NHX:E=1.1.1.1:S=yeast]):0.1[&&NHX:S=Fungi])[&&NHX:D=N:E=1.1.1.1];','ADH NHX tree');
44 test_roundtrip('(gene1_Hu[&&NHX:S=Hu_Homo_sapiens],(gene2_Hu[&&NHX:S=Hu_Homo_sapiens],gene2_Mu[&&NHX:S=Mu_Mus_musculus]));','notung nhx example http://www.cs.cmu.edu/~aiton/split/Manual-2.6.master014.html');
45 test_roundtrip('(cow_gene1,(mouse_gene2,cow_gene2)[&&NHX:B=100]);','notung nhx bootstrap http://www.cs.cmu.edu/~aiton/split/Manual-2.6.master014.html');
47 # Read in some larger trees from data files...
48 test_roundtrip(read_file(test_input_file('nhx-bacteria.nhx')),'r-sig-phylo mailing list http://www.mail-archive.com/r-sig-phylo@r-project.org/msg00516.html');
49 test_roundtrip(read_file(test_input_file('ex1.nucl.nhx')),'treebest example nhx');
50 # Note: these files aren't reproduced exactly in their online form. We need to round-trip them once
51 # before including them in the test, because the ordering of annotation keys is not a well-defined
52 # part of the NHX format. Since nhx.pm sorts the keys before output, once they've been through
53 # one time, the ordering becomes stable.
54 test_roundtrip(read_file(test_input_file('wellcome_tol.nhx')),'Wellcome Trust ToL (from http://iphylo.blogspot.com/2009/02/thoughts-on-wellcome-interactive-tree.html)');
56 # Uncomment to run (takes a long time!!)
57 #test_roundtrip(read_file(test_input_file('tol-2010-02-18.nhx')),'Tolweb.org converted to NHX');
59 test_roundtrip(read_file(test_input_file('biorecipe.nhx')),'Biorecipes NHX file (http://www.biorecipes.com/Orthologues/StatusPage/pics/TreeEukaryota.nt)');
61 sub test_roundtrip {
62   my $string = shift;
63   my $desc = shift;
65   my $in = Bio::TreeIO->new(-format => 'nhx',
66                             -string => $string,
67                             -verbose => $verbose
68                             );
69   
70   my $t = $in->next_tree;
71   my $out;
72   if (defined $t) {
73     $out = $t->as_text('nhx');
74   }
76   $desc = "Roundtrip: $desc";
77   return is($out,$string,$desc);
80 sub read_file {
81   my $file = shift;
82   local $/=undef;
83   my $string;
84   open my $IN, '<', $file or die "Could not read file '$file': $!\n";
85   binmode $IN;
86   $string = <$IN>;
87   close $IN;
88   $string =~ s/\n//g;
89   $string =~ s/\r//g; # For files with Windows line-endings
90   #print STDERR "STR: $string\n";
91   return $string;