blib/script/spearmans.pl untested

filter: Statementstmt 0.0% 0 / 182 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 233 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 =head1 NAME
3
4 spearmans.pl - This program calculates Spearman's Rank Correlation
5 between two datasets.
6
7 =head1 SYNOPSIS
8
9 This utility takes two files in the format of umls-similarity.pl output
10 and calculates the Spearman's Rank Correlation.
11
12 =head1 USAGE
13
14 Usage: spearmans.pl [OPTIONS] FILE1 FILE2
15
16 =head1 INPUT
17
18 FILE1 FILE2
19
20 Two files in the following format:
21
22 score<>CUI1<>CUI2
23 score<>CUI3<>CUI4
24 ect ...
25
26 The output format of umls-similarity.pl will work here because it
27 extracts the CUIs from the paranthesis, so no worries. If you are
28 not using the CUI format or a umls-similarity.pl output file though,
29 use the --word option described below.
30
31 =head1 Optional Arguments:
32
33 Displays the quick summary of program options.
34
35 =head2 --word
36
37 The format of the input files contains words rather than CUIs and/or
38 is not a umls-similarity.pl output file.
39
40 =head2 --N
41
42 displays N - the number of term pairs the correlation
43 is being calculated over. This would be any term pair
44 that has a score greater than or equal to zero.
45
46 =head2 --precision NUMBER
47
48 Displays values up to NUMBER places of decimal. Default is 4.
49
50 =head2 --version
51
52 Displays the version information.
53
54 =head1 OUTPUT
55
56 The Spearman's Rank Correlation between the two files.
57
58 =head1 SYSTEM REQUIREMENTS
59
60 =over
61
62 =item * Perl (version 5.8.5 or better) - http://www.perl.org
63
64 =item * UMLS::Interface - http://search.cpan.org/dist/UMLS-Interface
65
66 =item * UMLS::Similarity - http://search.cpan.org/dist/UMLS-Similarity
67
68 =back
69
70 =head1 CONTACT US
71
72 If you have any trouble installing and using UMLS-Similarity,
73 please contact us via the users mailing list :
74
75 umls-similarity@yahoogroups.com
76
77 You can join this group by going to:
78
79 http://tech.groups.yahoo.com/group/umls-similarity/
80
81 You may also contact us directly if you prefer :
82
83 Bridget T. McInnes: bthomson at cs.umn.edu
84
85 Ted Pedersen : tpederse at d.umn.edu
86
87 =head1 AUTHOR
88
89 Bridget T. McInnes, University of Minnesota
90
91 =head1 COPYRIGHT
92
93 Copyright (c) 2007-2011,
94
95 Bridget T. McInnes, University of Minnesota
96 bthomson at cs.umn.edu
97
98 Ted Pedersen, University of Minnesota Duluth
99 tpederse at d.umn.edu
100
101
102 Siddharth Patwardhan, University of Utah, Salt Lake City
103 sidd@cs.utah.edu
104
105 Serguei Pakhomov, University of Minnesota Twin Cities
106 pakh0002@umn.edu
107
108 This program is free software; you can redistribute it and/or modify it under
109 the terms of the GNU General Public License as published by the Free Software
110 Foundation; either version 2 of the License, or (at your option) any later
111 version.
112
113 This program is distributed in the hope that it will be useful, but WITHOUT
114 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
115 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
116
117 You should have received a copy of the GNU General Public License along with
118 this program; if not, write to:
119
120 The Free Software Foundation, Inc.,
121 59 Temple Place - Suite 330,
122 Boston, MA 02111-1307, USA.
123
124 =cut
125
126 ###############################################################################
127
128 # THE CODE STARTS HERE
129 ###############################################################################
130
131 # ================================
132 # COMMAND LINE OPTIONS AND USAGE
133 # ================================
134
135 use Getopt::Long;
136
137 eval(GetOptions( "version", "help", "word", "N", "precision=s", "t")) or die ("Please check the above mentioned option(s).\n");
138
139 # if help is defined, print out help
140 if( defined $opt_help ) {
141 $opt_help = 1;
142 &showHelp();
143 exit;
144 }
145
146 # if version is requested, show version
147 if( defined $opt_version ) {
148 $opt_version = 1;
149 &showVersion();
150 exit;
151 }
152
153 # check for precision
154 my $precision = 4;
155 if(defined $opt_precision) {
156 if ($opt_precision !~ /^\d+$/) {
157 print STDERR "Value for switch --precision should be integer >= 0\n";
158 &minimalUsageNotes();
159 exit;
160 }
161 $precision = $opt_precision;
162 }
163 my $floatformat = join '', '%', '.', $precision, 'f';
164
165 # At least 2 terms should be given on the command line.
166 if( scalar(@ARGV) < 2 ) {
167 print STDERR "At least 2 terms or CUIs should be given on the \n";
168 print STDERR "command line.\n\n";
169 &minimalUsageNotes();
170 exit;
171 }
172
173 # initialize variables
174 my $xfile = shift;
175 my $yfile = shift;
176
177 open(X, $xfile) || die "Could not open file: $xfile\n";
178 open(Y, $yfile) || die "Could not open file: $yfile\n";
179
180
181 my $ymean = 0;
182 my $xmean = 0;
183
184 my $ycount = 0;
185 my $xcount = 0;
186
187 my %xhash = ();
188 my %yhash = ();
189
190 my %xlist = ();
191 my %ylist = ();
192
193 my $xnegative = 0;
194 my $ynegative = 0;
195
196 my $xtotal = 0;
197 my $ytotal = 0;
198
199 while(<X>) {
200 chomp;
201 my ($score, $t1, $t2) = split/<>/;
202
203 my $c1 = ""; my $c2 = "";
204 if(defined $opt_word) {
205 $c1 = $t1; $c2 = $t2;
206 }
207 else {
208 $t1=~/(C[0-9]+)/;
209 $c1 = $1;
210
211 $t2=~/(C[0-9]+)/;
212 $c2 = $1;
213 }
214
215
216 if($score == -1.0000) { $xnegative++; next; }
217
218 $xtotal++;
219
220 $term = "$c1 $c2";
221 push @{$xhash{$score}}, $term;
222 $xlist{$term}++;
223 }
224
225 while(<Y>) {
226 chomp;
227 my ($score, $t1, $t2) = split/<>/;
228
229
230 my $c1 = ""; my $c2 = "";
231 if(defined $opt_word) {
232 $t1=~s/\(C[0-9]+\)//g;
233 $t2=~s/\(C[0-9]+\)//g;
234 $c1 = $t1; $c2 = $t2;
235 }
236 else {
237 $t1=~/(C[0-9]+)/;
238 $c1 = $1;
239
240 $t2=~/(C[0-9]+)/;
241 $c2 = $1;
242 }
243
244
245 if($score == -1.0000) { $ynegative++; next; }
246
247 $ytotal++;
248
249 $term = "$c1 $c2";
250 push @{$yhash{$score}}, $term;
251 $ylist{$term}++;
252 }
253
254 my %xrank = ();
255 my %yrank = ();
256
257 my $rank = 1;
258 foreach my $score (sort {$b<=>$a} keys %xhash) {
259
260 my $count = 0;
261 my $computed_rank = 0; my $crank = $rank + 1;
262 foreach my $term (@{$xhash{$score}}) {
263 if(exists $ylist{$term}) {
264 $computed_rank += $crank;
265 $count++; $crank++;
266 }
267 }
268
269 if($count == 0) { next; }
270
271 $computed_rank = $computed_rank / $count;
272
273 foreach my $term (@{$xhash{$score}}) {
274 if(! (exists $ylist{$term})) { next; }
275 $xrank{$term} = $computed_rank;
276 $xmean += $computed_rank;
277 $xcount++;
278 $rank++;
279 }
280 }
281
282 $rank = 0;
283 foreach my $score (sort {$b<=>$a} keys %yhash) {
284
285 my $count = 0;
286 my $computed_rank = 0; my $crank = $rank + 1;
287 foreach my $term (@{$yhash{$score}}) {
288 if(exists $xlist{$term}) {
289 $computed_rank += $crank;
290 $count++; $crank++;
291 }
292 }
293
294 if($count == 0) { next; }
295
296 $computed_rank = $computed_rank / $count;
297
298 foreach my $term (@{$yhash{$score}}) {
299 if(! (exists $xlist{$term})) { next; }
300 $yrank{$term} = $computed_rank;
301 $ymean += $computed_rank;
302 $ycount++;
303 $rank++;
304 }
305 }
306
307 print STDERR "$xcount : $ycount\n";
308
309 $xmean = $xmean/$xcount;
310 $ymean = $ymean/$ycount;
311
312 my $numerator = 0;
313 my $xdenom = 0;
314 my $ydenom = 0;
315 foreach my $term (sort keys %xrank) {
316 my $xi = $xrank{$term};
317 my $yi = $yrank{$term};
318
319 $numerator += ( ($xi-$xmean) * ($yi-$ymean) );
320
321 $xdenom += ( ($xi - $xmean)**2 );
322 $ydenom += ( ($yi - $ymean)**2 );
323 }
324
325 my $denominator = sqrt($xdenom * $ydenom);
326
327
328 if($denominator <= 0) {
329 print STDERR "Correlation can not be calculated.\n";
330 print STDERR "Files do not contain similar ngrams.\n";
331 exit;
332 }
333
334 my $pearsons = $numerator / $denominator;
335
336 my $score = sprintf $floatformat, $pearsons;
337
338 # calculate N
339 my $yN= $ytotal - $ynegative;
340 my $xN= $xtotal - $xnegative;
341 my $N = $yN;
342 if($xN < $yN){ $N = $xN; }
343
344 # calculate tscore
345 #my $t = $score/(sqrt( (1-$score**2)/($N-2) ));
346
347 # calculate z-score
348 #my $Fr = .5 * log((1+$score)/(1-$score));
349 #my $z = sqrt(($N-3)/1.06) * $Fr;
350
351 print "Spearman's Rank Correlation: $score ";
352 if(defined $opt_N) { print "(N: $N) "; }
353 #if(defined $opt_t) { print "(t: $t) "; }
354 print "\n";
355
356 ##############################################################################
357 # function to output minimal usage notes
358 ##############################################################################
359 sub minimalUsageNotes {
360
361 print "Usage: spearmans.pl [OPTIONS] FILE1 FILE2\n";
362 &askHelp();
363 exit;
364 }
365
366 ##############################################################################
367 # function to output help messages for this program
368 ##############################################################################
369 sub showHelp() {
370
371 print "This is a utility that takes as input two files and returns\n";
372 print "the spearman's rank correlation between the two datsets.\n\n";
373
374 print "Usage: spearmans.pl [OPTIONS] FILE1 FILE2\n\n";
375
376 print "Options:\n\n";
377
378 print "--precision NUMBER Displays values upto NUMBER places of \n";
379 print " decimal.\n\n";
380
381 print "--N Prints the total number of term\n";
382 print " pairs the correlation metric is\n";
383 print " using.\n\n";
384
385 print "--word The format of the input files contains words \n";
386 print " rather than CUIs or is not a umls-similarity.pl \n";
387 print " output file. \n\n";
388
389 print "--version Prints the version number\n\n";
390
391 print "--help Prints this help message.\n\n";
392 }
393
394 ##############################################################################
395 # function to output the version number
396 ##############################################################################
397 sub showVersion {
398 print '$Id: spearmans.pl,v 1.13 2015/06/23 13:33:06 btmcinnes Exp $';
399 print "\nCopyright (c) 2009-2011, Ted Pedersen & Bridget McInnes\n";
400 }
401
402 ##############################################################################
403 # function to output "ask for help" message when user's goofed
404 ##############################################################################
405 sub askHelp {
406 print STDERR "Type spearmans.pl --help for help.\n";
407 }
408