annotate SumTest.py @ 0:d0fbc0d93d05 draft

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