Mercurial > repos > devteam > column_maker
annotate column_maker.py @ 10:beec6ecc7d3c draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
author | iuc |
---|---|
date | Thu, 22 Aug 2024 08:25:32 +0000 |
parents | 33b81f9ea109 |
children |
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 """ |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
3 This tool takes a tab-delimited textfile as input and creates new columns in |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
4 the file which are the result of a computation performed on every row in the |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
5 original file. The tool will skip over empty and comment (starting with a #) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
6 lines within the file. It does not change the formatting of any original, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
7 retained 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
|
8 """ |
0 | 9 |
6
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 argparse |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
11 import enum |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
12 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
|
13 import sys |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
14 # Functions that may be used in the compute expression |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
15 from math import ( # noqa: F401 |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
16 acos, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
17 acosh, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
18 asin, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
19 asinh, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
20 atan, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
21 atan2, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
22 atanh, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
23 cbrt, |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
24 ceil, |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
25 comb, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
26 copysign, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
27 cos, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
28 cosh, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
29 degrees, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
30 dist, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
31 erf, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
32 erfc, |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
33 exp, |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
34 exp2, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
35 expm1, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
36 fabs, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
37 factorial, |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
38 floor, |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
39 fmod, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
40 frexp, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
41 fsum, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
42 gamma, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
43 gcd, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
44 hypot, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
45 inf, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
46 isclose, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
47 isfinite, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
48 isinf, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
49 isnan, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
50 isqrt, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
51 ldexp, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
52 lgamma, |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
53 log, |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
54 log10, |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
55 log1p, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
56 log2, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
57 modf, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
58 nextafter, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
59 perm, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
60 pi, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
61 pow, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
62 prod, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
63 remainder, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
64 sin, |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
65 sqrt, |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
66 tan, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
67 tanh, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
68 tau, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
69 trunc, |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
70 ulp, |
8
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
71 ) |
227e82286a0e
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit c623e4b9f6dd6ff4b88d9386f3b0a0cb497e0a0e"
devteam
parents:
7
diff
changeset
|
72 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
73 from numpy import format_float_positional |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
74 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
75 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
76 class Mode(enum.Enum): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
77 APPEND = '' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
78 INSERT = 'I' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
79 REPLACE = 'R' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
80 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
81 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
82 def from_str(s, to_type): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
83 if to_type is list: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
84 return [part.strip(' ') for part in s.split(',')] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
85 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
86 return to_type(s) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
87 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
88 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
89 def to_str(obj): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
90 if type(obj) is list: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
91 return ','.join([to_str(i) for i in obj]) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
92 if args.avoid_scientific_notation and type(obj) is float: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
93 return format_float_positional(obj) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
94 return str(obj) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
95 |
0 | 96 |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
97 parser = argparse.ArgumentParser() |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
98 parser.add_argument('input', type=str, help='input file') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
99 parser.add_argument('output', type=str, help='output file') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
100 parser.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
101 '-t', '--column-types', nargs='?', required=True, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
102 help='A comma-separated list of column types in the input file' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
103 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
104 parser.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
105 '--avoid-scientific-notation', action='store_true', |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
106 help='avoid scientific notation' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
107 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
108 parser.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
109 '--header', action='store_true', |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
110 help='The input has a header line with column names. ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
111 'Actions must specify names of newly calculated columns.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
112 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
113 parser.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
114 '--fail-on-non-existent-columns', action='store_true', |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
115 help='If an action references a column number that is not existent ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
116 'when the expression gets computed, the default behavior is to treat ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
117 'this as a case of rows for which the expression cannot be computed. ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
118 'The behavior of the tool will then depend on which of the ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
119 'non-computable switches is in effect. With this flag, in contrast, ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
120 'the tool will fail directly upon encountering a non-existing column.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
121 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
122 non_computable = parser.add_mutually_exclusive_group() |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
123 non_computable.add_argument('--fail-on-non-computable', action='store_true') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
124 non_computable.add_argument('--skip-non-computable', action='store_true') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
125 non_computable.add_argument('--keep-non-computable', action='store_true') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
126 non_computable.add_argument('--non-computable-blank', action='store_true') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
127 non_computable.add_argument('--non-computable-default') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
128 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
129 group = parser.add_mutually_exclusive_group(required=True) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
130 group.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
131 '-a', '--actions', nargs='*', type=str, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
132 help='One or more action(s) of the format EXPR;[COL_ADD_SPEC];[COL_NAME]' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
133 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
134 group.add_argument( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
135 '-f', '--file', type=str, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
136 help='File to read actions from (mutually exclusive with -a)' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
137 ) |
6
0aeda7a81b46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit a993d43d9d1702a6cf584683cf72527a3f999236"
devteam
parents:
5
diff
changeset
|
138 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
|
139 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
140 if not args.column_types: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
141 with open(args.input) as fh: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
142 if not fh.readline(): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
143 # Generally, the input must have at least one column to be |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
144 # considered tabular, but empty files are ok and should produce |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
145 # empty output. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
146 with open(args.output, 'w') as out: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
147 pass |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
148 sys.exit() |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
149 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
150 "Missing column types. " |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
151 "In Galaxy, click the pencil icon on the history item and " |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
152 "select the Auto-detect option to correct it. " |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
153 "This tool can only be used with tab-delimited data." |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
154 ) |
7
e7c273e8d4d6
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 35c79ccee143e8d178ebd24f6317888de3ca0187"
devteam
parents:
6
diff
changeset
|
155 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
156 in_column_types = [t.strip() for t in args.column_types.split(',')] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
157 in_columns = len(in_column_types) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
158 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
159 # Prepare initial column variable names and type cast representations |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
160 # for column data types |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
161 cols, type_casts = [], [] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
162 for n, col_type in enumerate(in_column_types, start=1): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
163 col_name = "c%d" % n |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
164 cols.append(col_name) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
165 col_str = ', '.join(cols) # 'c1, c2, c3, c4' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
166 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
167 # Define lambda for type-casting of original row fields |
0 | 168 try: |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
169 cast_types = eval( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
170 'lambda fields: [from_str(s, t) for s, t in zip(fields, [%s])]' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
171 % args.column_types |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
172 ) |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
173 except Exception as err: |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
174 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
175 'While parsing column types, the following problem occured: "%s"' |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
176 % err |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
177 ) |
0 | 178 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
179 # Get and parse actions |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
180 if args.file: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
181 actions = [] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
182 with open(args.file) as i: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
183 for line in i: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
184 line = line.strip() |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
185 if line: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
186 actions.append(line) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
187 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
188 actions = args.actions |
0 | 189 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
190 # each action must be a full data row manipulation instruction of the form: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
191 # EXPR;[COL_ADD_SPEC];[COL_NAME] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
192 # where EXPR is the actual expression to compute on the row, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
193 # COL_ADD_SPEC consists of a column index and a mode identifier for how the |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
194 # new column should be added. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
195 # Examples: 3I (insert new col before current column 3), |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
196 # 2R (replace current column 2 with new column); |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
197 # a missing COL_ADD_SPEC is interpreted as mode A (append new column at the |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
198 # end of the row). |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
199 # COL_NAME is required with the --header option and specifies the name of the |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
200 # new column; without --header, any COL_NAME gets ignored. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
201 operators = 'is|not|or|and' |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
202 builtin_functions = ( |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
203 'abs|all|any|ascii|bin|bool|chr|complex|divmod|float|format|hex|int|len|' |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
204 'list|map|max|min|oct|ord|pow|range|reversed|round|set|sorted|str|sum|type' |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
205 ) |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
206 |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
207 math_functions = ( |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
208 'acos|acosh|asin|asinh|atan|atan2|atanh|cbrt|ceil|comb|copysign|cos|cosh|degrees|' |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
209 'dist|erf|erfc|exp|exp2|expm1|fabs|factorial|floor|fmod|frexp|fsum|gamma|gcd|' |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
210 'hypot|inf|isclose|isfinite|isinf|isnan|isqrt|ldexp|lgamma|log|log10|log1p|' |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
211 'log2|modf|nextafter|perm|pi|pow|prod|remainder|sin|' |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
212 'sqrt|tan|tanh|tau|trunc|ulp' |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
213 ) |
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
214 builtin_and_math_functions = builtin_functions + '|' + math_functions |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
215 imported_numpy_function = 'format_float_positional' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
216 string_and_list_methods = [ |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
217 name for name in dir('') + dir([]) if not name.startswith('_') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
218 ] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
219 whitelist = r"^([c0-9\+\-\*\/\(\)\.\'\"><=,:! ]|%s|%s|%s|%s)*$" % ( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
220 operators, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
221 builtin_and_math_functions, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
222 imported_numpy_function, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
223 '|'.join(string_and_list_methods) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
224 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
225 valid_pat = re.compile(whitelist) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
226 ops = [] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
227 num_cols = in_columns |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
228 for ac in actions: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
229 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
230 expr_string, col_add_spec, new_col_name = ac.split(';') |
10
beec6ecc7d3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit 06a92568f3409b035fd8a08725db20933d8d15e6
iuc
parents:
9
diff
changeset
|
231 print(expr_string) |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
232 except ValueError: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
233 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
234 'Invalid Action: "%s". ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
235 'Required format: EXPR;[COL_ADD_SPEC];[COL_NAME]' % ac |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
236 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
237 if not valid_pat.match(expr_string): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
238 sys.exit('Invalid expression: "%s"' % expr_string) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
239 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
240 expr_lambda = eval('lambda %s: %s' % (col_str, expr_string)) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
241 except Exception as e: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
242 if str(e).startswith('invalid syntax'): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
243 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
244 'Expression "%s" caused a syntax error during parsing.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
245 % expr_string |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
246 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
247 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
248 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
249 'While parsing expression "%s" the following problem occured: ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
250 '"%s"' % (expr_string, str(e)) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
251 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
252 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
253 new_col_idx = int(col_add_spec[:-1] or '0') - 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
254 except ValueError: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
255 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
256 'COL_ADD_SPECS need to start with a (1-based) column index. ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
257 'Could not parse a column index from "%s"' % col_add_spec |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
258 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
259 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
260 mode = Mode(col_add_spec[-1:]) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
261 except ValueError: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
262 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
263 'COL_ADD_SPECS need to end in a single-character mode identifier ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
264 '("I", or "R"), or be empty (for Append mode). ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
265 'Could not parse a valid identifier from "%s"' % col_add_spec |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
266 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
267 if mode is Mode.REPLACE: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
268 if new_col_idx < 0 or new_col_idx >= num_cols: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
269 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
270 'Cannot replace the contents of column %d as specified by ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
271 'action "%s". No such column at this point of the ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
272 'computation' % (new_col_idx + 1, ac) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
273 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
274 if not new_col_name and args.header: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
275 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
276 'A name is required for any new columns when using an existing ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
277 'header line (--header option), but found none in action: ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
278 '"%s"' % ac |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
279 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
280 # Successfully parsed the instruction |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
281 # Store the expression lambda, the index and name of the new column, and |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
282 # the original string representation of the expression (for use in |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
283 # potential later error messages). |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
284 ops.append([expr_lambda, new_col_idx, mode, new_col_name, expr_string]) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
285 if mode is Mode.APPEND or mode is Mode.INSERT: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
286 # If the current expression results in an additional column, |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
287 # we need to handle the new field in subsequent lambda functions. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
288 num_cols += 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
289 col_str += ', c%d' % num_cols |
4
3335c0d583d8
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/column_maker commit 2b17bdfc47ca4d7f1a584216c4bd61a7050df7ea"
devteam
parents:
2
diff
changeset
|
290 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
291 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
292 # ready to start parsing the input file |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
293 print( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
294 'Computing %d new columns with instructions %s' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
295 % (num_cols - in_columns, actions) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
296 ) |
0 | 297 skipped_lines = 0 |
298 first_invalid_line = 0 | |
299 invalid_line = None | |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
300 lines_computed = 0 |
0 | 301 total_lines = 0 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
302 non_existent_col_pat = re.compile(r"name 'c\d+' is not defined") |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
303 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
304 with open(args.input, encoding='utf-8') as fh, \ |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
305 open(args.output, 'w', encoding='utf-8') as out: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
306 if args.header: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
307 # compute new header line from original |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
308 header_cols = fh.readline().strip('\n').split('\t') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
309 for _, col_idx, mode, col_name, _ in ops: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
310 if mode is Mode.INSERT: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
311 header_cols.insert(col_idx, col_name) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
312 elif mode is Mode.REPLACE: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
313 header_cols[col_idx] = col_name |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
314 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
315 header_cols.append(col_name) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
316 out.write('\t'.join(header_cols) + '\n') |
0 | 317 |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
318 # read data, skipping empty and comment lines, and perform computations |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
319 # that will result in new columns |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
320 for i, line in enumerate(fh): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
321 total_lines += 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
322 line = line.rstrip('\n') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
323 if not line or line.startswith('#'): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
324 skipped_lines += 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
325 if not invalid_line: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
326 first_invalid_line = i + 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
327 invalid_line = line |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
328 continue |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
329 fields = line.split('\t') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
330 if len(fields) == in_columns: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
331 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
332 typed_fields = cast_types(fields) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
333 except ValueError as e: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
334 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
335 'Failed to convert some of the columns in line #%d to their ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
336 'expected types. The error was: "%s" for the line: "%s"' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
337 % (i, str(e), line) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
338 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
339 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
340 # A "suspicious" line with less or more fields than expected |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
341 # Type-casting for it might fail or not, but it is pointless to |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
342 # even try because subsequent computation of any expression will |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
343 # fail anyway as expression lambdas expect a fixed number of |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
344 # arguments. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
345 # Lets pass in a copy of the original string fields, let |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
346 # the computation of the first expression fail, then have that |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
347 # situation handled according to the non-computable settings in |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
348 # effect. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
349 typed_fields = fields[:] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
350 for fun, col_idx, mode, col_name, ex in ops: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
351 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
352 try: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
353 new_val = fun(*typed_fields) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
354 except NameError as e: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
355 # Python 3.10+ would have the problematic name |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
356 # available as e.name |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
357 if non_existent_col_pat.fullmatch(str(e)) and ( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
358 not args.fail_on_non_existent_columns |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
359 ): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
360 # Looks like a reference to a non-existent column |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
361 # and we are not supposed to fail on it directly. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
362 # Reraise and have it handled as a non-computable |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
363 # row. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
364 raise |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
365 # NameErrors are not row-specific, but indicate a |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
366 # general problem with the user-supplied expression. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
367 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
368 'While parsing expression "%s" the following ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
369 'problem occured: "%s"' % (ex, str(e)) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
370 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
371 except Exception as e: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
372 if args.skip_non_computable: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
373 # log that a line got skipped, then stop computing |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
374 # for this line |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
375 skipped_lines += 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
376 if not invalid_line: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
377 first_invalid_line = i + 1 |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
378 invalid_line = line |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
379 break |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
380 if args.keep_non_computable: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
381 # write the original line unchanged and stop computing |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
382 # for this line |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
383 out.write(line + '\n') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
384 break |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
385 if args.non_computable_blank: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
386 new_val = '' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
387 elif args.non_computable_default is not None: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
388 new_val = args.non_computable_default |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
389 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
390 # --fail_on_non_computable |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
391 # (which is default behavior, too) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
392 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
393 'Could not compute a new column value using "%s" on ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
394 'line #%d: "%s". Error was "%s"' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
395 % (ex, i, line, str(e)) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
396 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
397 if mode is Mode.INSERT: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
398 fields.insert(col_idx, new_val) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
399 typed_fields.insert(col_idx, new_val) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
400 elif mode is Mode.REPLACE: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
401 if col_idx > len(fields): |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
402 # Intentionally allow "replacing" one column beyond |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
403 # current fields since this can be used to fix |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
404 # short lines in the input. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
405 sys.exit( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
406 'Cannot replace column #%d in line with %d columns: ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
407 '"%s"' % (col_idx + 1, len(fields), line) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
408 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
409 fields[col_idx:col_idx + 1] = [new_val] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
410 typed_fields[col_idx:col_idx + 1] = [new_val] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
411 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
412 fields.append(new_val) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
413 typed_fields.append(new_val) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
414 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
415 fields = [to_str(field) for field in fields] |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
416 out.write('\t'.join(fields) + '\n') |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
417 lines_computed += 1 |
0 | 418 |
419 | |
9
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
420 valid_lines = total_lines - skipped_lines |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
421 if valid_lines > 0: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
422 print( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
423 'Computed new column values for %4.2f%% of %d lines written.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
424 % (100.0 * lines_computed / valid_lines, valid_lines) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
425 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
426 elif args.fail_on_non_existent_columns: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
427 # Warn the user that there could be an issue with an expression. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
428 print( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
429 'Could not compute a new column for any input row! ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
430 'Please check your expression(s) "%s" for problems.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
431 % actions |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
432 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
433 else: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
434 # Same, but the problem could also be a reference to a non-existent |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
435 # column. |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
436 print( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
437 'Could not compute a new column for any input row! ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
438 'Please check your expression(s) "%s" for references to non-existent ' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
439 'columns or other problems.' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
440 % actions |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
441 ) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
442 if skipped_lines > 0: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
443 print('Skipped %d invalid lines starting at line #%d: "%s"' % |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
444 (skipped_lines, first_invalid_line, invalid_line)) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
445 if lines_computed < valid_lines: |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
446 print( |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
447 'Rewrote %d lines unmodified because computation of a new value failed' |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
448 % (valid_lines - lines_computed) |
33b81f9ea109
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/column_maker commit fe76077775aaca531f6a563fdfcbd73fbf1528e7
iuc
parents:
8
diff
changeset
|
449 ) |