PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/liftover/bedLiftover_wrapper.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 70 lines | 48 code | 8 blank | 14 comment | 11 complexity | bc350105dddeea0035e33e9859e8a2bc MD5 | raw file
  1. #!/usr/bin/env python
  2. #Guruprasad Ananda
  3. """
  4. Converts coordinates from one build/assembly to another using liftOver binary and mapping files downloaded from UCSC.
  5. """
  6. import os, string, subprocess, sys
  7. import tempfile
  8. import re
  9. assert sys.version_info[:2] >= ( 2, 4 )
  10. def stop_err(msg):
  11. sys.stderr.write(msg)
  12. sys.exit()
  13. def safe_bed_file(infile):
  14. """Make a BED file with track and browser lines ready for liftOver.
  15. liftOver will fail with track or browser lines. We can make it happy
  16. by converting these to comments. See:
  17. https://lists.soe.ucsc.edu/pipermail/genome/2007-May/013561.html
  18. """
  19. fix_pat = re.compile("^(track|browser)")
  20. (fd, fname) = tempfile.mkstemp()
  21. in_handle = open(infile)
  22. out_handle = open(fname, "w")
  23. for line in in_handle:
  24. if fix_pat.match(line):
  25. line = "#" + line
  26. out_handle.write(line)
  27. in_handle.close()
  28. out_handle.close()
  29. return fname
  30. if len( sys.argv ) != 7:
  31. stop_err( "USAGE: prog input out_file1 out_file2 input_dbkey output_dbkey minMatch" )
  32. infile = sys.argv[1]
  33. outfile1 = sys.argv[2]
  34. outfile2 = sys.argv[3]
  35. in_dbkey = sys.argv[4]
  36. mapfilepath = sys.argv[5]
  37. minMatch = sys.argv[6]
  38. try:
  39. assert float(minMatch)
  40. except:
  41. minMatch = 0.1
  42. #ensure dbkey is set
  43. if in_dbkey == "?":
  44. stop_err( "Input dataset genome build unspecified, click the pencil icon in the history item to specify it." )
  45. if not os.path.isfile( mapfilepath ):
  46. stop_err( "%s mapping is not currently available." % ( mapfilepath.split('/')[-1].split('.')[0] ) )
  47. safe_infile = safe_bed_file(infile)
  48. cmd_line = "liftOver -minMatch=" + str(minMatch) + " " + safe_infile + " " + mapfilepath + " " + outfile1 + " " + outfile2 + " > /dev/null"
  49. try:
  50. # have to nest try-except in try-finally to handle 2.4
  51. try:
  52. proc = subprocess.Popen( args=cmd_line, shell=True, stderr=subprocess.PIPE )
  53. returncode = proc.wait()
  54. stderr = proc.stderr.read()
  55. if returncode != 0:
  56. raise Exception, stderr
  57. except Exception, e:
  58. raise Exception, 'Exception caught attempting conversion: ' + str( e )
  59. finally:
  60. os.remove(safe_infile)