annotate ccat_wrapper.py @ 0:a0c8dc671a23 draft

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