Skip to content

Instantly share code, notes, and snippets.

@chrishiestand
Created October 1, 2013 09:40
Show Gist options
  • Save chrishiestand/6776077 to your computer and use it in GitHub Desktop.
Save chrishiestand/6776077 to your computer and use it in GitHub Desktop.
a quick and dirty python script to modify parts of an ATOM PDB file in a very domain-specific way
#!/usr/bin/env python3
import re, argparse
#NE2 HIS -> HSE
#standard fields = [ 'record', 'serial', 'name', 'altLoc', 'resName', 'chainID', 'resSeq',
# 'iCode', 'x', 'y', 'z', 'occupancy', 'tempFactor', 'element', 'charge' ]
parser = argparse.ArgumentParser(description='Changes HIS to HSE in PDB file where "NE2 HIS" found, outputs to sdout')
parser.add_argument("file", type=str)
args = parser.parse_args()
file = args.file
with open(file, 'r', newline='') as pdb_file:
contents = pdb_file.read()
matchgroup = re.findall(r'NE2 HIS (\w ?\d+)', contents)
for line in contents.split("\n"):
match = re.search(r' HIS (\w ?\d+)', line)
if match:
new_line = re.sub('HIS ' + match.group(1), 'HSE ' + match.group(1), line)
print(new_line)
else:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment