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         4  
  2         56  
3 2     2   8 use base 'Bio::Phylo::Parsers::Abstract';
  2         4  
  2         451  
4 2     2   11 use Bio::Phylo::Util::CONSTANT;
  2         4  
  2         701  
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 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   5 my $self = shift;
24 4         15 my $fh = $self->_handle;
25 4         14 my $fac = $self->_factory;
26 4         26 my $taxa = $fac->create_taxa;
27 4   100     44 local $/ = $self->_args->{'-fieldsep'} || "\n";
28 4   50     14 my $delim = $self->_args->{'-delim'} || "\t";
29 4         9 my @header;
30 4         21 LINE: while (<$fh>) {
31 17         41 chomp;
32 17         91 my @fields = split /$delim/, $_;
33 17         32 my $name;
34             my %meta;
35            
36             # this means it is actually tabular, which also means it has a header
37 17 50       39 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         99 my $taxon = $fac->create_taxon( '-name' => $name );
62            
63             # attach metadata, if any
64 17         45 for my $predicate ( keys %meta ) {
65             $taxon->add_meta(
66 0         0 $fac->create_meta( '-triple' => { $predicate => $meta{$predicate} } )
67             );
68             }
69 17         53 $taxa->insert( $taxon );
70             }
71 4         23 return $taxa;
72             }
73              
74             # podinherit_insert_token
75              
76             =head1 SEE ALSO
77              
78             There is a mailing list at L
79             for any user or developer questions and discussions.
80              
81             =over
82              
83             =item L
84              
85             The taxon list parser is called by the L object.
86             Look there for examples.
87              
88             =item L
89              
90             Also see the manual: L and L.
91              
92             =back
93              
94             =head1 CITATION
95              
96             If you use Bio::Phylo in published research, please cite it:
97              
98             B, B, B, B
99             and B, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
100             I B<12>:63.
101             L
102              
103             =cut
104              
105             1;