Skip to content
Permalink
5ab4ed8e4a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
36 lines (30 sloc) 1.46 KB
def swap_and_relabel_chains(input_pdb_path, output_pdb_path):
chain_a_atoms = []
chain_b_atoms = []
other_lines = []
# Read the PDB file and segregate lines based on chain A, chain B, and others
with open(input_pdb_path, 'r') as pdb_file:
for line in pdb_file:
if line.startswith('ATOM'):
chain_id = line[21] # Chain identifier is at column 22 (0-based indexing)
if chain_id == 'A':
chain_a_atoms.append(line)
elif chain_id == 'B':
chain_b_atoms.append(line)
else:
other_lines.append(line)
else:
other_lines.append(line)
# Swap chain identifiers: A -> B and B -> A
swapped_chain_a_atoms = [atom[:21] + 'B' + atom[22:] for atom in chain_a_atoms]
swapped_chain_b_atoms = [atom[:21] + 'A' + atom[22:] for atom in chain_b_atoms]
# Combine swapped chains and other lines, with chain B atoms first
modified_lines = swapped_chain_b_atoms + swapped_chain_a_atoms + other_lines
# Write the modified content to a new PDB file
with open(output_pdb_path, 'w') as output_pdb:
for line in modified_lines:
output_pdb.write(line)
# Example usage
input_pdb_path = './experimental_structures/exp_6bgg.pdb' # Replace with your input PDB file path
output_pdb_path = './6bgg.pdb' # Desired output file path
swap_and_relabel_chains(input_pdb_path, output_pdb_path)