diff test-data/util/shrink_tabix.py @ 4:69b5327a1f88 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gemini commit 283362494058ed64143b1f27afb447b8a1cb4313
author iuc
date Fri, 14 Dec 2018 13:02:36 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/util/shrink_tabix.py	Fri Dec 14 13:02:36 2018 -0500
@@ -0,0 +1,42 @@
+from __future__ import print_function
+
+import argparse
+
+import pysam
+
+
+def main(infile, ofile, region):
+    print(infile, '->', ofile)
+    with pysam.Tabixfile(infile) as i:
+        fformat = i.format.lower()
+        if fformat == 'sam':
+            fformat = 'bed'
+        if ofile[-3:] == '.gz':
+            ofile = ofile[:-3]
+        with open(ofile, 'w') as o:
+            try:
+                region_it = i.fetch(region=region)
+            except ValueError:
+                region_it = i.fetch(region='chr' + region)
+            for line in i.header:
+                o.write(line + '\n')
+            for line in region_it:
+                o.write(str(line) + '\n')
+    pysam.tabix_index(ofile, preset=fformat, force=True)
+
+
+if __name__ == '__main__':
+    p = argparse.ArgumentParser()
+    p.add_argument('infile')
+    p.add_argument(
+        '-r', '--region',
+        required=True,
+        help='the region of the input file to rewrite'
+    )
+    p.add_argument(
+        '-o', '--ofile',
+        required=True,
+        help="the name of the output file"
+    )
+    args = vars(p.parse_args())
+    main(**args)