comparison SumTest.py @ 0:d0fbc0d93d05 draft

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