view 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
line wrap: on
line source

#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help='Tabular Input File Name')
parser.add_argument('-o','--output', help='Tabular Output File')
parser.add_argument(
    '-c', '--columns', nargs='+', help='Column Headers to Sort By'
)
parser.add_argument(
    '-d', '--discard', action='store_true',
    help='Discard remaining columns'
)

args=parser.parse_args()

with open(args.input) as data:
    hdr = next(data)
    columns = hdr.rstrip('\n').split('\t')
    idx = [columns.index(name) for name in args.columns]
    if not args.discard:
        idx += [i for i in range(len(columns)) if i not in idx]
    rearranged_cols = [columns[i] for i in idx]
    with open(args.output, 'w') as out:
        out.write('\t'.join(rearranged_cols) + '\n')
        for line in data:
            columns = line.rstrip('\n').split('\t')
            rearranged_cols = [columns[i] for i in idx]
            out.write('\t'.join(rearranged_cols) + '\n')