script/umls-association.pl untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 238 SCAR 40.3
CC
7
Cov
0%
calculateStat
56.0
minimalUsageNotes
2.0
showHelp
2.0
CRAP 56.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 umls-association.pl This program calculates the assocation between
6 two concepts (or terms)
7  
8 =head1 SYNOPSIS
9  
10 This utility takes two concepts (or terms) and returns their assocation
11 score
12  
13 =head1 USAGE
14  
15 Usage: umls-assocation.pl [OPTIONS] CUI1|TERM CUI2|TERM
16  
17 =head1 INPUT
18  
19 =head2 [CUI1|TERM1] [CUI2|TERM2]
20  
21 The input are two terms or two CUIs associated to concepts in the UMLS.
22  
23 =head1 OPTIONS
24  
25 Optional command line arguements
26  
27 =head2 General Options:
28  
29 Displays the quick summary of program options.
30  
31 =head3 --measure
32  
33 The measure used to calculate the assocation. Default = tscore.
34  
35 The package uses the Text::NSP package to do the calculation.
36 The measure included within this package are:
37
38 1. Dice Coefficient
39 2. Fishers exact test - left sided
40 3. Fishers exact test - right sided
41 4. Fishers twotailed test - right sided
42 5. Jaccard Coefficient
43 6. Log-likelihood ratio
44 7. Mutual Information
45 8. Odds Ratio
46 9. Pointwise Mutual Information
47 10. Phi Coefficient
48 11. Pearson's Chi Squared Test
49 12. Poisson Stirling Measure
50 13. T-score (DEFAULT)
51  
52 =head3 --help
53  
54 Displays the quick summary of program options.
55  
56 =head3 --version
57  
58 Displays the version information.
59  
60 =head2 Input Options:
61  
62 =head3 --infile FILE
63  
64 A file containing pairs of concepts or terms in the following format:
65  
66 term1<>term2
67
68 or
69  
70 cui1<>cui2
71  
72 or
73  
74 cui1<>term2
75  
76 or
77  
78 term1<>cui2
79  
80 Unless the --matrix option is chosen then it is just a list of CUIS:
81 cui1
82 cui2
83 cui3
84 ...
85  
86 =head2 General Database Options:
87  
88 =head3 --username STRING
89  
90 Username is required to access the umls database on mysql
91  
92 =head3 --password STRING
93  
94 Password is required to access the umls database on mysql
95  
96 =head3 --hostname STRING
97  
98 Hostname where mysql is located. DEFAULT: localhost
99  
100 =head3 --socket STRING
101  
102 Socket where the mysql.sock or mysqld.sock is located.
103 DEFAULT: mysql.sock
104  
105 =head2 UMLS::Interface Database Options:
106  
107 =head3 --umlsdatabase STRING
108  
109 Database contain UMLS DEFAULT: umls
110  
111 =head2 UMLS::Association Database Options:
112  
113 =head3 --assocdatabase STRING
114  
115 The UMLS-Association database containing the CUI bigrams and their
116 associated frequency information. DEFAULT: CUI_BIGRAMS
117  
118 =head1 OUTPUT
119  
120 The association between the two concepts (or terms)
121  
122 =head1 SYSTEM REQUIREMENTS
123  
124 =over
125  
126 =item * Perl (version 5.8.5 or better) - http://www.perl.org
127  
128 =item * UMLS::Interface - http://search.cpan.org/dist/UMLS-Interface
129  
130 =item * Text::NSP - http://search.cpan.org/dist/Text-NSP
131  
132 =back
133  
134 =head1 CONTACT US
135
136 If you have any trouble installing and using UMLS-Assocation,
137 please contact us via the users mailing list :
138
139 umls-association@yahoogroups.com
140
141 You can join this group by going to:
142
143 http://tech.groups.yahoo.com/group/umls-assocation/
144
145 You may also contact us directly if you prefer :
146
147 Bridget T. McInnes: btmcinnes at vcu.edu
148  
149 =head1 AUTHOR
150  
151 Bridget T. McInnes, Virginia Commonwealth University
152 Alexander D. McQuilkin, Virginia Commonwealth University
153  
154 =head1 COPYRIGHT
155  
156 Copyright (c) 2015
157  
158 Bridget T. McInnes, Virginia Commonwealth University
159 btmcinnes at vcu.edu
160  
161 This program is free software; you can redistribute it and/or modify it under
162 the terms of the GNU General Public License as published by the Free Software
163 Foundation; either version 2 of the License, or (at your option) any later
164 version.
165  
166 This program is distributed in the hope that it will be useful, but WITHOUT
167 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
168 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
169  
170 You should have received a copy of the GNU General Public License along with
171 this program; if not, write to:
172  
173 The Free Software Foundation, Inc.,
174 59 Temple Place - Suite 330,
175 Boston, MA 02111-1307, USA.
176  
177 =cut
178  
179 ###############################################################################
180  
181 # THE CODE STARTS HERE
182 ###############################################################################
183  
184 # ================================
185 # COMMAND LINE OPTIONS AND USAGE
186 # ================================
187  
188  
189 use UMLS::Interface;
190 use UMLS::Association;
191 use Getopt::Long;
192  
193 eval(GetOptions( "version", "help", "debug", "username=s", "password=s", "hostname=s", "umlsdatabase=s", "assocdatabase=s", "socket=s", "infile=s", "measure=s", "getdescendants", "config=s","precision=s")) or die ("Please check the above mentioned option(s).\n");
194  
195  
196 # if help is defined, print out help
197 if( defined $opt_help ) {
198 $opt_help = 1;
199 &showHelp();
200 exit;
201 }
202  
203 # if version is requested, show version
204 if( defined $opt_version ) {
205 $opt_version = 1;
206 &showVersion();
207 exit;
208 }
209  
210 # At least 1 term should be given on the command line.
211 if(!(defined $opt_infile) && (scalar(@ARGV) < 1) ) {
212 print STDERR "No term was specified on the command line\n";
213 #&minimalUsageNotes();
214 exit;
215 }
216  
217 # set UMLS-Interface options
218 my %umls_option_hash = ();
219  
220 $umls_option_hash{"t"} = 1;
221  
222 if(defined $opt_debug) {
223 $umls_option_hash{"debug"} = $opt_debug;
224 }
225 if(defined $opt_verbose) {
226 $umls_option_hash{"verbose"} = $opt_verbose;
227 }
228 if(defined $opt_username) {
229 $umls_option_hash{"username"} = $opt_username;
230 }
231 if(defined $opt_driver) {
232 $umls_option_hash{"driver"} = $opt_driver;
233 }
234 if(defined $opt_umlsdatabase) {
235 $umls_option_hash{"database"} = $opt_umlsdatabase;
236 }
237 if(defined $opt_password) {
238 $umls_option_hash{"password"} = $opt_password;
239 }
240 if(defined $opt_hostname) {
241 $umls_option_hash{"hostname"} = $opt_hostname;
242 }
243 if(defined $opt_socket) {
244 $umls_option_hash{"socket"} = $opt_socket;
245 }
246 if(defined $opt_config){
247 $umls_option_hash{"config"} = $opt_config;
248 }
249  
250 # instantiate instance of UMLS-Interface
251 my $umls = UMLS::Interface->new(\%umls_option_hash);
252 die "Unable to create UMLS::Interface object.\n" if(!$umls);
253  
254 # set UMLS-Association option hash
255 my %assoc_option_hash = ();
256  
257 if(defined $opt_debug) {
258 $assoc_option_hash{"debug"} = $opt_debug;
259 }
260 if(defined $opt_verbose) {
261 $assoc_option_hash{"verbose"} = $opt_verbose;
262 }
263 if(defined $opt_username) {
264 $assoc_option_hash{"username"} = $opt_username;
265 }
266 if(defined $opt_driver) {
267 $assoc_option_hash{"driver"} = $opt_driver;
268 }
269 if(defined $opt_assocdatabase) {
270 $assoc_option_hash{"database"} = $opt_assocdatabase;
271 }
272 if(defined $opt_password) {
273 $assoc_option_hash{"password"} = $opt_password;
274 }
275 if(defined $opt_hostname) {
276 $assoc_option_hash{"hostname"} = $opt_hostname;
277 }
278 if(defined $opt_socket) {
279 $assoc_option_hash{"socket"} = $opt_socket;
280 }
281 if(defined $opt_getdescendants) {
282 $assoc_option_hash{"getdescendants"} = $opt_getdescendants;
283 }
284 if(defined $opt_precision){
285 $assoc_option_hash{"precision"} = $opt_precision;
286 }
287 $assoc_option_hash{"umls"} = $umls;
288  
289  
290 # instantiate instance of UMLS-Assocation
291 my $mmb = UMLS::Association->new(\%assoc_option_hash);
292 die "Unable to create UMLS::Association object.\n" if(!$mmb);
293  
294 # if --infile option defined calculate assocation over pairs in
295 # the input file
296 if(defined $opt_infile) {
297 open(FILE, $opt_infile) || die "Could not open file ($opt_infile)\n";
298 while(<FILE>) {
299 chomp;
300 my ($i1, $i2) = split/<>/;
301 calculateStat($i1, $i2);
302 }
303 }
304 # otherwise calculate the assocation over the two input terms
305 else {
306 my $input1 = shift; my $input2 = shift;
307 calculateStat($input1, $input2);
308 }
309  
310 sub calculateStat {
311  
312 my $input1 = shift;
313 my $input2 = shift;
314
315 my $term1 = $input1;
316 my $term2 = $input2;
317
318 my $c1 = undef;
319 if($input1=~/C[0-9]+/) {
320 push @{$c1}, $input1;
321 $term1 = $umls->getAllPreferredTerm($input1);
322 }
323 else {
324 $c1 = $umls->getConceptList($input1);
325 }
326
327 my $c2 = undef;
328 if($input2=~/C[0-9]+/) {
329 push @{$c2}, $input2;
330 $term2 = $umls->getAllPreferredTerm($input2);
331 }
332 else {
333 $c2 = $umls->getConceptList($input2);
334 }
335
336 my $measure = "tscore";
337 if(defined $opt_measure) {
338 $measure = $opt_measure;
339
340 if(! ($measure=~/(ll|pmi|tmi|ps|x2|phi|leftFisher|rightFisher|twotailed|dice|jaccard|odds|tscore)/)) {
341 print STDER "That measure is not defined for this program\n";
342 &showHelp();
343 exit;
344 }
345 }
346  
347 my $max = -1.000; my $mc1 = ""; my $mc2 = "";
348 foreach my $cui1 (@{$c1}) {
349 foreach my $cui2 (@{$c2}) {
350 my $stat = $mmb->calculateStatistic($cui1, $cui2, $measure);
351 #my $stat2 = $mmb->calculateStatistic($cui2, $cui1, $measure);
352 # my $stat = $stat1 + $stat2 / 2;
353 if($stat > $max) {
354 $max = $stat; $mc1 = $cui1; $mc2 = $cui2;
355 }
356 }
357 }
358 if($max == 0) { $max = -1; }
359 print "$max<>$term1($mc1)<>$term2($mc2)\n";
360 }
361  
362 ##############################################################################
363 # function to output minimal usage notes
364 ##############################################################################
365 sub minimalUsageNotes {
366
367 print "Usage: umls-association.pl [OPTIONS] [TERM1 TERM2] [CUI1 CUI2]\n";
368 &askHelp();
369 exit;
370 }
371  
372 ##############################################################################
373 # function to output help messages for this program
374 ##############################################################################
375 sub showHelp() {
376
377 print "This is a utility that takes as input either two terms \n";
378 print "or two CUIs from the command line or a file and returns \n";
379 print "their association using one of the following measures: \n";
380  
381 print "1. Dice Coefficient (dice) \n";
382 print "2. Fishers exact test - left sided (left)\n";
383 print "3. Fishers exact test - right sided (right)\n";
384 print "4. Fishers twotailed test - right sided (twotailed)\n";
385 print "5. Jaccard Coefficient (jaccard)\n";
386 print "6. Log-likelihood ratio (ll)\n";
387 print "7. Mutual Information (tmi)\n";
388 print "8. Odds Ratio (oods)\n";
389 print "9. Pointwise Mutual Information (pmi)\n";
390 print "10. Phi Coefficient (phi)\n";
391 print "11. Pearson's Chi Squared Test (chi)\n";
392 print "12. Poisson Stirling Measure (ps)\n";
393 print "13. T-score (tscore) DEFAULT\n\n";
394  
395 print "Usage: umls-assocation.pl [OPTIONS] TERM1 TERM2\n\n";
396  
397 print "General Options:\n\n";
398  
399 print "--measure MEASURE The measure to use to calculate the\n";
400 print " assocation. (DEFAULT: tscore)\n\n";
401  
402 print "--precision N Displays values upto N places of decimal. (DEFAULT: 4)\n\n";
403  
404 print "--version Prints the version number\n\n";
405
406 print "--help Prints this help message.\n\n";
407  
408 print "--getdescendants Calculates the association score taking into account
409 the occurrences of descendants of the specified CUIs\n\n";
410  
411 print "--config FILE Configuration file\n\n";
412  
413 print "\n\nInput Options: \n\n";
414  
415 print "--infile FILE File containing TERM or CUI pairs\n\n";
416  
417 print "\n\nGeneral Database Options:\n\n";
418  
419 print "--username STRING Username required to access mysql\n\n";
420  
421 print "--password STRING Password required to access mysql\n\n";
422  
423 print "--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n";
424 print "--socket STRING Socket for mysql (DEFAULT: /tmp/mysql.sock\n\n";
425  
426 print "\n\nUMLS-Interface Database Options: \n\n";
427  
428 print "--umlsdatabase STRING Database contain UMLS (DEFAULT: umls)\n\n";
429
430 print "\n\nUMLS-Association Database Options: \n\n";
431  
432 print "--assocdatabase STRING Database containing CUI bigrams
433 (DEFAULT: CUI_BIGRAMS)\n\n";
434
435
436  
437 }
438 ##############################################################################
439 # function to output the version number
440 ##############################################################################
441 sub showVersion {
442 print '$Id: umls-assocation.pl,v 0.01 2015/06/24 19:25:05 btmcinnes Exp $';
443 print "\nCopyright (c) 2015, Bridget McInnes\n";
444 }
445  
446 ##############################################################################
447 # function to output "ask for help" message when user's goofed
448 ##############################################################################
449 sub askHelp {
450 print STDERR "Type umls-association.pl --help for help.\n";
451 }
452