-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcollect_pH_stats.py
executable file
·279 lines (227 loc) · 8.97 KB
/
collect_pH_stats.py
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
#!/usr/bin/env python
import csv, sys, re, math
from optparse import OptionParser
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Residue(object):
""" A holder for all of the data for a residue """
def __init__(self, name='', number=0, offset=0.0, frac_prot=0.0,
pred=0.0, transitions=0):
self.name = name
self.number = int(number)
self.offset = float(offset)
self.frac_prot = float(frac_prot)
self.pred = float(pred)
self.transitions = int(transitions)
def __eq__(self, other):
return self.name == other.name and self.number == other.number
def __gt__(self, other):
return self.number > other.number
def __lt__(self, other):
return self.number < other.number
def __ne__(self, other):
return not self == other
def __ge__(self, other):
return self.number >= other.number
def __le__(self, other):
return self.number <= other.number
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class PhSet(object):
""" A set of pH values """
def __init__(self, pH=7.0):
self.pH = float(pH)
self.residue_list = []
def add_residue(self, **kwargs):
""" Adds a residue to the list of residues """
self.residue_list.append(Residue(**kwargs))
def __gt__(self, other):
return self.pH > other.pH
def __eq__(self, other):
return self.pH == other.pH
def __ge__(self, other):
return self > other or self == other
def __lt__(self, other):
return not self >= other
def __le__(self, other):
return not self > other
def __ne__(self, other):
return self.pH != other.pH
def __contains__(self, element):
for res in self.residue_list:
if res == element: return True
return False
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def set_list(lines):
""" Converts lines of input into a list of PhSet's """
start_of_set = \
re.compile(r'Solvent pH is *([-+]?\d+(?:\.\d*)?|\.\d+)')
residue_line = \
re.compile(r'([A-Z45]+) *(\d+) *: Offset *([-+]?\d+(?:\.\d*)?|\.\d+|-*[Ii]nf) *Pred *([-+]?\d+(?:\.\d*)?|\.\d+|-*[Ii]nf) *Frac Prot *(\d+(?:\.\d*)?) *Transitions *(\d+)')
end_of_set = \
re.compile(r'Average total molecular protonation: *(\d+(?:\.\d*))')
ph_setlist = []
i = 0
while i < len(lines):
start = start_of_set.match(lines[i])
if start:
my_set = PhSet(start.groups()[0])
end = end_of_set.match(lines[i])
while i < len(lines) and not end:
my_info = residue_line.match(lines[i])
if my_info:
info = my_info.groups()
my_set.add_residue(name=info[0], number=info[1], offset=info[2],
pred=info[3], frac_prot=info[4],
transitions=info[5])
else:
end = end_of_set.match(lines[i])
if end:
my_set.avg_prot = float(end.groups()[0])
break
i += 1
ph_setlist.append(my_set)
i += 1
return ph_setlist
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def _get_residues(ph_setlist):
# Get all of the residues in the ph_setlist
residues = []
for phset in ph_setlist:
if not residues: residues = phset.residue_list[:]
for residue in phset.residue_list:
if residue in residues: continue
residues.append(residue)
# Sort the residue list
residues.sort()
return residues
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def _individual_info(lines, outfile, data_type, data_desc, hill=False):
if data_type == 'frac_deprot':
data_type = 'frac_prot'
do_deprot = True
else:
do_deprot = False
# Write the fraction protonated
ph_setlist = set_list(lines)
ph_setlist.sort()
residues = _get_residues(ph_setlist)
# Write out the first couple rows (just headers)
firstline = []
secondline = []
for res in residues:
firstline += ['%s %d' % (res.name, res.number), '', '']
secondline += ['pH', data_desc, '']
outfile.writerow(firstline)
outfile.writerow(secondline)
# Now write out each row
for phset in ph_setlist:
line = []
for res in residues:
if not res in phset.residue_list:
line += ['-----', '', '']
continue
myres = phset.residue_list[phset.residue_list.index(res)]
if not hill:
if do_deprot:
line += [phset.pH, 1-getattr(myres, data_type), '']
else:
line += [phset.pH, getattr(myres, data_type), '']
else:
if myres.frac_prot == 1:
line += [phset.pH, '-inf', '']
elif myres.frac_prot == 0:
line += [phset.pH, 'inf', '']
else:
line += [phset.pH,
math.log10((1-myres.frac_prot)/myres.frac_prot), '']
outfile.writerow(line)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def pka_info(lines, outfile):
_individual_info(lines, outfile, 'pred', 'predicted pKa')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def frac_info(lines, outfile):
_individual_info(lines, outfile, 'frac_prot', 'Fraction Protonated')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def frac_deprot_info(lines, outfile):
_individual_info(lines, outfile, 'frac_deprot', 'Fraction Deprotonated')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def hill_info(lines, outfile):
_individual_info(lines, outfile, 'frac_prot', 'Hill plot', hill=True)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def trans_info(lines, outfile):
_individual_info(lines, outfile, 'transitions', 'Transitions')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def offset_info(lines, outfile):
_individual_info(lines, outfile, 'offset', 'Offset From pH')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def full_info(lines, outfile):
ph_setlist = set_list(lines)
ph_setlist.sort()
# Write out the first couple rows (just headers)
firstline = ['Residue', '']
secondline = ['', '']
for phset in ph_setlist:
firstline += ['', 'pH %.2f' % phset.pH, '', '']
secondline += ['pKa', 'Frac Prot', 'Transitions', '']
outfile.writerow(firstline)
outfile.writerow(secondline)
residues = _get_residues(ph_setlist)
# Now write out all of the rows
res_rows = [['%4s %4d' % (res.name, res.number), ''] for res in residues]
for i, res in enumerate(residues):
for phset in ph_setlist:
if not res in phset:
res_rows[i] += ['-----', '-----', '-----', '']
continue
res_item = phset.residue_list[phset.residue_list.index(res)]
res_rows[i] += [res_item.pred, res_item.frac_prot, res_item.transitions, '']
for row in res_rows: outfile.writerow(row)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def main():
import signal
parser = OptionParser(usage='%prog [Options] stat_file1 [stat_file2 ...]')
parser.add_option('-o', '--output', dest='output', default='results.csv',
help='Final CSV file with total statistics. [Default %default]')
parser.add_option('-i', '--info', dest='info', default=None,
help='What info do you want printed out?. Available ' +
'options are [pKa, frac_prot, transitions, offset, hill, ' +
'frac_deprot]. Default: full description')
opt, args = parser.parse_args()
# Interrupt handler
def sigint_handle(*args, **kwargs):
print ''
parser.print_help()
sys.exit(1)
signal.signal(signal.SIGINT, sigint_handle)
if not opt.output.endswith('.csv'): opt.output += '.csv'
if opt.info == None:
run_method = full_info
elif opt.info.lower() == 'pka':
run_method = pka_info
elif opt.info.lower().startswith('frac_p'):
run_method = frac_info
elif opt.info.lower().startswith('frac_d'):
run_method = frac_deprot_info
elif opt.info.lower().startswith('trans'):
run_method = trans_info
elif opt.info.lower() == 'offset':
run_method = offset_info
elif opt.info.lower() == 'hill':
run_method = hill_info
else:
run_method = full_info
outfile = csv.writer(open(opt.output, 'wb'))
lines = []
if not args:
lines = sys.stdin.readlines()
else:
for arg in args:
tmp = open(arg, 'r')
lines += tmp.readlines()
tmp.close()
for i in range(len(lines)):
lines[i] = lines[i].strip()
run_method(lines, outfile)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
main()
print 'Done!'