blib/script/getIC.pl untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 205 SCAR 6.9
CC
1
Cov
0%
minimalUsageNotes
2.0
showHelp
2.0
showVersion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 getIC.pl - This program returns the information content of a concept or a term.
6  
7 =head1 SYNOPSIS
8  
9 This program takes in a CUI or a term and returns its information content.
10  
11 =head1 USAGE
12  
13 Usage: getIC.pl [OPTION] [CUI|TERM]
14  
15 =head1 INPUT
16  
17 =head2 Required Arguments:
18  
19 =head3 [CUI|TERM}
20  
21 Concept Unique Identifier (CUI) or a term from the Unified Medical
22 Language System (UMLS)
23  
24 =head2 Optional Arguments:
25  
26 =head3 --intrinsic [seco|sanchez]
27  
28 Uses intrinic information content of the CUIs defined by Sanchez and
29 Betet 2011 or Seco, et al 2004.
30  
31 =head3 --icfrequency
32  
33 Calculate information content using the frequency information
34 in FILE. The file must be in the following format:
35  
36 CUI<>freq
37 CUI<>freq
38  
39 See the example files called icfrequency in the samples/ directory.
40  
41 =head3 --icpropagation FILE
42  
43 Calculate information content using the probability information
44 in FILE. The file must be in the following format:
45  
46 CUI<>prob
47 CUI<>prob
48  
49 See the example files called icpropagation in the samples/ directory.
50  
51 =head3 --config FILE
52  
53 This is the configuration file. The format of the configuration
54 file is as follows:
55  
56 SAB :: <include|exclude> <source1, source2, ... sourceN>
57  
58 REL :: <include|exclude> <relation1, relation2, ... relationN>
59  
60 RELA :: <include|exclude> <rela1, rela2, ... relaN> (optional)
61  
62 For example, if we wanted to use the MSH vocabulary with only
63 the RB/RN relations, the configuration file would be:
64  
65 SAB :: include MSH
66 REL :: include RB, RN
67 RELA :: include inverse_isa, isa
68  
69 or
70  
71 SAB :: include MSH
72 REL :: exclude PAR, CHD
73  
74 If you go to the configuration file directory, there will
75 be example configuration files for the different runs that
76 you have performed.
77  
78  
79 =head3 --smooth
80  
81 Incorporate Laplace smoothing, where the frequency count of each of the
82 concepts in the taxonomy is incremented by one. The advantage of
83 doing this is that it avoides having a concept that has a probability
84 of zero. The disadvantage is that it can shift the overall probability
85 mass of the concepts from what is actually seen in the corpus.
86  
87  
88 =head3 --realtime
89  
90 This option will not create a database of the information content
91 for all of concepts in the specified set of sources and relations
92 in the config file
93  
94 =head3 --infile
95  
96 Takes a file of CUIs (one per line) and returns their information
97 content.
98  
99 =head3 --debug
100  
101 Sets the debug flag for testing
102  
103 =head3 --username STRING
104  
105 Username is required to access the umls database on MySql
106 unless it was specified in the my.cnf file at installation
107  
108 =head3 --password STRING
109  
110 Password is required to access the umls database on MySql
111 unless it was specified in the my.cnf file at installation
112  
113 =head3 --hostname STRING
114  
115 Hostname where mysql is located. DEFAULT: localhost
116  
117 =head3 --socket STRING
118  
119 The socket your mysql is using. DEFAULT: /tmp/mysql.sock
120  
121 =head3 --database STRING
122  
123 Database contain UMLS DEFAULT: umls
124  
125 =head4 --help
126  
127 Displays the quick summary of program options.
128  
129 =head4 --version
130  
131 Displays the version information.
132  
133 =head1 OUTPUT
134  
135 List of CUIs that are associated with the input term
136  
137 =head1 SYSTEM REQUIREMENTS
138  
139 =over
140  
141 =item * Perl (version 5.8.5 or better) - http://www.perl.org
142  
143 =back
144  
145 =head1 AUTHOR
146  
147 Bridget T. McInnes, University of Minnesota
148  
149 =head1 COPYRIGHT
150  
151 Copyright (c) 2007-2009,
152  
153 Bridget T. McInnes, University of Minnesota
154 bthomson at cs.umn.edu
155
156 Ted Pedersen, University of Minnesota Duluth
157 tpederse at d.umn.edu
158  
159 Siddharth Patwardhan, University of Utah, Salt Lake City
160 sidd@cs.utah.edu
161
162 Serguei Pakhomov, University of Minnesota Twin Cities
163 pakh0002@umn.edu
164  
165 This program is free software; you can redistribute it and/or modify it under
166 the terms of the GNU General Public License as published by the Free Software
167 Foundation; either version 2 of the License, or (at your option) any later
168 version.
169  
170 This program is distributed in the hope that it will be useful, but WITHOUT
171 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
172 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
173  
174 You should have received a copy of the GNU General Public License along with
175 this program; if not, write to:
176  
177 The Free Software Foundation, Inc.,
178 59 Temple Place - Suite 330,
179 Boston, MA 02111-1307, USA.
180  
181 =cut
182  
183 ###############################################################################
184  
185 # THE CODE STARTS HERE
186 ###############################################################################
187  
188 # ================================
189 # COMMAND LINE OPTIONS AND USAGE
190 # ================================
191  
192  
193 use UMLS::Interface;
194 use Getopt::Long;
195  
196 eval(GetOptions( "version", "help", "debug", "intrinsic=s", "infile=s", "icfrequency=s", "icpropagation=s", "realtime", "username=s", "password=s", "hostname=s", "database=s", "socket=s", "config=s", "smooth")) or die ("Please check the above mentioned option(s).\n");
197  
198  
199 # if help is defined, print out help
200 if( defined $opt_help ) {
201 $opt_help = 1;
202 &showHelp();
203 exit;
204 }
205  
206 # if version is requested, show version
207 if( defined $opt_version ) {
208 $opt_version = 1;
209 &showVersion();
210 exit;
211 }
212  
213 # At least 1 CUI should be given on the command line.
214 if(scalar(@ARGV) < 1) {
215 print STDERR "No term or file was specified on the command line\n";
216 &minimalUsageNotes();
217 exit;
218 }
219  
220 my $umls = "";
221 my %option_hash = ();
222  
223 if(defined $opt_intrinsic) {
224 $option_hash{"intrinsic"} = $opt_intrinsic;
225 }
226 elsif(defined $opt_icfrequency) {
227 $option_hash{"icfrequency"} = $opt_icfrequency;
228 }
229 else {
230 $option_hash{"icpropagation"} = $opt_icpropagation;
231 }
232  
233 if(defined $opt_realtime) {
234 $option_hash{"realtime"} = $opt_realtime;
235 }
236 if(defined $opt_config) {
237 $option_hash{"config"} = $opt_config;
238 }
239 if(defined $opt_smooth) {
240 $option_hash{"smooth"} = $opt_smooth;
241 }
242 if(defined $opt_verbose) {
243 $option_hash{"verbose"} = $opt_verbose;
244 }
245 if(defined $opt_debug) {
246 $option_hash{"debug"} = $opt_debug;
247 }
248 if(defined $opt_username) {
249 $option_hash{"username"} = $opt_username;
250 }
251 if(defined $opt_driver) {
252 $option_hash{"driver"} = $opt_driver;
253 }
254 if(defined $opt_database) {
255 $option_hash{"database"} = $opt_database;
256 }
257 if(defined $opt_password) {
258 $option_hash{"password"} = $opt_password;
259 }
260 if(defined $opt_hostname) {
261 $option_hash{"hostname"} = $opt_hostname;
262 }
263 if(defined $opt_socket) {
264 $option_hash{"socket"} = $opt_socket;
265 }
266  
267 $umls = UMLS::Interface->new(\%option_hash);
268 die "Unable to create UMLS::Interface object.\n" if(!$umls);
269  
270 $umls->setPropagationParameters(\%option_hash);
271
272 # if the icpropagation or icfrequency option is not
273 # defined set the default options for icpropagation
274 if( (!defined $opt_intrinsic) &&
275 (!defined $opt_icpropagation) &&
276 (!defined $opt_icfrequency) ) {
277
278 print STDERR "Setting default propagation file\n";
279
280 # get the icfrequency file
281 my $icfrequency = ""; foreach my $path (@INC) {
282 if(-e $path."/UMLS/icfrequency.default.dat") {
283 $icfrequency = $path."/UMLS/icfrequency.default.dat";
284 }
285 elsif(-e $path."\\UMLS\\icfrequency.default.dat") {
286 $icfrequency = $path."\\UMLS\\icfrequency.default.dat";
287 }
288 }
289
290 # set the cuilist
291 my $fhash = $umls->getCuiList();
292
293 # load the frequency counts
294 open(FILE, $icfrequency) || die "Could not open $icfrequency\n";
295 while(<FILE>) {
296 chomp;
297 my ($cui, $freq) = split/<>/;
298 if(exists ${$fhash}{$cui}) {
299 ${$fhash}{$cui} = $freq;
300 }
301 }
302
303 # propagate the counts
304 my $phash = $umls->propagateCounts($fhash);
305 }
306  
307 my @array = ();
308 if(defined $opt_infile) {
309 open(FILE, $opt_infile) || die "Could not open $opt_infile\n";
310 while(<FILE>) {
311 chomp;
312 push @array, $_;
313 }
314 }
315 else {
316 my $input = shift;
317 push @array, $input;
318 }
319  
320 foreach my $input (@array) {
321 my $term = $input;
322 my $c = undef;
323  
324 if($input=~/C[0-9]+/) {
325 push @{$c}, $input;
326 my $terms = $umls->getConceptList($input);
327 $term = shift @{$terms};
328 }
329 else {
330 $c = $umls->getConceptList($input);
331 }
332
333 my $printFlag = 0;
334 my $precision = 4;
335 my $floatformat = join '', '%', '.', $precision, 'f';
336 foreach my $cui (@{$c}) {
337 # make certain cui exists in this view
338 if($umls->exists($cui) == 0) { print STDERR "$cui\n"; next; }
339
340 my $ic = 0;
341 if(defined $opt_intrinsic) {
342 if($opt_intrinsic=~/seco/) {
343 $ic = $umls->getSecoIntrinsicIC($cui);
344 }
345 else {
346 $ic = $umls->getSanchezIntrinsicIC($cui);
347 }
348 }
349 else {
350 $ic = $umls->getIC($cui);
351 }
352 # my $pic = sprintf $floatformat, $ic;
353 # my $pprob = sprintf $floatformat, $prob;
354
355 print "The information content of $term ($cui) is $ic\n";
356 }
357 }
358  
359 ##############################################################################
360 # function to output minimal usage notes
361 ##############################################################################
362 sub minimalUsageNotes {
363
364 print "Usage: getIC.pl [OPTIONS] [CUI|TERM] \n";
365 &askHelp();
366 exit;
367 }
368  
369 ##############################################################################
370 # function to output help messages for this program
371 ##############################################################################
372 sub showHelp() {
373  
374
375 print "This is a utility that takes as input a term \n";
376 print "or a CUI and returns its information content (IC).\n\n";
377
378 print "Usage: getIC.pl [OPTIONS] IC | FREQUENCY FILE [CUI|TERM]\n\n";
379  
380 print "Options:\n\n";
381  
382 print "--infile FILE File containing TERM or CUI pairs\n\n";
383  
384 print "--intrinsic [seco|sanchez] Use the intrinsic IC defined either by\n";
385 print " Seco et al 2004 or Sanchez et al 2011\n\n";
386  
387 print "--icfrequency Flag specifying that a frequency file\n";
388 print " was specified on the command line\n\n";
389  
390 print "--icpropagation Flag specifiying that a propagation file\n";
391 print " was specified (this is the DEFAULT)\n\n";
392  
393 print "--realtime This option finds the information content\n";
394 print " in realtime rather than building an index\n\n";
395  
396 print "--config FILE Configuration file\n\n";
397  
398 print "--smooth Incorporate Laplace smoothing, when \n";
399 print " calculating the probability of a concept\n\n";
400 print "--debug Sets the debug flag for testing\n\n";
401  
402 print "--username STRING Username required to access mysql\n\n";
403  
404 print "--password STRING Password required to access mysql\n\n";
405  
406 print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n";
407  
408 print "--database STRING Database contain UMLS (DEFAULT: umls)\n\n";
409
410 print "--socket STRING Socket used by mysql (DEFAULT: /tmp.mysql.sock)\n\n";
411  
412 print "--version Prints the version number\n\n";
413
414 print "--help Prints this help message.\n\n";
415 }
416  
417 ##############################################################################
418 # function to output the version number
419 ##############################################################################
420 sub showVersion {
421 print '$Id: getIC.pl,v 1.20 2013/04/21 13:20:22 btmcinnes Exp $';
422 print "\nCopyright (c) 2008, Ted Pedersen & Bridget McInnes\n";
423 }
424  
425 ##############################################################################
426 # function to output "ask for help" message when user's goofed
427 ##############################################################################
428 sub askHelp {
429 print STDERR "Type getIC.pl --help for help.\n";
430 }
431