Skip to content

Instantly share code, notes, and snippets.

@diallobakary4
Last active April 23, 2022 17:26
Show Gist options
  • Save diallobakary4/c648b406c0214ffbe5bca3ea54f15779 to your computer and use it in GitHub Desktop.
Save diallobakary4/c648b406c0214ffbe5bca3ea54f15779 to your computer and use it in GitHub Desktop.
Fix the atom types names in a acpype generated itp file for Gromacs.Input: *_GMX.itp *_NEW.pdb eg: NAD_H_GMX.itp NAD_H_NEW.pdbto run: python itpsort2.py NAD_H_GMX.itp NAD_H_NEW.pdb
import os
import sys
from shutil import copyfile
"""
Fix the atom types names in a acpype generated itp file for Gromacs.
Input: *_GMX.itp *_NEW.pdb eg: NAD_H_GMX.itp NAD_H_NEW.pdb
to run: python itpsort2.py NAD_H_GMX.itp NAD_H_NEW.pdb
"""
file = sys.argv[1] #"NAD_H_GMX.itp"
pdb = sys.argv[2] # "NAD_H_NEW.pdb"
copyfile(file, file + "backup")
lines = open(file , "r").readlines()
pdb = open(pdb, "r").readlines()
#comment everything before atom type
comment = 1
change_atom_type = 0
out_lines = []
for line in lines:
#just comment everything before moleculetype section
if "[ moleculetype ]" in line:
comment = 0 #we don't anymore when we reach molecule section
if comment:
line = ";" + line
#changing atom type
if line[10:11] == "":
change_atom_type = 0
if change_atom_type:
index = int(line.split()[0])
atom_type = pdb[index].split()[-1].strip()
line = line[:8] + " " + atom_type + line[11:]
if "; nr type resi res atom cgnr charge mass ; qtot bond_type" in line:
change_atom_type = 1 #we have reached the atoms section
out_lines.append(line)
with open(file, "w") as out:
out.writelines(out_lines)
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment