-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_boxplots_aa_frequencies_tm_vs_nontm_helices.py
287 lines (236 loc) · 9.34 KB
/
draw_boxplots_aa_frequencies_tm_vs_nontm_helices.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
280
281
282
283
284
285
286
287
#!/usr/bin/env python
"""
File Name : get_ss_from_struc.py
Creation Date : 02-06-2019
Last Modified : Mi 26 Jun 2019 20:36:59 CEST
Author : Luca Deininger
Function of the script :
"""
from Bio.PDB import *
import numpy as np
import os
import collections
import warnings
import string
from Bio import BiopythonWarning
#import matplotlib.pyplot as plt
import pickle
warnings.simplefilter('ignore', BiopythonWarning)
from pylab import plot, show, savefig, xlim, figure, \
hold, ylim, legend, boxplot, setp, axes
# function for setting the colors of the box plots pairs
def setBoxColors(bp, color_):
for i in range(len(bp['boxes'])):
setp(bp['boxes'][i], color=color_)
for i in range(len(bp['caps'])):
setp(bp['caps'][i], color=color_)
for i in range(len(bp['whiskers'])):
setp(bp['whiskers'][i], color=color_)
for i in range(len(bp['medians'])):
setp(bp['medians'][i], color=color_)
def dssp_to_dict(dssp_obj):
"""
dssp returns a weird datastructure -> conversion to dict.
"""
dssp = {}
for k in list(dssp_obj.keys()):
dssp[k] = dssp_obj[k]
return dssp
def get_tm_chains(pdbtm_file, pdb):
"""
Get chains of pdbtm structure containing transmembrane helices.
"""
pdb_id = pdb[3:7]
entries = [x for x in pdbtm_file if pdb_id in x]
chains = [x[5] for x in entries]
return chains
def get_aa_in_helices(pdb_dir, pdb, chains):
"""
Returns: relative and absolute counts for each aa in helices present in given chains.
"""
p = PDBParser()
structure = p.get_structure("bla", pdb_dir+pdb)
# Always take first model
model = structure[0]
# DSSP to get sec structure of aas
dssp = dssp_to_dict(DSSP(model, pdb_dir+pdb))
# filter dssp dict for helix and extract aa
aa_in_helices = {k: v for k,
v in dssp.items() if v[2] == "H" and k[0] in chains}
# keep only amino acid in dict (much more information like coordinated provided)
aa_in_helices = [v[1] for k, v in aa_in_helices.items()]
# count occurrence of amino acids + fill dict with no occurring aas
aa_in_helices = collections.Counter(aa_in_helices)
aa_in_helices = fill_dict_0s(aa_in_helices)
# relative frequencies of aas
sum_ = sum(aa_in_helices.values())
if sum_ > 0:
rel_aa_in_helices = {k: float(v/sum_)
for k, v in aa_in_helices.items()}
else:
rel_aa_in_helices = {}
# pop 'aa' X
aa_in_helices.pop('X', None)
rel_aa_in_helices.pop('X', None)
# ordering dict (important for plots later)
aa_in_helices = collections.OrderedDict(sorted(aa_in_helices.items()))
rel_aa_in_helices = collections.OrderedDict(
sorted(rel_aa_in_helices.items()))
return aa_in_helices, rel_aa_in_helices
def sum_up_counter(list_counter):
"""
Provided a list of MULTIPLE Counter dictionaries it returns ONE Counter dictionary
uniting all counts.
"""
result = collections.Counter()
for x in list_counter:
result += x
result = collections.OrderedDict(sorted(result.items()))
return result
def fill_dict_0s(counter_dict):
"""
For every aa not in counter dict: Add: aa->0.
"""
for x in aas:
if x not in counter_dict:
counter_dict[x] = 0
return counter_dict
def get_distribution(aa_in_helices, aas_of_interest):
"""
Returns the distributions of the counts of each amino acid across multiple counter_dicts
by returning a list of 20 lists.
"""
result = []
for aa in aas_of_interest:
result.append(
[v for counter_dict in aa_in_helices for k, v in counter_dict.items() if k[0] == aa])
return result
def parse_files(pdbs, pdbtms, pdbtm_all_ids, pdb_dir, pdbtm_dir):
pdb_aa_helices = []
pdb_aa_helices_rel = []
pdbtm_aa_helices = []
pdbtm_aa_helices_rel = []
#end_index = 5
end_index = len(pdbs)
for pdb in pdbtms[:end_index]:
print("PDBTM:", pdb)
chains = get_tm_chains(pdbtm_all_ids, pdb)
result = get_aa_in_helices(pdbtm_dir, pdb, chains)
pdbtm_aa_helices.append(result[0])
pdbtm_aa_helices_rel.append(result[1])
for pdb in pdbs[:end_index]:
print("PDB:", pdb)
try:
result = get_aa_in_helices(
pdb_dir, pdb, list(string.ascii_uppercase))
pdb_aa_helices.append(result[0])
pdb_aa_helices_rel.append(result[1])
except:
print("DSSP fails:", pdb)
continue
return pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel
def export_(pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel):
folder = "serialized/draw_boxplot_"
pickle.dump(pdb_aa_helices, open(folder+"pdb_aa_helices.p", "wb"))
pickle.dump(pdbtm_aa_helices, open(folder+"pdbtm_aa_helices.p", "wb"))
pickle.dump(pdb_aa_helices_rel, open(folder+"pdb_aa_helices_rel.p", "wb"))
pickle.dump(pdbtm_aa_helices_rel, open(
folder+"pdbtm_aa_helices_rel.p", "wb"))
def import_():
print("Importing serialized datastructures...")
folder = "serialized/draw_boxplot_"
pdb_aa_helices = pickle.load(open(folder + "pdb_aa_helices.p", "rb"))
pdbtm_aa_helices = pickle.load(open(folder + "pdbtm_aa_helices.p", "rb"))
pdb_aa_helices_rel = pickle.load(
open(folder + "pdb_aa_helices_rel.p", "rb"))
pdbtm_aa_helices_rel = pickle.load(
open(folder + "pdbtm_aa_helices_rel.p", "rb"))
return pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel
def main():
parse_again=False # True
# directories of sampled pdbs and pdbtms
pdb_dir = "train_pdb_structures/"
pdbtm_dir = "pdbtm_structures/"
# defining all one letter code amino acids
global aas
aas = list(string.ascii_uppercase)
for no_aa in ["B", "J", "O", "U", "X", "Z"]:
aas.remove(no_aa)
# Get all pdbtm ids + chain
f = open('data/pdbtm_all_list.txt', 'r')
pdbtm_all_ids = f.readlines()
f.close()
if parse_again:
pdbs = os.listdir(pdb_dir)
pdbtms = os.listdir(pdbtm_dir)
pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel = parse_files(pdbs, pdbtms, pdbtm_all_ids, pdb_dir, pdbtm_dir)
export_(pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel)
else:
pdb_aa_helices, pdbtm_aa_helices, pdb_aa_helices_rel, pdbtm_aa_helices_rel=import_()
# Boxplots of amino acid distributions
print("Plotting Boxplots...")
# Relative values without outliers
# all together
fig = figure(figsize=(12, 6))
ax = axes()
nr_aas=len(aas)
bp=boxplot(get_distribution(pdbtm_aa_helices_rel, aas), positions=[
6+x*3-0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "blue")
bp=boxplot(get_distribution(pdb_aa_helices_rel, aas), positions=[
4+x*3+0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "black")
ylim(0,0.3)
ax.set_xticklabels([""]+aas+[""])
ax.set_xticks([2+x*3 for x in range(nr_aas+2)])
# draw temporary red and blue lines and use them to create a legend
hB, = plot([1,1],'k-', color = 'black')
hR, = plot([1,1],'k-', color = 'blue')
legend((hB, hR),('NON-TM helices', 'TM helices'))
hB.set_visible(False)
hR.set_visible(False)
savefig('figures/boxplot_rel_all_aas_without_outliers.png')
# hydrophobic aas
fig = figure(figsize=(8, 6))
ax = axes()
#plt.figure(figsize=(12, 6))
hydrophobic=["C", "F","G", "I", "L", "V", "W"]
nr_aas=len(hydrophobic)
bp=boxplot(get_distribution(pdbtm_aa_helices_rel, hydrophobic), positions=[
6+x*3-0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "blue")
bp=boxplot(get_distribution(pdb_aa_helices_rel, hydrophobic), positions=[
4+x*3+0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "black")
ylim(0,0.3)
ax.set_xticklabels([""]+hydrophobic+[""])
ax.set_xticks([2+x*3 for x in range(nr_aas+2)])
# draw temporary red and blue lines and use them to create a legend
hB, = plot([1,1],'k-', color = 'black')
hR, = plot([1,1],'k-', color = 'blue')
legend((hB, hR),('NON-TM helices', 'TM helices'))
hB.set_visible(False)
hR.set_visible(False)
savefig('figures/boxplot_rel_hydrophobic_without_outliers.png')
# Plotting hydrophlic amino acids
fig = figure(figsize=(8, 6))
ax = axes()
hydrophilic=["D", "E", "K", "R"]
nr_aas=len(hydrophilic)
bp=boxplot(get_distribution(pdbtm_aa_helices_rel, hydrophilic), positions=[
6+x*3-0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "blue")
bp=boxplot(get_distribution(pdb_aa_helices_rel, hydrophilic), positions=[
4+x*3+0.6 for x in range(nr_aas)], widths=0.6, showfliers=False)
setBoxColors(bp, "black")
ylim(0,0.3)
ax.set_xticklabels([""]+hydrophilic+[""])
ax.set_xticks([2+x*3 for x in range(nr_aas+2)])
# draw temporary red and blue lines and use them to create a legend
hB, = plot([1,1],'k-', color = 'black')
hR, = plot([1,1],'k-', color = 'blue')
legend((hB, hR),('NON-TM helices', 'TM helices'))
hB.set_visible(False)
hR.set_visible(False)
savefig('figures/boxplot_rel_hydrophilic_without_outliers.png')
main()