Mercurial > repos > bgruening > column_arrange_by_header
comparison column_arrange.py @ 1:6c6d26ff01ff draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
author | bgruening |
---|---|
date | Fri, 15 Feb 2019 07:45:03 -0500 |
parents | f18f67056946 |
children |
comparison
equal
deleted
inserted
replaced
0:f18f67056946 | 1:6c6d26ff01ff |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 import pandas as pd | 2 |
3 import argparse | 3 import argparse |
4 | 4 |
5 parser = argparse.ArgumentParser() | 5 parser = argparse.ArgumentParser() |
6 parser.add_argument('-i', '--input', help='Tabular Input File Name') | 6 parser.add_argument('-i', '--input', help='Tabular Input File Name') |
7 parser.add_argument('-o','--output', help='Tabular Output File') | 7 parser.add_argument('-o','--output', help='Tabular Output File') |
8 parser.add_argument('-c', '--columns', nargs='+', help='Column Headers to Sort By') | 8 parser.add_argument( |
9 '-c', '--columns', nargs='+', help='Column Headers to Sort By' | |
10 ) | |
11 parser.add_argument( | |
12 '-d', '--discard', action='store_true', | |
13 help='Discard remaining columns' | |
14 ) | |
15 | |
9 args=parser.parse_args() | 16 args=parser.parse_args() |
10 | 17 |
11 cols = args.columns | 18 with open(args.input) as data: |
12 table = pd.read_csv(args.input, sep='\t') | 19 hdr = next(data) |
13 blist = list(table.columns) | 20 columns = hdr.rstrip('\n').split('\t') |
14 for token in cols: | 21 idx = [columns.index(name) for name in args.columns] |
15 blist.remove(token) | 22 if not args.discard: |
16 sorted_table = table[args.columns + blist] | 23 idx += [i for i in range(len(columns)) if i not in idx] |
17 # write without index, seperated by tabs | 24 rearranged_cols = [columns[i] for i in idx] |
18 sorted_table.to_csv(args.output, sep='\t', index=False) | 25 with open(args.output, 'w') as out: |
26 out.write('\t'.join(rearranged_cols) + '\n') | |
27 for line in data: | |
28 columns = line.rstrip('\n').split('\t') | |
29 rearranged_cols = [columns[i] for i in idx] | |
30 out.write('\t'.join(rearranged_cols) + '\n') |