File Coverage

blib/lib/Bio/Phylo/Parsers/Phylip.pm
Criterion Covered Total %
statement 30 32 93.7
branch 5 8 62.5
condition 5 8 62.5
subroutine 5 5 100.0
pod n/a
total 45 53 84.9


line stmt bran cond sub pod time code
1             package Bio::Phylo::Parsers::Phylip;
2 1     1   6 use strict;
  1         2  
  1         24  
3 1     1   4 use warnings;
  1         2  
  1         21  
4 1     1   5 use base 'Bio::Phylo::Parsers::Abstract';
  1         2  
  1         274  
5 1     1   6 use Bio::Phylo::Util::Exceptions 'throw';
  1         2  
  1         250  
6              
7             =head1 NAME
8              
9             Bio::Phylo::Parsers::Phylip - Parser used by Bio::Phylo::IO, no serviceable parts inside
10              
11             =head1 DESCRIPTION
12              
13             This module is used for parsing PHYLIP character state matrix files. At present this only
14             works on non-interleaved files. As PHYLIP files don't indicate what data type they are you
15             should indicate this as an argument to the Bio::Phylo::IO::parse function, i.e.:
16              
17             use Bio::Phylo::IO 'parse';
18             my $file = shift @ARGV;
19             my $type = 'dna'; # or rna, protein, restriction, standard, continuous
20             my $matrix = parse(
21             '-file' => $file,
22             '-format' => 'phylip',
23             '-type' => $type,
24             )->[0];
25             print ref($matrix); # probably prints Bio::Phylo::Matrices::Matrix;
26              
27             =cut
28              
29             sub _parse {
30 4     4   6 my $self = shift;
31 4         15 my $factory = $self->_factory;
32 4   50     12 my $type = $self->_args->{'-type'} || 'standard';
33 4         13 my $handle = $self->_handle;
34 4         23 my $matrix = $factory->create_matrix( '-type' => $type );
35 4         8 my ( $ntax, $nchar );
36 4         23 LINE: while (<$handle>) {
37 20         31 my ( $name, $seq );
38 20 100 66     142 if ( /^\s*(\d+)\s+(\d+)\s*$/ && !$ntax && !$nchar ) {
    50 66        
39 4         17 ( $ntax, $nchar ) = ( $1, $2 );
40 4         13 next LINE;
41             }
42             elsif ( /^\s*(\S+)\s+(.+)$/ ) {
43 16         54 ( $name, $seq ) = ( $1, $2 );
44 16         33 $seq =~ s/\s//g;
45             }
46             else {
47 0         0 $name = substr( $_, 0, 10 );
48 0         0 $seq = substr( $_, 10 );
49             }
50 16         47 $matrix->insert(
51             $factory->create_datum(
52             '-type' => $type,
53             '-name' => $name,
54             '-char' => $matrix->get_type_object->split($seq),
55             )
56             );
57             }
58 4         12 my ( $my_nchar, $my_ntax ) = ( $matrix->get_nchar, $matrix->get_ntax );
59 4 50       16 $nchar != $my_nchar
60             && throw 'BadFormat' => "observed ($my_nchar) != expected ($nchar) nchar";
61 4 50       9 $ntax != $my_ntax
62             && throw 'BadFormat' => "observed ($my_ntax) != expected ($ntax) ntax";
63 4         13 return $matrix;
64             }
65              
66             # podinherit_insert_token
67              
68             =head1 SEE ALSO
69              
70             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
71             for any user or developer questions and discussions.
72              
73             =over
74              
75             =item L<Bio::Phylo::IO>
76              
77             The PHYLIP parser is called by the L<Bio::Phylo::IO> object.
78             Look there for examples.
79              
80             =item L<Bio::Phylo::Manual>
81              
82             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
83              
84             =back
85              
86             =head1 CITATION
87              
88             If you use Bio::Phylo in published research, please cite it:
89              
90             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
91             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
92             I<BMC Bioinformatics> B<12>:63.
93             L<http://dx.doi.org/10.1186/1471-2105-12-63>
94              
95             =cut
96              
97             1;