Mercurial > repos > mvdbeek > add_input_name_as_column
comparison add_input_name_as_column.py @ 6:3284b72eef56 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/add_input_name_as_column commit 9292c57c34283543e86ecc65f805977224f6fc7b"
| author | iuc |
|---|---|
| date | Wed, 25 Mar 2020 07:11:05 -0400 |
| parents | 06061aa49527 |
| children | 8061668d0868 |
comparison
equal
deleted
inserted
replaced
| 5:06061aa49527 | 6:3284b72eef56 |
|---|---|
| 1 import argparse | |
| 1 import io | 2 import io |
| 2 import argparse | |
| 3 | 3 |
| 4 | 4 |
| 5 def Parser(): | 5 def Parser(): |
| 6 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") |
| 7 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") |
| 8 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") |
| 9 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") |
| 10 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") |
| 11 the_parser.add_argument('--prepend', action='store_true', default=False, help='Prepend column instead of appending' ) | |
| 12 | |
| 11 args = the_parser.parse_args() | 13 args = the_parser.parse_args() |
| 12 return args | 14 return args |
| 13 | 15 |
| 14 | 16 |
| 15 args = Parser() | 17 args = Parser() |
| 17 | 19 |
| 18 with io.open(args.input, encoding="utf-8") as input, io.open(args.output, 'w', encoding="utf-8") as output: | 20 with io.open(args.input, encoding="utf-8") as input, io.open(args.output, 'w', encoding="utf-8") as output: |
| 19 for i, line in enumerate(input): | 21 for i, line in enumerate(input): |
| 20 line = line.strip('\n') | 22 line = line.strip('\n') |
| 21 if (i == 0) and args.header: | 23 if (i == 0) and args.header: |
| 22 line = "%s\t%s\n" % (line, args.header) | 24 new_entry = args.header |
| 23 else: | 25 else: |
| 24 line = "%s\t%s\n" % (line, args.label) | 26 new_entry = args.label |
| 27 if args.prepend: | |
| 28 line = "%s\t%s\n" % (new_entry, line) | |
| 29 else: | |
| 30 line = "%s\t%s\n" % (line, new_entry) | |
| 25 output.write(line) | 31 output.write(line) |
