| 1 | # UMLS::Similarity::jcn.pm | ||
| 2 | # | ||
| 3 | # Module implementing the semantic relatedness measure described | ||
| 4 | # by Jiang and Conrath (1997) | ||
| 5 | # | ||
| 6 | # Copyright (c) 2009-2011, | ||
| 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 | # Ying Liu, University of Minnesota | ||
| 21 | # liux0935 at umn.edu | ||
| 22 | # | ||
| 23 | # This program is free software; you can redistribute it and/or | ||
| 24 | # modify it under the terms of the GNU General Public License | ||
| 25 | # as published by the Free Software Foundation; either version 2 | ||
| 26 | # of the License, or (at your option) any later version. | ||
| 27 | # | ||
| 28 | # This program is distributed in the hope that it will be useful, | ||
| 29 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 31 | # GNU General Public License for more details. | ||
| 32 | # | ||
| 33 | # You should have received a copy of the GNU General Public License | ||
| 34 | # along with this program; if not, write to | ||
| 35 | # | ||
| 36 | # The Free Software Foundation, Inc., | ||
| 37 | # 59 Temple Place - Suite 330, | ||
| 38 | # Boston, MA 02111-1307, USA. | ||
| 39 | |||
| 40 | |||
| 41 | package UMLS::Similarity::jcn; | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use warnings; | ||
| 45 | use UMLS::Similarity; | ||
| 46 | use UMLS::Similarity::ErrorHandler; | ||
| 47 | |||
| 48 | use vars qw($VERSION); | ||
| 49 | $VERSION = '0.09'; | ||
| 50 | |||
| 51 | my $debug = 0; | ||
| 52 | my $intrinsic = undef; | ||
| 53 | my $originaloption = undef; | ||
| 54 | |||
| 55 | sub new | ||
| 56 | { | ||
| 57 | my $className = shift; | ||
| 58 | my $interface = shift; | ||
| 59 | my $params = shift; | ||
| 60 | |||
| 61 | return undef if(ref $className); | ||
| 62 | |||
| 63 | if($debug) { print STDERR "In UMLS::Similarity::jcn->new()\n"; } | ||
| 64 | |||
| 65 | my $self = {}; | ||
| 66 | |||
| 67 | # Bless the object. | ||
| 68 | bless($self, $className); | ||
| 69 | |||
| 70 | # The backend interface object. | ||
| 71 | $self->{'interface'} = $interface; | ||
| 72 | |||
| 73 | # check the configuration file if defined | ||
| 74 | my $errorhandler = UMLS::Similarity::ErrorHandler->new("jcn", $interface); | ||
| 75 | if(!$errorhandler) { | ||
| 76 | print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n"; | ||
| 77 | exit; | ||
| 78 | } | ||
| 79 | |||
| 80 | if(defined $params->{"intrinsic"}) { | ||
| 81 | $intrinsic = $params->{"intrinsic"}; | ||
| 82 | |||
| 83 | # set the propagation/frequency information | ||
| 84 | $interface->setPropagationParameters($params); | ||
| 85 | |||
| 86 | } | ||
| 87 | else { | ||
| 88 | # set the propagation/frequency information | ||
| 89 | $interface->setPropagationParameters($params); | ||
| 90 | |||
| 91 | # if the icpropagation or icfrequency option is not | ||
| 92 | # defined set the default options for icpropagation | ||
| 93 | if( (!defined $params->{"icpropagation"}) && | ||
| 94 | (!defined $params->{"icfrequency"}) ) { | ||
| 95 | |||
| 96 | print STDERR "Setting default propagation file (jcn)\n"; | ||
| 97 | |||
| 98 | # get the icfrequency file | ||
| 99 | my $icfrequency = ""; foreach my $path (@INC) { | ||
| 100 | if(-e $path."/UMLS/icfrequency.default.dat") { | ||
| 101 | $icfrequency = $path."/UMLS/icfrequency.default.dat"; | ||
| 102 | } | ||
| 103 | elsif(-e $path."\\UMLS\\icfrequency.default.dat") { | ||
| 104 | $icfrequency = $path."\\UMLS\\icfrequency.default.dat"; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | # set the cuilist | ||
| 109 | my $fhash = $interface->getCuiList(); | ||
| 110 | |||
| 111 | # load the frequency counts | ||
| 112 | open(FILE, $icfrequency) || die "Could not open $icfrequency\n"; | ||
| 113 | while(<FILE>) { | ||
| 114 | chomp; | ||
| 115 | my ($cui, $freq) = split/<>/; | ||
| 116 | if(exists ${$fhash}{$cui}) { | ||
| 117 | ${$fhash}{$cui} = $freq; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | # propagate the counts | ||
| 122 | my $phash = $interface->propagateCounts($fhash); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | # check if the original distance score should be returned rather | ||
| 126 | # than the similarity score | ||
| 127 | if(defined $params->{"original"}) { $originaloption = 1; } | ||
| 128 | return $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub getRelatedness | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | return undef if(!defined $self || !ref $self); | ||
| 137 | |||
| 138 | my $concept1 = shift; | ||
| 139 | my $concept2 = shift; | ||
| 140 | |||
| 141 | if($concept1 eq $concept2) { return 1; } | ||
| 142 | |||
| 143 | my $interface = $self->{'interface'}; | ||
| 144 | |||
| 145 | my $ic1; my $ic2; | ||
| 146 | if(defined $intrinsic) { | ||
| 147 | if($intrinsic=~/sanchez/) { | ||
| 148 | $ic1 = $interface->getSanchezIntrinsicIC($concept1); | ||
| 149 | $ic2 = $interface->getSanchezIntrinsicIC($concept2); | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | $ic1 = $interface->getSecoIntrinsicIC($concept1); | ||
| 153 | $ic2 = $interface->getSecoIntrinsicIC($concept2); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | $ic1 = $interface->getIC($concept1); | ||
| 158 | $ic2 = $interface->getIC($concept2); | ||
| 159 | } | ||
| 160 | |||
| 161 | # Check to make certain that the IC for each of the | ||
| 162 | # concepts is greater than zero otherwise return zero | ||
| 163 | # for lack of data | ||
| 164 | if($ic1 <= 0 or $ic2 <= 0) { return -1; } | ||
| 165 | |||
| 166 | # get the lcses of the concepts | ||
| 167 | my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2); | ||
| 168 | |||
| 169 | # get the IC of the lcs with the lowest IC | ||
| 170 | my $iclcs = 0; my $l; | ||
| 171 | foreach my $lcs (@{$lcses}) { | ||
| 172 | my $value = 0; | ||
| 173 | if(defined $intrinsic) { | ||
| 174 | if($intrinsic=~/sanchez/) { | ||
| 175 | $value = $interface->getSanchezIntrinsicIC($lcs); | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $value = $interface->getSecoIntrinsicIC($lcs); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | else { | ||
| 182 | $value = $interface->getIC($lcs); | ||
| 183 | } | ||
| 184 | if($iclcs < $value) { $iclcs = $value; $l = $lcs; } | ||
| 185 | } | ||
| 186 | |||
| 187 | # return -1 | ||
| 188 | if($iclcs <= 0) { return -1; } | ||
| 189 | |||
| 190 | # calculate the distance | ||
| 191 | my $distance = ($ic1 + $ic2) - (2 * $iclcs); | ||
| 192 | |||
| 193 | # if the distance is zero | ||
| 194 | # implies ic1 == ic2 == ic3 (most probably all three represent | ||
| 195 | # the same concept)... i.e. maximum relatedness... i.e. infinity... | ||
| 196 | # We'll return the maximum possible value ("Our infinity"). | ||
| 197 | # Here's how we got our infinity... | ||
| 198 | # distance = ic1 + ic2 - (2 x ic3) | ||
| 199 | # Largest possible value for (1/distance) is infinity, when distance = 0. | ||
| 200 | # That won't work for us... Whats the next value on the list... | ||
| 201 | # the smallest value of distance greater than 0... | ||
| 202 | # Consider the formula again... distance = ic1 + ic2 - (2 x ic3) | ||
| 203 | # We want the value of distance when ic1 or ic2 have information content | ||
| 204 | # slightly more than that of the root (ic3)... (let ic2 == ic3 == 0) | ||
| 205 | # Assume frequency counts of 0.01 less than the frequency count of the | ||
| 206 | # root for computing ic1... | ||
| 207 | # sim = 1/ic1 | ||
| 208 | # sim = 1/(-log((freq(root) - 0.01)/freq(root))) | ||
| 209 | if($distance <= 0) { | ||
| 210 | my $rootFreq = $interface->getFrequency($l); | ||
| 211 | |||
| 212 | # if the root frequency is greater than zero | ||
| 213 | if ($rootFreq > 0.01) { | ||
| 214 | $distance = -log (($rootFreq - 0.01) / $rootFreq); | ||
| 215 | } | ||
| 216 | # otherwise the root frequency is 0 so return 0 for lack of data | ||
| 217 | else { | ||
| 218 | return -1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | if(defined $originaloption) { return $distance; } | ||
| 223 | |||
| 224 | # now calculate the similarity score | ||
| 225 | my $score = -1; | ||
| 226 | if($distance > 0) { | ||
| 227 | $score = 1 / $distance; | ||
| 228 | } | ||
| 229 | |||
| 230 | return $score; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; | ||
| 234 | __END__ |