annotate pandas_pivot_table.py @ 3:4b65133e0722 draft

"planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit e0560d99fd88685476e4b65235b89edeb20e8426-dirty"
author jjohnson
date Thu, 17 Dec 2020 22:23:11 +0000
parents 6f05390deffa
children eaf2444a2a50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
2
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
3 import argparse
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
4 import json
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
5 import pandas as pd
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
6 import sys
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
7 from json.decoder import JSONDecodeError
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
8
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
9
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
10 def __main__():
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
11 p = argparse.ArgumentParser()
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
12 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
13 '-i', '--input',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
14 type=argparse.FileType('r'),
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
15 required=True,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
16 help='Tabular input file to pivot'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
17 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
18 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
19 '-o', '--output',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
20 type=argparse.FileType('w'),
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
21 required=True,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
22 help='Output file'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
23 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
24 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
25 '-S', '--skiprows',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
26 type=int,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
27 default=0,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
28 help='Input column names'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
29 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
30 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
31 '-H', '--header',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
32 default=None,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
33 help='Input column names'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
34 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
35 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
36 '-P', '--prefix',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
37 default=None,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
38 help='Prefix for input column names'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
39 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
40 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
41 '-I', '--index',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
42 help='index columns'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
43 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
44 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
45 '-C', '--columns',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
46 help='columns values which are returned as columns'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
47 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
48 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
49 '-V', '--values',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
50 help='values'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
51 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
52 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
53 '-F', '--aggfunc',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
54 help='aggregate functions on the values'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
55 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
56 p.add_argument(
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
57 '-N', '--fill_value',
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
58 default=None,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
59 help='fill value for missing values'
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
60 )
2
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
61 p.add_argument(
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
62 '-f', '--float_format',
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
63 default='%0.6f',
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
64 help=''
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
65 )
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
66 args = p.parse_args()
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
67
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
68 def getValueType(val):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
69 if val or 0. == val:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
70 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
71 return int(val)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
72 except ValueError:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
73 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
74 return float(val)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
75 except ValueError:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
76 return val
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
77 return None
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
78
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
79 def getColumn(name, dfcols):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
80 if name in dfcols:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
81 return name
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
82 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
83 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
84 i = int(name)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
85 return dfcols[i]
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
86 except Exception:
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
87 print('%s not a column in %s' % (name, dfcols),
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
88 file=sys.stderr)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
89 exit(1)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
90
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
91 def getColumns(val, dfcols):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
92 fields = [v.strip() for v in val.split(',')]
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
93 cols = []
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
94 for name in fields:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
95 cols.append(getColumn(name, dfcols))
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
96 return cols
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
97
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
98 def getAggFunc(funcStr, dfcols):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
99 af = funcStr
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
100 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
101 af = json.loads(funcStr)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
102 except JSONDecodeError as de:
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
103 print('"%s" is not a json string: ' % funcStr, de.msg,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
104 file=sys.stderr)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
105 exit(1)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
106 if isinstance(af, dict):
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
107 aggfunc = {getColumn(k, dfcols): v for k, v in af.items()}
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
108 elif isinstance(af, list):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
109 aggfunc = af
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
110 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
111 aggfunc = af
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
112 return aggfunc
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
113
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
114 if args.prefix:
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
115 df = pd.read_table(args.input,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
116 skiprows=args.skiprows,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
117 header=None,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
118 prefix=args.prefix)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
119 elif args.header:
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
120 df = pd.read_table(args.input,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
121 skiprows=args.skiprows,
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
122 header=args.header)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
123 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
124 df = pd.read_table(args.input, skiprows=args.skiprows)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
125 df_columns = df.columns.tolist()
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
126 index = getColumns(args.index, df_columns)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
127 columns = getColumns(args.columns, df_columns)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
128 values = getColumns(args.values, df_columns)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
129 fill_value = getValueType(args.fill_value)
3
4b65133e0722 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit e0560d99fd88685476e4b65235b89edeb20e8426-dirty"
jjohnson
parents: 2
diff changeset
130 aggfunc = getAggFunc(args.aggfunc.replace('\'', '"'), values)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
131 pdf = df.pivot_table(index=index, columns=columns,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
132 values=values, aggfunc=aggfunc,
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
133 fill_value=fill_value)
1
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
134 pdf_cols = ['_'.join(reversed(p)) if isinstance(p, tuple) else p
c02f59711eb6 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
jjohnson
parents: 0
diff changeset
135 for p in pdf.columns.tolist()]
2
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
136 pdf.to_csv(args.output,
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
137 sep='\t',
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
138 float_format=args.float_format,
6f05390deffa "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 1ca7433aaa606c636f796c75b6cfd8a080e2d5bc-dirty"
jjohnson
parents: 1
diff changeset
139 header=pdf_cols)
0
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
140
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
141
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
142 if __name__ == "__main__":
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
143 __main__()