annotate pandas_pivot_table.py @ 0:621144f8dbe9 draft

"planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
author jjohnson
date Wed, 02 Dec 2020 22:59:57 +0000
parents
children c02f59711eb6
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 re
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 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
7 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
8 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
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
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
11 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
12 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
13 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
14 '-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
15 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
16 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
17 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
18 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
19 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
20 '-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
21 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
22 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
23 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
24 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
25 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
26 '-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
27 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
28 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
29 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
30 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
31 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
32 '-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
33 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
34 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
35 )
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.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
37 '-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
38 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
39 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
40 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
41 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
42 '-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
43 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
44 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
45 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
46 '-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
47 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
48 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
49 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
50 '-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
51 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
52 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
53 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
54 '-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
55 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
56 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
57 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
58 '-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
59 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
60 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
61 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
62 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
63
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
64 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
65 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
66 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
67 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
68 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
69 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
70 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
71 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
72 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
73 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
74
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
75 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
76 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
77 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
78 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
79 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
80 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
81 return dfcols[i]
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
82 except:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
83 print('%s not a column in %s' % (name, dfcols), file=sys.stderr)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
84 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
85
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
86 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
87 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
88 cols = []
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
89 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
90 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
91 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
92
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
93 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
94 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
95 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
96 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
97 except JSONDecodeError as de:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
98 print('"%s" is not a json string: ' % funcStr, de.msg, file=sys.stderr)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
99 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
100 if isinstance(af, dict):
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
101 aggfunc = {getColumn(k, dfcols) : v for k,v in af.items()}
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
102 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
103 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
104 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
105 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
106 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
107
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
108 if args.prefix:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
109 df = pd.read_table(args.input, skiprows=args.skiprows, header=None, prefix=args.prefix)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
110 elif args.header:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
111 df = pd.read_table(args.input, skiprows=args.skiprows, header=args.header)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
112 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
113 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
114 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
115 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
116 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
117 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
118 fill_value = getValueType(args.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
119 aggfunc = getAggFunc(args.aggfunc, values)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
120 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
121 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
122 fill_value=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
123 pdf_cols = ['_'.join(reversed(p)) if isinstance(p, tuple) else p for p in pdf.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
124 pdf.to_csv(args.output, sep='\t', float_format='%0.6f', header=pdf_cols)
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
125
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
126
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
127 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
128 __main__()