| 1 | # | ||||||||||
| 2 | # BioPerl module for Bio::TreeIO::cluster | ||||||||||
| 3 | # | ||||||||||
| 4 | # Contributed by Guillaume Rousse <Guillaume-dot-Rousse-at-inria-dot-fr> | ||||||||||
| 5 | # | ||||||||||
| 6 | # Copyright INRIA | ||||||||||
| 7 | # | ||||||||||
| 8 | # You may distribute this module under the same terms as perl itself | ||||||||||
| 9 | |||||||||||
| 10 | # POD documentation - main docs before the code | ||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Bio::TreeIO::cluster - A TreeIO driver module for parsing Algorithm::Cluster::treecluster() output | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | # do not use this module directly | ||||||||||
| 19 | use Bio::TreeIO; | ||||||||||
| 20 | use Algorithm::Cluster; | ||||||||||
| 21 | my ($result, $linkdist) = Algorithm::Cluster::treecluster( | ||||||||||
| 22 | distances => $matrix | ||||||||||
| 23 | ); | ||||||||||
| 24 | my $treeio = Bio::TreeIO->new( | ||||||||||
| 25 | -format => 'cluster', | ||||||||||
| 26 | -result => $result, | ||||||||||
| 27 | -linkdist => $linkdist, | ||||||||||
| 28 | -labels => $labels | ||||||||||
| 29 | ); | ||||||||||
| 30 | my $tree = $treeio->next_tree; | ||||||||||
| 31 | |||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||
| 33 | |||||||||||
| 34 | This is a driver module for parsing Algorithm::Cluster::treecluster() output. | ||||||||||
| 35 | |||||||||||
| 36 | =head1 FEEDBACK | ||||||||||
| 37 | |||||||||||
| 38 | =head2 Mailing Lists | ||||||||||
| 39 | |||||||||||
| 40 | User feedback is an integral part of the evolution of this and other | ||||||||||
| 41 | Bioperl modules. Send your comments and suggestions preferably to | ||||||||||
| 42 | the Bioperl mailing list. Your participation is much appreciated. | ||||||||||
| 43 | |||||||||||
| 44 | bioperl-l@bioperl.org - General discussion | ||||||||||
| 45 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||||||||||
| 46 | |||||||||||
| 47 | =head2 Support | ||||||||||
| 48 | |||||||||||
| 49 | Please direct usage questions or support issues to the mailing list: | ||||||||||
| 50 | |||||||||||
| 51 | I<bioperl-l@bioperl.org> | ||||||||||
| 52 | |||||||||||
| 53 | rather than to the module maintainer directly. Many experienced and | ||||||||||
| 54 | reponsive experts will be able look at the problem and quickly | ||||||||||
| 55 | address it. Please include a thorough description of the problem | ||||||||||
| 56 | with code and data examples if at all possible. | ||||||||||
| 57 | |||||||||||
| 58 | =head2 Reporting Bugs | ||||||||||
| 59 | |||||||||||
| 60 | Report bugs to the Bioperl bug tracking system to help us keep track | ||||||||||
| 61 | of the bugs and their resolution. Bug reports can be submitted via | ||||||||||
| 62 | the web: | ||||||||||
| 63 | |||||||||||
| 64 | https://github.com/bioperl/bioperl-live/issues | ||||||||||
| 65 | |||||||||||
| 66 | =head1 AUTHOR - Guillaume Rousse | ||||||||||
| 67 | |||||||||||
| 68 | Email Guillaume-dot-Rousse-at-inria-dot-fr | ||||||||||
| 69 | |||||||||||
| 70 | =head1 CONTRIBUTORS | ||||||||||
| 71 | |||||||||||
| 72 | Jason Stajich - jason-at-bioperl-dot-org | ||||||||||
| 73 | |||||||||||
| 74 | =head1 APPENDIX | ||||||||||
| 75 | |||||||||||
| 76 | The rest of the documentation details each of the object methods. | ||||||||||
| 77 | Internal methods are usually preceded with a _ | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | # Let the code begin... | ||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | package Bio::TreeIO::cluster; | ||||||||||
| 86 | 1 | $Bio::TreeIO::cluster::VERSION = '1.7.8'; | |||||||||
| 87 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 88 | |||||||||||
| 89 | 1 | ▶ | use Bio::Event::EventGeneratorI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 1 | ▶ | use IO::String; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | use base qw(Bio::TreeIO); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 93 | |||||||||||
| 94 | =head2 new | ||||||||||
| 95 | |||||||||||
| 96 | Title : new | ||||||||||
| 97 | Usage : my $obj = Bio::TreeIO::cluster->new(); | ||||||||||
| 98 | Function: Builds a new Bio::TreeIO::cluster object for reading Algorithm::Cluster::treecluster output | ||||||||||
| 99 | Returns : Bio::TreeIO::cluster | ||||||||||
| 100 | Args :-result => Algorithm::Cluster result | ||||||||||
| 101 | -linkdist => distance between links | ||||||||||
| 102 | -labels => node labels | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | sub _initialize { | ||||||||
|
Subroutine
_initialize: not called CC=1 SCAR=6.9
| |||||||||||
| 107 | 0 | my $self = shift; | |||||||||
| 108 | ($self->{_result},$self->{_linkdist}, | ||||||||||
| 109 | 0 | $self->{_labels}) = $self->_rearrange([qw | |||||||||
| 110 | (RESULT LINKDIST LABELS)], | ||||||||||
| 111 | @_); | ||||||||||
| 112 | 0 | $self->SUPER::_initialize(@_); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | =head2 next_tree | ||||||||||
| 116 | |||||||||||
| 117 | Title : next_tree | ||||||||||
| 118 | Usage : my $tree = $treeio->next_tree | ||||||||||
| 119 | Function: Gets the next tree in the stream | ||||||||||
| 120 | Returns : Bio::Tree::TreeI | ||||||||||
| 121 | Args : none | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||
| 126 | 0 | ▶ | sub next_tree { | ||||||||
|
Subroutine
next_tree: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 127 | 0 | my ($self) = @_; | |||||||||
| 128 | 0 | ▶ | if( ! $self->{_result} ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | $self->warn("Must provide value 'result' and 'linkdist' and 'labels' when initializing a TreeIO::cluster object"); | |||||||||
| 130 | 0 | return; | |||||||||
| 131 | } | ||||||||||
| 132 | 0 | $self->_eventHandler->start_document(); | |||||||||
| 133 | |||||||||||
| 134 | # build tree from the root | ||||||||||
| 135 | 0 | $self->_eventHandler->start_element({Name => 'tree'}); | |||||||||
| 136 | 0 | $self->_recurse(-1, 0); | |||||||||
| 137 | 0 | $self->_recurse(-1, 1); | |||||||||
| 138 | 0 | $self->_eventHandler->end_element({Name => 'tree'}); | |||||||||
| 139 | |||||||||||
| 140 | 0 | return $self->_eventHandler->end_document; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | sub _recurse { | ||||||||
|
Subroutine
_recurse: not called CC=2 SCAR=17.9
| |||||||||||
| 144 | 0 | my ($self, $line, $column) = @_; | |||||||||
| 145 | |||||||||||
| 146 | 0 | my $id = $self->{_result}->[$line]->[$column]; | |||||||||
| 147 | 0 | ▶ | if ($id >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | # leaf | ||||||||||
| 149 | 0 | $self->debug("leaf $id\n"); | |||||||||
| 150 | 0 | $self->debug("distance $self->{_linkdist}->[$line]\n"); | |||||||||
| 151 | 0 | $self->debug("label $self->{_labels}->[$id]\n"); | |||||||||
| 152 | 0 | $self->_eventHandler->start_element({Name => 'node'}); | |||||||||
| 153 | 0 | $self->_eventHandler->start_element({Name => 'branch_length'}); | |||||||||
| 154 | 0 | $self->_eventHandler->characters($self->{_linkdist}->[$line]); | |||||||||
| 155 | 0 | $self->_eventHandler->end_element({Name => 'branch_length'}); | |||||||||
| 156 | 0 | $self->_eventHandler->start_element({Name => 'id'}); | |||||||||
| 157 | 0 | $self->_eventHandler->characters($self->{_labels}->[$id]); | |||||||||
| 158 | 0 | $self->_eventHandler->end_element({Name => 'id'}); | |||||||||
| 159 | 0 | $self->_eventHandler->start_element({Name => 'leaf'}); | |||||||||
| 160 | 0 | $self->_eventHandler->characters(1); | |||||||||
| 161 | 0 | $self->_eventHandler->end_element({Name => 'leaf'}); | |||||||||
| 162 | 0 | $self->_eventHandler->end_element({Name => 'node'}); | |||||||||
| 163 | } else { | ||||||||||
| 164 | # internal node | ||||||||||
| 165 | 0 | $self->debug("internal node $id\n"); | |||||||||
| 166 | 0 | $self->debug("distance $self->{_linkdist}->[$line]\n"); | |||||||||
| 167 | 0 | $self->_eventHandler->start_element({Name => 'node'}); | |||||||||
| 168 | 0 | $self->_eventHandler->start_element({Name => 'branch_length'}); | |||||||||
| 169 | 0 | $self->_eventHandler->characters($self->{_linkdist}->[$line]); | |||||||||
| 170 | 0 | $self->_eventHandler->end_element({Name => 'branch_length'}); | |||||||||
| 171 | 0 | $self->_eventHandler->start_element({Name => 'leaf'}); | |||||||||
| 172 | 0 | $self->_eventHandler->characters(0); | |||||||||
| 173 | 0 | $self->_eventHandler->end_element({Name => 'leaf'}); | |||||||||
| 174 | 0 | $self->_eventHandler->start_element({Name => 'tree'}); | |||||||||
| 175 | 0 | my $child_id = - ($id + 1); | |||||||||
| 176 | 0 | $self->_recurse($child_id, 0); | |||||||||
| 177 | 0 | $self->_recurse($child_id, 1); | |||||||||
| 178 | 0 | $self->_eventHandler->end_element({Name => 'tree'}); | |||||||||
| 179 | 0 | $self->_eventHandler->end_element({Name => 'node'}); | |||||||||
| 180 | |||||||||||
| 181 | } | ||||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | =head2 write_tree | ||||||||||
| 185 | |||||||||||
| 186 | Title : write_tree | ||||||||||
| 187 | Usage : | ||||||||||
| 188 | Function: Sorry not possible with this format | ||||||||||
| 189 | Returns : none | ||||||||||
| 190 | Args : none | ||||||||||
| 191 | |||||||||||
| 192 | |||||||||||
| 193 | =cut | ||||||||||
| 194 | |||||||||||
| 195 | 0 | ▶ | sub write_tree{ | ||||||||
|
Subroutine
write_tree: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 196 | 0 | $_[0]->throw("Sorry the format 'cluster' can only be used as an input format"); | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 1 | 1; | |||||||||