File Coverage

blib/lib/Bio/Phylo/Parsers/Taxlist.pm
Criterion Covered Total %
statement 29 39 74.3
branch 1 8 12.5
condition 3 4 75.0
subroutine 5 5 100.0
pod n/a
total 38 56 67.8


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