annotate test-data/util/shrink_simple_tab.py @ 4:9aad9ef0502c draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
author iuc
date Fri, 14 Dec 2018 12:41:48 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
9aad9ef0502c 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
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
2
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
3 import argparse
9aad9ef0502c 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
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
5
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
6
9aad9ef0502c 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):
9aad9ef0502c 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')
9aad9ef0502c 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
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
10 if (
9aad9ef0502c 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]
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
12 ) or (
9aad9ef0502c 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]
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
14 ):
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
15 return True
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
16
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
17
9aad9ef0502c 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):
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
19 print(infile, '->', ofile)
9aad9ef0502c 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:
9aad9ef0502c 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:
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
22 # copy header lines
9aad9ef0502c 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):
9aad9ef0502c 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))
9aad9ef0502c 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:
9aad9ef0502c 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):
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
27 o.write(line)
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
28
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
29
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
30 if __name__ == '__main__':
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
31 p = argparse.ArgumentParser()
9aad9ef0502c 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')
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
33 p.add_argument(
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
34 '-r', '--region',
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
35 required=True,
9aad9ef0502c 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'
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
37 )
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
38 p.add_argument(
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
39 '-o', '--ofile',
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
40 required=True,
9aad9ef0502c 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"
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
42 )
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
43 p.add_argument(
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
44 '-c', '--cols',
9aad9ef0502c 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,
9aad9ef0502c 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, "
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
47 "respectively"
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
48 )
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
49 p.add_argument(
9aad9ef0502c 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',
9aad9ef0502c 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,
9aad9ef0502c 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 '
9aad9ef0502c 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.'
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
54 )
9aad9ef0502c 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())
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
56
9aad9ef0502c 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(':')
9aad9ef0502c 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('-')]
9aad9ef0502c 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)
9aad9ef0502c planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
iuc
parents:
diff changeset
60
9aad9ef0502c 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'])