Mercurial > repos > iuc > column_order_header_sort
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6ae9724caf4d |
---|---|
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 key_column = sys.argv[4] | |
12 | |
13 try: | |
14 key_column = int( key_column ) - 1 | |
15 except Exception: | |
16 key_column = None | |
17 | |
18 header = None | |
19 with open( input_filename, 'r' ) as fh: | |
20 header = fh.readline().strip( '\r\n' ) | |
21 header = header.split( delimiter ) | |
22 assert len( header ) == len( set( header ) ), "Header values must be unique" | |
23 sorted_header = list( header ) | |
24 if key_column is None: | |
25 columns = [] | |
26 else: | |
27 columns = [ key_column ] | |
28 sorted_header.pop( key_column ) | |
29 sorted_header.sort() | |
30 | |
31 for key in sorted_header: | |
32 columns.append( header.index( key ) ) | |
33 | |
34 awk_cmd = AWK_CMD % ( delimiter, delimiter, ",".join( map( lambda x: "$%i" % ( x + 1 ), columns ) ) ) | |
35 sys.exit( subprocess.call( [ 'gawk', awk_cmd, input_filename ], stdout=open( output_filename, 'wb+' ), shell=False ) ) |