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