| 
80
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 #Greg Von Kuster
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 import sys
 | 
| 
 | 
     5 #from rpy import *
 | 
| 
 | 
     6 import rpy2.robjects as robjects
 | 
| 
 | 
     7 from rpy2.robjects.packages import importr
 | 
| 
 | 
     8 r = robjects.r
 | 
| 
 | 
     9 grdevices = importr('grDevices')
 | 
| 
 | 
    10 
 | 
| 
 | 
    11 def stop_err(msg):
 | 
| 
 | 
    12     sys.stderr.write(msg)
 | 
| 
 | 
    13     sys.exit()
 | 
| 
 | 
    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             matrix += row
 | 
| 
 | 
    66 
 | 
| 
 | 
    67     if skipped_lines < i:
 | 
| 
 | 
    68         try:
 | 
| 
 | 
    69             fv = robjects.FloatVector(matrix)
 | 
| 
 | 
    70             m = r['matrix'](fv, ncol=len(columns),byrow=True)
 | 
| 
 | 
    71             r.pdf( out_fname, 8, 8 )
 | 
| 
 | 
    72             #r.plot( array( matrix ), type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 )
 | 
| 
 | 
    73             r.plot( m, type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 )
 | 
| 
 | 
    74             #r.dev_off()
 | 
| 
 | 
    75             grdevices.dev_off()
 | 
| 
 | 
    76         except Exception, exc:
 | 
| 
 | 
    77             stop_err( "%s" %str( exc ) )
 | 
| 
 | 
    78     else:
 | 
| 
 | 
    79         stop_err( "All values in both columns %s and %s are non-numeric or empty." % ( sys.argv[3], sys.argv[4] ) )
 | 
| 
 | 
    80 
 | 
| 
 | 
    81     print "Scatter plot on columns %s, %s. " % ( sys.argv[3], sys.argv[4] )
 | 
| 
 | 
    82     if skipped_lines > 0:
 | 
| 
 | 
    83         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 )
 | 
| 
 | 
    84 
 | 
| 
 | 
    85     #r.quit( save="no" )
 | 
| 
 | 
    86 
 | 
| 
 | 
    87 if __name__ == "__main__":
 | 
| 
 | 
    88     main()
 |