blib/script/findNumberTurns.pl untested

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