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