BioPerl

 view release on metacpan or  search on metacpan

Bio/Annotation/TagTree.pm  view on Meta::CPAN

# BioPerl module for Bio::Annotation::TagTree
#
# Cared for Chris Fields
#
# You may distribute this module under the same terms as perl itself.
# Refer to the Perl Artistic License (see the license accompanying this
# software package, or see http://www.perl.com/language/misc/Artistic.html)
# for the terms under which you may use, modify, and redistribute this module.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# POD documentation - main docs before the code

=head1 NAME

Bio::Annotation::TagTree - AnnotationI with tree-like hierarchal key-value
relationships ('structured tags') that can be represented as simple text.

=head1 SYNOPSIS

   use Bio::Annotation::TagTree;
   use Bio::Annotation::Collection;

   my $col = Bio::Annotation::Collection->new();

   # data structure can be an array reference with a data structure
   # corresponding to that defined by Data::Stag:

   my $sv = Bio::Annotation::TagTree->new(-tagname => 'mytag1',
                                          -value => $data_structure);
   $col->add_Annotation($sv);

   # regular text passed is parsed based on the tagformat().
   my $sv2 = Bio::Annotation::TagTree->new(-tagname => 'mytag2',
                                          -tagformat => 'xml',
                                          -value => $xmltext);
   $col->add_Annotation($sv2);

=head1 DESCRIPTION

This takes tagged data values and stores them in a hierarchal structured
element-value hierarchy (complements of Chris Mungall's Data::Stag module). Data
can then be represented as text using a variety of output formats (indention,
itext, xml, spxr). Furthermore, the data structure can be queried using various
means. See L<Data::Stag> for details.

Data passed in using value() or the '-value' parameter upon instantiation
can either be:

1) an array reference corresponding to the data structure for Data::Stag;

2) a text string in 'xml', 'itext', 'spxr', or 'indent' format. The default
format is 'xml'; this can be changed using tagformat() prior to using value() or
by passing in the proper format using '-tagformat' upon instantiation;

3) another Bio::Annotation::TagTree or Data::Stag node instance.  In both cases
a deep copy (duplicate) of the instance is generated.

Beyond checking for an array reference no format guessing occurs (so, for
roundtrip tests ensure that the IO formats correspond). For now, we recommend
when using text input to set tagformat() to one of these formats prior to data
loading to ensure the proper Data::Stag parser is selected. After data loading,
the tagformat() can be changed to change the text string format returned by
value(). (this may be rectified in the future)

This Annotation type is fully BioSQL compatible and could be considered a
temporary replacement for nested Bio::Annotation::Collections, at least until
BioSQL and bioperl-db can support nested annotation collections.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support 

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and 
reponsive experts will be able look at the problem and quickly 
address it. Please include a thorough description of the problem 
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution.  Bug reports can be submitted via
or the web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR 

Chris Fields

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal
methods are usually preceded with a _

=cut

# Let the code begin...

package Bio::Annotation::TagTree;
use strict;

# Object preamble - inherits from Bio::Root::Root

use base qw(Bio::Annotation::SimpleValue);
use Data::Stag;

Bio/Annotation/TagTree.pm  view on Meta::CPAN

    $h->{'value'} = $self->value;
}

=head2 tagname

 Title   : tagname
 Usage   : $obj->tagname($newval)
 Function: Get/set the tagname for this annotation value.

           Setting this is optional. If set, it obviates the need to provide
           a tag to AnnotationCollection when adding this object.
 Example :
 Returns : value of tagname (a scalar)
 Args    : new value (a scalar, optional)

=cut

sub tagname {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        $self->{'tagname'} = $value;
    }
    return $self->{'tagname'};
}

=head1 Specific accessors for TagTree

=cut

=head2 value

 Title   : value
 Usage   : $obj->value($newval)
 Function: Get/set the value for this annotation.
 Returns : value of value
 Args    : newvalue (optional)

=cut

sub value {
    my ( $self, $value ) = @_;

    # set mode? This resets the entire tagged database
    my $format = $self->tagformat;
    if ($value) {
        if ( ref $value ) {
            if ( ref $value eq 'ARRAY' ) {

                # note the tagname() is not used here; it is only used for
                # storing this AnnotationI in the annotation collection
                eval { $self->{db} = Data::Stag->nodify($value) };
            }
            else {

                # assuming this is blessed; passing on to node() and copy
                $self->node( $value, 'copy' );
            }
        }
        else {

            # not trying to guess here for now; we go by the tagformat() setting
            my $h = Data::Stag->getformathandler($format);
            eval { $self->{db} = Data::Stag->from( $format . 'str', $value ) };
        }
        $self->throw("Data::Stag error:\n$@") if $@;
    }

    # get mode?
    # How do we return a data structure?
    # for now, we use the output (if there is a Data::Stag node present)
    # may need to run an eval {} to catch Data::Stag output errors
    $self->node->$format;
}

=head2 tagformat

 Title   : tagformat
 Usage   : $obj->tagformat($newval)
 Function: Get/set the output tag format for this annotation.
 Returns : value of tagformat
 Args    : newvalue (optional) - format for the data passed into value
           must be of values 'xml', 'indent', 'sxpr', 'itext', 'perl'

=cut

my %IS_VALID_FORMAT = map { $_ => 1 } qw(xml indent sxpr itext);

sub tagformat {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        $self->throw( "$value is not a valid format; valid format types:\n"
              . join( ',', map { "'$_'" } keys %IS_VALID_FORMAT ) )
          if !exists $IS_VALID_FORMAT{$value};
        $self->{'tagformat'} = $value;
    }
    return $self->{'tagformat'};
}

=head2 node

 Title   : node
 Usage   : $obj->node()
 Function: Get/set the topmost Data::Stag node used for this annotation.  
 Returns : Data::Stag node implementation
           (default is Data::Stag::StagImpl)
 Args    : (optional) Data::Stag node implementation
           (optional)'copy' => flag to create a copy of the node

=cut

sub node {
    my ( $self, $value, $copy ) = @_;
    if ( defined $value && ref $value ) {
        $self->{'db'} =
          $value->isa('Data::Stag::StagI')
          ? ( $copy && $copy eq 'copy' ? $value->duplicate : $value )
          : $value->isa('Bio::Annotation::TagTree') ? ( $copy
              && $copy eq 'copy' ? $value->node->duplicate : $value->node )
          : $self->throw(
            'Object must be Data::Stag::StagI or Bio::Annotation::TagTree');
    }



( run in 0.519 second using v1.01-cache-2.11-cpan-39bf76dae61 )