File Coverage

blib/lib/Bio/Phylo/Parsers/Phylip.pm
Criterion Covered Total %
statement 27 29 93.1
branch 5 8 62.5
condition 5 8 62.5
subroutine 4 4 100.0
pod n/a
total 41 49 83.6


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