comparison panama_run.py @ 8:168b9ff24bcb default tip

Merge multiple heads.
author Dave Bouvier <dave@bx.psu.edu>
date Mon, 27 Jan 2014 14:07:41 -0500
parents bef89654259a 88a4d4c31bb6
children
comparison
equal deleted inserted replaced
7:bef89654259a 8:168b9ff24bcb
1 #!/usr/bin/env python
2 """
3 Run panama on expression and snp data
4 usage: panama_run.py [options]
5 --exp_data: Expression data CSV file
6 --snp_data: SNP data file
7 --output1: Output CSV dataset
8 """
9
10 import optparse, os, sys, subprocess, tempfile, shutil
11 from galaxy import eggs
12 import pkg_resources; pkg_resources.require( "bx-python" )
13 from bx.cookbook import doc_optparse
14 from galaxy import util
15
16 def stop_err( msg ):
17 sys.stderr.write( '%s\n' % msg )
18 sys.exit()
19
20
21
22 def __main__():
23 #Parse Command Line
24 parser = optparse.OptionParser()
25 parser.add_option( '', '--exp_data', dest='exp_data', help='The input Expression dataset')
26 parser.add_option( '', '--snp_data', dest='snp_data', help='The input snp dataset' )
27 parser.add_option( '', '--output1', dest='output1', help='The output CSV dataset' )
28 ( options, args ) = parser.parse_args()
29
30 try:
31 tmp_dir = tempfile.mkdtemp()
32 panama_output_file_name = os.path.join(tmp_dir, "PANAMA_results.csv")
33 command = 'panama %s %s -d %s' %(options.exp_data,options.snp_data,tmp_dir)
34 print command
35 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
36 tmp_stderr = open( tmp, 'wb' )
37 proc = subprocess.Popen( args=command, shell=True, stderr=tmp_stderr.fileno() )
38 returncode = proc.wait()
39 tmp_stderr.close()
40 # get stderr, allowing for case where it's very large
41 tmp_stderr = open( tmp, 'rb' )
42 stderr = ''
43 buffsize = 1048576
44 try:
45 while True:
46 stderr += tmp_stderr.read( buffsize )
47 if not stderr or len( stderr ) % buffsize != 0:
48 break
49 except OverflowError:
50 pass
51 tmp_stderr.close()
52 if returncode != 0:
53 raise Exception, stderr
54 except Exception, e:
55 #clean up temp files
56 print e
57 if os.path.exists( tmp_dir ):
58 shutil.rmtree( tmp_dir )
59 stop_err( 'Error opening panama')
60 # Move tmp_aligns_file_name to our output dataset location
61 shutil.move( panama_output_file_name, options.output1 )
62 #clean up temp files
63 if os.path.exists( tmp_dir ):
64 shutil.rmtree( tmp_dir )
65 # check that there are results in the output file
66 if os.path.getsize( options.output1 ) > 0:
67 sys.stdout.write( 'PANAM results generated' )
68 else:
69 stop_err( 'Error generating PANAMA results' )
70
71 if __name__=="__main__": __main__()