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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
16
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
17 def stop_err(msg):
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
18 sys.stderr.write(msg)
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
19 sys.exit()
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
22 def main():
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
23
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
24 in_fname = sys.argv[1]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
25 out_fname = sys.argv[2]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
26 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
27 columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
28 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
29 stop_err( "Columns not specified, your query does not contain a column of numerical data." )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
30 title = sys.argv[5]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
31 xlab = sys.argv[6]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
32 ylab = sys.argv[7]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
33
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
34 matrix = []
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
35 skipped_lines = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
36 first_invalid_line = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
37 invalid_value = ''
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
38 invalid_column = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
41 valid = True
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
44 row = []
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
45 fields = line.split( "\t" )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
46 for column in columns:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
47 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
50 row.append( float( "nan" ) )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
51 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
52 row.append( float( fields[column] ) )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
53 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
54 valid = False
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
55 skipped_lines += 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
56 if not first_invalid_line:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
57 first_invalid_line = i + 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
58 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
59 invalid_value = fields[column]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
60 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
61 invalid_value = ''
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
62 invalid_column = column + 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
63 break
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
64 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
65 valid = False
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
66 skipped_lines += 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
69
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
70 if valid:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
71 matrix.append( row )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
72
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
73 if skipped_lines < i:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
81 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
82 stop_err( "All values in both columns %s and %s are non-numeric or empty." % ( sys.argv[3], sys.argv[4] ) )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
87
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
88 if __name__ == "__main__":
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
89 main()