-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatherStats.py
More file actions
executable file
·343 lines (309 loc) · 13.2 KB
/
gatherStats.py
File metadata and controls
executable file
·343 lines (309 loc) · 13.2 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
#!/usr/bin/env python
import json
from json import encoder
encoder.FLOAT_REPR = lambda o: format(o, '.4f')
import sys
import os
import glob
import copy
import math
import numpy as np
from dnaprodb_utils import C
#import matplotlib.pyplot as plt
DATA_PATH = C["DATA_PATH"]
# Load PDB Components dictionary
with open(os.path.join(DATA_PATH,'components.json')) as FILE:
COMPONENTS = json.load(FILE)
def sumDicts(acc, val, scale):
for key in acc:
if(key not in val):
continue
if(isinstance(acc[key], dict)):
sumDicts(acc[key], val[key], scale)
else:
acc[key] += val[key]/scale
def makeStats(data, nonNumericKeys, nonNumericValues, key_string=[]):
for key in data:
if(isinstance(data[key], dict)):
makeStats(data[key], nonNumericKeys, nonNumericValues, key_string+[key])
elif(isinstance(data[key], list)):
if(key in nonNumericKeys):
# get counts of array of values
values = nonNumericValues[nonNumericKeys.index(key)]
counts = {}
for value in values:
if(len(data[key]) > 0):
counts[value] = float(data[key].count(value))/len(data[key])
else:
counts[value] = 0.0
data[key] = counts
else:
# get stats of array of numbers
m = np.array(data[key], dtype=np.float32)
cutoff_lower = 0.5
cutoff_upper = 999.9
ks = '.'.join(key_string+[key])
#bw = 0.3
#sb = cutoff_lower
#a = a[np.logical_or(a >= cutoff_lower, a < cutoff_upper)]
#nbins = 0
#if( len(a) > 0 ):
## Determine a proper cut-off for masking: Want to remove upper and lower outlier bins
#nbins = int(math.ceil((a.max()-a.min())/bw))
#if(nbins > 9):
#h, e = np.histogram(a, nbins)
#h = h[h > 0]
#mh = np.median(h)
#ma = np.median(a)
#MAD = np.median(np.abs(h-mh))
#if(MAD > 0):
#Z = np.abs(0.6745*(h-mh)/MAD)
## Set bottom tail cut-off
#iO = -1 # index of previously seen outlier
#for i in range(len(Z)):
#if(Z[i] > 3.5):
#if(i-iO > 1):
#break
#cutoff_lower = (i+1)*bw + sb
## Set upper tail cut-off
#iO = len(Z)+1
#for i in reversed(range(len(Z))):
#if(Z[i] > 3.5):
#if(iO- i > 1):
#break
#cutoff_upper = (i-1)*bw + sb
## Mask the values removing outliers
#print(nbins, len(a), cutoff_lower, cutoff_upper, ks)
#if(cutoff_lower == cutoff_upper):
#print(h)
#print(Z)
#print(mh, ma, MAD)
#plt.hist(a, nbins, normed=1, facecolor='g', alpha=0.75)
#plt.title(ks)
#plt.show()
#exit(0)
m = m[np.logical_and(m >= cutoff_lower, m < cutoff_upper)]
if(m.size > 0):
stats = {
"mean": m.mean(),
"std": m.std(),
"median": np.median(m),
"max": m.max(),
"min": m.min(),
"p10": np.percentile(m, 10),
"p20": np.percentile(m, 20),
"p50": np.percentile(m, 50),
"p80": np.percentile(m, 80),
"p90": np.percentile(m, 90)
}
#if(ks in ("DGARG.basa.sr.sc", "DALYS.basa.pp.sc", "DCHIS.vdw_sum.pp.sc", "DTTYR.hbond_sum.sr.sc")):
#print(m.min())
#print(m.max())
#bw = 0.5
#nbins = int(math.ceil((m.max()-m.min())/bw))
#if(nbins > 1):
#plt.hist(m, nbins, normed=1, facecolor='g', alpha=0.75)
#plt.title(ks)
#plt.show()
else:
stats = {
"mean": 0,
"std": 0,
"median": 0,
"max": 0,
"min": 0,
"p10": 0,
"p20": 0,
"p50": 0,
"p80": 0,
"p90": 0
}
for s in stats:
stats[s] = float(stats[s])
data[key] = stats
else:
continue
nucMtyLabel = ['wg', 'sg', 'bs', 'sr', 'pp']
resMtyLabel = ['mc', 'sc']
# Interactions
int_mty_fields = ["basa", "hbond_sum", "vdw_sum"]
int_template = {
"geometry": [],
"mean_nn_distance": [],
"min_distance": [],
"cm_distance": [],
"count": 0
}
for f in int_mty_fields:
int_template[f] = {}
for nmty in nucMtyLabel:
int_template[f][nmty] = {}
for rmty in resMtyLabel:
int_template[f][nmty][rmty] = []
# Nucleotides
nuc_mty_fields = ["hbond_sum", "vdw_interaction_sum"]
nuc_template = {
"basa_sum": {},
"residue_interaction_count": [],
"count": 0
}
for nmty in nucMtyLabel:
nuc_template["basa_sum"][nmty] = []
for f in nuc_mty_fields:
nuc_template[f] = {}
for nmty in nucMtyLabel:
nuc_template[f][nmty] = {}
for rmty in resMtyLabel:
nuc_template[f][nmty][rmty] = []
# Residues
res_mty_fields = ["hbond_sum", "vdw_interaction_sum"]
res_template = {
"basa_sum": {},
"nucleotide_interaction_count": [],
"count": 0
}
for rmty in resMtyLabel:
res_template["basa_sum"][rmty] = []
for f in res_mty_fields:
res_template[f] = {}
for nmty in nucMtyLabel:
res_template[f][nmty] = {}
for rmty in resMtyLabel:
res_template[f][nmty][rmty] = []
# Data Dict
nucleotides = ['DA','DC','DG','DT','DI','DU']
residues = [
'ALA','ARG','ASN','ASP','CYS',
'GLU','GLN','GLY','HIS','ILE',
'LEU','LYS','MET','PHE','PRO',
'SER','THR','TRP','TYR','VAL'
]
DATA = {} # keys: NUC-RES, RES, NUC
for nuc in nucleotides:
DATA[nuc] = copy.deepcopy(nuc_template)
for res in residues:
DATA[nuc+res] = copy.deepcopy(int_template)
for res in residues:
DATA[res] = copy.deepcopy(res_template)
# loop over directories
dirlist = sys.argv[1]
DIRS = open(dirlist)
for d in DIRS:
d = d.strip()
for listFile in glob.glob("./{}/list*.txt".format(d)):
LF = open(listFile)
for pdbid in LF:
pdbid = pdbid.strip()
if(os.access("{}/{}.json".format(d,pdbid), os.R_OK)):
with open("{}/{}.json".format(d,pdbid)) as D:
data = json.load(D)
if("error" in data):
continue
# Make nuc and res lookups
nucDict = {}
resDict = {}
try:
for n in data["dna"]["nucleotides"]:
nucDict[n["id"]] = n
for r in data["protein"]["residues"]:
resDict[r["id"]] = r
except:
print(("{}: json data has unexpected format.".format(pdbid)))
continue
# Loop over model data
for model in data["interfaces"]["models"]:
for interface in model:
for nr in interface["nucleotide-residue_interactions"]:
res = nr["res_name"]
if(res not in residues):
res = COMPONENTS[res]['_chem_comp.mon_nstd_parent_comp_id']
nuc = nr["nuc_name"]
if(nuc not in nucleotides):
nuc = COMPONENTS[nuc]['_chem_comp.mon_nstd_parent_comp_id']
# Add moiety fields
for f in int_mty_fields:
for nmty in nucMtyLabel:
if(not (nmty in nr[f])):
continue
for rmty in resMtyLabel:
DATA[nuc+res][f][nmty][rmty].append(nr[f][nmty][rmty])
# Add geometry
DATA[nuc+res]["geometry"].append(nr["geometry"])
# Add min_dist
DATA[nuc+res]["min_distance"].append(nr["min_distance"])
# Add mean_nn_dist
DATA[nuc+res]["mean_nn_distance"].append(nr["mean_nn_distance"])
# Add cm distance
DATA[nuc+res]["cm_distance"].append(nr["cm_distance"])
# Add count
DATA[nuc+res]["count"] += 1
for n in interface["nucleotide_data"]:
nuc = nucDict[n["nuc_id"]]["name"]
if(nuc not in nucleotides):
nuc = COMPONENTS[nuc]['_chem_comp.mon_nstd_parent_comp_id']
# Add moiety fields
for f in nuc_mty_fields:
for nmty in nucMtyLabel:
if(not (nmty in n[f])):
continue
for rmty in resMtyLabel:
DATA[nuc][f][nmty][rmty].append(n[f][nmty][rmty])
# Add BASA
for nmty in nucMtyLabel:
if(not (nmty in n["basa_sum"])):
continue
DATA[nuc]["basa_sum"][nmty].append(n["basa_sum"][nmty])
# Add interaction counts
DATA[nuc]["residue_interaction_count"].append(n["residue_interaction_count"])
DATA[nuc]["count"] += 1
for r in interface["residue_data"]:
res = resDict[r["res_id"]]["name"]
if(res not in residues):
res = COMPONENTS[res]['_chem_comp.mon_nstd_parent_comp_id']
# Add moiety fields
for f in res_mty_fields:
for nmty in nucMtyLabel:
if(not (nmty in r[f])):
continue
for rmty in resMtyLabel:
DATA[res][f][nmty][rmty].append(r[f][nmty][rmty])
# Add BASA
for rmty in resMtyLabel:
if(not (rmty in r["basa_sum"])):
continue
DATA[res]["basa_sum"][rmty].append(r["basa_sum"][rmty])
# Add interaction counts
DATA[res]["nucleotide_interaction_count"].append(r["nucleotide_interaction_count"])
DATA[res]["count"] += 1
LF.close()
DIRS.close()
# Compile statistics
nonNumericKeys = ["geometry"]
nonNumericValues = [["psuedo_pair", "psuedo_stack", "none"]]
makeStats(DATA, nonNumericKeys, nonNumericValues)
# Add fallback stats
DATA["fallback"] = {}
for f in int_mty_fields:
DATA["fallback"][f] = {}
for n in nucMtyLabel:
DATA["fallback"][f][n] = {}
for r in resMtyLabel:
DATA["fallback"][f][n][r] = {
"mean": 0,
"std": 0,
"median": 0,
"max": 0,
"min": 0,
"p10": 0,
"p20": 0,
"p50": 0,
"p80": 0,
"p90": 0
}
# Add stat averages
for n in nucleotides[:-2]:
for r in residues:
sumDicts(DATA["fallback"], DATA[n+r], 80.0)
OUT = open(os.path.join(DATA_PATH, "interaction_stats.json"), "w")
OUT.write(json.dumps(DATA, indent=None,separators=(',', ':'), sort_keys=True))
OUT.close()