Phylo/Parsers/Ubiometa.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 68 CC 4 SCAR 14.3
CC
4
Cov
0%
_copy_identifiers
6.0
_copy_namespaces
6.0
_parse
6.0
CRAP 4.2
1 package Bio::Phylo::Parsers::Ubiometa;
2 use base 'Bio::Phylo::Parsers::Abstract';
3 use Bio::Phylo::Util::Dependency 'XML::Twig';
4 use Bio::Phylo::NeXML::Entities '/entities/';
5 use strict;
6  
7 =head1 NAME
8  
9 Bio::Phylo::Parsers::Ubiometa - Parser used by Bio::Phylo::IO, no serviceable
10 parts inside
11  
12 =head1 DESCRIPTION
13  
14 This module parses RDF metadata for uBio namebank records. An example of such a
15 record is here: L<http://www.ubio.org/authority/metadata.php?lsid=urn:lsid:ubio.org:namebank:2481730>
16  
17 The parser creates a single L<Bio::Phylo::Taxa::Taxon> object to which all
18 metadata are attached as L<Bio::Phylo::NeXML::Meta> objects. This taxon is
19 embedded in a taxa block, or optionally in a L<Bio::Phylo::Project> object
20 if the C<-as_project> flag was provided to the call to C<parse()>.
21  
22 =cut
23  
24 # prettify the output, make sure there is a name and description
25 sub _rss_prettify {
26 my ( $self, $taxon ) = @_;
27 $taxon->set_name( $taxon->get_meta_object('dc:subject') );
28 $taxon->set_desc(
29 $taxon->get_meta_object('dc:type')
30 . ', Rank: '
31 . $taxon->get_meta_object('gla:rank')
32 . ', Status: '
33 . $taxon->get_meta_object('ubio:lexicalStatus')
34 );
35 }
36  
37 # copy the local part of the LSID to the guid field and dc:identifier
38 sub _copy_identifiers {
39 my ( $self, $elt, $obj ) = @_;
40 if ( $elt->att('rdf:about') =~ /(\d+)$/ ) {
41 my $namebankID = $1;
42 $obj->set_guid($namebankID);
43 $obj->add_meta( $self->_factory->create_meta(
44 '-triple' => { 'dc:identifier' => $namebankID }
45 ) );
46 }
47 }
48  
49 # attach namespaces from element to object
50 sub _copy_namespaces {
51 my ( $self, $elt, $obj ) = @_;
52 for my $att_name ( $elt->att_names ) {
53 if ( $att_name =~ /xmlns:(\S+)/ ) {
54 my $prefix = $1;
55 my $ns = $elt->att($att_name);
56 $obj->set_namespaces( $prefix => $ns );
57 }
58 }
59 }
60  
61 sub _parse {
62 my $self = shift;
63 my $fac = $self->_factory;
64 my $taxa = $fac->create_taxa;
65 XML::Twig->new(
66 'twig_handlers' => {
67 'rdf:RDF' => sub {
68 my ( $twig, $elt ) = @_;
69 my $taxon = $fac->create_taxon;
70
71 # attach namespaces from root element to taxon
72 $self->_copy_namespaces($elt,$taxon);
73
74 # attach metadata
75 my ($child) = $elt->children('rdf:Description');
76 for my $meta_elt ( $child->children ) {
77 my $val = encode_entities( $meta_elt->att('rdf:resource') || $meta_elt->text );
78 my $key = $meta_elt->tag;
79 if ( $key eq 'dc:identifier' ) {
80 $key = 'ubio:namebankIdentifier';
81 }
82 $taxon->add_meta( $fac->create_meta(
83 '-triple' => { $key => $val }
84 ) );
85 }
86
87 # parse the rdf:about lsid, use the numerical part as
88 # guid and dc:identifier
89 $self->_copy_identifiers($child,$taxon);
90
91 # prettify the output, make sure there is a name and description
92 $self->_rss_prettify($taxon);
93
94 $taxa->insert($taxon);
95 }
96 }
97 )->parse($self->_string);
98 return $taxa;
99 }
100  
101 # podinherit_insert_token
102  
103 =head1 SEE ALSO
104  
105 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
106 for any user or developer questions and discussions.
107  
108 =over
109  
110 =item L<Bio::Phylo::IO>
111  
112 The uBio metadata parser is called by the L<Bio::Phylo::IO> object.
113 Look there to learn more about parsing.
114  
115 =item L<Bio::Phylo::Manual>
116  
117 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
118  
119 =back
120  
121 =head1 CITATION
122  
123 If you use Bio::Phylo in published research, please cite it:
124  
125 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
126 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
127 I<BMC Bioinformatics> B<12>:63.
128 L<http://dx.doi.org/10.1186/1471-2105-12-63>
129  
130  
131  
132 =cut
133  
134 1;