annotate column_maker.py @ 0:4dadd821722f draft

Imported from capsule None
author devteam
date Mon, 28 Jul 2014 11:30:22 -0400
parents
children 292c605c2dc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
2 # This tool takes a tab-delimited textfile as input and creates another column in the file which is the result of
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
3 # a computation performed on every row in the original file. The tool will skip over invalid lines within the file,
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
4 # informing the user about the number of lines skipped.
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
5
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
6 import sys, re
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
7 # These functions may be used in compute expression:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
8 from math import log,exp,sqrt,ceil,floor
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
9
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
10
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
11 assert sys.version_info[:2] >= ( 2, 4 )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
12
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
13 def stop_err( msg ):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
14 sys.stderr.write( msg )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
15 sys.exit()
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
16
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
17 inp_file = sys.argv[1]
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
18 out_file = sys.argv[2]
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
19 expr = sys.argv[3]
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
20 round_result = sys.argv[4]
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
21 try:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
22 in_columns = int( sys.argv[5] )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
23 except:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
24 stop_err( "Missing or invalid 'columns' metadata value, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data." )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
25 if in_columns < 2:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
26 # To be considered tabular, data must fulfill requirements of the sniff.is_column_based() method.
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
27 stop_err( "Missing or invalid 'columns' metadata value, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data." )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
28 try:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
29 in_column_types = sys.argv[6].split( ',' )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
30 except:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
31 stop_err( "Missing or invalid 'column_types' metadata value, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data." )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
32 if len( in_column_types ) != in_columns:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
33 stop_err( "The 'columns' metadata setting does not conform to the 'column_types' metadata setting, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data." )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
34
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
35 # Unescape if input has been escaped
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
36 mapped_str = {
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
37 '__lt__': '<',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
38 '__le__': '<=',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
39 '__eq__': '==',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
40 '__ne__': '!=',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
41 '__gt__': '>',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
42 '__ge__': '>=',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
43 '__sq__': '\'',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
44 '__dq__': '"',
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
45 }
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
46 for key, value in mapped_str.items():
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
47 expr = expr.replace( key, value )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
48
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
49 operators = 'is|not|or|and'
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
50 builtin_and_math_functions = 'abs|all|any|bin|chr|cmp|complex|divmod|float|hex|int|len|long|max|min|oct|ord|pow|range|reversed|round|sorted|str|sum|type|unichr|unicode|log|exp|sqrt|ceil|floor'
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
51 string_and_list_methods = [ name for name in dir('') + dir([]) if not name.startswith('_') ]
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
52 whitelist = "^([c0-9\+\-\*\/\(\)\.\'\"><=,:! ]|%s|%s|%s)*$" % (operators, builtin_and_math_functions, '|'.join(string_and_list_methods))
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
53 if not re.compile(whitelist).match(expr):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
54 stop_err("Invalid expression")
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
55
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
56 # Prepare the column variable names and wrappers for column data types
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
57 cols, type_casts = [], []
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
58 for col in range( 1, in_columns + 1 ):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
59 col_name = "c%d" % col
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
60 cols.append( col_name )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
61 col_type = in_column_types[ col - 1 ].strip()
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
62 if round_result == 'no' and col_type == 'int':
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
63 col_type = 'float'
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
64 type_cast = "%s(%s)" % ( col_type, col_name )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
65 type_casts.append( type_cast )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
66
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
67 col_str = ', '.join( cols ) # 'c1, c2, c3, c4'
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
68 type_cast_str = ', '.join( type_casts ) # 'str(c1), int(c2), int(c3), str(c4)'
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
69 assign = "%s = line.split( '\\t' )" % col_str
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
70 wrap = "%s = %s" % ( col_str, type_cast_str )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
71 skipped_lines = 0
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
72 first_invalid_line = 0
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
73 invalid_line = None
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
74 lines_kept = 0
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
75 total_lines = 0
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
76 out = open( out_file, 'wt' )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
77
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
78 # Read input file, skipping invalid lines, and perform computation that will result in a new column
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
79 code = '''
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
80 for i, line in enumerate( file( inp_file ) ):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
81 total_lines += 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
82 line = line.rstrip( '\\r\\n' )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
83 if not line or line.startswith( '#' ):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
84 skipped_lines += 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
85 if not invalid_line:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
86 first_invalid_line = i + 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
87 invalid_line = line
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
88 continue
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
89 try:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
90 %s
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
91 %s
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
92 new_val = %s
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
93 if round_result == "yes":
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
94 new_val = int( round( new_val ) )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
95 new_line = line + '\\t' + str( new_val )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
96 print >> out, new_line
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
97 lines_kept += 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
98 except:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
99 skipped_lines += 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
100 if not invalid_line:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
101 first_invalid_line = i + 1
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
102 invalid_line = line
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
103 ''' % ( assign, wrap, expr )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
104
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
105 valid_expr = True
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
106 try:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
107 exec code
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
108 except Exception, e:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
109 out.close()
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
110 if str( e ).startswith( 'invalid syntax' ):
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
111 valid_expr = False
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
112 stop_err( 'Expression "%s" likely invalid. See tool tips, syntax and examples.' % expr )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
113 else:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
114 stop_err( str( e ) )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
115
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
116 if valid_expr:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
117 out.close()
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
118 valid_lines = total_lines - skipped_lines
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
119 print 'Creating column %d with expression %s' % ( in_columns + 1, expr )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
120 if valid_lines > 0:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
121 print 'kept %4.2f%% of %d lines.' % ( 100.0*lines_kept/valid_lines, total_lines )
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
122 else:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
123 print 'Possible invalid expression "%s" or non-existent column referenced. See tool tips, syntax and examples.' % expr
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
124 if skipped_lines > 0:
4dadd821722f Imported from capsule None
devteam
parents:
diff changeset
125 print 'Skipped %d invalid lines starting at line #%d: "%s"' % ( skipped_lines, first_invalid_line, invalid_line )