-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickStats.py
More file actions
410 lines (331 loc) · 16.1 KB
/
Copy pathQuickStats.py
File metadata and controls
410 lines (331 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/python
#Copyright (c) 2016, Justin R. Klesmith
#All rights reserved.
#QuickStats: Get the statistics from a enrich run
from __future__ import division
from subprocess import check_output
from math import log
import StringIO
import argparse
import time
import os
__author__ = "Justin R. Klesmith"
__copyright__ = "Copyright 2016, Justin R. Klesmith"
__credits__ = ["Justin R. Klesmith", "Timothy A. Whitehead"]
__license__ = "BSD-3"
__version__ = "1.4X, Build: 201507X"
__maintainer__ = "Justin R. Klesmith"
__email__ = "klesmit3@msu.edu"
#Build Notes:
#1.3 - 20150616 - Fixed counting bug at the end of the tile in CodonSubs such that it's just less than and not equal to
#Get commandline arguments
parser = argparse.ArgumentParser(description='Quick Enrich Stats - Note: you must pre-normalize the data using QuickNormalize.py.')
parser.add_argument('-f', dest='file', action='store', help='File of your already normalized dataset')
parser.add_argument('-p', dest='path', action='store', help='What is the path to the enrich tile directory? ie: ./tile/')
parser.add_argument('-l', dest='tilelength', action='store', help='Tile length override')
parser.add_argument('-s', dest='tilestart', action='store', help='Tile start override')
args = parser.parse_args()
#Verify inputs
if args.file == None:
print "No normalized file given"
quit()
if args.path == None:
print "No enrich path given"
quit()
#Global vars
AA_Table = '*ACDEFGHIKLMNPQRSTVWY'
Mutations = {}
NumResi = 0 #Tile length
NormData = ""
StartResidue = 0
def Build_Matrix():
#Populate Mutation Dictionary with None Data
for j in xrange(0+StartResidue,NumResi+StartResidue):
for i in enumerate(AA_Table):
try:
#Mutations[ResID][MutID[1]][0 = NormLog2, 1 = Unselected, 2 = Selected]
Mutations[j][i[1]] = [None, None, None]
except KeyError:
Mutations[j] = {}
Mutations[j][i[1]] = [None, None, None]
return Mutations
def ImportNormData():
global NumResi
global NormData
global StartResidue
lines = 0
normdata = ""
#Import the previously normalized data
with open(args.file) as infile:
copy = False
for line in infile:
if line.strip() == "Location,Mutation,Normalized_ER,Unselected_Reads,Selected_Reads,RawLog2":
copy = True
elif line.strip() == "Normalized Heatmap":
copy = False
elif line.startswith("Tile Length: "):
if args.tilelength != None:
NumResi = int(args.tilelength)
else:
NumResi = int(line.strip()[13:])
print "Tile length: "+str(NumResi)
elif line.startswith("Start residue (-s): "):
split = line.split(" ")
if args.tilestart != None:
StartResidue = int(args.tilestart)
else:
StartResidue = int(split[3]) #Set the start residue
elif copy:
NormData = NormData + line
lines = lines + 1
#NumResi = int(lines / 21) #Set the tile length
return normdata
def PopulateMutArrays():
#Loop through the output
for line in StringIO.StringIO(NormData):
split = line.split(",")
location = int(split[0])
identity = str(split[1])
#Ignore if our location is above our number of residues
if location > (NumResi + StartResidue - 1):
print "Above Tile Length Reject: "+str(location)+"-"+str(identity)
continue
#Ignore if our location is below our number of residues
if location < StartResidue:
print "Below Tile Start Reject "+str(location)+"-"+str(identity)
continue
Mutations[location][identity][0] = split[2]
Mutations[location][identity][1] = split[3]
Mutations[location][identity][2] = split[4].rstrip('\n')
return Mutations
def DNAReads():
reads = {} #Initialize the variable for the number of reads 0=unsel, 1=sel
SC = 0
UC = 0
selectedcounts = ""
unselectedcounts = ""
if os.path.isfile(args.path+'data/output/counts_sel_example_F_N_include_filtered_B_DNA_qc'):
selectedcounts = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_sel_example_F_N_include_filtered_B_DNA_qc'])
elif os.path.isfile(args.path+'data/output/counts_sel_example_F_N_include_filtered_R1_DNA_qc'):
selectedcounts = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_sel_example_F_N_include_filtered_R1_DNA_qc'])
else:
print "Can't find selected DNA counts"
quit()
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc'):
unselectedcounts = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc'):
unselectedcounts = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc'])
else:
print "Can't find unselected DNA counts"
quit()
#Loop through the output
for line in StringIO.StringIO(selectedcounts):
split = line.split(" ")
SC = SC + int(split[0].rstrip('\n'))
for line in StringIO.StringIO(unselectedcounts):
split = line.split(" ")
UC = UC + int(split[0].rstrip('\n'))
reads[0] = str(UC) #Set the unselected reads
reads[1] = str(SC) #Set the selected reads
return reads
def MutationCounts():
muts = {}
NM00 = 0
NM10 = 0
NM15 = 0
NM30 = 0
NM50 = 0
NM100 = 0
FiveThreshold = 0
Retained = 0
for j in xrange(0+StartResidue,NumResi+StartResidue):
for i in enumerate(AA_Table):
if Mutations[j][i[1]][0] != "NS":
if float(Mutations[j][i[1]][0]) > 0.00:
NM00 += 1
if float(Mutations[j][i[1]][0]) > 0.10:
NM10 += 1
if float(Mutations[j][i[1]][0]) > 0.15:
NM15 += 1
if float(Mutations[j][i[1]][0]) > 0.30:
NM30 += 1
if float(Mutations[j][i[1]][0]) > 0.50:
NM50 += 1
if float(Mutations[j][i[1]][0]) > 1.00:
NM100 += 1
if Mutations[j][i[1]][1] != "None":
if int(Mutations[j][i[1]][1]) >= 5:
FiveThreshold += 1
if Mutations[j][i[1]][2] != "None":
Retained += 1
muts[0] = NM00
muts[1] = NM10
muts[2] = NM15
muts[3] = NM30
muts[4] = NM50
muts[5] = NM100
muts[6] = FiveThreshold
muts[7] = Retained
return muts
def Nonsynonymous():
reads = {}
Total = 0
Single = 0
WT = 0
ALL = ""
M1 = ""
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_PRO_qc'):
ALL = check_output(["awk", 'FNR>1{ print $1,$9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_PRO_qc'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_PRO_qc'):
ALL = check_output(["awk", 'FNR>1{ print $1,$9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_PRO_qc'])
else:
print "Unsel protein counts not found"
quit()
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_PRO_qc.m1'):
M1 = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_PRO_qc.m1'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_PRO_qc.m1'):
M1 = check_output(["awk", 'FNR>1{ print $9 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_PRO_qc.m1'])
else:
print "Unsel protein counts.m1 not found"
quit()
#Loop through the output
for line in StringIO.StringIO(ALL):
split = line.split(" ")
if split[0] == "NA-NA":
WT = int(split[1])
Total = Total + int(split[1].rstrip('\n'))
for line in StringIO.StringIO(M1):
split = line.split(" ")
Single = Single + int(split[0].rstrip('\n'))
reads[0] = WT #Wild-type
reads[1] = Single #.m1
reads[2] = (Total - Single - WT) #all - .m1 - WT
return reads
def CodonSubs():
codons = {}
One = 0
Two = 0
Three = 0
#Get the start of translation
TranslateStart = 0
TranslateEnd = 0
with open(args.path+'input/example_local_config') as infile:
for line in infile:
if line.startswith("<translate_start>"):
TSLen = len(line)
TranslateStart = int(line[17:TSLen-20])
TranslateEnd = TranslateStart+(3*NumResi)
ALL = ""
M1 = ""
M2 = ""
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc'):
ALL = check_output(["awk", 'FNR>1{ print $4,$5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc'):
ALL = check_output(["awk", 'FNR>1{ print $4,$5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc'])
else:
print "Counts unsel DNA not found."
quit()
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc.m1'):
M1 = check_output(["awk", 'FNR>1{ print $5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc.m1'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc.m1'):
M1 = check_output(["awk", 'FNR>1{ print $5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc.m1'])
else:
print "Counts unsel DNA.m1 not found."
quit()
if os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc.m2'):
M2 = check_output(["awk", 'FNR>1{ print $5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_B_DNA_qc.m2'])
elif os.path.isfile(args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc.m2'):
M2 = check_output(["awk", 'FNR>1{ print $5 }', args.path+'data/output/counts_unsel_example_F_N_include_filtered_R1_DNA_qc.m2'])
else:
print "Counts unsel DNA.m2 not found."
quit()
#Check for single base mutations
for line in StringIO.StringIO(M1):
split = line.split(" ")
if int(split[0]) >= TranslateStart and int(split[0]) < TranslateEnd: #Check to see that the base is in our tile
One = One + 1
#Check for double base mutations
for line in StringIO.StringIO(M2):
split2 = line.split(" ")
location = split2[0].split(",") #Get the individual mutation locations
if int(location[0]) >= TranslateStart and int(location[0]) < TranslateEnd: #Check to see that the base is in our tile
if int(location[1]) >= TranslateStart and int(location[1]) < TranslateEnd: #Check to see that the base is in our tile
codon1 = int((int(location[0]) - int(TranslateStart))/3)
codon2 = int((int(location[1]) - int(TranslateStart))/3)
if codon1 == codon2:
Two = Two + 1
#Check for triple base mutations
for line in StringIO.StringIO(ALL):
split3 = line.split(" ")
if split3[0] == "3": #Test to see that there are three mutations
location = split3[1].split(",") #Get the individual mutation locations
if int(location[0]) >= TranslateStart and int(location[0]) < TranslateEnd: #Check to see that the base is in our tile
if int(location[1]) >= TranslateStart and int(location[1]) < TranslateEnd: #Check to see that the base is in our tile
if int(location[2]) >= TranslateStart and int(location[2]) < TranslateEnd: #Check to see that the base is in our tile
codon1 = int((int(location[0]) - int(TranslateStart))/3)
codon2 = int((int(location[1]) - int(TranslateStart))/3)
codon3 = int((int(location[2]) - int(TranslateStart))/3)
if codon1 == codon2 and codon2 == codon3:
Three = Three + 1
codons[0] = One #1-base sub
codons[1] = Two #2-base sub
codons[2] = Three #3-base sub
return codons
def RunStats():
print "Stat run parameters:"
print time.strftime("%H:%M:%S")
print time.strftime("%m/%d/%Y")
print "Nomalized file: "+args.file
print "Data path: "+args.path
print "Tile length: "+str(NumResi)
print "Tile start: "+str(StartResidue)
if args.tilelength != None:
print "Custom tile length passed on the command line"
if args.tilestart != None:
print "Custom tile start passed on the command line"
reads = DNAReads()
print "Unselected DNA sequences (reads) from Enrich: "+reads[0]
print "Selected DNA sequences (reads) from Enrich: "+reads[1]
mutations = MutationCounts()
print "Number of mutations above 0.00: "+str(mutations[0])
print "Number of mutations above 0.10: "+str(mutations[1])
print "Number of mutations above 0.15: "+str(mutations[2])
print "Number of mutations above 0.30: "+str(mutations[3])
print "Number of mutations above 0.50: "+str(mutations[4])
print "Number of mutations above 1.00: "+str(mutations[5])
print "Number unselected mutants above threshold of 5: "+str(mutations[6])
print "Number of mutations retained in the selected population (not given a 1 if significant in unsel): "+str(mutations[7])
codons = CodonSubs()
print "Percent of possible codon subsititions observed in the unselected population:"
print "1-base substitution (#codons*9): {0:.1f}".format((codons[0]/(9*NumResi)*100))+"% "+str(codons[0])+"/"+str(9*NumResi)
print "2-base substitutions (#codons*27): {0:.1f}".format((codons[1]/(27*NumResi)*100))+"% "+str(codons[1])+"/"+str(27*NumResi)
print "3-base substitutions (#codons*27): {0:.1f}".format((codons[2]/(27*NumResi)*100))+"% "+str(codons[2])+"/"+str(27*NumResi)
print "Total base substitutions: "+str(codons[0]+codons[1]+codons[2])+"/"+str(63*NumResi)
nonsynonymous = Nonsynonymous()
print "Percent of unselected reads with: "
print "No nonsynonymous mutations: {0:.1f}".format((nonsynonymous[0]/int(reads[0]))*100)+"% "+str(nonsynonymous[0])+"/"+reads[0]
print "One nonsynonymous mutation: {0:.1f}".format((nonsynonymous[1]/int(reads[0]))*100)+"% "+str(nonsynonymous[1])+"/"+reads[0]
print "Multiple nonsynonymous mutations: {0:.1f}".format((nonsynonymous[2]/int(reads[0]))*100)+"% "+str(nonsynonymous[2])+"/"+reads[0]
print "Coverage of possible single nonsynonymous amino acid mutations: {0:.1f}".format((mutations[6]/(NumResi*20))*100)+"% "+str(mutations[6])+"/"+str(NumResi*20)
return
def main():
#Write out preample
print "QuickStats"
print "Author: "+__author__
print "Contact: "+__email__
print __copyright__
print "License: "+__license__
print "Credits: "+__credits__[0]+", "+__credits__[1]
print ""
print "Please cite:"
print "Github [user: JKlesmith] (www.github.com)"
print "Klesmith JR, Bacik J-P, Michalczyk R, Whitehead TA. 2015. Comprehensive sequence-flux mapping of metabolic pathways in living cells."
print "Kowalsky CA, Klesmith JR, Stapleton JA, Kelly V, Reichkitzer N, Whitehead TA. 2015. High-Resolution Sequence-Function Mapping of Full-Length Proteins. PLoS ONE 10(3):e0118193. doi:10.1371/journal.pone.0118193."
print ""
#Print out run stats
ImportNormData()
Build_Matrix()
PopulateMutArrays()
RunStats()
if __name__ == '__main__':
main()