/usr/share/perl5/Bio/Tools/Run/Phylo/PhyloBase.pm is in libbio-perl-run-perl 1.7.1-3.
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 | #
# BioPerl module for Bio::Tools::Run::Phylo::PhyloBase
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Sendu Bala <bix@sendu.me.uk>
#
# Copyright Sendu Bala
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::Tools::Run::Phylo::PhyloBase- base module for phylo wrappers
=head1 SYNOPSIS
use base qw(Bio::Tools::Run::Phylo::PhyloBase);
=head1 DESCRIPTION
For use by Bio::Tools::Run::Phylo modules as a base in place of
Bio::Tools::Run::WrapperBase.
This is based on WrapperBase but provides additional phylo-related private
helper subs.
=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:
http://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Sendu Bala
Email bix@sendu.me.uk
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
package Bio::Tools::Run::Phylo::PhyloBase;
use strict;
use Bio::AlignIO;
use Bio::TreeIO;
use base qw(Bio::Tools::Run::WrapperBase);
=head2 _alignment
Title : _alignment
Usage : $aln = $obj->_alignment()
Function: Get/set an alignment object, generating one from a file if desired.
Returns : Bio::Align::AlignI (probably a Bio::SimpleAlign)
Args : none to get
OR filename & input format of the alignment file (latter defaults to
guess) to set from file
OR Bio::Align::AlignI to set
=cut
sub _alignment {
my ($self, $thing, $format) = @_;
if (ref($thing) && $thing->isa('Bio::Align::AlignI')) {
$self->{_align_obj} = $thing;
}
elsif ($thing && -e $thing) {
my $align_in = Bio::AlignIO->new(-verbose => $self->verbose, -file => $thing, $format ? (-format => $format) : ());
my $aln = $align_in->next_aln || $self->throw("Alignment file '$thing' had no alignment!");
$align_in->close();
$self->{_align_obj} = $aln;
}
return $self->{_align_obj};
}
=head2 _write_alignment
Title : _write_alignment
Usage : $obj->_write_alignment()
Function: Writes the alignment object returned by _alignment() out in the
desired format to a temp file.
Returns : filename
Args : string to desribe format (default 'fasta'), any other options to pass
to AlignIO
=cut
sub _write_alignment {
my ($self, $format, @options) = @_;
my $align = $self->_alignment || $self->throw("_write_alignment called when _alignment had not been set");
$format ||= 'fasta';
my ($tfh, $tempfile) = $self->io->tempfile(-dir => $self->tempdir);
my $out = Bio::AlignIO->new(-verbose => $self->verbose, '-fh' => $tfh, '-format' => $format, @options);
$align->set_displayname_flat;
$out->write_aln($align);
$out->close();
$out = undef;
close($tfh);
undef $tfh;
return $tempfile;
}
=head2 _tree
Title : _tree
Usage : $tree = $obj->_tree()
Function: Get/set a tree object, generating one from a file/database if desired
Returns : Bio::Tree::TreeI
Args : none to get, OR to set:
OR filename & input format of the tree file (latter defaults to
guess) to set from file
OR Bio::Tree::TreeI
OR Bio::DB::Taxonomy when _alignment() has been set and where
sequences in the alignment have ids matching species in the taxonomy
database
=cut
sub _tree {
my ($self, $thing, $format) = @_;
if ($thing) {
my $tree;
if (ref($thing) && $thing->isa('Bio::Tree::TreeI')) {
$tree = $thing;
}
elsif (ref($thing) && $thing->isa('Bio::DB::Taxonomy')) {
# get all the alignment sequence names
my @species_names = $self->_get_seq_names;
$tree = $thing->get_tree(@species_names);
# convert node ids to their seq_ids for correct output with TreeIO
foreach my $node ($tree->get_nodes) {
my $seq_id = $node->name('supplied');
$seq_id = $seq_id ? shift @{$seq_id} : ($node->node_name ? $node->node_name : $node->id);
$node->id($seq_id);
}
}
elsif (-e $thing) {
my $tree_in = Bio::TreeIO->new(-verbose => $self->verbose, -file => $thing, $format ? (-format => $format) : ());
$tree = $tree_in->next_tree || $self->throw("Tree file '$thing' had no tree!");
$tree_in->close;
}
$self->{_tree_obj} = $tree || $self->throw("'$thing' supplied but unable to generate a tree from it");
}
return $self->{_tree_obj};
}
=head2 _write_tree
Title : _write_tree
Usage : $obj->_write_tree()
Function: Writes the tree object returned by _tree() out in the desired format
to a temp file.
Returns : filename
Args : string to desribe format (default 'newick')
=cut
sub _write_tree {
my ($self, $format) = @_;
my $tree = $self->_tree || $self->throw("_write_tree called when _tree had not been set");
$format ||= 'newick';
my ($tfh, $tempfile) = $self->io->tempfile(-dir => $self->tempdir);
my $out = Bio::TreeIO->new(-verbose => $self->verbose, -fh => $tfh, -format => $format);
$out->write_tree($tree);
$out->close();
$out = undef;
close($tfh);
undef $tfh;
return $tempfile;
}
=head2 _get_seq_names
Title : _get_seq_names
Usage : @names = $obj->_get_seq_names()
Function: Get all the sequence names (from id()) of the sequenes in the
alignment. _alignment() must be set prior to calling this.
Returns : list of strings (seq ids)
Args : none
=cut
sub _get_seq_names {
my $self = shift;
my $aln = $self->_alignment || $self->throw("_get_seq_names called when _alignment had not been set");
my @names;
foreach my $seq ($aln->each_seq) {
push(@names, $seq->id);
}
return @names;
}
=head2 _get_node_names
Title : _get_node_names
Usage : @names = $obj->_get_node_names()
Function: Get all the node names (from id()) of the nodes in the tree.
_tree must be set prior to calling this.
Returns : list of strings (node ids)
Args : none
=cut
sub _get_node_names {
my $self = shift;
my $tree = $self->_tree || $self->throw("_get_node_names called when _tree had not been set");
my @names;
foreach my $node ($tree->get_leaf_nodes) {
push(@names, $node->id);
}
return @names;
}
=head2 _check_names
Title : _check_names
Usage : if ($obj->_check_names) { ... }
Function: Determine if all sequences in the alignment file have a corresponding
node in the tree file. _alignment() and _tree() must be set
prior to calling this.
Returns : boolean (will also warn about the specific problems when returning
false)
Args : none
=cut
sub _check_names {
my $self = shift;
my @seq_names = $self->_get_seq_names;
my %node_names = map { $_ => 1 } $self->_get_node_names;
# (not interested in tree nodes that don't map to sequence, since we
# expect the tree to have internal nodes not represented by sequence)
foreach my $name (@seq_names) {
$self->{_unmapped}{$name} = 1 unless defined $node_names{$name};
}
if (defined($self->{_unmapped})) {
my $count = scalar(keys %{$self->{_unmapped}});
my $unmapped = join(", ", keys %{$self->{_unmapped}});
$self->warn("$count unmapped ids between the supplied alignment and tree: $unmapped");
return 0;
}
return 1;
}
1;
|