comparison test-data/util/shrink_tabix.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
comparison
equal deleted inserted replaced
3:40f85b45e472 4:96a2d270bf0f
1 from __future__ import print_function
2
3 import argparse
4
5 import pysam
6
7
8 def main(infile, ofile, region):
9 print(infile, '->', ofile)
10 with pysam.Tabixfile(infile) as i:
11 fformat = i.format.lower()
12 if fformat == 'sam':
13 fformat = 'bed'
14 if ofile[-3:] == '.gz':
15 ofile = ofile[:-3]
16 with open(ofile, 'w') as o:
17 try:
18 region_it = i.fetch(region=region)
19 except ValueError:
20 region_it = i.fetch(region='chr' + region)
21 for line in i.header:
22 o.write(line + '\n')
23 for line in region_it:
24 o.write(str(line) + '\n')
25 pysam.tabix_index(ofile, preset=fformat, force=True)
26
27
28 if __name__ == '__main__':
29 p = argparse.ArgumentParser()
30 p.add_argument('infile')
31 p.add_argument(
32 '-r', '--region',
33 required=True,
34 help='the region of the input file to rewrite'
35 )
36 p.add_argument(
37 '-o', '--ofile',
38 required=True,
39 help="the name of the output file"
40 )
41 args = vars(p.parse_args())
42 main(**args)