Mercurial > repos > mvdbeek > scatterplot_test
comparison scatterplot.py @ 0:11627baf830e draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/scatterplot commit a697f2f6d8562012634b3d63572c358b71ebf823-dirty
author | mvdbeek |
---|---|
date | Wed, 14 Sep 2016 12:40:20 -0400 |
parents | |
children | b4102b163488 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:11627baf830e |
---|---|
1 #!/usr/bin/env python | |
2 # Greg Von Kuster | |
3 | |
4 import sys | |
5 | |
6 from numpy import array | |
7 from rpy import r | |
8 | |
9 | |
10 def stop_err(msg): | |
11 sys.stderr.write(msg) | |
12 sys.exit() | |
13 | |
14 | |
15 def main(): | |
16 | |
17 in_fname = sys.argv[1] | |
18 out_fname = sys.argv[2] | |
19 try: | |
20 columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1 | |
21 except: | |
22 stop_err( "Columns not specified, your query does not contain a column of numerical data." ) | |
23 title = sys.argv[5] | |
24 xlab = sys.argv[6] | |
25 ylab = sys.argv[7] | |
26 | |
27 matrix = [] | |
28 skipped_lines = 0 | |
29 first_invalid_line = 0 | |
30 invalid_value = '' | |
31 invalid_column = 0 | |
32 i = 0 | |
33 for i, line in enumerate( file( in_fname ) ): | |
34 valid = True | |
35 line = line.rstrip( '\r\n' ) | |
36 if line and not line.startswith( '#' ): | |
37 row = [] | |
38 fields = line.split( "\t" ) | |
39 for column in columns: | |
40 try: | |
41 val = fields[column] | |
42 if val.lower() == "na": | |
43 row.append( float( "nan" ) ) | |
44 else: | |
45 row.append( float( fields[column] ) ) | |
46 except: | |
47 valid = False | |
48 skipped_lines += 1 | |
49 if not first_invalid_line: | |
50 first_invalid_line = i + 1 | |
51 try: | |
52 invalid_value = fields[column] | |
53 except: | |
54 invalid_value = '' | |
55 invalid_column = column + 1 | |
56 break | |
57 else: | |
58 valid = False | |
59 skipped_lines += 1 | |
60 if not first_invalid_line: | |
61 first_invalid_line = i + 1 | |
62 | |
63 if valid: | |
64 matrix.append( row ) | |
65 | |
66 if skipped_lines < i: | |
67 try: | |
68 r.pdf( out_fname, 8, 8 ) | |
69 r.plot( array( matrix ), type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 ) | |
70 r.dev_off() | |
71 except Exception, exc: | |
72 stop_err( "%s" % str( exc ) ) | |
73 else: | |
74 stop_err( "All values in both columns %s and %s are non-numeric or empty." % ( sys.argv[3], sys.argv[4] ) ) | |
75 | |
76 print "Scatter plot on columns %s, %s. " % ( sys.argv[3], sys.argv[4] ) | |
77 if skipped_lines > 0: | |
78 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 ) | |
79 | |
80 r.quit( save="no" ) | |
81 | |
82 if __name__ == "__main__": | |
83 main() |