Phylo/Parsers/Tnrs.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 64 CC 3 SCAR 16.1
CC
3
Cov
0%
_parse
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.0
1 package Bio::Phylo::Parsers::Tnrs;
2 use strict;
3 use base 'Bio::Phylo::Parsers::Abstract';
4 use Data::Dumper;
5 use Bio::Phylo::Util::Exceptions 'throw';
6 use Bio::Phylo::Util::CONSTANT qw'/looks_like/ :namespaces :objecttypes';
7 use Bio::Phylo::Util::Dependency 'JSON';
8 use Bio::Phylo::Util::Dependency 'URI';
9  
10 =head1 NAME
11  
12 Bio::Phylo::Parsers::Tnrs - Parser used by Bio::Phylo::IO, no serviceable parts inside
13  
14 =head1 DESCRIPTION
15  
16 This module parses JSON output produced by the PhyloTastic taxonomic name
17 reconciliation service. It returns a taxa block with semantically annotated
18 taxon objects.
19  
20 =cut
21  
22 sub _parse {
23 my $self = shift;
24 my $fh = $self->_handle;
25 my $fac = $self->_factory;
26 my $taxa = $fac->create_taxa( '-namespaces' => { 'tnrs' => _NS_TNRS_ } );
27 my $data_structure = JSON::decode_json( do { local $/; <$fh> } );
28 my %authority;
29
30 # iterate over all returned names
31 for my $name ( @{ $data_structure->{'names'} } ) {
32
33 # instantiate taxon object
34 my $taxon = $fac->create_taxon( '-name' => $name->{'submittedName'} );
35
36 # get best match with URI
37 my ($match) = sort { $b->{'score'} <=> $a->{'score'} }
38 grep { defined $_->{'uri'} }
39 grep { defined $_ } @{ $name->{'matches'} };
40
41 # parse out 'authority', i.e. domain name
42 my $uri = $match->{'uri'};
43 my $auth = URI->new($uri)->authority;
44 $authority{$auth} = 1 if $auth;
45
46 # no URI, no domain...
47 if ( $auth ) {
48  
49 # attach metadata
50 $taxon->add_meta(
51 $fac->create_meta(
52 '-triple' => { "tnrs:${auth}" => $uri }
53 )
54 );
55
56 # attach link
57 $taxon->set_link($uri);
58 }
59  
60 $taxa->insert($taxon);
61 }
62
63 # need to attach source Ids to taxa block
64 my $metadata = $data_structure->{'metadata'};
65 for my $source ( keys %authority ) {
66 $taxa->add_meta(
67 $fac->create_meta(
68 '-triple' => { 'tnrs:source' => $source }
69 )
70 );
71 }
72
73 return $taxa;
74 }
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 TNRS parser is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object.
86 Look there to learn how to parse taxa in general
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;