ColabFold / data /beta /protein.patch
introvoyz041's picture
Migrated from GitHub
007eda2 verified
Raw
History Blame Contribute Delete
5 kB
--- /content/alphafold_ori/common/protein.py 2021-08-10 10:03:26.580274340 +0000
+++ /content/alphafold/alphafold/common/protein.py 2021-08-10 10:04:47.660884323 +0000
@@ -19,6 +19,9 @@
from alphafold.common import residue_constants
from Bio.PDB import PDBParser
import numpy as np
+from string import ascii_uppercase,ascii_lowercase
+
+CHAIN_IDs = ascii_uppercase+ascii_lowercase
FeatureDict = Mapping[str, np.ndarray]
ModelOutput = Mapping[str, Any] # Is a nested dict.
@@ -75,14 +78,15 @@
if chain_id is not None:
chain = model[chain_id]
+ chains = [chain]
else:
chains = list(model.get_chains())
- if len(chains) != 1:
- raise ValueError(
- 'Only single chain PDBs are supported when chain_id not specified. '
- f'Found {len(chains)} chains.')
- else:
- chain = chains[0]
+ # if len(chains) != 1:
+ # raise ValueError(
+ # 'Only single chain PDBs are supported when chain_id not specified. '
+ # f'Found {len(chains)} chains.')
+ # else:
+ # chain = chains[0]
atom_positions = []
aatype = []
@@ -90,31 +94,35 @@
residue_index = []
b_factors = []
- for res in chain:
- if res.id[2] != ' ':
- raise ValueError(
- f'PDB contains an insertion code at chain {chain.id} and residue '
- f'index {res.id[1]}. These are not supported.')
- res_shortname = residue_constants.restype_3to1.get(res.resname, 'X')
- restype_idx = residue_constants.restype_order.get(
- res_shortname, residue_constants.restype_num)
- pos = np.zeros((residue_constants.atom_type_num, 3))
- mask = np.zeros((residue_constants.atom_type_num,))
- res_b_factors = np.zeros((residue_constants.atom_type_num,))
- for atom in res:
- if atom.name not in residue_constants.atom_types:
+ PARAM_CHAIN_BREAK = 100
+ residue_index_prev = 0
+ for k,chain in enumerate(chains):
+ for res in chain:
+ if res.id[2] != ' ':
+ raise ValueError(
+ f'PDB contains an insertion code at chain {chain.id} and residue '
+ f'index {res.id[1]}. These are not supported.')
+ res_shortname = residue_constants.restype_3to1.get(res.resname, 'X')
+ restype_idx = residue_constants.restype_order.get(
+ res_shortname, residue_constants.restype_num)
+ pos = np.zeros((residue_constants.atom_type_num, 3))
+ mask = np.zeros((residue_constants.atom_type_num,))
+ res_b_factors = np.zeros((residue_constants.atom_type_num,))
+ for atom in res:
+ if atom.name not in residue_constants.atom_types:
+ continue
+ pos[residue_constants.atom_order[atom.name]] = atom.coord
+ mask[residue_constants.atom_order[atom.name]] = 1.
+ res_b_factors[residue_constants.atom_order[atom.name]] = atom.bfactor
+ if np.sum(mask) < 0.5:
+ # If no known atom positions are reported for the residue then skip it.
continue
- pos[residue_constants.atom_order[atom.name]] = atom.coord
- mask[residue_constants.atom_order[atom.name]] = 1.
- res_b_factors[residue_constants.atom_order[atom.name]] = atom.bfactor
- if np.sum(mask) < 0.5:
- # If no known atom positions are reported for the residue then skip it.
- continue
- aatype.append(restype_idx)
- atom_positions.append(pos)
- atom_mask.append(mask)
- residue_index.append(res.id[1])
- b_factors.append(res_b_factors)
+ aatype.append(restype_idx)
+ atom_positions.append(pos)
+ atom_mask.append(mask)
+ residue_index.append(res.id[1] + residue_index_prev + PARAM_CHAIN_BREAK*k)
+ b_factors.append(res_b_factors)
+ residue_index_prev = residue_index[-1]
return Protein(
atom_positions=np.array(atom_positions),
@@ -150,10 +158,21 @@
pdb_lines.append('MODEL 1')
atom_index = 1
- chain_id = 'A'
+ chain_index = -1
+ residue_index_prev = residue_index[0]-100
# Add all atom sites.
for i in range(aatype.shape[0]):
+ if residue_index[i] - residue_index_prev > 1: # chain break
+ chain_index += 1
+ res_num = 1
+ if chain_index > 0:
+ pdb_lines.append("TER")
+ elif residue_index[i] != residue_index_prev:
+ res_num += 1
+ residue_index_prev = residue_index[i]
+ chain_id = CHAIN_IDs[chain_index]
res_name_3 = res_1to3(aatype[i])
+ #
for atom_name, pos, mask, b_factor in zip(
atom_types, atom_positions[i], atom_mask[i], b_factors[i]):
if mask < 0.5:
@@ -169,7 +188,7 @@
# PDB is a columnar format, every space matters here!
atom_line = (f'{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}'
f'{res_name_3:>3} {chain_id:>1}'
- f'{residue_index[i]:>4}{insertion_code:>1} '
+ f'{res_num:>4}{insertion_code:>1} '
f'{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}'
f'{occupancy:>6.2f}{b_factor:>6.2f} '
f'{element:>2}{charge:>2}')