/tools/peak_calling/ccat_wrapper.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 41 lines · 35 code · 6 blank · 0 comment · 6 complexity · 2e2d7f62f405275c5b17be597e76d561 MD5 · raw file

  1. import sys, subprocess, tempfile, shutil, os.path
  2. CCAT_BINARY = "CCAT"
  3. def get_top_count( filename ):
  4. for line in open( filename ):
  5. if line.startswith( 'outputNum' ):
  6. return int( line.split()[-1].strip() )
  7. def stop_err( tmp_dir, exception ):
  8. print >> sys.stderr, "Error running CCAT."
  9. shutil.rmtree( tmp_dir ) #some error has occurred, provide info and remove possibly non-empty temp directory
  10. raise exception
  11. def main():
  12. input_tag_file = sys.argv[1]
  13. input_control_file = sys.argv[2]
  14. chrom_info_file = sys.argv[3]
  15. input_config_file = sys.argv[4]
  16. project_name = sys.argv[5]
  17. output_peak_file = sys.argv[6]
  18. output_region_file = sys.argv[7]
  19. output_top_file = sys.argv[8]
  20. output_log_file = sys.argv[9]
  21. tmp_dir = tempfile.mkdtemp()
  22. try:
  23. assert os.path.exists( chrom_info_file ), "The required chromosome length file does not exist."
  24. proc = subprocess.Popen( args="%s %s > %s" % ( CCAT_BINARY, " ".join( map( lambda x: "'%s'" % x, [ input_tag_file, input_control_file, chrom_info_file, input_config_file, project_name ] ) ), output_log_file ), shell=True, cwd=tmp_dir )
  25. proc.wait()
  26. if proc.returncode:
  27. raise Exception( "Error code: %i" % proc.returncode )
  28. output_num = get_top_count( input_config_file )
  29. shutil.move( os.path.join( tmp_dir, "%s.significant.peak" % project_name ), output_peak_file )
  30. shutil.move( os.path.join( tmp_dir, "%s.significant.region" % project_name ), output_region_file )
  31. shutil.move( os.path.join( tmp_dir, "%s.top%i.peak" % ( project_name, output_num ) ), output_top_file )
  32. except Exception, e:
  33. return stop_err( tmp_dir, e )
  34. os.rmdir( tmp_dir ) #clean up empty temp working directory
  35. if __name__ == "__main__": main()