# HG changeset patch # User bgruening # Date 1550234703 18000 # Node ID 6c6d26ff01ffe407c778c8dfb0897b9c4af20bfa # Parent f18f67056946d38ce64d13f4e13a3fb03eb4f5dd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/column_arrange_by_header commit b6e0b2de32ddb91085235397728623a35ad13f42 diff -r f18f67056946 -r 6c6d26ff01ff columnArrange.xml --- a/columnArrange.xml Fri Oct 16 14:31:13 2015 -0400 +++ b/columnArrange.xml Fri Feb 15 07:45:03 2019 -0500 @@ -1,17 +1,15 @@ - + by header name - - pandas - @@ -22,9 +20,15 @@ + + + + @@ -38,7 +42,36 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -48,14 +81,16 @@ - + **What it does** -With this tool you can specify (by naming the header) which columns need to be leftmost. -The columns which are not specified will be ordered as before, right of the columns which were specified. +With this tool you can specify - by name - the order of columns for tabular +data. +Columns not specified will remain ordered as before and be moved to the right +of the specified columns, as shown in the following example. Input file:: @@ -63,12 +98,15 @@ a b c d a b c d -Specifying **CHeader** and **BHeader**, as the columns that should be leftmost, generates:: +Specifying **CHeader** and **BHeader**, as the columns that should be leftmost, +generates:: CHeader BHeader AHeader DHeader c b a d c b a d +Alternatively, you can choose to retain *only* the specified columns in their +new arrangement and discard all other columns. diff -r f18f67056946 -r 6c6d26ff01ff column_arrange.py --- a/column_arrange.py Fri Oct 16 14:31:13 2015 -0400 +++ b/column_arrange.py Fri Feb 15 07:45:03 2019 -0500 @@ -1,18 +1,30 @@ -#!/usr/bin/env python -import pandas as pd +#!/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( + '-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() -cols = args.columns -table = pd.read_csv(args.input, sep='\t') -blist = list(table.columns) -for token in cols: - blist.remove(token) -sorted_table = table[args.columns + blist] -# write without index, seperated by tabs -sorted_table.to_csv(args.output, sep='\t', index=False) +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') diff -r f18f67056946 -r 6c6d26ff01ff column_arrange_by_header.tar.gz Binary file column_arrange_by_header.tar.gz has changed diff -r f18f67056946 -r 6c6d26ff01ff tool_dependencies.xml --- a/tool_dependencies.xml Fri Oct 16 14:31:13 2015 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ - - - - - -