0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import sys, subprocess, tempfile, shutil, os.path
|
|
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 CCAT_BINARY = "/usr/bin/ccat/CCAT3.0/bin/CCAT"
|
|
17 input_tag_file = sys.argv[1]
|
|
18 input_control_file = sys.argv[2]
|
|
19 genome = sys.argv[3]
|
|
20 input_config_file = sys.argv[4]
|
|
21 project_name = sys.argv[5]
|
|
22 output_peak_file = sys.argv[6]
|
|
23 output_region_file = sys.argv[7]
|
|
24 output_top_file = sys.argv[8]
|
|
25 output_log_file = sys.argv[9]
|
|
26 root_dir = sys.argv[10]
|
|
27
|
|
28 ###### CREATE ANNOTATION FILES #########
|
|
29 databasePath = root_dir+"/database/files"
|
|
30
|
|
31 subprocess.Popen('mkdir -p '+databasePath+'/nebulaAnnotations', shell=True)
|
|
32 subprocess.Popen('mkdir -p '+databasePath+'/nebulaAnnotations/'+genome, shell=True)
|
|
33
|
|
34 nebulaGenomePath=databasePath+"/nebulaAnnotations/"+genome
|
|
35
|
|
36 FAIFILE='n'
|
|
37 LENFILE='n'
|
|
38 DICTFILE='n'
|
|
39 CHROFILE='n'
|
|
40 MAPFILE='n'
|
|
41
|
|
42 if not os.path.isfile(nebulaGenomePath+"/"+genome+".len"):
|
|
43 FAIFILE='y'
|
|
44 LENFILE='y'
|
|
45
|
|
46 FILEPATH=databasePath.replace("database/files","tool-data")
|
|
47
|
|
48 cmd='bash /usr/bin/create_annotation_files.sh '+FAIFILE+" "+LENFILE+" "+DICTFILE+" "+CHROFILE+" "+FILEPATH+" "+genome+" "+MAPFILE+" "+nebulaGenomePath
|
|
49 process=subprocess.Popen(cmd, shell=True)
|
|
50 process.wait()
|
|
51
|
|
52 chrom_info_file=nebulaGenomePath+"/"+genome+".len"
|
|
53
|
|
54 tmp_dir = tempfile.mkdtemp()
|
|
55 try:
|
|
56 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 )
|
|
57 proc.wait()
|
|
58 if proc.returncode:
|
|
59 raise Exception( "Error code: %i" % proc.returncode )
|
|
60 output_num = get_top_count( input_config_file )
|
|
61 shutil.move( os.path.join( tmp_dir, "%s.significant.peak" % project_name ), output_peak_file )
|
|
62 shutil.move( os.path.join( tmp_dir, "%s.significant.region" % project_name ), output_region_file )
|
|
63 shutil.move( os.path.join( tmp_dir, "%s.top%i.peak" % ( project_name, output_num ) ), output_top_file )
|
|
64 except Exception, e:
|
|
65 return stop_err( tmp_dir, e )
|
|
66 os.rmdir( tmp_dir ) #clean up empty temp working directory
|
|
67
|
|
68 if __name__ == "__main__": main()
|