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