annotate sam_pileup.py @ 0:95612c159681

Uploaded tool tarball.
author devteam
date Mon, 26 Aug 2013 14:21:12 -0400
parents
children e7d863c5c5d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
1 #!/usr/bin/env python
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
2
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
3 """
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
4 Creates a pileup file from a bam file and a reference.
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
5
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
6 usage: %prog [options]
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
7 -p, --input1=p: bam file
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
8 -o, --output1=o: Output pileup
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
9 -R, --ref=R: Reference file type
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
10 -n, --ownFile=n: User-supplied fasta reference file
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
11 -d, --dbkey=d: dbkey of user-supplied file
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
12 -x, --indexDir=x: Index directory
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
13 -b, --bamIndex=b: BAM index file
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
14 -s, --lastCol=s: Print the mapping quality as the last column
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
15 -i, --indels=i: Only output lines containing indels
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
16 -M, --mapCap=M: Cap mapping quality
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
17 -c, --consensus=c: Call the consensus sequence using MAQ consensu model
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
18 -T, --theta=T: Theta paramter (error dependency coefficient)
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
19 -N, --hapNum=N: Number of haplotypes in sample
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
20 -r, --fraction=r: Expected fraction of differences between a pair of haplotypes
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
21 -I, --phredProb=I: Phred probability of an indel in sequencing/prep
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
22
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
23 """
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
24
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
25 import os, shutil, subprocess, sys, tempfile
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
26 from galaxy import eggs
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
27 import pkg_resources; pkg_resources.require( "bx-python" )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
28 from bx.cookbook import doc_optparse
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
29
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
30 def stop_err( msg ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
31 sys.stderr.write( '%s\n' % msg )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
32 sys.exit()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
33
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
34 def check_seq_file( dbkey, GALAXY_DATA_INDEX_DIR ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
35 seqFile = '%s/sam_fa_indices.loc' % GALAXY_DATA_INDEX_DIR
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
36 seqPath = ''
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
37 for line in open( seqFile ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
38 line = line.rstrip( '\r\n' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
39 if line and not line.startswith( '#' ) and line.startswith( 'index' ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
40 fields = line.split( '\t' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
41 if len( fields ) < 3:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
42 continue
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
43 if fields[1] == dbkey:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
44 seqPath = fields[2].strip()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
45 break
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
46 return seqPath
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
47
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
48 def __main__():
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
49 #Parse Command Line
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
50 options, args = doc_optparse.parse( __doc__ )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
51 seqPath = check_seq_file( options.dbkey, options.indexDir )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
52 # output version # of tool
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
53 try:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
54 tmp = tempfile.NamedTemporaryFile().name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
55 tmp_stdout = open( tmp, 'wb' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
56 proc = subprocess.Popen( args='samtools 2>&1', shell=True, stdout=tmp_stdout )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
57 tmp_stdout.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
58 returncode = proc.wait()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
59 stdout = None
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
60 for line in open( tmp_stdout.name, 'rb' ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
61 if line.lower().find( 'version' ) >= 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
62 stdout = line.strip()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
63 break
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
64 if stdout:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
65 sys.stdout.write( 'Samtools %s\n' % stdout )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
66 else:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
67 raise Exception
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
68 except:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
69 sys.stdout.write( 'Could not determine Samtools version\n' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
70 #prepare file names
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
71 tmpDir = tempfile.mkdtemp()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
72 tmpf0 = tempfile.NamedTemporaryFile( dir=tmpDir )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
73 tmpf0_name = tmpf0.name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
74 tmpf0.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
75 tmpf0bam_name = '%s.bam' % tmpf0_name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
76 tmpf0bambai_name = '%s.bam.bai' % tmpf0_name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
77 tmpf1 = tempfile.NamedTemporaryFile( dir=tmpDir )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
78 tmpf1_name = tmpf1.name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
79 tmpf1.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
80 tmpf1fai_name = '%s.fai' % tmpf1_name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
81 #link bam and bam index to working directory (can't move because need to leave original)
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
82 os.symlink( options.input1, tmpf0bam_name )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
83 os.symlink( options.bamIndex, tmpf0bambai_name )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
84 #get parameters for pileup command
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
85 if options.lastCol == 'yes':
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
86 lastCol = '-s'
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
87 else:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
88 lastCol = ''
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
89 if options.indels == 'yes':
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
90 indels = '-i'
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
91 else:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
92 indels = ''
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
93 opts = '%s %s -M %s' % ( lastCol, indels, options.mapCap )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
94 if options.consensus == 'yes':
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
95 opts += ' -c -T %s -N %s -r %s -I %s' % ( options.theta, options.hapNum, options.fraction, options.phredProb )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
96 #prepare basic pileup command
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
97 cmd = 'samtools pileup %s -f %s %s > %s'
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
98 try:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
99 # have to nest try-except in try-finally to handle 2.4
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
100 try:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
101 #index reference if necessary and prepare pileup command
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
102 if options.ref == 'indexed':
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
103 if not os.path.exists( "%s.fai" % seqPath ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
104 raise Exception, "No sequences are available for '%s', request them by reporting this error." % options.dbkey
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
105 cmd = cmd % ( opts, seqPath, tmpf0bam_name, options.output1 )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
106 elif options.ref == 'history':
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
107 os.symlink( options.ownFile, tmpf1_name )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
108 cmdIndex = 'samtools faidx %s' % ( tmpf1_name )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
109 tmp = tempfile.NamedTemporaryFile( dir=tmpDir ).name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
110 tmp_stderr = open( tmp, 'wb' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
111 proc = subprocess.Popen( args=cmdIndex, shell=True, cwd=tmpDir, stderr=tmp_stderr.fileno() )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
112 returncode = proc.wait()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
113 tmp_stderr.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
114 # get stderr, allowing for case where it's very large
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
115 tmp_stderr = open( tmp, 'rb' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
116 stderr = ''
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
117 buffsize = 1048576
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
118 try:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
119 while True:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
120 stderr += tmp_stderr.read( buffsize )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
121 if not stderr or len( stderr ) % buffsize != 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
122 break
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
123 except OverflowError:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
124 pass
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
125 tmp_stderr.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
126 #did index succeed?
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
127 if returncode != 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
128 raise Exception, 'Error creating index file\n' + stderr
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
129 cmd = cmd % ( opts, tmpf1_name, tmpf0bam_name, options.output1 )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
130 #perform pileup command
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
131 tmp = tempfile.NamedTemporaryFile( dir=tmpDir ).name
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
132 tmp_stderr = open( tmp, 'wb' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
133 proc = subprocess.Popen( args=cmd, shell=True, cwd=tmpDir, stderr=tmp_stderr.fileno() )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
134 returncode = proc.wait()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
135 tmp_stderr.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
136 #did it succeed?
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
137 # get stderr, allowing for case where it's very large
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
138 tmp_stderr = open( tmp, 'rb' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
139 stderr = ''
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
140 buffsize = 1048576
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
141 try:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
142 while True:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
143 stderr += tmp_stderr.read( buffsize )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
144 if not stderr or len( stderr ) % buffsize != 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
145 break
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
146 except OverflowError:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
147 pass
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
148 tmp_stderr.close()
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
149 if returncode != 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
150 raise Exception, stderr
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
151 except Exception, e:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
152 stop_err( 'Error running Samtools pileup tool\n' + str( e ) )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
153 finally:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
154 #clean up temp files
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
155 if os.path.exists( tmpDir ):
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
156 shutil.rmtree( tmpDir )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
157 # check that there are results in the output file
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
158 if os.path.getsize( options.output1 ) > 0:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
159 sys.stdout.write( 'Converted BAM to pileup' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
160 else:
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
161 stop_err( 'The output file is empty. Your input file may have had no matches, or there may be an error with your input file or settings.' )
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
162
95612c159681 Uploaded tool tarball.
devteam
parents:
diff changeset
163 if __name__ == "__main__" : __main__()