Mercurial > repos > devteam > column_maker
annotate column_maker.py @ 8:227e82286a0e draft
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
author | devteam |
---|---|
date | Wed, 24 Feb 2021 05:19:12 +0000 |
parents | e7c273e8d4d6 |
children | 33b81f9ea109 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
2 """ |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
3 This tool takes a tab-delimited textfile as input and creates another column in |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
4 the file which is the result of a computation performed on every row in the |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
5 original file. The tool will skip over invalid lines within the file, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
6 informing the user about the number of lines skipped. |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
7 """ |
0 | 8 |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
9 import argparse |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
10 import json |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
11 import re |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
12 import sys |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
13 # functions that may be used in the compute expression |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
14 from math import ( # noqa: F401 |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
15 ceil, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
16 exp, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
17 floor, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
18 log, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
19 log10, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
20 sqrt |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
21 ) |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
22 |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
23 from numpy import format_float_positional # noqa: F401 |
0 | 24 |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
25 parser = argparse.ArgumentParser() |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
26 parser.add_argument('input', type=argparse.FileType('r'), help="input file") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
27 parser.add_argument('output', type=argparse.FileType('wt'), help="output file") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
28 parser.add_argument('cond', nargs='?', type=str, help="expression") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
29 parser.add_argument('columns', nargs='?', type=int, help="number of columns") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
30 parser.add_argument('column_types', nargs='?', type=str, help="comma separated list of column types") |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
31 parser.add_argument('--round', action="store_true", |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
32 help="round result") |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
33 parser.add_argument('--avoid_scientific_notation', action="store_true", |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
34 help="avoid scientific notation") |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
35 parser.add_argument('--header_new_column_name', default=None, type=str, |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
36 help="First line of input is a header line with column " |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
37 "names and this should become the name of the new " |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
38 "column") |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
39 parser.add_argument('--load_json', default=None, type=argparse.FileType('r'), |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
40 help="overwrite parsed arguments from json file") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
41 args = parser.parse_args() |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
42 |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
43 argparse_dict = vars(args) |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
44 if args.load_json: |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
45 json_dict = json.load(args.load_json) |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
46 argparse_dict.update(json_dict) |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
47 |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
48 fh = argparse_dict['input'] |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
49 out = argparse_dict['output'] |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
50 expr = argparse_dict['cond'] |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
51 round_result = argparse_dict['round'] |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
52 avoid_scientific_notation = argparse_dict['avoid_scientific_notation'] |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
53 |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
54 if argparse_dict['header_new_column_name'] is not None: |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
55 header_line = fh.readline().strip('\n') |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
56 out.write( |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
57 '{0}\t{1}\n'.format( |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
58 header_line, argparse_dict['header_new_column_name'] |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
59 ) |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
60 ) |
0 | 61 try: |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
62 in_columns = int(argparse_dict['columns']) |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
63 if in_columns < 1: |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
64 # To be considered tabular, data must have at least one column. |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
65 raise ValueError |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
66 except Exception: |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
67 if not fh.readline(): |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
68 # empty file content is ok and should produce empty output |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
69 out.close() |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
70 sys.exit() |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
71 sys.exit("Missing or invalid 'columns' metadata value, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data.") |
0 | 72 try: |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
73 in_column_types = argparse_dict['column_types'].split(',') |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
74 except Exception: |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
75 sys.exit("Missing or invalid 'column_types' metadata value, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data.") |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
76 if len(in_column_types) != in_columns: |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
77 sys.exit("The 'columns' metadata setting does not conform to the 'column_types' metadata setting, click the pencil icon in the history item and select the Auto-detect option to correct it. This tool can only be used with tab-delimited data.") |
0 | 78 |
79 operators = 'is|not|or|and' | |
5
c6fdc1118036
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 0cb87d8f454f205af021d653d7f7d5a7c14c7718"
devteam
parents:
4
diff
changeset
|
80 builtin_and_math_functions = 'abs|all|any|bin|chr|cmp|complex|divmod|float|bool|hex|int|len|long|max|min|oct|ord|pow|range|reversed|round|sorted|str|sum|type|unichr|unicode|log|log10|exp|sqrt|ceil|floor' |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
81 string_and_list_methods = [name for name in dir('') + dir([]) if not name.startswith('_')] |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
82 whitelist = r"^([c0-9\+\-\*\/\(\)\.\'\"><=,:! ]|%s|%s|%s)*$" % (operators, builtin_and_math_functions, '|'.join(string_and_list_methods)) |
0 | 83 if not re.compile(whitelist).match(expr): |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
84 sys.exit("Invalid expression") |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
85 if avoid_scientific_notation: |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
86 expr = "format_float_positional(%s)" % expr |
0 | 87 |
88 # Prepare the column variable names and wrappers for column data types | |
89 cols, type_casts = [], [] | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
90 for col in range(1, in_columns + 1): |
0 | 91 col_name = "c%d" % col |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
92 cols.append(col_name) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
93 col_type = in_column_types[col - 1].strip() |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
94 if not round_result and col_type == 'int': |
0 | 95 col_type = 'float' |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
96 type_cast = "%s(%s)" % (col_type, col_name) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
97 type_casts.append(type_cast) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
98 |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
99 col_str = ', '.join(cols) # 'c1, c2, c3, c4' |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
100 type_cast_str = ', '.join(type_casts) # 'str(c1), int(c2), int(c3), str(c4)' |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
101 assign = "%s = line.split('\\t')" % col_str |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
102 if len(cols) == 1: |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
103 # Single column, unpacking by assignment won't work |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
104 assign += '[0]' |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
105 wrap = "%s = %s" % (col_str, type_cast_str) |
0 | 106 skipped_lines = 0 |
107 first_invalid_line = 0 | |
108 invalid_line = None | |
109 lines_kept = 0 | |
110 total_lines = 0 | |
111 | |
112 # Read input file, skipping invalid lines, and perform computation that will result in a new column | |
113 code = ''' | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
114 for i, line in enumerate(fh): |
0 | 115 total_lines += 1 |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
116 line = line.rstrip('\\r\\n') |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
117 if not line or line.startswith('#'): |
0 | 118 skipped_lines += 1 |
119 if not invalid_line: | |
120 first_invalid_line = i + 1 | |
121 invalid_line = line | |
122 continue | |
123 try: | |
124 %s | |
125 %s | |
126 new_val = %s | |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
127 if round_result: |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
128 new_val = int(round(new_val)) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
129 new_line = line + '\\t' + str(new_val) + "\\n" |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
130 out.write(new_line) |
0 | 131 lines_kept += 1 |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
132 except Exception: |
0 | 133 skipped_lines += 1 |
134 if not invalid_line: | |
135 first_invalid_line = i + 1 | |
136 invalid_line = line | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
137 fh.close() |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
138 ''' % (assign, wrap, expr) |
0 | 139 |
140 valid_expr = True | |
141 try: | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
142 exec(code) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
143 except Exception as e: |
0 | 144 out.close() |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
145 if str(e).startswith('invalid syntax'): |
0 | 146 valid_expr = False |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
147 sys.exit('Expression "%s" likely invalid. See tool tips, syntax and examples.' % expr) |
0 | 148 else: |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
149 sys.exit(str(e)) |
0 | 150 |
151 if valid_expr: | |
152 out.close() | |
153 valid_lines = total_lines - skipped_lines | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
154 print('Creating column %d with expression %s' % (in_columns + 1, expr)) |
0 | 155 if valid_lines > 0: |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
156 print('kept %4.2f%% of %d lines.' % (100.0 * lines_kept / valid_lines, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
157 total_lines)) |
0 | 158 else: |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
159 print('Possible invalid expression "%s" or non-existent column referenced. See tool tips, syntax and examples.' % expr) |
0 | 160 if skipped_lines > 0: |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
161 print('Skipped %d invalid lines starting at line #%d: "%s"' % |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
162 (skipped_lines, first_invalid_line, invalid_line)) |