annotate test-data/util/shrink_simple_tab.py @ 4:96a2d270bf0f draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
author iuc
date Fri, 14 Dec 2018 12:58:00 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
1 from __future__ import print_function
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
2
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
3 import argparse
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
4 from functools import partial
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
5
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
6
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
7 def keep_line(line, pos_cols, region):
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
8 fields = line.rstrip().split(b'\t')
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
9 if fields[pos_cols[0]] == region[0]: # same chromosome
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
10 if (
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
11 region[1] < int(fields[pos_cols[1]]) < region[2]
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
12 ) or (
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
13 region[1] < int(fields[pos_cols[2]]) < region[2]
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
14 ):
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
15 return True
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
16
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
17
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
18 def main(infile, ofile, num_header_lines):
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
19 print(infile, '->', ofile)
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
20 with open(infile, 'rb') as i:
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
21 with open(ofile, 'wb') as o:
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
22 # copy header lines
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
23 for c in range(num_header_lines):
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
24 o.write(next(i))
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
25 for line in i:
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
26 if keep_line(line):
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
27 o.write(line)
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
28
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
29
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
30 if __name__ == '__main__':
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
31 p = argparse.ArgumentParser()
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
32 p.add_argument('infile')
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
33 p.add_argument(
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
34 '-r', '--region',
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
35 required=True,
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
36 help='the region of the input file to rewrite'
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
37 )
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
38 p.add_argument(
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
39 '-o', '--ofile',
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
40 required=True,
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
41 help="the name of the output file"
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
42 )
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
43 p.add_argument(
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
44 '-c', '--cols',
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
45 nargs=3, type=int, required=True,
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
46 help="the columns of the input file specifying chrom, start and stop, "
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
47 "respectively"
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
48 )
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
49 p.add_argument(
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
50 '-n', '--num-header-lines',
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
51 type=int, default=0,
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
52 help='the number of header lines present in the input; These will '
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
53 'always be copied over to the new file.'
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
54 )
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
55 args = vars(p.parse_args())
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
56
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
57 chrom, reg = args['region'].split(':')
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
58 region = [chrom.encode()] + [int(x) for x in reg.split('-')]
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
59 keep_line = partial(keep_line, pos_cols=args['cols'], region=region)
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
60
96a2d270bf0f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
61 main(args['infile'], args['ofile'], args['num_header_lines'])