annotate column_order_header_sort.py @ 0:6ae9724caf4d draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
author iuc
date Wed, 12 Apr 2017 17:17:18 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
1 #!/usr/bin/env python
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
2
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
3 import subprocess
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
4 import sys
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
5
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
6 AWK_CMD = """BEGIN{FS="%s"; OFS="%s";} {print %s;}"""
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
7
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
8 input_filename = sys.argv[1]
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
9 output_filename = sys.argv[2]
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
10 delimiter = sys.argv[3]
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
11 key_column = sys.argv[4]
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
12
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
13 try:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
14 key_column = int( key_column ) - 1
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
15 except Exception:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
16 key_column = None
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
17
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
18 header = None
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
19 with open( input_filename, 'r' ) as fh:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
20 header = fh.readline().strip( '\r\n' )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
21 header = header.split( delimiter )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
22 assert len( header ) == len( set( header ) ), "Header values must be unique"
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
23 sorted_header = list( header )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
24 if key_column is None:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
25 columns = []
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
26 else:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
27 columns = [ key_column ]
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
28 sorted_header.pop( key_column )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
29 sorted_header.sort()
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
30
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
31 for key in sorted_header:
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
32 columns.append( header.index( key ) )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
33
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
34 awk_cmd = AWK_CMD % ( delimiter, delimiter, ",".join( map( lambda x: "$%i" % ( x + 1 ), columns ) ) )
6ae9724caf4d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_order_header_sort commit d562cc65926c8c95af21467177b253b6ac985cb4
iuc
parents:
diff changeset
35 sys.exit( subprocess.call( [ 'gawk', awk_cmd, input_filename ], stdout=open( output_filename, 'wb+' ), shell=False ) )