annotate tabpad.py @ 0:2feaf3d2805b draft

"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
author iuc
date Tue, 10 Dec 2019 21:02:47 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
2
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
3 import argparse
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
4 import re
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
5
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
6
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
7 def padfile(infile, outfile, fieldcnt=None):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
8 with open(infile, 'r') as fh:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
9 out = open(outfile, 'w')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
10 commentlines = []
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
11 tabs = '\t' * fieldcnt if fieldcnt is not None else None
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
12
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
13 def pad_line(txtline, tabs=None):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
14 line = txtline.rstrip('\r\n')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
15 fields = line.split('\t')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
16 if not tabs:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
17 tabs = '\t' * len(fields)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
18 out.write('%s%s\n' % (line, tabs[len(fields):]))
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
19
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
20 for i, txtline in enumerate(fh):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
21 if txtline.lstrip().startswith('#'):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
22 commentlines.append(txtline)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
23 else:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
24 if commentlines:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
25 for i in range(len(commentlines) - 1):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
26 out.write(commentlines[i])
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
27 pad_line(commentlines[-1], tabs=tabs)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
28 commentlines = []
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
29 pad_line(txtline, tabs=tabs)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
30 out.close()
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
31
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
32
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
33 def fieldcount(infile):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
34 fieldcnt = 0
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
35 with open(infile, 'r') as fh:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
36 for i, line in enumerate(fh):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
37 fieldcnt = max(fieldcnt, len(line.rstrip('\r\n').split('\t')))
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
38 return fieldcnt
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
39
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
40
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
41 def tsvname(infile):
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
42 return re.sub('.txt$', '', infile) + '.tsv'
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
43
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
44
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
45 def __main__():
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
46 parser = argparse.ArgumentParser(
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
47 description='Pad a file with TABS for equal field size across lines')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
48 parser.add_argument(
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
49 '-i', '--input', help='input file')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
50 parser.add_argument(
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
51 '-o', '--output', help='output file')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
52 parser.add_argument(
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
53 'files', nargs='*', help='.txt files')
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
54 args = parser.parse_args()
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
55
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
56 if args.input:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
57 outfile = args.output if args.output else tsvname(args.input)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
58 fieldcnt = fieldcount(args.input)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
59 padfile(args.input, outfile, fieldcnt=fieldcnt)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
60 for infile in args.files:
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
61 outfile = tsvname(infile)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
62 fieldcnt = fieldcount(infile)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
63 padfile(infile, outfile, fieldcnt=fieldcnt)
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
64
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
65
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
66 if __name__ == "__main__":
2feaf3d2805b "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cat commit 863ad85836c80811d1d6b82eaf3ce903b273368a"
iuc
parents:
diff changeset
67 __main__()