annotate tools/peak_calling/ccat_wrapper.py @ 2:c2a356708570

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