blib/lib/UMLS/Similarity/faith.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 156 SCAR 59.4
CC
19
Cov
0%
getRelatedness
132.0
new
90.0
BEGIN
2.0
CRAP 380.0
1 # UMLS::Similarity::faith.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by Pirro and Euzenat (2010)
5 #
6 # Copyright (c) 2009-2015,
7 #
8 # Bridget T McInnes, University of Minnesota, Twin Cities
9 # bthomson at umn.edu
10 #
11 # Siddharth Patwardhan, University of Utah, Salt Lake City
12 # sidd at cs.utah.edu
13 #
14 # Serguei Pakhomov, University of Minnesota, Twin Cities
15 # pakh002 at umn.edu
16 #
17 # Ted Pedersen, University of Minnesota, Duluth
18 # tpederse at d.umn.edu
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of the GNU General Public License
22 # as published by the Free Software Foundation; either version 2
23 # of the License, or (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to
32 #
33 # The Free Software Foundation, Inc.,
34 # 59 Temple Place - Suite 330,
35 # Boston, MA 02111-1307, USA.
36  
37  
38 package UMLS::Similarity::faith;
39  
40 use strict;
41 use warnings;
42 use UMLS::Similarity;
43 use UMLS::Similarity::ErrorHandler;
44  
45 use vars qw($VERSION);
46 $VERSION = '0.07';
47  
48 my $intrinsic = undef;
49 my $debug = 0;
50  
51 sub new
52 {
53 my $className = shift;
54 my $interface = shift;
55 my $params = shift;
56  
57 return undef if(ref $className);
58  
59 if($debug) { print STDERR "In UMLS::Similarity::faith->new()\n"; }
60  
61 my $self = {};
62
63 # Bless the object.
64 bless($self, $className);
65
66 # The backend interface object.
67 $self->{'interface'} = $interface;
68
69 # check the configuration file if defined
70 my $errorhandler = UMLS::Similarity::ErrorHandler->new("faith", $interface);
71 if(!$errorhandler) {
72 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
73 exit;
74 }
75
76 if(defined $params->{"intrinsic"}) {
77 $intrinsic = $params->{"intrinsic"};
78  
79 # set the propagation/frequency information
80 $interface->setPropagationParameters($params);
81  
82 }
83 else {
84 # set the propagation/frequency information
85 $interface->setPropagationParameters($params);
86
87 # if the icpropagation or icfrequency option is not
88 # defined set the default options for icpropagation
89 if( (!defined $params->{"icpropagation"}) &&
90 (!defined $params->{"icfrequency"}) ) {
91
92 print STDERR "Setting default propagation file (faith)\n";
93
94 # get the icfrequency file
95 my $icfrequency = ""; foreach my $path (@INC) {
96 if(-e $path."/UMLS/icfrequency.default.dat") {
97 $icfrequency = $path."/UMLS/icfrequency.default.dat";
98 }
99 elsif(-e $path."\\UMLS\\icfrequency.default.dat") {
100 $icfrequency = $path."\\UMLS\\icfrequency.default.dat";
101 }
102 }
103
104 # set the cuilist
105 my $fhash = $interface->getCuiList();
106
107 # load the frequency counts
108 open(FILE, $icfrequency) || die "Could not open $icfrequency\n";
109 while(<FILE>) {
110 chomp;
111 my ($cui, $freq) = split/<>/;
112 if(exists ${$fhash}{$cui}) {
113 ${$fhash}{$cui} = $freq;
114 }
115 }
116
117 # propagate the counts
118 my $phash = $interface->propagateCounts($fhash);
119 }
120 }
121 return $self;
122 }
123  
124  
125 sub getRelatedness
126 {
127 my $self = shift;
128 return undef if(!defined $self || !ref $self);
129 my $concept1 = shift;
130 my $concept2 = shift;
131  
132 if($concept1 eq $concept2) { return 1; }
133  
134 # set up the interface
135 my $interface = $self->{'interface'};
136
137 my $ic1; my $ic2;
138 if(defined $intrinsic) {
139  
140 if($intrinsic=~/sanchez/) {
141 $ic1 = $interface->getSanchezIntrinsicIC($concept1);
142 $ic2 = $interface->getSanchezIntrinsicIC($concept2);
143 }
144 else {
145 $ic1 = $interface->getSecoIntrinsicIC($concept1);
146 $ic2 = $interface->getSecoIntrinsicIC($concept2);
147 }
148 }
149 else {
150 $ic1 = $interface->getIC($concept1);
151 $ic2 = $interface->getIC($concept2);
152 }
153
154 # if any of the concepts have an ic of zero return -1
155 if( ($ic1 <= 0) || ($ic2 <= 0) ) { return -1; }
156 # get the lcses
157 my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2);
158
159 # get the ic of the lcs with the lowest ic score
160 my $iclcs = 0; my $l = "";
161 foreach my $lcs (@{$lcses}) {
162 my $value;
163 if(defined $intrinsic) {
164  
165 if($intrinsic=~/sanchez/) {
166 $value = $interface->getSanchezIntrinsicIC($lcs);
167 }
168 else {
169 $value = $interface->getSecoIntrinsicIC($lcs);
170 }
171 }
172 else {
173 $value = $interface->getIC($lcs);
174 }
175 if($iclcs < $value) { $iclcs = $value; $l = $lcs; }
176 }
177  
178 # if it is zero just return -1
179 if($iclcs <= 0) { return -1; }
180  
181 # calculate faith
182 my $score = 0;
183 if($ic1 > 0 and $ic2 > 0) {
184 my $a = $iclcs;
185 my $b = $ic1 + $ic2 - $iclcs;
186 $score = $a / $b;
187 }
188
189 return $score
190 }
191  
192  
193 1;
194 __END__