-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintfold.py
873 lines (748 loc) · 35.4 KB
/
intfold.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
#%%
import os
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
import jax
from IPython.utils import io
import subprocess
import tqdm.notebook
# --- Python imports ---
import colabfold as cf
import pairmsa
import sys
import pickle
import argparse
from urllib import request
from concurrent import futures
import json
from matplotlib import gridspec
import matplotlib.pyplot as plt
import numpy as np
import py3Dmol
import haiku as hk
from alphafold.model import model
from alphafold.model import config
from alphafold.model import data
from alphafold.model import modules
from alphafold.data import parsers
from alphafold.data import pipeline
from alphafold.data.tools import jackhmmer
from alphafold.common import protein
### Check your OS for localcolabfold
import platform
pf = platform.system()
if pf == 'Windows':
print('ColabFold on Windows')
elif pf == 'Darwin':
print('ColabFold on Mac')
device="cpu"
elif pf == 'Linux':
print('ColabFold on Linux')
device="gpu"
#%%
### Parse sequence, entry, and EC number
parser = argparse.ArgumentParser(description='Parse sequence, entry and EC number for batch processing')
parser.add_argument('--seq', type=str, help='Amino acid sequence')
parser.add_argument('--entry', type=str, help='Entry of the amino acid sequence')
parser.add_argument('--ec', type=str, help='EC number of the amino acid sequence, eg: 1.1.1.1')
parser.add_argument('--out', type=str, default='/app/intermediate/', help='Output directory')
args = parser.parse_args()
assert args.seq != None
assert args.entry != None
assert args.ec != None
def run_jackhmmer(sequence, prefix):
fasta_path = f"{prefix}.fasta"
with open(fasta_path, 'wt') as f:
f.write(f'>query\n{sequence}')
pickled_msa_path = f"{prefix}.jackhmmer.pickle"
if os.path.isfile(pickled_msa_path):
msas_dict = pickle.load(open(pickled_msa_path,"rb"))
msas, deletion_matrices, names = (msas_dict[k] for k in ['msas', 'deletion_matrices', 'names'])
full_msa = []
for msa in msas:
full_msa += msa
else:
# --- Find the closest source ---
test_url_pattern = 'https://storage.googleapis.com/alphafold-colab{:s}/latest/uniref90_2021_03.fasta.1'
ex = futures.ThreadPoolExecutor(3)
def fetch(source):
request.urlretrieve(test_url_pattern.format(source))
return source
fs = [ex.submit(fetch, source) for source in ['', '-europe', '-asia']]
source = None
for f in futures.as_completed(fs):
source = f.result()
ex.shutdown()
break
jackhmmer_binary_path = '/usr/bin/jackhmmer'
dbs = []
num_jackhmmer_chunks = {'uniref90': 59, 'smallbfd': 17, 'mgnify': 71}
total_jackhmmer_chunks = sum(num_jackhmmer_chunks.values())
with tqdm.notebook.tqdm(total=total_jackhmmer_chunks, bar_format=TQDM_BAR_FORMAT) as pbar:
def jackhmmer_chunk_callback(i):
pbar.update(n=1)
pbar.set_description('Searching uniref90')
jackhmmer_uniref90_runner = jackhmmer.Jackhmmer(
binary_path=jackhmmer_binary_path,
database_path=f'https://storage.googleapis.com/alphafold-colab{source}/latest/uniref90_2021_03.fasta',
get_tblout=True,
num_streamed_chunks=num_jackhmmer_chunks['uniref90'],
streaming_callback=jackhmmer_chunk_callback,
z_value=135301051)
dbs.append(('uniref90', jackhmmer_uniref90_runner.query(fasta_path)))
pbar.set_description('Searching smallbfd')
jackhmmer_smallbfd_runner = jackhmmer.Jackhmmer(
binary_path=jackhmmer_binary_path,
database_path=f'https://storage.googleapis.com/alphafold-colab{source}/latest/bfd-first_non_consensus_sequences.fasta',
get_tblout=True,
num_streamed_chunks=num_jackhmmer_chunks['smallbfd'],
streaming_callback=jackhmmer_chunk_callback,
z_value=65984053)
dbs.append(('smallbfd', jackhmmer_smallbfd_runner.query(fasta_path)))
pbar.set_description('Searching mgnify')
jackhmmer_mgnify_runner = jackhmmer.Jackhmmer(
binary_path=jackhmmer_binary_path,
database_path=f'https://storage.googleapis.com/alphafold-colab{source}/latest/mgy_clusters_2019_05.fasta',
get_tblout=True,
num_streamed_chunks=num_jackhmmer_chunks['mgnify'],
streaming_callback=jackhmmer_chunk_callback,
z_value=304820129)
dbs.append(('mgnify', jackhmmer_mgnify_runner.query(fasta_path)))
# --- Extract the MSAs and visualize ---
# Extract the MSAs from the Stockholm files.
# NB: deduplication happens later in pipeline.make_msa_features.
mgnify_max_hits = 501
msas = []
deletion_matrices = []
names = []
for db_name, db_results in dbs:
unsorted_results = []
for i, result in enumerate(db_results):
msa, deletion_matrix, target_names = parsers.parse_stockholm(result['sto'])
e_values_dict = parsers.parse_e_values_from_tblout(result['tbl'])
e_values = [e_values_dict[t.split('/')[0]] for t in target_names]
zipped_results = zip(msa, deletion_matrix, target_names, e_values)
if i != 0:
# Only take query from the first chunk
zipped_results = [x for x in zipped_results if x[2] != 'query']
unsorted_results.extend(zipped_results)
sorted_by_evalue = sorted(unsorted_results, key=lambda x: x[3])
db_msas, db_deletion_matrices, db_names, _ = zip(*sorted_by_evalue)
if db_msas:
if db_name == 'mgnify':
db_msas = db_msas[:mgnify_max_hits]
db_deletion_matrices = db_deletion_matrices[:mgnify_max_hits]
db_names = db_names[:mgnify_max_hits]
msas.append(db_msas)
deletion_matrices.append(db_deletion_matrices)
names.append(db_names)
msa_size = len(set(db_msas))
print(f'{msa_size} Sequences Found in {db_name}')
pickle.dump({"msas":msas,
"deletion_matrices":deletion_matrices,
"names":names}, open(pickled_msa_path,"wb"))
return msas, deletion_matrices, names
import re
# define sequence
#entry = 'A0A2X4TJX1'
#sequence = 'MKPLPVPAFTLIAGDRSISGSATGTPYELRKLMKFAGRSNVAPTTELFAMSQINEAIQHVRDGKARYRVVLKADF'
#sequence = 'PIAQIHILEGRSDEQKETLIREVSEAISRSLDAPLTSVRVIITEMAKGHFGIGGELASK' #@param {type:"string"}
entry = args.entry
sequence = args.seq
ec = args.ec
sequence = re.sub("[^A-Z:/]", "", sequence.upper())
sequence = re.sub(":+",":",sequence)
sequence = re.sub("/+","/",sequence)
sequence = re.sub("^[:/]+","",sequence)
sequence = re.sub("[:/]+$","",sequence)
try:
ec1 = ec.split('.')[0]
ec2 = ec.split('.')[1]
ec3 = ec.split('.')[2]
ec4 = ec.split('.')[3]
except:
print('EC number not in correct format.')
exit(-1)
jobname = args.ec + '_' + args.entry#"test" #@param {type:"string"}
jobname = re.sub(r'\W+', '', jobname)
# define number of copies
homooligomer = "1" #@param {type:"string"}
homooligomer = re.sub("[:/]+",":",homooligomer)
homooligomer = re.sub("^[:/]+","",homooligomer)
homooligomer = re.sub("[:/]+$","",homooligomer)
if len(homooligomer) == 0: homooligomer = "1"
homooligomer = re.sub("[^0-9:]", "", homooligomer)
homooligomers = [int(h) for h in homooligomer.split(":")]
#@markdown - `sequence` Specify protein sequence to be modelled.
#@markdown - Use `/` to specify intra-protein chainbreaks (for trimming regions within protein).
#@markdown - Use `:` to specify inter-protein chainbreaks (for modeling protein-protein hetero-complexes).
#@markdown - For example, sequence `AC/DE:FGH` will be modelled as polypeptides: `AC`, `DE` and `FGH`. A seperate MSA will be generates for `ACDE` and `FGH`.
#@markdown If `pair_msa` is enabled, `ACDE`'s MSA will be paired with `FGH`'s MSA.
#@markdown - `homooligomer` Define number of copies in a homo-oligomeric assembly.
#@markdown - Use `:` to specify different homooligomeric state (copy numer) for each component of the complex.
#@markdown - For example, **sequence:**`ABC:DEF`, **homooligomer:** `2:1`, the first protein `ABC` will be modeled as a homodimer (2 copies) and second `DEF` a monomer (1 copy).
ori_sequence = sequence
sequence = sequence.replace("/","").replace(":","")
seqs = ori_sequence.replace("/","").split(":")
if len(seqs) != len(homooligomers):
if len(homooligomers) == 1:
homooligomers = [homooligomers[0]] * len(seqs)
homooligomer = ":".join([str(h) for h in homooligomers])
else:
while len(seqs) > len(homooligomers):
homooligomers.append(1)
homooligomers = homooligomers[:len(seqs)]
homooligomer = ":".join([str(h) for h in homooligomers])
print("WARNING: Mismatch between number of breaks ':' in 'sequence' and 'homooligomer' definition")
full_sequence = "".join([s*h for s,h in zip(seqs,homooligomers)])
# prediction directory
output_dir = os.path.join(args.out, ec1, ec2, ec3, ec4)
os.makedirs(output_dir, exist_ok=True)
MIN_SEQUENCE_LENGTH = 16
MAX_SEQUENCE_LENGTH = 2500
aatypes = set('ACDEFGHIKLMNPQRSTVWY') # 20 standard aatypes
if not set(full_sequence).issubset(aatypes):
raise Exception(f'Input sequence contains non-amino acid letters: {set(sequence) - aatypes}. AlphaFold only supports 20 standard amino acids as inputs.')
if len(full_sequence) < MIN_SEQUENCE_LENGTH:
raise Exception(f'Input sequence is too short: {len(full_sequence)} amino acids, while the minimum is {MIN_SEQUENCE_LENGTH}')
if len(full_sequence) > MAX_SEQUENCE_LENGTH:
raise Exception(f'Input sequence is too long: {len(full_sequence)} amino acids, while the maximum is {MAX_SEQUENCE_LENGTH}. Please use the full AlphaFold system for long sequences.')
if len(full_sequence) > 1400:
print(f"WARNING: For a typical Google-Colab-GPU (16G) session, the max total length is ~1400 residues. You are at {len(full_sequence)}! Run Alphafold may crash.")
print(f"homooligomer: '{homooligomer}'")
print(f"total_length: '{len(full_sequence)}'")
print(f"working_directory: '{output_dir}'")
#%%
TQDM_BAR_FORMAT = '{l_bar}{bar}| {n_fmt}/{total_fmt} [elapsed: {elapsed} remaining: {remaining}]'
#@markdown Once this cell has been executed, you will see
#@markdown statistics about the multiple sequence alignment
#@markdown (MSA) that will be used by AlphaFold. In particular,
#@markdown you’ll see how well each residue is covered by similar
#@markdown sequences in the MSA.
#@markdown (Note that the search against databases and the actual prediction can take some time, from minutes to hours, depending on the length of the protein and what type of GPU you are allocated by Colab.)
#@markdown ---
msa_method = "mmseqs2" #@param ["mmseqs2","jackhmmer","single_sequence","precomputed"]
#@markdown - `mmseqs2` - FAST method from [ColabFold](https://github.com/sokrypton/ColabFold)
#@markdown - `jackhmmer` - default method from Deepmind (SLOW, but may find more/less sequences).
#@markdown - `single_sequence` - use single sequence input
#@markdown - `precomputed` If you have previously run this notebook and saved the results,
#@markdown you can skip this step by uploading
#@markdown the previously generated `prediction_?????/msa.pickle`
#@markdown ---
#@markdown **custom msa options**
add_custom_msa = False #@param {type:"boolean"}
msa_format = "fas" #@param ["fas","a2m","a3m","sto","psi","clu"]
#@markdown - `add_custom_msa` - If enabled, you'll get an option to upload your custom MSA in the specified `msa_format`. Note: Your MSA will be supplemented with those from 'mmseqs2' or 'jackhmmer', unless `msa_method` is set to 'single_sequence'.
#@markdown ---
#@markdown **pair msa options**
#@markdown Experimental option for protein complexes. Pairing currently only supported for proteins in same operon (prokaryotic genomes).
pair_mode = "unpaired" #@param ["unpaired","unpaired+paired","paired"] {type:"string"}
#@markdown - `unpaired` - generate seperate MSA for each protein.
#@markdown - `unpaired+paired` - attempt to pair sequences from the same operon within the genome.
#@markdown - `paired` - only use sequences that were sucessfully paired.
#@markdown Options to prefilter each MSA before pairing. (It might help if there are any paralogs in the complex.)
pair_cov = 50 #@param [0,25,50,75,90] {type:"raw"}
pair_qid = 20 #@param [0,15,20,30,40,50] {type:"raw"}
#@markdown - `pair_cov` prefilter each MSA to minimum coverage with query (%) before pairing.
#@markdown - `pair_qid` prefilter each MSA to minimum sequence identity with query (%) before pairing.
# --- Search against genetic databases ---
os.makedirs('tmp', exist_ok=True)
msas, deletion_matrices = [],[]
if add_custom_msa:
print(f"upload custom msa in '{msa_format}' format")
msa_dict = files.upload()
lines = msa_dict[list(msa_dict.keys())[0]].decode()
# convert to a3m
with open(f"tmp/upload.{msa_format}","w") as tmp_upload:
tmp_upload.write(lines)
os.system(f"reformat.pl {msa_format} a3m tmp/upload.{msa_format} tmp/upload.a3m")
a3m_lines = open("tmp/upload.a3m","r").read()
# parse
msa, mtx = parsers.parse_a3m(a3m_lines)
msas.append(msa)
deletion_matrices.append(mtx)
if len(msas[0][0]) != len(sequence):
raise ValueError("ERROR: the length of msa does not match input sequence")
if msa_method == "precomputed":
print("upload precomputed pickled msa from previous run")
pickled_msa_dict = files.upload()
msas_dict = pickle.loads(pickled_msa_dict[list(pickled_msa_dict.keys())[0]])
msas, deletion_matrices = (msas_dict[k] for k in ['msas', 'deletion_matrices'])
elif msa_method == "single_sequence":
if len(msas) == 0:
msas.append([sequence])
deletion_matrices.append([[0]*len(sequence)])
else:
seqs = ori_sequence.replace('/','').split(':')
_blank_seq = ["-" * len(seq) for seq in seqs]
_blank_mtx = [[0] * len(seq) for seq in seqs]
def _pad(ns,vals,mode):
if mode == "seq": _blank = _blank_seq.copy()
if mode == "mtx": _blank = _blank_mtx.copy()
if isinstance(ns, list):
for n,val in zip(ns,vals): _blank[n] = val
else: _blank[ns] = vals
if mode == "seq": return "".join(_blank)
if mode == "mtx": return sum(_blank,[])
if len(seqs) == 1 or "unpaired" in pair_mode:
# gather msas
if msa_method == "mmseqs2":
prefix = cf.get_hash("".join(seqs))
prefix = os.path.join('tmp',prefix)
print(f"running mmseqs2")
A3M_LINES = cf.run_mmseqs2(seqs, prefix, filter=True)
for n, seq in enumerate(seqs):
# tmp directory
prefix = cf.get_hash(seq)
prefix = os.path.join('tmp',prefix)
if msa_method == "mmseqs2":
# run mmseqs2
a3m_lines = A3M_LINES[n]
msa, mtx = parsers.parse_a3m(a3m_lines)
msas_, mtxs_ = [msa],[mtx]
elif msa_method == "jackhmmer":
print(f"running jackhmmer on seq_{n}")
# run jackhmmer
msas_, mtxs_, names_ = ([sum(x,())] for x in run_jackhmmer(seq, prefix))
# pad sequences
for msa_,mtx_ in zip(msas_,mtxs_):
msa,mtx = [sequence],[[0]*len(sequence)]
for s,m in zip(msa_,mtx_):
msa.append(_pad(n,s,"seq"))
mtx.append(_pad(n,m,"mtx"))
msas.append(msa)
deletion_matrices.append(mtx)
####################################################################################
# PAIR_MSA
####################################################################################
if len(seqs) > 1 and (pair_mode == "paired" or pair_mode == "unpaired+paired"):
print("attempting to pair some sequences...")
if msa_method == "mmseqs2":
prefix = cf.get_hash("".join(seqs))
prefix = os.path.join('tmp',prefix)
print(f"running mmseqs2_noenv_nofilter on all seqs")
A3M_LINES = cf.run_mmseqs2(seqs, prefix, use_env=False, use_filter=False)
_data = []
for a in range(len(seqs)):
print(f"prepping seq_{a}")
_seq = seqs[a]
_prefix = os.path.join('tmp',cf.get_hash(_seq))
if msa_method == "mmseqs2":
a3m_lines = A3M_LINES[a]
_msa, _mtx, _lab = pairmsa.parse_a3m(a3m_lines,
filter_qid=pair_qid/100,
filter_cov=pair_cov/100)
elif msa_method == "jackhmmer":
_msas, _mtxs, _names = run_jackhmmer(_seq, _prefix)
_msa, _mtx, _lab = pairmsa.get_uni_jackhmmer(_msas[0], _mtxs[0], _names[0],
filter_qid=pair_qid/100,
filter_cov=pair_cov/100)
if len(_msa) > 1:
_data.append(pairmsa.hash_it(_msa, _lab, _mtx, call_uniprot=False))
else:
_data.append(None)
Ln = len(seqs)
O = [[None for _ in seqs] for _ in seqs]
for a in range(Ln):
if _data[a] is not None:
for b in range(a+1,Ln):
if _data[b] is not None:
print(f"attempting pairwise stitch for {a} {b}")
O[a][b] = pairmsa._stitch(_data[a],_data[b])
_seq_a, _seq_b, _mtx_a, _mtx_b = (*O[a][b]["seq"],*O[a][b]["mtx"])
##############################################
# filter to remove redundant sequences
##############################################
ok = []
with open("tmp/tmp.fas","w") as fas_file:
fas_file.writelines([f">{n}\n{a+b}\n" for n,(a,b) in enumerate(zip(_seq_a,_seq_b))])
os.system("hhfilter -maxseq 1000000 -i tmp/tmp.fas -o tmp/tmp.id90.fas -id 90")
for line in open("tmp/tmp.id90.fas","r"):
if line.startswith(">"): ok.append(int(line[1:]))
##############################################
print(f"found {len(_seq_a)} pairs ({len(ok)} after filtering)")
if len(_seq_a) > 0:
msa,mtx = [sequence],[[0]*len(sequence)]
for s_a,s_b,m_a,m_b in zip(_seq_a, _seq_b, _mtx_a, _mtx_b):
msa.append(_pad([a,b],[s_a,s_b],"seq"))
mtx.append(_pad([a,b],[m_a,m_b],"mtx"))
msas.append(msa)
deletion_matrices.append(mtx)
'''
# triwise stitching (WIP)
if Ln > 2:
for a in range(Ln):
for b in range(a+1,Ln):
for c in range(b+1,Ln):
if O[a][b] is not None and O[b][c] is not None:
print(f"attempting triwise stitch for {a} {b} {c}")
list_ab = O[a][b]["lab"][1]
list_bc = O[b][c]["lab"][0]
msa,mtx = [sequence],[[0]*len(sequence)]
for i,l_b in enumerate(list_ab):
if l_b in list_bc:
j = list_bc.index(l_b)
s_a = O[a][b]["seq"][0][i]
s_b = O[a][b]["seq"][1][i]
s_c = O[b][c]["seq"][1][j]
m_a = O[a][b]["mtx"][0][i]
m_b = O[a][b]["mtx"][1][i]
m_c = O[b][c]["mtx"][1][j]
msa.append(_pad([a,b,c],[s_a,s_b,s_c],"seq"))
mtx.append(_pad([a,b,c],[m_a,m_b,m_c],"mtx"))
if len(msa) > 1:
msas.append(msa)
deletion_matrices.append(mtx)
print(f"found {len(msa)} triplets")
'''
####################################################################################
####################################################################################
# save MSA as pickle
#pickle.dump({"msas":msas,"deletion_matrices":deletion_matrices},
# open(os.path.join(output_dir,"msa.pickle"),"wb"))
make_msa_plot = False#len(msas[0]) > 1
if make_msa_plot:
plt = cf.plot_msas(msas, ori_sequence)
plt.savefig(os.path.join(output_dir,"msa_coverage.png"), bbox_inches = 'tight', dpi=300)
#%%
#@title run alphafold
num_relax = "None"
rank_by = "pLDDT" #@param ["pLDDT","pTMscore"]
use_turbo = True #@param {type:"boolean"}
max_msa = "512:1024" #@param ["512:1024", "256:512", "128:256", "64:128", "32:64"]
max_msa_clusters, max_extra_msa = [int(x) for x in max_msa.split(":")]
#@markdown - `rank_by` specify metric to use for ranking models (For protein-protein complexes, we recommend pTMscore)
#@markdown - `use_turbo` introduces a few modifications (compile once, swap params, adjust max_msa) to speedup and reduce memory requirements. Disable for default behavior.
#@markdown - `max_msa` defines: `max_msa_clusters:max_extra_msa` number of sequences to use. When adjusting after GPU crash, be sure to `Runtime` → `Restart runtime`. (Lowering will reduce GPU requirements, but may result in poor model quality. This option ignored if `use_turbo` is disabled)
show_images = True #@param {type:"boolean"}
#@markdown - `show_images` To make things more exciting we show images of the predicted structures as they are being generated. (WARNING: the order of images displayed does not reflect any ranking).
#@markdown ---
#@markdown #### Sampling options
#@markdown There are two stochastic parts of the pipeline. Within the feature generation (choice of cluster centers) and within the model (dropout).
#@markdown To get structure diversity, you can iterate through a fixed number of random_seeds (using `num_samples`) and/or enable dropout (using `is_training`).
num_models = 1 #@param [1,2,3,4,5] {type:"raw"}
use_ptm = True #@param {type:"boolean"}
num_ensemble = 1 #@param [1,8] {type:"raw"}
max_recycles = 3 #@param [1,3,6,12,24,48] {type:"raw"}
tol = 0 #@param [0,0.1,0.5,1] {type:"raw"}
is_training = False #@param {type:"boolean"}
num_samples = 1 #@param [1,2,4,8,16,32] {type:"raw"}
subsample_msa = True #@param {type:"boolean"}
#@markdown - `subsample_msa` subsample large MSA to `3E7/length` sequences to avoid crashing the preprocessing protocol. (This option ignored if `use_turbo` is disabled.)
save_pae_json = False#True
save_tmp_pdb = False#True
if use_ptm == False and rank_by == "pTMscore":
print("WARNING: models will be ranked by pLDDT, 'use_ptm' is needed to compute pTMscore")
rank_by = "pLDDT"
#############################
# delete old files
#############################
for f in os.listdir(output_dir):
if "rank_" in f:
os.remove(os.path.join(output_dir, f))
#############################
# homooligomerize
#############################
lengths = [len(seq) for seq in seqs]
msas_mod, deletion_matrices_mod = cf.homooligomerize_heterooligomer(msas, deletion_matrices,
lengths, homooligomers)
#############################
# define input features
#############################
def _placeholder_template_feats(num_templates_, num_res_):
return {
'template_aatype': np.zeros([num_templates_, num_res_, 22], np.float32),
'template_all_atom_masks': np.zeros([num_templates_, num_res_, 37, 3], np.float32),
'template_all_atom_positions': np.zeros([num_templates_, num_res_, 37], np.float32),
'template_domain_names': np.zeros([num_templates_], np.float32),
'template_sum_probs': np.zeros([num_templates_], np.float32),
}
num_res = len(full_sequence)
feature_dict = {}
feature_dict.update(pipeline.make_sequence_features(full_sequence, 'test', num_res))
feature_dict.update(pipeline.make_msa_features(msas_mod, deletion_matrices=deletion_matrices_mod))
if not use_turbo:
feature_dict.update(_placeholder_template_feats(0, num_res))
def do_subsample_msa(F, random_seed=0):
'''subsample msa to avoid running out of memory'''
N = len(F["msa"])
L = len(F["residue_index"])
N_ = int(3E7/L)
if N > N_:
print(f"whhhaaa... too many sequences ({N}) subsampling to {N_}")
np.random.seed(random_seed)
idx = np.append(0,np.random.permutation(np.arange(1,N)))[:N_]
F_ = {}
F_["msa"] = F["msa"][idx]
F_["deletion_matrix_int"] = F["deletion_matrix_int"][idx]
F_["num_alignments"] = np.full_like(F["num_alignments"],N_)
for k in ['aatype', 'between_segment_residues',
'domain_name', 'residue_index',
'seq_length', 'sequence']:
F_[k] = F[k]
return F_
else:
return F
################################
# set chain breaks
################################
Ls = []
for seq,h in zip(ori_sequence.split(":"),homooligomers):
Ls += [len(s) for s in seq.split("/")] * h
Ls_plot = sum([[len(seq)]*h for seq,h in zip(seqs,homooligomers)],[])
feature_dict['residue_index'] = cf.chain_break(feature_dict['residue_index'], Ls)
###########################
# run alphafold
###########################
def parse_results(prediction_result, processed_feature_dict):
b_factors = prediction_result['plddt'][:,None] * prediction_result['structure_module']['final_atom_mask']
dist_bins = jax.numpy.append(0,prediction_result["distogram"]["bin_edges"])
dist_mtx = dist_bins[prediction_result["distogram"]["logits"].argmax(-1)]
contact_mtx = jax.nn.softmax(prediction_result["distogram"]["logits"])[:,:,dist_bins < 8].sum(-1)
out = {"unrelaxed_protein": protein.from_prediction(processed_feature_dict, prediction_result, b_factors=b_factors),
"plddt": prediction_result['plddt'],
"pLDDT": prediction_result['plddt'].mean(),
"dists": dist_mtx,
"adj": contact_mtx}
if "ptm" in prediction_result:
out.update({"pae": prediction_result['predicted_aligned_error'],
"pTMscore": prediction_result['ptm']})
return out
model_names = ['model_1', 'model_2', 'model_3', 'model_4', 'model_5'][:num_models]
total = len(model_names) * num_samples
with tqdm.notebook.tqdm(total=total, bar_format=TQDM_BAR_FORMAT) as pbar:
#######################################################################
# precompile model and recompile only if length changes
#######################################################################
if use_turbo:
name = "model_5_ptm" if use_ptm else "model_5"
N = len(feature_dict["msa"])
L = len(feature_dict["residue_index"])
compiled = (N, L, use_ptm, max_recycles, tol, num_ensemble, max_msa, is_training)
if "COMPILED" in dir():
if COMPILED != compiled: recompile = True
else: recompile = True
if recompile:
cf.clear_mem(device)
cfg = config.model_config(name)
# set size of msa (to reduce memory requirements)
msa_clusters = min(N, max_msa_clusters)
cfg.data.eval.max_msa_clusters = msa_clusters
cfg.data.common.max_extra_msa = max(min(N-msa_clusters,max_extra_msa),1)
cfg.data.common.num_recycle = max_recycles
cfg.model.num_recycle = max_recycles
cfg.model.recycle_tol = tol
cfg.data.eval.num_ensemble = num_ensemble
params = data.get_model_haiku_params(name,'./alphafold/data')
model_runner = model.RunModel(cfg, params, is_training=is_training)
COMPILED = compiled
recompile = False
else:
cf.clear_mem(device)
recompile = True
# cleanup
if "outs" in dir(): del outs
outs = {}
cf.clear_mem("cpu")
#######################################################################
def report(key):
pbar.update(n=1)
o = outs[key]
line = f"{key} recycles:{o['recycles']} tol:{o['tol']:.2f} pLDDT:{o['pLDDT']:.2f}"
if use_ptm: line += f" pTMscore:{o['pTMscore']:.2f}"
print(line)
if show_images:
fig = cf.plot_protein(o['unrelaxed_protein'], Ls=Ls_plot, dpi=100)
# plt.show()
plt.ion()
if save_tmp_pdb:
tmp_pdb_path = os.path.join(output_dir,f'unranked_{key}_unrelaxed.pdb')
pdb_lines = protein.to_pdb(o['unrelaxed_protein'])
with open(tmp_pdb_path, 'w') as f: f.write(pdb_lines)
if use_turbo:
# go through each random_seed
for seed in range(num_samples):
# prep input features
if subsample_msa:
sampled_feats_dict = do_subsample_msa(feature_dict, random_seed=seed)
processed_feature_dict = model_runner.process_features(sampled_feats_dict, random_seed=seed)
else:
processed_feature_dict = model_runner.process_features(feature_dict, random_seed=seed)
# go through each model
for num, model_name in enumerate(model_names):
name = model_name+"_ptm" if use_ptm else model_name
key = f"{name}_seed_{seed}"
pbar.set_description(f'Running {key}')
# replace model parameters
params = data.get_model_haiku_params(name, './alphafold/data')
for k in model_runner.params.keys():
model_runner.params[k] = params[k]
# predict
prediction_result, (r, t) = cf.to(model_runner.predict(processed_feature_dict, random_seed=seed),"cpu")
# save results
outs[key] = parse_results(prediction_result, processed_feature_dict)
np.save('{}/{}_msa.npy'.format(output_dir, entry), prediction_result['representations']['msa'])
np.save('{}/{}_msa_first_row.npy'.format(output_dir, entry), prediction_result['representations']['msa_first_row'])
np.save('{}/{}_pair.npy'.format(output_dir, entry), prediction_result['representations']['pair'])
np.save('{}/{}_single.npy'.format(output_dir, entry), prediction_result['representations']['single'])
exit()
outs[key].update({"recycles":r, "tol":t})
report(key)
del prediction_result, params
del sampled_feats_dict, processed_feature_dict
else:
# go through each model
for num, model_name in enumerate(model_names):
name = model_name+"_ptm" if use_ptm else model_name
params = data.get_model_haiku_params(name, './alphafold/data')
cfg = config.model_config(name)
cfg.data.common.num_recycle = cfg.model.num_recycle = max_recycles
cfg.model.recycle_tol = tol
cfg.data.eval.num_ensemble = num_ensemble
model_runner = model.RunModel(cfg, params, is_training=is_training)
# go through each random_seed
for seed in range(num_samples):
key = f"{name}_seed_{seed}"
pbar.set_description(f'Running {key}')
processed_feature_dict = model_runner.process_features(feature_dict, random_seed=seed)
prediction_result, (r, t) = cf.to(model_runner.predict(processed_feature_dict, random_seed=seed),"cpu")
outs[key] = parse_results(prediction_result, processed_feature_dict)
outs[key].update({"recycles":r, "tol":t})
report(key)
# cleanup
del processed_feature_dict, prediction_result
del params, model_runner, cfg
cf.clear_mem("gpu")
# delete old files
for f in os.listdir(output_dir):
if "rank" in f:
os.remove(os.path.join(output_dir, f))
# Find the best model according to the mean pLDDT.
model_rank = list(outs.keys())
model_rank = [model_rank[i] for i in np.argsort([outs[x][rank_by] for x in model_rank])[::-1]]
# Write out the prediction
for n,key in enumerate(model_rank):
prefix = f"rank_{n+1}_{key}"
pred_output_path = os.path.join(output_dir,f'{prefix}_unrelaxed.pdb')
fig = cf.plot_protein(outs[key]["unrelaxed_protein"], Ls=Ls_plot, dpi=200)
#plt.savefig(os.path.join(output_dir,f'{prefix}.png'), bbox_inches = 'tight')
plt.close(fig)
pdb_lines = protein.to_pdb(outs[key]["unrelaxed_protein"])
#with open(pred_output_path, 'w') as f:
#f.write(pdb_lines)
############################################################
#print(f"model rank based on {rank_by}")
#for n,key in enumerate(model_rank):
# print(f"rank_{n+1}_{key} {rank_by}:{outs[key][rank_by]:.2f}")
#%%
#@title Refine structures with Amber-Relax (Optional)
num_relax = "None" #@param ["None", "Top1", "Top5", "All"] {type:"string"}
if num_relax == "None":
num_relax = 0
elif num_relax == "Top1":
num_relax = 1
elif num_relax == "Top5":
num_relax = 5
else:
num_relax = len(model_names) * num_samples
if num_relax > 0:
if "relax" not in dir():
# add conda environment to path
sys.path.append('./colabfold-conda/lib/python3.7/site-packages')
# import libraries
from alphafold.relax import relax
from alphafold.relax import utils
with tqdm.notebook.tqdm(total=num_relax, bar_format=TQDM_BAR_FORMAT) as pbar:
pbar.set_description(f'AMBER relaxation')
for n,key in enumerate(model_rank):
if n < num_relax:
prefix = f"rank_{n+1}_{key}"
pred_output_path = os.path.join(output_dir,f'{prefix}_relaxed.pdb')
if not os.path.isfile(pred_output_path):
amber_relaxer = relax.AmberRelaxation(
max_iterations=0,
tolerance=2.39,
stiffness=10.0,
exclude_residues=[],
max_outer_iterations=20)
relaxed_pdb_lines, _, _ = amber_relaxer.process(prot=outs[key]["unrelaxed_protein"])
with open(pred_output_path, 'w') as f:
f.write(relaxed_pdb_lines)
pbar.update(n=1)
#%%
#@title Display 3D structure {run: "auto"}
rank_num = 1 #@param ["1", "2", "3", "4", "5"] {type:"raw"}
color = "lDDT" #@param ["chain", "lDDT", "rainbow"]
show_sidechains = False #@param {type:"boolean"}
show_mainchains = False #@param {type:"boolean"}
key = model_rank[rank_num-1]
prefix = f"rank_{rank_num}_{key}"
pred_output_path = os.path.join(output_dir,f'{prefix}_relaxed.pdb')
if not os.path.isfile(pred_output_path):
pred_output_path = os.path.join(output_dir,f'{prefix}_unrelaxed.pdb')
cf.show_pdb(pred_output_path, show_sidechains, show_mainchains, color, Ls=Ls_plot).show()
if color == "lDDT": cf.plot_plddt_legend().show()
if use_ptm:
cf.plot_confidence(outs[key]["plddt"], outs[key]["pae"], Ls=Ls_plot).show()
else:
cf.plot_confidence(outs[key]["plddt"], Ls=Ls_plot).show()
#%%
#@title Extra outputs
dpi = 300#@param {type:"integer"}
save_to_txt = False#True #@param {type:"boolean"}
save_pae_json = False# True #@param {type:"boolean"}
#@markdown - save data used to generate contact and distogram plots below to text file (pae values can be found in json file if `use_ptm` is enabled)
if use_ptm:
#print("predicted alignment error")
cf.plot_paes([outs[k]["pae"] for k in model_rank], Ls=Ls_plot, dpi=dpi)
#plt.savefig(os.path.join(output_dir,f'predicted_alignment_error.png'), bbox_inches = 'tight', dpi=np.maximum(200,dpi))
# plt.show()
#print("predicted contacts")
cf.plot_adjs([outs[k]["adj"] for k in model_rank], Ls=Ls_plot, dpi=dpi)
#plt.savefig(os.path.join(output_dir,f'predicted_contacts.png'), bbox_inches = 'tight', dpi=np.maximum(200,dpi))
# plt.show()
#print("predicted distogram")
cf.plot_dists([outs[k]["dists"] for k in model_rank], Ls=Ls_plot, dpi=dpi)
#plt.savefig(os.path.join(output_dir,f'predicted_distogram.png'), bbox_inches = 'tight', dpi=np.maximum(200,dpi))
# plt.show()
#print("predicted LDDT")
cf.plot_plddts([outs[k]["plddt"] for k in model_rank], Ls=Ls_plot, dpi=dpi)
#plt.savefig(os.path.join(output_dir,f'predicted_LDDT.png'), bbox_inches = 'tight', dpi=np.maximum(200,dpi))
# plt.show()
def do_save_to_txt(filename, adj, dists):
adj = np.asarray(adj)
dists = np.asarray(dists)
L = len(adj)
with open(filename,"w") as out:
out.write("i\tj\taa_i\taa_j\tp(cbcb<8)\tmaxdistbin\n")
for i in range(L):
for j in range(i+1,L):
if dists[i][j] < 21.68 or adj[i][j] >= 0.001:
line = f"{i+1}\t{j+1}\t{full_sequence[i]}\t{full_sequence[j]}\t{adj[i][j]:.3f}"
line += f"\t>{dists[i][j]:.2f}" if dists[i][j] == 21.6875 else f"\t{dists[i][j]:.2f}"
out.write(f"{line}\n")
for n,key in enumerate(model_rank):
if save_to_txt:
txt_filename = os.path.join(output_dir,f'rank_{n+1}_{key}.raw.txt')
do_save_to_txt(txt_filename,adj=outs[key]["adj"],dists=outs[key]["dists"])
if use_ptm and save_pae_json:
pae = outs[key]["pae"]
max_pae = pae.max()
# Save pLDDT and predicted aligned error (if it exists)
pae_output_path = os.path.join(output_dir,f'rank_{n+1}_{key}_pae.json')
# Save predicted aligned error in the same format as the AF EMBL DB
rounded_errors = np.round(np.asarray(pae), decimals=1)
indices = np.indices((len(rounded_errors), len(rounded_errors))) + 1
indices_1 = indices[0].flatten().tolist()
indices_2 = indices[1].flatten().tolist()
pae_data = json.dumps([{
'residue1': indices_1,
'residue2': indices_2,
'distance': rounded_errors.flatten().tolist(),
'max_predicted_aligned_error': max_pae.item()
}],
indent=None,
separators=(',', ':'))
with open(pae_output_path, 'w') as f:
f.write(pae_data)
#%%