File Coverage

blib/lib/Bio/Phylo/Parsers/Taxlist.pm
Criterion Covered Total %
statement 26 36 72.2
branch 1 8 12.5
condition 3 4 75.0
subroutine 4 4 100.0
pod n/a
total 34 52 65.3


line stmt bran cond sub pod time code
1             package Bio::Phylo::Parsers::Taxlist;
2 2     2   12 use strict;
  2         3  
  2         53  
3 2     2   9 use base 'Bio::Phylo::Parsers::Abstract';
  2         4  
  2         360  
4 2     2   13 use Bio::Phylo::Util::CONSTANT;
  2         3  
  2         569  
5              
6             =head1 NAME
7              
8             Bio::Phylo::Parsers::Taxlist - Parser used by Bio::Phylo::IO, no serviceable parts inside
9              
10             =head1 DESCRIPTION
11              
12             This module is used for importing sets of taxa from plain text files, one taxon
13             on each line. It is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object, so
14             look there for usage examples. If you want to parse from a string, you
15             may need to indicate the field separator (default is '\n') to the
16             Bio::Phylo::IO->parse call:
17              
18             -fieldsep => '\n',
19              
20             =cut
21              
22             sub _parse {
23 4     4   9 my $self = shift;
24 4         17 my $fh = $self->_handle;
25 4         13 my $fac = $self->_factory;
26 4         26 my $taxa = $fac->create_taxa;
27 4   100     25 local $/ = $self->_args->{'-fieldsep'} || "\n";
28 4   50     12 my $delim = $self->_args->{'-delim'} || "\t";
29 4         7 my @header;
30 4         26 LINE: while (<$fh>) {
31 17         31 chomp;
32 17         85 my @fields = split /$delim/, $_;
33 17         30 my $name;
34             my %meta;
35            
36             # this means it is actually tabular, which also means it has a header
37 17 50       31 if ( scalar @fields > 1 ) {
38            
39             # this happens the first line
40 0 0       0 if ( not @header ) {
41 0         0 @header = @fields;
42 0         0 for my $predicate ( @header ) {
43 0 0       0 if ( $predicate =~ /^(.+?):.+$/ ) {
44 0         0 my $prefix = $1;
45             $taxa->set_namespaces(
46 0         0 $prefix => $Bio::Phylo::Util::CONSTANT::NS->{$prefix}
47             );
48             }
49             }
50 0         0 next LINE;
51             }
52            
53             # create key value pairs to attach
54 0         0 for my $i ( 1 .. $#fields ) {
55 0 0       0 $meta{$header[$i]} = $fields[$i] if $fields[$i];
56             }
57             }
58            
59             # this is the first field regardless
60 17         34 $name = shift @fields;
61 17         82 my $taxon = $fac->create_taxon( '-name' => $name );
62            
63             # attach metadata, if any
64 17         40 for my $predicate ( keys %meta ) {
65             $taxon->add_meta(
66 0         0 $fac->create_meta( '-triple' => { $predicate => $meta{$predicate} } )
67             );
68             }
69 17         48 $taxa->insert( $taxon );
70             }
71 4         27 return $taxa;
72             }
73              
74             # podinherit_insert_token
75              
76             =head1 SEE ALSO
77              
78             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
79             for any user or developer questions and discussions.
80              
81             =over
82              
83             =item L<Bio::Phylo::IO>
84              
85             The taxon list parser is called by the L<Bio::Phylo::IO> object.
86             Look there for examples.
87              
88             =item L<Bio::Phylo::Manual>
89              
90             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
91              
92             =back
93              
94             =head1 CITATION
95              
96             If you use Bio::Phylo in published research, please cite it:
97              
98             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
99             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
100             I<BMC Bioinformatics> B<12>:63.
101             L<http://dx.doi.org/10.1186/1471-2105-12-63>
102              
103             =cut
104              
105             1;