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