annotate tools/stats/filtering.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 # This tool takes a tab-delimited text file as input and creates filters on columns based on certain properties.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # The tool will skip over invalid lines within the file, informing the user about the number of lines skipped.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from __future__ import division
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import sys, re, os.path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # Older py compatibility
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 set()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 from sets import Set as set
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def get_operands( filter_condition ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 # Note that the order of all_operators is important
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 items_to_strip = ['+', '-', '**', '*', '//', '/', '%', '<<', '>>', '&', '|', '^', '~', '<=', '<', '>=', '>', '==', '!=', '<>', ' and ', ' or ', ' not ', ' is ', ' is not ', ' in ', ' not in ']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 for item in items_to_strip:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if filter_condition.find( item ) >= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 filter_condition = filter_condition.replace( item, ' ' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 operands = set( filter_condition.split( ' ' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 return operands
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 in_fname = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 out_fname = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 cond_text = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 in_columns = int( sys.argv[4] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 assert sys.argv[5] #check to see that the column types variable isn't null
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 in_column_types = sys.argv[5].split( ',' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 stop_err( "Data does not appear to be tabular. This tool can only be used with tab-delimited data." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 # Unescape if input has been escaped
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 mapped_str = {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 '__lt__': '<',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 '__le__': '<=',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 '__eq__': '==',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 '__ne__': '!=',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 '__gt__': '>',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 '__ge__': '>=',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 '__sq__': '\'',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 '__dq__': '"',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 for key, value in mapped_str.items():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 cond_text = cond_text.replace( key, value )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 # Attempt to determine if the condition includes executable stuff and, if so, exit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 secured = dir()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 operands = get_operands(cond_text)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 for operand in operands:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 check = int( operand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if operand in secured:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 stop_err( "Illegal value '%s' in condition '%s'" % ( operand, cond_text ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 # Work out which columns are used in the filter (save using 1 based counting)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 used_cols = sorted(set(int(match.group()[1:]) \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 for match in re.finditer('c(\d)+', cond_text)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 largest_col_index = max(used_cols)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 # Prepare the column variable names and wrappers for column data types. Only
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 # cast columns used in the filter.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 cols, type_casts = [], []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 for col in range( 1, largest_col_index + 1 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 col_name = "c%d" % col
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 cols.append( col_name )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 col_type = in_column_types[ col - 1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if col in used_cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 type_cast = "%s(%s)" % ( col_type, col_name )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 #If we don't use this column, don't cast it.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 #Otherwise we get errors on things like optional integer columns.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 type_cast = col_name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 type_casts.append( type_cast )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 col_str = ', '.join( cols ) # 'c1, c2, c3, c4'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 type_cast_str = ', '.join( type_casts ) # 'str(c1), int(c2), int(c3), str(c4)'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 assign = "%s, = line.split( '\\t' )[:%i]" % ( col_str, largest_col_index )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 wrap = "%s = %s" % ( col_str, type_cast_str )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 invalid_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 invalid_line = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 lines_kept = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 total_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 out = open( out_fname, 'wt' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 # Read and filter input file, skipping invalid lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 code = '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 for i, line in enumerate( file( in_fname ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 total_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 line = line.rstrip( '\\r\\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 if not line or line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 %s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 %s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 if %s:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 lines_kept += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 print >> out, line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 invalid_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 if not invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 first_invalid_line = i + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 invalid_line = line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 ''' % ( assign, wrap, cond_text )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 valid_filter = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 exec code
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 if str( e ).startswith( 'invalid syntax' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 valid_filter = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 stop_err( 'Filter condition "%s" likely invalid. See tool tips, syntax and examples.' % cond_text )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 stop_err( str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 if valid_filter:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 valid_lines = total_lines - skipped_lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 print 'Filtering with %s, ' % cond_text
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if valid_lines > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 print 'kept %4.2f%% of %d valid lines (%d total lines).' % ( 100.0*lines_kept/valid_lines, valid_lines, total_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 print 'Possible invalid filter condition "%s" or non-existent column referenced. See tool tips, syntax and examples.' % cond_text
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 if invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 print 'Skipped %d invalid line(s) starting at line #%d: "%s"' % ( invalid_lines, first_invalid_line, invalid_line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 if skipped_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 print 'Skipped %i comment (starting with #) or blank line(s)' % skipped_lines