blib/script/query-umls-similarity-webinterface.pl untested

filter: Statementstmt 0.0% 0 / 287 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 91 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 521 SCAR 81.0
CC
57
Cov
0%
checkOptions
552.0
setOptions
506.0
loadInput
132.0
CRAP 3306.0
1 #!/usr/local/bin/perl
2
3 =head1 NAME
4
5 query-umls-similarity-webinterface.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: query-umls-similarity-webinterface.pl [OPTIONS] [CUI1|TERM1] [CUI2|TERM2]
15
16 =head1 INPUT
17
18 =head3 [CUI1|TERM1] [CUI2|TERM2]
19
20 The input are two terms or two CUIs associated to concepts in the UMLS.
21
22 =head2 OPTIONS:
23
24 =head3 --url URL
25
26 The url to the server hosting the webinterface.
27
28 DEFAULT: http://atlas.ahc.umn.edu
29
30 =head3 --sab SOURCES
31
32 The UMLS source(s) used to obtain the similarity or relatedness values.
33
34 Currently, for similarity the following sources are available through
35 the web interface: MSH, OMIM, FMA or SNOMEDCT. For relatedness, the available
36 sources are: MSH, SNOMEDCT or UMLS_ALL (which refers to the entire umls).
37
38 For example:
39
40 --sab MSH
41
42 Note: In the UMLS::Similarity package, we differentiate between
43 the sources used for relatendess and similarity measures the
44 sabdef variable for relatedness and the sab variable for similarity
45 but to simplify things we only use hte rel variable right now. I
46 hope this doesn't confuse anyone.
47
48 For additional sources to be added please email me and we can see
49 about adding them. Also note, that the UMLS::Similarity package
50 allows for any combination of sources to be used.
51
52 =head3 --rel RELATIONS
53
54 The UMLS relations used to obtain the similarity or relatedness values.
55 Currently, for similarity following relations are available through the
56 web interface: PAR/CHD or RB/RN. For relatedness: CUI/PAR/CHD/RB/RN or
57 CUI.
58
59 For example:
60
61 --rel PAR/CHD
62
63 Note: The relatedness measure use definition information and therefore
64 CUI refers to using the definition of the concept itself while PAR,
65 for example, refers to using the definition of the concepts parent
66 relations. In the UMLS::Similarity package, we differentiate between
67 these using the reldef and rel variables but to simplify things
68 we only use hte rel variable right now.
69
70 For additional relations to be added please email me and we can see
71 about adding them. Also note, that the UMLS::Similarity package
72 allows for any combination of sources/relations to be used.
73
74 =head3 --measure MEASURE
75
76 Use the MEASURE module to calculate the semantic similarity. The
77 available measure are:
78 1. Leacock and Chodorow (1998) referred to as lch
79 2. Wu and Palmer (1994) referred to as wup
80 3. The basic path measure referred to as path
81 4. Rada, et. al. (1989) referred to as cdist
82 5. Nguyan and Al-Mubaid (2006) referred to as nam
83 6. Resnik (1996) referred to as res
84 7. Lin (1988) referred to as lin
85 8. Jiang and Conrath (1997) referred to as jcn
86 9. The vector measure referred to as vector
87
88 =head3 --infile FILE
89
90 A file containing pairs of concepts or terms in any of the following
91 formats:
92
93 term1<>term2
94 cui1<>cui2
95 cui1<>term2
96 term1<>cui2
97
98 =head1 SYSTEM REQUIREMENTS
99
100 =over
101
102 =item * Perl (version 5.8.5 or better) - http://www.perl.org
103
104 =back
105
106 =head1 CHEAT SHEET
107
108 =head2 BASIC EXAMPLE
109
110 The simplist case is:
111
112 query-umls-similarity-webinterface.pl hand skull
113
114 which returns the similarity between 'hand' 'skull' using the
115 path measure where the path information is obtained from the
116 PAR/CHD relations in MSH.
117
118 =head2 MODIFY THE DEFAULT MEASURE
119
120 There are a number of additional similarity or relatedness measures
121 that you can use: Leacock & Chodorow (lch), Wu & Palmer (wup), Lin
122 (lin) Resnik (res), Jiang & Conrath (jcn), Lesk (lesk and the Vector
123 Measure (vector).
124
125 To change the measure, use the --measure option. For example:
126
127 query-umls-similarity-webinterface.pl --measure lesk hand skull
128
129
130 =head2 MODIFY THE DEFAULT SOURCE/RELATIONS
131
132 There are also a number of additional SOURCE/RELATION options that
133 you can use.
134
135 For the similarity measures, you can use:
136
137 Source Relations
138 -----------------------
139 SNOMEDCT PAR/CHD
140 SNOMEDCT RB/RN
141 MSH PAR/CHD
142 MSH RB/RN
143 FMA PAR/CHD
144 FMA RB/RN
145 OMIM PAR/CHD
146 OMIM RB/RN
147
148 This means that the path information will be obtained from the specified
149 source following the specified relations.
150
151 For the relatedness measures, things are a little different because the
152 relations refer to what relations the extended definition is derived
153 from. In the interface, you can use:
154
155 Source Relations
156 -----------------------
157 SNOMEDCT CUI/PAR/CHD/RB/RN
158 SNOMEDCT CUI
159 MSH CUI/PAR/CHD/RB/RN
160 MSH CUI
161 UMLS_ALL CUI/PAR/CHD/RB/RN
162 UMLS_ALL CUI
163
164 If this is confusing, send me an email and I can explain in more detail.
165
166 To change the source and relations, use the --sab and --rel options. For
167 example:
168
169 query-umls-similarity-webinterface.pl --sab SNOMEDCT --rel PAR/CHD hand skull
170
171 =head1 CONTACT US
172
173 If you have any trouble installing and using UMLS-Similarity,
174 please contact us via the users mailing list :
175
176 umls-similarity@yahoogroups.com
177
178 You can join this group by going to:
179
180 http://tech.groups.yahoo.com/group/umls-similarity/
181
182 You may also contact us directly if you prefer :
183
184 Bridget T. McInnes: bthomson at cs.umn.edu
185
186 Ted Pedersen : tpederse at d.umn.edu
187
188 =head1 AUTHOR
189
190 Bridget T. McInnes, University of Minnesota
191
192 =head1 COPYRIGHT
193
194 Copyright (c) 2007-2011,
195
196 Bridget T. McInnes, University of Minnesota
197 bthomson at cs.umn.edu
198
199 Ted Pedersen, University of Minnesota Duluth
200 tpederse at d.umn.edu
201
202 Serguei Pakhomov, University of Minnesota Twin Cities
203 pakh0002 at umn.edu
204
205 Ying Liu, University of Minnesota Twin Cities
206 liux at umn.edu
207
208 This program is free software; you can redistribute it and/or modify it under
209 the terms of the GNU General Public License as published by the Free Software
210 Foundation; either version 2 of the License, or (at your option) any later
211 version.
212
213 This program is distributed in the hope that it will be useful, but WITHOUT
214 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
215 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
216
217 You should have received a copy of the GNU General Public License along with
218 this program; if not, write to:
219
220 The Free Software Foundation, Inc.,
221 59 Temple Place - Suite 330,
222 Boston, MA 02111-1307, USA.
223
224 =cut
225
226 ###############################################################################
227
228 # THE CODE STARTS HERE
229 ###############################################################################
230
231 # ================================
232 # COMMAND LINE OPTIONS AND USAGE
233 # ================================
234
235 use Getopt::Long;
236 use URI::Escape;
237 use LWP;
238
239 eval(GetOptions( "version", "help", "url=s", "measure=s", "sab=s", "rel=s", "infile=s", "debug")) or die ("Please check the above mentioned option(s).\n");
240
241 # if debug is defined
242 my $debug = 0;
243 if(defined $opt_debug) { $debug = 1; }
244
245 # if help is defined, print out help
246 if( defined $opt_help ) {
247 $opt_help = 1;
248 &showHelp();
249 exit;
250 }
251
252 # if version is requested, show version
253 if( defined $opt_version ) {
254 $opt_version = 1;
255 &showVersion();
256 exit;
257 }
258
259 # At least 2 terms should be given on the command line.
260 if( !(defined $opt_infile) and (scalar(@ARGV) < 2) ) {
261 print STDERR "At least 2 terms or CUIs should be given on the \n";
262 print STDERR "command line or use the --infile option\n";
263 &minimalUsageNotes();
264 exit;
265 }
266
267 # set browser handler
268 my $browser = LWP::UserAgent->new;
269
270 # initialize variables
271 my $rel = "";
272 my $sab = "";
273 my $reldef = "";
274 my $sabdef = "";
275 my $rmeasure = "";
276 my $smeasure = "";
277 my $button = "";
278 my $measure = "";
279 my $url = "";
280
281 # check the input options
282 &checkOptions();
283
284 # set the options
285 &setOptions();
286
287 # get the concept pairs
288 my $input = &loadInput();
289
290 # get the similarity for each input pair
291 foreach my $pair (@{$input}) {
292 my ($input1, $input2) = split/<>/, $pair;
293
294 if($debug) {
295 print STDERR "$input1 : $input2\n";
296 print STDERR "SAB: $sab\n";
297 print STDERR "REL: $rel\n";
298 print STDERR "SABDEF: $sabdef\n";
299 print STDERR "RELDEF: $reldef\n";
300 print STDERR "BUTTON: $button\n";
301 print STDERR "MEASURES: $rmeasure $smeasure\n";
302 }
303
304 # query the web interface
305 my $page = &queryWebInterface($input1, $input2);
306
307 # extract the similarity information
308 my $output = &extractInformation($page);
309
310 # print output
311 print "$output\n";
312
313 my $num = 2;
314 while($num--){
315 sleep(1);
316 }
317
318 }
319
320 sub extractInformation
321 {
322 my $page = shift;
323
324 if($page=~/View errors/) {
325 $page=~/<input type=\"text\" name="word1\" id=\"word1in\" value=\"(.*?)\" \/>/;
326 my $word1 = $1;
327 $page=~/<input type=\"text\" name="word2\" id=\"word2in\" value=\"(.*?)\" \/>/;
328 my $word2 = $1;
329
330 return "-1<>$word1<>$word2";
331 }
332 else {
333 $page=~/<p class=\"results\">The (similarity|relatedness) of (.*?) \(<a href=\"\#\" onclick=\"showWindow \(\'umls\_wps\.cgi\?wps=(C[0-9]+)(|.*?\')?\, \'\'\); return false;\">(C[0-9]+)<\/a> \) and (.*?) \(<a href=\"\#\" onclick=\"showWindow \(\'umls\_wps\.cgi\?wps=(C[0-9]+)(|.*?\')\, \'\'\); return false;\">(C[0-9]+)<\/a> \) using (.*?) \(.*?\) is (.*?)\.<\/p>/;
334
335 my $word1 = $2;
336 my $cui1 = $3;
337
338 my $word2 = $6;
339 my $cui2 = $7;
340
341 my $score = $11;
342
343 return "$score<>$word1($cui1)<>$word2($cui2)";
344 }
345
346 }
347
348
349 sub queryWebInterface
350 {
351 my $i1 = shift;
352 my $i2 = shift;
353
354 if($debug) { print STDERR "In queryWebInterface($i1, $i2)\n"; }
355
356
357 my $qurl = "$url/cgi-bin/umls_similarity.cgi?word1=$i1&word2=$i2&sab=$sab&rel=$rel&similarity=$smeasure&button=$button&sabdef=$sabdef&reldef=$reldef&relatedness=$rmeasure";
358
359 while(1) {
360 my $resp = $browser->get($qurl);
361 if ($resp->is_success) { }
362 else { print $resp->status_line, "$qurl \n"; }
363
364 my $webpage = $resp->content;
365
366 return $webpage;
367
368 }
369 }
370
371 sub loadInput {
372
373 if($debug) { print STDERR "In loadInput\n"; }
374
375 my @input_array = ();
376
377 # if file is defined get the terms or cuis from the input file
378 if(defined $opt_infile) {
379
380 if($debug) { print STDERR "FILE ($opt_infile) DEFINED\n"; }
381
382 open(FILE, $infile) || die "Could not open file: $infile\n";
383 my $linecounter = 1;
384 while(<FILE>) {
385 chomp;
386 if($_=~/^\s*$/) { next; }
387 if($_=~/\<\>/) {
388 # escape the ' character on input if it exists
389 if(! ($_=~/\\\'/)) { $_=~s/'/\\'/g; }
390
391 push @input_array, $_;
392 }
393 else {
394 print STDERR "There is an error in the input file ($infile)\n";
395 print STDERR "one line $linecounter. The input is not in the\n";
396 print STDERR "correct format. Here is the input line:\n";
397 print STDERR "$_\n\n";
398 exit;
399 }
400 }
401 }
402 # otherwise get them from the command line
403
404 else {
405 if($debug) { print STDERR "Command Line terms/cuis defined\n"; }
406
407 my $i1 = shift @ARGV;
408 my $i2 = shift @ARGV;
409
410 # escape the ' character on input if it exists
411 if(! ($i1=~/\\\'/)) { $i1=~s/'/\\'/g; }
412 if(! ($i2=~/\\\'/)) { $i2=~s/'/\\'/g; }
413
414 if($debug) { print STDERR "INPUT: $i1 $i2\n"; }
415
416 my $input = "$i1<>$i2";
417 push @input_array, $input;
418 }
419
420 return \@input_array;
421 }
422
423 # checks the user input options
424 sub checkOptions {
425
426 if($debug) { print STDERR "In checkOptions\n"; }
427
428 if(defined $opt_measure) {
429 if(! ($opt_measure=~/\b(path|wup|lch|cdist|nam|vector|res|lin|random|jcn|lesk)\b/)) {
430 print STDERR "The measure ($opt_measure) is not defined for\n";
431 print STDERR "the UMLS-Similarity package at this time.\n\n";
432 &minimalUsageNotes();
433 exit;
434 }
435 }
436 if(defined $opt_sab) {
437
438 if(defined $opt_measure && $opt_measure=~/lesk|vector/) {
439 if(! ($opt_sab=~/MSH|SNOMEDCT|UMLS_ALL/) ) {
440 print STDERR "The --sab $opt_sab is currently not available through the web interface\n";
441 print STDERR "for the relatedness measure ($opt_measure)\n";
442 &minimalUsageNotes();
443 exit;
444 }
445 }
446 else {
447 if(! ($opt_sab=~/MSH|OMIM|SNOMEDCT|FMA/) ) {
448 print STDERR "The --sab $opt_sab is currently not available through the web interface\n";
449 print STDERR "for the similarity measure (";
450 if(defined $opt_measure) { print STDERR "$opt_measure)\n"; }
451 else { print STDERR "path)\n"; }
452 &minimalUsageNotes();
453 exit;
454 }
455 }
456 }
457
458 if(defined $opt_rel) {
459
460 my @rels = split/\//, $opt_rel;
461 my $cui = 0; my $p = 0; my $c = 0; my $rb = 0 ; my $rn = 0;
462 foreach my $rel (@rels) {
463 if($rel=~/PAR/) { $p++; }
464 elsif($rel=~/CHD/) { $c++; }
465 elsif($rel=~/RN/) { $rn++; }
466 elsif($rel=~/RB/) { $rb++; }
467 elsif($rel=~/CUI/) { $cui++; }
468 else {
469 print STDERR "The relation $rel is not available in the webinterface.\n";
470 &minimalUsageNotes();
471 exit;
472 }
473 }
474
475 if(defined $opt_measure && $opt_measure=~/lesk|vector/) {
476 if(! (($cui > 0 && $p > 0 && $c > 0 && $rb > 0 && $rn > 0) ||
477 ($cui > 0 && $p < 1 && $c < 1 && $rb < 1 && $rn < 1)) ) {
478 print STDERR "The --rel $opt_rel is currently not available through the web interface\n";
479 print STDERR "for the relatedness measure ($opt_measure).\n";
480 &minimalUsageNotes();
481 exit;
482 }
483 }
484 else {
485 if($cui > 0) {
486 print STDERR "The CUI relation is only available for the relatedness measures.\n\n";
487 &minimalUsageNotes();
488 exit;
489 }
490 if($p > 0 && $c > 0 && $rb > 0 && $rn > 0) {
491 print STDERR "The options PAR/CHD/RB/RN is currently not available\n";
492 &minimalUsageNotes();
493 exit;
494 }
495
496 if($p > 0 && $c < 1) {
497 print STDERR "Missing CHD option in PAR/CHD\n";
498 &minimalUsageNotes();
499 exit;
500 }
501 if($p < 1 && $c > 0) {
502 print STDERR "Missing PAR option in PAR/CHD\n";
503 &minimalUsageNotes();
504 exit;
505 }
506 if($rb > 0 && $rn < 1) {
507 print STDERR "Missing RN option in RB/RN\n";
508 &minimalUsageNotes();
509 exit;
510 }
511 if($rb < 1 && $rn > 0) {
512 print STDERR "Missing RB option in RB/RN\n";
513 &minimalUsageNotes();
514 exit;
515 }
516 }
517 }
518 }
519
520
521
522 # set user input and default options
523 sub setOptions {
524
525 if($debug) { print STDERR "In setOptions\n"; }
526
527 my $default = "";
528 my $set = "";
529
530 # set url
531 $url = "http://atlas.ahc.umn.edu/";
532 if(defined $opt_url) {
533 $url = $opt_url;
534 $set .= " --set $url\n";
535 }
536 else { $default .= " --default $url\n"; }
537
538 # set file
539 if(defined $opt_infile) {
540 $infile = $opt_infile;
541 $set .= " --infile $opt_infile\n";
542 }
543
544 # set the measures
545 $smeasure = "path";
546 $rmeasure = "vector";
547 $measure = "similarity";
548 if(defined $opt_measure) {
549 $set .= " --measure $opt_measure\n";
550 if($opt_measure=~/lesk|vector/) {
551 $button = "Compute+Relatedness";
552 $rmeasure = $opt_measure;
553 $measure = "relatedness";
554 }
555 else {
556 $button = "Compute+Similarity";
557 $smeasure = $opt_measure;
558 }
559 }
560 else {
561 $button = "Compute+Similarity";
562 $default .= " --measure $smeasure\n";
563 }
564
565 $sab = "MSH";
566 $sabdef = "UMLS_ALL";
567 if(defined $opt_sab) {
568 if($measure eq "similarity") { $sab = $opt_sab; }
569 else { $sabdef = $opt_sab; }
570 }
571 else {
572 if($measure eq "similarity") { $default .= " --sab $sab\n"; }
573 else { $default .= " --sab $sabdef\n"; }
574 }
575
576 # get the relation options for similarity and relatedness
577 $reldef = "CUI/PAR/CHD/RB/RN";
578 $rel = "PAR/CHD";
579 if(defined $opt_rel) {
580 my @rels = split/\//, $opt_rel;
581 my $cui = 0; my $p = 0; my $c = 0; my $rb = 0 ; my $rn = 0;
582 foreach my $rel (@rels) {
583 if($rel=~/PAR/) { $p++; }
584 if($rel=~/CHD/) { $c++; }
585 if($rel=~/RN/) { $rn++; }
586 if($rel=~/RB/) { $rb++; }
587 if($rel=~/CUI/) { $cui++; }
588 }
589
590 if($cui > 0 && $p > 0 && $c > 0 && $rb > 0 && $rn > 0) { $reldef = "CUI/PAR/CHD/RB%2fRN"; }
591 if($cui > 0 && $p < 1 && $c < 1 && $rb < 1 && $rn < 1) { $reldef = "CUI"; }
592
593 if($p > 0 && $c > 0) { $rel = "PAR/CHD"; }
594 if($rb > 0 && $rn > 0) { $rel = "RB/RN"; }
595
596 $set .= " --rel $opt_rel\n";
597 }
598 else {
599 if($measure eq "similarity") { $default .= " --rel PAR/CHD"; }
600 else { $default .= " --rel CUI/PAR/CHD/RB/RN"; }
601
602 }
603
604 # set the relation options for the web browser
605 $reldef=~s/\//%2F/g;
606 $rel=~s/\//%2F/g;
607
608
609 # check settings
610 if($default eq "") { $default = " No default settings\n"; }
611 if($set eq "") { $set = " No user defined settings\n"; }
612
613 # print options
614 print STDERR "Default Settings:\n";
615 print STDERR "$default\n";
616
617 print STDERR "User Settings:\n";
618 print STDERR "$set\n";
619 }
620
621 ##############################################################################
622 # function to output minimal usage notes
623 ##############################################################################
624 sub minimalUsageNotes {
625
626 print "Usage: query-umls-similarity-webinterface.pl [OPTIONS] [TERM1 TERM2] [CUI1 CUI2]\n";
627 &askHelp();
628 exit;
629 }
630
631 ##############################################################################
632 # function to output help messages for this program
633 ##############################################################################
634 sub showHelp() {
635
636 print "This is a utility that takes as input either two terms \n";
637 print "or two CUIs from the command line or a file and returns \n";
638 print "the similarity between the two using the UMLS-Similarity\n";
639 print "web interface\n";
640
641 print "Usage: query-umls-similarity-webinterface.pl [OPTIONS] TERM1 TERM2\n\n";
642
643 print "General Options:\n\n";
644
645 print "--url URL The url to the server hosting the webinterface\n";
646 print " DEFAULT: http://atlas.ahc.umn.edu\n\n";
647
648 print "--measure MEASURE The measure to use to calculate the\n";
649 print " semantic similarity. (DEFAULT: path)\n\n";
650
651 print "--sab SOURCE(s) Source(s) used to obtain similarity score.\n";
652 print " Similarity DEFAULT: MSH\n";
653 print " Relatedness DEFAULT: UMLS_ALL\n\n";
654
655 print "--rel RELATION(s) Relation(s) used to obtain similarity score.\n";
656 print " Similarity DEFAULT: PAR/CHD\n";
657 print " Relatedness DEFAULT: CUI/PAR/CHD/RB/RN\n\n";
658
659 print "--version Prints the version number\n\n";
660
661 print "--help Prints this help message.\n\n";
662
663
664 print "--infile FILE File containing TERM or CUI pairs\n\n";
665
666 }
667 ##############################################################################
668 # function to output the version number
669 ##############################################################################
670 sub showVersion {
671 print '$Id: query-umls-similarity-webinterface.pl,v 1.9 2012/04/17 11:45:01 btmcinnes Exp $';
672 print "\nCopyright (c) 2011, Ted Pedersen & Bridget McInnes\n";
673 }
674
675 ##############################################################################
676 # function to output "ask for help" message when user's goofed
677 ##############################################################################
678 sub askHelp {
679 print STDERR "Type query-umls-similarity-webinterface.pl --help for help.\n";
680 }
681