File Coverage

blib/lib/OBO/Core/Synonym.pm
Criterion Covered Total %
statement 102 105 97.1
branch 29 38 76.3
condition 15 18 83.3
subroutine 14 14 100.0
pod 5 6 83.3
total 165 181 91.1


line stmt bran cond sub pod time code
1             # $Id: Synonym.pm 2014-11-14 erick.antezana $
2             #
3             # Module : Synonym.pm
4             # Purpose : A synonym for this term.
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::Synonym;
11              
12 16     16   9960 use OBO::Core::Dbxref;
  16         28  
  16         376  
13 16     16   5120 use OBO::Core::Def;
  16         32  
  16         420  
14 16     16   4136 use OBO::Util::Set;
  16         35  
  16         388  
15              
16 16     16   116 use Carp;
  16         59  
  16         755  
17 16     16   60 use strict;
  16         30  
  16         372  
18 16     16   55 use warnings;
  16         19  
  16         19392  
19              
20             sub new {
21 2176     2176 0 2427 my $class = shift;
22 2176         2269 my $self = {};
23              
24 2176         3448 $self->{SCOPE} = undef; # required: {exact_synonym, broad_synonym, narrow_synonym, related_synonym}
25 2176         5221 $self->{DEF} = OBO::Core::Def->new(); # required
26 2176         2361 $self->{SYNONYM_TYPE_NAME} = undef; # optional
27              
28 2176         2637 bless ($self, $class);
29 2176         3150 return $self;
30             }
31              
32             =head2 scope
33              
34             Usage - print $synonym->scope() or $synonym->scope("EXACT")
35             Returns - the synonym scope (string)
36             Args - the synonym scope: 'EXACT', 'BROAD', 'NARROW', 'RELATED'
37             Function - gets/sets the synonym scope
38            
39             =cut
40              
41             sub scope {
42 14101 100   14101 1 20430 if ($_[1]) {
43 2186         5010 my $possible_scopes = OBO::Util::Set->new();
44 2186         3322 my @synonym_scopes = ('EXACT', 'BROAD', 'NARROW', 'RELATED');
45 2186         4528 $possible_scopes->add_all(@synonym_scopes);
46 2186 50       4512 if ($possible_scopes->contains($_[1])) {
47 2186         5849 $_[0]->{SCOPE} = $_[1];
48             } else {
49 0         0 croak 'The synonym scope you provided must be one of the following: ', join (', ', @synonym_scopes);
50             }
51             }
52 14101         29875 return $_[0]->{SCOPE};
53             }
54              
55             =head2 def
56              
57             Usage - print $synonym->def() or $synonym->def($def)
58             Returns - the synonym definition (OBO::Core::Def)
59             Args - the synonym definition (OBO::Core::Def)
60             Function - gets/sets the synonym definition
61            
62             =cut
63              
64             sub def {
65 17551 100   17551 1 23750 $_[0]->{DEF} = $_[1] if ($_[1]);
66 17551         34132 return $_[0]->{DEF};
67             }
68              
69             =head2 synonym_type_name
70              
71             Usage - print $synonym->synonym_type_name() or $synonym->synonym_type_name("UK_SPELLING")
72             Returns - the name of the synonym type associated to this synonym
73             Args - the synonym type name (string)
74             Function - gets/sets the synonym name
75            
76             =cut
77              
78             sub synonym_type_name {
79 9006 100   9006 1 13958 $_[0]->{SYNONYM_TYPE_NAME} = $_[1] if ($_[1]);
80 9006         11974 return $_[0]->{SYNONYM_TYPE_NAME};
81             }
82              
83             =head2 def_as_string
84              
85             Usage - $synonym->def_as_string() or $synonym->def_as_string("Here goes the synonym.", "[GOC:elh, PMID:9334324]")
86             Returns - the synonym text (string)
87             Args - the synonym text plus the dbxref list describing the source of this definition
88             Function - gets/sets the definition of this synonym
89            
90             =cut
91              
92             sub def_as_string {
93 14160     14160 1 12941 my $synonym = $_[1];
94 14160         10429 my $dbxref_as_string = $_[2];
95 14160 100 66     25635 if ($synonym && $dbxref_as_string){
96 2164         2481 my $def = $_[0]->{DEF};
97 2164         3862 $def->text($synonym);
98 2164         3871 my $dbxref_set = OBO::Util::DbxrefSet->new();
99              
100 2164         3198 __dbxref($dbxref_set, $dbxref_as_string);
101            
102 2164         4704 $def->dbxref_set($dbxref_set);
103             }
104 1951         2678 my @sorted_dbxrefs = map { $_->[0] } # restore original values
  720         956  
105 1951         3404 sort { $a->[1] cmp $b->[1] } # sort
106 14160         26892 map { [$_, lc($_->as_string)] } # transform: value, sortkey
107             $_[0]->{DEF}->dbxref_set()->get_set();
108            
109 14160         14782 my @result = (); # a Set?
110 14160         15803 foreach my $dbxref (@sorted_dbxrefs) {
111 1951         3070 push @result, $dbxref->as_string();
112             }
113             # min output: "synonym text" [dbxref's]
114             # full output: "synonym text" synonym_scope SYNONYM_TYPE_NAME [dbxref's] <-- to get this use 'OBO::Core::Term::synonym_as_string()'
115 14160         27388 return '"'.$_[0]->{DEF}->text().'"'.' ['.join(', ', @result).']';
116             }
117              
118             =head2 equals
119              
120             Usage - print $synonym->equals($another_synonym)
121             Returns - either 1 (true) or 0 (false)
122             Args - the synonym (OBO::Core::Synonym) to compare with
123             Function - tells whether this synonym is equal to the parameter
124            
125             =cut
126              
127             sub equals {
128 3870     3870 1 3136 my $result = 0;
129 3870 50 33     6175 if ($_[1] && eval { $_[1]->isa('OBO::Core::Synonym') }) {
  3870         12749  
130              
131 3870 50       6067 croak 'The scope of this synonym is undefined.' if (!defined($_[0]->{SCOPE}));
132 3870 50       5670 croak 'The scope of the target synonym is undefined.' if (!defined($_[1]->{SCOPE}));
133            
134 3870   100     10646 $result = (($_[0]->{SCOPE} eq $_[1]->{SCOPE}) && ($_[0]->{DEF}->equals($_[1]->{DEF})));
135            
136 3870         4075 my $s1 = $_[0]->{SYNONYM_TYPE_NAME};
137 3870         3664 my $s2 = $_[1]->{SYNONYM_TYPE_NAME};
138 3870 100 100     11408 if ($s1 || $s2) {
139 24 100 100     88 if (defined $s1 && defined $s2) {
140 5   100     21 $result = $result && ($s1 eq $s2);
141             } else {
142 19         24 $result = 0;
143             }
144             }
145             } else {
146 0         0 croak "An unrecognized object type (not a OBO::Core::Synonym) was found: '", $_[1], "'";
147             }
148 3870         9961 return $result;
149             }
150              
151             sub __dbxref () {
152 2164 50   2164   4436 caller eq __PACKAGE__ or croak "You cannot call this (__dbxref) prived method!";
153             #
154             # $_[0] ==> set
155             # $_[1] ==> dbxref string
156             #
157 2164         1999 my $dbxref_set = $_[0];
158 2164         1779 my $dbxref_as_string = $_[1];
159            
160 2164         7338 $dbxref_as_string =~ s/^\[//;
161 2164         5954 $dbxref_as_string =~ s/\]$//;
162 2164         2042 $dbxref_as_string =~ s/\\,/;;;;/g; # trick to keep the comma's
163 2164         1696 $dbxref_as_string =~ s/\\"/;;;;;/g; # trick to keep the double quote's
164            
165 2164         3573 my @lineas = $dbxref_as_string =~ /\"([^\"]*)\"/g; # get the double-quoted pieces
166 2164         3227 foreach my $l (@lineas) {
167 8         9 my $cp = $l;
168 8         8 $l =~ s/,/;;;;/g; # trick to keep the comma's
169 8         83 $dbxref_as_string =~ s/\Q$cp\E/$l/;
170             }
171            
172 2164         3525 my @dbxrefs = split (',', $dbxref_as_string);
173            
174 2164         5707 my $r_db_acc = qr/([ \*\.\w-]*):([ '\#~\w:\\\+\?\{\}\$\/\(\)\[\]\.=&!%_-]*)/o;
175 2164         3944 my $r_desc = qr/\s+\"([^\"]*)\"/o;
176 2164         3640 my $r_mod = qr/\s+(\{[\w ]+=[\w ]+\})/o;
177            
178 2164         6593 foreach my $entry (@dbxrefs) {
179 278         333 my ($match, $db, $acc, $desc, $mod) = undef;
180 278         702 my $dbxref = OBO::Core::Dbxref->new();
181 278 100       11347 if ($entry =~ m/$r_db_acc$r_desc$r_mod?/) {
    50          
182 8         14 $db = __unescape($1);
183 8         13 $acc = __unescape($2);
184 8         15 $desc = __unescape($3);
185 8 100       21 $mod = __unescape($4) if ($4);
186             } elsif ($entry =~ m/$r_db_acc$r_desc?$r_mod?/) {
187 270         498 $db = __unescape($1);
188 270         409 $acc = __unescape($2);
189 270 50       535 $desc = __unescape($3) if ($3);
190 270 50       487 $mod = __unescape($4) if ($4);
191             } else {
192 0         0 croak "ERROR: Check the 'dbxref' field of '", $entry, "'.";
193             }
194            
195             # set the dbxref:
196 278         1009 $dbxref->name($db.':'.$acc);
197 278 100       541 $dbxref->description($desc) if (defined $desc);
198 278 100       416 $dbxref->modifier($mod) if (defined $mod);
199 278         593 $dbxref_set->add($dbxref);
200             }
201             }
202              
203             sub __unescape {
204 565 50   565   1152 caller eq __PACKAGE__ or croak "You cannot call this (__unescape) prived method!";
205 565         812 my $match = $_[0];
206 565         567 $match =~ s/;;;;;/\\"/g;
207 565         472 $match =~ s/;;;;/\\,/g;
208 565         720 return $match;
209             }
210              
211             1;
212              
213             __END__