Skip to content

Instantly share code, notes, and snippets.

@andersx
Created March 27, 2018 15:05
Show Gist options
  • Select an option

  • Save andersx/a49df1707a3763703960c5ce2d56ffe2 to your computer and use it in GitHub Desktop.

Select an option

Save andersx/a49df1707a3763703960c5ce2d56ffe2 to your computer and use it in GitHub Desktop.
Example of how to align multiple PDB structures to the same reference.
# The MIT License
#
# Copyright (c) 2010-2018 Anders S. Christensen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import Bio.PDB
# Select what residues numbers you wish to align
# and put them in a list
start_id = 1
end_id = 70
atoms_to_be_aligned = range(start_id, end_id + 1)
# Start the parser
pdb_parser = Bio.PDB.PDBParser(QUIET = True)
# Get the structures
ref_structure = pdb_parser.get_structure("reference", "1ubq.pdb")
sample_structure = pdb_parser.get_structure("sample", "1d3z.pdb")
# Define the reference model and a list of atoms
ref_model = ref_structure[0]
ref_atoms = []
# Iterate of all chains in the model in order to find all residues
for ref_chain in ref_model:
# Iterate of all residues in each model in order to find proper atoms
for ref_res in ref_chain:
# Check if residue number ( .get_id() ) is in the list
if ref_res.get_id()[1] in atoms_to_be_aligned:
# Append CA atom to list
ref_atoms.append(ref_res['CA'])
# Instead of selecting the 0'th model, we loop over
# every model in the sample_structure:
# Old code:
# sample_model = sample_structure[0]
# New code:
for sample_model in sample_structure:
sample_atoms = []
# Do the same for the sample structure
for sample_chain in sample_model:
for sample_res in sample_chain:
if sample_res.get_id()[1] in atoms_to_be_aligned:
sample_atoms.append(sample_res['CA'])
# Now we initiate the superimposer:
super_imposer = Bio.PDB.Superimposer()
super_imposer.set_atoms(ref_atoms, sample_atoms)
super_imposer.apply(sample_model.get_atoms())
# Print RMSD:
print super_imposer.rms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment