Mercurial > repos > devteam > column_maker
annotate column_maker.py @ 6:0aeda7a81b46 draft
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
author | devteam |
---|---|
date | Wed, 30 Dec 2020 00:49:52 +0000 |
parents | c6fdc1118036 |
children | e7c273e8d4d6 |
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 |
0 | 12 |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
13 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
|
14 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
|
15 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
|
16 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
|
17 parser.add_argument('round', nargs='?', type=str, choices=['yes', 'no'], |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
18 help="round result") |
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('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
|
20 parser.add_argument('column_types', nargs='?', type=str, help="comma separated list of column types") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
21 parser.add_argument('avoid_scientific_notation', nargs='?', type=str, choices=['yes', 'no'], |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
22 help="avoid scientific notation") |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 |
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
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 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
|
33 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
|
34 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
|
35 round_result = argparse_dict['round'] |
0 | 36 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
|
37 in_columns = int(argparse_dict['columns']) |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
38 except Exception: |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
39 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 | 40 if in_columns < 2: |
41 # To be considered tabular, data must fulfill requirements of the sniff.is_column_based() method. | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
42 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 | 43 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
|
44 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
|
45 except Exception: |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
46 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.") |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
47 if len(in_column_types) != in_columns: |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
48 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.") |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
49 avoid_scientific_notation = argparse_dict['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
|
50 |
0 | 51 # Unescape if input has been escaped |
52 mapped_str = { | |
53 '__lt__': '<', | |
54 '__le__': '<=', | |
55 '__eq__': '==', | |
56 '__ne__': '!=', | |
57 '__gt__': '>', | |
58 '__ge__': '>=', | |
59 '__sq__': '\'', | |
60 '__dq__': '"', | |
61 } | |
62 for key, value in mapped_str.items(): | |
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 expr = expr.replace(key, value) |
0 | 64 |
65 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
|
66 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
|
67 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
|
68 whitelist = r"^([c0-9\+\-\*\/\(\)\.\'\"><=,:! ]|%s|%s|%s)*$" % (operators, builtin_and_math_functions, '|'.join(string_and_list_methods)) |
0 | 69 if not re.compile(whitelist).match(expr): |
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 exit("Invalid expression") |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
71 if avoid_scientific_notation == "yes": |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
72 expr = "format_float_positional(%s)" % expr |
0 | 73 |
74 # Prepare the column variable names and wrappers for column data types | |
75 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
|
76 for col in range(1, in_columns + 1): |
0 | 77 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
|
78 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
|
79 col_type = in_column_types[col - 1].strip() |
0 | 80 if round_result == 'no' and col_type == 'int': |
81 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
|
82 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
|
83 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
|
84 |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 wrap = "%s = %s" % (col_str, type_cast_str) |
0 | 89 skipped_lines = 0 |
90 first_invalid_line = 0 | |
91 invalid_line = None | |
92 lines_kept = 0 | |
93 total_lines = 0 | |
94 | |
95 # Read input file, skipping invalid lines, and perform computation that will result in a new column | |
96 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
|
97 # 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
|
98 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
|
99 ceil, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
100 exp, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
101 floor, |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
102 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
|
103 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
|
104 sqrt |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
105 ) |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
106 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
|
107 |
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
108 for i, line in enumerate(fh): |
0 | 109 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
|
110 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
|
111 if not line or line.startswith('#'): |
0 | 112 skipped_lines += 1 |
113 if not invalid_line: | |
114 first_invalid_line = i + 1 | |
115 invalid_line = line | |
116 continue | |
117 try: | |
118 %s | |
119 %s | |
120 new_val = %s | |
121 if round_result == "yes": | |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
122 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
|
123 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
|
124 out.write(new_line) |
0 | 125 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
|
126 except Exception: |
0 | 127 skipped_lines += 1 |
128 if not invalid_line: | |
129 first_invalid_line = i + 1 | |
130 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
|
131 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
|
132 ''' % (assign, wrap, expr) |
0 | 133 |
134 valid_expr = True | |
135 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
|
136 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
|
137 except Exception as e: |
0 | 138 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
|
139 if str(e).startswith('invalid syntax'): |
0 | 140 valid_expr = False |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
141 exit('Expression "%s" likely invalid. See tool tips, syntax and examples.' % expr) |
0 | 142 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
|
143 exit(str(e)) |
0 | 144 |
145 if valid_expr: | |
146 out.close() | |
147 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
|
148 print('Creating column %d with expression %s' % (in_columns + 1, expr)) |
0 | 149 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
|
150 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
|
151 total_lines)) |
0 | 152 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
|
153 print('Possible invalid expression "%s" or non-existent column referenced. See tool tips, syntax and examples.' % expr) |
0 | 154 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
|
155 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
|
156 (skipped_lines, first_invalid_line, invalid_line)) |