annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
1 #!/usr/bin/env python
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
2
0
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
3 import argparse
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
4
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
5 parser = argparse.ArgumentParser()
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
6 parser.add_argument('-i', '--input', help='Tabular Input File Name')
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
7 parser.add_argument('-o','--output', help='Tabular Output File')
1
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
8 parser.add_argument(
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
9 '-c', '--columns', nargs='+', help='Column Headers to Sort By'
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
10 )
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
11 parser.add_argument(
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
12 '-d', '--discard', action='store_true',
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
13 help='Discard remaining columns'
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
14 )
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
15
0
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
16 args=parser.parse_args()
f18f67056946 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit 088e73e958b55dc765778641b8a84080cc289f85-dirty
bgruening
parents:
diff changeset
17
1
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
18 with open(args.input) as data:
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
19 hdr = next(data)
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
20 columns = hdr.rstrip('\n').split('\t')
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
21 idx = [columns.index(name) for name in args.columns]
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
22 if not args.discard:
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
23 idx += [i for i in range(len(columns)) if i not in idx]
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
24 rearranged_cols = [columns[i] for i in idx]
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
25 with open(args.output, 'w') as out:
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
26 out.write('\t'.join(rearranged_cols) + '\n')
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
27 for line in data:
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
28 columns = line.rstrip('\n').split('\t')
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
29 rearranged_cols = [columns[i] for i in idx]
6c6d26ff01ff planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42
bgruening
parents: 0
diff changeset
30 out.write('\t'.join(rearranged_cols) + '\n')