comparison tools/peak_calling/ccat_wrapper.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9071e359b9a3
1 import sys, subprocess, tempfile, shutil, os.path
2
3 CCAT_BINARY = "CCAT"
4
5 def get_top_count( filename ):
6 for line in open( filename ):
7 if line.startswith( 'outputNum' ):
8 return int( line.split()[-1].strip() )
9
10 def stop_err( tmp_dir, exception ):
11 print >> sys.stderr, "Error running CCAT."
12 shutil.rmtree( tmp_dir ) #some error has occurred, provide info and remove possibly non-empty temp directory
13 raise exception
14
15 def main():
16 input_tag_file = sys.argv[1]
17 input_control_file = sys.argv[2]
18 chrom_info_file = sys.argv[3]
19 input_config_file = sys.argv[4]
20 project_name = sys.argv[5]
21 output_peak_file = sys.argv[6]
22 output_region_file = sys.argv[7]
23 output_top_file = sys.argv[8]
24 output_log_file = sys.argv[9]
25
26 tmp_dir = tempfile.mkdtemp()
27 try:
28 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 )
29 proc.wait()
30 if proc.returncode:
31 raise Exception( "Error code: %i" % proc.returncode )
32 output_num = get_top_count( input_config_file )
33 shutil.move( os.path.join( tmp_dir, "%s.significant.peak" % project_name ), output_peak_file )
34 shutil.move( os.path.join( tmp_dir, "%s.significant.region" % project_name ), output_region_file )
35 shutil.move( os.path.join( tmp_dir, "%s.top%i.peak" % ( project_name, output_num ) ), output_top_file )
36 except Exception, e:
37 return stop_err( tmp_dir, e )
38 os.rmdir( tmp_dir ) #clean up empty temp working directory
39
40 if __name__ == "__main__": main()