blib/script/findShortestPathLength.pl untested

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