annotate scatterplot.py @ 0:c12b0759203b draft

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