| 1 | # UMLS::Similarity::res.pm | ||
| 2 | # | ||
| 3 | # Module implementing the semantic relatedness measure described | ||
| 4 | # by Resnik (1995) | ||
| 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::res; | ||
| 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.07'; | ||
| 50 | |||
| 51 | my $debug = 0; | ||
| 52 | my $stoption = 0; | ||
| 53 | my $intrinsic= 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::res->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("res", $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 | # load the propagation information for semantic types | ||
| 88 | elsif(defined $params->{"st"}) { | ||
| 89 | # set the st option | ||
| 90 | $stoption = 1; | ||
| 91 | |||
| 92 | if(defined $params->{"icfrequency"}) { | ||
| 93 | |||
| 94 | # get the file name | ||
| 95 | my $icfrequency = $params->{"icfrequency"}; | ||
| 96 | |||
| 97 | # initialize hte hash | ||
| 98 | my %fhash = (); | ||
| 99 | |||
| 100 | # load the frequency counts | ||
| 101 | open(FILE, $icfrequency) || die "Could not open $icfrequency\n"; | ||
| 102 | while(<FILE>) { | ||
| 103 | chomp; | ||
| 104 | if($_=~/REL|SAB|N/) { next; } | ||
| 105 | my ($st, $freq) = split/<>/; | ||
| 106 | $fhash{$st} = $freq; | ||
| 107 | } | ||
| 108 | |||
| 109 | # if defined smoothing set the smoothing parameter | ||
| 110 | if(defined $params->{"smooth"}) { $interface->setStSmoothing(); } | ||
| 111 | |||
| 112 | # propagate the semantic type counts | ||
| 113 | $interface->propagateStCounts(\%fhash); | ||
| 114 | |||
| 115 | } | ||
| 116 | else { | ||
| 117 | my $icpropagation = ""; | ||
| 118 | # get the icpropagation file if defined | ||
| 119 | if(defined $params->{"icpropagation"}) { | ||
| 120 | $icpropagation = $params->{"icpropagation"}; | ||
| 121 | } | ||
| 122 | # otherwise get the default icpropagation file | ||
| 123 | else { | ||
| 124 | foreach my $path (@INC) { | ||
| 125 | if(-e $path."/UMLS/icpropagation.st.default.dat") { | ||
| 126 | $icpropagation = $path."/UMLS/icpropagation.st.default.dat"; | ||
| 127 | } | ||
| 128 | elsif(-e $path."\\UMLS\\icpropagation.st.default.dat") { | ||
| 129 | $icpropagation = $path."\\UMLS\\icpropagation.st.default.dat"; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | # get the probability counts | ||
| 135 | my %hash = (); | ||
| 136 | open(FILE, $icpropagation) || die "Could not open $icpropagation\n"; | ||
| 137 | while(<FILE>) { | ||
| 138 | chomp; | ||
| 139 | my ($st, $freq) = split/<>/; | ||
| 140 | $hash{$st} = $freq; | ||
| 141 | } | ||
| 142 | |||
| 143 | $interface->loadStPropagationHash(\%hash); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | # load the propagation information for the concepts | ||
| 147 | else { | ||
| 148 | |||
| 149 | # set the propagation/frequency information | ||
| 150 | $interface->setPropagationParameters($params); | ||
| 151 | |||
| 152 | # if the icpropagation or icfrequency option is not | ||
| 153 | # defined set the default options for icpropagation | ||
| 154 | if( (!defined $params->{"icpropagation"}) && | ||
| 155 | (!defined $params->{"icfrequency"}) ) { | ||
| 156 | |||
| 157 | print STDERR "Setting default propagation file (res)\n"; | ||
| 158 | |||
| 159 | # get the icfrequency file | ||
| 160 | my $icfrequency = ""; foreach my $path (@INC) { | ||
| 161 | if(-e $path."/UMLS/icfrequency.default.dat") { | ||
| 162 | $icfrequency = $path."/UMLS/icfrequency.default.dat"; | ||
| 163 | } | ||
| 164 | elsif(-e $path."\\UMLS\\icfrequency.default.dat") { | ||
| 165 | $icfrequency = $path."\\UMLS\\icfrequency.default.dat"; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | # set the cuilist | ||
| 170 | my $fhash = $interface->getCuiList(); | ||
| 171 | |||
| 172 | # load the frequency counts | ||
| 173 | open(FILE, $icfrequency) || die "Could not open $icfrequency\n"; | ||
| 174 | while(<FILE>) { | ||
| 175 | chomp; | ||
| 176 | my ($cui, $freq) = split/<>/; | ||
| 177 | if(exists ${$fhash}{$cui}) { | ||
| 178 | ${$fhash}{$cui} = $freq; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | # propagate the counts | ||
| 183 | my $phash = $interface->propagateCounts($fhash); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | return $self; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | sub getRelatedness | ||
| 191 | { | ||
| 192 | my $self = shift; | ||
| 193 | return undef if(!defined $self || !ref $self); | ||
| 194 | my $concept1 = shift; | ||
| 195 | my $concept2 = shift; | ||
| 196 | |||
| 197 | # set up the interface | ||
| 198 | my $interface = $self->{'interface'}; | ||
| 199 | |||
| 200 | # get the lcses of the concepts | ||
| 201 | my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2); | ||
| 202 | |||
| 203 | # get the ic of the lcs with the lowest ic score | ||
| 204 | my $score = 0; my $l = ""; | ||
| 205 | if($stoption == 1) { | ||
| 206 | foreach my $lcs (@{$lcses}) { | ||
| 207 | my $sts = $interface->getSt($lcs); | ||
| 208 | foreach my $st (@{$sts}) { | ||
| 209 | my $value = $interface->getStIC($st); | ||
| 210 | if($score < $value) { $score = $value; $l = $lcs; } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | elsif(defined $intrinsic) { | ||
| 215 | foreach my $lcs (@{$lcses}) { | ||
| 216 | my $value; | ||
| 217 | if($intrinsic=~/sanchez/) { | ||
| 218 | $value = $interface->getSanchezIntrinsicIC($lcs); | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | $value = $interface->getSecoIntrinsicIC($lcs); | ||
| 222 | } | ||
| 223 | if($score < $value) { $score = $value; $l = $lcs; } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | foreach my $lcs (@{$lcses}) { | ||
| 228 | my $value = $interface->getIC($lcs); | ||
| 229 | if($score < $value) { $score = $value; $l = $lcs; } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | # if the information content is less then zero return -1 | ||
| 234 | if($score <= 0) { return -1; } | ||
| 235 | |||
| 236 | # return that score | ||
| 237 | return $score | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | 1; | ||
| 242 | __END__ |