| 1 | # $Id: GraphAdaptor.pm 10525 2006-09-26 22:03:22Z sendu $ | ||
| 2 | # | ||
| 3 | # BioPerl Graph adaptor for FAST::Bio::Ontology::SimpleGOEngine | ||
| 4 | # | ||
| 5 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 6 | # | ||
| 7 | # Cared for by Nat Goodman <natg at shore.net> | ||
| 8 | # | ||
| 9 | # (c) Nathan Goodman natg@shore.net 2005 | ||
| 10 | # (c) ISB, Institute for Systems Biology 2005 | ||
| 11 | # | ||
| 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. | ||
| 16 | # | ||
| 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. | ||
| 20 | # | ||
| 21 | # You may distribute this module under the same terms as perl itself | ||
| 22 | |||
| 23 | # POD documentation - main docs before the code | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor - Graph adaptor for | ||
| 28 | FAST::Bio::Ontology::SimpleGOEngine | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor; | ||
| 33 | |||
| 34 | my $graph = FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor; | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 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 FAST::Bio::Ontology::SimpleGOEngine. | ||
| 41 | |||
| 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. | ||
| 46 | |||
| 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 | ||
| 50 | |||
| 51 | FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor02 | ||
| 52 | |||
| 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. | ||
| 57 | |||
| 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. | ||
| 61 | |||
| 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 | ||
| 77 | |||
| 78 | =head1 FEEDBACK | ||
| 79 | |||
| 80 | =head2 Mailing Lists | ||
| 81 | |||
| 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. | ||
| 85 | |||
| 86 | bioperl-l@bioperl.org - General discussion | ||
| 87 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 88 | |||
| 89 | =head2 Support | ||
| 90 | |||
| 91 | Please direct usage questions or support issues to the mailing list: | ||
| 92 | |||
| 93 | I<bioperl-l@bioperl.org> | ||
| 94 | |||
| 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. | ||
| 99 | |||
| 100 | =head2 Reporting Bugs | ||
| 101 | |||
| 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: | ||
| 105 | |||
| 106 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 107 | |||
| 108 | =head1 AUTHOR | ||
| 109 | |||
| 110 | Nat Goodman | ||
| 111 | |||
| 112 | Email: natg at shore.net | ||
| 113 | |||
| 114 | Address: | ||
| 115 | |||
| 116 | Institute for Systems Biology | ||
| 117 | 1441 N 34th St | ||
| 118 | Seattle, WA 98103-8904 | ||
| 119 | |||
| 120 | =head1 APPENDIX | ||
| 121 | |||
| 122 | The rest of the documentation details each of the object | ||
| 123 | methods. Internal methods are usually preceded with a _ | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | # Let the code begin... | ||
| 128 | |||
| 129 | package FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor; | ||
| 130 | |||
| 131 | use Graph::Directed; | ||
| 132 | |||
| 133 | use strict; | ||
| 134 | use FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor02; | ||
| 135 | |||
| 136 | use base qw(FAST::Bio::Root::Root); | ||
| 137 | |||
| 138 | our $VERSION = '1.00'; | ||
| 139 | |||
| 140 | =head2 new | ||
| 141 | |||
| 142 | Title : new | ||
| 143 | Usage : $graph = FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor->new() | ||
| 144 | Function: Creates a new graph | ||
| 145 | Returns : FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor02 or | ||
| 146 | FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor05 object, | ||
| 147 | depending on which Graph version is available | ||
| 148 | Args : none | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub new { | ||
| 153 | my( $class ) = @_; | ||
| 154 | $class = ref $class || $class; | ||
| 155 | |||
| 156 | my $self= | ||
| 157 | ( defined $Graph::VERSION && $Graph::VERSION >= 0.5 ) ? | ||
| 158 | bless ( {}, $class ) : | ||
| 159 | bless ( {}, 'FAST::Bio::Ontology::SimpleGOEngine::GraphAdaptor02' ); | ||
| 160 | $self->{_graph}=new Graph::Directed; | ||
| 161 | $self->{_vertex_attributes}={}; | ||
| 162 | $self->{_edge_attributes}={}; | ||
| 163 | return $self; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Here are the main methods | ||
| 167 | |||
| 168 | sub add_vertex { | ||
| 169 | my $self=shift; | ||
| 170 | $self->_graph->add_vertex(@_); | ||
| 171 | } | ||
| 172 | sub has_vertex { | ||
| 173 | my $self=shift; | ||
| 174 | $self->_graph->has_vertex(@_); | ||
| 175 | } | ||
| 176 | sub add_edge { | ||
| 177 | my $self=shift; | ||
| 178 | $self->_graph->add_edge(@_); | ||
| 179 | } | ||
| 180 | sub has_edge { | ||
| 181 | my $self=shift; | ||
| 182 | $self->_graph->has_edge(@_); | ||
| 183 | } | ||
| 184 | sub vertices { | ||
| 185 | my $self=shift; | ||
| 186 | $self->_graph->vertices(@_); | ||
| 187 | } | ||
| 188 | sub edges { | ||
| 189 | my $self=shift; | ||
| 190 | $self->_graph->edges(@_); | ||
| 191 | } | ||
| 192 | sub edges_at { | ||
| 193 | my $self=shift; | ||
| 194 | $self->_graph->edges_at(@_); | ||
| 195 | } | ||
| 196 | sub predecessors { | ||
| 197 | my $self=shift; | ||
| 198 | $self->_graph->predecessors(@_); | ||
| 199 | } | ||
| 200 | sub successors { | ||
| 201 | my $self=shift; | ||
| 202 | $self->_graph->successors(@_); | ||
| 203 | } | ||
| 204 | sub source_vertices { | ||
| 205 | my $self=shift; | ||
| 206 | $self->_graph->source_vertices(); | ||
| 207 | } | ||
| 208 | sub sink_vertices { | ||
| 209 | my $self=shift; | ||
| 210 | $self->_graph->sink_vertices(); | ||
| 211 | } | ||
| 212 | # The following methods workaround a performance problem in Graph v0.5x | ||
| 213 | # when attributes are attached to the graph | ||
| 214 | sub set_vertex_attribute { | ||
| 215 | my($self,$v,$attribute,$value)=@_; | ||
| 216 | $self->_vertex2attributes($v)->{$attribute}=$value; | ||
| 217 | } | ||
| 218 | sub get_vertex_attribute { | ||
| 219 | my($self,$v,$attribute)=@_; | ||
| 220 | $self->_vertex2attributes($v)->{$attribute}; | ||
| 221 | } | ||
| 222 | sub set_edge_attribute { | ||
| 223 | my($self,$u,$v,$attribute,$value)=@_; | ||
| 224 | $self->_edge2attributes($u,$v)->{$attribute}=$value; | ||
| 225 | } | ||
| 226 | sub get_edge_attribute { | ||
| 227 | my($self,$u,$v,$attribute)=@_; | ||
| 228 | $self->_edge2attributes($u,$v)->{$attribute}; | ||
| 229 | } | ||
| 230 | |||
| 231 | =head2 _graph | ||
| 232 | |||
| 233 | Title : _graph | ||
| 234 | Usage : $self->_graph(); | ||
| 235 | Function: Internal method to access 'real' graph | ||
| 236 | Returns : Graph::Directed object | ||
| 237 | Args : none | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub _graph {$_[0]->{_graph}; } | ||
| 242 | |||
| 243 | =head2 _vertex_attributes | ||
| 244 | |||
| 245 | Title : _vertex_attributes | ||
| 246 | Usage : $self->vertex_attributes(); | ||
| 247 | Function: Internal method to access HASH used to store vertex attributes | ||
| 248 | Returns : Graph::Directed object | ||
| 249 | Args : none | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub _vertex_attributes {$_[0]->{_vertex_attributes}; } | ||
| 254 | |||
| 255 | =head2 _edge_attributes | ||
| 256 | |||
| 257 | Title : _edge_attributes | ||
| 258 | Usage : $self->edge_attributes(); | ||
| 259 | Function: Internal method to access HASH used to store edge attributes | ||
| 260 | Returns : Graph::Directed object | ||
| 261 | Args : none | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub _edge_attributes {$_[0]->{_edge_attributes}; } | ||
| 266 | |||
| 267 | =head2 _vertex2attributes | ||
| 268 | |||
| 269 | Title : _vertex2attributes | ||
| 270 | Usage : $value=$graph->_vertex2attributes($v_->{ATTRIBUTE}; | ||
| 271 | $graph->_vertex2attributes($v)->{ATTRIBUTE}=$value; | ||
| 272 | Function: Internal method to access attributes for a specific vertex | ||
| 273 | Returns : HASH | ||
| 274 | Args : none | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub _vertex2attributes { | ||
| 279 | my($self,$vertex)=@_; | ||
| 280 | $self->_vertex_attributes->{$vertex} or $self->_vertex_attributes->{$vertex}={}; | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 _edge2attributes | ||
| 284 | |||
| 285 | Title : _edge2attributes | ||
| 286 | Usage : $value=$graph->_edge2attributes($u,$v)->{ATTRIBUTE}; | ||
| 287 | $graph->_edge2attributes($u,$v)->{ATTRIBUTE}=$value; | ||
| 288 | Function: Internal method to access HASH used to store edge attributes | ||
| 289 | Returns : HASH | ||
| 290 | Args : none | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub _edge2attributes { | ||
| 295 | my($self,$u,$v)=@_; | ||
| 296 | $self->_edge_attributes->{$u,$v} or $self->_edge_attributes->{$u,$v}={}; | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; |