| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | |||
| 4 | ###################################################################### | ||
| 5 | # nex2text_tree.pl -- Visulation of tree as strings | ||
| 6 | ###################################################################### | ||
| 7 | # | ||
| 8 | # $Author: thladish $ | ||
| 9 | # $Date: 2006/08/24 06:41:57 $ | ||
| 10 | # $Revision: 1.7 $ | ||
| 11 | # $Id: nexplot.pl,v 1.33 2006/05/17 21:45:52 thladish Exp | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | |||
| 15 | use Data::Dumper; | ||
| 16 | use Bio::NEXUS; | ||
| 17 | |||
| 18 | use Getopt::Long; | ||
| 19 | use Pod::Usage; | ||
| 20 | |||
| 21 | |||
| 22 | ## INPUT PARAMETERS | ||
| 23 | my $verticalOtuSpacing = 4; # Even number increments are better for the binary trees | ||
| 24 | my $lowerYbound = 0; | ||
| 25 | my $lowerXbound = 5; | ||
| 26 | my $treeWidth = 30; | ||
| 27 | my $cladogram_type = 0; # 0 or 'normal' or 'accelerated' | ||
| 28 | |||
| 29 | |||
| 30 | ### Options parsing and processing using Getopts### | ||
| 31 | |||
| 32 | my %h; | ||
| 33 | |||
| 34 | my $opt_result; | ||
| 35 | |||
| 36 | local $SIG{__WARN__} = sub { die $_[0] }; ## Warning signal to die signal for capturing by warnings by 'eval' function | ||
| 37 | eval { | ||
| 38 | GetOptions (\%h, 'manual|m', 'help|h', 'demo', 'tree_width|w=i','otu_spacing|s=i', 'input|i=s','output|o=s', 'version|V','verbose','cladogram|c=s'); | ||
| 39 | }; | ||
| 40 | |||
| 41 | if ($@ ne '') { | ||
| 42 | die "Options parsing error : $@-- use \'perl nex2text_tree.pl --help\' for correct options\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | if ($h{'cladogram'}) { | ||
| 46 | die ( "Only 'normal', 'accelerated' options supported : see \'perl nex2text_tree.pl --help\' for correct options \n" ) if not ($h{'cladogram'} =~/accelerated/i or $h{'cladogram'} =~/normal/i); | ||
| 47 | $cladogram_type = lc($h{'cladogram'}); | ||
| 48 | } | ||
| 49 | local $SIG{__WARN__} = sub { warn $_[0] }; ## turning back warning signal | ||
| 50 | |||
| 51 | if ( exists $h{tree_width} ) { $treeWidth = $h{'tree_width'}} | ||
| 52 | if ( exists $h{otu_spacing} ) { $verticalOtuSpacing = $h{'otu_spacing'}} | ||
| 53 | |||
| 54 | pod2usage(1) if $h{'help'}; | ||
| 55 | pod2usage(-exitstatus => 0, -verbose => 2) if $h{man}; | ||
| 56 | die '$Id: nex2text_tree.pl,v 1.7 2006/08/24 06:41:57 thladish Exp $',"\n" if ( $h{ 'V' } ); | ||
| 57 | die "Dataset not provided : Input file option(--input) or demo option (--demo) must be specified.\n" if ((not exists($h{input})) and (not exists ($h{demo}))); | ||
| 58 | |||
| 59 | my $input_file = $h{'input'}; | ||
| 60 | my $output_file = $h{'output'}; | ||
| 61 | my $out_file_h; | ||
| 62 | |||
| 63 | print "#" x 50,"\n" if $h{'verbose'}; | ||
| 64 | |||
| 65 | open (INP, $input_file) || die "Cannot open input file : $input_file\n" if (not $h{demo}); | ||
| 66 | if ($output_file) { | ||
| 67 | open (OUT, ">$output_file") || die "Cannot open output file for writing : $output_file\n"; | ||
| 68 | $out_file_h = \*OUT; | ||
| 69 | print "Output filename :$out_file_h\n" if $h{'verbose'}; | ||
| 70 | }else { | ||
| 71 | $out_file_h = \*STDOUT; | ||
| 72 | } | ||
| 73 | |||
| 74 | ##OTHER VARIABLEs | ||
| 75 | my $maxNodeWidth = 0; # maximum node with used for calculating the X bound of the plot | ||
| 76 | my $treeHeight = 0; | ||
| 77 | my $upperYbound = 0; | ||
| 78 | my $upperXbound = 0; | ||
| 79 | my $tree_string; # Contains the tree as string ( upperXbound by upperYbound matrix) | ||
| 80 | |||
| 81 | my $tree; | ||
| 82 | |||
| 83 | ### Obtaining the Tree object to be converted to text. | ||
| 84 | |||
| 85 | if ($h{'demo'}) { | ||
| 86 | ## Demo datasets | ||
| 87 | #TREE con_50_majrule = (Arabidopsis_thaliana_CAA42069.1:0.135290,Rosellinia_necatrix_BAC54258.1:0.129096,(Fusarium_oxysporum_BAA85768.1:0.088840,((Emericella_nidulans_AAB50255.1:0.197013,((((Debaryomyces_occidentalis_CAA37787.1:0.046199,Pichia_stipitis_AAB86817.3:0.052188)inode8:0.040288[0.96],Candida_albicans_AAB68996.1:0.031209)inode7:0.025670[0.82],((Kluyveromyces_lactis_CAA43224.1:0.070408,((Saccharomyces_cerevisiae_CAA89576.1:0.047955,Saccharomyces_cerevisiae_AAB65003.1:0.137095)inode12:0.047348[0.67],Candida_glabrata_CAA41203.1:0.083197)inode11:0.099385[1.00])inode10:0.052048[0.63],Pachysolen_tannophilus_AAD02430.1:0.101883)inode9:0.043340[0.61])inode6:0.136023[1.00],Schizosaccharomyces_pombe_CAB41053.1:0.144540)inode5:0.080044[0.90])inode4:0.067075[0.70],(((((((Tigriopus_californicus_AAC80533.1:0.008887,Tigriopus_californicus_AAC80535.1:0.018489,Tigriopus_californicus_AAC80537.1:0.017438)inode19:0.023821[0.71],(Tigriopus_californicus_AAC80553.1:0.024848,Tigriopus_californicus_AAC80552.1:0.009379)inode20:0.027883[0.82])inode18:0.134497[0.97],Drosophila_melanogaster_AAF53553.1:0.207983,(((Chaetoceros_gracilis_BAC54099.1:0.119229,(Chlorella_vulgaris_BAC76447.1:0.315427,Chlorella_vulgaris_BAC76448.1:0.323294,Chlamydomonas_reinhardtii_AAB00729.1:0.303235)inode24:0.335171[1.00])inode23:0.160784[0.76],Phaeodactylum_tricornutum_AAO43197.1:0.089007)inode22:0.913134[1.00],Arabidopsis_thaliana_BAB10887.1:1.406471)inode21:1.038263[0.99])inode17:0.079735[0.59],((Mus_musculus_CAA25899.1:0.008307,Rattus_norvegicus_AAA21711.1:0.009662)inode26:0.024280[0.74],(Gallus_gallus_CAA25046.1:0.055226,Rattus_norvegicus_AAA41015.1:0.117358)inode27:0.040335[0.69])inode25:0.052721[0.84])inode16:0.075053[0.63],Drosophila_melanogaster_AAF53554.1:0.069168)inode15:0.079006[0.67],(((((Arabidopsis_thaliana_AAB72175.1:0.064459,Arabidopsis_thaliana_CAB78127.1:0.023524)inode32:0.064349[0.92],Oryza_sativa_AAA63515.1:0.038470)inode31:0.060944[0.86],Oryza_sativa_BAB90158.1:0.225540)inode30:0.137772[0.99],Chlamydomonas_reinhardtii_CAB16954.1:0.152376)inode29:0.152746[0.99],((Caenorhabditis_elegans_CAA98555.1:0.177383,Caenorhabditis_elegans_AAB92035.1:0.058081)inode34:0.326057[0.98],Plasmodium_falciparum_AAN36650.1:0.301053)inode33:0.094984[0.71])inode28:0.091560[0.91])inode14:0.106401[0.56],Dictyostelium_discoideum_AAO53091.1:0.414927)inode13:0.105943[0.79])inode3:0.087158[0.96])inode2:0.069976[0.98])root; | ||
| 88 | #TREE Tree = (Plasmodium_falciparum_16805076:1.800,(Arabidopsis_thaliana_18399137:1.642,((Schizosaccharomyces_pombe_19115679:0.837,Saccharomyces_cerevisiae_6323318:0.837)inode3:0.705,(Caenorhabditis_elegans_17563246:1.220,(Homo_sapiens_4503659:1.063,(Anopheles_gambiae_agCT48044:0.250,Drosophila_melanogaster_7300667:0.250)inode6:0.813)inode5:0.157)inode4:0.322)inode2:0.100)inode1:0.158)root:.2; | ||
| 89 | my $tr=<<STRING; | ||
| 90 | BEGIN TREES; | ||
| 91 | TREE Tree = (Plasmodium_falciparum_16805076:1.800,(Arabidopsis_thaliana_18399137:1.642,((Schizosaccharomyces_pombe_19115679:0.837,Saccharomyces_cerevisiae_6323318:0.837)inode3:0.705,(Caenorhabditis_elegans_17563246:1.220,(Homo_sapiens_4503659:1.063,(Anopheles_gambiae_agCT48044:0.250,Drosophila_melanogaster_7300667:0.250)inode6:0.813)inode5:0.157)inode4:0.322)inode2:0.100)inode1:0.158)root:.2; | ||
| 92 | END; | ||
| 93 | STRING | ||
| 94 | |||
| 95 | # TreesBlock object creation | ||
| 96 | my $tree_b = new Bio::NEXUS::TreesBlock; | ||
| 97 | $tree_b->parse_block($tr); | ||
| 98 | $tree = $tree_b->get_tree; | ||
| 99 | } else { | ||
| 100 | |||
| 101 | my $nexus = new Bio::NEXUS($input_file); | ||
| 102 | $tree = $nexus->get_block("Trees")->get_tree; | ||
| 103 | } | ||
| 104 | |||
| 105 | ### Processing of the Tree | ||
| 106 | |||
| 107 | my $root = $tree->get_rootnode; | ||
| 108 | |||
| 109 | |||
| 110 | # Setting x and y coordiates for all the nodes based on the Tree | ||
| 111 | $tree->_set_xcoord($treeWidth,$cladogram_type); | ||
| 112 | $tree->_set_ycoord($lowerYbound,$verticalOtuSpacing); | ||
| 113 | my @nodes = @{$tree->node_list()}; | ||
| 114 | |||
| 115 | print "Width of Tree : ", $treeWidth,"\n" if $h{'verbose'}; | ||
| 116 | print "Vertical spacing : ", $verticalOtuSpacing,"\n" if $h{'verbose'}; | ||
| 117 | print "Name of tree : ", $tree->name,"\n" if $h{'verbose'}; | ||
| 118 | print "No. of nodes : ", $#nodes +1,"\n" if $h{'verbose'}; | ||
| 119 | print "No. of OTUs : ", scalar @{$tree->otu_list},"\n" if $h{'verbose'}; | ||
| 120 | print "Newick Format \n", $tree->as_string,"\n\n" if $h{'verbose'}; | ||
| 121 | print "#" x 50,"\n\n" if $h{'verbose'}; | ||
| 122 | |||
| 123 | my @sorted; | ||
| 124 | for my $node (@nodes) { | ||
| 125 | push @sorted, $node->xcoord(); | ||
| 126 | } | ||
| 127 | @sorted = sort { $a <=> $b } @sorted; | ||
| 128 | |||
| 129 | my $amp = $treeWidth / pop @sorted; # unit of branch length | ||
| 130 | for my $node (@nodes) { | ||
| 131 | $node->_set_xcoord( $lowerXbound + ($node->xcoord() * $amp) ); | ||
| 132 | } | ||
| 133 | |||
| 134 | # Setting x and y bounds for the plot | ||
| 135 | &__find_treemax($root); | ||
| 136 | $upperYbound = $treeHeight + 5; | ||
| 137 | $upperXbound = $treeWidth + $maxNodeWidth; | ||
| 138 | for(my $i = 0; $i < int($upperYbound) ; $i++) { | ||
| 139 | $tree_string->[$i] = [split(/1/,' 1' x $upperXbound)]; | ||
| 140 | #print $#{$tree_string->[$i]} , "\n"; | ||
| 141 | } | ||
| 142 | #print "Tree height - $treeHeight Tree width - $treeWidth\n\n"; | ||
| 143 | &__print_tree($root,$lowerXbound,$lowerYbound); | ||
| 144 | |||
| 145 | ## Printing the tree to the output | ||
| 146 | for(my $i = 0; $i < int($upperYbound) ; $i++) { ## No of rows (height) | ||
| 147 | print $out_file_h join '', @{$tree_string->[$i]} , "\n"; | ||
| 148 | } | ||
| 149 | |||
| 150 | close OUT; | ||
| 151 | close INP; | ||
| 152 | ###### End of the program | ||
| 153 | |||
| 154 | |||
| 155 | # + ------------------ Pl | ||
| 156 | # | +---------- Ar | ||
| 157 | # --| | | ||
| 158 | # |----| | ||
| 159 | # | +------------- Sc | ||
| 160 | |||
| 161 | |||
| 162 | sub __print_tree { | ||
| 163 | my ($node, $x0, $y0) = @_; | ||
| 164 | my $name = $node->name(); | ||
| 165 | my $is_otu = $node->is_otu; | ||
| 166 | my $x1 = int ($node->xcoord); | ||
| 167 | my $y1 = int ($node->ycoord); | ||
| 168 | |||
| 169 | my $i; | ||
| 170 | my $j; | ||
| 171 | my $delta; | ||
| 172 | #print " $name x0=$x0 y0=$y0 -- x1=$x1 y1=$y1 \n"; | ||
| 173 | $i = $x1 - $x0; | ||
| 174 | $delta = ($i > 0 ) ? 1 : -1; | ||
| 175 | until ($i == 0 ) { # Horizontal lines | ||
| 176 | $j = $x0 + $i; | ||
| 177 | if ($j==$x1 and (not $is_otu)) { | ||
| 178 | $$tree_string[$y1][$j] = "+"; | ||
| 179 | }else { | ||
| 180 | $$tree_string[$y1][$j] = "-"; | ||
| 181 | } | ||
| 182 | #print " horizontal - j=$j,y0=$y0 \n"; | ||
| 183 | $i = $i - $delta; | ||
| 184 | } # end of until | ||
| 185 | |||
| 186 | unless ($node->name eq 'root') { | ||
| 187 | $i = $y1 - $y0; | ||
| 188 | $delta = ($i >= 0 ) ? 1 : -1; | ||
| 189 | until ($i == 0 ) { # Vertical lines of the tree ( only for the non-root nodes) | ||
| 190 | $j = $y0 + $i; | ||
| 191 | if ($j==$y1) { | ||
| 192 | #$$tree_string[$j][$x0] = ($delta > 0) ? "\\" : "/"; | ||
| 193 | $$tree_string[$j][$x0] = "+"; | ||
| 194 | }else { | ||
| 195 | $$tree_string[$j][$x0] = "|"; | ||
| 196 | } | ||
| 197 | #print " vertical -i=$i x0=$x0,j=$j \n"; | ||
| 198 | $i = $i - $delta; | ||
| 199 | } #end of until | ||
| 200 | } # end of unless | ||
| 201 | |||
| 202 | if ($node->is_otu()) { | ||
| 203 | $$tree_string[$y1][$x1 + 2] = $name; | ||
| 204 | }else { | ||
| 205 | # $$tree_string[$y1][$x1] = $name; | ||
| 206 | } | ||
| 207 | if (not $node->is_otu) { | ||
| 208 | foreach my $child (@{$node->children()} ) { | ||
| 209 | &__print_tree($child, $x1, $y1); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | # Finds the maximum x and y ranges of the tree (Obtained from nexplot.pl) | ||
| 215 | sub __find_treemax { | ||
| 216 | my $node = shift; | ||
| 217 | my $name = $node->name(); | ||
| 218 | my $x1 = int ($node->xcoord); | ||
| 219 | my $y1 = int ($node->ycoord); | ||
| 220 | if ($node->is_otu()) { | ||
| 221 | my ($x, $y) = ($x1 + 7.5, $y1); # For each item, computes branch length with name and compares to highest known value | ||
| 222 | |||
| 223 | $treeHeight=$y if ($y > $treeHeight); | ||
| 224 | $maxNodeWidth=length($name) if ($maxNodeWidth < length($name)); | ||
| 225 | |||
| 226 | } | ||
| 227 | if (not $node->is_otu) { | ||
| 228 | foreach my $child (@{$node->children()} ) { | ||
| 229 | &__find_treemax($child); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # Picks first OTU and finds treemax plus character length, that is, | ||
| 235 | # the x range of the entire plot (Obtained from nexplot.pl) | ||
| 236 | sub __find_xbound { | ||
| 237 | my ($node) = @_; | ||
| 238 | my $name = $node->name(); | ||
| 239 | my $x1 = int ($node->xcoord); | ||
| 240 | my $y1 = int ($node->ycoord); | ||
| 241 | if (not $node->is_otu()) { | ||
| 242 | foreach my $child (@{$node->children()} ) { | ||
| 243 | return 1 if &__find_xbound($child, $x1, $y1); | ||
| 244 | } | ||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | my ($x, $y) = ($x1 + 7.5, $y1 - 2.5); | ||
| 248 | return 0; | ||
| 249 | } | ||
| 250 | |||
| 251 | __DATA__ |