| 1 | package Bio::Phylo::Unparsers::Nexml; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Unparsers::Abstract'; | ||
| 5 | use Bio::Phylo::Util::CONSTANT qw':objecttypes looks_like_object'; | ||
| 6 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 7 | use Bio::Phylo::Util::Dependency 'XML::Twig'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Bio::Phylo::Unparsers::Nexml - Serializer used by Bio::Phylo::IO, no serviceable parts inside | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This module serializes Taxa objects, Forest objects and Matrix objects | ||
| 16 | to NeXML. | ||
| 17 | |||
| 18 | =begin comment | ||
| 19 | |||
| 20 | Type : Wrapper | ||
| 21 | Title : _to_string($matrix) | ||
| 22 | Usage : $nexus->_to_string($matrix); | ||
| 23 | Function: Stringifies a matrix object into | ||
| 24 | a nexus formatted table. | ||
| 25 | Alias : | ||
| 26 | Returns : SCALAR | ||
| 27 | Args : Bio::Phylo::Matrices::Matrix; | ||
| 28 | |||
| 29 | =end comment | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub _to_string { | ||
| 34 | my $self = shift; | ||
| 35 | my $taxa_obj = $self->{'PHYLO'}; | ||
| 36 | if ( $taxa_obj->can('_type') && $taxa_obj->_type == _PROJECT_ ) { | ||
| 37 | return $taxa_obj->to_xml; | ||
| 38 | } | ||
| 39 | if ( $taxa_obj->can('_type') && $taxa_obj->_type == _DESCRIPTION_ ) { | ||
| 40 | return $taxa_obj->to_xml; | ||
| 41 | } | ||
| 42 | if ( $taxa_obj->can('_type') && $taxa_obj->_type != _TAXA_ ) { | ||
| 43 | if ( $taxa_obj->can('make_taxa') ) { | ||
| 44 | my $obj = $taxa_obj->make_taxa; | ||
| 45 | my $attached_obj = $taxa_obj; | ||
| 46 | for my $contained_obj ( @{ $attached_obj->get_entities } ) { | ||
| 47 | if ( $contained_obj->_type == _DATUM_ ) { | ||
| 48 | $contained_obj->set_name(); | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | for my $node ( @{ $contained_obj->get_entities } ) { | ||
| 52 | $node->set_name(); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | $taxa_obj = $obj; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | throw 'ObjectMismatch' => | ||
| 60 | "Object ($taxa_obj) is not a taxa object,\n and doesn't link to one"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # else { | ||
| 65 | # throw 'ObjectMismatch' => "Can't serialize $taxa_obj to nexml"; | ||
| 66 | # } | ||
| 67 | my $parse_twig = XML::Twig->new; | ||
| 68 | my $nexml_twig = XML::Twig->new; | ||
| 69 | my $nexml_root = XML::Twig::Elt->new( | ||
| 70 | 'nex:nexml', | ||
| 71 | { | ||
| 72 | 'xmlns:nex' => 'http://www.nexml.org/2009', | ||
| 73 | 'version' => '0.9', | ||
| 74 | 'generator' => __PACKAGE__ . ' v.' . $Bio::Phylo::VERSION, | ||
| 75 | 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 76 | 'xsi:schemaLocation' => | ||
| 77 | 'http://www.nexml.org/2009 http://www.nexml.org/1.0/nexml.xsd', | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | eval { | ||
| 81 | my $taxa_elt = | ||
| 82 | $parse_twig->parse( $taxa_obj->to_xml( %{ $self->{TAXA_ARGS} } ) ); | ||
| 83 | $taxa_elt->root->paste($nexml_root); | ||
| 84 | }; | ||
| 85 | die $@, $taxa_obj->to_xml( ${ $self->{TAXA_ARGS} } ) if $@; | ||
| 86 | if ( $taxa_obj->get_matrices ) { | ||
| 87 | for my $characters_obj ( reverse @{ $taxa_obj->get_matrices } ) { | ||
| 88 | eval { | ||
| 89 | my $characters_elt = | ||
| 90 | $parse_twig->parse( | ||
| 91 | $characters_obj->to_xml( %{ $self->{MATRIX_ARGS} } ) ); | ||
| 92 | $characters_elt->root->paste( 'last_child', $nexml_root ); | ||
| 93 | }; | ||
| 94 | die $@, $characters_obj->to_xml( %{ $self->{MATRIX_ARGS} } ) if $@; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | if ( $taxa_obj->get_forests ) { | ||
| 98 | for my $forest_obj ( reverse @{ $taxa_obj->get_forests } ) { | ||
| 99 | eval { | ||
| 100 | my $forest_elt = | ||
| 101 | $parse_twig->parse( | ||
| 102 | $forest_obj->to_xml( %{ $self->{FOREST_ARGS} } ) ); | ||
| 103 | $forest_elt->root->paste( 'last_child', $nexml_root ); | ||
| 104 | }; | ||
| 105 | die $@, $forest_obj->to_xml( %{ $self->{FOREST_ARGS} } ) if $@; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | $nexml_twig->set_root($nexml_root); | ||
| 109 | $nexml_twig->set_xml_version('1.0'); | ||
| 110 | $nexml_twig->set_encoding('UTF-8'); | ||
| 111 | $nexml_twig->set_pretty_print('indented'); | ||
| 112 | $nexml_twig->set_empty_tag_style('normal'); | ||
| 113 | my $nexml_string = $nexml_twig->sprint(); | ||
| 114 | return $nexml_string; | ||
| 115 | } | ||
| 116 | |||
| 117 | # podinherit_insert_token | ||
| 118 | |||
| 119 | =head1 SEE ALSO | ||
| 120 | |||
| 121 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 122 | for any user or developer questions and discussions. | ||
| 123 | |||
| 124 | =over | ||
| 125 | |||
| 126 | =item L<Bio::Phylo::IO> | ||
| 127 | |||
| 128 | The NeXML serializer is called by the L<Bio::Phylo::IO> object. | ||
| 129 | |||
| 130 | =item L<Bio::Phylo::Manual> | ||
| 131 | |||
| 132 | Also see the manual: L<Bio::Phylo::Manual>. | ||
| 133 | |||
| 134 | =item L<http://www.nexml.org> | ||
| 135 | |||
| 136 | The NeXML project is housed at L<http://www.nexml.org> | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =head1 CITATION | ||
| 141 | |||
| 142 | If you use Bio::Phylo in published research, please cite it: | ||
| 143 | |||
| 144 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 145 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 146 | I<BMC Bioinformatics> B<12>:63. | ||
| 147 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | 1; |