Mercurial > repos > iuc > gemini_region
comparison test-data/util/shrink_simple_tab.py @ 4:601b70d10d92 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:44:11 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:33efe43c1dbb | 4:601b70d10d92 |
---|---|
1 from __future__ import print_function | |
2 | |
3 import argparse | |
4 from functools import partial | |
5 | |
6 | |
7 def keep_line(line, pos_cols, region): | |
8 fields = line.rstrip().split(b'\t') | |
9 if fields[pos_cols[0]] == region[0]: # same chromosome | |
10 if ( | |
11 region[1] < int(fields[pos_cols[1]]) < region[2] | |
12 ) or ( | |
13 region[1] < int(fields[pos_cols[2]]) < region[2] | |
14 ): | |
15 return True | |
16 | |
17 | |
18 def main(infile, ofile, num_header_lines): | |
19 print(infile, '->', ofile) | |
20 with open(infile, 'rb') as i: | |
21 with open(ofile, 'wb') as o: | |
22 # copy header lines | |
23 for c in range(num_header_lines): | |
24 o.write(next(i)) | |
25 for line in i: | |
26 if keep_line(line): | |
27 o.write(line) | |
28 | |
29 | |
30 if __name__ == '__main__': | |
31 p = argparse.ArgumentParser() | |
32 p.add_argument('infile') | |
33 p.add_argument( | |
34 '-r', '--region', | |
35 required=True, | |
36 help='the region of the input file to rewrite' | |
37 ) | |
38 p.add_argument( | |
39 '-o', '--ofile', | |
40 required=True, | |
41 help="the name of the output file" | |
42 ) | |
43 p.add_argument( | |
44 '-c', '--cols', | |
45 nargs=3, type=int, required=True, | |
46 help="the columns of the input file specifying chrom, start and stop, " | |
47 "respectively" | |
48 ) | |
49 p.add_argument( | |
50 '-n', '--num-header-lines', | |
51 type=int, default=0, | |
52 help='the number of header lines present in the input; These will ' | |
53 'always be copied over to the new file.' | |
54 ) | |
55 args = vars(p.parse_args()) | |
56 | |
57 chrom, reg = args['region'].split(':') | |
58 region = [chrom.encode()] + [int(x) for x in reg.split('-')] | |
59 keep_line = partial(keep_line, pos_cols=args['cols'], region=region) | |
60 | |
61 main(args['infile'], args['ofile'], args['num_header_lines']) |