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-2015 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   13117 use OBO::Core::Dbxref;
  16         39  
  16         457  
13 16     16   7990 use OBO::Core::Def;
  16         41  
  16         465  
14 16     16   6473 use OBO::Util::Set;
  16         38  
  16         400  
15              
16 16     16   84 use Carp;
  16         91  
  16         937  
17 16     16   80 use strict;
  16         45  
  16         362  
18 16     16   79 use warnings;
  16         25  
  16         27690  
19              
20             sub new {
21 2182     2182 0 3336 my $class = shift;
22 2182         3418 my $self = {};
23              
24 2182         4590 $self->{SCOPE} = undef; # required: {exact_synonym, broad_synonym, narrow_synonym, related_synonym}
25 2182         6109 $self->{DEF} = OBO::Core::Def->new(); # required
26 2182         3245 $self->{SYNONYM_TYPE_NAME} = undef; # optional
27              
28 2182         3154 bless ($self, $class);
29 2182         4783 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 14134 100   14134 1 26778 if ($_[1]) {
43 2192         5674 my $possible_scopes = OBO::Util::Set->new();
44 2192         4723 my @synonym_scopes = ('EXACT', 'BROAD', 'NARROW', 'RELATED');
45 2192         5792 $possible_scopes->add_all(@synonym_scopes);
46 2192 50       6119 if ($possible_scopes->contains($_[1])) {
47 2192         8013 $_[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 14134         44012 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 17581 100   17581 1 35817 $_[0]->{DEF} = $_[1] if ($_[1]);
66 17581         49335 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 9033 100   9033 1 18457 $_[0]->{SYNONYM_TYPE_NAME} = $_[1] if ($_[1]);
80 9033         18684 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 14208     14208 1 19969 my $synonym = $_[1];
94 14208         16942 my $dbxref_as_string = $_[2];
95 14208 100 66     35313 if ($synonym && $dbxref_as_string){
96 2170         3281 my $def = $_[0]->{DEF};
97 2170         5569 $def->text($synonym);
98 2170         5732 my $dbxref_set = OBO::Util::DbxrefSet->new();
99              
100 2170         4235 __dbxref($dbxref_set, $dbxref_as_string);
101            
102 2170         6113 $def->dbxref_set($dbxref_set);
103             }
104 1951         3930 my @sorted_dbxrefs = map { $_->[0] } # restore original values
105 720         1418 sort { $a->[1] cmp $b->[1] } # sort
106 1951         5196 map { [$_, lc($_->as_string)] } # transform: value, sortkey
107 14208         39343 $_[0]->{DEF}->dbxref_set()->get_set();
108            
109 14208         24979 my @result = (); # a Set?
110 14208         23737 foreach my $dbxref (@sorted_dbxrefs) {
111 1951         5167 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 14208         38706 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 3885     3885 1 4633 my $result = 0;
129 3885 50 33     9126 if ($_[1] && eval { $_[1]->isa('OBO::Core::Synonym') }) {
  3885         18156  
130              
131 3885 50       8593 croak 'The scope of this synonym is undefined.' if (!defined($_[0]->{SCOPE}));
132 3885 50       7951 croak 'The scope of the target synonym is undefined.' if (!defined($_[1]->{SCOPE}));
133            
134 3885   100     15076 $result = (($_[0]->{SCOPE} eq $_[1]->{SCOPE}) && ($_[0]->{DEF}->equals($_[1]->{DEF})));
135            
136 3885         6024 my $s1 = $_[0]->{SYNONYM_TYPE_NAME};
137 3885         5297 my $s2 = $_[1]->{SYNONYM_TYPE_NAME};
138 3885 100 100     15457 if ($s1 || $s2) {
139 24 100 100     122 if (defined $s1 && defined $s2) {
140 5   100     28 $result = $result && ($s1 eq $s2);
141             } else {
142 19         32 $result = 0;
143             }
144             }
145             } else {
146 0         0 croak "An unrecognized object type (not a OBO::Core::Synonym) was found: '", $_[1], "'";
147             }
148 3885         15841 return $result;
149             }
150              
151             sub __dbxref () {
152 2170 50   2170   5444 caller eq __PACKAGE__ or croak "You cannot call this (__dbxref) prived method!";
153             #
154             # $_[0] ==> set
155             # $_[1] ==> dbxref string
156             #
157 2170         2451 my $dbxref_set = $_[0];
158 2170         3086 my $dbxref_as_string = $_[1];
159            
160 2170         7804 $dbxref_as_string =~ s/^\[//;
161 2170         7395 $dbxref_as_string =~ s/\]$//;
162 2170         3163 $dbxref_as_string =~ s/\\,/;;;;/g; # trick to keep the comma's
163 2170         2557 $dbxref_as_string =~ s/\\"/;;;;;/g; # trick to keep the double quote's
164            
165 2170         3711 my @lineas = $dbxref_as_string =~ /\"([^\"]*)\"/g; # get the double-quoted pieces
166 2170         3719 foreach my $l (@lineas) {
167 8         13 my $cp = $l;
168 8         14 $l =~ s/,/;;;;/g; # trick to keep the comma's
169 8         83 $dbxref_as_string =~ s/\Q$cp\E/$l/;
170             }
171            
172 2170         3925 my @dbxrefs = split (',', $dbxref_as_string);
173            
174 2170         6723 my $r_db_acc = qr/([ \*\.\w-]*):([ '\#~\w:\\\+\?\{\}\$\/\(\)\[\]\.=&!%_-]*)/o;
175 2170         5640 my $r_desc = qr/\s+\"([^\"]*)\"/o;
176 2170         5402 my $r_mod = qr/\s+(\{[\w ]+=[\w ]+\})/o;
177            
178 2170         8062 foreach my $entry (@dbxrefs) {
179 278         513 my ($match, $db, $acc, $desc, $mod) = undef;
180 278         884 my $dbxref = OBO::Core::Dbxref->new();
181 278 100       17301 if ($entry =~ m/$r_db_acc$r_desc$r_mod?/) {
    50          
182 8         22 $db = __unescape($1);
183 8         21 $acc = __unescape($2);
184 8         23 $desc = __unescape($3);
185 8 100       33 $mod = __unescape($4) if ($4);
186             } elsif ($entry =~ m/$r_db_acc$r_desc?$r_mod?/) {
187 270         571 $db = __unescape($1);
188 270         556 $acc = __unescape($2);
189 270 50       771 $desc = __unescape($3) if ($3);
190 270 50       743 $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         1665 $dbxref->name($db.':'.$acc);
197 278 100       729 $dbxref->description($desc) if (defined $desc);
198 278 100       572 $dbxref->modifier($mod) if (defined $mod);
199 278         830 $dbxref_set->add($dbxref);
200             }
201             }
202              
203             sub __unescape {
204 565 50   565   1329 caller eq __PACKAGE__ or croak "You cannot call this (__unescape) prived method!";
205 565         1285 my $match = $_[0];
206 565         782 $match =~ s/;;;;;/\\"/g;
207 565         773 $match =~ s/;;;;/\\,/g;
208 565         1094 return $match;
209             }
210              
211             1;
212              
213             __END__