Mercurial > repos > iuc > column_remove_by_header
comparison column_remove_by_header.py @ 0:372967836e98 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
author | iuc |
---|---|
date | Wed, 12 Apr 2017 17:17:29 -0400 |
parents | |
children | 2040e4c2750a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:372967836e98 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import subprocess | |
4 import sys | |
5 | |
6 AWK_CMD = """BEGIN{FS="%s"; OFS="%s";} {print %s;}""" | |
7 | |
8 input_filename = sys.argv[1] | |
9 output_filename = sys.argv[2] | |
10 delimiter = sys.argv[3] | |
11 keep_columns = sys.argv[4] | |
12 strip_characters = sys.argv[5] | |
13 | |
14 if keep_columns == "--keep": | |
15 keep_columns = True | |
16 else: | |
17 keep_columns = False | |
18 | |
19 names = [] | |
20 for name in sys.argv[6:]: | |
21 names.append( name ) | |
22 | |
23 header = None | |
24 with open( input_filename, 'r' ) as fh: | |
25 header = fh.readline().strip( '\r\n' ) | |
26 header = header.split( delimiter ) | |
27 columns = [] | |
28 for i, key in enumerate( header, 1 ): | |
29 if i == 1 and strip_characters: | |
30 key = key.lstrip( strip_characters ) | |
31 if ( keep_columns and key in names ) or ( not keep_columns and key not in names ): | |
32 columns.append( i ) | |
33 print( "Kept", len( columns ), "of", len( header ), "columns." ) | |
34 awk_cmd = AWK_CMD % ( delimiter, delimiter, ",".join( map( lambda x: "$%s" % x, columns ) ) ) | |
35 sys.exit( subprocess.call( [ 'gawk', awk_cmd, input_filename ], stdout=open( output_filename, 'wb+' ), shell=False ) ) |