Skip to content

Instantly share code, notes, and snippets.

@neksa
Created September 15, 2022 18:17
Show Gist options
  • Save neksa/1abc9746fcba46bfc043ddbebfee8247 to your computer and use it in GitHub Desktop.
Save neksa/1abc9746fcba46bfc043ddbebfee8247 to your computer and use it in GitHub Desktop.
Pattern for adding values to b-factors with biotite
from biotite.structure.io.pdb import get_structure, PDBFile
#################################################
# residue id, atom name : value in [0..1] domain
# input data
values = {
(1, 'CA'): 0.5,
(2, 'C'): 0.8,
}
pdb_fname = "test.pdb"
pdb_fname_output = "test_bfactor.pdb"
##############################
# read PDB structure
structure = PDBFile.read(pdb_fname).get_structure(model=1)
##############################
# assign bfactors
structure.set_annotation(
"b_factor", np.zeros(structure.array_length(), float)
)
for (resi, name), value in values.items():
mask = (structure.atom_name == name) & (structure.res_id == resi)
structure.b_factor[mask] = value * 100.0 # scale to make b-factor in range [0..100]
##############################
# save PDB file with bfactors
pdb_file = PDBFile()
pdb_file.set_structure(structure)
pdb_file.write(pdb_fname_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment