blib/script/create-icfrequency.pl untested

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