Bio::Annotation
TagTree
Toolbar
Summary
Bio::Annotation::TagTree - AnnotationI with tree-like hierarchal key-value
relationships ('structured tags') that can be represented as simple text.
Package variables
Privates (from "my" definitions)
$DEFAULT_CB = sub { $_[0]->value || '' }
%IS_VALID_FORMAT = map { $_ => 1 } qw(xml indent sxpr itext)
Included modules
Inherit
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);
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
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.
Methods
Methods description
Title : new Usage : my $sv = Bio::Annotation::TagTree->new(); Function: Instantiate a new TagTree object Returns : Bio::Annotation::TagTree object Args : -value => $value to initialize the object data field [optional] -tagname => $tag to initialize the tagname [optional] -tagformat => format for output [optional] (types 'xml', 'itext', 'sxpr', 'indent', default = 'itext') -node => Data::Stag node or Bio::Annotation::TagTree instance |
Title : as_text Usage : my $text = $obj->as_text Function: return the string "Value: $v" where $v is the value Returns : string Args : none |
Title : display_text Usage : my $str = $ann->display_text(); Function: returns a string. Unlike as_text(), this method returns a string formatted as would be expected for the specific implementation.
One can pass a callback as an argument which allows custom text
generation; the callback is passed the current instance and any text
returned
Example :
Returns : a string
Args : [optional] callback |
Title : hash_tree Usage : my $hashtree = $value->hash_tree Function: For supporting the AnnotationI interface just returns the value as a hashref with the key 'value' pointing to the value Maybe reimplement using Data::Stag::hash()? Returns : hashrf Args : none |
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) |
Title : value Usage : $obj->value($newval) Function: Get/set the value for this annotation. Returns : value of value Args : newvalue (optional) |
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' |
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 |
Title : element Usage : Function: Returns the element name (key name) for this node Example : Returns : scalar Args : none |
Title : data Usage : Function: Returns the data structure (array ref) for this node Example : Returns : array ref Args : none |
Title : children Usage : Function: Get the top-level array of Data::Stag nodes or (if the top level is a terminal node) a scalar value.
This is similar to StructuredValue's get_values() method, with the
key difference being instead of array refs and scalars you get either
Data::Stag nodes or the value for this particular node.
For consistency (since one could recursively check nodes),
we use the same method name as Data::Stag children().
Example :
Returns : an array
Args : none |
Title : subnodes Usage : Function: Get the top-level array of Data::Stag nodes. Unlike children(), this only returns an array of nodes (if this is a terminal node, no value is returned) Example : Returns : an array of nodes Args : none |
Title : get Usage : Function: Returns the nodes or value for the named element or path Example : Returns : returns array of nodes or a scalar (if node is terminal) dependent on wantarray Args : none |
Title : find Usage : Function: Recursively searches for and returns the nodes or values for the named element or path Example : Returns : returns array of nodes or scalars (for terminal nodes) Args : none |
Title : findnode Usage : Function: Recursively searches for and returns a list of nodes of the given element path Example : Returns : returns array of nodes Args : none |
Title : findval Usage : Function: Example : Returns : returns array of nodes or values Args : none |
Title : addchild Usage : $struct->addchild(['name' => [['foo'=> 'bar1']]]); Function: add new child node to the current node. One can pass in a node, TagTree, or data structure; for instance, in the above, this would translate to (in XML):
bar1
Returns : node
Args : first arg = element name
all other args are added as tag-value pairs |
Title : add Usage : $struct->add('foo', 'bar1', 'bar2', 'bar3'); Function: add tag-value nodes to the current node. In the above, this would translate to (in XML): <foo>bar1</foo> <foo>bar2</foo> <foo>bar3</foo> Returns : Args : first arg = element name all other args are added as tag-value pairs |
Title : set Usage : $struct->set('foo','bar'); Function: sets a single tag-value pair in the current node. Note this differs from add() in that this replaces any data already present Returns : node Args : first arg = element name all other args are added as tag-value pairs |
Title : unset Usage : $struct->unset('foo'); Function: unsets all key-value pairs of the passed element from the current node Returns : node Args : element name |
Title : free Usage : $struct->free Function: removes all data from the current node Returns : Args : |
Title : hash Usage : $struct->hash; Function: turns the tag-value tree into a hash, all data values are array refs Returns : hash Args : first arg = element name all other args are added as tag-value pairs |
Title : pairs Usage : $struct->pairs; Function: turns the tag-value tree into a hash, all data values are scalar Returns : hash Args : first arg = element name all other args are added as tag-value pairs, note that duplicates will be lost |
Title : qmatch Usage : @persons = $s->qmatch('person', ('name'=>'fred')); Function : returns all elements in the node tree which match the element name and the key-value pair Returns : Array of nodes Args : return-element str, match-element str, match-value str |
Title : tnodes Usage : @termini = $s->tnodes; Function : returns all terminal nodes below this node Returns : Array of nodes Args : return-element str, match-element str, match-value str |
Title : ntnodes Usage : @termini = $s->ntnodes; Function : returns all nonterminal nodes below this node Returns : Array of nodes Args : return-element str, match-element str, match-value str |
Title : get_all_values Usage : @termini = $s->get_all_values; Function : returns all terminal node values Returns : Array of values Args : return-element str, match-element str, match-value str
This is meant to emulate the values one would get from StructureValue's get_all_values() method. Note, however, using this method dissociates the tag-value relationship (i.e. you only get the value list, no elements) |
Methods code
sub new
{ my ( $class, @args ) = @_;
my $self = $class->SUPER::new();
my ( $node, $value, $tag, $format, $verbose ) = $self->_rearrange(
[
qw(
NODE
VALUE
TAGNAME
TAGFORMAT
VERBOSE)
],
@args
);
$self->throw("Cant use both node and value; mutually exclusive")
if defined $node && defined $value;
defined $tag && $self->tagname($tag);
$format ||= 'itext';
$self->tagformat($format);
defined $value && $self->value($value);
defined $node && $self->node($node);
defined $verbose && $self->verbose($verbose);
return $self;} |
sub as_text
{ my ($self) = @_;
return "TagTree: " . $self->value;} |
sub display_text
{ my ( $self, $cb ) = @_;
$cb ||= $DEFAULT_CB;
$self->throw("Callback must be a code reference") if ref $cb ne 'CODE';
return $cb->($self);
}} |
sub hash_tree
{ my ($self) = @_;
my $h = {};
$h->{'value'} = $self->value;} |
sub tagname
{ my ( $self, $value ) = @_;
if ( defined $value ) {
$self->{'tagname'} = $value;
}
return $self->{'tagname'};} |
sub value
{ my ( $self, $value ) = @_;
my $format = $self->tagformat;
if ($value) {
if ( ref $value ) {
if ( ref $value eq 'ARRAY' ) {
eval { $self->{db} = Data::Stag->nodify($value) };
}
else {
$self->node( $value, 'copy' );
}
}
else {
my $h = Data::Stag->getformathandler($format);
eval { $self->{db} = Data::Stag->from( $format . 'str', $value ) };
}
$self->throw("Data::Stag error:\n$@") if $@;
}
$self->node->$format;} |
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'};} |
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');
}
if (!$self->{'db'}) {
$self->{'db'} = Data::Stag->new();
}
return $self->{'db'};} |
sub element
{ my $self = shift;
return $self->node->element;} |
sub data
{ my $self = shift;
return $self->node->data;} |
sub children
{ my $self = shift;
return $self->node->children;} |
sub subnodes
{ my $self = shift;
return $self->node->subnodes;} |
sub get
{ my ( $self, @vals ) = @_;
return $self->node->get(@vals);} |
sub find
{ my ( $self, @vals ) = @_;
return $self->node->find(@vals);} |
sub findnode
{ my ( $self, @vals ) = @_;
return $self->node->findnode(@vals);} |
sub findval
{ my ( $self, @vals ) = @_;
return $self->node->findval(@vals);} |
sub addchild
{ my ( $self, @vals ) = @_;
if ( !$self->element ) {
@vals > 1 ? $self->value(\@ vals ) : $self->value( $vals[0] );
return $self->{db};
}
elsif ( !$self->node->ntnodes ) {
$self->throw("Can't add child to node; only terminal node is present!");
}
else {
return $self->node->addchild(@vals);
}} |
sub add
{ my ( $self, @vals ) = @_;
if ( !$self->node->element ) {
$self->throw("Can't add to terminal element!");
}
return $self->node->add(@vals);} |
sub set
{ my ( $self, @vals ) = @_;
if ( !$self->node->element ) {
$self->throw("Can't add to tree; empty tree!");
}
return $self->node->set(@vals);} |
sub unset
{ my ( $self, @vals ) = @_;
return $self->node->unset(@vals);} |
sub free
{ my ($self) = @_;
return $self->node->free;} |
sub hash
{ my ($self) = @_;
return $self->node->hash;} |
sub pairs
{ my ($self) = @_;
return $self->node->pairs;} |
sub qmatch
{ my ( $self, @vals ) = @_;
return $self->node->qmatch(@vals);} |
sub tnodes
{ my ($self) = @_;
return $self->node->tnodes;} |
sub ntnodes
{ my ($self) = @_;
return $self->node->ntnodes;} |
sub get_all_values
{ my $self = shift;
my @kids = $self->children;
my @vals;
while ( my $val = shift @kids ) {
( ref $val ) ? push @kids, $val->children : push @vals, $val;
}
return @vals;
}
1;} |
General documentation
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
Please direct usage questions or support issues to the mailing list:
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.
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://redmine.open-bio.org/projects/bioperl/
Chris Fields
The rest of the documentation details each of the object methods. Internal
methods are usually preceded with a _
| AnnotationI implementing functions | Top |
| Specific accessors for TagTree | Top |
| Data::Stag convenience methods | Top |
Because Data::Stag uses blessed arrays and the core Bioperl class uses blessed
hashes, TagTree uses an internal instance of a Data::Stag node for data storage.
Therefore the following methods actually delegate to the Data:::Stag internal
instance.
For consistency (since one could recursively check child nodes), methods retain
the same names as Data::Stag. Also, no 'magic' (AUTOLOAD'ed) methods are
employed, simply b/c full-fledged Data::Stag functionality can be attained by
grabbing the Data::Stag instance using node().
| StructureValue-like methods | Top |