| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | |||
| 4 | #---------------------------PERLDOC STARTS HERE------------------------------------------------------------------ | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | ws-getAllowablePath | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | #--------------------------------------------------------------------------------------------------------------------- | ||
| 13 | |||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | |||
| 18 | This program authenticates user by asking for valid username and password to connect to UMLSKS. Once the user is | ||
| 19 | authenticated program takes two terms from the user and finds the shortest path (semantic | ||
| 20 | distance) and the semantic relatedness(hso) between those two concepts using the heirarchical | ||
| 21 | structure of the UMLSKS Metathesaurus database. The program queries SNOMED-CT database with the CUIs user enters and displays the shortest | ||
| 22 | path along with the concepts through which the two inputs are connected. | ||
| 23 | |||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | =head2 Basic Usage | ||
| 30 | |||
| 31 | =pod | ||
| 32 | |||
| 33 | perl ws-getAllowablePath.pl --verbose 1 -sources SNOMEDCT,MSH --rels PAR --dirs U --config configfilename --login loginfile --log logfile --patterns patternsfile --testfile test_file | ||
| 34 | |||
| 35 | --verbose: Sets verbose flag to true if value is set to 1, and thus displays all the authentication information for the user. | ||
| 36 | |||
| 37 | --sources : UMLS sources can be specified by providing list of sources seperated | ||
| 38 | by comma. These sources will be used to query and retrieve the information. | ||
| 39 | |||
| 40 | --rels : UMLS relations can be specified by providing list of relations seperated | ||
| 41 | by comma. These relations will be used to query and retrieve the information. | ||
| 42 | |||
| 43 | --dirs : Directions for UMLS relations can be specified by providing list of directions seperated | ||
| 44 | by comma. Different directions are U - Up , D - Down and H - Horizontal. | ||
| 45 | These directions will be used to find the allowable path. The sequence of these directions | ||
| 46 | should be same as the sequence of relations provided by the --rels option. | ||
| 47 | |||
| 48 | --config : Instead of providing sources, relations and directions on command line, they can be | ||
| 49 | specified using a configuration file, which can be provided with this option. | ||
| 50 | It takes complete path and name of the file. The config file is expected in following format: | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | =pod | ||
| 55 | |||
| 56 | =over | ||
| 57 | |||
| 58 | =item SAB :: include SNOMEDCT,MSH | ||
| 59 | |||
| 60 | =item REL :: include PAR,RB | ||
| 61 | |||
| 62 | =item DIR :: include U,H | ||
| 63 | |||
| 64 | =item RELA :: include RB-has_part | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | =pod | ||
| 71 | |||
| 72 | --login : User can specify login credentials through the file, which should be of form: | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item username :: xyz | ||
| 77 | |||
| 78 | =item password :: pqr | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | --log : User can spacify the logfile name in which the log will be recorded. | ||
| 83 | --verbose option must be set on to have log generated. | ||
| 84 | |||
| 85 | --patterns : User can specify the set of allowable patterns that should be used while calculating | ||
| 86 | an allowable path. This options accepts a regex inside a file specified by patternsfile. | ||
| 87 | |||
| 88 | The regex may be of form : | ||
| 89 | |||
| 90 | /\bU+\b|\bU+D+\b|\bU+H+\b|\bU+H+D+\b|\bD+\b|\bD+H+\b|\bH+D+\b|\bH+\b/ | ||
| 91 | |||
| 92 | U : upward arrow | ||
| 93 | |||
| 94 | D: downward arrow | ||
| 95 | |||
| 96 | H: Horizontal arrow | ||
| 97 | |||
| 98 | Each regex is seperated from another using | (or). Each regex represents one allowed pattern from the set. | ||
| 99 | |||
| 100 | Right now, the vector length of any size is allowed in the allowed direction, | ||
| 101 | |||
| 102 | For example, | ||
| 103 | |||
| 104 | \bU+\b : means a path which consists of one or more than one upward arrows is allowed. | ||
| 105 | |||
| 106 | --testfile : User can specify the list of test CUIs stored in the test_file throught this option. | ||
| 107 | The program would generate output for all the CUI pairs sequentially. | ||
| 108 | |||
| 109 | Follwing is a sample output | ||
| 110 | |||
| 111 | =over | ||
| 112 | |||
| 113 | =item Enter username to connect to UMLSKS:mchoudhari | ||
| 114 | |||
| 115 | =item Enter password: | ||
| 116 | |||
| 117 | =item Enter first query CUI:C0013378 | ||
| 118 | |||
| 119 | =item Enter second query CUI:C0011167 | ||
| 120 | |||
| 121 | =item First input is a CUI: C0013378 | ||
| 122 | |||
| 123 | =item Second input is a CUI: C0011167 | ||
| 124 | |||
| 125 | =item Final shortest path :Dysgeusia (C0013378) (U)->Taste Perception (C0039336) (U)->Special sensory functions (C0730359) (U)->Esthesia (C0036658) (U)->Nervous System Physiological Phenomena (C0027767) (U)->Function (C0542341) (D)->Digestion (C0012238) (D)->Oral cavity AND/OR esophageal function (C1268925) (D)->Deglutition (C0011167) | ||
| 126 | |||
| 127 | =item Final path cost : 80 | ||
| 128 | |||
| 129 | =item Changes in Direction : 1 | ||
| 130 | |||
| 131 | =item Semantic relatedness(hso) : 9 | ||
| 132 | |||
| 133 | =item Enter first query CUI:stop | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =head2 Modules/Packages | ||
| 138 | |||
| 139 | =pod | ||
| 140 | |||
| 141 | This program uses following packages: | ||
| 142 | |||
| 143 | =over | ||
| 144 | |||
| 145 | =item package GetNeighbors | ||
| 146 | |||
| 147 | sub GetNeighbors::read_object which reads hash reference object passed to this | ||
| 148 | sub and fetches the required Neighbors' information. | ||
| 149 | |||
| 150 | sub GetNeighbors::format_object calls appropriate functions like format_homogenous_hash, | ||
| 151 | format_scalar, format_homogenous_array depending on the object reference it is called with. | ||
| 152 | format_homogenous_hash,format_scalar and format_homogenous_array are subroutines which | ||
| 153 | read the objects they are called with and fetch the desired information. | ||
| 154 | |||
| 155 | =item package ConnectUMLS | ||
| 156 | |||
| 157 | sub ConnectUMLS::get_pt to get the proxy ticket using a web service. | ||
| 158 | |||
| 159 | sub ConnectUMLS::connect_umls to connect to UMLS by sending username | ||
| 160 | and password and getting back a proxy ticket. | ||
| 161 | |||
| 162 | =item package ValidateTerm | ||
| 163 | |||
| 164 | sub ValidateTerm::validateTerm to accepts an input and validates it | ||
| 165 | for as valid or invalid CUI or a term. | ||
| 166 | |||
| 167 | =item package GetUserData | ||
| 168 | |||
| 169 | sub GetUserData::getUserDetails to get username and password from the user. | ||
| 170 | |||
| 171 | =item package Query | ||
| 172 | |||
| 173 | sub Query::runQuery which takes method name, service and other parameters as argument and calls the web service. | ||
| 174 | It also displays the information received from the web service and other error messages. | ||
| 175 | |||
| 176 | =item package FormGraph | ||
| 177 | |||
| 178 | sub FormGraph::form_graph forms graph using standard BFS algorithm and creates a graph | ||
| 179 | using the concepts and their neighbor concepts. It finds shortest path between two | ||
| 180 | input concepts and displays the path. | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | #--------------------------------------------------------------------------------------------------------------------------- | ||
| 187 | |||
| 188 | #------------------------------PERLDOC ENDS HERE------------------------------------------------------------------------------ | ||
| 189 | |||
| 190 | |||
| 191 | ############################################################################### | ||
| 192 | ########## CODE STARTS HERE ################################################# | ||
| 193 | |||
| 194 | #use lib "/home/mugdha/UMLS-HSO/UMLS-HSO/WebService-UMLSKS-Similarity/lib"; | ||
| 195 | |||
| 196 | use strict; | ||
| 197 | use warnings; | ||
| 198 | use SOAP::Lite; | ||
| 199 | use Term::ReadKey; | ||
| 200 | use WebService::UMLSKS::GetUserData; | ||
| 201 | use WebService::UMLSKS::ValidateTerm; | ||
| 202 | use WebService::UMLSKS::Query; | ||
| 203 | use WebService::UMLSKS::ConnectUMLS; | ||
| 204 | use WebService::UMLSKS::Similarity; | ||
| 205 | use WebService::UMLSKS::GetCUIs; | ||
| 206 | use WebService::UMLSKS::FormGraph; | ||
| 207 | use Log::Message::Simple qw[msg error debug]; | ||
| 208 | |||
| 209 | #use get_all_associatedCUIs; | ||
| 210 | use Getopt::Long; | ||
| 211 | #use SOAP::Lite +trace => 'debug'; | ||
| 212 | no warnings qw/redefine/; | ||
| 213 | |||
| 214 | |||
| 215 | #Program that returns the shortest path between two concepts using UMLS database. | ||
| 216 | |||
| 217 | # Author : Mugdha | ||
| 218 | # Reference: Program provided by Olivier B., NLM. | ||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | my $verbose = ''; | ||
| 223 | my $log_file = ''; | ||
| 224 | my $patterns_file = ''; | ||
| 225 | my $sources = ''; | ||
| 226 | my $relations = ''; | ||
| 227 | my $directions = ''; | ||
| 228 | my $similarity; | ||
| 229 | my $config_file = ''; | ||
| 230 | my $login_file = ''; | ||
| 231 | my $test_file = ''; | ||
| 232 | my $input1 = ''; | ||
| 233 | my $input2 = ''; | ||
| 234 | my $chd_threshold = ''; | ||
| 235 | my $sib_threshold = ''; | ||
| 236 | |||
| 237 | |||
| 238 | GetOptions( 'verbose:i' => \$verbose , 'sources=s' => \$sources , 'rels=s' =>\$relations, 'dirs=s' =>\$directions, | ||
| 239 | 'config=s' =>\$config_file, 'log=s' => \$log_file, 'login=s' => \$login_file ,'patterns=s' => \$patterns_file, | ||
| 240 | 'testfile=s' => \$test_file, 'input1=s' => \$input1, 'input2=s'=>\$input2,'chd_threshold=s' => \$chd_threshold, 'sib_threshold=s'=>\$sib_threshold); | ||
| 241 | |||
| 242 | # Reference for use of Log package | ||
| 243 | # http://perldoc.perl.org/Log/Message/Simple.html#msg(%22message-string%22-%5b%2cVERBOSE%5d) | ||
| 244 | |||
| 245 | |||
| 246 | if($log_file ne '' && $verbose eq 1){ | ||
| 247 | |||
| 248 | open (LOG , '>>', $log_file); | ||
| 249 | |||
| 250 | |||
| 251 | $Log::Message::Simple::MSG_FH = \*LOG; | ||
| 252 | $Log::Message::Simple::ERROR_FH = \*LOG; | ||
| 253 | $Log::Message::Simple::DEBUG_FH = \*LOG; | ||
| 254 | |||
| 255 | # Following code to print time and date in readable form is taken from following source | ||
| 256 | #http://perl.about.com/od/perltutorials/a/perllocaltime_2.htm | ||
| 257 | |||
| 258 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
| 259 | my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); | ||
| 260 | my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); | ||
| 261 | my $year = 1900 + $yearOffset; | ||
| 262 | my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year"; | ||
| 263 | |||
| 264 | # Code from source ends here | ||
| 265 | |||
| 266 | debug("\n*********************************\n Log written on : | ||
| 267 | $theTime\n************************************", $verbose); | ||
| 268 | |||
| 269 | } | ||
| 270 | |||
| 271 | if($log_file ne '' && $verbose eq '') | ||
| 272 | { | ||
| 273 | print "\nPlease set verbose option to true to see the log\n"; | ||
| 274 | exit; | ||
| 275 | } | ||
| 276 | |||
| 277 | if(defined $config_file && $config_file ne "") | ||
| 278 | { | ||
| 279 | $similarity = WebService::UMLSKS::Similarity->new({"config" => $config_file}); | ||
| 280 | # print "\n creating object of similarity with config file"; | ||
| 281 | msg("\n creating object of similarity with config file", $verbose); | ||
| 282 | } | ||
| 283 | |||
| 284 | else | ||
| 285 | { | ||
| 286 | if($sources eq "" && $relations eq "" && $directions eq "") | ||
| 287 | { | ||
| 288 | # use default things | ||
| 289 | msg("\n creating default object of similarity", $verbose); | ||
| 290 | $similarity = WebService::UMLSKS::Similarity->new(); | ||
| 291 | } | ||
| 292 | else{ | ||
| 293 | |||
| 294 | if( $sources ne "" && $relations ne "" && $directions ne "") | ||
| 295 | { | ||
| 296 | # user specified sources through command line | ||
| 297 | my @source_list = split ("," , $sources); | ||
| 298 | my @relation_list = split ("," , $relations); | ||
| 299 | my @direction_list = split ("," , $directions); | ||
| 300 | $similarity = WebService::UMLSKS::Similarity->new({"sources" => \@source_list, | ||
| 301 | "rels" => \@relation_list, | ||
| 302 | "dirs" => \@direction_list } ); | ||
| 303 | msg("\n creating object of similarity with sources, rels and dirs", $verbose); | ||
| 304 | #$ConfigurationParameters{"SAB"} = \@sources_list; | ||
| 305 | } | ||
| 306 | else{ | ||
| 307 | |||
| 308 | if($relations ne "" && $directions ne "") | ||
| 309 | { | ||
| 310 | # user specified rels through command line | ||
| 311 | my @relation_list = split ("," , $relations); | ||
| 312 | my @direction_list = split ("," , $directions); | ||
| 313 | $similarity = WebService::UMLSKS::Similarity->new({ "rels" => \@relation_list, | ||
| 314 | "dirs" => \@direction_list }); | ||
| 315 | msg("\n creating object of similarity with rels and dirs", $verbose); | ||
| 316 | #$ConfigurationParameters{"REL"} = \@relation_list; | ||
| 317 | } | ||
| 318 | |||
| 319 | if($sources ne "") | ||
| 320 | { | ||
| 321 | my @source_list = split ("," , $sources); | ||
| 322 | |||
| 323 | $similarity = WebService::UMLSKS::Similarity->new({"sources" => \@source_list} ); | ||
| 324 | msg("\n creating object of similarity with sources", $verbose); | ||
| 325 | |||
| 326 | } | ||
| 327 | |||
| 328 | if($relations ne "" && $directions eq "") | ||
| 329 | { | ||
| 330 | print "\nYou have not supplied directions for your relations\n"; | ||
| 331 | exit; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | } | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | #if(defined $sources && $directions) | ||
| 341 | #{ | ||
| 342 | # my @source_list = split ("," , $sources); | ||
| 343 | # my @direction_list = split ("," , $directions); | ||
| 344 | # $similarity = WebService::UMLSKS::Similarity->new({"sources" => \@source_list, | ||
| 345 | # "dirs" => \@direction_list } ); | ||
| 346 | # | ||
| 347 | #} | ||
| 348 | |||
| 349 | #if(defined $sources && defined $relations) | ||
| 350 | #{ | ||
| 351 | # my @relation_list = split ("," , $relations); | ||
| 352 | # my @source_list = split ("," , $sources); | ||
| 353 | # $similarity = WebService::UMLSKS::Similarity->new({"sources" => \@source_list, | ||
| 354 | # "rels" => \@relation_list}); | ||
| 355 | # | ||
| 356 | #} | ||
| 357 | |||
| 358 | my @sources = @{$similarity->{'SAB'}}; | ||
| 359 | my @relations = @{$similarity->{'REL'}}; | ||
| 360 | my @directions = @{$similarity->{'DIR'}}; | ||
| 361 | my @attributes = @{$similarity->{'RELA'}}; | ||
| 362 | |||
| 363 | my $test_flag = 0; | ||
| 364 | my $scriptflag = 0; | ||
| 365 | my %input = (); | ||
| 366 | my @querylist1 = (); | ||
| 367 | my @querylist2 = (); | ||
| 368 | |||
| 369 | msg("\n sources:@sources rels:@relations and dirs:@directions and attributes : @attributes", $verbose); | ||
| 370 | |||
| 371 | # If this is a testing mode, then set continue to length of the file i.e. number of rows | ||
| 372 | |||
| 373 | if($input1 ne "" && $input2 ne "") | ||
| 374 | { | ||
| 375 | # Inputs are specified through script. | ||
| 376 | $scriptflag = 1; | ||
| 377 | |||
| 378 | # This is creating the file for writing output | ||
| 379 | #open(OUTPUT,">","output.txt") or die("Error: cannot open file 'output.txt'\n"); | ||
| 380 | #close OUTPUT; | ||
| 381 | |||
| 382 | #open(TIME,">","time.txt") or die("Error: cannot open file 'time.txt'\n"); | ||
| 383 | #close TIME; | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | if($test_file ne ""){ | ||
| 388 | |||
| 389 | open(MYDATA, $test_file) or die("Error: cannot open file 'data.txt'\n"); | ||
| 390 | |||
| 391 | |||
| 392 | # This is creating the file for writing output | ||
| 393 | open(OUTPUT,">","output.txt") or die("Error: cannot open file 'output.txt'\n"); | ||
| 394 | close OUTPUT; | ||
| 395 | |||
| 396 | #open(TIME,">","time.txt") or die("Error: cannot open file 'time.txt'\n"); | ||
| 397 | #close TIME; | ||
| 398 | |||
| 399 | #open(OUT,">","inter_output.txt") or die("Error: cannot open file 'inter_output.txt'\n"); | ||
| 400 | #close OUT; | ||
| 401 | |||
| 402 | my $line; | ||
| 403 | $test_flag = 1; | ||
| 404 | my $lnum = 1; | ||
| 405 | while( $line = <MYDATA> ){ | ||
| 406 | chomp($line); | ||
| 407 | #print "$lnum: $line\n"; | ||
| 408 | |||
| 409 | $line =~ /\s*(.*)\s*<>\s*(.*?)$/; | ||
| 410 | my $query1 = $1; | ||
| 411 | my $query2 = $2; | ||
| 412 | $query1 =~ s/\s*//g; | ||
| 413 | $query2 =~ s/\s*//g; | ||
| 414 | msg ( "\nquery1 : $query1, query2: $query2", $verbose); | ||
| 415 | push(@querylist1, $query1); | ||
| 416 | push(@querylist2, $query2); | ||
| 417 | #$input{$query1} = $query2; | ||
| 418 | $lnum++; | ||
| 419 | } | ||
| 420 | |||
| 421 | } | ||
| 422 | |||
| 423 | # This is used to continue asking for the new term to user unless you enter 'stop'. | ||
| 424 | |||
| 425 | my $continue = 1; | ||
| 426 | |||
| 427 | my $object_ref; | ||
| 428 | |||
| 429 | # Declaring hash ParentInfo to store parent CUIs' information in following format: | ||
| 430 | # ParentInfo : hash { CUI => (list of parents CUIs) | ||
| 431 | # CUI => (list of parents CUIs) | ||
| 432 | # ......... | ||
| 433 | # } | ||
| 434 | |||
| 435 | my %ParentInfo ; | ||
| 436 | |||
| 437 | # Declaring ListCUI : queue/ list of CUIs elligible for parent search | ||
| 438 | |||
| 439 | my @ListCUI = (); | ||
| 440 | my $allowable_pattern_regex = ''; | ||
| 441 | |||
| 442 | # Creating object of class GetUserData | ||
| 443 | my $g = WebService::UMLSKS::GetUserData->new; | ||
| 444 | my $service = ""; | ||
| 445 | |||
| 446 | if(defined $login_file && $login_file ne "") | ||
| 447 | { | ||
| 448 | # Login details specified through the file | ||
| 449 | # call sub getService using object of GetUserData | ||
| 450 | # Receive a $service object if the user is a valid user. | ||
| 451 | |||
| 452 | my $username = ""; | ||
| 453 | my $pwd = ""; | ||
| 454 | |||
| 455 | open( LOGIN, $login_file ) | ||
| 456 | or die("Error: cannot open file '$login_file'\n"); | ||
| 457 | |||
| 458 | my @login_details = <LOGIN>; | ||
| 459 | foreach my $detail (@login_details){ | ||
| 460 | |||
| 461 | #msg( "\n $detail", $verbose); | ||
| 462 | $detail =~ /\s*(.*)\s*::\s*(.*?)$/; | ||
| 463 | #print "\n $1 \t $2"; | ||
| 464 | my $detail_name = $1; | ||
| 465 | my $detail_value = $2; | ||
| 466 | chomp($detail_value); | ||
| 467 | chomp($detail_name); | ||
| 468 | if($detail_name ne "" && $detail_value ne ""){ | ||
| 469 | if($detail_name =~ /\b[Uu]sername\b/){ | ||
| 470 | $username = $detail_value; | ||
| 471 | } | ||
| 472 | if($detail_name =~ /\b[pP]assword\b|\b[Pp]wd\b/){ | ||
| 473 | $pwd = $detail_value; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | else | ||
| 477 | { | ||
| 478 | print "\n Invalid login file"; | ||
| 479 | exit; | ||
| 480 | } | ||
| 481 | |||
| 482 | } | ||
| 483 | |||
| 484 | $service = $g->getService($verbose, $username, $pwd); | ||
| 485 | |||
| 486 | |||
| 487 | } | ||
| 488 | |||
| 489 | else | ||
| 490 | { | ||
| 491 | # call the sub getUserDetails. | ||
| 492 | # Receive a $service object if the user is a valid user. | ||
| 493 | |||
| 494 | $service = $g->getUserDetails($verbose); | ||
| 495 | |||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | |||
| 500 | # User enetered wrong username or password. | ||
| 501 | |||
| 502 | if ( $service == 0 ) { | ||
| 503 | $continue = 0; | ||
| 504 | } | ||
| 505 | |||
| 506 | if($chd_threshold eq ""){ | ||
| 507 | $chd_threshold = 10000; | ||
| 508 | } | ||
| 509 | |||
| 510 | if($sib_threshold eq ""){ | ||
| 511 | $sib_threshold = 10000; | ||
| 512 | } | ||
| 513 | |||
| 514 | |||
| 515 | # If allowable patterns are specified by user using the patterns_file then, | ||
| 516 | # set the regex from the file | ||
| 517 | |||
| 518 | if(defined $patterns_file && $patterns_file ne "") | ||
| 519 | { | ||
| 520 | |||
| 521 | open( PATTERN, $patterns_file ) | ||
| 522 | or die("Error: cannot open file '$patterns_file'\n"); | ||
| 523 | |||
| 524 | my @p = <PATTERN>; | ||
| 525 | my $allowable_regex = $p[0]; | ||
| 526 | msg ("\n regex from file is : $allowable_regex", $verbose); | ||
| 527 | chomp($allowable_regex); | ||
| 528 | $allowable_regex =~ s/\s*//g; | ||
| 529 | #my $regex; | ||
| 530 | |||
| 531 | if($allowable_regex =~ m/^\// && $allowable_regex =~ m/\/$/) | ||
| 532 | { | ||
| 533 | msg( "\n regex has two backslashes",$verbose); | ||
| 534 | $allowable_regex =~ m/^\/(.*)\/$/; | ||
| 535 | $allowable_pattern_regex = $1; | ||
| 536 | my $valid = eval { qr/$allowable_pattern_regex/}; | ||
| 537 | |||
| 538 | if($@) | ||
| 539 | { | ||
| 540 | #print "\n false"; | ||
| 541 | |||
| 542 | } | ||
| 543 | else | ||
| 544 | { | ||
| 545 | #print "\n true"; | ||
| 546 | } | ||
| 547 | msg ("\n regex extracted is : $allowable_pattern_regex", $verbose); | ||
| 548 | } | ||
| 549 | else | ||
| 550 | { | ||
| 551 | print "\n You entered Invalid regex in patterns file"; | ||
| 552 | exit; | ||
| 553 | |||
| 554 | } | ||
| 555 | #$allowable_pattern_regex =~ chop($allowable_pattern_regex); | ||
| 556 | #$allowable_pattern_regex =~ s/^\///g; | ||
| 557 | msg("allowable path regex: $allowable_pattern_regex",$verbose); | ||
| 558 | |||
| 559 | } | ||
| 560 | |||
| 561 | # Else, use the defualt regex representing the default set of allowable patterns | ||
| 562 | |||
| 563 | else | ||
| 564 | { | ||
| 565 | |||
| 566 | # This regex is formed using the allowed paths' patterns given in HSO paper | ||
| 567 | # Here 1, denotes upward arrow/vector, 2 denotes downward arrow and 3 denoted | ||
| 568 | # horizontal arrows. | ||
| 569 | |||
| 570 | #$allowable_pattern_regex = '\b1+\b|\b1+2+\b|\b1+3+\b|\b1+3+2+\b|\b2+\b|\b2+3+\b|\b3+2+\b|\b3+\b'; | ||
| 571 | $allowable_pattern_regex = '\bU+\b|\bU+D+\b|\bU+H+\b|\bU+H+D+\b|\bD+\b|\bD+H+\b|\bH+D+\b|\bH+\b'; | ||
| 572 | # $allowable_pattern_regex = | ||
| 573 | #'\bU{1,5}\b|\bU{1,5}D{1,5}\b|\bU{1,5}H{1,5}\b|\bU{1,5}H{1,5}D{1,5}\b|\bD{1,5}\b|\bD{1,5}H{1,5}\b|\bH{1,5}D{1,5}\b|\bH{1,5}\b'; | ||
| 574 | |||
| 575 | } | ||
| 576 | |||
| 577 | |||
| 578 | |||
| 579 | # Creating object of query and passing the method name along with parameters. | ||
| 580 | |||
| 581 | my $query = WebService::UMLSKS::Query->new; | ||
| 582 | |||
| 583 | # Creating Connect object to call sub get_pt while forming a query. | ||
| 584 | |||
| 585 | my $c = WebService::UMLSKS::ConnectUMLS->new; | ||
| 586 | |||
| 587 | # Creating GetParents object to get back the parents of input terms. | ||
| 588 | |||
| 589 | #my $read_parents = WebService::UMLSKS::GetParents->new; | ||
| 590 | |||
| 591 | # Creating GetCUIs object to get back CUIs related to terms. | ||
| 592 | |||
| 593 | my $get_CUIs = WebService::UMLSKS::GetCUIs->new; | ||
| 594 | |||
| 595 | # Creating object of FormGraph | ||
| 596 | |||
| 597 | my $form_graph = WebService::UMLSKS::FormGraph->new; | ||
| 598 | |||
| 599 | my $proxy_ticket = $c->get_pt(); | ||
| 600 | |||
| 601 | my $currenttest = 0; | ||
| 602 | |||
| 603 | while ( $continue == 1 ) { | ||
| 604 | |||
| 605 | my $term1; | ||
| 606 | my $term2; | ||
| 607 | if($test_flag == 1){ | ||
| 608 | #print "\n$querylist1[0]"; | ||
| 609 | #print "\n$querylist2[0]"; | ||
| 610 | $term1 = ""; | ||
| 611 | $term2 = ""; | ||
| 612 | $term1 = $querylist1[$currenttest]; | ||
| 613 | $term2 = $querylist2[$currenttest]; | ||
| 614 | $currenttest++; | ||
| 615 | if(!$term1) | ||
| 616 | { | ||
| 617 | #print "\nend of test"; | ||
| 618 | $continue = 0; | ||
| 619 | next; | ||
| 620 | |||
| 621 | } | ||
| 622 | msg("term1: $term1 , term2 :$term2 ", $verbose); | ||
| 623 | |||
| 624 | } | ||
| 625 | if($scriptflag == 1 && $test_flag == 0) | ||
| 626 | { | ||
| 627 | $term1 = ""; | ||
| 628 | $term2 = ""; | ||
| 629 | $term1 = $input1; | ||
| 630 | $term2 = $input2; | ||
| 631 | $test_flag = 1; | ||
| 632 | |||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | if($test_flag == 0){ | ||
| 637 | # After the authentication, accept a first query term or CUI from the user. | ||
| 638 | print "\nEnter first query CUI:"; | ||
| 639 | $term1 = <>; | ||
| 640 | |||
| 641 | # Remove white spaces. | ||
| 642 | chomp($term1); | ||
| 643 | |||
| 644 | # If user enters 'stop', exit the program. | ||
| 645 | if ( $term1 =~ /stop/i ) { | ||
| 646 | exit; | ||
| 647 | } | ||
| 648 | |||
| 649 | # Else continue with asking the new query term. | ||
| 650 | |||
| 651 | |||
| 652 | # After the authentication, accept a first query term or CUI from the user. | ||
| 653 | |||
| 654 | print "\nEnter second query CUI:"; | ||
| 655 | $term2 = <>; | ||
| 656 | |||
| 657 | # Remove white spaces. | ||
| 658 | chomp($term2); | ||
| 659 | |||
| 660 | if ( $term2 =~ /stop/i ) { | ||
| 661 | exit; | ||
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 665 | |||
| 666 | # Validate the term by passing it to the sub validateTerm which belongs to class getTerm. | ||
| 667 | # Create object of class getTerm to access the sub validateTerm. | ||
| 668 | |||
| 669 | my $valid = WebService::UMLSKS::ValidateTerm->new; | ||
| 670 | my $isvalid_input1 = $valid->validateTerm($term1); | ||
| 671 | my $isvalid_input2 = $valid->validateTerm($term2); | ||
| 672 | |||
| 673 | # Depending on the value returned by validateTerm form a query for UMLSKS. | ||
| 674 | |||
| 675 | my @allCUIOfTerm1 = (); | ||
| 676 | my @allCUIOfTerm2 = (); | ||
| 677 | |||
| 678 | # If the inputs are invalid, accepts new input | ||
| 679 | if($isvalid_input1 eq 'invalid') | ||
| 680 | { | ||
| 681 | print "\n Your first input is not a valid CUI"; | ||
| 682 | next; | ||
| 683 | } | ||
| 684 | elsif($isvalid_input2 eq 'invalid') | ||
| 685 | { | ||
| 686 | print "\n Your second input is not a valid CUI"; | ||
| 687 | next; | ||
| 688 | } | ||
| 689 | |||
| 690 | # else inputs are either valid CUIs or terms | ||
| 691 | else | ||
| 692 | { | ||
| 693 | |||
| 694 | # Check if input1 is term or CUI | ||
| 695 | |||
| 696 | # If the input entered by user is term, call findCUIByExact webservice, | ||
| 697 | # to get back the CUI. | ||
| 698 | if($isvalid_input1 eq 'term') | ||
| 699 | { | ||
| 700 | my %CUI_ref = %{$get_CUIs->get_CUI_info($service,$term1,\@sources,$verbose)}; | ||
| 701 | if(!%CUI_ref) | ||
| 702 | { | ||
| 703 | print "\n Term $term1 does not exist in database."; | ||
| 704 | |||
| 705 | next; | ||
| 706 | } | ||
| 707 | else | ||
| 708 | { | ||
| 709 | print "\n Information about first input term : $term1 ->"; | ||
| 710 | foreach my $c (keys %CUI_ref){ | ||
| 711 | push(@allCUIOfTerm1,$CUI_ref{$c}); | ||
| 712 | print "\nPreferred term : $c and CUI : $CUI_ref{$c}"; | ||
| 713 | } | ||
| 714 | |||
| 715 | } | ||
| 716 | } | ||
| 717 | elsif($isvalid_input1 eq 'cui'){ | ||
| 718 | print "\nFirst input is a CUI: $term1"; | ||
| 719 | push(@allCUIOfTerm1,$term1); | ||
| 720 | } | ||
| 721 | |||
| 722 | # Check if input2 is term or CUI | ||
| 723 | |||
| 724 | # If the input entered by user is term, call findCUIByExact webservice, | ||
| 725 | # to get back the CUI. | ||
| 726 | if($isvalid_input2 eq 'term') | ||
| 727 | { | ||
| 728 | my %CUI_ref = %{$get_CUIs->get_CUI_info($service,$term2,\@sources,$verbose)}; | ||
| 729 | if(!%CUI_ref) | ||
| 730 | { | ||
| 731 | print "\n Term $term2 does not exist in database."; | ||
| 732 | next; | ||
| 733 | } | ||
| 734 | else | ||
| 735 | { | ||
| 736 | print "\n Information about second input term : $term2 ->"; | ||
| 737 | foreach my $c (keys %CUI_ref){ | ||
| 738 | push(@allCUIOfTerm2,$CUI_ref{$c}); | ||
| 739 | print "\nPreferred term : $c and CUI : $CUI_ref{$c}"; | ||
| 740 | } | ||
| 741 | |||
| 742 | } | ||
| 743 | } | ||
| 744 | elsif($isvalid_input2 eq 'cui'){ | ||
| 745 | print "\nSecond input is a CUI: $term2"; | ||
| 746 | push(@allCUIOfTerm2,$term2); | ||
| 747 | } | ||
| 748 | } | ||
| 749 | |||
| 750 | my $t1 = ""; | ||
| 751 | my $t2 = ""; | ||
| 752 | |||
| 753 | |||
| 754 | #print "\n arrays are \n cuis of term 1 : @allCUIOfTerm1 cuis of term2 : @allCUIOfTerm2"; | ||
| 755 | |||
| 756 | if($#allCUIOfTerm1 == 0 && $#allCUIOfTerm2 == 0){ | ||
| 757 | # print "\nJust one CUI for both terms"; | ||
| 758 | msg("Both the terms have just one CUI",$verbose); | ||
| 759 | |||
| 760 | # Calling formGraph to make a graph and find the allowable shortest path | ||
| 761 | |||
| 762 | $t1 = $allCUIOfTerm1[0]; | ||
| 763 | $t2 = $allCUIOfTerm2[0]; | ||
| 764 | |||
| 765 | # Check if both the inputs are same. | ||
| 766 | |||
| 767 | msg("\n before calling form grpah : regex: $allowable_pattern_regex", $verbose); | ||
| 768 | my $return_val = $form_graph-> | ||
| 769 | form_graph($t1,$t2,$service, $verbose, \@sources, \@relations,\@directions,\@attributes,$allowable_pattern_regex,$test_flag, $sib_threshold,$chd_threshold); | ||
| 770 | if($return_val eq 'same'){ | ||
| 771 | next; | ||
| 772 | } | ||
| 773 | |||
| 774 | } | ||
| 775 | |||
| 776 | else | ||
| 777 | { | ||
| 778 | #print "\n cuis of 1 : @allCUIOfTerm1, cuis of 2 : @allCUIOfTerm2"; | ||
| 779 | print "\n Do you want to calculate Semantic Relatedness between all the combinations of the CUIs (y/n):"; | ||
| 780 | my $option = <>; | ||
| 781 | chomp($option); | ||
| 782 | #print "\n option is : $option"; | ||
| 783 | if($option eq 'y'){ | ||
| 784 | #print "\n inside for loops"; | ||
| 785 | my $i; | ||
| 786 | my $j; | ||
| 787 | for($i = 0; $i <= $#allCUIOfTerm1; $i++){ | ||
| 788 | |||
| 789 | for ($j = 0 ; $j <= $#allCUIOfTerm2; $j++){ | ||
| 790 | #print "\n in both loops"; | ||
| 791 | my $t1 = $allCUIOfTerm1[$i]; | ||
| 792 | my $t2 = $allCUIOfTerm2[$j]; | ||
| 793 | print "\n\nterm 1 : $t1 term 2 : $t2"; | ||
| 794 | my $return_val = $form_graph-> | ||
| 795 | form_graph($t1,$t2,$service, $verbose, \@sources, \@relations,\@directions,\@attributes,$allowable_pattern_regex,$test_flag, $sib_threshold,$chd_threshold); | ||
| 796 | if($return_val eq 'same'){ | ||
| 797 | print "\n $t1 and $t2 are same"; | ||
| 798 | next; | ||
| 799 | } | ||
| 800 | } | ||
| 801 | |||
| 802 | } | ||
| 803 | |||
| 804 | |||
| 805 | } | ||
| 806 | else | ||
| 807 | { | ||
| 808 | #print "\n cuis of term 1 ; @allCUIOfTerm1, cuis of term 2 @allCUIOfTerm2"; | ||
| 809 | |||
| 810 | $t1 = $allCUIOfTerm1[0]; | ||
| 811 | $t2 = $allCUIOfTerm2[0]; | ||
| 812 | |||
| 813 | my $return_val = $form_graph-> | ||
| 814 | form_graph($t1,$t2,$service, $verbose, \@sources, \@relations,\@directions,\@attributes,$allowable_pattern_regex,$test_flag, $sib_threshold,$chd_threshold); | ||
| 815 | if($return_val eq 'same'){ | ||
| 816 | next; | ||
| 817 | } | ||
| 818 | |||
| 819 | } | ||
| 820 | } | ||
| 821 | |||
| 822 | |||
| 823 | |||
| 824 | |||
| 825 | } | ||
| 826 | |||
| 827 | |||
| 828 | undef @sources; | ||
| 829 | undef @relations; | ||
| 830 | undef @directions; | ||
| 831 | |||
| 832 | |||
| 833 | #-------------------------------PERLDOC STARTS HERE------------------------------------------------------------- | ||
| 834 | |||
| 835 | |||
| 836 | =head1 SEE ALSO | ||
| 837 | |||
| 838 | ValidateTerm.pm GetUserData.pm Query.pm ConnectUMLS.pm | ||
| 839 | |||
| 840 | =cut | ||
| 841 | |||
| 842 | |||
| 843 | =head1 AUTHORS | ||
| 844 | |||
| 845 | Mugdha Choudhari University of Minnesota Duluth | ||
| 846 | E<lt>chou0130 at d.umn.eduE<gt> | ||
| 847 | |||
| 848 | Ted Pedersen, University of Minnesota Duluth | ||
| 849 | E<lt>tpederse at d.umn.eduE<gt> | ||
| 850 | |||
| 851 | |||
| 852 | |||
| 853 | |||
| 854 | =head1 COPYRIGHT | ||
| 855 | |||
| 856 | Copyright (C) 2011, Mugdha Choudhari, Ted Pedersen | ||
| 857 | |||
| 858 | This program is free software; you can redistribute it and/or modify | ||
| 859 | it under the terms of the GNU General Public License as published by | ||
| 860 | the Free Software Foundation; either version 2 of the License, or (at | ||
| 861 | your option) any later version. | ||
| 862 | |||
| 863 | This program is distributed in the hope that it will be useful, but | ||
| 864 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 865 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 866 | General Public License for more details. | ||
| 867 | |||
| 868 | You should have received a copy of the GNU General Public License | ||
| 869 | along with this program; if not, write to | ||
| 870 | The Free Software Foundation, Inc., | ||
| 871 | 59 Temple Place - Suite 330, | ||
| 872 | Boston, MA 02111-1307, USA. | ||
| 873 | |||
| 874 | =cut | ||
| 875 | |||
| 876 | #---------------------------------PERLDOC ENDS HERE--------------------------------------------------------------- | ||
| 877 |