diff 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 diff
--- 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')