blib/script/findPathToRoot.pl untested

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