annotate tools/ngs_simulation/ngs_simulation.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Runs Ben's simulation.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 usage: %prog [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -i, --input=i: Input genome (FASTA format)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -g, --genome=g: If built-in, the genome being used
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -l, --read_len=l: Read length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -c, --avg_coverage=c: Average coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -e, --error_rate=e: Error rate (0-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -n, --num_sims=n: Number of simulations to run
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -p, --polymorphism=p: Frequency/ies for minor allele (comma-separate list of 0-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 -d, --detection_thresh=d: Detection thresholds (comma-separate list of 0-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 -p, --output_png=p: Plot output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 -s, --summary_out=s: Whether or not to output a file with summary of all simulations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 -m, --output_summary=m: File name for output summary of all simulations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 -f, --new_file_path=f: Directory for summary output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 # removed output of all simulation results on request (not working)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 # -r, --sim_results=r: Output all tabular simulation results (number of polymorphisms times number of detection thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 # -o, --output=o: Base name for summary output for each run
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 import os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 import random, sys, tempfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 sys.stderr.write( '%s\n' % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 # validate parameters
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 error = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 read_len = int( options.read_len )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if read_len <= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 raise Exception, ' greater than 0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 except TypeError, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 error = ': %s' % str( e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 stop_err( 'Make sure your number of reads is an integer value%s' % error )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 error = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 avg_coverage = int( options.avg_coverage )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if avg_coverage <= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 raise Exception, ' greater than 0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 error = ': %s' % str( e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 stop_err( 'Make sure your average coverage is an integer value%s' % error )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 error = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 error_rate = float( options.error_rate )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if error_rate >= 1.0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 error_rate = 10 ** ( -error_rate / 10.0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 elif error_rate < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 raise Exception, ' between 0 and 1'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 error = ': %s' % str( e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 stop_err( 'Make sure the error rate is a decimal value%s or the quality score is at least 1' % error )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 num_sims = int( options.num_sims )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 except TypeError, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 stop_err( 'Make sure the number of simulations is an integer value: %s' % str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if len( options.polymorphism ) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 polymorphisms = [ float( p ) for p in options.polymorphism.split( ',' ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 stop_err( 'Select at least one polymorphism value to use' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if len( options.detection_thresh ) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 detection_threshes = [ float( dt ) for dt in options.detection_thresh.split( ',' ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 stop_err( 'Select at least one detection threshold to use' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 # mutation dictionaries
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 hp_dict = { 'A':'G', 'G':'A', 'C':'T', 'T':'C', 'N':'N' } # heteroplasmy dictionary
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 mt_dict = { 'A':'C', 'C':'A', 'G':'T', 'T':'G', 'N':'N'} # misread dictionary
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 # read fasta file to seq string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 all_lines = open( options.input, 'rb' ).readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 seq = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 for line in all_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 line = line.rstrip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 if line.startswith('>'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 seq += line.upper()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 seq_len = len( seq )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 # output file name template
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 # removed output of all simulation results on request (not working)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 # if options.sim_results == "true":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 # out_name_template = os.path.join( options.new_file_path, 'primary_output%s_' + options.output + '_visible_tabular' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 # else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 # out_name_template = tempfile.NamedTemporaryFile().name + '_%s'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 out_name_template = tempfile.NamedTemporaryFile().name + '_%s'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 print 'out_name_template:', out_name_template
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 # set up output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 outputs = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 i = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 for p in polymorphisms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 outputs[ p ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 for d in detection_threshes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 outputs[ p ][ d ] = out_name_template % i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 i += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 # run sims
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 for polymorphism in polymorphisms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 for detection_thresh in detection_threshes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 output = open( outputs[ polymorphism ][ detection_thresh ], 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 output.write( 'FP\tFN\tGENOMESIZE=%s\n' % seq_len )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 sim_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 while sim_count < num_sims:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 # randomly pick heteroplasmic base index
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 hbase = random.choice( range( 0, seq_len ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 #hbase = seq_len/2#random.randrange( 0, seq_len )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 # create 2D quasispecies list
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 qspec = map( lambda x: [], [0] * seq_len )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 # simulate read indices and assign to quasispecies
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 while i < ( avg_coverage * ( seq_len / read_len ) ): # number of reads (approximates coverage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 start = random.choice( range( 0, seq_len ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 #start = seq_len/2#random.randrange( 0, seq_len ) # assign read start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if random.random() < 0.5: # positive sense read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 end = start + read_len # assign read end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if end > seq_len: # overshooting origin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 read = range( start, seq_len ) + range( 0, ( end - seq_len ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 else: # regular read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 read = range( start, end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 else: # negative sense read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 end = start - read_len # assign read end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if end < -1: # overshooting origin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 read = range( start, -1, -1) + range( ( seq_len - 1 ), ( seq_len + end ), -1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 else: # regular read
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 read = range( start, end, -1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 # assign read to quasispecies list by index
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 for j in read:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 if j == hbase and random.random() < polymorphism: # heteroplasmic base is variant with p = het
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 ref = hp_dict[ seq[ j ] ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 else: # ref is the verbatim reference nucleotide (all positions)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 ref = seq[ j ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 if random.random() < error_rate: # base in read is misread with p = err
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 qspec[ j ].append( mt_dict[ ref ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 else: # otherwise we carry ref through to the end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 qspec[ j ].append(ref)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 # last but not least
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 i += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 bases, fpos, fneg = {}, 0, 0 # last two will be outputted to summary file later
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 for i, nuc in enumerate( seq ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 cov = len( qspec[ i ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 bases[ 'A' ] = qspec[ i ].count( 'A' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 bases[ 'C' ] = qspec[ i ].count( 'C' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 bases[ 'G' ] = qspec[ i ].count( 'G' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 bases[ 'T' ] = qspec[ i ].count( 'T' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 # calculate max NON-REF deviation
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 del bases[ nuc ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 maxdev = float( max( bases.values() ) ) / cov
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 # deal with non-het sites
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 if i != hbase:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 if maxdev >= detection_thresh: # greater than detection threshold = false positive
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 fpos += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 # deal with het sites
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 if i == hbase:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 hnuc = hp_dict[ nuc ] # let's recover het variant
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 if ( float( bases[ hnuc ] ) / cov ) < detection_thresh: # less than detection threshold = false negative
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 fneg += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 del bases[ hnuc ] # ignore het variant
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 maxdev = float( max( bases.values() ) ) / cov # check other non-ref bases at het site
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 if maxdev >= detection_thresh: # greater than detection threshold = false positive (possible)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 fpos += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 # output error sums and genome size to summary file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 output.write( '%d\t%d\n' % ( fpos, fneg ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 sim_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 # close output up
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 output.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 # Parameters (heteroplasmy, error threshold, colours)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 het=c(%s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 err=c(%s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 grade = (0:32)/32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 hues = rev(gray(grade))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 ''' % ( ','.join( [ str( p ) for p in polymorphisms ] ), ','.join( [ str( d ) for d in detection_threshes ] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 # Suppress warnings
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 r( 'options(warn=-1)' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 # Create allsum (for FP) and allneg (for FN) objects
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 r( 'allsum <- data.frame()' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 for polymorphism in polymorphisms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 for detection_thresh in detection_threshes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 output = outputs[ polymorphism ][ detection_thresh ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 cmd = '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 ngsum = read.delim('%s', header=T)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 ngsum$fprate <- ngsum$FP/%s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 ngsum$hetcol <- %s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 ngsum$errcol <- %s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 allsum <- rbind(allsum, ngsum)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 ''' % ( output, seq_len, polymorphism, detection_thresh )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 r( cmd )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 if os.path.getsize( output ) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 for p in outputs.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 for d in outputs[ p ].keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 sys.stderr.write(outputs[ p ][ d ] + ' '+str( os.path.getsize( outputs[ p ][ d ] ) )+'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 if options.summary_out == "true":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 r( 'write.table(summary(ngsum), file="%s", quote=FALSE, sep="\t", row.names=FALSE)' % options.output_summary )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 # Summary objects (these could be printed)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 tr_pos <- tapply(allsum$fprate,list(allsum$hetcol,allsum$errcol), mean)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 tr_neg <- tapply(allsum$FN,list(allsum$hetcol,allsum$errcol), mean)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 cat('\nFalse Positive Rate Summary\n\t', file='%s', append=T, sep='\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 write.table(format(tr_pos, digits=4), file='%s', append=T, quote=F, sep='\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 cat('\nFalse Negative Rate Summary\n\t', file='%s', append=T, sep='\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 write.table(format(tr_neg, digits=4), file='%s', append=T, quote=F, sep='\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 ''' % tuple( [ options.output_summary ] * 4 ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 # Setup graphs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 #pdf(paste(prefix,'_jointgraph.pdf',sep=''), 15, 10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 png('%s', width=800, height=500, units='px', res=250)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 layout(matrix(data=c(1,2,1,3,1,4), nrow=2, ncol=3), widths=c(4,6,2), heights=c(1,10,10))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 ''' % options.output_png )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 # Main title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 genome = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 if options.genome:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 genome = '%s: ' % options.genome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 par(mar=c(0,0,0,0))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 plot(1, type='n', axes=F, xlab='', ylab='')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 text(1,1,paste('%sVariation in False Positives and Negatives (', %s, ' simulations, coverage ', %s,')', sep=''), font=2, family='sans', cex=0.7)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 ''' % ( genome, options.num_sims, options.avg_coverage ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 # False positive boxplot
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 par(mar=c(5,4,2,2), las=1, cex=0.35)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 boxplot(allsum$fprate ~ allsum$errcol, horizontal=T, ylim=rev(range(allsum$fprate)), cex.axis=0.85)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 title(main='False Positives', xlab='false positive rate', ylab='')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 ''' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 # False negative heatmap (note zlim command!)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 num_polys = len( polymorphisms )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 num_dets = len( detection_threshes )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 par(mar=c(5,4,2,1), las=1, cex=0.35)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 image(1:%s, 1:%s, tr_neg, zlim=c(0,1), col=hues, xlab='', ylab='', axes=F, border=1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 axis(1, at=1:%s, labels=rownames(tr_neg), lwd=1, cex.axis=0.85, axs='i')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 axis(2, at=1:%s, labels=colnames(tr_neg), lwd=1, cex.axis=0.85)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 title(main='False Negatives', xlab='minor allele frequency', ylab='detection threshold')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 ''' % ( num_polys, num_dets, num_polys, num_dets ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 # Scale alongside
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 par(mar=c(2,2,2,3), las=1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 image(1, grade, matrix(grade, ncol=length(grade), nrow=1), col=hues, xlab='', ylab='', xaxt='n', las=1, cex.axis=0.85)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 title(main='Key', cex=0.35)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 mtext('false negative rate', side=1, cex=0.35)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 ''' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 # Close graphics
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 r( '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 layout(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 dev.off()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 ''' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 # Tidy up
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 # r( 'rm(folder,prefix,sim,cov,het,err,grade,hues,i,j,ngsum)' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 if __name__ == "__main__" : __main__()