annotate tools/stats/cor.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Greg Von Kuster
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Calculate correlations between numeric columns in a tab delim file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 usage: %prog infile output.txt columns method
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 method = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 assert method in ( "pearson", "kendall", "spearman" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 columns = map( int, sys.argv[3].split( ',' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 stop_err( "Problem determining columns, perhaps your query does not contain a column of numerical data." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 matrix = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 invalid_value = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 invalid_column = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 for i, line in enumerate( file( sys.argv[1] ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 valid = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 line = line.rstrip('\n\r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 # Extract values and convert to floats
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 row = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 for column in columns:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 column -= 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 fields = line.split( "\t" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 if len( fields ) <= column:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 val = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if val.lower() == "na":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 row.append( float( "nan" ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 row.append( float( fields[column] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 invalid_value = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 invalid_column = column+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if valid:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 matrix.append( row )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 if skipped_lines < i:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 out = open( sys.argv[2], "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 stop_err( "Unable to open output file" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 # Run correlation
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 value = r.cor( array( matrix ), use="pairwise.complete.obs", method=method )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 except Exception, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 stop_err("%s" %str( exc ))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 for row in value:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 print >> out, "\t".join( map( str, row ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 if skipped_lines > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 msg = "..Skipped %d lines starting with line #%d. " %( skipped_lines, first_invalid_line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if invalid_value and invalid_column > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 msg += "Value '%s' in column %d is not numeric." % ( invalid_value, invalid_column )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 print msg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 main()