annotate pandas_pivot_table.py @ 1:c02f59711eb6 draft

"planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit af9d36fa3efb1cf136a69e7ed1a5f06261f9b0d6-dirty"
author jjohnson
date Wed, 16 Dec 2020 16:13:51 +0000
parents 621144f8dbe9
children 6f05390deffa
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 )
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
61 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
62
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
63 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
64 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
65 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
66 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
67 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
68 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
69 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
70 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
71 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
72 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
73
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
74 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
75 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
76 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
77 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
78 try:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
79 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
80 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
81 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
82 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
83 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
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:
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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
106 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
107 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
108
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
109 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
110 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
111 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
112 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
113 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
114 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
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=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
118 else:
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 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
127 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
128 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
129 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
130 for p in pdf.columns.tolist()]
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.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
132
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
133
621144f8dbe9 "planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/pandas_pivot_table/ commit 80684939b0bf75abb5cc70a9878054c1f734b651-dirty"
jjohnson
parents:
diff changeset
134 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
135 __main__()