Newer
Older
############################################
# imports
############################################
import jax
import requests
import hashlib
import tarfile
import time
import pickle
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
###########################################
# control gpu/cpu memory usage
###########################################
def rm(x):
'''remove data from device'''
jax.tree_util.tree_map(lambda y: y.device_buffer.delete(), x)
def to(x,device="cpu"):
'''move data to device'''
d = jax.devices(device)[0]
return jax.tree_util.tree_map(lambda y:jax.device_put(y,d), x)
def clear_mem(device="gpu"):
'''remove all data from device'''
backend = jax.lib.xla_bridge.get_backend(device)
for buf in backend.live_buffers(): buf.delete()
##########################################
# call mmseqs2
##########################################
def run_mmseqs2(query_sequence, prefix, use_env=True, filter=False):
def submit(query_sequence, mode):
res = requests.post('https://a3m.mmseqs.com/ticket/msa', data={'q':f">1\n{query_sequence}", 'mode': mode})
return res.json()
def status(ID):
res = requests.get(f'https://a3m.mmseqs.com/ticket/{ID}')
return res.json()
def download(ID, path):
res = requests.get(f'https://a3m.mmseqs.com/result/download/{ID}')
with open(path,"wb") as out: out.write(res.content)
if filter:
mode = "env" if use_env else "all"
else:
mode = "env-nofilter" if use_env else "nofilter"
path = f"{prefix}_{mode}"
if not os.path.isdir(path): os.mkdir(path)
# call mmseqs2 api
tar_gz_file = f'{path}/out.tar.gz'
if not os.path.isfile(tar_gz_file):
out = submit(query_sequence, mode)
while out["status"] in ["RUNNING","PENDING"]:
time.sleep(1)
out = status(out["id"])
download(out["id"], tar_gz_file)
# parse a3m files
a3m_lines = []
a3m = f"{prefix}_{mode}.a3m"
if not os.path.isfile(a3m):
with tarfile.open(tar_gz_file) as tar_gz: tar_gz.extractall(path)
a3m_files = [f"{path}/uniref.a3m"]
if use_env: a3m_files.append(f"{path}/bfd.mgnify30.metaeuk30.smag30.a3m")
a3m_out = open(a3m,"w")
for a3m_file in a3m_files:
for line in open(a3m_file,"r"):
line = line.replace("\x00","")
if len(line) > 0:
a3m_lines.append(line)
a3m_out.write(line)
else:
a3m_lines = open(a3m).readlines()
return "".join(a3m_lines)
#########################################################################
# utils
#########################################################################
def get_hash(x):
return hashlib.sha1(x.encode()).hexdigest()
def cov_filter(msas, deletion_matrices, cov=0):
if cov > 0:
filtered = 0
new_msas = []
new_mtxs = []
for msa,mtx in zip(msas,deletion_matrices):
new_msa = []
new_mtx = []
for s,m in zip(msa,mtx):
c = (np.array(list(s)) != "-").mean()
if c >= cov/100:
new_msa.append(s)
new_mtx.append(m)
else:
filtered += 1
new_msas.append(new_msa)
new_mtxs.append(new_mtx)
print(f"Filtered {filtered} number of sequences that don't cover at least {cov}% of query")
return new_msas, new_mtxs
else:
return msas, deletion_matrices
def homooliomerize(msas, deletion_matrices, homooligomer):
if homooligomer == 1:
return msas, deletion_matrices
else:
new_msas = []
new_mtxs = []
for o in range(homooligomer):
L = num_res * o
R = num_res * (homooligomer-(o+1))
for msa,mtx in zip(msas,deletion_matrices):
new_msas.append(["-"*L+seq+"-"*R for seq in msa])
new_mtxs.append([[0]*L+mtx+[0]*R for mtx in deletion_matrix])
return new_msas, new_mtxs
def chain_break(idx_res, Ls, length=200):
# Minkyung's code
# add big enough number to residue index to indicate chain breaks
L_prev = 0
for L_i in Ls[:-1]:
idx_res[L_prev+L_i:] += lengths
L_prev += L_i
return idx_res
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
##################################################
# plotting
##################################################
def plot_plddt_legend():
thresh = ['plDDT:','Very low (<50)','Low (60)','OK (70)','Confident (80)','Very high (>90)']
plt.figure(figsize=(1,0.1),dpi=100)
########################################
for c in ["#FFFFFF","#FF0000","#FFFF00","#00FF00","#00FFFF","#0000FF"]:
plt.bar(0, 0, color=c)
plt.legend(thresh, frameon=False,
loc='center', ncol=6,
handletextpad=1,
columnspacing=1,
markerscale=0.5,)
plt.axis(False)
return plt
def plot_confidence(plddt=None, pae=None):
use_ptm = False if paes is None else True
if use_ptm:
plt.figure(figsize=(10,3),dpi=100)
plt.subplot(1,2,1);
else:
plt.figure(figsize=(5,3),dpi=100)
plt.title('Predicted lDDT')
plt.plot(plddt)
for n in range(homooligomer+1):
x = n*(len(sequence))
plt.plot([x,x],[0,100],color="black")
plt.ylabel('plDDT')
plt.xlabel('position')
if use_ptm:
plt.subplot(1,2,2);plt.title('Predicted Aligned Error')
plt.imshow(pae, cmap="bwr",vmin=0,vmax=30)
plt.colorbar()
plt.xlabel('Scored residue')
plt.ylabel('Aligned residue')
return plt
def show_pdb(pred_output_path, show_sidechains=False, show_mainchains=False, color="lDDT"):
view = py3Dmol.view(js='https://3dmol.org/build/3Dmol.js',)
view.addModel(open(pred_output_path,'r').read(),'pdb')
if color == "lDDT":
view.setStyle({'cartoon': {'colorscheme': {'prop':'b','gradient': 'roygb','min':50,'max':90}}})
elif color == "rainbow":
view.setStyle({'cartoon': {'color':'spectrum'}})
elif color == "chain":
for n,chain,color in zip(range(homooligomer),list("ABCDEFGH"),
["lime","cyan","magenta","yellow","salmon","white","blue","orange"]):
view.setStyle({'chain':chain},{'cartoon': {'color':color}})
if show_sidechains:
BB = ['C','O','N']
view.addStyle({'and':[{'resn':["GLY","PRO"],'invert':True},{'atom':BB,'invert':True}]},
{'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
view.addStyle({'and':[{'resn':"GLY"},{'atom':'CA'}]},
{'sphere':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
view.addStyle({'and':[{'resn':"PRO"},{'atom':['C','O'],'invert':True}]},
{'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
if show_mainchains:
BB = ['C','O','N','CA']
view.addStyle({'atom':BB},{'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
view.zoomTo()
return view