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
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
14
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
15 def stop_err(msg):
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
16 sys.stderr.write(msg)
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
17 sys.exit()
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
20 def main():
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
21
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
22 in_fname = sys.argv[1]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
23 out_fname = sys.argv[2]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
24 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
25 columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
26 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
27 stop_err( "Columns not specified, your query does not contain a column of numerical data." )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
28 title = sys.argv[5]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
29 xlab = sys.argv[6]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
30 ylab = sys.argv[7]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
31
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
32 matrix = []
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
33 skipped_lines = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
34 first_invalid_line = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
35 invalid_value = ''
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
36 invalid_column = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
37 i = 0
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
38 for i, line in enumerate( file( in_fname ) ):
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
39 valid = True
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
42 row = []
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
43 fields = line.split( "\t" )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
44 for column in columns:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
45 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
48 row.append( float( "nan" ) )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
49 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
50 row.append( float( fields[column] ) )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
51 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
52 valid = False
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
53 skipped_lines += 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
54 if not first_invalid_line:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
55 first_invalid_line = i + 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
56 try:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
57 invalid_value = fields[column]
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
58 except:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
59 invalid_value = ''
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
60 invalid_column = column + 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
61 break
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
62 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
63 valid = False
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
64 skipped_lines += 1
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
67
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
68 if valid:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
69 matrix.append( row )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
70
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
71 if skipped_lines < i:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
76 r.dev_off()
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
79 else:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
80 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
81
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
82 print "Scatter plot on columns %s, %s. " % ( sys.argv[3], sys.argv[4] )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
83 if skipped_lines > 0:
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
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 )
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
85
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
86 if __name__ == "__main__":
c12b0759203b Imported from capsule None
devteam
parents:
diff changeset
87 main()