blib/script/findDFS.pl untested

filter: Statementstmt 0.0% 0 / 280 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 430 SCAR 75.5
CC
43
Cov
0%
_depthFirstSearch
1892.0
minimalUsageNotes
2.0
showHelp
2.0
CRAP 1892.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 findDFS.pl - This program runs a dfs over a specified set of sources
6 and relations in the UMLS.
7  
8 =head1 SYNOPSIS
9  
10 This is a utility runs a dfs over a specified set of sources
11 and relations in the UMLS returning the depth, number of paths
12 to the root, branching factor, leaf and node count.
13  
14 =head1 USAGE
15  
16 Usage: findDFS.pl CONFIGFILE [OPTIONS]
17  
18 =head1 INPUT
19  
20 =head2 Required Arguments:
21  
22 =head3 CONFIGFILE
23  
24 Configuration file containing the set of sources and
25 relations to use. The default uses MSH and the PAR/CHD
26 relations.
27  
28 The format of the configuration file is as follows:
29  
30 SAB :: <include|exclude> <source1, source2, ... sourceN>
31  
32 REL :: <include|exclude> <relation1, relation2, ... relationN>
33  
34 RELA :: <include|exclude> <rela1, rela2, ... relaN> (optional)
35  
36 The SAB, REL and RELA are for specifing what sources and relations
37 should be used when traversing the UMLS. For example, if we
38 wanted to use the MSH vocabulary with only the RB/RN relations,
39 the configuration file would be:
40  
41 SAB :: include MSH
42 REL :: include RB, RN
43 RELA :: include isa, inverse_isa
44  
45 or if we wanted to use MSH and use any relation except for PAR/CHD,
46 the configuration would be:
47  
48 SAB :: include MSH
49 REL :: exclude PAR, CHD
50  
51 An example of the configuration file can be seen in the samples/ directory.
52  
53 =head2 Optional Arguments:
54  
55 =head3 --debug
56  
57 Sets the debug flag for testing
58  
59 =head3 --username STRING
60  
61 Username is required to access the umls database on MySql
62 unless it was specified in the my.cnf file at installation
63  
64 =head3 --password STRING
65  
66 Password is required to access the umls database on MySql
67 unless it was specified in the my.cnf file at installation
68  
69 =head3 --hostname STRING
70  
71 Hostname where mysql is located. DEFAULT: localhost
72  
73 =head3 --socket STRING
74  
75 The socket your mysql is using. DEFAULT: /tmp/mysql.sock
76  
77 =head3 --database STRING
78  
79 Database contain UMLS DEFAULT: umls
80  
81 =head3 --debugpath FILE
82  
83 This option prints out the path information for debugging
84 purposes.
85  
86 =head3 --depth NUMBER
87  
88 Searches up to the specified depth. The default is to
89 search the complete hierarchy
90  
91 =head3 --root CUI
92  
93 Starts the search at a specified CUI. The default starts
94 the search at the UMLS root node
95  
96 =head3 --level NUMBER
97  
98 Returns the number of CUIs above and below this NUMBER
99  
100 =head3 --help
101  
102 Displays the quick summary of program options.
103  
104 =head3 --version
105  
106 Displays the version information.
107  
108 =head1 OUTPUT
109  
110 The program returns the following:
111  
112 1. the maximum depth
113 2. paths to root
114 3. sources
115 4. maximum branching factor
116 5. average branching factor
117 6. number of leaf nodes
118 7. number of nodes
119 8. root
120  
121  
122 =head1 SYSTEM REQUIREMENTS
123  
124 =over
125  
126 =item * Perl (version 5.8.5 or better) - http://www.perl.org
127  
128 =back
129  
130 =head1 AUTHOR
131  
132 Bridget T. McInnes, University of Minnesota
133  
134 =head1 COPYRIGHT
135  
136 Copyright (c) 2007-2009,
137  
138 Bridget T. McInnes, University of Minnesota
139 bthomson at cs.umn.edu
140
141 Ted Pedersen, University of Minnesota Duluth
142 tpederse at d.umn.edu
143  
144 Siddharth Patwardhan, University of Utah, Salt Lake City
145 sidd@cs.utah.edu
146
147 Serguei Pakhomov, University of Minnesota Twin Cities
148 pakh0002@umn.edu
149  
150 This program is free software; you can redistribute it and/or modify it under
151 the terms of the GNU General Public License as published by the Free Software
152 Foundation; either version 2 of the License, or (at your option) any later
153 version.
154  
155 This program is distributed in the hope that it will be useful, but WITHOUT
156 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
157 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
158  
159 You should have received a copy of the GNU General Public License along with
160 this program; if not, write to:
161  
162 The Free Software Foundation, Inc.,
163 59 Temple Place - Suite 330,
164 Boston, MA 02111-1307, USA.
165  
166 =cut
167  
168 ###############################################################################
169  
170 # THE CODE STARTS HERE
171 ###############################################################################
172  
173 # ================================
174 # COMMAND LINE OPTIONS AND USAGE
175 # ================================
176  
177  
178 use UMLS::Interface;
179 use Getopt::Long;
180  
181 eval(GetOptions( "version", "help", "username=s", "password=s", "hostname=s", "database=s", "socket=s", "depth=s", "root=s", "debugpath=s", "debug", "level=s")) or die ("Please check the above mentioned option(s).\n");
182  
183  
184  
185 # if help is defined, print out help
186 if( defined $opt_help ) {
187 $opt_help = 1;
188 &showHelp();
189 exit;
190 }
191  
192 # if version is requested, show version
193 if( defined $opt_version ) {
194 $opt_version = 1;
195 &showVersion();
196 exit;
197 }
198  
199 # At least 1 term should be given on the command line.
200 if(scalar(@ARGV) < 1) {
201 print STDERR "The config file was not specified on the command line\n";
202 &minimalUsageNotes();
203 exit;
204 }
205  
206 my $config = shift;
207  
208 my $umls = "";
209  
210 my %option_hash = ();
211  
212 $option_hash{"config"} = $config;
213  
214 if(defined $opt_debug) {
215 $option_hash{"debug"} = $opt_debug;
216 }
217  
218 if(defined $opt_username) {
219 $option_hash{"username"} = $opt_username;
220 }
221 if(defined $opt_driver) {
222 $option_hash{"driver"} = $opt_driver;
223 }
224 if(defined $opt_database) {
225 $option_hash{"database"} = $opt_database;
226 }
227 if(defined $opt_password) {
228 $option_hash{"password"} = $opt_password;
229 }
230 if(defined $opt_hostname) {
231 $option_hash{"hostname"} = $opt_hostname;
232 }
233 if(defined $opt_socket) {
234 $option_hash{"socket"} = $opt_socket;
235 }
236  
237 $umls = UMLS::Interface->new(\%option_hash);
238 die "Unable to create UMLS::Interface object.\n" if(!$umls);
239  
240 # get the relations from the configuration file
241 my $configrel = $umls->getRelString();
242 $configrel=~/(REL) (\:\:) (include|exclude) (.*?)$/;
243 my $relationstring = $4;
244  
245 # check to make certain the configuration file only contains
246 # heirarchical relations (PAR/CHD or RB/RN).
247 my @relations = split/\s*\,\s*/, $relationstring;
248 foreach my $rel (@relations) {
249 if(! ($rel=~/(PAR|CHD|RB|RN)/) ) {
250 print STDERR "The findDFS.pl program only supports heirarchical relations (PAR/CHD or RB/RN).\n";
251 &minimalUsageNotes();
252 exit;
253 }
254 }
255  
256 # set the root
257 my $root = "C0000000";
258 if(defined $opt_root) {
259 $root = $opt_root;
260 }
261  
262 # set paths to root counter;
263 my $paths_to_root = 0;
264  
265 # set branching variables
266 my $max_branch = 0;
267 my $avg_branch = 0;
268 my $branch_counter = 0;
269 my %branch_hash = ();
270  
271 # set leaf node counter
272 my %leafs = ();
273 my %nodes = ();
274 my %decendents = ();
275  
276 # set max depth variable
277 my $max_depth = 0;
278  
279 # set the sources
280 my $sources = "";
281 open(CONFIG, $config) || die "Could not open config file: $config\n";
282 while(<CONFIG>) {
283 if($_=~/SAB\s+\:\:\s+(include|exclude)\s+(.*)/) {
284 $sources = $2;
285 }
286 }
287  
288 # if the debugpath option is turned on open up the table file
289 if($opt_debugpath) {
290
291 open(TABLEFILE, ">$opt_debugpath") || die "Could not open $opt_debugpath";
292 }
293  
294 # get the first set of children and start the dfs
295 my $children= $umls->getChildren($root);
296  
297 # update the branching variables
298 $max_branch = $#{$children} + 1;
299 $branch_hash{$root} = $max_branch;
300  
301 foreach my $child (@{$children}) {
302 my @array = ();
303 push @array, $root;
304 my $path = \@array;
305 my $d = 0;
306 &_depthFirstSearch($child, $d, $path,*TABLEFILE);
307 }
308  
309 # set the node count for the root
310 if($#{$children} >= 0) {
311 $nodes{$root}++;
312 $decendents{$root}++;
313 }
314 else {
315 $leafs{$root}++;
316 }
317  
318 # close the file and set the permissions
319 if($opt_debugpath) {
320 close TABLEFILE;
321 my $temp = chmod 0777, $tableFile;
322 }
323  
324 # calculate the max and average number of branches
325 foreach my $cui (sort keys %branch_hash) {
326 if($branch_hash{$cui} > $max_branch) { $max_branch = $branch_hash{$cui}; }
327 $avg_branch += $branch_hash{$cui};
328 $branch_counter++;
329 }
330 $avg_branch = $avg_branch / $branch_counter;
331  
332 # set the node and leaf counts
333 my $leaf_count = keys %leafs;
334 my $node_count = keys %nodes;
335 my $decendent_count = keys %decendents;
336  
337 my $total_count= $leaf_count + $node_count;
338  
339 # get the average depth
340 my $avg_leaf_depth = 0; my $avg_node_depth = 0;
341 foreach my $c (sort keys %leafs) { $avg_leaf_depth+=$leafs{$c}; }
342 foreach my $c (sort keys %nodes) { $avg_node_depth+=$nodes{$c}; }
343  
344 my $avg_depth = ($avg_leaf_depth + $avg_node_depth) / $total_count;
345 if($leaf_count > 0) {
346 $avg_leaf_depth = $avg_leaf_depth / $leaf_count;
347 }
348 if($node_count > 0) {
349 $avg_node_depth = $avg_node_depth / $node_count;
350 }
351  
352 # get the standard deviation
353 my $avg_leaf_sd = 0; $avg_node_sd = 0;
354 foreach my $c (sort keys %leafs) { $avg_sd += ($leafs{$c}-$avg_depth)**2; }
355 foreach my $c (sort keys %nodes) { $avg_sd += ($nodes{$c}-$avg_depth)**2; }
356  
357 $avg_sd = $avg_sd/$total_count;
358 $avg_sd = sqrt($avg_sd);
359  
360 # get the mean depth of the leafs and nodes
361 # also get the --level information if defined
362 my $leaf_mean = int($leaf_count / 2);
363 my $node_mean = int($node_count / 2);
364  
365 my $leaf_mean_depth = 0;
366 my $node_mean_depth = 0;
367 my $level_leaf_above = 0;
368 my $level_leaf_below = 0;
369 my $level_node_above = 0;
370 my $level_node_below = 0;
371 my $counter = 1;
372  
373 $counter = 1;
374 foreach my $c (sort {$nodes{$b}<=>$nodes{$a}} keys %nodes) {
375 if($counter == $node_mean) {
376 $node_mean_depth = $nodes{$c};
377 }
378 if($nodes{$c} >= $opt_level) { $level_node_above++; }
379 if($nodes{$c} < $opt_level) { $level_node_below++; }
380 $counter++;
381 }
382
383 $subsumers = $leaf_count + $node_count;
384  
385 # print out the information
386 print "max_depth : $max_depth\n";
387 print "avg_depth : $avg_depth\n";
388 print "sd_depth : $avg_sd\n";
389 print "paths_to_root : $paths_to_root\n";
390 print "sources : $sources\n";
391 print "max_branch : $max_branch\n";
392 print "avg_branch : $avg_branch\n";
393 print "leaf_count : $leaf_count\n";
394 print "node_count : $node_count\n";
395 print "decendents : $decendent_count\n";
396 print "avg_leaf_depth : $avg_leaf_depth\n";
397 print "avg_node_depth : $avg_node_depth\n";
398 print "mean_leaf_depth : $leaf_mean_depth\n";
399 print "mean_node_depth : $node_mean_depth\n";
400 print "root : $root\n";
401 if(defined $opt_level) {
402 print "nodes_above_level : $level_node_above\n";
403 print "leafs_above_level : $level_leaf_above\n";
404 print "nodes_below_level : $level_node_below\n";
405 print "leafs_below_level : $level_leaf_below\n";
406 }
407  
408 #########################################################################
409 # Depth First Search (DFS)
410 #########################################################################
411 sub _depthFirstSearch
412 {
413 my $concept = shift;
414 my $d = shift;
415 my $array = shift;
416 local(*F) = shift;
417  
418 # increment the depth
419 $d++;
420  
421 # if the depth option has been set and the depth
422 # is now greater than the set depth just return
423 if(defined $opt_depth) {
424 if($d > $opt_depth) { return; }
425 }
426
427 # set the max depth
428 if($d > $max_depth) { $max_depth = $d; }
429  
430 # if concept is one of the following just return
431 #C1274012|Ambiguous concept (inactive concept)
432 if($concept=~/C1274012/) { return; }
433 #C1274013|Duplicate concept (inactive concept)
434 if($concept=~/C1274013/) { return; }
435 #C1276325|Reason not stated concept (inactive concept)
436 if($concept=~/C1276325/) { return; }
437 #C1274014|Outdated concept (inactive concept)
438 if($concept=~/C1274014/) { return; }
439 #C1274015|Erroneous concept (inactive concept)
440 if($concept=~/C1274015/) { return; }
441 #C1274021|Moved elsewhere (inactive concept)
442 if($concept=~/C1274021/) { return; }
443 #C2733115|limited status concept
444 if($concept=~/C2733115/) { return 1; }
445 #C1443286|
446 if($concept=~/C1443286/) { return; }
447  
448 if($concept=~/C0332280/) { return 1; }
449 if($concept=~/C0458341/) { return 1; }
450 if($concept=~/C0562418/) { return 1; }
451 if($concept=~/C0588390/) { return 1; }
452 if($concept=~/C1264758/) { return 1; }
453 if($concept=~/C1273115/) { return 1; }
454 if($concept=~/C1274012/) { return 1; }
455 if($concept=~/C1274013/) { return 1; }
456 if($concept=~/C1274014/) { return 1; }
457 if($concept=~/C1274015/) { return 1; }
458 if($concept=~/C1274021/) { return 1; }
459 if($concept=~/C1276325/) { return 1; }
460 if($concept=~/C1285556/) { return 1; }
461 if($concept=~/C1285659/) { return 1; }
462 if($concept=~/C1298232/) { return 1; }
463 if($concept=~/C1299995/) { return 1; }
464 if($concept=~/C2733115/) { return 1; }
465 if($concept=~/C3645578/) { return 1; }
466 if($concept=~/C3645611/) { return 1; }
467 if($concept=~/C3645619/) { return 1; }
468
469 # set the new path
470 my @path = @{$array};
471 push @path, $concept;
472
473 my $series = join " ", @path;
474
475 # print information into the file if debugpath option is set
476 if($opt_debugpath) { print F "$concept\t$d\t$series\n"; }
477
478 # increment the number of paths
479 $paths_to_root++;
480  
481 # get all the children
482 my $children = $umls->getChildren($concept);
483  
484
485 my $branches = 0;
486 # search through the children
487 foreach my $child (@{$children}) {
488
489 # check if child cui has already in the path
490 my $flag = 0;
491 foreach my $cui (@path) {
492 if($cui eq $child) {
493 $flag = 1;
494 }
495 }
496  
497 # if it isn't continue on with the depth first search
498 if($flag == 0) {
499 &_depthFirstSearch($child, $d, \@path, *F);
500 $branches++;
501 }
502 }
503
504 # update the branching variables
505 if($branches > 0) {
506 $branch_hash{$concept} = $branches;
507 }
508
509 # get the decendent and capture the max depth
510 if(exists $decendents{$concept}) {
511 if($d > $decendents{$concept}) {
512 $decendents{$concept} = $d;
513 }
514 } else { $decendents{$concept} = $d; }
515
516
517 # set the leaf count and capture the max depth
518 if($branches == 0) {
519 if(exists $leafs{$concept}) {
520 if($d > $leafs{$concept}) {
521 $leafs{$concept} = $d;
522 }
523 } else { $leafs{$concept} = $d; }
524 }
525 else {
526 if(exists $nodes{$concept}) {
527 if($d > $nodes{$concept}) {
528 $nodex{$concept} = $d; }
529 }
530 else { $nodes{$concept} = $d; }
531 }
532 }
533 ##############################################################################
534 # function to output minimal usage notes
535 ##############################################################################
536 sub minimalUsageNotes {
537
538 print "Usage: findDFS.pl CONFIGFILE [OPTIONS]\n";
539 &askHelp();
540 exit;
541 }
542  
543 ##############################################################################
544 # function to output help messages for this program
545 ##############################################################################
546 sub showHelp() {
547  
548
549 print "This is a utility runs a dfs over a specified set of sources\n";
550 print "and relations in the UMLS returning the depth, number of paths\n";
551 print "to the root, branching factor, leaf and node count.\n\n";
552
553 print "Usage: findDFS.pl CONFIGFILE [OPTIONS]\n\n";
554  
555 print "Options:\n\n";
556  
557 print "--debug Sets the debug flag for testing\n\n";
558  
559 print "--username STRING Username required to access mysql\n\n";
560  
561 print "--password STRING Password required to access mysql\n\n";
562  
563 print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n";
564  
565 print "--database STRING Database contain UMLS (DEFAULT: umls)\n\n";
566
567 print "--socket STRING Socket used by mysql (DEFAULT: /tmp.mysql.sock)\n\n";
568  
569 print "--debugpath FILE Stores path information in FILE\n\n";
570
571 print "--depth NUMBER Searches up to the specified depth\n";
572 print " Default searches the complete taxonomy\n\n";
573  
574 print "--level NUMBER Returns the number of CUIs above and \n";
575 print " below this NUMBER.\n\n";
576  
577 print "--root CUI Starts the search at a specified CUI\n";
578 print " Default is the UMLS root\n\n";
579
580 print "--version Prints the version number\n\n";
581
582 print "--help Prints this help message.\n\n";
583 }
584  
585 ##############################################################################
586 # function to output the version number
587 ##############################################################################
588 sub showVersion {
589 print '$Id: findDFS.pl,v 1.26 2014/02/06 13:08:19 btmcinnes Exp $';
590 print "\nCopyright (c) 2008, Ted Pedersen & Bridget McInnes\n";
591 }
592  
593 ##############################################################################
594 # function to output "ask for help" message when user's goofed
595 ##############################################################################
596 sub askHelp {
597 print STDERR "Type findDFS.pl --help for help.\n";
598 }
599