annotate cor.py @ 0:24e01abf9e34 draft default tip

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