tag fourth (and hopefully last) alpha
[bioperl-live.git] / branch-1-6 / Bio / Ontology / SimpleGOEngine / GraphAdaptor.pm
blob6482c93c8d98d6ca934a2cb86bee64b3d6cab426
1 # $Id: GraphAdaptor.pm 10525 2006-09-26 22:03:22Z sendu $
3 # BioPerl Graph adaptor for Bio::Ontology::SimpleGOEngine
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Nat Goodman <natg at shore.net>
9 # (c) Nathan Goodman natg@shore.net 2005
10 # (c) ISB, Institute for Systems Biology 2005
12 # You may distribute this module under the same terms as perl itself.
13 # Refer to the Perl Artistic License (see the license accompanying this
14 # software package, or see http://www.perl.com/language/misc/Artistic.html)
15 # for the terms under which you may use, modify, and redistribute this module.
17 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
18 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 # You may distribute this module under the same terms as perl itself
23 # POD documentation - main docs before the code
25 =head1 NAME
27 Bio::Ontology::SimpleGOEngine::GraphAdaptor - Graph adaptor for
28 Bio::Ontology::SimpleGOEngine
30 =head1 SYNOPSIS
32 use Bio::Ontology::SimpleGOEngine::GraphAdaptor;
34 my $graph = Bio::Ontology::SimpleGOEngine::GraphAdaptor;
36 =head1 DESCRIPTION
38 This is a think adaptor to simplify use of the old and new versions of
39 the standard CPAN Graph module (old is versions 0.2x; new is 0.5x and
40 beyond) within Bio::Ontology::SimpleGOEngine.
42 This module implements only those Graph methods used by
43 SimpleGOEngine. It is far from a complete compatibility layer! It
44 also implements workarounds for cerain performance problems in the
45 current versions of Graph v0.5x.
47 This class provides a 'new' method that determines which version of
48 Graph is available. The object returned by 'new' is blessed into this
49 class if the new version of Graph is available, else into the subclass
51 Bio::Ontology::SimpleGOEngine::GraphAdaptor02
53 This class provides implementations for the required graph methods
54 using the new version of Graph. In most cases, these are simple
55 pass-throughs. Methods that differ in v0.2x are implemented in the
56 subclass.
58 The methods implemented here or in the subclasses are listed below.
59 In all cases, we implemented the Graph v0.5x interface. Consult the
60 Graph v0.5x man page for details.
62 add_vertex
63 has_vertex
64 add_edge
65 has_edge
66 vertices
67 edges
68 edges_at
69 predecessors
70 successors
71 set_vertex_attribute
72 get_vertex_attribute
73 set_edge_attribute
74 get_edge_attribute
75 source_vertices
76 sink_vertices
78 =head1 FEEDBACK
80 =head2 Mailing Lists
82 User feedback is an integral part of the evolution of this and other
83 Bioperl modules. Send your comments and suggestions preferably to the
84 Bioperl mailing lists Your participation is much appreciated.
86 bioperl-l@bioperl.org - General discussion
87 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
89 =head2 Support
91 Please direct usage questions or support issues to the mailing list:
93 I<bioperl-l@bioperl.org>
95 rather than to the module maintainer directly. Many experienced and
96 reponsive experts will be able look at the problem and quickly
97 address it. Please include a thorough description of the problem
98 with code and data examples if at all possible.
100 =head2 Reporting Bugs
102 report bugs to the Bioperl bug tracking system to help us keep track
103 the bugs and their resolution. Bug reports can be submitted via the
104 web:
106 http://bugzilla.open-bio.org/
108 =head1 AUTHOR
110 Nat Goodman
112 Email: natg at shore.net
114 Address:
116 Institute for Systems Biology
117 1441 N 34th St
118 Seattle, WA 98103-8904
120 =head1 APPENDIX
122 The rest of the documentation details each of the object
123 methods. Internal methods are usually preceded with a _
125 =cut
127 # Let the code begin...
129 package Bio::Ontology::SimpleGOEngine::GraphAdaptor;
131 use Graph::Directed;
133 use strict;
134 use Bio::Ontology::SimpleGOEngine::GraphAdaptor02;
136 use base qw(Bio::Root::Root);
138 =head2 new
140 Title : new
141 Usage : $graph = Bio::Ontology::SimpleGOEngine::GraphAdaptor->new()
142 Function: Creates a new graph
143 Returns : Bio::Ontology::SimpleGOEngine::GraphAdaptor02 or
144 Bio::Ontology::SimpleGOEngine::GraphAdaptor05 object,
145 depending on which Graph version is available
146 Args : none
148 =cut
150 sub new {
151 my( $class ) = @_;
152 $class = ref $class || $class;
154 my $self=
155 ( defined $Graph::VERSION && $Graph::VERSION >= 0.5 ) ?
156 bless ( {}, $class ) :
157 bless ( {}, 'Bio::Ontology::SimpleGOEngine::GraphAdaptor02' );
158 $self->{_graph}=new Graph::Directed;
159 $self->{_vertex_attributes}={};
160 $self->{_edge_attributes}={};
161 return $self;
164 # Here are the main methods
166 sub add_vertex {
167 my $self=shift;
168 $self->_graph->add_vertex(@_);
170 sub has_vertex {
171 my $self=shift;
172 $self->_graph->has_vertex(@_);
174 sub add_edge {
175 my $self=shift;
176 $self->_graph->add_edge(@_);
178 sub has_edge {
179 my $self=shift;
180 $self->_graph->has_edge(@_);
182 sub vertices {
183 my $self=shift;
184 $self->_graph->vertices(@_);
186 sub edges {
187 my $self=shift;
188 $self->_graph->edges(@_);
190 sub edges_at {
191 my $self=shift;
192 $self->_graph->edges_at(@_);
194 sub predecessors {
195 my $self=shift;
196 $self->_graph->predecessors(@_);
198 sub successors {
199 my $self=shift;
200 $self->_graph->successors(@_);
202 sub source_vertices {
203 my $self=shift;
204 $self->_graph->source_vertices();
206 sub sink_vertices {
207 my $self=shift;
208 $self->_graph->sink_vertices();
210 # The following methods workaround a performance problem in Graph v0.5x
211 # when attributes are attached to the graph
212 sub set_vertex_attribute {
213 my($self,$v,$attribute,$value)=@_;
214 $self->_vertex2attributes($v)->{$attribute}=$value;
216 sub get_vertex_attribute {
217 my($self,$v,$attribute)=@_;
218 $self->_vertex2attributes($v)->{$attribute};
220 sub set_edge_attribute {
221 my($self,$u,$v,$attribute,$value)=@_;
222 $self->_edge2attributes($u,$v)->{$attribute}=$value;
224 sub get_edge_attribute {
225 my($self,$u,$v,$attribute)=@_;
226 $self->_edge2attributes($u,$v)->{$attribute};
229 =head2 _graph
231 Title : _graph
232 Usage : $self->_graph();
233 Function: Internal method to access 'real' graph
234 Returns : Graph::Directed object
235 Args : none
237 =cut
239 sub _graph {$_[0]->{_graph}; }
241 =head2 _vertex_attributes
243 Title : _vertex_attributes
244 Usage : $self->vertex_attributes();
245 Function: Internal method to access HASH used to store vertex attributes
246 Returns : Graph::Directed object
247 Args : none
249 =cut
251 sub _vertex_attributes {$_[0]->{_vertex_attributes}; }
253 =head2 _edge_attributes
255 Title : _edge_attributes
256 Usage : $self->edge_attributes();
257 Function: Internal method to access HASH used to store edge attributes
258 Returns : Graph::Directed object
259 Args : none
261 =cut
263 sub _edge_attributes {$_[0]->{_edge_attributes}; }
265 =head2 _vertex2attributes
267 Title : _vertex2attributes
268 Usage : $value=$graph->_vertex2attributes($v_->{ATTRIBUTE};
269 $graph->_vertex2attributes($v)->{ATTRIBUTE}=$value;
270 Function: Internal method to access attributes for a specific vertex
271 Returns : HASH
272 Args : none
274 =cut
276 sub _vertex2attributes {
277 my($self,$vertex)=@_;
278 $self->_vertex_attributes->{$vertex} or $self->_vertex_attributes->{$vertex}={};
281 =head2 _edge2attributes
283 Title : _edge2attributes
284 Usage : $value=$graph->_edge2attributes($u,$v)->{ATTRIBUTE};
285 $graph->_edge2attributes($u,$v)->{ATTRIBUTE}=$value;
286 Function: Internal method to access HASH used to store edge attributes
287 Returns : HASH
288 Args : none
290 =cut
292 sub _edge2attributes {
293 my($self,$u,$v)=@_;
294 $self->_edge_attributes->{$u,$v} or $self->_edge_attributes->{$u,$v}={};