sync w/ main trunk
[bioperl-live.git] / Bio / Ontology / Relationship.pm
blobae41c5b5067d63dae5ffc5ff8250a0ca9e667641
1 # $Id$
3 # BioPerl module for Relationship
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Christian M. Zmasek <czmasek@gnf.org> or <cmzmasek@yahoo.com>
9 # (c) Christian M. Zmasek, czmasek@gnf.org, 2002.
10 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
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::Relationship - a relationship for an ontology
29 =head1 SYNOPSIS
31 $rel = Bio::Ontology::Relationship->new( -identifier => "16847",
32 -subject_term => $subj,
33 -object_term => $obj,
34 -predicate_term => $pred );
36 =head1 DESCRIPTION
38 This is a basic implementation of Bio::Ontology::RelationshipI.
40 The terminology we use here is the one commonly used for ontologies,
41 namely the triple of (subject, predicate, object), which in addition
42 is scoped in a namespace (ontology). It is called triple because it is
43 a tuple of three ontology terms.
45 There are other terminologies in use for expressing relationships. For
46 those who it helps to better understand the concept, the triple of
47 (child, relationship type, parent) would be equivalent to the
48 terminology chosen here, disregarding the question whether the notion
49 of parent and child is sensible in the context of the relationship
50 type or not. Especially in the case of ontologies with a wide variety
51 of predicates the parent/child terminology and similar ones can
52 quickly become ambiguous (e.g., A synthesises B), meaningless (e.g., A
53 binds B), or even conflicting (e.g., A is-parent-of B), and are
54 therefore strongly discouraged.
56 =head1 FEEDBACK
58 =head2 Mailing Lists
60 User feedback is an integral part of the evolution of this and other
61 Bioperl modules. Send your comments and suggestions preferably to the
62 Bioperl mailing lists Your participation is much appreciated.
64 bioperl-l@bioperl.org - General discussion
65 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
67 =head2 Support
69 Please direct usage questions or support issues to the mailing list:
71 L<bioperl-l@bioperl.org>
73 rather than to the module maintainer directly. Many experienced and
74 reponsive experts will be able look at the problem and quickly
75 address it. Please include a thorough description of the problem
76 with code and data examples if at all possible.
78 =head2 Reporting Bugs
80 Report bugs to the Bioperl bug tracking system to help us keep track
81 the bugs and their resolution. Bug reports can be submitted via
82 the web:
84 http://bugzilla.open-bio.org/
86 =head1 AUTHOR
88 Christian M. Zmasek
90 Email: czmasek@gnf.org or cmzmasek@yahoo.com
92 WWW: http://www.genetics.wustl.edu/eddy/people/zmasek/
94 Address:
96 Genomics Institute of the Novartis Research Foundation
97 10675 John Jay Hopkins Drive
98 San Diego, CA 92121
100 =head1 CONTRIBUTORS
102 Hilmar Lapp, email: hlapp at gmx.net
104 =head1 APPENDIX
106 The rest of the documentation details each of the object
107 methods. Internal methods are usually preceded with a _
109 =cut
112 # Let the code begin...
115 package Bio::Ontology::Relationship;
116 use strict;
117 use Bio::Ontology::TermI;
119 use base qw(Bio::Root::Root Bio::Ontology::RelationshipI);
124 =head2 new
126 Title : new
127 Usage : $rel = Bio::Ontology::Relationship->new(-identifier => "16847",
128 -subject_term => $subject,
129 -object_term => $object,
130 -predicate_term => $type );
131 Function: Creates a new Bio::Ontology::Relationship.
132 Returns : A new Bio::Ontology::Relationship object.
133 Args : -identifier => the identifier of this relationship [scalar]
134 -subject_term => the subject term [Bio::Ontology::TermI]
135 -object_term => the object term [Bio::Ontology::TermI]
136 -predicate_term => the predicate term [Bio::Ontology::TermI]
138 =cut
140 sub new {
142 my( $class, @args ) = @_;
144 my $self = $class->SUPER::new( @args );
146 my ( $identifier,
147 $subject_term,
148 $child, # for backwards compatibility
149 $object_term,
150 $parent, # for backwards compatibility
151 $predicate_term,
152 $reltype, # for backwards compatibility
153 $ont)
154 = $self->_rearrange( [qw( IDENTIFIER
155 SUBJECT_TERM
156 CHILD_TERM
157 OBJECT_TERM
158 PARENT_TERM
159 PREDICATE_TERM
160 RELATIONSHIP_TYPE
161 ONTOLOGY)
162 ], @args );
164 $self->init();
166 $self->identifier( $identifier );
167 $subject_term = $child unless $subject_term;
168 $object_term = $parent unless $object_term;
169 $predicate_term = $reltype unless $predicate_term;
170 $self->subject_term( $subject_term) if $subject_term;
171 $self->object_term( $object_term) if $object_term;
172 $self->predicate_term( $predicate_term ) if $predicate_term;
173 $self->ontology($ont) if $ont;
175 return $self;
177 } # new
181 =head2 init
183 Title : init()
184 Usage : $rel->init();
185 Function: Initializes this Relationship to all undef.
186 Returns :
187 Args :
189 =cut
191 sub init {
192 my( $self ) = @_;
194 $self->{ "_identifier" } = undef;
195 $self->{ "_subject_term" } = undef;
196 $self->{ "_object_term" } = undef;
197 $self->{ "_predicate_term" } = undef;
198 $self->ontology(undef);
200 } # init
204 =head2 identifier
206 Title : identifier
207 Usage : $rel->identifier( "100050" );
209 print $rel->identifier();
210 Function: Set/get for the identifier of this Relationship.
211 Returns : The identifier [scalar].
212 Args : The identifier [scalar] (optional).
214 =cut
216 sub identifier {
217 my ( $self, $value ) = @_;
219 if ( defined $value ) {
220 $self->{ "_identifier" } = $value;
223 return $self->{ "_identifier" };
224 } # identifier
229 =head2 subject_term
231 Title : subject_term
232 Usage : $rel->subject_term( $subject );
234 $subject = $rel->subject_term();
235 Function: Set/get for the subject term of this Relationship.
237 The common convention for ontologies is to express
238 relationships between terms as triples (subject, predicate,
239 object).
241 Returns : The subject term [Bio::Ontology::TermI].
242 Args : The subject term [Bio::Ontology::TermI] (optional).
244 =cut
246 sub subject_term {
247 my ( $self, $term ) = @_;
249 if ( defined $term ) {
250 $self->_check_class( $term, "Bio::Ontology::TermI" );
251 $self->{ "_subject_term" } = $term;
254 return $self->{ "_subject_term" };
256 } # subject_term
260 =head2 object_term
262 Title : object_term
263 Usage : $rel->object_term( $object );
265 $object = $rel->object_term();
266 Function: Set/get for the object term of this Relationship.
268 The common convention for ontologies is to express
269 relationships between terms as triples (subject, predicate,
270 object).
272 Returns : The object term [Bio::Ontology::TermI].
273 Args : The object term [Bio::Ontology::TermI] (optional).
275 =cut
277 sub object_term {
278 my ( $self, $term ) = @_;
280 if ( defined $term ) {
281 $self->_check_class( $term, "Bio::Ontology::TermI" );
282 $self->{ "_object_term" } = $term;
285 return $self->{ "_object_term" };
290 =head2 predicate_term
292 Title : predicate_term
293 Usage : $rel->predicate_term( $type );
295 $type = $rel->predicate_term();
296 Function: Set/get for the predicate (relationship type) of this
297 relationship.
299 The common convention for ontologies is to express
300 relationships between terms as triples (subject, predicate,
301 object).
303 Returns : The predicate term [Bio::Ontology::TermI].
304 Args : The predicate term [Bio::Ontology::TermI] (optional).
306 =cut
308 sub predicate_term {
309 my ( $self, $term ) = @_;
311 if ( defined $term ) {
312 $self->_check_class( $term, "Bio::Ontology::TermI" );
313 $self->{ "_predicate_term" } = $term;
316 return $self->{ "_predicate_term" };
320 =head2 ontology
322 Title : ontology
323 Usage : $ont = $obj->ontology()
324 Function: Get/set the ontology that defined this relationship.
325 Example :
326 Returns : an object implementing L<Bio::Ontology::OntologyI>
327 Args : on set, undef or an object implementing
328 Bio::Ontology::OntologyI (optional)
330 See L<Bio::Ontology::OntologyI>.
332 =cut
334 sub ontology{
335 my $self = shift;
336 my $ont;
338 if(@_) {
339 $ont = shift;
340 if($ont) {
341 $ont = Bio::Ontology::Ontology->new(-name => $ont) if ! ref($ont);
342 if(! $ont->isa("Bio::Ontology::OntologyI")) {
343 $self->throw(ref($ont)." does not implement ".
344 "Bio::Ontology::OntologyI. Bummer.");
347 return $self->{"_ontology"} = $ont;
349 return $self->{"_ontology"};
352 =head2 to_string
354 Title : to_string()
355 Usage : print $rel->to_string();
356 Function: to_string method for Relationship.
357 Returns : A string representation of this Relationship.
358 Args :
360 =cut
362 sub to_string {
363 my( $self ) = @_;
365 local $^W = 0;
367 my $s = "";
369 $s .= "-- Identifier:\n";
370 $s .= $self->identifier()."\n";
371 $s .= "-- Subject Term Identifier:\n";
372 $s .= $self->subject_term()->identifier()."\n";
373 $s .= "-- Object Term Identifier:\n";
374 $s .= $self->object_term()->identifier()."\n";
375 $s .= "-- Relationship Type Identifier:\n";
376 $s .= $self->predicate_term()->identifier();
378 return $s;
380 } # to_string
384 sub _check_class {
385 my ( $self, $value, $expected_class ) = @_;
387 if ( ! defined( $value ) ) {
388 $self->throw( "Found [undef] where [$expected_class] expected" );
390 elsif ( ! ref( $value ) ) {
391 $self->throw( "Found [scalar] where [$expected_class] expected" );
393 elsif ( ! $value->isa( $expected_class ) ) {
394 $self->throw( "Found [" . ref( $value ) . "] where [$expected_class] expected" );
397 } # _check_type
399 #################################################################
400 # aliases for backwards compatibility
401 #################################################################
403 =head1 Deprecated Methods
405 These methods are deprecated and defined here solely to preserve
406 backwards compatibility.
408 =cut
410 *child_term = \&subject_term;
411 *parent_term = \&object_term;
412 *relationship_type = \&predicate_term;