| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | umls-similarity.pl - This program returns a semantic similarity score between two concepts. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | This is a utility that takes as input either two terms (DEFAULT) | ||
| 10 | or two CUIs and returns the similarity between the two. | ||
| 11 | |||
| 12 | =head1 USAGE | ||
| 13 | |||
| 14 | Usage: umls-similarity.pl [OPTIONS] [CUI1|TERM1] [CUI2|TERM2] | ||
| 15 | |||
| 16 | =head1 INPUT | ||
| 17 | |||
| 18 | =head2 [CUI1|TERM1] [CUI2|TERM2] | ||
| 19 | |||
| 20 | The input are two terms or two CUIs associated to concepts in the UMLS. | ||
| 21 | |||
| 22 | =head1 OPTIONS | ||
| 23 | |||
| 24 | Optional command line arguements | ||
| 25 | |||
| 26 | =head2 General Options: | ||
| 27 | |||
| 28 | =head3 --config FILE | ||
| 29 | |||
| 30 | This is the configuration file. There are six configuration options | ||
| 31 | that can be used depending on which measure you are using. The | ||
| 32 | path, wup, cmatch, zhong, lch, lin, jcn and res measures require the SAB | ||
| 33 | and REL options to be set while the vector and lesk measures require | ||
| 34 | the SABDEF and RELDEF options. | ||
| 35 | |||
| 36 | The SAB and REL options are used to determine which sources and | ||
| 37 | relations the path information is to be obtained from. The format | ||
| 38 | of the configuration file is as follows: | ||
| 39 | |||
| 40 | SAB :: <include|exclude> <source1, source2, ... sourceN> | ||
| 41 | |||
| 42 | REL :: <include|exclude> <relation1, relation2, ... relationN> | ||
| 43 | |||
| 44 | For example, if we wanted to use the MSH vocabulary with only | ||
| 45 | the RB/RN relations, the configuration file would be: | ||
| 46 | |||
| 47 | SAB :: include MSH | ||
| 48 | REL :: include RB, RN | ||
| 49 | |||
| 50 | or | ||
| 51 | |||
| 52 | SAB :: include MSH | ||
| 53 | REL :: exclude PAR, CHD | ||
| 54 | |||
| 55 | The SABDEF and RELDEF options are used to determine the sources | ||
| 56 | and relations the extended definition is to be obtained from. | ||
| 57 | We call the definition used by the measure, the extended definition | ||
| 58 | because this may include definitions from related concepts. | ||
| 59 | |||
| 60 | The format of the configuration file is as follows: | ||
| 61 | |||
| 62 | SABDEF :: <include|exclude> <source1, source2, ... sourceN> | ||
| 63 | |||
| 64 | RELDEF :: <include|exclude> <relation1, relation2, ... relationN> | ||
| 65 | |||
| 66 | The possible relations that can be included in RELDEF are: | ||
| 67 | 1. all of the possible relations in MRREL such as PAR, CHD, ... | ||
| 68 | 2. CUI which refers the concepts definition | ||
| 69 | 3. ST which refers to the concepts semantic types definition | ||
| 70 | 4. TERM which refers to the concepts associated terms | ||
| 71 | |||
| 72 | |||
| 73 | For example, if we wanted to use the definitions from MSH vocabulary | ||
| 74 | and we only wanted the definition of the CUI and the definitions of the | ||
| 75 | CUIs SIB relation, the configuration file would be: | ||
| 76 | |||
| 77 | SABDEF :: include MSH | ||
| 78 | RELDEF :: include CUI, SIB | ||
| 79 | |||
| 80 | Note: RELDEF takes any of MRREL relations and two special 'relations': | ||
| 81 | |||
| 82 | 1. CUI which refers to the CUIs definition | ||
| 83 | |||
| 84 | 2. TERM which refers to the terms associated with the CUI | ||
| 85 | |||
| 86 | If you go to the configuration file directory, there will | ||
| 87 | be example configuration files for the different runs that | ||
| 88 | you have performed. | ||
| 89 | |||
| 90 | For more information about the configuration options (including the | ||
| 91 | RELA and RELADEF options) please see the README. | ||
| 92 | |||
| 93 | =head3 --realtime | ||
| 94 | |||
| 95 | This option will not create a database of the path information | ||
| 96 | for all of concepts in the specified set of sources and relations | ||
| 97 | in the config file but obtain the information for just the | ||
| 98 | input concept | ||
| 99 | |||
| 100 | This is option is only available for the path and ic measures. | ||
| 101 | |||
| 102 | =head3 --forcerun | ||
| 103 | |||
| 104 | This option will bypass any command prompts such as asking | ||
| 105 | if you would like to continue with the index creation. | ||
| 106 | |||
| 107 | This is also only necessary for the path and ic measures | ||
| 108 | |||
| 109 | =head3 --measure MEASURE | ||
| 110 | |||
| 111 | Use the MEASURE module to calculate the semantic similarity. The | ||
| 112 | available measure are: | ||
| 113 | 1. Leacock and Chodorow (1998) referred to as lch | ||
| 114 | 2. Wu and Palmer (1994) referred to as wup | ||
| 115 | 3. Zhong, et al. (2002) referred to as zhong | ||
| 116 | 4. The basic path measure referred to as path | ||
| 117 | 5. The undirected path measure referred to as upath | ||
| 118 | 6. Rada, et. al. (1989) referred to as cdist | ||
| 119 | 7. Nguyan and Al-Mubaid (2006) referred to as nam | ||
| 120 | 8. Resnik (1996) referred to as res | ||
| 121 | 9. Lin (1988) referred to as lin | ||
| 122 | 10 Jiang and Conrath (1997) referred to as jcn | ||
| 123 | 11. The vector measure referred to as vector | ||
| 124 | 12. Pekar and Staab (2002) referred to as pks | ||
| 125 | 13. Pirro and Euzenat (2010) referred to as faith | ||
| 126 | 14. Maedche and Staab (2001) referred to as cmatch | ||
| 127 | 15. Batet, et al (2011) referred to as batet | ||
| 128 | 16. S{\'a}nchez, et al. (2012) referred to as sanchez | ||
| 129 | |||
| 130 | =head3 --original | ||
| 131 | |||
| 132 | This returns the original score of the measures proposed by Rada, | ||
| 133 | et al (cdist), Nguyen & Al-Mubaid (nam) and Jiang \& Conrath (jcn). | ||
| 134 | The default returns the reciprocal of these measures in order to | ||
| 135 | use them as similarity measures. | ||
| 136 | |||
| 137 | =head3 --precision N | ||
| 138 | |||
| 139 | Displays values up to N places of decimal. | ||
| 140 | |||
| 141 | =head3 --allsenses | ||
| 142 | |||
| 143 | This option prints out all the possible CUIs pairs and their semantic | ||
| 144 | similarity score if one of the inputs is a term that maps to more than | ||
| 145 | one CUI. Right now we just return the CUIs that are the most similar. | ||
| 146 | |||
| 147 | =head3 --help | ||
| 148 | |||
| 149 | Displays the quick summary of program options. | ||
| 150 | |||
| 151 | =head3 --version | ||
| 152 | |||
| 153 | Displays the version information. | ||
| 154 | |||
| 155 | =head2 Input Options: | ||
| 156 | |||
| 157 | =head3 --infile FILE | ||
| 158 | |||
| 159 | A file containing pairs of concepts or terms in the following format: | ||
| 160 | |||
| 161 | term1<>term2 | ||
| 162 | |||
| 163 | or | ||
| 164 | |||
| 165 | cui1<>cui2 | ||
| 166 | |||
| 167 | or | ||
| 168 | |||
| 169 | cui1<>term2 | ||
| 170 | |||
| 171 | or | ||
| 172 | |||
| 173 | term1<>cui2 | ||
| 174 | |||
| 175 | Unless the --matrix option is chosen then it is just a list of CUIS: | ||
| 176 | cui1 | ||
| 177 | cui2 | ||
| 178 | cui3 | ||
| 179 | ... | ||
| 180 | |||
| 181 | =head3 --matrix | ||
| 182 | |||
| 183 | This option returns a matrix of similarity scores given a file | ||
| 184 | containing a list of CUIs. The file is passed using the --infile | ||
| 185 | option | ||
| 186 | |||
| 187 | =head3 --loadcache FILE | ||
| 188 | |||
| 189 | FILE containing similarity scores of cui pairs in the following | ||
| 190 | format: | ||
| 191 | |||
| 192 | score<>CUI1<>CUI2 | ||
| 193 | |||
| 194 | =head3 --getcache FILE | ||
| 195 | |||
| 196 | Outputs the cache into FILE once the program has finished. | ||
| 197 | |||
| 198 | =head2 Debug Options: | ||
| 199 | |||
| 200 | =head3 --debug | ||
| 201 | |||
| 202 | Sets the UMLS-Interface debug flag on for testing | ||
| 203 | |||
| 204 | =head3 --info | ||
| 205 | |||
| 206 | Displays information about the concept if it doesn't | ||
| 207 | exist in the source. | ||
| 208 | |||
| 209 | =head3 --verbose | ||
| 210 | |||
| 211 | This option will print out the table information to the | ||
| 212 | config file that you specified. | ||
| 213 | |||
| 214 | =head2 Database Options: | ||
| 215 | |||
| 216 | =head3 --username STRING | ||
| 217 | |||
| 218 | Username is required to access the umls database on mysql | ||
| 219 | |||
| 220 | =head3 --password STRING | ||
| 221 | |||
| 222 | Password is required to access the umls database on mysql | ||
| 223 | |||
| 224 | =head3 --hostname STRING | ||
| 225 | |||
| 226 | Hostname where mysql is located. DEFAULT: localhost | ||
| 227 | |||
| 228 | =head3 --socket STRING | ||
| 229 | |||
| 230 | Socket where the mysql.sock or mysqld.sock is located. | ||
| 231 | DEFAULT: mysql.sock | ||
| 232 | |||
| 233 | =head3 --database STRING | ||
| 234 | |||
| 235 | Database contain UMLS DEFAULT: umls | ||
| 236 | |||
| 237 | =head2 Path-based Options | ||
| 238 | |||
| 239 | =head3 --undirected | ||
| 240 | |||
| 241 | The shortest path is undirected. This is only available with | ||
| 242 | the path measure itself using the --realtime option. It is also | ||
| 243 | currently limited to the RB/RN and/or PAR/CHD relations. | ||
| 244 | |||
| 245 | =head2 IC Measure Options: | ||
| 246 | |||
| 247 | =head3 --st | ||
| 248 | |||
| 249 | Uses the information content of the CUIs semantic type to calculate the | ||
| 250 | res measures. If the --icpropagation or --icfrequency files are specified | ||
| 251 | they must contain the probability or frequency counts of the semantic types. | ||
| 252 | If they aren't specified the defaults will be used. | ||
| 253 | |||
| 254 | =head3 --intrinsic [seco|sanchez] | ||
| 255 | |||
| 256 | Uses intrinic information content of the CUIs defined by Sanchez and | ||
| 257 | Betet 2011 or Seco, et al 2004. | ||
| 258 | |||
| 259 | =head3 --icpropagation FILE | ||
| 260 | |||
| 261 | FILE containing the propagation counts of the CUIs. This file must be | ||
| 262 | in the following format: | ||
| 263 | |||
| 264 | CUI<>probability | ||
| 265 | |||
| 266 | where probability is the probability of the concept occurring. | ||
| 267 | |||
| 268 | See create-icpropagation.pl for more information. | ||
| 269 | |||
| 270 | =head3 --icfrequency FILE | ||
| 271 | |||
| 272 | FILE containing frequency counts of CUIs. This file must be in the | ||
| 273 | following format: | ||
| 274 | |||
| 275 | CUI<>freq | ||
| 276 | |||
| 277 | where freq is the frequency in which the concept occurred in some text. See | ||
| 278 | create-icfrequency.pl for more information. | ||
| 279 | |||
| 280 | =head3 --smooth | ||
| 281 | |||
| 282 | Incorporate Laplace smoothing, where the frequency count of each of the | ||
| 283 | concepts in the taxonomy is incremented by one. The advantage of | ||
| 284 | doing this is that it avoids having a concept that has a probability | ||
| 285 | of zero. The disadvantage is that it can shift the overall probability | ||
| 286 | mass of the concepts from what is actually seen in the corpus. | ||
| 287 | |||
| 288 | This can only be used in conjunction with the --icfrequency options | ||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | =head2 Vector Measure Options: | ||
| 293 | |||
| 294 | =head3 --vectormatrix FILE | ||
| 295 | |||
| 296 | This is the matrix file that contains the vector information to | ||
| 297 | use with the vector measure. This is required if you specify vector | ||
| 298 | with the --measure option. | ||
| 299 | |||
| 300 | This file is generated by the vector-input.pl program. An example | ||
| 301 | of this file can be found in the samples/ directory and is called | ||
| 302 | matrix. | ||
| 303 | |||
| 304 | =head3 --vectorindex FILE | ||
| 305 | |||
| 306 | This is the index file that contains the vector information to | ||
| 307 | use with the vector measure. This is required if you specify vector | ||
| 308 | with the --measure option. | ||
| 309 | |||
| 310 | This file is generated by the vector-input.pl program. An example | ||
| 311 | of this file can be found in the samples/ directory and is called | ||
| 312 | index. | ||
| 313 | |||
| 314 | =head3 --debugfile FILE | ||
| 315 | |||
| 316 | This prints the vector information to file, FILE, for debugging | ||
| 317 | purposes. | ||
| 318 | |||
| 319 | =head3 --dictfile FILE | ||
| 320 | |||
| 321 | This is a dictionary file for the vector measure. It contains | ||
| 322 | the 'definitions' of a concept or term which would be used | ||
| 323 | rather than the definitions from the UMLS. If you would like | ||
| 324 | to use dictfile as a augmentation of the UMLS definitions, | ||
| 325 | then use the --config option in conjunction with the --dictfile | ||
| 326 | option. | ||
| 327 | |||
| 328 | |||
| 329 | The expect format for the --dictfile file is: | ||
| 330 | |||
| 331 | CUI: <definition> | ||
| 332 | CUI: <definition> | ||
| 333 | TERM: <definition> | ||
| 334 | TERM: <definition> | ||
| 335 | |||
| 336 | There are three different option configurations that you have with the | ||
| 337 | --dictfile. | ||
| 338 | |||
| 339 | 1. No --dictfile - which will use the UMLS definitions | ||
| 340 | |||
| 341 | umls-similarity.pl --measure lesk hand foot | ||
| 342 | |||
| 343 | 2. --dictfile - which will just use the dictfile definitions | ||
| 344 | |||
| 345 | umls-similarity.pl --measure lesk --dictfile samples/dictfile hand foot | ||
| 346 | |||
| 347 | 3. --dictfile + --config - which will use both the UMLS and dictfile | ||
| 348 | definitions | ||
| 349 | |||
| 350 | umls-similarity.pl --measure lesk --dictfile samples/dictfile --config | ||
| 351 | configuration hand foot | ||
| 352 | |||
| 353 | Keep in mind, when using this file with the --config option, if | ||
| 354 | one of the CUIs or terms that you are obtaining the similarity | ||
| 355 | for does not exist in the file the vector will be empty which | ||
| 356 | will lead to strange similarity scores. | ||
| 357 | |||
| 358 | An example of this file can be found in the samples/ directory | ||
| 359 | and is called dictfile. | ||
| 360 | |||
| 361 | =head3 --defraw | ||
| 362 | |||
| 363 | This is a flag for the vector measures. The definitions | ||
| 364 | used are 'cleaned'. If the --defraw flag is set they will not be | ||
| 365 | cleaned. | ||
| 366 | |||
| 367 | =head3 --stoplist FILE | ||
| 368 | |||
| 369 | A file containing a list of words to be excluded from the features in | ||
| 370 | the lesk and vector method on a word by word basis. The format required | ||
| 371 | is one stopword per line, words are in the regular expression format. | ||
| 372 | For example: | ||
| 373 | |||
| 374 | /\b[a-zA-Z]\b/ | ||
| 375 | /\b[aA]board\b/ | ||
| 376 | /\b[aA]bout\b/ | ||
| 377 | /\b[aA]bove\b/ | ||
| 378 | /\b[aA]cross\b/ | ||
| 379 | /\b[aA]fter\b/ | ||
| 380 | /\b[aA]gain\b/ | ||
| 381 | |||
| 382 | The sample file, stoplist-nsp.regex, is under the samples directory. | ||
| 383 | |||
| 384 | =head3 --compoundfile FILE | ||
| 385 | |||
| 386 | This is a compound word file for the vector and lesks measures. | ||
| 387 | It containsthe compound words which we want to consider them as | ||
| 388 | one wordwhen we compare the relatedness. Each compound word is | ||
| 389 | a line in the file and compound words are seperated by space. | ||
| 390 | When using this option with vector, make sure the vectormatrix | ||
| 391 | and vectorindex file are based on the corpus proprocessed by | ||
| 392 | replacing the compound words in the Text-NSP package. An example | ||
| 393 | is under /sample/compoundword.txt | ||
| 394 | |||
| 395 | =head3 --stem | ||
| 396 | |||
| 397 | This is a flag for the vector and lesk method. If the --stem flag is | ||
| 398 | set, definition words are stemmed using the Lingua::Stem::En module. | ||
| 399 | |||
| 400 | =head1 SYSTEM REQUIREMENTS | ||
| 401 | |||
| 402 | =over | ||
| 403 | |||
| 404 | =item * Perl (version 5.8.5 or better) - http://www.perl.org | ||
| 405 | |||
| 406 | =item * UMLS::Interface - http://search.cpan.org/dist/UMLS-Interface | ||
| 407 | |||
| 408 | =item * UMLS::Similarity - http://search.cpan.org/dist/UMLS-Similarity | ||
| 409 | |||
| 410 | =back | ||
| 411 | |||
| 412 | =head1 CONTACT US | ||
| 413 | |||
| 414 | If you have any trouble installing and using UMLS-Similarity, | ||
| 415 | please contact us via the users mailing list : | ||
| 416 | |||
| 417 | umls-similarity@yahoogroups.com | ||
| 418 | |||
| 419 | You can join this group by going to: | ||
| 420 | |||
| 421 | http://tech.groups.yahoo.com/group/umls-similarity/ | ||
| 422 | |||
| 423 | You may also contact us directly if you prefer : | ||
| 424 | |||
| 425 | Bridget T. McInnes: bthomson at cs.umn.edu | ||
| 426 | |||
| 427 | Ted Pedersen : tpederse at d.umn.edu | ||
| 428 | |||
| 429 | =head1 AUTHOR | ||
| 430 | |||
| 431 | Bridget T. McInnes, University of Minnesota | ||
| 432 | |||
| 433 | =head1 COPYRIGHT | ||
| 434 | |||
| 435 | Copyright (c) 2007-2011, | ||
| 436 | |||
| 437 | Bridget T. McInnes, University of Minnesota | ||
| 438 | bthomson at cs.umn.edu | ||
| 439 | |||
| 440 | Ted Pedersen, University of Minnesota Duluth | ||
| 441 | tpederse at d.umn.edu | ||
| 442 | |||
| 443 | Siddharth Patwardhan, University of Utah, Salt Lake City | ||
| 444 | sidd at cs.utah.edu | ||
| 445 | |||
| 446 | Serguei Pakhomov, University of Minnesota Twin Cities | ||
| 447 | pakh0002 at umn.edu | ||
| 448 | |||
| 449 | Ying Liu, University of Minnesota Twin Cities | ||
| 450 | liux at umn.edu | ||
| 451 | |||
| 452 | This program is free software; you can redistribute it and/or modify it under | ||
| 453 | the terms of the GNU General Public License as published by the Free Software | ||
| 454 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 455 | version. | ||
| 456 | |||
| 457 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 458 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 459 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 460 | |||
| 461 | You should have received a copy of the GNU General Public License along with | ||
| 462 | this program; if not, write to: | ||
| 463 | |||
| 464 | The Free Software Foundation, Inc., | ||
| 465 | 59 Temple Place - Suite 330, | ||
| 466 | Boston, MA 02111-1307, USA. | ||
| 467 | |||
| 468 | =cut | ||
| 469 | |||
| 470 | ############################################################################### | ||
| 471 | |||
| 472 | # THE CODE STARTS HERE | ||
| 473 | ############################################################################### | ||
| 474 | |||
| 475 | # ================================ | ||
| 476 | # COMMAND LINE OPTIONS AND USAGE | ||
| 477 | # ================================ | ||
| 478 | |||
| 479 | use UMLS::Interface; | ||
| 480 | use Getopt::Long; | ||
| 481 | |||
| 482 | eval(GetOptions( "version", "help", "username=s", "password=s", "hostname=s", "database=s", "freqfile=s", "socket=s", "measure=s", "config=s", "infile=s", "matrix", "dbfile=s", "precision=s", "info", "allsenses", "original", "forcerun", "debug", "verbose", "icfrequency=s", "smooth", "st", "intrinsic=s", "icpropagation=s", "undirected", "realtime", "stoplist=s", "stem", "debugfile=s", "vectormatrix=s", "vectorindex=s", "defraw", "dictfile=s", "doubledef=s", "compoundfile=s", "t", "loadcache=s", "getcache=s", "weighted")) or die ("Please check the above mentioned option(s).\n"); | ||
| 483 | |||
| 484 | |||
| 485 | my $debug = 0; | ||
| 486 | |||
| 487 | # if help is defined, print out help | ||
| 488 | if( defined $opt_help ) { | ||
| 489 | $opt_help = 1; | ||
| 490 | &showHelp(); | ||
| 491 | exit; | ||
| 492 | } | ||
| 493 | |||
| 494 | # if version is requested, show version | ||
| 495 | if( defined $opt_version ) { | ||
| 496 | $opt_version = 1; | ||
| 497 | &showVersion(); | ||
| 498 | exit; | ||
| 499 | } | ||
| 500 | |||
| 501 | # At least 2 terms should be given on the command line. | ||
| 502 | if( !(defined $opt_infile) and (scalar(@ARGV) < 2) ) { | ||
| 503 | print STDERR "At least 2 terms or CUIs should be given on the \n"; | ||
| 504 | print STDERR "command line or use the --infile option\n"; | ||
| 505 | &minimalUsageNotes(); | ||
| 506 | exit; | ||
| 507 | } | ||
| 508 | |||
| 509 | # initialize variables | ||
| 510 | my $icpropagation = ""; | ||
| 511 | my $precision = ""; | ||
| 512 | my $floatformat = ""; | ||
| 513 | my $database = ""; | ||
| 514 | my $hostname = ""; | ||
| 515 | my $socket = ""; | ||
| 516 | my $measure = ""; | ||
| 517 | my $umls = ""; | ||
| 518 | my $noscore = ""; | ||
| 519 | my $infile = ""; | ||
| 520 | my $intrinsic = ""; | ||
| 521 | |||
| 522 | my %similarityHash = (); | ||
| 523 | |||
| 524 | my @input_array = (); | ||
| 525 | |||
| 526 | &checkOptions (); | ||
| 527 | &setOptions (); | ||
| 528 | &loadUMLS (); | ||
| 529 | |||
| 530 | my $meas = &loadMeasures(); | ||
| 531 | |||
| 532 | &loadInput (); | ||
| 533 | &calculateSimilarity(); | ||
| 534 | |||
| 535 | if(defined $opt_getcache) { &getCache(); } | ||
| 536 | |||
| 537 | sub calculateSimilarity { | ||
| 538 | |||
| 539 | if($debug) { print STDERR "In calculateSimilarity\n"; } | ||
| 540 | |||
| 541 | if(defined $opt_matrix) { | ||
| 542 | foreach my $el (@input_array) { | ||
| 543 | print "$el\t"; | ||
| 544 | } | ||
| 545 | print "\n"; | ||
| 546 | } | ||
| 547 | |||
| 548 | my @secondary_array = @input_array; | ||
| 549 | |||
| 550 | foreach my $input1 (@input_array) { | ||
| 551 | |||
| 552 | if(! (defined $opt_matrix) ) { | ||
| 553 | my ($i1, $i2) = split/<>/, $input1; | ||
| 554 | $i1=~s/^\s+//g; $i1=~s/\s+$//g; | ||
| 555 | $i2=~s/^\s+//g; $i2=~s/\s+$//g; | ||
| 556 | |||
| 557 | $input1 = $i1; | ||
| 558 | @secondary_array = (); | ||
| 559 | push @secondary_array, $i2; | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | print "$input1\t"; | ||
| 563 | } | ||
| 564 | |||
| 565 | foreach $input2 (@secondary_array) { | ||
| 566 | |||
| 567 | my $c1 = undef; my $c2 = undef; | ||
| 568 | |||
| 569 | my $cui_flag1 = 0; | ||
| 570 | my $cui_flag2 = 0; | ||
| 571 | |||
| 572 | # if lesk or vector measures are being used check if the dictfile | ||
| 573 | # option is set without the config option - if it is then the measures | ||
| 574 | # will take the terms as input - don't map them to cuis in the umls. | ||
| 575 | # the definitions used by these measures are coming from the dictfile | ||
| 576 | # not the umls therefore the actual terms are required by the measures. | ||
| 577 | if( ($measure=~/(lesk|vector|o1vector)/) && (defined $opt_dictfile) ) { | ||
| 578 | if( (defined $opt_dictfile) && (defined $opt_config) ) { | ||
| 579 | |||
| 580 | if($input1=~/C[0-9]+/) { push @{$c1}, $input1; } | ||
| 581 | else { $c1 = $umls->getDefConceptList($input1); } | ||
| 582 | |||
| 583 | if($input2=~/C[0-9]+/) { push @{$c2}, $input2; } | ||
| 584 | else { $c2 = $umls->getDefConceptList($input2); } | ||
| 585 | |||
| 586 | for my $i (0..$#{$c1}) { ${$c1}[$i] .= "#$input1"; } | ||
| 587 | for my $i (0..$#{$c2}) { ${$c2}[$i] .= "#$input2"; } | ||
| 588 | |||
| 589 | if($#{$c1} < 0) { push @{$c1}, $input1; } | ||
| 590 | if($#{$c2} < 0) { push @{$c2}, $input2; } | ||
| 591 | |||
| 592 | |||
| 593 | } | ||
| 594 | else { | ||
| 595 | push @{$c1}, $input1; | ||
| 596 | push @{$c2}, $input2; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | # otherwise get the cuis for the input terms unless they are | ||
| 600 | # already cuis then just add them to the mapping arrays | ||
| 601 | else { | ||
| 602 | if($input1=~/^C[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/) { | ||
| 603 | push @{$c1}, $input1; | ||
| 604 | $cui_flag1 = 1; | ||
| 605 | } | ||
| 606 | elsif($measure=~/(lesk|vector|o1vector)/) { | ||
| 607 | $c1 = $umls->getDefConceptList($input1); | ||
| 608 | } | ||
| 609 | else { | ||
| 610 | $c1 = $umls->getConceptList($input1); | ||
| 611 | } | ||
| 612 | |||
| 613 | # check if input2 contains cuis | ||
| 614 | if($input2=~/^C[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/) { | ||
| 615 | push @{$c2}, $input2; | ||
| 616 | $cui_flag2 = 1; | ||
| 617 | } | ||
| 618 | elsif($measure=~/(lesk|vector|o1vector)/) { | ||
| 619 | $c2 = $umls->getDefConceptList($input2); | ||
| 620 | } | ||
| 621 | else { | ||
| 622 | $c2 = $umls->getConceptList($input2); | ||
| 623 | } | ||
| 624 | } | ||
| 625 | |||
| 626 | my $t1 = $input1; my $t2 = $input2; | ||
| 627 | if($cui_flag1) { | ||
| 628 | $t1 = $umls->getAllPreferredTerm($input1); | ||
| 629 | } | ||
| 630 | if($cui_flag2) { | ||
| 631 | $t2 = $umls->getAllPreferredTerm($input2); | ||
| 632 | } | ||
| 633 | |||
| 634 | # get the similarity between the concepts | ||
| 635 | foreach my $cc1 (@{$c1}) { | ||
| 636 | foreach my $cc2 (@{$c2}) { | ||
| 637 | if(exists $similarityHash{$cc1}{$cc2}) { next; } | ||
| 638 | if(exists $similarityHash{$cc2}{$cc1}) { | ||
| 639 | $similarityHash{$cc1}{$cc2} = $similarityHash{$cc2}{$cc1}; | ||
| 640 | next; | ||
| 641 | } | ||
| 642 | my $score = ""; | ||
| 643 | $value = $meas->getRelatedness($cc1, $cc2); | ||
| 644 | $score = sprintf $floatformat, $value; | ||
| 645 | $similarityHash{$cc1}{$cc2} = $score; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | # find the maximum score | ||
| 650 | # find the minimum score | ||
| 651 | my $max_cc1 = ""; my $max_cc2 = ""; my $max_score = -1; | ||
| 652 | my $min_cc1 = ""; my $min_cc2 = ""; my $min_score = 999; | ||
| 653 | foreach my $concept1 (@{$c1}) { | ||
| 654 | foreach my $concept2 (@{$c2}) { | ||
| 655 | if($max_score <= $similarityHash{$concept1}{$concept2}) { | ||
| 656 | $max_score = $similarityHash{$concept1}{$concept2}; | ||
| 657 | $max_cc1 = $concept1; | ||
| 658 | $max_cc2 = $concept2; | ||
| 659 | } | ||
| 660 | if($min_score > $similarityHash{$concept1}{$concept2}) { | ||
| 661 | $min_score = $similarityHash{$concept1}{$concept2}; | ||
| 662 | $min_cc1 = $concept1; | ||
| 663 | $min_cc2 = $concept2; | ||
| 664 | } | ||
| 665 | } | ||
| 666 | } | ||
| 667 | |||
| 668 | my $score = 0; my $cc1 = ""; my $cc2 = ""; | ||
| 669 | if($measure eq "nam") { | ||
| 670 | $score = $min_score; | ||
| 671 | $cc1 = $min_cc1; | ||
| 672 | $cc2 = $min_cc2; | ||
| 673 | } | ||
| 674 | else { | ||
| 675 | $score = $max_score; | ||
| 676 | $cc1 = $max_cc1; | ||
| 677 | $cc2 = $max_cc2; | ||
| 678 | } | ||
| 679 | |||
| 680 | # print the matrix | ||
| 681 | if(defined $opt_matrix) { print "$score\t"; } | ||
| 682 | # print all the concepts and their scores | ||
| 683 | elsif( (defined $opt_allsenses) && ($cc1 ne "" or $cc2 ne "") ) { | ||
| 684 | foreach my $cc1 (sort keys %similarityHash) { | ||
| 685 | foreach my $cc2 (sort keys %{$similarityHash{$cc1}}) { | ||
| 686 | if(defined $opt_dictfile) { | ||
| 687 | if(defined $opt_config) { | ||
| 688 | $cc1=~/(C[0-9]+)\#/; | ||
| 689 | my $a1 = $1; my $a2 = $'; | ||
| 690 | $cc2=~/(C[0-9]+)\#/; | ||
| 691 | my $b1 = $1; my $b2 = $'; | ||
| 692 | if($a2=~/C[0-9]+/) { $a2 = $t1; } | ||
| 693 | if($b2=~/C[0-9]+/) { $b2 = $t2; } | ||
| 694 | print "$score<>$a2($a1)<>$b2($b1)\n"; | ||
| 695 | } | ||
| 696 | else { | ||
| 697 | print "$score<>$cc1<>$cc2\n"; | ||
| 698 | } | ||
| 699 | } | ||
| 700 | elsif($cui_flag1 and $cui_flag2){ print "$score<>$cc1($t1)<>$cc2($t2)\n"; } | ||
| 701 | elsif($cui_flag1) { print "$score<>$t1($cc1)<>$input2($cc2)\n"; } | ||
| 702 | elsif($cui_flag2) { print "$score<>$input1($cc1)<>$t2($cc2)\n"; } | ||
| 703 | else { print "$score<>$input1($cc1)<>$input2($cc2)\n"; } | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | # print the most similar concepts and the score | ||
| 708 | elsif($cc1 ne "" or $cc2 ne "") { | ||
| 709 | if(defined $opt_dictfile) { | ||
| 710 | if($cc1=~/(C[0-9]+)\#/) { | ||
| 711 | my $a1 = $1; my $a2 = $'; #' | ||
| 712 | if($a2=~/C[0-9]+/) { $a2 = $t1; } | ||
| 713 | $cc1 = "$a2($a1)"; | ||
| 714 | } | ||
| 715 | if($cc2=~/(C[0-9]+)\#/) { | ||
| 716 | my $b1 = $1; my $b2 = $'; #' | ||
| 717 | if($b2=~/C[0-9]+/) { $b2 = $t2; } | ||
| 718 | $cc2 = "$b2($b1)"; | ||
| 719 | } | ||
| 720 | print "$score<>$cc1<>$cc2\n"; | ||
| 721 | } | ||
| 722 | elsif($cui_flag1 and $cui_flag2){ print "$score<>$cc1($t1)<>$cc2($t2)\n"; } | ||
| 723 | elsif($cui_flag1) { print "$score<>$t1($cc1)<>$input2($cc2)\n"; } | ||
| 724 | elsif($cui_flag2) { print "$score<>$input1($cc1)<>$t2($cc2)\n"; } | ||
| 725 | else { print "$score<>$input1($cc1)<>$input2($cc2)\n"; } | ||
| 726 | } | ||
| 727 | # there were no concepts to print - one of them was missing a similarity score | ||
| 728 | else { | ||
| 729 | if($#c1 > -1) { | ||
| 730 | foreach my $cc1 (@c1) { | ||
| 731 | if(defined $opt_dictfile){ print "$noscore<>$input1<>$input2\n"; } | ||
| 732 | elsif($cuiflag1) { print "$noscore<>$cc1($t1)<>$input2\n"; } | ||
| 733 | else { print "$noscore<>$t1($cc1)<>$input2\n"; } | ||
| 734 | if($opt_info) { print " => $input2 does not exist\n"; } | ||
| 735 | |||
| 736 | if(! defined $opt_allsenses) { last; } | ||
| 737 | } | ||
| 738 | } | ||
| 739 | elsif($#c2 > -1) { | ||
| 740 | foreach my $cc2 (@c2) { | ||
| 741 | if(defined $opt_dictfile){ print "$noscore<>$input1<>$input2\n"; } | ||
| 742 | elsif($cuiflag1) { print "$noscore<>$input1<>$cc2($t2)\n"; } | ||
| 743 | else { print "$noscore<>$input1<>$t2($cc2)\n"; } | ||
| 744 | if($opt_info) { print " => $input1 does not exist\n"; } | ||
| 745 | |||
| 746 | if(! defined $opt_allsenses) { last; } | ||
| 747 | } | ||
| 748 | } | ||
| 749 | else { | ||
| 750 | print "$noscore<>$input1<>$input2\n"; | ||
| 751 | if($opt_info) { print " => $input2 nor $input1 exist\n"; } | ||
| 752 | } | ||
| 753 | } | ||
| 754 | } | ||
| 755 | # if the matrix is defined - print a new line | ||
| 756 | if(defined $opt_matrix) { print "\n"; } | ||
| 757 | } | ||
| 758 | |||
| 759 | } | ||
| 760 | |||
| 761 | sub loadInput { | ||
| 762 | |||
| 763 | if($debug) { print STDERR "In loadInput\n"; } | ||
| 764 | |||
| 765 | # if file and matrix is defined get the cuis from the input file | ||
| 766 | if( (defined $opt_infile) && (defined $opt_matrix) ) { | ||
| 767 | |||
| 768 | if($debug) { print STDERR "FILE ($opt_infile) DEFINED\n"; } | ||
| 769 | |||
| 770 | open(FILE, $infile) || die "Could not open file: $infile\n"; | ||
| 771 | my $linecounter = 0; | ||
| 772 | while(<FILE>) { | ||
| 773 | chomp; | ||
| 774 | $linecounter++; | ||
| 775 | if($_=~/^\s*$/) { next; } | ||
| 776 | #if($_=~/C[0-9]+/) { | ||
| 777 | push @input_array, $_; | ||
| 778 | #} | ||
| 779 | #else { | ||
| 780 | #print STDERR "There is an error in the input file ($infile)\n"; | ||
| 781 | #print STDERR "on line $linecounter. The input is not in the\n"; | ||
| 782 | #print STDERR "correct format. Here is the input line:\n"; | ||
| 783 | #print STDERR "$_\n\n"; | ||
| 784 | #exit; | ||
| 785 | #} | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | # if file is defined get the terms or cuis from the input file | ||
| 790 | elsif(defined $opt_infile) { | ||
| 791 | |||
| 792 | if($debug) { print STDERR "FILE ($opt_infile) DEFINED\n"; } | ||
| 793 | |||
| 794 | open(FILE, $infile) || die "Could not open file: $infile\n"; | ||
| 795 | my $linecounter = 0; | ||
| 796 | while(<FILE>) { | ||
| 797 | chomp; | ||
| 798 | $linecounter++; | ||
| 799 | if($_=~/^\s*$/) { next; } | ||
| 800 | if($_=~/\<\>/) { | ||
| 801 | # escape the ' character on input if it exists | ||
| 802 | if(! ($_=~/\\\'/)) { $_=~s/'/\\'/g; } | ||
| 803 | |||
| 804 | push @input_array, $_; | ||
| 805 | } | ||
| 806 | else { | ||
| 807 | print STDERR "There is an error in the input file ($infile)\n"; | ||
| 808 | print STDERR "on line $linecounter. The input is not in the\n"; | ||
| 809 | print STDERR "correct format. Here is the input line:\n"; | ||
| 810 | print STDERR "$_\n\n"; | ||
| 811 | exit; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | } | ||
| 815 | # otherwise get them from the command line | ||
| 816 | |||
| 817 | else { | ||
| 818 | if($debug) { print STDERR "Command Line terms/cuis defined\n"; } | ||
| 819 | |||
| 820 | my $i1 = shift @ARGV; | ||
| 821 | my $i2 = shift @ARGV; | ||
| 822 | |||
| 823 | # escape the ' character on input if it exists | ||
| 824 | if(! ($i1=~/\\\'/)) { $i1=~s/'/\\'/g; } | ||
| 825 | if(! ($i2=~/\\\'/)) { $i2=~s/'/\\'/g; } | ||
| 826 | |||
| 827 | if($debug) { print STDERR "INPUT: $i1 $i2\n"; } | ||
| 828 | |||
| 829 | my $input = "$i1<>$i2"; | ||
| 830 | push @input_array, $input; | ||
| 831 | } | ||
| 832 | } | ||
| 833 | |||
| 834 | # load the appropriate measure | ||
| 835 | sub loadMeasures { | ||
| 836 | |||
| 837 | my $meas; | ||
| 838 | |||
| 839 | # initialize the ic and path options hash tables | ||
| 840 | my %icoptions = (); | ||
| 841 | my %pathoptions = (); | ||
| 842 | |||
| 843 | # set the path options if defined | ||
| 844 | if($measure=~/upath/) { | ||
| 845 | if(defined $opt_weighted) { | ||
| 846 | $pathoptions{"weighted"} = $opt_weighted; | ||
| 847 | } | ||
| 848 | } | ||
| 849 | |||
| 850 | # set the information content options if defined | ||
| 851 | if($measure=~/res|jcn|lin|faith/) { | ||
| 852 | if(defined $opt_st) { | ||
| 853 | $icoptions{"st"} = $opt_st; | ||
| 854 | } | ||
| 855 | if(defined $opt_intrinsic) { | ||
| 856 | $icoptions{"intrinsic"} = $opt_intrinsic; | ||
| 857 | } | ||
| 858 | if(defined $opt_icpropagation) { | ||
| 859 | $icoptions{"icpropagation"} = $opt_icpropagation; | ||
| 860 | } | ||
| 861 | if(defined $opt_icfrequency) { | ||
| 862 | $icoptions{"icfrequency"} = $opt_icfrequency; | ||
| 863 | } | ||
| 864 | if(defined $opt_smooth) { | ||
| 865 | $icoptions{"smooth"} = $opt_smooth; | ||
| 866 | } | ||
| 867 | if(defined $opt_realtime) { | ||
| 868 | $icoptions{"realtime"} = $opt_realtime; | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | |||
| 873 | # set the original options if defined | ||
| 874 | if(defined $opt_original) { | ||
| 875 | if($measure=~/jcn/) { | ||
| 876 | $icoptions{"original"} = $opt_original; | ||
| 877 | } | ||
| 878 | if($measure=~/cdist|nam|zhong/) { | ||
| 879 | $pathoptions{"original"} = $opt_original; | ||
| 880 | } | ||
| 881 | } | ||
| 882 | |||
| 883 | # set vector and its options | ||
| 884 | if($measure eq "vector") { | ||
| 885 | require "UMLS/Similarity/vector.pm"; | ||
| 886 | |||
| 887 | my %vectoroptions = (); | ||
| 888 | |||
| 889 | if(defined $opt_doubledef) { | ||
| 890 | $vectoroptions{"doubledef"} = $opt_doubledef; | ||
| 891 | } | ||
| 892 | if(defined $opt_compoundfile) { | ||
| 893 | $vectoroptions{"compoundfile"} = $opt_compoundfile; | ||
| 894 | } | ||
| 895 | if(defined $opt_dictfile) { | ||
| 896 | $vectoroptions{"dictfile"} = $opt_dictfile; | ||
| 897 | } | ||
| 898 | if(defined $opt_config) { | ||
| 899 | $vectoroptions{"config"} = $opt_config; | ||
| 900 | } | ||
| 901 | if(defined $opt_smooth) { | ||
| 902 | $vectoroptions{"smooth"} = $opt_smooth; | ||
| 903 | } | ||
| 904 | if(defined $opt_vectorindex) { | ||
| 905 | $vectoroptions{"vectorindex"} = $opt_vectorindex; | ||
| 906 | } | ||
| 907 | if(defined $opt_debugfile) { | ||
| 908 | $vectoroptions{"debugfile"} = $opt_debugfile; | ||
| 909 | } | ||
| 910 | if(defined $opt_vectormatrix) { | ||
| 911 | $vectoroptions{"vectormatrix"} = $opt_vectormatrix; | ||
| 912 | } | ||
| 913 | if(defined $opt_config) { | ||
| 914 | $vectoroptions{"config"} = $opt_config; | ||
| 915 | } | ||
| 916 | if(defined $opt_defraw) { | ||
| 917 | $vectoroptions{"defraw"} = $opt_defraw; | ||
| 918 | } | ||
| 919 | if(defined $opt_stoplist) { | ||
| 920 | $vectoroptions{"stoplist"} = $opt_stoplist; | ||
| 921 | } | ||
| 922 | if(defined $opt_stem) { | ||
| 923 | $vectoroptions{"stem"} = $opt_stem; | ||
| 924 | } | ||
| 925 | |||
| 926 | $meas = UMLS::Similarity::vector->new($umls,\%vectoroptions); | ||
| 927 | } | ||
| 928 | # load the module implementing the Leacock and | ||
| 929 | # Chodorow (1998) measure | ||
| 930 | if($measure eq "lch") { | ||
| 931 | use UMLS::Similarity::lch; | ||
| 932 | $meas = UMLS::Similarity::lch->new($umls); | ||
| 933 | } | ||
| 934 | # loading the module implementing the Wu and | ||
| 935 | # Palmer (1994) measure | ||
| 936 | if($measure eq "wup") { | ||
| 937 | use UMLS::Similarity::wup; | ||
| 938 | $meas = UMLS::Similarity::wup->new($umls); | ||
| 939 | } | ||
| 940 | # loading the module implementing the Maedche and | ||
| 941 | # Staab (2001) measure | ||
| 942 | if($measure eq "cmatch") { | ||
| 943 | use UMLS::Similarity::cmatch; | ||
| 944 | $meas = UMLS::Similarity::cmatch->new($umls); | ||
| 945 | } | ||
| 946 | # loading the module implementing the Sanchez, et al. | ||
| 947 | # (2012) measure | ||
| 948 | if($measure eq "sanchez") { | ||
| 949 | use UMLS::Similarity::sanchez; | ||
| 950 | $meas = UMLS::Similarity::sanchez->new($umls); | ||
| 951 | } | ||
| 952 | # loading module implementing the Batet, et al. (2011) | ||
| 953 | # measure | ||
| 954 | if($measure eq "batet") { | ||
| 955 | use UMLS::Similarity::batet; | ||
| 956 | $meas = UMLS::Similarity::batet->new($umls); | ||
| 957 | } | ||
| 958 | # loading the module implementing the Pekar and | ||
| 959 | # Staab (2002) measure | ||
| 960 | if($measure eq "pks") { | ||
| 961 | use UMLS::Similarity::pks; | ||
| 962 | $meas = UMLS::Similarity::pks->new($umls); | ||
| 963 | } | ||
| 964 | # loading the module implementing closeness | ||
| 965 | # modification of wup | ||
| 966 | #if($measure eq "closeness") { | ||
| 967 | #use UMLS::Similarity::closeness; | ||
| 968 | #$meas = UMLS::Similarity::closeness->new($umls); | ||
| 969 | #} | ||
| 970 | # loading the module implementing the Zhong | ||
| 971 | # et al (2002) measure | ||
| 972 | if($measure eq "zhong") { | ||
| 973 | use UMLS::Similarity::zhong; | ||
| 974 | $meas = UMLS::Similarity::zhong->new($umls, \%pathoptions); | ||
| 975 | } | ||
| 976 | # loading the module implementing the simple edge counting | ||
| 977 | # measure of semantic relatedness. | ||
| 978 | if($measure eq "path") { | ||
| 979 | use UMLS::Similarity::path; | ||
| 980 | $meas = UMLS::Similarity::path->new($umls); | ||
| 981 | } | ||
| 982 | # loading the module implementing the undirected edge counting | ||
| 983 | # measure of semantic relatedness. | ||
| 984 | if($measure eq "upath") { | ||
| 985 | use UMLS::Similarity::upath; | ||
| 986 | $meas = UMLS::Similarity::upath->new($umls, \%pathoptions); | ||
| 987 | } | ||
| 988 | |||
| 989 | # load the module implementing the Rada, et. al. | ||
| 990 | # (1989) called the Conceptual Distance measure | ||
| 991 | if($measure eq "cdist") { | ||
| 992 | use UMLS::Similarity::cdist; | ||
| 993 | $meas = UMLS::Similarity::cdist->new($umls, \%pathoptions); | ||
| 994 | } | ||
| 995 | # load the module implementing the Nguyen and | ||
| 996 | # Al-Mubaid (2006) measure | ||
| 997 | if($measure eq "nam") { | ||
| 998 | use UMLS::Similarity::nam; | ||
| 999 | $meas = UMLS::Similarity::nam->new($umls, \%pathoptions); | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | # load the module implementing the Resnik (1995) measure | ||
| 1003 | if($measure eq "res") { | ||
| 1004 | use UMLS::Similarity::res; | ||
| 1005 | $meas = UMLS::Similarity::res->new($umls, \%icoptions); | ||
| 1006 | } | ||
| 1007 | # load the module implementing the Jiang and Conrath | ||
| 1008 | # (1997) measure | ||
| 1009 | if($measure eq "jcn") { | ||
| 1010 | use UMLS::Similarity::jcn; | ||
| 1011 | $meas = UMLS::Similarity::jcn->new($umls, \%icoptions); | ||
| 1012 | } | ||
| 1013 | # load the module implementing the Lin (1998) measure | ||
| 1014 | if($measure eq "lin") { | ||
| 1015 | use UMLS::Similarity::lin; | ||
| 1016 | $meas = UMLS::Similarity::lin->new($umls, \%icoptions); | ||
| 1017 | } | ||
| 1018 | # load the module implementing the Perro and Euzenat (2010) measure | ||
| 1019 | if($measure eq "faith") { | ||
| 1020 | use UMLS::Similarity::faith; | ||
| 1021 | $meas = UMLS::Similarity::faith->new($umls, \%icoptions); | ||
| 1022 | } | ||
| 1023 | # load the module implementing the random measure | ||
| 1024 | if($measure eq "random") { | ||
| 1025 | use UMLS::Similarity::random; | ||
| 1026 | $meas = UMLS::Similarity::random->new($umls); | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | # load the module implementing the lesk measure | ||
| 1030 | if($measure eq "lesk") { | ||
| 1031 | use UMLS::Similarity::lesk; | ||
| 1032 | my %leskoptions = (); | ||
| 1033 | |||
| 1034 | if(defined $opt_compoundfile) { | ||
| 1035 | $leskoptions{"compoundfile"} = $opt_compoundfile; | ||
| 1036 | } | ||
| 1037 | if(defined $opt_config) { | ||
| 1038 | $leskoptions{"config"} = $opt_config; | ||
| 1039 | } | ||
| 1040 | if(defined $opt_smooth) { | ||
| 1041 | $leskoptions{"smooth"} = $opt_smooth; | ||
| 1042 | } | ||
| 1043 | if(defined $opt_stoplist) { | ||
| 1044 | $leskoptions{"stoplist"} = $opt_stoplist; | ||
| 1045 | } | ||
| 1046 | if(defined $opt_stem) { | ||
| 1047 | $leskoptions{"stem"} = $opt_stem; | ||
| 1048 | } | ||
| 1049 | if(defined $opt_debugfile) { | ||
| 1050 | $leskoptions{"debugfile"} = $opt_debugfile; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | if(defined $opt_defraw) { | ||
| 1054 | $leskoptions{"defraw"} = $opt_defraw; | ||
| 1055 | } | ||
| 1056 | if(defined $opt_dictfile) { | ||
| 1057 | $leskoptions{"dictfile"} = $opt_dictfile; | ||
| 1058 | } | ||
| 1059 | if(defined $opt_doubledef) { | ||
| 1060 | $leskoptions{"doubledef"} = $opt_doubledef; | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | $meas = UMLS::Similarity::lesk->new($umls,\%leskoptions); | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | if($measure eq "o1vector") { | ||
| 1067 | #use UMLS::Similarity::o1vector; | ||
| 1068 | my %o1vectoroptions = (); | ||
| 1069 | |||
| 1070 | if(defined $opt_compoundfile) { | ||
| 1071 | $o1vectoroptions{"compoundfile"} = $opt_compoundfile; | ||
| 1072 | } | ||
| 1073 | if(defined $opt_config) { | ||
| 1074 | $o1vectoroptions{"config"} = $opt_config; | ||
| 1075 | } | ||
| 1076 | if(defined $opt_smooth) { | ||
| 1077 | $o1vectoroptions{"smooth"} = $opt_smooth; | ||
| 1078 | } | ||
| 1079 | if(defined $opt_stoplist) { | ||
| 1080 | $o1vectoroptions{"stoplist"} = $opt_stoplist; | ||
| 1081 | } | ||
| 1082 | if(defined $opt_stem) { | ||
| 1083 | $o1vectoroptions{"stem"} = $opt_stem; | ||
| 1084 | } | ||
| 1085 | if(defined $opt_debugfile) { | ||
| 1086 | $o1vectoroptions{"debugfile"} = $opt_debugfile; | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | if(defined $opt_defraw) { | ||
| 1090 | $o1vectoroptions{"defraw"} = $opt_defraw; | ||
| 1091 | } | ||
| 1092 | if(defined $opt_dictfile) { | ||
| 1093 | $o1vectoroptions{"dictfile"} = $opt_dictfile; | ||
| 1094 | } | ||
| 1095 | if(defined $opt_doubledef) { | ||
| 1096 | $o1vectoroptions{"doubledef"} = $opt_doubledef; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | #$meas = UMLS::Similarity::o1vector->new($umls,\%o1vectoroptions); | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | die "Unable to create measure object.\n" if(!$meas); | ||
| 1103 | |||
| 1104 | return $meas; | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | # load the UMLS | ||
| 1108 | sub loadUMLS { | ||
| 1109 | |||
| 1110 | if(defined $opt_t) { | ||
| 1111 | $option_hash{"t"} = 1; | ||
| 1112 | } | ||
| 1113 | if(defined $opt_config) { | ||
| 1114 | $option_hash{"config"} = $opt_config; | ||
| 1115 | } | ||
| 1116 | if(defined $opt_debug) { | ||
| 1117 | $option_hash{"debug"} = $opt_debug; | ||
| 1118 | } | ||
| 1119 | if(defined $opt_forcerun) { | ||
| 1120 | $option_hash{"forcerun"} = $opt_forcerun; | ||
| 1121 | } | ||
| 1122 | if(defined $opt_realtime) { | ||
| 1123 | $option_hash{"realtime"} = $opt_realtime; | ||
| 1124 | } | ||
| 1125 | if(defined $opt_verbose) { | ||
| 1126 | $option_hash{"verbose"} = $opt_verbose; | ||
| 1127 | } | ||
| 1128 | if(defined $opt_undirected) { | ||
| 1129 | $option_hash{"undirected"} = $opt_undirected; | ||
| 1130 | } | ||
| 1131 | if(defined $opt_username and defined $opt_password) { | ||
| 1132 | $option_hash{"driver"} = "mysql"; | ||
| 1133 | $option_hash{"database"} = $database; | ||
| 1134 | $option_hash{"username"} = $opt_username; | ||
| 1135 | $option_hash{"password"} = $opt_password; | ||
| 1136 | $option_hash{"hostname"} = $hostname; | ||
| 1137 | $option_hash{"socket"} = $socket; | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | $umls = UMLS::Interface->new(\%option_hash); | ||
| 1141 | die "Unable to create UMLS::Interface object.\n" if(!$umls); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | # checks the user input options | ||
| 1145 | sub checkOptions { | ||
| 1146 | |||
| 1147 | if( (defined $opt_matrix) && !(defined $opt_infile)) { | ||
| 1148 | print STDERR "The file must be specified using the --infile option\n"; | ||
| 1149 | &minimalUsageNotes(); | ||
| 1150 | exit; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | if(defined $opt_measure) { | ||
| 1154 | if($opt_measure=~/\b(path|upath|wup|cmatch|batet|sanchez|pks|closeness|zhong|lch|cdist|nam|vector|res|lin|faith|random|jcn|lesk|o1vector)\b/) { | ||
| 1155 | # good to go | ||
| 1156 | } | ||
| 1157 | else { | ||
| 1158 | print STDERR "The measure ($opt_measure) is not defined for\n"; | ||
| 1159 | print STDERR "the UMLS-Similarity package at this time.\n\n"; | ||
| 1160 | &minimalUsageNotes(); | ||
| 1161 | exit; | ||
| 1162 | } | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | if(defined $opt_stoplist) { | ||
| 1166 | if(! ($opt_measure=~/vector|lesk|o1vector/) ) { | ||
| 1167 | print STDERR "The --stoplist option is only available\n"; | ||
| 1168 | print STDERR "when using the lesk or vector measure.\n\n"; | ||
| 1169 | &minimalUsageNotes(); | ||
| 1170 | exit; | ||
| 1171 | } | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | if(defined $opt_stem) { | ||
| 1175 | if(! ($opt_measure=~/vector|lesk|o1vector/) ) { | ||
| 1176 | print STDERR "The --stem option is only available\n"; | ||
| 1177 | print STDERR "when using the lesk or vector measure.\n\n"; | ||
| 1178 | &minimalUsageNotes(); | ||
| 1179 | exit; | ||
| 1180 | } | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | if(defined $opt_dictfile) { | ||
| 1184 | if(! ($opt_measure=~/vector|lesk|o1vector/) ) { | ||
| 1185 | print STDERR "The --dictfile option is only available\n"; | ||
| 1186 | print STDERR "when using the lesk or vector measure.\n\n"; | ||
| 1187 | &minimalUsageNotes(); | ||
| 1188 | exit; | ||
| 1189 | } | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | if(defined $opt_doubledef) { | ||
| 1193 | if(! ($opt_measure=~/vector|lesk|o1vector/) ) { | ||
| 1194 | print STDERR "The --doubledef option is only available\n"; | ||
| 1195 | print STDERR "when using the lesk or vector measure.\n\n"; | ||
| 1196 | &minimalUsageNotes(); | ||
| 1197 | exit; | ||
| 1198 | } | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | if(defined $opt_debugfile) { | ||
| 1202 | if(! ($opt_measure=~/(vector|lesk|o1vector)/) ) { | ||
| 1203 | print STDERR "The --debugfile option is only available\n"; | ||
| 1204 | print STDERR "when using the lesk or vector measure.\n\n"; | ||
| 1205 | &minimalUsageNotes(); | ||
| 1206 | exit; | ||
| 1207 | } | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | if(defined $opt_vectormatrix and defined $opt_vectorindex) { | ||
| 1211 | if(! ($opt_measure=~/vector/) ) { | ||
| 1212 | print STDERR "The --vectormatrix and --vectorindex options are only\n"; | ||
| 1213 | print STDERR "available when using the vector measure.\n\n"; | ||
| 1214 | &minimalUsageNotes(); | ||
| 1215 | exit; | ||
| 1216 | } | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | if(defined $opt_vectormatrix) { | ||
| 1220 | if(! ($opt_measure=~/vector/) ) { | ||
| 1221 | print STDERR "The --vectormatrix option is only available\n"; | ||
| 1222 | print STDERR "when using the vector measure. \n\n"; | ||
| 1223 | &minimalUsageNotes(); | ||
| 1224 | exit; | ||
| 1225 | } | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | if(defined $opt_compoundfile) { | ||
| 1229 | if((!($opt_measure=~/vector/)) and | ||
| 1230 | (!($opt_measure=~/lesk/)) and | ||
| 1231 | (!($opt_measure=~/o1vector/)) ) { | ||
| 1232 | print STDERR "The --compoundfile option is only available\n"; | ||
| 1233 | print STDERR "when using the vector or lesk measure. \n\n"; | ||
| 1234 | &minimalUsageNotes(); | ||
| 1235 | exit; | ||
| 1236 | } | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | if(defined $opt_vectorindex) { | ||
| 1240 | if(! ($opt_measure=~/vector/) ) { | ||
| 1241 | print STDERR "The --vectorindex option is only available\n"; | ||
| 1242 | print STDERR "when using the vector measure.\n\n"; | ||
| 1243 | &minimalUsageNotes(); | ||
| 1244 | exit; | ||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | # make ceratin if the undirected option is specified that | ||
| 1249 | # it is only for the path option | ||
| 1250 | if($opt_undirected) { | ||
| 1251 | if( (defined $opt_measure) && (!($opt_measure=~/(path|lch|nam)/)) ) { | ||
| 1252 | print STDERR "The --undirected option can only be used with\n"; | ||
| 1253 | print STDERR "the path measure\n"; | ||
| 1254 | &minimalUsageNotes(); | ||
| 1255 | exit; | ||
| 1256 | } | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | # the --smooth option can only be used with the icfrequency options | ||
| 1260 | if(defined $opt_smooth) { | ||
| 1261 | if(!defined $opt_icfrequency) { | ||
| 1262 | print STDERR "The --smooth option can only be used with the\n"; | ||
| 1263 | print STDERR "--icfrequency option.\n\n"; | ||
| 1264 | &minimalUsageNotes(); | ||
| 1265 | exit; | ||
| 1266 | } | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | # the icpropagation and icfrequency options can only be used | ||
| 1270 | # with specific measures | ||
| 1271 | if(defined $opt_icpropagation || defined $opt_icfrequency) { | ||
| 1272 | if( !($opt_measure=~/(res|lin|faith|jcn)/) ) { | ||
| 1273 | print STDERR "The --icpropagation or --icfrequency options\n"; | ||
| 1274 | print STDERR "may only be specified when using the res, lin,\n"; | ||
| 1275 | print STDERR "jcn or faith measures.\n\n"; | ||
| 1276 | &minimalUsageNotes(); | ||
| 1277 | exit; | ||
| 1278 | } | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | |||
| 1282 | # the icpropagation and icfrequency options can only be used | ||
| 1283 | # with specific measures | ||
| 1284 | if(defined $opt_st) { | ||
| 1285 | if( !($opt_measure=~/(res)/) ) { | ||
| 1286 | print STDERR "Currently, the --st option may only be specified when\n"; | ||
| 1287 | print "using the Resnik (res) measure.\n\n"; | ||
| 1288 | &minimalUsageNotes(); | ||
| 1289 | exit; | ||
| 1290 | } | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | |||
| 1294 | if(defined $opt_icpropagation and defined $opt_icfrequency) { | ||
| 1295 | print STDERR "You can not specify both the --icpropagation and\n"; | ||
| 1296 | print STDERR "--icfrequency options at the same time.\n\n"; | ||
| 1297 | &minimalUsageNotes(); | ||
| 1298 | exit; | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | if(defined $opt_icpropagation and defined $opt_intrinsic) { | ||
| 1302 | print STDERR "You can not specify both the --icpropagation and\n"; | ||
| 1303 | print STDERR "--intrinsic options at the same time.\n\n"; | ||
| 1304 | &minimalUsageNotes(); | ||
| 1305 | exit; | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | if(defined $opt_icfrequency and defined $opt_intrinsic) { | ||
| 1309 | print STDERR "You can not specify both the --icfrequency and\n"; | ||
| 1310 | print STDERR "--intrinsic options at the same time.\n\n"; | ||
| 1311 | &minimalUsageNotes(); | ||
| 1312 | exit; | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | if(defined $opt_precision) { | ||
| 1316 | if ($opt_precision !~ /^\d+$/) { | ||
| 1317 | print STDERR "Value for switch --precision should be integer >= 0\n"; | ||
| 1318 | &minimalUsageNotes(); | ||
| 1319 | exit; | ||
| 1320 | } | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | if(defined $opt_original) { | ||
| 1324 | if(! ($opt_measure=~/nam|cdist|jcn|zhong/) ) { | ||
| 1325 | print STDERR "The --original option can only be used for the nam, cdist, jcn or zhong measures\n"; | ||
| 1326 | &minimalUsageNotes(); | ||
| 1327 | exit; | ||
| 1328 | } | ||
| 1329 | } | ||
| 1330 | |||
| 1331 | if(defined $opt_realtime) { | ||
| 1332 | if($opt_measure=~/vector/) { | ||
| 1333 | print STDERR "The --realtime option is only available for the similarity measures\n"; | ||
| 1334 | &minimalUsageNotes(); | ||
| 1335 | exit; | ||
| 1336 | } | ||
| 1337 | } | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | # set user input and default options | ||
| 1341 | sub setOptions { | ||
| 1342 | |||
| 1343 | if($debug) { print STDERR "In setOptions\n"; } | ||
| 1344 | |||
| 1345 | my $default = ""; | ||
| 1346 | my $set = ""; | ||
| 1347 | |||
| 1348 | if(defined $opt_undirected) { | ||
| 1349 | $set .= " --undirected\n"; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | if(defined $opt_intrinsic) { | ||
| 1353 | $set .= " --intrinsic $opt_intrinsic\n"; | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | if(defined $opt_icpropagation) { | ||
| 1357 | $set .= " --icpropagation $opt_icpropagation\n"; | ||
| 1358 | } | ||
| 1359 | |||
| 1360 | if(defined $opt_icfrequency) { | ||
| 1361 | $set .= " --icfrequency $opt_icfrequency\n"; | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | if(defined $opt_smooth) { | ||
| 1365 | $set .= " --smooth\n"; | ||
| 1366 | } | ||
| 1367 | |||
| 1368 | if(defined $opt_debugfile) { | ||
| 1369 | $set .= " --debugfile $opt_debugfile\n"; | ||
| 1370 | } | ||
| 1371 | if(defined $opt_vectormatrix) { | ||
| 1372 | $set .= " --vectormatrix $opt_vectormatrix\n"; | ||
| 1373 | } | ||
| 1374 | |||
| 1375 | if(defined $opt_vectorindex) { | ||
| 1376 | $set .= " --vectorindex $opt_vectorindex\n"; | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | if(defined $opt_dictfile) { | ||
| 1380 | $set .= " --dictfile $opt_dictfile\n"; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | if(defined $opt_doubledef) { | ||
| 1384 | $set .= " --doubledef $opt_doubledef\n"; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | if(defined $opt_compounfile) { | ||
| 1388 | $set .= " --compoundfile $opt_compoundfile\n"; | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | if(defined $opt_defraw) { | ||
| 1392 | $set .= " --defraw\n"; | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | if(defined $opt_stoplist) { | ||
| 1396 | $set .= " --stoplist $opt_stoplist\n"; | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | # check config file | ||
| 1400 | if(defined $opt_config) { | ||
| 1401 | $config = $opt_config; | ||
| 1402 | $set .= " --config $config\n"; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | # check instrinsic file | ||
| 1406 | if(defined $opt_instrinsic) { | ||
| 1407 | $instrinsic = $opt_instrinsic; | ||
| 1408 | $set .= " --instrinsic \n"; | ||
| 1409 | } | ||
| 1410 | |||
| 1411 | # set file | ||
| 1412 | if(defined $opt_infile) { | ||
| 1413 | $infile = $opt_infile; | ||
| 1414 | $set .= " --infile $opt_infile\n"; | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | # set matrix | ||
| 1418 | if(defined $opt_matrix) { | ||
| 1419 | $set .= " --matrix\n"; | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | # set precision | ||
| 1423 | $precision = 4; | ||
| 1424 | if(defined $opt_precision) { | ||
| 1425 | $precision = $opt_precision; | ||
| 1426 | $set .= " --precision $precision\n"; | ||
| 1427 | } | ||
| 1428 | else { | ||
| 1429 | $precision = 4; | ||
| 1430 | $default .= " --precision $precision\n"; | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | |||
| 1434 | # create the floating point conversion format as required by sprintf! | ||
| 1435 | $floatformat = join '', '%', '.', $precision, 'f'; | ||
| 1436 | |||
| 1437 | # set the zero score with appropriate precision | ||
| 1438 | $noscore = sprintf $floatformat, -1; | ||
| 1439 | |||
| 1440 | # set database options | ||
| 1441 | if(defined $opt_username) { | ||
| 1442 | |||
| 1443 | if(defined $opt_username) { | ||
| 1444 | $set .= " --username $opt_username\n"; | ||
| 1445 | } | ||
| 1446 | if(defined $opt_password) { | ||
| 1447 | $set .= " --password XXXXXXX\n"; | ||
| 1448 | } | ||
| 1449 | if(defined $opt_database) { | ||
| 1450 | $database = $opt_database; | ||
| 1451 | $set .= " --database $database\n"; | ||
| 1452 | } | ||
| 1453 | else { | ||
| 1454 | $database = "umls"; | ||
| 1455 | $default .= " --database $database\n"; | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | if(defined $opt_hostname) { | ||
| 1459 | $hostname = $opt_hostname; | ||
| 1460 | $set .= " --hostname $hostname\n"; | ||
| 1461 | } | ||
| 1462 | else { | ||
| 1463 | $hostname = "localhost"; | ||
| 1464 | $default .= " --hostname $hostname\n"; | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | if(defined $opt_socket) { | ||
| 1468 | $socket = $opt_socket; | ||
| 1469 | $set .= " --socket $socket\n"; | ||
| 1470 | } | ||
| 1471 | else { | ||
| 1472 | $socket = "/tmp/mysql.sock\n"; | ||
| 1473 | $default .= " --socket $socket\n"; | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | # set the semantic similarity measure to be used | ||
| 1478 | if(defined $opt_measure) { | ||
| 1479 | $measure = $opt_measure; | ||
| 1480 | $set .= " --measure $measure\n"; | ||
| 1481 | } | ||
| 1482 | else { | ||
| 1483 | $measure = "path"; | ||
| 1484 | $default .= " --measure $measure\n"; | ||
| 1485 | } | ||
| 1486 | |||
| 1487 | if(defined $opt_realtime) { | ||
| 1488 | $set .= " --realtime\n"; | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | if(defined $opt_debug) { | ||
| 1492 | $set .= " --debug\n"; | ||
| 1493 | } | ||
| 1494 | |||
| 1495 | if(defined $opt_verbose) { | ||
| 1496 | $set .= " --verbose\n"; | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | if(defined $opt_info) { | ||
| 1500 | $set .= " --info\n"; | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | if(defined $opt_getcache) { | ||
| 1504 | $set .= " --getcache $opt_getcache\n"; | ||
| 1505 | } | ||
| 1506 | |||
| 1507 | # set cache if defined | ||
| 1508 | if(defined $opt_loadcache) { | ||
| 1509 | if($debug) { print STDERR "Loading Cache\n"; } | ||
| 1510 | |||
| 1511 | open(CACHE, $opt_loadcache) || die "Could not open cache $opt_loadcache\n"; | ||
| 1512 | while(<CACHE>) { | ||
| 1513 | chomp; | ||
| 1514 | my ($score, $c1, $c2) = split/<>/; | ||
| 1515 | $similarityHash{$c1}{$c2} = $score; | ||
| 1516 | $similarityHash{$c2}{$c1} = $score; | ||
| 1517 | } | ||
| 1518 | if($debug) { print STDERR "Finished Loading Cache\n"; } | ||
| 1519 | close CACHE; | ||
| 1520 | $set .= " --loadcache $opt_loadcache\n"; | ||
| 1521 | } | ||
| 1522 | |||
| 1523 | # check settings | ||
| 1524 | if($default eq "") { $default = " No default settings\n"; } | ||
| 1525 | if($set eq "") { $set = " No user defined settings\n"; } | ||
| 1526 | |||
| 1527 | # print options | ||
| 1528 | print STDERR "Default Settings:\n"; | ||
| 1529 | print STDERR "$default\n"; | ||
| 1530 | |||
| 1531 | print STDERR "User Settings:\n"; | ||
| 1532 | print STDERR "$set\n"; | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | # method dumps cache to given file | ||
| 1536 | sub getCache { | ||
| 1537 | |||
| 1538 | open(CACHE, ">$opt_getcache") || die "\n\n ERROR: Could not open cachefile $opt_getcache\n"; | ||
| 1539 | |||
| 1540 | foreach my $c1 (sort keys %similarityHash) { | ||
| 1541 | foreach my $c2 (sort keys %{$similarityHash{$c1}}) { | ||
| 1542 | print CACHE "$similarityHash{$c1}{$c2}<>$c1<>$c2\n"; | ||
| 1543 | } | ||
| 1544 | } | ||
| 1545 | close CACHE; | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | ############################################################################## | ||
| 1549 | # function to output minimal usage notes | ||
| 1550 | ############################################################################## | ||
| 1551 | sub minimalUsageNotes { | ||
| 1552 | |||
| 1553 | print "Usage: umls-similarity.pl [OPTIONS] [TERM1 TERM2] [CUI1 CUI2]\n"; | ||
| 1554 | &askHelp(); | ||
| 1555 | exit; | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | ############################################################################## | ||
| 1559 | # function to output help messages for this program | ||
| 1560 | ############################################################################## | ||
| 1561 | sub showHelp() { | ||
| 1562 | |||
| 1563 | print "This is a utility that takes as input either two terms \n"; | ||
| 1564 | print "or two CUIs from the command line or a file and returns \n"; | ||
| 1565 | print "the similarity between the following measures: \n"; | ||
| 1566 | print " 1. Leacock and Chodorow (1998) referred to as lch\n"; | ||
| 1567 | print " 2. Wu and Palmer (1994) referred to as wup\n"; | ||
| 1568 | print " 3. Zhong, et al. (2002) referred to as zhong\n"; | ||
| 1569 | print " 4. The basic path measure referred to as path\n"; | ||
| 1570 | print " 5. The undirected path measure referred to as upath\n"; | ||
| 1571 | print " 6. Rada, et. al. (1989) referred to as cdist\n"; | ||
| 1572 | print " 7. Nguyan and Al-Mubaid (2006) referred to as nam\n"; | ||
| 1573 | print " 8. Resnik (1996) referred to as res\n"; | ||
| 1574 | print " 9. Lin (1988) referred to as lin\n"; | ||
| 1575 | print " 10. Jiang and Conrath (1997) referred to as jcn\n"; | ||
| 1576 | print " 11. The vector measure referred to as vector\n"; | ||
| 1577 | print " 12. Pekar and Staab (2002) referred to as pks\n"; | ||
| 1578 | print " 13. Perro and Euzenat (2010) referred to as faith\n"; | ||
| 1579 | print " 14. Maedche and Staab (2001) referred to as cmatch\n"; | ||
| 1580 | print " 15. Batet, et al (2011) referred to as batet\n"; | ||
| 1581 | print " 16. S{\'a}nchez, et al. (2012) referred to as sanchez\n\n"; | ||
| 1582 | |||
| 1583 | print "Usage: umls-similarity.pl [OPTIONS] TERM1 TERM2\n\n"; | ||
| 1584 | |||
| 1585 | print "General Options:\n\n"; | ||
| 1586 | |||
| 1587 | print "--config FILE Configuration file\n\n"; | ||
| 1588 | |||
| 1589 | |||
| 1590 | print "--realtime This option finds the path and propagation\n"; | ||
| 1591 | print " information for the similarity measures in \n"; | ||
| 1592 | print " realtime rather than building an index\n\n"; | ||
| 1593 | |||
| 1594 | print "--forcerun This option will bypass any command prompts\n"; | ||
| 1595 | print " such as asking if you would like to continue\n"; | ||
| 1596 | print " with the index creation. Necessary only for the\n"; | ||
| 1597 | print " path based and ic similarity measures\n\n"; | ||
| 1598 | |||
| 1599 | print "--loadcache FILE FILE containing similarity scores of cui pairs.\n\n"; | ||
| 1600 | |||
| 1601 | print "--getcache FILE Outputs the cache into FILE \n\n"; | ||
| 1602 | |||
| 1603 | print "--measure MEASURE The measure to use to calculate the\n"; | ||
| 1604 | print " semantic similarity. (DEFAULT: path)\n\n"; | ||
| 1605 | |||
| 1606 | print "--original Returns the original distance score of the nam,\n"; | ||
| 1607 | print " jcn or cdist measure (rather than the similarity \n"; | ||
| 1608 | print " score which is the default\n\n"; | ||
| 1609 | |||
| 1610 | print "--precision N Displays values upto N places of decimal.\n\n"; | ||
| 1611 | |||
| 1612 | print "--allsenses This option prints out all the possible\n"; | ||
| 1613 | print " CUIs pairs and their semantic similarity\n"; | ||
| 1614 | print " score if one of the inputs is a term that\n"; | ||
| 1615 | print " maps to more than one CUI. Right now we \n"; | ||
| 1616 | print " return the CUIs that are the most similar.\n\n"; | ||
| 1617 | |||
| 1618 | print "--version Prints the version number\n\n"; | ||
| 1619 | |||
| 1620 | print "--help Prints this help message.\n\n"; | ||
| 1621 | |||
| 1622 | print "\n\nInput Options: \n\n"; | ||
| 1623 | |||
| 1624 | print "--infile FILE File containing TERM or CUI pairs\n\n"; | ||
| 1625 | |||
| 1626 | print "--matrix This option returns a matrix of similarity\n"; | ||
| 1627 | print " scores given a file containing a list of \n"; | ||
| 1628 | print " CUIs. File is specified using --infile.\n\n"; | ||
| 1629 | |||
| 1630 | print "\n\nDebug Options:\n\n"; | ||
| 1631 | |||
| 1632 | print "--debug Sets the UMLS-Interface debug flag on\n"; | ||
| 1633 | print " for testing purposes\n\n"; | ||
| 1634 | |||
| 1635 | print "--verbose This option prints out the path information\n"; | ||
| 1636 | print " to a file in your config directory.\n\n"; | ||
| 1637 | |||
| 1638 | print "--info Displays information about a concept if\n"; | ||
| 1639 | print " it doesn't exist in the source.\n\n"; | ||
| 1640 | |||
| 1641 | print "\n\nDatabase Options: \n\n"; | ||
| 1642 | |||
| 1643 | print "--username STRING Username required to access mysql\n\n"; | ||
| 1644 | |||
| 1645 | print "--password STRING Password required to access mysql\n\n"; | ||
| 1646 | |||
| 1647 | print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n"; | ||
| 1648 | print "--socket STRING Socket for mysql (DEFAULT: /tmp/mysql.sock\n\n"; | ||
| 1649 | |||
| 1650 | print "--database STRING Database contain UMLS (DEFAULT: umls)\n\n"; | ||
| 1651 | |||
| 1652 | print "\n\nPath-based Options:\n\n"; | ||
| 1653 | |||
| 1654 | print "--undirected The shortest path is undirected. This is\n"; | ||
| 1655 | print " only available for the path measure.\n\n"; | ||
| 1656 | |||
| 1657 | print "\n\nIC Measure Options:\n\n"; | ||
| 1658 | |||
| 1659 | print "--st Use the information content of the CUIs\n"; | ||
| 1660 | print " semantic type to calculate the res measure\n\n"; | ||
| 1661 | |||
| 1662 | print "--intrinsic [seco|sanchez] Use the intrinsic IC defined either by\n"; | ||
| 1663 | print " Seco et al 2004 or Sanchez et al 2011\n\n"; | ||
| 1664 | |||
| 1665 | print "--icpropagation FILE File containing the information content\n"; | ||
| 1666 | print " of the CUIs.\n\n"; | ||
| 1667 | |||
| 1668 | print "--icfrequency FILE File containing the frequency counts\n"; | ||
| 1669 | print " of the CUIs.\n\n"; | ||
| 1670 | |||
| 1671 | print "--smooth Incorporate LaPlace smoothing. Can only\n"; | ||
| 1672 | print " be used with the --icfrequency option\n\n"; | ||
| 1673 | |||
| 1674 | print "\n\nVector and Lesk Measure Options:\n\n"; | ||
| 1675 | |||
| 1676 | print "--vectormatrix FILE The matrix file containing the vector\n"; | ||
| 1677 | print " information for the vector measure.\n\n"; | ||
| 1678 | |||
| 1679 | print "--vectorindex FILE The index file containing the vector\n"; | ||
| 1680 | print " information for the vector measure.\n\n"; | ||
| 1681 | |||
| 1682 | print "--debugfile FILE This prints the vector or lesk information to file,\n"; | ||
| 1683 | print " FILE, for debugging purposes.\n\n"; | ||
| 1684 | |||
| 1685 | print "--dictfile FILE This is a dictionary file for the vector and lesk\n"; | ||
| 1686 | print " measure. It contains the 'definitions' of a concept\n"; | ||
| 1687 | print " which would be used rather than the definitions from\n"; | ||
| 1688 | print " the UMLS\n\n"; | ||
| 1689 | |||
| 1690 | print "--doubledef FILE This is a dictionary file for the vector and lesk\n"; | ||
| 1691 | print " measure. It finds the definition of every word's definition\n"; | ||
| 1692 | print " of the concept and construct the expanded definition.\n\n"; | ||
| 1693 | |||
| 1694 | print "--compoundfile FILE This is a compound word file for the vector and lesk\n"; | ||
| 1695 | print " measure. It contains the compound word lists.\n"; | ||
| 1696 | print " For the compounds words in the definitions \n"; | ||
| 1697 | print " are treated as a single unit.\n\n"; | ||
| 1698 | |||
| 1699 | print "--stoplist FILE A file containing a list of words to be excluded\n"; | ||
| 1700 | print " from the features in the vector and lesk method.\n\n"; | ||
| 1701 | |||
| 1702 | print "--stem This is a flag for the vector and lesk method. \n"; | ||
| 1703 | print " If the --stem flag is set, words are stemmed. \n\n"; | ||
| 1704 | |||
| 1705 | print "--defraw This is a flag for the vector or lesk measure. The \n"; | ||
| 1706 | print " definitions used are 'cleaned'. If the --defraw\n"; | ||
| 1707 | print " flag is set they will not be cleaned. \n\n"; | ||
| 1708 | |||
| 1709 | |||
| 1710 | } | ||
| 1711 | ############################################################################## | ||
| 1712 | # function to output the version number | ||
| 1713 | ############################################################################## | ||
| 1714 | sub showVersion { | ||
| 1715 | print '$Id: umls-similarity.pl,v 1.113 2015/06/24 19:25:05 btmcinnes Exp $'; | ||
| 1716 | print "\nCopyright (c) 2008-2011, Ted Pedersen & Bridget McInnes\n"; | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | ############################################################################## | ||
| 1720 | # function to output "ask for help" message when user's goofed | ||
| 1721 | ############################################################################## | ||
| 1722 | sub askHelp { | ||
| 1723 | print STDERR "Type umls-similarity.pl --help for help.\n"; | ||
| 1724 | } | ||
| 1725 |