annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
1 #!/usr/bin/env python
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
2
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
3 import subprocess
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
4 import sys
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
5
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
6 AWK_CMD = """BEGIN{FS="%s"; OFS="%s";} {print %s;}"""
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
7
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
8 input_filename = sys.argv[1]
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
9 output_filename = sys.argv[2]
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
10 delimiter = sys.argv[3]
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
11 keep_columns = sys.argv[4]
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
12 strip_characters = sys.argv[5]
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
13
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
14 if keep_columns == "--keep":
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
15 keep_columns = True
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
16 else:
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
17 keep_columns = False
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
18
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
19 names = []
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
20 for name in sys.argv[6:]:
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
21 names.append( name )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
22
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
23 header = None
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
24 with open( input_filename, 'r' ) as fh:
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
25 header = fh.readline().strip( '\r\n' )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
26 header = header.split( delimiter )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
27 columns = []
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
28 for i, key in enumerate( header, 1 ):
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
29 if i == 1 and strip_characters:
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
30 key = key.lstrip( strip_characters )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
31 if ( keep_columns and key in names ) or ( not keep_columns and key not in names ):
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
32 columns.append( i )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
33 print( "Kept", len( columns ), "of", len( header ), "columns." )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
34 awk_cmd = AWK_CMD % ( delimiter, delimiter, ",".join( map( lambda x: "$%s" % x, columns ) ) )
372967836e98 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_remove_by_header commit 2150a3264364471090b650bdffde9f9c0b47ac39
iuc
parents:
diff changeset
35 sys.exit( subprocess.call( [ 'gawk', awk_cmd, input_filename ], stdout=open( output_filename, 'wb+' ), shell=False ) )