| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | findLeastCommonSubsumer.pl - This program finds the least common subsumer | ||
| 6 | between two concepts. | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | This program takes two terms or CUIs and returns the least common | ||
| 11 | subsumer between them. | ||
| 12 | |||
| 13 | =head1 USAGE | ||
| 14 | |||
| 15 | Usage: findLeastCommonSubsumer.pl [OPTIONS] [CUI1|TERM1] [CUI2|TERM2] | ||
| 16 | |||
| 17 | =head1 INPUT | ||
| 18 | |||
| 19 | =head2 Required Arguments: | ||
| 20 | |||
| 21 | =head3 [CUI1|TERM1] [CUI2|TERM2] | ||
| 22 | |||
| 23 | A TERM or CUI (or some combination) from the Unified | ||
| 24 | Medical Language System | ||
| 25 | |||
| 26 | =head2 Optional Arguments: | ||
| 27 | |||
| 28 | =head3 --config FILE | ||
| 29 | |||
| 30 | This is the configuration file. The format of the configuration | ||
| 31 | file is as follows: | ||
| 32 | |||
| 33 | SAB :: <include|exclude> <source1, source2, ... sourceN> | ||
| 34 | |||
| 35 | REL :: <include|exclude> <relation1, relation2, ... relationN> | ||
| 36 | |||
| 37 | RELA :: <include|exclude> <rela1, rela2, ... relaN> (optional) | ||
| 38 | |||
| 39 | For example, if we wanted to use the MSH vocabulary with only | ||
| 40 | the RB/RN relations, the configuration file would be: | ||
| 41 | |||
| 42 | SAB :: include MSH | ||
| 43 | REL :: include RB, RN | ||
| 44 | RELA :: include inverse_isa, isa | ||
| 45 | |||
| 46 | or | ||
| 47 | |||
| 48 | SAB :: include MSH | ||
| 49 | REL :: exclude PAR, CHD | ||
| 50 | |||
| 51 | If you go to the configuration file directory, there will | ||
| 52 | be example configuration files for the different runs that | ||
| 53 | you have performed. | ||
| 54 | |||
| 55 | =head3 --depth | ||
| 56 | |||
| 57 | The minimum and maximum depth of the least common subsummer | ||
| 58 | |||
| 59 | =head3 --propagation | ||
| 60 | |||
| 61 | The Information Content of the least common subsumer | ||
| 62 | |||
| 63 | =head3 --infile | ||
| 64 | |||
| 65 | A file containing pairs of concepts or terms in the following format: | ||
| 66 | |||
| 67 | term1<>term2 | ||
| 68 | |||
| 69 | or | ||
| 70 | |||
| 71 | cui1<>cui2 | ||
| 72 | |||
| 73 | or | ||
| 74 | |||
| 75 | cui1<>term2 | ||
| 76 | |||
| 77 | or | ||
| 78 | |||
| 79 | term1<>cui2 | ||
| 80 | |||
| 81 | =head3 --debug | ||
| 82 | |||
| 83 | Sets the debug flag for testing | ||
| 84 | |||
| 85 | =head3 --username STRING | ||
| 86 | |||
| 87 | Username is required to access the umls database on MySql | ||
| 88 | unless it was specified in the my.cnf file at installation | ||
| 89 | |||
| 90 | =head3 --password STRING | ||
| 91 | |||
| 92 | Password is required to access the umls database on MySql | ||
| 93 | unless it was specified in the my.cnf file at installation | ||
| 94 | |||
| 95 | =head3 --hostname STRING | ||
| 96 | |||
| 97 | Hostname where mysql is located. DEFAULT: localhost | ||
| 98 | |||
| 99 | =head3 --socket STRING | ||
| 100 | |||
| 101 | The socket your mysql is using. DEFAULT: /tmp/mysql.sock | ||
| 102 | |||
| 103 | =head3 --database STRING | ||
| 104 | |||
| 105 | Database contain UMLS DEFAULT: umls | ||
| 106 | |||
| 107 | =head3 --realtime | ||
| 108 | |||
| 109 | This option will not create a database of the path information | ||
| 110 | for all of concepts in the specified set of sources and relations | ||
| 111 | in the config file but obtain the information for just the | ||
| 112 | input concept | ||
| 113 | |||
| 114 | =head3 --forcerun | ||
| 115 | |||
| 116 | This option will bypass any command prompts such as asking | ||
| 117 | if you would like to continue with the index creation. | ||
| 118 | |||
| 119 | =head3 --debugpath FILE | ||
| 120 | |||
| 121 | This option prints out the path information for debugging | ||
| 122 | purposes. This option is only really available with the | ||
| 123 | --reatime option because otherwise the path information is | ||
| 124 | stored in the database. You can get this information in a | ||
| 125 | file if you use the --verbose option while creating the index. | ||
| 126 | |||
| 127 | =head3 --verbose | ||
| 128 | |||
| 129 | This option will print out the table information to the | ||
| 130 | config file that you specified. | ||
| 131 | |||
| 132 | =head3 --cuilist FILE | ||
| 133 | |||
| 134 | This option takes in a file containing a list of CUIs (one CUI | ||
| 135 | per line) and stores only the path information for those CUIs | ||
| 136 | rather than for all of the CUIs given the specified set of | ||
| 137 | sources and relations | ||
| 138 | |||
| 139 | =head3 --help | ||
| 140 | |||
| 141 | Displays the quick summary of program options. | ||
| 142 | |||
| 143 | =head3 --version | ||
| 144 | |||
| 145 | Displays the version information. | ||
| 146 | |||
| 147 | =head1 OUTPUT | ||
| 148 | |||
| 149 | List of CUIs that are associated with the input term | ||
| 150 | |||
| 151 | =head1 SYSTEM REQUIREMENTS | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item * Perl (version 5.8.5 or better) - http://www.perl.org | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | Bridget T. McInnes, University of Minnesota | ||
| 162 | |||
| 163 | =head1 COPYRIGHT | ||
| 164 | |||
| 165 | Copyright (c) 2007-2009, | ||
| 166 | |||
| 167 | Bridget T. McInnes, University of Minnesota | ||
| 168 | bthomson at cs.umn.edu | ||
| 169 | |||
| 170 | Ted Pedersen, University of Minnesota Duluth | ||
| 171 | tpederse at d.umn.edu | ||
| 172 | |||
| 173 | Siddharth Patwardhan, University of Utah, Salt Lake City | ||
| 174 | sidd@cs.utah.edu | ||
| 175 | |||
| 176 | Serguei Pakhomov, University of Minnesota Twin Cities | ||
| 177 | pakh0002@umn.edu | ||
| 178 | |||
| 179 | This program is free software; you can redistribute it and/or modify it under | ||
| 180 | the terms of the GNU General Public License as published by the Free Software | ||
| 181 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 182 | version. | ||
| 183 | |||
| 184 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 185 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 186 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 187 | |||
| 188 | You should have received a copy of the GNU General Public License along with | ||
| 189 | this program; if not, write to: | ||
| 190 | |||
| 191 | The Free Software Foundation, Inc., | ||
| 192 | 59 Temple Place - Suite 330, | ||
| 193 | Boston, MA 02111-1307, USA. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | ############################################################################### | ||
| 198 | |||
| 199 | # THE CODE STARTS HERE | ||
| 200 | ############################################################################### | ||
| 201 | |||
| 202 | # ================================ | ||
| 203 | # COMMAND LINE OPTIONS AND USAGE | ||
| 204 | # ================================ | ||
| 205 | |||
| 206 | |||
| 207 | use UMLS::Interface; | ||
| 208 | use Getopt::Long; | ||
| 209 | |||
| 210 | eval(GetOptions( "version", "help", "debug", "username=s", "password=s", "hostname=s", "database=s", "socket=s", "config=s", "forcerun", "verbose", "debugpath=s", "cuilist=s", "realtime", "infile=s", "depth", "propagation=s")) or die ("Please check the above mentioned option(s).\n"); | ||
| 211 | |||
| 212 | |||
| 213 | # if help is defined, print out help | ||
| 214 | if( defined $opt_help ) { | ||
| 215 | $opt_help = 1; | ||
| 216 | &showHelp(); | ||
| 217 | exit; | ||
| 218 | } | ||
| 219 | |||
| 220 | # if version is requested, show version | ||
| 221 | if( defined $opt_version ) { | ||
| 222 | $opt_version = 1; | ||
| 223 | &showVersion(); | ||
| 224 | exit; | ||
| 225 | } | ||
| 226 | |||
| 227 | my @fileArray = (); | ||
| 228 | if(defined $opt_infile) { | ||
| 229 | open(FILE, $opt_infile) || die "Could not open infile: $opt_infile\n"; | ||
| 230 | while(<FILE>) { | ||
| 231 | chomp; | ||
| 232 | if($_=~/^\s*$/) { next; } | ||
| 233 | push @fileArray, $_; | ||
| 234 | } | ||
| 235 | close FILE; | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | # At least 2 terms and/or cuis should be given on the command line. | ||
| 239 | if( scalar(@ARGV) < 2 ) { | ||
| 240 | print STDERR "Two terms and/or CUIs are required\n"; | ||
| 241 | &minimalUsageNotes(); | ||
| 242 | exit; | ||
| 243 | } | ||
| 244 | |||
| 245 | my $i1 = shift; | ||
| 246 | my $i2 = shift; | ||
| 247 | |||
| 248 | my $string = "$i1<>$i2"; | ||
| 249 | push @fileArray, $string; | ||
| 250 | } | ||
| 251 | |||
| 252 | my $umls = ""; | ||
| 253 | |||
| 254 | my $precision = 4; | ||
| 255 | my $floatformat = join '', '%', '.', $precision, 'f'; | ||
| 256 | |||
| 257 | my %option_hash = (); | ||
| 258 | |||
| 259 | #$option_hash{"debug"} = 1; | ||
| 260 | |||
| 261 | if(defined $opt_propagation) { | ||
| 262 | $option_hash{"propagation"} = $opt_propagation; | ||
| 263 | } | ||
| 264 | if(defined $opt_debug) { | ||
| 265 | $option_hash{"debug"} = $opt_debug; | ||
| 266 | } | ||
| 267 | if(defined $opt_realtime) { | ||
| 268 | $option_hash{"realtime"} = $opt_realtime; | ||
| 269 | } | ||
| 270 | if(defined $opt_config) { | ||
| 271 | $option_hash{"config"} = $opt_config; | ||
| 272 | } | ||
| 273 | if(defined $opt_forcerun) { | ||
| 274 | $option_hash{"forcerun"} = $opt_forcerun; | ||
| 275 | } | ||
| 276 | if(defined $opt_verbose) { | ||
| 277 | $option_hash{"verbose"} = $opt_verbose; | ||
| 278 | } | ||
| 279 | if(defined $opt_debugpath) { | ||
| 280 | $option_hash{"debugpath"} = $opt_debugpath; | ||
| 281 | } | ||
| 282 | if(defined $opt_cuilist) { | ||
| 283 | $option_hash{"cuilist"} = $opt_cuilist; | ||
| 284 | } | ||
| 285 | if(defined $opt_username) { | ||
| 286 | $option_hash{"username"} = $opt_username; | ||
| 287 | } | ||
| 288 | if(defined $opt_driver) { | ||
| 289 | $option_hash{"driver"} = $opt_driver; | ||
| 290 | } | ||
| 291 | if(defined $opt_database) { | ||
| 292 | $option_hash{"database"} = $opt_database; | ||
| 293 | } | ||
| 294 | if(defined $opt_password) { | ||
| 295 | $option_hash{"password"} = $opt_password; | ||
| 296 | } | ||
| 297 | if(defined $opt_hostname) { | ||
| 298 | $option_hash{"hostname"} = $opt_hostname; | ||
| 299 | } | ||
| 300 | if(defined $opt_socket) { | ||
| 301 | $option_hash{"socket"} = $opt_socket; | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | $umls = UMLS::Interface->new(\%option_hash); | ||
| 306 | die "Unable to create UMLS::Interface object.\n" if(!$umls); | ||
| 307 | |||
| 308 | foreach my $element (@fileArray) { | ||
| 309 | |||
| 310 | my ($input1, $input2) = split/<>/, $element; | ||
| 311 | |||
| 312 | my $c1; my $c2; | ||
| 313 | |||
| 314 | # check if the input are CUIs or terms | ||
| 315 | if( ($input1=~/C[0-9]+/)) { | ||
| 316 | push @{$c1}, $input1; | ||
| 317 | } | ||
| 318 | else { | ||
| 319 | $c1 = $umls->getConceptList($input1); | ||
| 320 | } | ||
| 321 | if( ($input2=~/C[0-9]+/)) { | ||
| 322 | push @{$c2}, $input2; | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | $c2 = $umls->getConceptList($input2); | ||
| 326 | } | ||
| 327 | |||
| 328 | my $printFlag = 0; | ||
| 329 | |||
| 330 | foreach $cui1 (@{$c1}) { | ||
| 331 | foreach $cui2 (@{$c2}) { | ||
| 332 | |||
| 333 | my $t1 = $input1; | ||
| 334 | my $t2 = $input2; | ||
| 335 | |||
| 336 | if($t1=~/C[0-9]+/) { | ||
| 337 | ($t1) = $umls->getAllPreferredTerm($cui1); | ||
| 338 | } | ||
| 339 | |||
| 340 | if($t2=~/C[0-9]+/) { | ||
| 341 | ($t2) = $umls->getAllPreferredTerm($cui2); | ||
| 342 | } | ||
| 343 | |||
| 344 | if(($umls->exists($cui1) == 0) or | ||
| 345 | ($umls->exists($cui2) == 0) ) { next; } | ||
| 346 | |||
| 347 | if($cui1 eq $cui2) { | ||
| 348 | print "\nThe least common subsumer between $t1 ($cui1) and $t2 ($cui2) is $t1 ($cui1) "; | ||
| 349 | if(defined $opt_depth) { | ||
| 350 | my $min = $umls->findMinimumDepth($cui1); | ||
| 351 | my $max = $umls->findMaximumDepth($cui1); | ||
| 352 | print "with a min and max depth of $min and $max "; | ||
| 353 | } | ||
| 354 | if(defined $opt_propagation) { | ||
| 355 | my $ic = sprintf $floatformat, $umls->getIC($cui1); | ||
| 356 | print "with an IC of $ic "; | ||
| 357 | } | ||
| 358 | print "\n"; | ||
| 359 | |||
| 360 | $printFlag = 1; | ||
| 361 | next; | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | my $lcses = $umls->findLeastCommonSubsumer($cui1, $cui2); | ||
| 366 | |||
| 367 | foreach my $lcs (@{$lcses}) { | ||
| 368 | |||
| 369 | my ($t) = $umls->getAllPreferredTerm($lcs); | ||
| 370 | |||
| 371 | print "\nThe least common subsumer between $t1 ($cui1) and $t2 ($cui2) is $t ($lcs) "; | ||
| 372 | if(defined $opt_depth) { | ||
| 373 | my $min = $umls->findMinimumDepth($lcs); | ||
| 374 | my $max = $umls->findMaximumDepth($lcs); | ||
| 375 | print "with a min and max depth of $min and $max "; | ||
| 376 | } | ||
| 377 | if(defined $opt_propagation) { | ||
| 378 | my $ic = sprintf $floatformat, $umls->getIC($lcs); | ||
| 379 | print "with an IC of $ic "; | ||
| 380 | } | ||
| 381 | print "\n"; | ||
| 382 | |||
| 383 | $printFlag = 1; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | } | ||
| 387 | if( !($printFlag) ) { | ||
| 388 | print "\n"; | ||
| 389 | print "There is not a least common subsumer between $input1 and $input2 given the current view of the UMLS.\n\n"; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | ############################################################################## | ||
| 394 | # function to output minimal usage notes | ||
| 395 | ############################################################################## | ||
| 396 | sub minimalUsageNotes { | ||
| 397 | |||
| 398 | print "Usage: findLeastCommonSubsumer.pl [OPTIONS] [CUI1|TERM1] [CUI2|TERM2]\n"; | ||
| 399 | &askHelp(); | ||
| 400 | exit; | ||
| 401 | } | ||
| 402 | |||
| 403 | ############################################################################## | ||
| 404 | # function to output help messages for this program | ||
| 405 | ############################################################################## | ||
| 406 | sub showHelp() { | ||
| 407 | |||
| 408 | |||
| 409 | print "This is a utility that takes as input two Terms or CUIs\n"; | ||
| 410 | print "and returns the Least Common Subsumer between the two.\n\n"; | ||
| 411 | |||
| 412 | print "Usage: findLeastCommonSubsumer.pl [OPTIONS] [CUI1|TERM1] [CUI2|TERM2]\n\n"; | ||
| 413 | |||
| 414 | print "Options:\n\n"; | ||
| 415 | |||
| 416 | print "--infile FILE File containing TERM or CUI pairs\n\n"; | ||
| 417 | |||
| 418 | print "--depth Outputs the depth of the lcs\n\n"; | ||
| 419 | |||
| 420 | print "--propagation FILE Outputs the IC of the lcs\n\n"; | ||
| 421 | |||
| 422 | print "--debug Sets the debug flag for testing.\n\n"; | ||
| 423 | |||
| 424 | print "--username STRING Username required to access mysql\n\n"; | ||
| 425 | |||
| 426 | print "--password STRING Password required to access mysql\n\n"; | ||
| 427 | |||
| 428 | print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n"; | ||
| 429 | |||
| 430 | print "--database STRING Database contain UMLS (DEFAULT: umls)\n\n"; | ||
| 431 | |||
| 432 | print "--socket STRING Socket used by mysql (DEFAULT: /tmp.mysql.sock)\n\n"; | ||
| 433 | |||
| 434 | print "--config FILE Configuration file\n\n"; | ||
| 435 | |||
| 436 | |||
| 437 | print "--realtime This option will not create a database of the\n"; | ||
| 438 | print " path information for all of concepts but just\n"; | ||
| 439 | print " obtain the information for the input concept\n\n"; | ||
| 440 | |||
| 441 | |||
| 442 | print "--forcerun This option will bypass any command \n"; | ||
| 443 | print " prompts such as asking if you would \n"; | ||
| 444 | print " like to continue with the index \n"; | ||
| 445 | print " creation. \n\n"; | ||
| 446 | |||
| 447 | print "--debugpath FILE This option prints out the path\n"; | ||
| 448 | print " information for debugging purposes\n\n"; | ||
| 449 | |||
| 450 | print "--verbose This option prints out table information\n"; | ||
| 451 | print " to a directory in your config directory.\n\n"; | ||
| 452 | print "--cuilist FILE This option takes in a file containing a \n"; | ||
| 453 | print " list of CUIs (one CUI per line) and stores\n"; | ||
| 454 | print " only the path information for those CUIs\n"; | ||
| 455 | print " rather than for all of the CUIs\n\n"; | ||
| 456 | |||
| 457 | print "--version Prints the version number\n\n"; | ||
| 458 | |||
| 459 | print "--help Prints this help message.\n\n"; | ||
| 460 | } | ||
| 461 | |||
| 462 | ############################################################################## | ||
| 463 | # function to output the version number | ||
| 464 | ############################################################################## | ||
| 465 | sub showVersion { | ||
| 466 | print '$Id: findLeastCommonSubsumer.pl,v 1.21 2011/08/29 16:37:03 btmcinnes Exp $'; | ||
| 467 | print "\nCopyright (c) 2008, Ted Pedersen & Bridget McInnes\n"; | ||
| 468 | } | ||
| 469 | |||
| 470 | ############################################################################## | ||
| 471 | # function to output "ask for help" message when user's goofed | ||
| 472 | ############################################################################## | ||
| 473 | sub askHelp { | ||
| 474 | print STDERR "Type findLeastCommonSubsumer.pl --help for help.\n"; | ||
| 475 | } | ||
| 476 |