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