PageRenderTime 35ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/metag_tools/rmap_wrapper.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 88 lines | 77 code | 8 blank | 3 comment | 4 complexity | 9c95495dbcc7f51a652bb344f89dd9c3 MD5 | raw file
  1. #!/usr/bin/env python
  2. import os, sys, tempfile
  3. assert sys.version_info[:2] >= (2.4)
  4. def stop_err( msg ):
  5. sys.stderr.write( "%s\n" % msg )
  6. sys.exit()
  7. def __main__():
  8. # I/O
  9. target_path = sys.argv[1]
  10. infile = sys.argv[2]
  11. read_len = sys.argv[3] # -w
  12. align_len = sys.argv[4] # -h
  13. mismatch = sys.argv[5] # -m
  14. output_file = sys.argv[6]
  15. # first guess the read length
  16. guess_read_len = 0
  17. seq = ''
  18. for i, line in enumerate(open(infile)):
  19. line = line.rstrip('\r\n')
  20. if line.startswith('>'):
  21. if seq:
  22. guess_read_len = len(seq)
  23. break
  24. else:
  25. seq += line
  26. try:
  27. test = int(read_len)
  28. if test == 0:
  29. read_len = str(guess_read_len)
  30. else:
  31. assert test >= 20 and test <= 64
  32. except:
  33. stop_err('Invalid value for read length. Must be between 20 and 64.')
  34. try:
  35. int(align_len)
  36. except:
  37. stop_err('Invalid value for minimal length of a hit.')
  38. try:
  39. int(mismatch)
  40. #assert test >= 0 and test <= int(0.1*int(read_len))
  41. except:
  42. stop_err('Invalid value for mismatch numbers in an alignment.')
  43. all_files = []
  44. if os.path.isdir(target_path):
  45. # check target genome
  46. fa_files = os.listdir(target_path)
  47. for file in fa_files:
  48. file = "%s/%s" % ( target_path, file )
  49. file = os.path.normpath(file)
  50. all_files.append(file)
  51. else:
  52. stop_err("No sequences for %s are available for search, please report this error." %(target_path))
  53. for detail_file_path in all_files:
  54. output_tempfile = tempfile.NamedTemporaryFile().name
  55. command = "rmap -h %s -w %s -m %s -c %s %s -o %s 2>&1" % ( align_len, read_len, mismatch, detail_file_path, infile, output_tempfile )
  56. #print command
  57. try:
  58. os.system( command )
  59. except Exception, e:
  60. stop_err( str( e ) )
  61. try:
  62. os.system( 'cat %s >> %s' % ( output_tempfile, output_file ) )
  63. except Exception, e:
  64. stop_err( str( e ) )
  65. try:
  66. os.remove( output_tempfile )
  67. except:
  68. pass
  69. if __name__ == '__main__': __main__()