/usr/share/perl5/Bio/TreeIO/nexus.pm is in libbio-perl-perl 1.6.923-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | #
# BioPerl module for Bio::TreeIO::nexus
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Jason Stajich <jason-at-open-bio-dot-org>
#
# Copyright Jason Stajich
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::TreeIO::nexus - A TreeIO driver module for parsing Nexus tree output from PAUP
=head1 SYNOPSIS
use Bio::TreeIO;
my $in = Bio::TreeIO->new(-file => 't/data/cat_tre.tre');
while( my $tree = $in->next_tree ) {
}
=head1 DESCRIPTION
This is a driver module for parsing PAUP Nexus tree format which
basically is just a remapping of trees.
=head2 Comments
The nexus format allows node comments that are placed inside square
brackets. Usually the comments (implemented as tags for nodes) are
used to give a name for an internal node or record the bootstap value,
but other uses are possible.
The FigTree program by Andrew Rambaut adds various rendering
parameters inside comments and flags these comments by starting them
with '&!'. The parameters implemented here are 'label' and 'color'.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
https://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Jason Stajich
Email jason-at-open-bio-dot-org
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::TreeIO::nexus;
use strict;
use Bio::Event::EventGeneratorI;
use IO::String;
use base qw(Bio::TreeIO);
=head2 new
Title : new
Args : -header => boolean default is true
print/do not print #NEXUS header
-translate => boolean default is true
print/do not print Node Id translation to a number
=cut
sub _initialize {
my $self = shift;
$self->SUPER::_initialize(@_);
my ( $hdr, $trans ) = $self->_rearrange(
[
qw(HEADER
TRANSLATE)
],
@_
);
$self->header( defined $hdr ? $hdr : 1 );
$self->translate_node( defined $trans ? $trans : 1 );
}
=head2 next_tree
Title : next_tree
Usage : my $tree = $treeio->next_tree
Function: Gets the next tree in the stream
Returns : Bio::Tree::TreeI
Args : none
=cut
sub next_tree {
my ($self) = @_;
unless ( $self->{'_parsed'} ) {
$self->_parse;
}
return $self->{'_trees'}->[ $self->{'_treeiter'}++ ];
}
sub rewind {
shift->{'_treeiter'} = 0;
}
sub _parse {
my ($self) = @_;
$self->{'_parsed'} = 1;
$self->{'_treeiter'} = 0;
while ( defined( $_ = $self->_readline ) ) {
next if /^\s+$/;
last;
}
return unless ( defined $_ );
unless (/^\#NEXUS/i) {
$self->warn("File does not start with #NEXUS"); #'
return;
}
my $line;
while ( defined( $_ = $self->_readline ) ) {
$line .= $_;
}
my @sections = split( /#NEXUS/i, $line );
for my $s (@sections) {
my %translate;
if ( $self->verbose > 0 ) {
while ( $s =~ s/(\[[^\]]+\])// ) {
$self->debug("removing comment $1\n");
}
}
else {
$s =~ s/(\[[^\]]+\])//g;
}
if ( $s =~ /begin trees;(.+)(end;)?/si ) {
my $trees = $1;
if ( $trees =~ s/\s+translate\s+([^;]+);//i ) {
my @trans;
my $tr = $1;
while ($tr =~ m{\s*([^,\s]+?\s+(?:'[^']+'|[^'\s]+)),?}gc) {
push @trans, $1;
}
for my $n ( @trans ) {
if ($n =~ /^\s*(\S+)\s+(.+)$/) {
my ($id,$tag) = ($1,$2);
$tag =~ s/[\s,]+$//; # remove the extra spaces of the last taxon
$translate{$id} = $tag;
}
}
}
else {
$self->debug("no translate in: $trees\n");
}
while ($trees =~ /\s+tree\s+\*?\s*(\S+)\s*\=
\s*(?:\[\S+\])?\s*([^\;]+;)/igx)
{
my ( $tree_name, $tree_str ) = ( $1, $2 );
# MrBayes does not print colons for node label
# $tree_str =~ s/\)(\d*\.\d+)\)/:$1/g;
my $buf = IO::String->new($tree_str);
my $treeio = Bio::TreeIO->new(
-format => 'newick',
-fh => $buf
);
my $tree = $treeio->next_tree;
foreach my $node ( grep { $_->is_Leaf } $tree->get_nodes ) {
my $id = $node->id;
my $lookup = $translate{$id};
$node->id( $lookup || $id );
}
$tree->id($tree_name) if defined $tree_name;
push @{ $self->{'_trees'} }, $tree;
}
}
else {
$self->debug("begin_trees failed: $s\n");
}
}
if ( !@sections ) {
$self->debug("warn no sections: $line\n");
}
}
=head2 write_tree
Title : write_tree
Usage : $treeio->write_tree($tree);
Function: Writes a tree onto the stream
Returns : none
Args : Bio::Tree::TreeI
=cut
sub write_tree {
my ( $self, @trees ) = @_;
if ( $self->header ) {
$self->_print("#NEXUS\n\n");
}
my $translate = $self->translate_node;
my $time = localtime();
$self->_print( sprintf( "Begin trees; [Treefile created %s]\n", $time ) );
my ( $first, $nodecter, %node2num ) = ( 0, 1 );
foreach my $tree (@trees) {
if ( $first == 0
&& $translate )
{
$self->_print("\tTranslate\n");
$self->_print(
join(
",\n",
map {
$node2num{ $_->id } = $nodecter;
sprintf( "\t\t%d %s", $nodecter++, $_->id )
}
grep { $_->is_Leaf } $tree->get_nodes
),
"\n;\n"
);
}
my @data = _write_tree_Helper( $tree->get_root_node, \%node2num );
if ( $data[-1] !~ /\)$/ ) {
$data[0] = "(" . $data[0];
$data[-1] .= ")";
}
# by default all trees in bioperl are currently rooted
# something we'll try and fix one day....
$self->_print(
sprintf(
"\t tree %s = [&%s] %s;\n",
( $tree->id || sprintf( "Bioperl_%d", $first + 1 ) ),
( $tree->get_root_node ) ? 'R' : 'U',
join( ',', @data )
)
);
$first++;
}
$self->_print("End;\n");
$self->flush if $self->_flush_on_write && defined $self->_fh;
return;
}
sub _write_tree_Helper {
my ( $node, $node2num ) = @_;
return () if ( !defined $node );
my @data;
foreach my $n ( $node->each_Descendent() ) {
push @data, _write_tree_Helper( $n, $node2num );
}
if ( @data > 1 ) { # internal node
$data[0] = "(" . $data[0];
$data[-1] .= ")";
# FigTree comments start
my $comment_flag;
$comment_flag = 0
if ( $node->has_tag('color') or $node->has_tag('label') );
$data[-1] .= '[&!' if defined $comment_flag;
if ( $node->has_tag('color')) {
my $color = $node->get_tag_values('color');
$data[-1] .= "color=$color";
$comment_flag++;
}
if ( $node->has_tag('label')) {
my $label = $node->get_tag_values('label');
$data[-1] .= ',' if $comment_flag;
$data[-1] .= 'label="'. $label. '"';
}
$data[-1] .= ']' if defined $comment_flag;
# FigTree comments end
# let's explicitly write out the bootstrap if we've got it
my $b;
my $bl = $node->branch_length;
if ( !defined $bl ) {
}
elsif ( $bl =~ /\#/ ) {
$data[-1] .= $bl;
}
else {
$data[-1] .= ":$bl";
}
if ( defined( $b = $node->bootstrap ) ) {
$data[-1] .= sprintf( "[%s]", $b );
}
elsif ( defined( $b = $node->id ) ) {
$b = $node2num->{$b} if ( $node2num->{$b} ); # translate node2num
$data[-1] .= sprintf( "[%s]", $b ) if defined $b;
}
}
else { # leaf node
if ( defined $node->id || defined $node->branch_length ) {
my $id = defined $node->id ? $node->id : '';
if ( length($id) && $node2num->{$id} ) {
$id = $node2num->{$id};
}
if ( $node->has_tag('color')) {
my ($color) = $node->get_tag_values('color');
$id .= "[&!color=$color\]";
}
push @data,
sprintf( "%s%s",
$id,
defined $node->branch_length
? ":" . $node->branch_length
: '' );
}
}
return @data;
}
=head2 header
Title : header
Usage : $obj->header($newval)
Function:
Example :
Returns : value of header (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub header {
my $self = shift;
return $self->{'header'} = shift if @_;
return $self->{'header'};
}
=head2 translate_node
Title : translate_node
Usage : $obj->translate_node($newval)
Function:
Example :
Returns : value of translate_node (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub translate_node {
my $self = shift;
return $self->{'translate_node'} = shift if @_;
return $self->{'translate_node'};
}
1;
|