Mercurial > repos > mvdbeek > add_input_name_as_column
comparison add_input_name_as_column.py @ 5:06061aa49527 draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/add_input_name_as_column commit db90fc6943b99322a6d7459f644a6624c69a0be5-dirty
author | mvdbeek |
---|---|
date | Wed, 06 Mar 2019 09:16:29 -0500 |
parents | 3a1f8302302d |
children | 3284b72eef56 |
comparison
equal
deleted
inserted
replaced
4:b31219f26a8f | 5:06061aa49527 |
---|---|
1 import sys | 1 import io |
2 import argparse | 2 import argparse |
3 | 3 |
4 | |
4 def Parser(): | 5 def Parser(): |
5 the_parser = argparse.ArgumentParser(description="add label to last column of file") | 6 the_parser = argparse.ArgumentParser(description="add label to last column of file") |
6 the_parser.add_argument('--input', required=True, action="store", type=str, help="input tabular file") | 7 the_parser.add_argument('--input', required=True, action="store", type=str, help="input tabular file") |
7 the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") | 8 the_parser.add_argument('--output', required=True, action="store", type=str, help="output file path") |
8 the_parser.add_argument('--label', required=True, action="store", type=str, help="label to add in last column") | 9 the_parser.add_argument('--label', required=True, action="store", type=str, help="label to add in last column") |
9 the_parser.add_argument('--header', action="store", type=str, help="column label for last column") | 10 the_parser.add_argument('--header', action="store", type=str, help="column label for last column") |
10 args = the_parser.parse_args() | 11 args = the_parser.parse_args() |
11 return args | 12 return args |
12 | 13 |
13 args=Parser() | |
14 | 14 |
15 input=open(args.input) | 15 args = Parser() |
16 output=open(args.output, 'w') | 16 |
17 for i,line in enumerate(input): | 17 |
18 line=line.strip('\n') | 18 with io.open(args.input, encoding="utf-8") as input, io.open(args.output, 'w', encoding="utf-8") as output: |
19 if (i==0) and (args.header!=None): | 19 for i, line in enumerate(input): |
20 line=line+'\t'+args.header | 20 line = line.strip('\n') |
21 else: | 21 if (i == 0) and args.header: |
22 line=line+'\t'+args.label | 22 line = "%s\t%s\n" % (line, args.header) |
23 print >>output, line | 23 else: |
24 input.close() | 24 line = "%s\t%s\n" % (line, args.label) |
25 output.close() | 25 output.write(line) |