| 1 | package Bio::Phylo::Parsers::Json; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Parsers::Abstract'; | ||
| 5 | use Bio::Phylo::IO 'parse'; | ||
| 6 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 7 | use Bio::Phylo::Util::Dependency 'Bio::Phylo::NeXML::XML2JSON'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Bio::Phylo::Parsers::Json - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This module is used to import NeXML data that was re-formatted as JSON, using | ||
| 16 | the mapping implemented by L<XML::XML2JSON>. This module is experimental in that | ||
| 17 | complex NeXML-to-JSON mapped strings may fail to yield valid NeXML (and, | ||
| 18 | consequently, valid Bio::Phylo objects) in the round trip. The reason for this | ||
| 19 | is that the XML2JSON omits xmlns declarations in its JSON output. We try to work | ||
| 20 | around this here by re-introducing the default namespaces, but if any additional | ||
| 21 | ones were present in the original NeXML (e.g. to specify namspaces for metadata | ||
| 22 | predicates) these can't be reconstructed. In addition, the JSON that XML2JSON | ||
| 23 | produces doesn't preserve element order. We try to be lenient about this when | ||
| 24 | parsing the intermediate NeXML, though it is actually invalid. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub _parse { | ||
| 29 | my $self = shift; | ||
| 30 | my $fh = $self->_handle; | ||
| 31 | my $json = do { local $/; <$fh> }; | ||
| 32 | |||
| 33 | # perhaps not happy about prolog? | ||
| 34 | $json =~ s/"\@encoding":"\S+?","\@version":"1.0",//; | ||
| 35 | my $conf = Bio::Phylo::NeXML::XML2JSON->new; | ||
| 36 | my $xml = $conf->json2xml($json); | ||
| 37 | my $fac = $self->_factory; | ||
| 38 | my $proj = $fac->create_project; | ||
| 39 | my $ns = $proj->get_namespaces; | ||
| 40 | for my $pre ( keys %{ $ns } ) { | ||
| 41 | my $uri = $ns->{$pre}; | ||
| 42 | if ( $xml !~ /xmlns:$pre/ ) { | ||
| 43 | $xml =~ s/<nex:nexml /<nex:nexml xmlns:$pre="$uri" /; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return @{ parse( '-format' => 'nexml', '-string' => $xml ) }; | ||
| 47 | } | ||
| 48 | |||
| 49 | # podinherit_insert_token | ||
| 50 | |||
| 51 | =head1 SEE ALSO | ||
| 52 | |||
| 53 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 54 | for any user or developer questions and discussions. | ||
| 55 | |||
| 56 | =over | ||
| 57 | |||
| 58 | =item L<Bio::Phylo::IO> | ||
| 59 | |||
| 60 | The json parser is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object. | ||
| 61 | Look there to learn how to parse data using Bio::Phylo. | ||
| 62 | |||
| 63 | =item L<Bio::Phylo::Manual> | ||
| 64 | |||
| 65 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =head1 CITATION | ||
| 70 | |||
| 71 | If you use Bio::Phylo in published research, please cite it: | ||
| 72 | |||
| 73 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 74 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 75 | I<BMC Bioinformatics> B<12>:63. | ||
| 76 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | 1; |