annotate tools/plotting/scatterplot.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 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 in_fname = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 out_fname = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 stop_err( "Columns not specified, your query does not contain a column of numerical data." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 title = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 xlab = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 ylab = sys.argv[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 matrix = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 invalid_value = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 invalid_column = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 for i, line in enumerate( file( in_fname ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 valid = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 row = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 fields = line.split( "\t" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for column in columns:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 val = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if val.lower() == "na":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 row.append( float( "nan" ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 row.append( float( fields[column] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 first_invalid_line = i + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 invalid_value = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 invalid_value = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 invalid_column = column + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 if valid:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 matrix.append( row )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if skipped_lines < i:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 r.pdf( out_fname, 8, 8 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 r.plot( array( matrix ), type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 r.dev_off()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 except Exception, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 stop_err( "%s" %str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 else:
9071e359b9a3 Uploaded
xuebing
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] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 print "Scatter plot on columns %s, %s. " % ( sys.argv[3], sys.argv[4] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if skipped_lines > 0:
9071e359b9a3 Uploaded
xuebing
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 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 r.quit( save="no" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 main()