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