-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
207 lines (172 loc) · 9.19 KB
/
app.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
import ast, gzip, random, os, tempfile, time, sqlite3, urllib.request
import matplotlib, matplotlib.colors, matplotlib.pyplot as plt, seaborn as sns, pandas as pd, streamlit as st, streamlit_ext as ste, st_aggrid, py3Dmol, stmol
st.set_page_config(
page_title='Putative Novel Enzymes',
page_icon='🔬',
layout='wide',
)
#st.cache_resource.clear()
def uf(x):
return '{:,}'.format(x)
def select_dataframe_row(df_, selected_row_index, height=400):
gb = st_aggrid.GridOptionsBuilder.from_dataframe(df_)
gb.configure_selection(selection_mode='single', use_checkbox=True, pre_selected_rows=[ selected_row_index ])
gb.configure_grid_options(domLayout='normal')
#gb.configure_pagination()
#https://github.com/PablocFonseca/streamlit-aggrid/issues/57
gb.configure_grid_options(onFirstDataRendered=st_aggrid.JsCode("""
function(e) {
e.api.ensureIndexVisible(%d, 'middle');
}
""" % (selected_row_index,)).js_code)
gridOptions = gb.build()
gridResponse = st_aggrid.AgGrid(df_,
gridOptions=gridOptions,
#update_mode=st_aggrid.GridUpdateMode.SELECTION_CHANGED,
fit_columns_on_grid_load=True,
height=height,
width='100%',
enable_enterprise_modules=False,
allow_unsafe_jscode=True,
)
if not(len(gridResponse['selected_rows']) > 0): time.sleep(5) # Prevent annoying row-not-selected errors during loading
if len(gridResponse['selected_rows']) == 0: return None
return gridResponse['selected_rows'][0]
@st.cache_resource
def read_af2_v3_(af2_id):
url_ = f'https://alphafold.ebi.ac.uk/files/AF-{af2_id}-F1-model_v3.pdb'
with urllib.request.urlopen(url_) as url:
return url.read().decode('utf-8')
@st.cache_resource
def read_struct_():
return pd.read_csv('pipeline/results/af2_v3.tsv.gz', sep='\t')
@st.cache_resource
def read_pockets_():
return pd.read_csv('pipeline/results/af2_v3.obabel_hxr.autosite.summary.score60_pLDDT90.tsv.gz', sep='\t')
@st.cache_resource
def read_deepfri_():
return pd.read_csv('pipeline/results/af2_v3.DeepFRI_terms.tsv.gz', sep='\t')
@st.cache_resource
def read_deepfri_summary_():
df_ = read_deepfri_().sort_values('Score', ascending=False).groupby('Protein').head(1).rename({
'Protein': 'struct_id',
'Score': 'DeepFri_max_score',
'GO_term/EC_number name': 'DeepFri_max_GO/EC_name',
}, axis=1)
return df_[['struct_id', 'DeepFri_max_score', 'DeepFri_max_GO/EC_name']]
@st.cache_resource
def read_pockets_with_deepfri_summary_():
df_ = read_struct_()
df_ = df_.merge(read_pockets_().iloc[:,1:].add_prefix('pocket_').rename({'pocket_pocket_id': 'pocket_id'}, axis=1), left_on='UniProtKB_ac', right_on='pocket_struct_id')
df_ = df_.merge(read_deepfri_summary_(), left_on='UniProtKB_ac', right_on='struct_id', how='left')
df_ = df_.sort_values(['DeepFri_max_score'], ascending=False).reset_index(drop=True)
df_['struct_resid_in_pockets'] = df_['pocket_n_resid'] / df_['n_resid']
return df_
st.write('# Putative novel enzymes and small molecule binding proteins')
tab1, tab2 = st.tabs(['Browse examples', 'Global statistics'])
with tab1:
st.write('#### All structures/pockets')
st.markdown("""Examples:
Fig 2B [A0A849TG76](/app?entryID=A0A849TG76) and [A0A2D8BRH7](/app?entryID=A0A2D8BRH7);
Fig 2C [A0A849ZK06](/app?entryID=A0A849ZK06);
Fig 2D [S0EUL8](/app?entryID=S0EUL8).
""")
df_pockets_ = read_pockets_with_deepfri_summary_()
if st.checkbox('Hide structures with a general lack of compactness (struct_resid_in_pockets > 0.4)', value=True):
df_pockets_ = df_pockets_.query('struct_resid_in_pockets <= 0.4').reset_index(drop=True)
cols_drop_ = ['rgyr', 'pocket_xmin', 'pocket_xmax', 'pocket_ymin', 'pocket_ymax', 'pocket_zmin', 'pocket_zmax',
'pocket_cl_file', 'pocket_cl_isfile', 'pocket_struct_id',
'pocket_n_points', 'pocket_energy', 'pocket_energy_per_vol', 'pocket_rad_gyration', 'pocket_buriedness', 'pocket_resid', 'struct_id',]
df_pockets_aggrid_ = df_pockets_.drop(cols_drop_, axis=1).round(
{'pocket_score': 1, 'pocket_mean_pLDDT': 1, 'DeepFri_max_score': 2, 'struct_resid_in_pockets': 2})
try:
entryID = st.experimental_get_query_params().get('entryID')[0]
entryID_index_ = int(df_pockets_aggrid_.query('UniProtKB_ac == @entryID').index.values[0])
except:
entryID_index_ = 0
row_ = select_dataframe_row(df_pockets_aggrid_, selected_row_index=entryID_index_)
st.write(f'{uf(len(df_pockets_aggrid_))} pockets shown')
af2_id_ = row_['UniProtKB_ac']
pocket_id_ = row_['pocket_id']
resid_ = df_pockets_.query('UniProtKB_ac == @af2_id_ & pocket_id == @pocket_id_').squeeze().pocket_resid
cl_file_ = df_pockets_.query('UniProtKB_ac == @af2_id_ & pocket_id == @pocket_id_').squeeze().pocket_cl_file
pocket_resid_ = ast.literal_eval(resid_)
col1, col2 = st.columns(2)
with col1:
st.write(f'#### DeepFRI GO/EC terms for {af2_id_}')
df_terms_ = read_deepfri_().query('Protein == @af2_id_').sort_values('Score', ascending=False).round({'Score': 2,}).reset_index(drop=True)
if st.checkbox('Hide non-significant terms (Score < 0.5)', value=True):
df_terms_ = df_terms_.query('Score >= 0.5')
if st.checkbox('Hide terms without saliency data', value=True):
df_terms_ = df_terms_.query('(DeepFRI_ont == "EC") | DeepFRI_ont == "MF"')
rs_terms_ = select_dataframe_row(df_terms_, selected_row_index=0)
st.write(f'{af2_id_} in [UniProt](https://www.uniprot.org/uniprotkb/{af2_id_}/entry) / [AlphaFill](https://alphafill.eu/model?id={af2_id_}) / [Ensembl Bacteria](https://bacteria.ensembl.org/Multi/Search/Results?species=all;idx=;q={af2_id_};site=ensemblunit)')
try:
go_ec_ = rs_terms_['GO_term/EC_number']
except:
go_ec_ = None
with col2:
st.write(f'#### Structure/visualisation for {af2_id_}')
#st.write('Blue = pocket residues')
saliency_ = None
if not(go_ec_) is None:
fp_ = f'pipeline/results/af2_v3.DeepFRI_saliency/{af2_id_}_saliency.tsv.gz'
if os.path.getsize(fp_) > 1:
df_ = pd.read_csv(fp_, sep=',')
if go_ec_ in df_.columns:
st.write(f'Residues colored by saliency for {go_ec_}')
saliency_ = df_[go_ec_]
else:
st.write('No saliency available (CC/BP?)')
pdb_ = read_af2_v3_(af2_id_)
cmap_ = sns.color_palette('viridis', as_cmap=True)
if not(saliency_) is None:
colors_pocket = {i + 1: matplotlib.colors.to_hex(cmap_(val_)) for i, val_ in saliency_.items() }
else:
colors_pocket = {}
xyzview = py3Dmol.view()
# Add structure
xyzview.addModel(pdb_, format='pdb')
xyzview.setStyle({'model': 0}, {
'cartoon': {
'colorscheme': {
'prop': 'resi',
'map': colors_pocket,
},
'arrows': True,
}
})
# Add pocket surface
with gzip.open(os.path.join('pipeline', 'results', 'af2_v3.obabel_hxr.autosite.summary.score60_pLDDT90', os.path.basename(cl_file_) + '.gz')) as fh:
pocket_ = fh.read().decode('ascii')
xyzview.addModel(pocket_, format='pdb')
xyzview.setStyle({'model': -1}, {})
xyzview.addSurface(py3Dmol.VDW, {'opacity': 0.5, 'color': 'pink'}, {'model': -1})
# Back matter
xyzview.setBackgroundColor('#eeeeee')
xyzview.zoomTo()
stmol.showmol(xyzview, height=600, width=600)
#st.write(cmap_)
with tab2:
df_pockets_ = read_pockets_with_deepfri_summary_()
n_pockets_ = len(df_pockets_)
n_artefact_pockets_ = len(df_pockets_.query('struct_resid_in_pockets > .4'))
st.markdown(f'- {uf(len(read_pockets_()))} pockets in {uf(read_pockets_().struct_id.nunique())} structures with pocket_score > 60 and mean pLDDT > 90')
st.markdown(f'- {uf(n_artefact_pockets_)} of {uf(n_pockets_)} ({100*n_artefact_pockets_ / n_pockets_:.1f}%) pockets encompassing more than 40% of the structure')
n_deepfri_ = len(read_deepfri_())
n_deepfri_signif_ = len(read_deepfri_().query("Score > 0.5"))
st.markdown(f'- {uf(n_deepfri_)} DeepFRI predictions with {uf(n_deepfri_signif_)} significant at 0.5')
for ont_ in ['MF', 'EC', 'BP', 'CC']:
st.markdown(f'#### Term counts for {ont_}')
col_1_, col_2_ = st.columns(2)
value_counts_ = read_deepfri_().query('Score > 0.5 & DeepFRI_ont == @ont_')[['GO_term/EC_number name', 'GO_term/EC_number']].value_counts()
value_counts_plt_ = read_deepfri_().query('Score > 0.5 & DeepFRI_ont == @ont_')['GO_term/EC_number name'].value_counts()
with col_1_:
st.dataframe(value_counts_)
with col_2_:
fig, ax = plt.subplots(figsize=(5, 5))
value_counts_plt_.head(10).plot(kind='barh', color='tab:gray')
plt.title(f'Top 10 enriched terms ({ont_})')
plt.gca().invert_yaxis()
st.pyplot(fig)
#plt.savefig(f'DeepFRI_top10_{ont_}.svg', format='svg')#, bbox_inches='tight') # SVG version for manuscript: https://github.com/streamlit/streamlit/issues/796