Mercurial > repos > devteam > column_maker
annotate column_maker.py @ 7:e7c273e8d4d6 draft
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
author | devteam |
---|---|
date | Mon, 25 Jan 2021 09:59:49 +0000 |
parents | 0aeda7a81b46 |
children | 227e82286a0e |
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 |
0 | 13 |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 "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
|
27 "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
|
28 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
|
29 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
|
30 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
|
31 |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 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
|
36 |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 '{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
|
47 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
|
48 ) |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
49 ) |
0 | 50 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
|
51 in_columns = int(argparse_dict['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
|
52 if in_columns < 2: |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
53 # To be considered tabular, data must fulfill requirements of the sniff.is_column_based() method. |
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 # 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
|
58 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
|
59 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
|
60 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 | 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_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
|
63 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
|
64 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
|
65 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
|
66 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 | 67 |
68 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
|
69 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
|
70 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
|
71 whitelist = r"^([c0-9\+\-\*\/\(\)\.\'\"><=,:! ]|%s|%s|%s)*$" % (operators, builtin_and_math_functions, '|'.join(string_and_list_methods)) |
0 | 72 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
|
73 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
|
74 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
|
75 expr = "format_float_positional(%s)" % expr |
0 | 76 |
77 # Prepare the column variable names and wrappers for column data types | |
78 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
|
79 for col in range(1, in_columns + 1): |
0 | 80 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
|
81 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
|
82 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
|
83 if not round_result and col_type == 'int': |
0 | 84 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
|
85 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
|
86 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
|
87 |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
88 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
|
89 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
|
90 assign = "%s = line.split('\\t')" % col_str |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
91 wrap = "%s = %s" % (col_str, type_cast_str) |
0 | 92 skipped_lines = 0 |
93 first_invalid_line = 0 | |
94 invalid_line = None | |
95 lines_kept = 0 | |
96 total_lines = 0 | |
97 | |
98 # Read input file, skipping invalid lines, and perform computation that will result in a new column | |
99 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
|
100 # import here since flake8 complains otherwise |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
101 from math import ( |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
102 ceil, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
103 exp, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
104 floor, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
105 log, |
5
c6fdc1118036
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 0cb87d8f454f205af021d653d7f7d5a7c14c7718"
devteam
parents:
4
diff
changeset
|
106 log10, |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
107 sqrt |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
108 ) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
109 from numpy import format_float_positional |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
110 |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
111 for i, line in enumerate(fh): |
0 | 112 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
|
113 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
|
114 if not line or line.startswith('#'): |
0 | 115 skipped_lines += 1 |
116 if not invalid_line: | |
117 first_invalid_line = i + 1 | |
118 invalid_line = line | |
119 continue | |
120 try: | |
121 %s | |
122 %s | |
123 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
|
124 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
|
125 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
|
126 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
|
127 out.write(new_line) |
0 | 128 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
|
129 except Exception: |
0 | 130 skipped_lines += 1 |
131 if not invalid_line: | |
132 first_invalid_line = i + 1 | |
133 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
|
134 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
|
135 ''' % (assign, wrap, expr) |
0 | 136 |
137 valid_expr = True | |
138 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
|
139 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
|
140 except Exception as e: |
0 | 141 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
|
142 if str(e).startswith('invalid syntax'): |
0 | 143 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
|
144 sys.exit('Expression "%s" likely invalid. See tool tips, syntax and examples.' % expr) |
0 | 145 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
|
146 sys.exit(str(e)) |
0 | 147 |
148 if valid_expr: | |
149 out.close() | |
150 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
|
151 print('Creating column %d with expression %s' % (in_columns + 1, expr)) |
0 | 152 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
|
153 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
|
154 total_lines)) |
0 | 155 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
|
156 print('Possible invalid expression "%s" or non-existent column referenced. See tool tips, syntax and examples.' % expr) |
0 | 157 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
|
158 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
|
159 (skipped_lines, first_invalid_line, invalid_line)) |