File Coverage

blib/lib/Bio/Tools/Run/Phylo/PhyloBase.pm
Criterion Covered Total %
statement 12 86 13.9
branch 0 24 0.0
condition 0 37 0.0
subroutine 4 11 36.3
pod n/a
total 16 158 10.1


line stmt bran cond sub pod time code
1             #
2             # BioPerl module for Bio::Tools::Run::Phylo::PhyloBase
3             #
4             # Please direct questions and support issues to
5             #
6             # Cared for by Sendu Bala
7             #
8             # Copyright Sendu Bala
9             #
10             # You may distribute this module under the same terms as perl itself
11              
12             # POD documentation - main docs before the code
13              
14             =head1 NAME
15              
16             Bio::Tools::Run::Phylo::PhyloBase- base module for phylo wrappers
17              
18             =head1 SYNOPSIS
19              
20             use base qw(Bio::Tools::Run::Phylo::PhyloBase);
21              
22             =head1 DESCRIPTION
23              
24             For use by Bio::Tools::Run::Phylo modules as a base in place of
25             Bio::Tools::Run::WrapperBase.
26              
27             This is based on WrapperBase but provides additional phylo-related private
28             helper subs.
29              
30             =head1 FEEDBACK
31              
32             =head2 Mailing Lists
33              
34             User feedback is an integral part of the evolution of this and other
35             Bioperl modules. Send your comments and suggestions preferably to
36             the Bioperl mailing list. Your participation is much appreciated.
37              
38             bioperl-l@bioperl.org - General discussion
39             http://bioperl.org/wiki/Mailing_lists - About the mailing lists
40              
41             =head2 Support
42              
43             Please direct usage questions or support issues to the mailing list:
44              
45             I
46              
47             rather than to the module maintainer directly. Many experienced and
48             reponsive experts will be able look at the problem and quickly
49             address it. Please include a thorough description of the problem
50             with code and data examples if at all possible.
51              
52             =head2 Reporting Bugs
53              
54             Report bugs to the Bioperl bug tracking system to help us keep track
55             of the bugs and their resolution. Bug reports can be submitted via
56             the web:
57              
58             http://redmine.open-bio.org/projects/bioperl/
59              
60             =head1 AUTHOR - Sendu Bala
61              
62             Email bix@sendu.me.uk
63              
64             =head1 APPENDIX
65              
66             The rest of the documentation details each of the object methods.
67             Internal methods are usually preceded with a _
68              
69             =cut
70              
71             package Bio::Tools::Run::Phylo::PhyloBase;
72 4     4   21 use strict;
  4         5  
  4         99  
73              
74 4     4   20 use Bio::AlignIO;
  4         4  
  4         58  
75 4     4   439 use Bio::TreeIO;
  4         13578  
  4         72  
76              
77 4     4   22 use base qw(Bio::Tools::Run::WrapperBase);
  4         5  
  4         1582  
78              
79              
80             =head2 _alignment
81              
82             Title : _alignment
83             Usage : $aln = $obj->_alignment()
84             Function: Get/set an alignment object, generating one from a file if desired.
85             Returns : Bio::Align::AlignI (probably a Bio::SimpleAlign)
86             Args : none to get
87             OR filename & input format of the alignment file (latter defaults to
88             guess) to set from file
89             OR Bio::Align::AlignI to set
90              
91             =cut
92              
93             sub _alignment {
94 0     0     my ($self, $thing, $format) = @_;
95            
96 0 0 0       if (ref($thing) && $thing->isa('Bio::Align::AlignI')) {
    0 0        
97 0           $self->{_align_obj} = $thing;
98             }
99             elsif ($thing && -e $thing) {
100 0 0         my $align_in = Bio::AlignIO->new(-verbose => $self->verbose, -file => $thing, $format ? (-format => $format) : ());
101 0   0       my $aln = $align_in->next_aln || $self->throw("Alignment file '$thing' had no alignment!");
102 0           $align_in->close();
103 0           $self->{_align_obj} = $aln;
104             }
105            
106 0           return $self->{_align_obj};
107             }
108              
109             =head2 _write_alignment
110              
111             Title : _write_alignment
112             Usage : $obj->_write_alignment()
113             Function: Writes the alignment object returned by _alignment() out in the
114             desired format to a temp file.
115             Returns : filename
116             Args : string to desribe format (default 'fasta'), any other options to pass
117             to AlignIO
118              
119             =cut
120              
121             sub _write_alignment {
122 0     0     my ($self, $format, @options) = @_;
123 0   0       my $align = $self->_alignment || $self->throw("_write_alignment called when _alignment had not been set");
124 0   0       $format ||= 'fasta';
125            
126 0           my ($tfh, $tempfile) = $self->io->tempfile(-dir => $self->tempdir);
127            
128 0           my $out = Bio::AlignIO->new(-verbose => $self->verbose, '-fh' => $tfh, '-format' => $format, @options);
129 0           $align->set_displayname_flat;
130 0           $out->write_aln($align);
131            
132 0           $out->close();
133 0           $out = undef;
134 0           close($tfh);
135 0           undef $tfh;
136            
137 0           return $tempfile;
138             }
139              
140             =head2 _tree
141              
142             Title : _tree
143             Usage : $tree = $obj->_tree()
144             Function: Get/set a tree object, generating one from a file/database if desired
145             Returns : Bio::Tree::TreeI
146             Args : none to get, OR to set:
147             OR filename & input format of the tree file (latter defaults to
148             guess) to set from file
149             OR Bio::Tree::TreeI
150             OR Bio::DB::Taxonomy when _alignment() has been set and where
151             sequences in the alignment have ids matching species in the taxonomy
152             database
153              
154             =cut
155              
156             sub _tree {
157 0     0     my ($self, $thing, $format) = @_;
158            
159 0 0         if ($thing) {
160 0           my $tree;
161 0 0 0       if (ref($thing) && $thing->isa('Bio::Tree::TreeI')) {
    0 0        
    0          
162 0           $tree = $thing;
163             }
164             elsif (ref($thing) && $thing->isa('Bio::DB::Taxonomy')) {
165             # get all the alignment sequence names
166 0           my @species_names = $self->_get_seq_names;
167            
168 0           $tree = $thing->get_tree(@species_names);
169            
170             # convert node ids to their seq_ids for correct output with TreeIO
171 0           foreach my $node ($tree->get_nodes) {
172 0           my $seq_id = $node->name('supplied');
173 0 0         $seq_id = $seq_id ? shift @{$seq_id} : ($node->node_name ? $node->node_name : $node->id);
  0 0          
174            
175 0           $node->id($seq_id);
176             }
177             }
178             elsif (-e $thing) {
179 0 0         my $tree_in = Bio::TreeIO->new(-verbose => $self->verbose, -file => $thing, $format ? (-format => $format) : ());
180 0   0       $tree = $tree_in->next_tree || $self->throw("Tree file '$thing' had no tree!");
181 0           $tree_in->close;
182             }
183            
184 0   0       $self->{_tree_obj} = $tree || $self->throw("'$thing' supplied but unable to generate a tree from it");
185             }
186            
187 0           return $self->{_tree_obj};
188             }
189              
190             =head2 _write_tree
191              
192             Title : _write_tree
193             Usage : $obj->_write_tree()
194             Function: Writes the tree object returned by _tree() out in the desired format
195             to a temp file.
196             Returns : filename
197             Args : string to desribe format (default 'newick')
198              
199             =cut
200              
201             sub _write_tree {
202 0     0     my ($self, $format) = @_;
203 0   0       my $tree = $self->_tree || $self->throw("_write_tree called when _tree had not been set");
204 0   0       $format ||= 'newick';
205            
206 0           my ($tfh, $tempfile) = $self->io->tempfile(-dir => $self->tempdir);
207            
208 0           my $out = Bio::TreeIO->new(-verbose => $self->verbose, -fh => $tfh, -format => $format);
209 0           $out->write_tree($tree);
210            
211 0           $out->close();
212 0           $out = undef;
213 0           close($tfh);
214 0           undef $tfh;
215            
216 0           return $tempfile;
217             }
218              
219             =head2 _get_seq_names
220              
221             Title : _get_seq_names
222             Usage : @names = $obj->_get_seq_names()
223             Function: Get all the sequence names (from id()) of the sequenes in the
224             alignment. _alignment() must be set prior to calling this.
225             Returns : list of strings (seq ids)
226             Args : none
227              
228             =cut
229              
230             sub _get_seq_names {
231 0     0     my $self = shift;
232 0   0       my $aln = $self->_alignment || $self->throw("_get_seq_names called when _alignment had not been set");
233            
234 0           my @names;
235 0           foreach my $seq ($aln->each_seq) {
236 0           push(@names, $seq->id);
237             }
238            
239 0           return @names;
240             }
241              
242             =head2 _get_node_names
243              
244             Title : _get_node_names
245             Usage : @names = $obj->_get_node_names()
246             Function: Get all the node names (from id()) of the nodes in the tree.
247             _tree must be set prior to calling this.
248             Returns : list of strings (node ids)
249             Args : none
250              
251             =cut
252              
253             sub _get_node_names {
254 0     0     my $self = shift;
255 0   0       my $tree = $self->_tree || $self->throw("_get_node_names called when _tree had not been set");
256            
257 0           my @names;
258 0           foreach my $node ($tree->get_leaf_nodes) {
259 0           push(@names, $node->id);
260             }
261            
262 0           return @names;
263             }
264              
265             =head2 _check_names
266              
267             Title : _check_names
268             Usage : if ($obj->_check_names) { ... }
269             Function: Determine if all sequences in the alignment file have a corresponding
270             node in the tree file. _alignment() and _tree() must be set
271             prior to calling this.
272             Returns : boolean (will also warn about the specific problems when returning
273             false)
274             Args : none
275              
276             =cut
277              
278             sub _check_names {
279 0     0     my $self = shift;
280            
281 0           my @seq_names = $self->_get_seq_names;
282 0           my %node_names = map { $_ => 1 } $self->_get_node_names;
  0            
283            
284             # (not interested in tree nodes that don't map to sequence, since we
285             # expect the tree to have internal nodes not represented by sequence)
286 0           foreach my $name (@seq_names) {
287 0 0         $self->{_unmapped}{$name} = 1 unless defined $node_names{$name};
288             }
289            
290 0 0         if (defined($self->{_unmapped})) {
291 0           my $count = scalar(keys %{$self->{_unmapped}});
  0            
292 0           my $unmapped = join(", ", keys %{$self->{_unmapped}});
  0            
293 0           $self->warn("$count unmapped ids between the supplied alignment and tree: $unmapped");
294 0           return 0;
295             }
296            
297 0           return 1;
298             }
299              
300             1;