/tools/liftover/bedLiftover_wrapper.py
Python | 70 lines | 48 code | 8 blank | 14 comment | 11 complexity | bc350105dddeea0035e33e9859e8a2bc MD5 | raw file
- #!/usr/bin/env python
- #Guruprasad Ananda
- """
- Converts coordinates from one build/assembly to another using liftOver binary and mapping files downloaded from UCSC.
- """
- import os, string, subprocess, sys
- import tempfile
- import re
- assert sys.version_info[:2] >= ( 2, 4 )
- def stop_err(msg):
- sys.stderr.write(msg)
- sys.exit()
- def safe_bed_file(infile):
- """Make a BED file with track and browser lines ready for liftOver.
- liftOver will fail with track or browser lines. We can make it happy
- by converting these to comments. See:
- https://lists.soe.ucsc.edu/pipermail/genome/2007-May/013561.html
- """
- fix_pat = re.compile("^(track|browser)")
- (fd, fname) = tempfile.mkstemp()
- in_handle = open(infile)
- out_handle = open(fname, "w")
- for line in in_handle:
- if fix_pat.match(line):
- line = "#" + line
- out_handle.write(line)
- in_handle.close()
- out_handle.close()
- return fname
-
- if len( sys.argv ) != 7:
- stop_err( "USAGE: prog input out_file1 out_file2 input_dbkey output_dbkey minMatch" )
- infile = sys.argv[1]
- outfile1 = sys.argv[2]
- outfile2 = sys.argv[3]
- in_dbkey = sys.argv[4]
- mapfilepath = sys.argv[5]
- minMatch = sys.argv[6]
- try:
- assert float(minMatch)
- except:
- minMatch = 0.1
- #ensure dbkey is set
- if in_dbkey == "?":
- stop_err( "Input dataset genome build unspecified, click the pencil icon in the history item to specify it." )
- if not os.path.isfile( mapfilepath ):
- stop_err( "%s mapping is not currently available." % ( mapfilepath.split('/')[-1].split('.')[0] ) )
- safe_infile = safe_bed_file(infile)
- cmd_line = "liftOver -minMatch=" + str(minMatch) + " " + safe_infile + " " + mapfilepath + " " + outfile1 + " " + outfile2 + " > /dev/null"
- try:
- # have to nest try-except in try-finally to handle 2.4
- try:
- proc = subprocess.Popen( args=cmd_line, shell=True, stderr=subprocess.PIPE )
- returncode = proc.wait()
- stderr = proc.stderr.read()
- if returncode != 0:
- raise Exception, stderr
- except Exception, e:
- raise Exception, 'Exception caught attempting conversion: ' + str( e )
- finally:
- os.remove(safe_infile)