annotate ccat_wrapper.py @ 1:bbfd1fbff28a draft

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