blib/script/create-icpropagation.pl untested

filter: Statementstmt 0.0% 0 / 231 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 365 SCAR 71.4
CC
35
Cov
0%
setOptions
272.0
checkParameters
90.0
loadUMLS
42.0
CRAP 1260.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 create-icpropagation.pl - This program determines the probability
6 of the CUIs in a specified set of sources and relations.
7  
8 =head1 SYNOPSIS
9  
10 This program determines the probability of the CUIs in a
11 specified set of sources and relations.
12  
13 =head1 USAGE
14  
15 Usage: create-icpropagation.pl [OPTIONS] OUTPUTFILE ICFREQUENCY_FILE
16  
17  
18 =head2 OUTPUTFILE
19  
20 File in which the probability of the CUIs will be stored.
21  
22 The ouput file containing the probability of the CUIs has the
23 following format:
24  
25 SMOOTH :: <0|1>
26 SAB :: (include|exclude) <sources>
27 REL :: (include|exclude) <relations>
28 N :: NUMBER
29 REL :: <relations>
30 RELA :: <relas> <- if any are specified in the config
31 CUI<>probability
32 CUI<>probability
33 ...
34  
35 =head2 ICFREQUENCY FILE
36  
37 File containing the icfrequency counts
38  
39 The input file contains frequency counts for CUIs in the following
40 format:
41  
42 SAB :: (include|exclude) <sources>
43 REL :: (include|exclude) <relations>
44 N :: NUMBER
45 CUI<>freq
46 CUI<>freq
47 ...
48  
49 N is the total number of ngrams that occurred in the text used to
50 create the icfrequency file.
51  
52 =head2 Optional Arguments:
53  
54 =head3 --st
55  
56 This outputs the probability of the concepts semantic types
57 rather than the concepts themselves. The frequencies for the
58 st are propagated up the semantic network and therefore are
59 source independent. Note, that the semantic types are expected
60 in the icfrequency input file. This can be created using the
61 create-icfrequency.pl program with the --st option.
62  
63 If you are erroring out due to the header information on the
64 top of the icfrequency file, try using the --disregard option.
65  
66 =head3 --smooth
67  
68 Incorporate Laplace smoothing, where the frequency count of each of the
69 concepts in the taxonomy is incremented by one. The advantage of
70 doing this is that it avoides having a concept that has a probability
71 of zero. The disadvantage is that it can shift the overall probability
72 mass of the concepts from what is actually seen in the corpus.
73  
74 =head3 --config FILE
75  
76 This is the configuration file. The format of the configuration
77 file is as follows:
78  
79 SAB :: <include|exclude> <source1, source2, ... sourceN>
80  
81 REL :: <include|exclude> <relation1, relation2, ... relationN>
82  
83 For example, if we wanted to use the MSH vocabulary with only
84 the RB/RN relations, the configuration file would be:
85  
86 SAB :: include MSH
87 REL :: include RB, RN
88  
89 or
90  
91 SAB :: include MSH
92 REL :: exclude PAR, CHD
93  
94 If you go to the configuration file directory, there will
95 be example configuration files for the different runs that
96 you have performed.
97  
98 Note: You can use relations other than PAR/CHD and RB/RN for propagation
99 but we do not recommend it. The PAR/CHD and RB/RN relations are considered
100 the heirarchical relations in the UMLS which is required for propagation to
101 perform correctly.
102  
103 =head3 --disregard
104  
105 This ignores the SAB configuration that the icfrequency
106 file was created with
107  
108 =head3 --precision N
109  
110 Displays values upto N places of decimal.
111  
112 =head3 --username STRING
113  
114 Username is required to access the umls database on MySql
115  
116 =head3 --password STRING
117  
118 Password is required to access the umls database on MySql
119  
120 =head3 --hostname STRING
121  
122 Hostname where mysql is located. DEFAULT: localhost
123  
124 =head3 --database STRING
125  
126 Database contain UMLS DEFAULT: umls
127  
128 =head3 --debug
129  
130 Sets the UMLS-Interface debug flag on for testing
131  
132 =head3 --help
133  
134 Displays the quick summary of program options.
135  
136 =head3 --version
137  
138 Displays the version information.
139  
140 =head1 PROPAGATION
141  
142 The Information Content (IC) is defined as the negative log
143 of the probability of a concept.
144  
145 The probability of a concept, c, is determine by summing the
146 probability of the concept (P(c)) ocurring in some text plus
147 the probability its decendants (P(d)) occuring in some text
148 as see in below:
149  
150 P(c*) = P(c) + \sum_{d\exists decendant(c)} P(d)
151  
152  
153 The initial probability of a concept (P(c)) and its decendants
154 (P(d)) is obtained by dividing the number of times a concept is
155 seen in the corpus (freq(d)) by the total number of concepts (N)
156 as seen below:
157  
158 P(d) = freq(d) / N
159  
160 Not all of the concepts in the taxonomy will be seen in the corpus.
161 The package includes the option of using Laplace smoothing, where the
162 frequency count of each of the concepts in the taxonomy is incremented
163 by one. The advantage of doing this is that it avoides having a concept
164 that has a probability of zero. The disadvantage is that it can shift
165 the overall probability mass of the concepts from what is actually
166 seen in the corpus.
167  
168  
169 For more information on how this is calculated please see
170 the README file.
171  
172 =head1 SYSTEM REQUIREMENTS
173  
174 =over
175  
176 =item * Perl (version 5.8.5 or better) - http://www.perl.org
177  
178 =item * UMLS::Interface - http://search.cpan.org/dist/UMLS-Interface
179  
180 =item * UMLS::Similarity - http://search.cpan.org/dist/UMLS-Similarity
181  
182 =item * Text::NSP - http://search.cpan.org/dist/Text-NSP
183  
184 =item * MetaMap - http://mmtx.nlm.nih.gov/
185  
186 =back
187  
188 =head1 CONTACT US
189  
190 If you have any trouble installing and using CreatePropagationFile,
191 please contact us via the users mailing list :
192
193 umls-similarity@yahoogroups.com
194
195 You can join this group by going to:
196
197 http://tech.groups.yahoo.com/group/umls-similarity/
198
199 You may also contact us directly if you prefer :
200
201 Bridget T. McInnes: bthomson at cs.umn.edu
202  
203 Ted Pedersen : tpederse at d.umn.edu
204  
205 =head1 AUTHOR
206  
207 Bridget T. McInnes, University of Minnesota
208  
209 =head1 COPYRIGHT
210  
211 Copyright (c) 2007-2011,
212  
213 Bridget T. McInnes, University of Minnesota
214 bthomson at cs.umn.edu
215
216 Ted Pedersen, University of Minnesota Duluth
217 tpederse at d.umn.edu
218  
219  
220 Siddharth Patwardhan, University of Utah, Salt Lake City
221 sidd@cs.utah.edu
222
223 Serguei Pakhomov, University of Minnesota Twin Cities
224 pakh0002@umn.edu
225  
226 This program is free software; you can redistribute it and/or modify it under
227 the terms of the GNU General Public License as published by the Free Software
228 Foundation; either version 2 of the License, or (at your option) any later
229 version.
230  
231 This program is distributed in the hope that it will be useful, but WITHOUT
232 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
233 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
234  
235 You should have received a copy of the GNU General Public License along with
236 this program; if not, write to:
237  
238 The Free Software Foundation, Inc.,
239 59 Temple Place - Suite 330,
240 Boston, MA 02111-1307, USA.
241  
242 =cut
243  
244 ###############################################################################
245  
246 # THE CODE STARTS HERE
247 ###############################################################################
248  
249 # ================================
250 # COMMAND LINE OPTIONS AND USAGE
251 # ================================
252  
253 use UMLS::Interface;
254 use Getopt::Long;
255 use File::Path;
256  
257 eval(GetOptions( "version", "help", "username=s", "password=s", "hostname=s", "database=s", "socket=s", "config=s", "debug", "t", "precision=s", "smooth", "disregard", "st")) or die ("Please check the above mentioned option(s).\n");
258  
259  
260 # if help is defined, print out help
261 if( defined $opt_help ) {
262 $opt_help = 1;
263 &showHelp();
264 exit;
265 }
266  
267 # if version is requested, show version
268 if( defined $opt_version ) {
269 $opt_version = 1;
270 &showVersion();
271 exit;
272 }
273  
274 # At least 2 terms should be given on the command line.
275 if( scalar(@ARGV) < 2) {
276 print STDERR "At least 2 files should be specified on the command line.\n";
277 &minimalUsageNotes();
278 exit;
279 }
280  
281 # get the input and output files
282 my $outputfile = shift;
283 my $inputfile = shift;
284  
285 # check to see if output file exists, and if so, if we should overwrite...
286 if ( -e $outputfile )
287 {
288 print "Output file $outputfile already exists! Overwrite (Y/N)? ";
289 $reply = <STDIN>;
290 chomp $reply;
291 $reply = uc $reply;
292 exit 0 if ($reply ne "Y");
293 }
294  
295 # initialize variables
296 my $database = "";
297 my $hostname = "";
298 my $socket = "";
299 my $umls = "";
300 my $floatformat = "";
301 my $smooth = 0;
302  
303 # check the options
304 &checkOptions ();
305 &setOptions ();
306  
307 # load the UMLS
308 &loadUMLS ();
309  
310 my $sabstring = $umls->getSabString();
311 my $relstring = $umls->getRelString();
312 my $relastring = $umls->getRelaString();
313  
314 # check parameters
315 &checkParameters($inputfile);
316  
317 # get the frequency counts
318 my $cuiHash = &getFileCounts($inputfile);
319  
320 # set the information content parameters
321
322 # propagate the counts
323 my $propagationHash = "";
324 my $N = "";
325 if(defined $opt_st) {
326  
327 # if defined smoothing set the smoothing parameter
328 if(defined $opt_smooth) {
329 $umls->setStSmoothing();
330 }
331  
332 # propagate the semantic type counts
333 $propagationHash = $umls->propagateStCounts($cuiHash);
334
335 # get the total number of semantic types in the frequency file
336 $N = $umls->getStN();
337 }
338 else {
339
340 # set the propagation parameters
341 my %params = ();
342 if(defined $opt_smooth) { $params{"smooth"} = 1; }
343 $umls->setPropagationParameters(\%params);
344
345 # propagate the counts
346 $propagationHash = $umls->propagateCounts($cuiHash);
347
348 # get the total number of CUIs in the frequency file
349 $N = $umls->getN();
350 }
351  
352 # print out the propagation counts
353 open(OUTPUT, ">$outputfile") || die "Could not open $outputfile\n";
354 print OUTPUT "SMOOTH: $smooth\n";
355 print OUTPUT "$sabstring\n";
356 print OUTPUT "$relstring\n";
357 if($relastring ne "") {
358 print OUTPUT "$relastring\n";
359 }
360 print OUTPUT "N :: $N\n";
361 foreach my $cui (sort keys %{$propagationHash}) {
362 my $freq = ${$propagationHash}{$cui};
363  
364 # check if precision needs to be set
365 if(defined $opt_precision) {
366 $freq = sprintf $floatformat, $freq;
367 }
368
369 print OUTPUT "$cui<>$freq\n";
370 }
371 close OUTPUT;
372  
373 sub checkParameters {
374 # get the frequency file
375 my $file = shift;
376
377 # open the file to get the parameters
378 open(FILE, $file) || die "Could not open --icfrequency file : $file\n";
379  
380 # check to make certain the source/relations are correct
381 my $fsabstring = <FILE>; chomp $fsabstring;
382 my $frelstring = <FILE>; chomp $frelstring;
383 my $frelastring = <FILE>; chomp $frelastring;
384  
385 # check if rela is actually specified
386 if($frelastring=~/N\s*\:\:/) { $frelastring = ""; }
387  
388 if(!$opt_disregard) {
389 # check the sources
390 if(! ($umls->checkParameters($fsabstring, $sabstring)) ) {
391 print STDERR "The icfrequency file was created using the following configuration:\n";
392 print STDERR " $fsabstring\n";
393 print STDERR "Please modify your configuration file.\n\n";
394 exit;
395 }
396 # check the relations
397 if(! ($umls->checkParameters($frelstring, $relstring)) ) {
398 print STDERR "The icfrequency file was created using the following configuration:\n";
399 print STDERR " $frelstring\n";
400 print STDERR "Please modify your configuration file.\n\n";
401 exit;
402 }
403 # check the relas if we have them - this on is optional
404 if($frelastring=~/(include|exclude)/ || $relastring=~/(include|exclude)/) {
405 if(! ($umls->checkParameters($frelastring, $relastring)) ) {
406 if($frelastring ne "") {
407 print STDERR "The icfrequency file was created using the following configuration:\n";
408 print STDERR " $frelastring\n";
409 }
410 else {
411 print STDERR "The icfrequency file was not created using the following configuration:\n";
412 print STDERR " $relastring\n";
413 }
414 print STDERR "Please modify your configuration file.\n\n";
415 exit;
416 }
417 }
418 }
419  
420 # check to make certain the relations used are allowed
421 # propagation is set to only work for the PAR, CHD, RB and RN relations
422 if(! ($umls->checkHierarchicalRelations($frelstring)) ) {
423 print STDERR "The icfrequency file was created using the following configuration:\n";
424 print STDERR " $frelstring\n";
425 print STDERR "Propagation is set only to work for the PAR, CHD, RB and RN relations.\n\n";
426 exit;
427 }
428
429 close FILE;
430 }
431 sub getFileCounts {
432
433 my $file = shift;
434
435 # open the file
436 open(FILE, $file) || die "Could not open --icfrequency file : $file\n";
437  
438 # get the frequency counts
439 my %hash = ();
440 while(<FILE>) {
441 chomp;
442
443 # remove the headers
444 if($_=~/(include|exclude)/) { next; }
445 if($_=~/N\s*\:\:/) { next; }
446
447 # get the cui and the frequency
448 my ($cui, $freq) = split/<>/;
449  
450 # add tot he hash
451 if(exists $cuiHash{$cui}) {
452 $hash{$cui} += $freq;
453 }
454 else {
455 $hash{$cui} = $freq;
456 }
457 }
458 close FILE;
459
460 return \%hash;
461 }
462  
463 sub timeStamp {
464 my ($stamp);
465 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
466
467 $year += 1900;
468 $mon++;
469 $d = sprintf("%4d%2.2d%2.2d",$year,$mon,$mday);
470 $t = sprintf("%2.2d%2.2d%2.2d",$hour,$min,$sec);
471
472 $stamp = $d . $t;
473 return($stamp);
474 }
475  
476 # checks the user input options
477 sub checkOptions {
478  
479 if( (defined $opt_username && !defined $opt_password) ||
480 (!defined $opt_username && defined $opt_password) ) {
481 print STDERR "The --username and --password options\n";
482 print STDERR "must both be specified if you are using\n";
483 print STDERR "one of them.\n\n";
484 &minimalUsageNotes();
485 exit;
486 }
487  
488  
489 if(defined $opt_precision) {
490 if ($opt_precision !~ /^\d+$/) {
491 print STDERR "Value for switch --precision should be integer >= 0\n";
492 &minimalUsageNotes();
493 exit;
494 }
495  
496 }
497 }
498  
499 # set user input and default options
500 sub setOptions {
501  
502 if($debug) { print STDERR "In setOptions\n"; }
503  
504 my $default = "";
505 my $set = "";
506  
507 # check config file
508 if(defined $opt_config) {
509 $config = $opt_config;
510 $set .= " --config $config\n";
511 }
512  
513 if(defined $opt_disregard) {
514 $set .= " --disregard\n";
515 }
516  
517 if(defined $opt_smooth) {
518 $set .= " --smooth\n";
519 $smooth = 1;
520 }
521  
522 if(defined $opt_st) {
523 $set .= " --st\n";
524 }
525
526 # set databasee options
527 if(defined $opt_username) {
528  
529 if(defined $opt_username) {
530 $set .= " --username $opt_username\n";
531 }
532 if(defined $opt_password) {
533 $set .= " --password XXXXXXX\n";
534 }
535 if(defined $opt_database) {
536 $database = $opt_database;
537 $set .= " --database $database\n";
538 }
539 else {
540 $database = "umls";
541 $default .= " --database $database\n";
542 }
543  
544 if(defined $opt_hostname) {
545 $hostname = $opt_hostname;
546 $set .= " --hostname $hostname\n";
547 }
548 else {
549 $hostname = "localhost";
550 $default .= " --hostname $hostname\n";
551 }
552
553 if(defined $opt_socket) {
554 $socket = $opt_socket;
555 $set .= " --socket $socket\n";
556 }
557 else {
558 $socket = "/tmp/mysql.sock\n";
559 $default .= " --socket $socket\n";
560 }
561 }
562
563 if(defined $opt_precision) {
564  
565 # create the floating point conversion format as required by sprintf!
566 $floatformat = join '', '%', '.', $opt_precision, 'f';
567
568 # set the output information
569 $set .= " --precision $opt_precision";
570 }
571
572
573 if(defined $opt_debug) {
574 $set .= " --debug\n";
575 }
576  
577 # check settings
578 if($default eq "") { $default = " No default settings\n"; }
579 if($set eq "") { $set = " No user defined settings\n"; }
580  
581 # print options
582 print STDERR "Default Settings:\n";
583 print STDERR "$default\n";
584
585 print STDERR "User Settings:\n";
586 print STDERR "$set\n";
587 }
588  
589 # load the UMLS
590 sub loadUMLS {
591
592 if(defined $opt_t) {
593 $option_hash{"t"} = 1;
594 }
595 if(defined $opt_config) {
596 $option_hash{"config"} = $opt_config;
597 }
598 if(defined $opt_debug) {
599 $option_hash{"debug"} = $opt_debug;
600 }
601 if(defined $opt_username and defined $opt_password) {
602 $option_hash{"driver"} = "mysql";
603 $option_hash{"database"} = $database;
604 $option_hash{"username"} = $opt_username;
605 $option_hash{"password"} = $opt_password;
606 $option_hash{"hostname"} = $hostname;
607 $option_hash{"socket"} = $socket;
608 }
609
610 $option_hash{"realtime"} = 1;
611  
612 $umls = UMLS::Interface->new(\%option_hash);
613 die "Unable to create UMLS::Interface object.\n" if(!$umls);
614 }
615  
616 ##############################################################################
617 # function to output minimal usage notes
618 ##############################################################################
619 sub minimalUsageNotes {
620
621 print "Usage: create-icpropagation.pl [OPTIONS] OUTPUTFILE ICFREQUENCY_FILE\n";
622 &askHelp();
623 exit;
624 }
625  
626 ##############################################################################
627 # function to output help messages for this program
628 ##############################################################################
629 sub showHelp() {
630
631 print "This is a utility that takes as input an output and input\n";
632 print "file and determines the propagation counts of the CUIs in\n";
633 print "a specified set of sources and relation using the frequency\n";
634 print "information from the inputfile\n\n";
635
636 print "Usage: create-icpropagation.pl [OPTIONS] OUTPUTFILE INPUTFILE\n\n";
637  
638 print "Options:\n\n";
639  
640 print "--config FILE Configuration file\n\n";
641  
642 print "--st Outputs the probability of the semantic\n";
643 print " types in the icfrequency file.\n\n";
644  
645 print "--smooth Incorporate LaPlace smoothing\n\n";
646  
647 print "--precision N Displays values upto N places of decimal.\n\n";
648 print "--username STRING Username required to access mysql\n\n";
649  
650 print "--password STRING Password required to access mysql\n\n";
651  
652 print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n";
653  
654 print "--database STRING Database contain UMLS (DEFAULT: umls)\n\n";
655  
656 print "--debug Sets the UMLS-Interface debug flag on\n";
657 print " for testing purposes\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 ##############################################################################
665 # function to output the version number
666 ##############################################################################
667 sub showVersion {
668 print '$Id: create-icpropagation.pl,v 1.12 2011/05/20 13:23:56 btmcinnes Exp $';
669 print "\nCopyright (c) 2008-2011, Ted Pedersen & Bridget McInnes\n";
670 }
671  
672 ##############################################################################
673 # function to output "ask for help" message when user's goofed
674 ##############################################################################
675 sub askHelp {
676 print STDERR "Type create-icpropagation.pl --help for help.\n";
677 }