File Coverage

blib/lib/OBO/Core/Dbxref.pm
Criterion Covered Total %
statement 44 53 83.0
branch 24 34 70.5
condition 14 33 42.4
subroutine 11 11 100.0
pod 7 8 87.5
total 100 139 71.9


line stmt bran cond sub pod time code
1             # $Id: Dbxref.pm 2014-03-29 erick.antezana $
2             #
3             # Module : Dbxref.pm
4             # Purpose : Reference structure.
5             # License : Copyright (c) 2006-2014 by Erick Antezana. All rights reserved.
6             # This program is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8             # Contact : Erick Antezana
9             #
10             package OBO::Core::Dbxref;
11              
12 19     19   5020 use Carp;
  19         29  
  19         1107  
13 19     19   82 use strict;
  19         20  
  19         445  
14 19     19   65 use warnings;
  19         36  
  19         20982  
15              
16             sub new {
17 3129     3129 0 3767 my $class = shift;
18 3129         3215 my $self = {};
19              
20 3129         4898 $self->{DB} = ''; # required, scalar (1)
21 3129         3444 $self->{ACC} = ''; # required, scalar (1)
22 3129         3573 $self->{DESCRIPTION} = ''; # scalar (0..1)
23 3129         3102 $self->{MODIFIER} = ''; # scalar (0..1)
24            
25 3129         4464 bless ($self, $class);
26 3129         5408 return $self;
27             }
28              
29             =head2 name
30              
31             Usage - print $dbxref->name() or $dbxref->name($name)
32             Returns - the dbxref name (string)
33             Args - the dbxref name (string) that follows this convention DB:ACC (ACC may be an empty string like some dbxrefs from the ChEBI ontology)
34             Function - gets/sets the dbxref name
35            
36             =cut
37              
38             sub name {
39 21000 100 33 21000 1 72664 if ($_[1]) {
    50          
40 3128 50 33     13687 if ($_[1] =~ /([\*\.\w-]*):([ ,;'\#~\w:\\\+\?\{\}\$\/\(\)\[\]\.=&!%_-]*)/ || # See $r_db_acc in Term.pm
41             $_[1] =~ /(http):\/\/(.*)/) {
42 3128         6151 $_[0]->{DB} = $1;
43 3128         6458 $_[0]->{ACC} = $2;
44             }
45             } elsif (!defined($_[0]->{DB}) || !defined($_[0]->{ACC})) {
46 0         0 croak 'The name of this dbxref is not defined.';
47             } else { # get-mode
48 17872         53517 return $_[0]->{DB}.':'.$_[0]->{ACC};
49             }
50             }
51              
52             # Alias
53             *id = \&name;
54              
55             =head2 db
56              
57             Usage - print $dbxref->db() or $dbxref->db($db)
58             Returns - the dbxref db (string)
59             Args - the dbxref db (string)
60             Function - gets/sets the dbxref db
61            
62             =cut
63              
64             sub db {
65 3226 100   3226 1 6474 if ($_[1]) {
    50          
66 1         4 $_[0]->{DB} = $_[1];
67             } elsif (!defined($_[0]->{DB})) {
68 0         0 croak "The database (db) of this 'dbxref' is not defined.";
69             } else { # get-mode
70 3225         7025 return $_[0]->{DB};
71             }
72             }
73              
74             =head2 acc
75              
76             Usage - print $dbxref->acc() or $dbxref->acc($acc)
77             Returns - the dbxref acc (string)
78             Args - the dbxref acc (string)
79             Function - gets/sets the dbxref acc
80            
81             =cut
82              
83             sub acc {
84 3226 100   3226 1 6463 if ($_[1]) {
    50          
85 1         2 $_[0]->{ACC} = $_[1];
86             } elsif (!defined($_[0]->{ACC})) {
87 0         0 croak 'The accession number (acc) of this dbxref is not defined.';
88             } else { # get-mode
89 3225         7124 return $_[0]->{ACC};
90             }
91             }
92              
93             =head2 description
94              
95             Usage - print $dbxref->description() or $dbxref->description($description)
96             Returns - the dbxref description (string)
97             Args - the dbxref description (string)
98             Function - gets/sets the dbxref description
99            
100             =cut
101              
102             sub description {
103 324 100 33 324 1 1070 if ($_[1]) {
    50          
104 30         72 $_[0]->{DESCRIPTION} = $_[1];
105             } elsif (!defined($_[0]->{DB}) || !defined($_[0]->{ACC})) {
106 0         0 croak 'The name of this dbxref is not defined.';
107             } else { # get-mode
108 294         497 return $_[0]->{DESCRIPTION};
109             }
110             }
111              
112             =head2 modifier
113              
114             Usage - print $dbxref->modifier() or $dbxref->modifier($modifier)
115             Returns - the optional trailing modifier (string)
116             Args - the optional trailing modifier (string)
117             Function - gets/sets the optional trailing modifier
118            
119             =cut
120              
121             sub modifier {
122 299 100 33 299 1 1034 if ($_[1]) {
    50          
123 9         24 $_[0]->{MODIFIER} = $_[1];
124             } elsif (!defined($_[0]->{DB}) || !defined($_[0]->{ACC})) {
125 0         0 croak 'The name of this dbxref is not defined.';
126             } else { # get-mode
127 290         677 return $_[0]->{MODIFIER};
128             }
129             }
130              
131             =head2 as_string
132              
133             Usage - print $dbxref->as_string()
134             Returns - returns this dbxref ([name "description" {modifier}]) as string
135             Args - none
136             Function - returns this dbxref as string
137            
138             =cut
139              
140             sub as_string {
141 9826 50 33 9826 1 25994 croak 'The name of this dbxref is not defined.' if (!defined($_[0]->{DB}) || !defined($_[0]->{ACC}));
142 9826         15118 my $result = $_[0]->{DB}.':'.$_[0]->{ACC};
143 9826 100 66     34277 $result .= ' "'.$_[0]->{DESCRIPTION}.'"' if (defined $_[0]->{DESCRIPTION} && $_[0]->{DESCRIPTION} ne '');
144 9826 100 66     30352 $result .= ' '.$_[0]->{MODIFIER} if (defined $_[0]->{MODIFIER} && $_[0]->{MODIFIER} ne '');
145 9826         24435 return $result;
146             }
147              
148             =head2 equals
149              
150             Usage - print $dbxref->equals($another_dbxref)
151             Returns - either 1(true) or 0 (false)
152             Args - the dbxref(OBO::Core::Dbxref) to compare with
153             Function - tells whether this dbxref is equal to the parameter
154            
155             =cut
156              
157             sub equals {
158 8 50 33 8 1 23 if ($_[1] && eval { $_[1]->isa('OBO::Core::Dbxref') }) {
  8         100  
159            
160 8 50 33     36 if (!defined($_[0]->{DB}) || !defined($_[0]->{ACC})) {
161 0         0 croak 'The name of this dbxref is undefined.';
162             }
163 8 50 33     28 if (!defined($_[1]->{DB}) || !defined($_[1]->{ACC})) {
164 0         0 croak 'The name of the target dbxref is undefined.';
165             }
166 8   66     96 return (($_[0]->{DB} eq $_[1]->{DB}) &&
167             ($_[0]->{ACC} eq $_[1]->{ACC}) &&
168             ($_[0]->{DESCRIPTION} eq $_[1]->{DESCRIPTION}) &&
169             ($_[0]->{MODIFIER} eq $_[1]->{MODIFIER}));
170             } else {
171 0           croak "An unrecognized object type (not a OBO::Core::Dbxref) was found: '", $_[1], "'";
172             }
173 0           return 0;
174             }
175              
176             1;
177              
178             __END__