annotate SNV/annotate.py @ 7:351b3acadd17 default tip

Uploaded
author ryanmorin
date Tue, 18 Oct 2011 18:33:15 -0400
parents 74f5ea818cea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
1 #!/usr/bin/env python
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
2
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
3 """
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
4 Annotates filtered SNVMix output that has been attached to codon information (outputs the same data with additional columns describing the predicted effect of the SNV)
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
5
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
6 usage: %prog [options]
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
7 -i, --input1=i: bam file
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
8 -o, --output1=o: Output pileup
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
9 """
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
10
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
11 import os, shutil, subprocess, sys, tempfile
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
12 from galaxy import eggs
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
13 import pkg_resources; pkg_resources.require( "bx-python" )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
14 from bx.cookbook import doc_optparse
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
15
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
16
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
17
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
18 def stop_err( msg ):
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
19 sys.stderr.write( '%s\n' % msg )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
20 sys.exit()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
21
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
22 def check_seq_file( dbkey, GALAXY_DATA_INDEX_DIR ):
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
23 seqFile = '%s/sam_fa_indices.loc' % GALAXY_DATA_INDEX_DIR
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
24 seqPath = ''
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
25 for line in open( seqFile ):
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
26 line = line.rstrip( '\r\n' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
27 if line and not line.startswith( '#' ) and line.startswith( 'index' ):
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
28 fields = line.split( '\t' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
29 if len( fields ) < 3:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
30 continue
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
31 if fields[1] == dbkey:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
32 seqPath = fields[2].strip()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
33 break
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
34 return seqPath
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
35
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
36 def __main__():
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
37 #Parse Command Line
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
38 options, args = doc_optparse.parse( __doc__ )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
39 #make temp dir
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
40 tmpDir = tempfile.mkdtemp()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
41
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
42 cmd = 'identify_nonsynonymous_mutations.pl < %s > %s'
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
43 try:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
44 # have to nest try-except in try-finally to handle 2.4
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
45 try:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
46 cmd = cmd % ( options.input1, options.output1 )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
47 print(cmd, "\n")
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
48 tmp = tempfile.NamedTemporaryFile( dir=tmpDir ).name
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
49 tmp_stderr = open( tmp, 'wb' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
50
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
51 proc = subprocess.Popen( args=cmd, shell=True, cwd=tmpDir, stderr=tmp_stderr.fileno() )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
52 returncode = proc.wait()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
53 tmp_stderr.close()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
54 #did it succeed?
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
55 # get stderr, allowing for case where it's very large
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
56 tmp_stderr = open( tmp, 'rb' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
57 stderr = ''
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
58 try:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
59 while True:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
60 stderr += tmp_stderr.read( )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
61 if not stderr:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
62 break
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
63 except OverflowError:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
64 pass
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
65 tmp_stderr.close()
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
66 if returncode != 0:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
67 raise Exception, stderr
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
68 except Exception, e:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
69 stop_err( 'Error running annotator tool\n' + str( e ) )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
70 finally:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
71 #clean up temp files
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
72 if os.path.exists( tmpDir ):
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
73 shutil.rmtree( tmpDir )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
74 # check that there are results in the output file
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
75 if os.path.getsize( options.output1 ) > 0:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
76 sys.stdout.write( 'wrote annotated output' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
77 else:
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
78 stop_err( 'The output file is empty' )
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
79
74f5ea818cea Uploaded
ryanmorin
parents:
diff changeset
80 if __name__ == "__main__" : __main__()