forked from grandrea/Alphafold-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_AF_all.py
More file actions
186 lines (149 loc) · 6.41 KB
/
Copy pathplot_AF_all.py
File metadata and controls
186 lines (149 loc) · 6.41 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
import matplotlib.pyplot as plt
import seaborn as sns; sns.set_theme()
import pickle
from Bio import SeqIO
from itertools import accumulate
import json
import glob
import numpy as np
import pandas as pd
#fasta file with input sequence must be in result directory
input_sequence_name = glob.glob('*fasta')[0]
#plot title as in fasta file
try:
protein_names_for_titile = input_sequence_name.replace('.fasta', '')
except:
protein_names_for_titile = ''
try:
input_sequence = SeqIO.to_dict(SeqIO.parse(input_sequence_name, 'fasta'))
#handle homomultimers with identical fasta headers
except ValueError:
with open(input_sequence_name) as original, open('corrected.fasta', 'w') as corrected:
records = SeqIO.parse(original, 'fasta')
for idx, record in enumerate(records):
record.id = record.id + str(idx)
print(record.id )
SeqIO.write(record, corrected, 'fasta')
corrected.close()
input_sequence = SeqIO.to_dict(SeqIO.parse('corrected.fasta', 'fasta'))
#pickle files coming out of AF run
file_list = glob.glob('*_multimer*.pkl')
file_list = sorted(file_list)
#finished run will produce a .json file with the model ranking
try:
json1_file = open('ranking_debug.json')
json1_str = json1_file.read()
model_stats = json.loads(json1_str)
json1_file.close()
model_ranking = model_stats.get('order')
except FileNotFoundError: #plot an incomplete run
model_ranking = []
PAE_list = []
width_list = []
list_ranking = {}
statistics_list = []
#read pkl, match model number with rank using json file
for index, file_name in enumerate(file_list):
d = pickle.load(open(file_name,'rb'))
model_name = file_name.replace('.pkl', '')
model_name = model_name.replace('result_', '')
try:
model_rank = model_ranking.index(model_name)
except ValueError:
model_rank = index
list_ranking[file_name] = [d, model_rank]
width_list.append(4)
statistics_list.append([model_name,
d.get('ptm'),
d.get('iptm'),
np.mean(d.get('plddt')),
d.get('ranking_confidence')])
PAE_list.append(d)
width_list.append(0.2)
#check protein lengths in input files for PAE plot
sequence_lengths = []
for item in input_sequence.keys():
sequence = input_sequence.get(item)
sequence_length = len(sequence.seq)
sequence_lengths.append(sequence_length)
#figure out positions for black lines delimiting proteins in PAE plot
line_position_accumulated = accumulate(sequence_lengths)
line_positions = []
for item in line_position_accumulated:
line_positions.append(item)
#write stats file--------
model_stats = pd.DataFrame(statistics_list,
columns = ['model', 'ptm', 'iptm', 'plddt', 'confrank'])
model_stats.to_csv('model_statistics.csv', index=False )
#PAE plot -------------------
output_name = str('predicted_alignment_error.png')
palette = sns.diverging_palette(220, 20, as_cmap=True)
fig, axs = plt.subplots(ncols=len(file_list) + 1,
gridspec_kw=dict(width_ratios=width_list),
figsize = (4*len(file_list), 4))
fig.subplots_adjust(top=0.8)
for file_name in file_list:
plot_number = list_ranking.get(file_name)[1]
PAE = list_ranking.get(file_name)[0]['predicted_aligned_error']
ipTM = list_ranking.get(file_name)[0]['iptm'].round(3)
tick_range = [1] + list(range(500, len(PAE), 500))
sns.heatmap(PAE,
cmap=palette,
ax=axs[plot_number],
cbar=False,
vmin= 0,
vmax = 30)
# axs[plot_number].yaxis.set_major_locator(mticker.MaxNLocator(5))
# axs[plot_number].xaxis.set_major_locator(mticker.MaxNLocator(5))
if plot_number == 0:
axs[plot_number].set_yticks(ticks = tick_range, labels = tick_range)
else:
axs[plot_number].set_yticks([], [])
axs[plot_number].set_xticks(ticks=tick_range, labels=tick_range)
axs[plot_number].title.set_text(str('model' +
str(list_ranking.get(file_name)[1]) +
'\n iptm: ' +
str(ipTM)))
#add black lines delimiting the two proteins
for element in line_positions:
axs[plot_number].vlines(element,
ymin=0,
ymax=len(PAE),
color='black',
linewidth=3)
axs[plot_number].hlines(element,
xmin=0,
xmax=len(PAE),
color='black',
linewidth=3)
fig.colorbar(axs[0].collections[0], cax=axs[-1])
fig.suptitle(str('Predicted alignment error ' + protein_names_for_titile))
plt.savefig(output_name)
#pLDDT plot------------------------------
output_name = str('pLDDT.png')
#palette = sns.light_palette("#2ecc71", as_cmap=True, reverse=True)
fig, axs = plt.subplots(ncols=len(file_list),
figsize = (6*len(file_list), 3))
for file_name in file_list:
plot_number = list_ranking.get(file_name)[1]
PAE = list_ranking.get(file_name)[0]['plddt']
tick_range = [1] + list(range(500, len(PAE), 500))
ytick_range = list(range(0, 100, 10))
try:
axs[plot_number].plot(list(range(0,len(PAE), 1)), PAE, color='b')
axs[plot_number].set_yticks(ticks = ytick_range, labels = ytick_range)
axs[plot_number].set_xticks(ticks = tick_range, labels = tick_range)
axs[plot_number].title.set_text(str('model' + str(list_ranking.get(file_name)[1])))
for element in line_positions:
axs[plot_number].vlines(element, ymin = 0, ymax = 100, color='black')
# #axs[plot_number].hlines(element, xmin = 0, xmax = len(PAE), color='black')
except TypeError: #handle single model and single pkl file
axs.plot(list(range(0,len(PAE), 1)), PAE, color='b')
axs.set_yticks(ticks = ytick_range, labels = ytick_range)
axs.set_xticks(ticks = tick_range, labels = tick_range)
axs.title.set_text(str('model' + str(list_ranking.get(file_name)[1])))
for element in line_positions:
axs.vlines(element, ymin = 0, ymax = 100, color='black')
#fig.colorbar(axs[0].collections[0], cax=axs[-1])
fig.suptitle(str('plddt ' + protein_names_for_titile))
plt.savefig(output_name)