annotate scatterplot.py @ 5:635c4cfd83ee draft default tip

Uploaded
author elindfor
date Wed, 15 Jan 2014 04:33:21 -0500
parents ec1cc2cd9c93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
1 #!/usr/bin/env python
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
2 #Greg Von Kuster
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
3
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
4 import sys
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
5 from rpy import *
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
6
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
7 def stop_err(msg):
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
8 sys.stderr.write(msg)
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
9 sys.exit()
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
10
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
11 def main():
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
12
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
13 in_fname = sys.argv[1]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
14 out_fname = sys.argv[2]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
15 try:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
16 columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
17 except:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
18 stop_err( "Columns not specified, your query does not contain a column of numerical data." )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
19 title = sys.argv[5]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
20 xlab = sys.argv[6]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
21 ylab = sys.argv[7]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
22
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
23 matrix = []
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
24 skipped_lines = 0
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
25 first_invalid_line = 0
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
26 invalid_value = ''
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
27 invalid_column = 0
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
28 i = 0
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
29 for i, line in enumerate( file( in_fname ) ):
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
30 valid = True
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
31 line = line.rstrip( '\r\n' )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
32 if line and not line.startswith( '#' ):
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
33 row = []
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
34 fields = line.split( "\t" )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
35 for column in columns:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
36 try:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
37 val = fields[column]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
38 if val.lower() == "na":
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
39 row.append( float( "nan" ) )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
40 else:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
41 row.append( float( fields[column] ) )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
42 except:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
43 valid = False
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
44 skipped_lines += 1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
45 if not first_invalid_line:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
46 first_invalid_line = i + 1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
47 try:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
48 invalid_value = fields[column]
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
49 except:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
50 invalid_value = ''
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
51 invalid_column = column + 1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
52 break
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
53 else:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
54 valid = False
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
55 skipped_lines += 1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
56 if not first_invalid_line:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
57 first_invalid_line = i+1
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
58
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
59 if valid:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
60 matrix.append( row )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
61
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
62 if skipped_lines < i:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
63 try:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
64 r.pdf( out_fname, 8, 8 )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
65 r.plot( array( matrix ), type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
66 r.dev_off()
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
67 except Exception, exc:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
68 stop_err( "%s" %str( exc ) )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
69 else:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
70 stop_err( "All values in both columns %s and %s are non-numeric or empty." % ( sys.argv[3], sys.argv[4] ) )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
71
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
72 print "Scatter plot on columns %s, %s. " % ( sys.argv[3], sys.argv[4] )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
73 if skipped_lines > 0:
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
74 print "Skipped %d lines starting with line #%d, value '%s' in column %d is not numeric." % ( skipped_lines, first_invalid_line, invalid_value, invalid_column )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
75
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
76 r.quit( save="no" )
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
77
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
78 if __name__ == "__main__":
ec1cc2cd9c93 Uploaded
elindfor
parents:
diff changeset
79 main()