| 1 | package Bio::Phylo::Unparsers::Rss1; | ||
| 2 | use strict; | ||
| 3 | use base 'Bio::Phylo::Unparsers::Abstract'; | ||
| 4 | use Bio::Phylo::Util::CONSTANT qw'/looks_like/'; | ||
| 5 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 6 | use Bio::Phylo::Util::Dependency 'XML::Twig'; | ||
| 7 | use Bio::Phylo::Util::Logger; | ||
| 8 | use Bio::Phylo::Factory; | ||
| 9 | |||
| 10 | my $fac = Bio::Phylo::Factory->new; | ||
| 11 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Bio::Phylo::Unparsers::Rss1 - Serializer used by Bio::Phylo::IO, no serviceable | ||
| 16 | parts inside | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This module represents the contents of the supplied project object as an RSS1.0 | ||
| 21 | string, i.e. as RDF. Which contents are the items in the feed? This depends on | ||
| 22 | the value of the '-recordSchema' argument. If no argument is provided, the feed | ||
| 23 | lists taxa, otherwise 'tree' or 'matrix' for a list of those in the project. | ||
| 24 | |||
| 25 | =begin comment | ||
| 26 | |||
| 27 | Type : Wrapper | ||
| 28 | Title : _to_string | ||
| 29 | Usage : my $rss1 = $obj->_to_string; | ||
| 30 | Function: Stringifies a Bio::Phylo::Project object into an RSS1.0 string | ||
| 31 | Alias : | ||
| 32 | Returns : SCALAR | ||
| 33 | Args : Bio::Phylo::Project object | ||
| 34 | |||
| 35 | =end comment | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub _to_string { | ||
| 40 | my $self = shift; | ||
| 41 | my $obj = $self->{'PHYLO'}; | ||
| 42 | my $type = lc($self->{'RECORDSCHEMA'}) || 'taxon'; | ||
| 43 | |||
| 44 | # this is the root channel description | ||
| 45 | my $description = $fac->create_description( | ||
| 46 | '-namespaces' => $obj->get_namespaces, | ||
| 47 | '-link' => $obj->get_link, | ||
| 48 | '-desc' => $obj->get_desc, | ||
| 49 | '-name' => $obj->get_name, | ||
| 50 | ); | ||
| 51 | |||
| 52 | # here we start the recursion to find the items in the | ||
| 53 | # feed as specified by recordSchema/$type | ||
| 54 | _visitor( $obj, $type, $description ); | ||
| 55 | |||
| 56 | # this is just to ensure that the produced xml | ||
| 57 | # is well-formed and we return a pretty printed version | ||
| 58 | my $twig = XML::Twig->new; | ||
| 59 | $twig->set_xml_version('1.0'); | ||
| 60 | $twig->set_encoding('UTF-8'); | ||
| 61 | $twig->set_pretty_print('indented'); | ||
| 62 | $twig->set_empty_tag_style('normal'); | ||
| 63 | my $xml = $description->to_xml; | ||
| 64 | eval { | ||
| 65 | $twig->parse( $xml ); | ||
| 66 | }; | ||
| 67 | if ( $@ ) { | ||
| 68 | $logger->fatal( "Couldn't produce RSS: $@\n\n$xml "); | ||
| 69 | } | ||
| 70 | return $twig->sprint(); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _visitor { | ||
| 74 | my ( $obj, $type, $description ) = @_; | ||
| 75 | |||
| 76 | # recordSchema/$type should match the lower case, | ||
| 77 | # local name of a class in the hierarchy | ||
| 78 | my $class = ref $obj; | ||
| 79 | $class =~ s/.+://; | ||
| 80 | $class = lc $class; | ||
| 81 | if ( $class eq $type ) { | ||
| 82 | $logger->info("Focal objects are of item type '$type'"); | ||
| 83 | |||
| 84 | # this creates an item in the feed, i.e. a resource | ||
| 85 | my $resource = $fac->create_resource( | ||
| 86 | '-name' => $obj->get_name, | ||
| 87 | '-link' => $obj->get_link, | ||
| 88 | '-desc' => $obj->get_desc, | ||
| 89 | ); | ||
| 90 | |||
| 91 | # we attach all additional metadata, i.e. beyond the | ||
| 92 | # standard RSS1.0 predicates | ||
| 93 | $resource->add_meta($_) for @{ $obj->get_meta }; | ||
| 94 | $description->insert($resource); | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | $logger->info("Focal '$class' objects are not of item type '$type'"); | ||
| 98 | |||
| 99 | # need to dig deeper | ||
| 100 | if ( $obj->can('get_entities') ) { | ||
| 101 | for my $ent ( @{ $obj->get_entities } ) { | ||
| 102 | _visitor($ent,$type,$description); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | # podinherit_insert_token | ||
| 109 | |||
| 110 | =head1 SEE ALSO | ||
| 111 | |||
| 112 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 113 | for any user or developer questions and discussions. | ||
| 114 | |||
| 115 | =over | ||
| 116 | |||
| 117 | =item L<Bio::Phylo::IO> | ||
| 118 | |||
| 119 | The json unparser is called by the L<Bio::Phylo::IO> object. | ||
| 120 | Look there to learn how to unparse objects. | ||
| 121 | |||
| 122 | =item L<Bio::Phylo::Manual> | ||
| 123 | |||
| 124 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 125 | |||
| 126 | =item L<http://www.json.org> | ||
| 127 | |||
| 128 | To learn more about the JavaScript Object Notation (JSON) format, visit | ||
| 129 | L<http://www.json.org>. | ||
| 130 | |||
| 131 | =back | ||
| 132 | |||
| 133 | =head1 CITATION | ||
| 134 | |||
| 135 | If you use Bio::Phylo in published research, please cite it: | ||
| 136 | |||
| 137 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 138 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 139 | I<BMC Bioinformatics> B<12>:63. | ||
| 140 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 1; |