def writeRTF(mol, parameters, netcharge, filename): import periodictable from htmd.version import version as htmdversion f = open(filename, "w") print("* Charmm RTF built by HTMD parameterize version {}".format(htmdversion()), file=f) print("* ", file=f) print(" 22 0", file=f) types = getSortedAndUniqueTypes(mol.atomtype, 'atom_types') for type in types: val = parameters.atom_types[type] print("MASS %5d %s %8.5f %s" % (val.number, type, val.mass, periodictable.elements[val.atomic_number]), file=f) print("\nAUTO ANGLES DIHE\n", file=f) assert np.unique(mol.resname).size == 1 # Check if all atoms have the same residue name print("RESI %s %8.5f" % (mol.resname[0], netcharge), file=f) print("GROUP", file=f) maxnamelen = max(4, np.max([len(na) for na in mol.name])) # Minimum field size of 4 maxatomtypelen = max(6, np.max([len(at) for at in mol.atomtype])) # Minimum field size of 6 for n, a, c in zip(mol.name, mol.atomtype, mol.charge): print("ATOM {0:>{1}s} {2:>{3}s} {4:8.6f}".format(n, maxnamelen, a, maxatomtypelen, c), file=f) for a in mol.bonds: print("BOND {:>4s} {:>4s}".format(*sorted([mol.name[a[0]], mol.name[a[1]]])), file=f) for a in mol.impropers: print("IMPR %4s %4s %4s %4s" % (mol.name[a[0]], mol.name[a[1]], mol.name[a[2]], mol.name[a[3]]), file=f) print("PATCH FIRST NONE LAST NONE", file=f) print("\nEND", file=f) f.close()
def writeRTF(mol, parameters, netcharge, filename): import periodictable from htmd.version import version as htmdversion f = open(filename, "w") print("* Charmm RTF built by HTMD parameterize version {}".format( htmdversion()), file=f) print("* ", file=f) print(" 22 0", file=f) types = getSortedAndUniqueTypes(mol.atomtype, 'atom_types') for type in types: val = parameters.atom_types[type] print("MASS %5d %s %8.5f %s" % (val.number, type, val.mass, periodictable.elements[val.atomic_number]), file=f) print("\nAUTO ANGLES DIHE\n", file=f) print("RESI MOL %8.5f" % netcharge, file=f) print("GROUP", file=f) for n, a, c in zip(mol.name, mol.atomtype, mol.charge): print("ATOM %4s %6s %8.6f" % (n, a, c), file=f) for a in mol.bonds: print("BOND {:>4s} {:>4s}".format( *sorted([mol.name[a[0]], mol.name[a[1]]])), file=f) for a in mol.impropers: print("IMPR %4s %4s %4s %4s" % (mol.name[a[0]], mol.name[a[1]], mol.name[a[2]], mol.name[a[3]]), file=f) print("PATCH FIRST NONE LAST NONE", file=f) print("\nEND", file=f) f.close()
def write(self, filename): f = open(filename, "w") print("* Charmm RTF built by HTMD parameterize version {}".format( htmdversion()), file=f) print("* ", file=f) print(" 22 0", file=f) for a in self.types: print("MASS %5d %s %8.5f %s" % (self.typeindex_by_type[a], a, self.mass_by_type[a], self.element_by_type[a]), file=f) print("\nAUTO ANGLES DIHE\n", file=f) print("RESI MOL %8.5f" % self.netcharge, file=f) print("GROUP", file=f) for a in self.names: print("ATOM %4s %6s %8.6f" % (a, self.type_by_name[a], self.charge_by_name[a]), file=f) for a in self.bonds: print("BOND %4s %4s" % (self.names[a[0]], self.names[a[1]]), file=f) for a in self.impropers: print("IMPR %4s %4s %4s %4s" % (self.names[a[0]], self.names[a[1]], self.names[a[2]], self.names[a[3]]), file=f) print("PATCH FIRST NONE LAST NONE", file=f) print("\nEND", file=f) f.close()
def writeRTF(mol, parameters, netcharge, filename): import periodictable from htmd.version import version as htmdversion f = open(filename, "w") print("* Charmm RTF built by HTMD parameterize version {}".format(htmdversion()), file=f) print("* ", file=f) print(" 22 0", file=f) types = getSortedAndUniqueTypes(mol.atomtype, 'atom_types') for type in types: val = parameters.atom_types[type] print("MASS %5d %s %8.5f %s" % (val.number, type, val.mass, periodictable.elements[val.atomic_number]), file=f) print("\nAUTO ANGLES DIHE\n", file=f) print("RESI MOL %8.5f" % netcharge, file=f) print("GROUP", file=f) maxnamelen = max(4, np.max([len(na) for na in mol.name])) # Minimum field size of 4 maxatomtypelen = max(6, np.max([len(at) for at in mol.atomtype])) # Minimum field size of 6 for n, a, c in zip(mol.name, mol.atomtype, mol.charge): print("ATOM {0:>{1}s} {2:>{3}s} {4:8.6f}".format(n, maxnamelen, a, maxatomtypelen, c), file=f) for a in mol.bonds: print("BOND {:>4s} {:>4s}".format(*sorted([mol.name[a[0]], mol.name[a[1]]])), file=f) for a in mol.impropers: print("IMPR %4s %4s %4s %4s" % (mol.name[a[0]], mol.name[a[1]], mol.name[a[2]], mol.name[a[3]]), file=f) print("PATCH FIRST NONE LAST NONE", file=f) print("\nEND", file=f) f.close()
def write(self, filename): for i in self.dihedrals: if i.e14 != 1.0: raise ValueError( "Can't express 1-4 electrostatic scaling in Charmm file format" ) f = open(filename, "w") print("* prm file built by HTMD parameterize version {}".format( htmdversion()), file=f) print("*\n", file=f) print("BONDS", file=f) for a in self.bonds: print("%-6s %-6s %8.2f %8.4f" % (a.types[0], a.types[1], a.k0, a.r0), file=f) print("\nANGLES", file=f) for a in self.angles: if a.kUB > 0.: print("%-6s %-6s %-6s %8.2f %8.2f %8.2f %8.2f" % (a.types[0], a.types[1], a.types[2], a.k0, a.theta0, a.kUB, a.rUB), file=f) else: print("%-6s %-6s %-6s %8.2f %8.2f" % (a.types[0], a.types[1], a.types[2], a.k0, a.theta0), file=f) print("\nDIHEDRALS", file=f) for a in self.dihedrals: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (a.types[0], a.types[1], a.types[2], a.types[3], a.k0, a.n, a.phi0), file=f) print("\nIMPROPER", file=f) for a in self.impropers: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (a.types[0], a.types[1], a.types[2], a.types[3], a.k0, a.n, a.phi0), file=f) print( "\nNONBONDED nbxmod 5 atom cdiel shift vatom vdistance vswitch -", file=f) print("cutnb 14.0 ctofnb 12.0 ctonnb 10.0 eps 1.0 e14fac 1.0 wmin 1.5", file=f) for a in self.nonbonded: if a.emin_14 is not None: # Charmm prm stores rmin/2 print("%-6s 0.0000 %8.4f %8.4f 0.0000 %8.4f %8.4f" % (a.types[0], a.emin, a.rmin / 2., a.emin_14, a.rmin_14 / 2.), file=f) else: print("%-6s 0.0000 %8.4f %8.4f" % (a.types[0], a.emin, a.rmin / 2.), file=f) f.close()
def writePRM(mol, parameters, filename): from htmd.version import version as htmdversion from htmd.util import ensurelist # for type, val in parameters.atom_types.items(): # if val.epsilon_14 != 1.0: # raise ValueError("Can't express 1-4 electrostatic scaling in Charmm file format") f = open(filename, "w") print("* prm file built by HTMD parameterize version {}".format(htmdversion()), file=f) print("*\n", file=f) print("BONDS", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.bonds], 'bond_types') for type in types: val = getParameter(type, parameters.bond_types) print("%-6s %-6s %8.2f %8.4f" % (type[0], type[1], val.k, val.req), file=f) print("\nANGLES", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.angles], 'angle_types') for type in types: val = getParameter(type, parameters.angle_types) print("%-6s %-6s %-6s %8.2f %8.2f" % (type[0], type[1], type[2], val.k, val.theteq), file=f) print("\nDIHEDRALS", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.dihedrals], 'dihedral_types') for type in types: val = getParameter(type, parameters.dihedral_types) for term in val: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], term.phi_k, term.per, term.phase), file=f) print("\nIMPROPER", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.impropers], 'improper_types') for type in types: type, field = findImproperType(type, parameters) val = parameters.__dict__[field][type] if field == 'improper_periodic_types': for term in ensurelist(val): print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], term.phi_k, term.per, term.phase), file=f) elif field == 'improper_types': print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], val.psi_k, 0, val.psi_eq), file=f) print("\nNONBONDED nbxmod 5 atom cdiel shift vatom vdistance vswitch -", file=f) print("cutnb 14.0 ctofnb 12.0 ctonnb 10.0 eps 1.0 e14fac 1.0 wmin 1.5", file=f) types = getSortedAndUniqueTypes(mol.atomtype, 'atom_types') for type in types: val = parameters.atom_types[type] if val.epsilon_14 != val.epsilon: print("%-6s 0.0000 %8.4f %8.4f 0.0000 %8.4f %8.4f" % (type, val.epsilon, val.rmin, val.epsilon_14, val.rmin_14), file=f) else: print("%-6s 0.0000 %8.4f %8.4f" % (type, val.epsilon, val.rmin), file=f) f.close()
def writePRM(mol, parameters, filename): from htmd.version import version as htmdversion from htmd.util import ensurelist # for type, val in parameters.atom_types.items(): # if val.epsilon_14 != 1.0: # raise ValueError("Can't express 1-4 electrostatic scaling in Charmm file format") f = open(filename, "w") print("* prm file built by HTMD parameterize version {}".format(htmdversion()), file=f) print("*\n", file=f) print("BONDS", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.bonds], 'bond_types') for type in types: val = parameters.bond_types[type] print("%-6s %-6s %8.2f %8.4f" % (type[0], type[1], val.k, val.req), file=f) print("\nANGLES", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.angles], 'angle_types') for type in types: val = parameters.angle_types[type] print("%-6s %-6s %-6s %8.2f %8.2f" % (type[0], type[1], type[2], val.k, val.theteq), file=f) print("\nDIHEDRALS", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.dihedrals], 'dihedral_types') for type in types: val = parameters.dihedral_types[type] for term in val: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], term.phi_k, term.per, term.phase), file=f) print("\nIMPROPER", file=f) types = getSortedAndUniqueTypes(mol.atomtype[mol.impropers], 'improper_types') for type in types: val, field = getImproperParameter(type, parameters) if field == 'improper_periodic_types': for term in ensurelist(val): print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], term.phi_k, term.per, term.phase), file=f) elif field == 'improper_types': print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % (type[0], type[1], type[2], type[3], val.psi_k, 0, val.psi_eq), file=f) print("\nNONBONDED nbxmod 5 atom cdiel shift vatom vdistance vswitch -", file=f) print("cutnb 14.0 ctofnb 12.0 ctonnb 10.0 eps 1.0 e14fac 1.0 wmin 1.5", file=f) types = getSortedAndUniqueTypes(mol.atomtype, 'atom_types') for type in types: val = parameters.atom_types[type] if val.epsilon_14 != val.epsilon: print("%-6s 0.0000 %8.4f %8.4f 0.0000 %8.4f %8.4f" % (type, val.epsilon, val.rmin, val.epsilon_14, val.rmin_14), file=f) else: print("%-6s 0.0000 %8.4f %8.4f" % (type, val.epsilon, val.rmin), file=f) f.close()
def write(self, filename): f = open(filename, "w") print("* Charmm RTF built by HTMD parameterize version {}".format(htmdversion()), file=f) print("* ", file=f) print(" 22 0", file=f) for a in self.types: print( "MASS %5d %s %8.5f %s" % (self.typeindex_by_type[a], a, self.mass_by_type[a], self.element_by_type[a]), file=f) print("\nAUTO ANGLES DIHE\n", file=f) print("RESI MOL %8.5f" % self.netcharge, file=f) print("GROUP", file=f) for a in self.names: print("ATOM %4s %6s %8.6f" % (a, self.type_by_name[a], self.charge_by_name[a]), file=f) for a in self.bonds: print("BOND %4s %4s" % (self.names[a[0]], self.names[a[1]]), file=f) for a in self.impropers: print("IMPR %4s %4s %4s %4s" % (self.names[a[0]], self.names[a[1]], self.names[a[2]], self.names[a[3]]), file=f) print("PATCH FIRST NONE LAST NONE", file=f) print("\nEND", file=f) f.close()
def write(self, filename): for i in self.dihedrals: if i.e14 != 1.0: raise ValueError("Can't express 1-4 electrostatic scaling in Charmm file format") f = open(filename, "w") print("* prm file built by HTMD parameterize version {}".format(htmdversion()), file=f) print("*\n", file=f) print("BONDS", file=f) for a in self.bonds: print("%-6s %-6s %8.2f %8.4f" % (a.types[0], a.types[1], a.k0, a.r0), file=f) print("\nANGLES", file=f) for a in self.angles: if a.kUB > 0.: print("%-6s %-6s %-6s %8.2f %8.2f %8.2f %8.2f" % ( a.types[0], a.types[1], a.types[2], a.k0, a.theta0, a.kUB, a.rUB), file=f) else: print("%-6s %-6s %-6s %8.2f %8.2f" % (a.types[0], a.types[1], a.types[2], a.k0, a.theta0), file=f) print("\nDIHEDRALS", file=f) for a in self.dihedrals: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % ( a.types[0], a.types[1], a.types[2], a.types[3], a.k0, a.n, a.phi0), file=f) print("\nIMPROPER", file=f) for a in self.impropers: print("%-6s %-6s %-6s %-6s %12.8f %d %12.8f" % ( a.types[0], a.types[1], a.types[2], a.types[3], a.k0, a.n, a.phi0), file=f) print("\nNONBONDED nbxmod 5 atom cdiel shift vatom vdistance vswitch -", file=f) print("cutnb 14.0 ctofnb 12.0 ctonnb 10.0 eps 1.0 e14fac 1.0 wmin 1.5", file=f) for a in self.nonbonded: if a.emin_14 is not None: # Charmm prm stores rmin/2 print("%-6s 0.0000 %8.4f %8.4f 0.0000 %8.4f %8.4f" % ( a.types[0], a.emin, a.rmin / 2., a.emin_14, a.rmin_14 / 2.), file=f) else: print("%-6s 0.0000 %8.4f %8.4f" % (a.types[0], a.emin, a.rmin / 2.), file=f) f.close()
def writeFRCMOD(mol, parameters, filename, typemap=None): from htmd.version import version as htmdversion if typemap is not None: parameters = mapAtomTypesParameterSet(parameters, typemap) atomtypes = np.vectorize(typemap.get)(mol.atomtype) else: atomtypes = mol.atomtype f = open(filename, "w") print("Frcmod generated by HTMD parameterize version {}".format( htmdversion()), file=f) print("MASS", file=f) for at in np.unique(atomtypes): print("%s %f %f" % (at, parameters.atom_types[at].mass, 0.), file=f) print("\nBOND", file=f) types = getSortedAndUniqueTypes(atomtypes[mol.bonds], 'bond_types') for type in types: val = getParameter(type, parameters.bond_types) print("%s %f %f" % ('-'.join(type), val.k, val.req), file=f) print("\nANGL", file=f) types = getSortedAndUniqueTypes(atomtypes[mol.angles], 'angle_types') for type in types: val = getParameter(type, parameters.angle_types) print("%s %f %f" % ('-'.join(type), val.k, val.theteq), file=f) print("\nDIHE", file=f) types = getSortedAndUniqueTypes(atomtypes[mol.dihedrals], 'dihedral_types') for type in types: val = getParameter(type, parameters.dihedral_types) toprint = [] for term in val: if term.phi_k == 0: continue toprint.append(term) # HACK: print at least one dihedral, even if the force constant is 0, otherwise "tleap" is not happy! if len(toprint) == 0: toprint.append(val[0]) for i, term in enumerate(toprint): # All terms of the same dihedral except the last one should be negative. http://ambermd.org/formats.html#frcmod if i == len(toprint) - 1: print("%s 1 %12.6f %12.6f %12.6f %12.6f %12.6f" % ('-'.join(type), term.phi_k, term.phase, term.per, term.scee, term.scnb), file=f) else: print("%s 1 %12.6f %12.6f %12.6f %12.6f %12.6f" % ('-'.join(type), term.phi_k, term.phase, -term.per, term.scee, term.scnb), file=f) print("\nIMPR", file=f) types = getSortedAndUniqueTypes(atomtypes[mol.impropers], 'improper_types') for type in types: val, field = getImproperParameter(type, parameters) if field == 'improper_periodic_types': if val.phi_k == 0: continue print("%s %f %f %f" % ('-'.join(type), val.phi_k, val.phase, val.per), file=f) elif field == 'improper_types': if val.psi_k == 0: continue print("%s %f %f %f" % ('-'.join(type), val.psi_k, val.psi_eq, 1), file=f) print("\nNONB", file=f) # Have to iterate over the types in use, which include cloned types, and map them back # to original type (which has the same vdw params), because a copy of a copy won't be in self.nonbonded. types = getSortedAndUniqueTypes(atomtypes, 'atom_types') for type in types: val = parameters.atom_types[type] print("%s %f %f" % (type, val.rmin, val.epsilon), file=f) print("", file=f) f.close()
def writeFrcmod(self, rtf, filename): t = dict() # Make a type mapping for any name != 2 chars in length # Because Amber file formats are horrid idx = 97 for tt in rtf.types: n = tt if len(tt) != 2: n = "z%c" % idx idx += 1 t[tt] = n # check to see whether the parameters can be expressed in Amber FRCMOD format if len(self.impropers) != 0: raise ValueError("Can't express CHARMM-style impropers in Amber file format") for i in self.angles: if i.rUB != 0. or i.kUB != 0.: raise ValueError("Can't express Urey-Bradley terms in Amber file format") for i in self.nonbonded: if i.rmin_14 is None or i.emin_14 is None: raise ValueError("Can't express unset 1-4 VdW terms in Amber file format") eps = math.fabs(i.emin - i.emin_14 * 2.) if i.rmin != i.rmin_14 or eps > 1.e-6: raise ValueError("Can't express 1-4 VdW terms that aren't 0.5x scaled in Amber file format") f = open(filename, "w") print("Frcmod generated by HTMD parameterize version {}".format(htmdversion()), file=f) print("MASS", file=f) for i in rtf.types: print("%s %f %f" % (t[i], rtf.mass_by_type[i], 0.), file=f) print("\nBOND", file=f) for i in self.bonds: print("%s-%s %f %f" % (t[i.types[0]], t[i.types[1]], i.k0, i.r0), file=f) print("\nANGL", file=f) for i in self.angles: print("%s-%s-%s %f %f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], i.k0, i.theta0), file=f) print("\nDIHE", file=f) output = dict() for i in self.dihedrals: if(i.improper == False): name = "%s-%s-%s-%s" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]]) if not (name in output): output[name] = 1 prm = self.dihedralParam(i.types[0], i.types[1], i.types[2], i.types[3]) for pi in range(len(prm)): p = prm[pi] sign = 1 scee = p.e14 scnb = 2. if pi < (len(prm) - 1): sign = -1 print("%2s-%2s-%2s-%2s 1 %f %f %f %f %f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]], p.k0, p.phi0, sign * p.n, scee, scnb), file=f) print("\nIMPR", file=f) output = dict() for i in self.dihedrals: if(i.improper == True): name = "%s-%s-%s-%s" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]]) if not (name in output): output[name] = 1 prm = self.dihedralParam(i.types[0], i.types[1], i.types[2], i.types[3]) for pi in range(len(prm)): p = prm[pi] sign = 1 if p.k0 != 0.: print("%2s-%2s-%2s-%2s %f %f %f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]], p.k0, p.phi0, sign * p.n ), file=f) print("\nNONB", file=f) # Have to iterate over the types in use # which include cloned types, an map them back # to original type (which has the same vdw params) # because a copy of a copy won't be in self.nonbonded. for i in set(rtf.types): p = None j = re.sub("x[0123456789]$", "", i) for b in self.nonbonded: if b.types[0] == j: p = b print("%s %f %f" % (t[i], 0.5 * p.rmin, -p.emin), file=f) print("", file=f) f.close() return t
def writeFrcmod(self, rtf, filename): t = dict() # Make a type mapping for any name != 2 chars in length # Because Amber file formats are horrid idx = 97 for tt in rtf.types: n = tt if len(tt) != 2: n = "z%c" % idx idx += 1 t[tt] = n # check to see whether the parameters can be expressed in Amber FRCMOD format # if len(self.impropers) != 0: # raise ValueError("Can't express CHARMM-style impropers in Amber file format") for i in self.angles: if i.rUB != 0. or i.kUB != 0.: raise ValueError( "Can't express Urey-Bradley terms in Amber file format") for i in self.nonbonded: if i.rmin_14 is None or i.emin_14 is None: raise ValueError( "Can't express unset 1-4 VdW terms in Amber file format") eps = math.fabs(i.emin - i.emin_14 * 2.) if i.rmin != i.rmin_14 or eps > 1.e-6: raise ValueError( "Can't express 1-4 VdW terms that aren't 0.5x scaled in Amber file format" ) f = open(filename, "w") print("Frcmod generated by HTMD parameterize version {}".format( htmdversion()), file=f) print("MASS", file=f) for i in rtf.types: print("%s %f %f" % (t[i], rtf.mass_by_type[i], 0.), file=f) print("\nBOND", file=f) for i in self.bonds: print("%s-%s %f %f" % (t[i.types[0]], t[i.types[1]], i.k0, i.r0), file=f) print("\nANGL", file=f) for i in self.angles: print( "%s-%s-%s %f %f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], i.k0, i.theta0), file=f) print("\nDIHE", file=f) output = dict() for i in self.dihedrals: assert i.improper == False name = "%s-%s-%s-%s" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]]) if not (name in output): output[name] = 1 prmx = self.dihedralParam(i.types[0], i.types[1], i.types[2], i.types[3]) # Prune prms that are zero prm = list() for pi in range(len(prmx)): p = prmx[pi] if p.k0 != 0.: prm.append(p) # HACK: leave at least one dihedral, even if the force constant is 0, # otherwise "tleap" is not happy! if len(prm) == 0: prm.append(prmx[0]) for p in prm: scee = 1. / p.e14 scnb = 2. print( "%2s-%2s-%2s-%2s 1 %12.6f %12.6f %12.6f %12.6f %12.6f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]], p.k0, p.phi0, p.n, scee, scnb), file=f) print("\nIMPR", file=f) output = dict() for i in self.impropers: if (i.improper == True): name = "%s-%s-%s-%s" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]]) if not (name in output): output[name] = 1 prm = self.improperParam(i.types[0], i.types[1], i.types[2], i.types[3]) for pi in range(len(prm)): p = prm[pi] sign = 1 if p.k0 != 0.: print("%2s-%2s-%2s-%2s %f %f %f" % (t[i.types[0]], t[i.types[1]], t[i.types[2]], t[i.types[3]], p.k0, p.phi0, sign * p.n), file=f) print("\nNONB", file=f) # Have to iterate over the types in use # which include cloned types, an map them back # to original type (which has the same vdw params) # because a copy of a copy won't be in self.nonbonded. for i in set(rtf.types): p = None j = re.sub("x[0123456789]$", "", i) for b in self.nonbonded: if b.types[0] == j: p = b print("%s %f %f" % (t[i], 0.5 * p.rmin, -p.emin), file=f) print("", file=f) f.close() return t
def writeFRCMOD(mol, parameters, filename, typemap=None): from htmd.version import version as htmdversion if typemap is not None: parameters = mapAtomTypesParameterSet(parameters, typemap) atomtypes = np.vectorize(typemap.get)(mol.atomtype) else: atomtypes = mol.atomtype f = open(filename, "w") f.write("Frcmod generated by HTMD parameterize version {}\n".format(htmdversion())) f.write("MASS\n") for at in np.unique(atomtypes): f.write('{:<2s} {:>8.3f} {:>10.2f}\n'.format(at, parameters.atom_types[at].mass, 0.)) f.write('\nBOND\n') types = getSortedAndUniqueTypes(atomtypes[mol.bonds], 'bond_types') for type in types: val = getParameter(type, parameters.bond_types) f.write('{:<2s}-{:<2s} {:>10.3f}{:>8.3f}\n'.format(type[0], type[1], val.k, val.req)) f.write('\nANGL\n') types = getSortedAndUniqueTypes(atomtypes[mol.angles], 'angle_types') for type in types: val = getParameter(type, parameters.angle_types) f.write('{:<2s}-{:<2s}-{:<2s} {:>10.3f}{:>9.3f}\n'.format(type[0], type[1], type[2], val.k, val.theteq)) f.write('\nDIHE\n') types = getSortedAndUniqueTypes(atomtypes[mol.dihedrals], 'dihedral_types') for type in types: val = getParameter(type, parameters.dihedral_types) toprint = [] for term in val: if term.phi_k < 1e-6: continue toprint.append(term) # HACK: print at least one dihedral, even if the force constant is 0, otherwise "tleap" is not happy! if len(toprint) == 0: toprint.append(val[0]) for i, term in enumerate(toprint): # All terms of the same dihedral except the last one should be negative. http://ambermd.org/formats.html#frcmod per = term.per if i != len(toprint)-1: per = -per fmt = '{:<2s}-{:<2s}-{:<2s}-{:<2s} {:>4d}{:>15.8f}{:>9.3f}{:>6.1f} SCEE={:1.1f} SCNB={:1.1f}\n' f.write(fmt.format(type[0], type[1], type[2], type[3], 1, term.phi_k, term.phase, per, term.scee, term.scnb)) f.write('\nIMPR\n') types = getSortedAndUniqueTypes(atomtypes[mol.impropers], 'improper_types') for type in types: type, field = findImproperType(type, parameters) val = parameters.__dict__[field][type] fmt = '{:<2s}-{:<2s}-{:<2s}-{:<2s} {:>10.8f}{:>9.3f}{:>6.1f}\n' if field == 'improper_periodic_types': if val.phi_k < 1e-6: continue f.write(fmt.format(type[0], type[1], type[2], type[3], val.phi_k, val.phase, val.per)) elif field == 'improper_types': if val.psi_k < 1e-6: continue f.write(fmt.format(type[0], type[1], type[2], type[3], val.psi_k, val.psi_eq, 1)) f.write('\nNONB\n') # Have to iterate over the types in use, which include cloned types, and map them back # to original type (which has the same vdw params), because a copy of a copy won't be in self.nonbonded. types = getSortedAndUniqueTypes(atomtypes, 'atom_types') for type in types: val = parameters.atom_types[type] f.write('{:<2s} {:>10.8f} {:>10.8f}\n'.format(type, val.rmin, val.epsilon)) f.write('\n') f.close()
def writeFRCMOD(mol, parameters, filename, typemap=None): from htmd.version import version as htmdversion if typemap is not None: parameters = mapAtomTypesParameterSet(parameters, typemap) atomtypes = np.vectorize(typemap.get)(mol.atomtype) else: atomtypes = mol.atomtype f = open(filename, "w") f.write("Frcmod generated by HTMD parameterize version {}\n".format(htmdversion())) f.write("MASS\n") for at in np.unique(atomtypes): f.write('{:<2s} {:>8.3f} {:>10.2f}\n'.format(at, parameters.atom_types[at].mass, 0.)) f.write('\nBOND\n') types = getSortedAndUniqueTypes(atomtypes[mol.bonds], 'bond_types') for type in types: val = getParameter(type, parameters.bond_types) f.write('{:<2s}-{:<2s} {:>10.3f}{:>8.3f}\n'.format(type[0], type[1], val.k, val.req)) f.write('\nANGL\n') types = getSortedAndUniqueTypes(atomtypes[mol.angles], 'angle_types') for type in types: val = getParameter(type, parameters.angle_types) f.write('{:<2s}-{:<2s}-{:<2s} {:>10.3f}{:>9.3f}\n'.format(type[0], type[1], type[2], val.k, val.theteq)) f.write('\nDIHE\n') types = getSortedAndUniqueTypes(atomtypes[mol.dihedrals], 'dihedral_types') for type in types: val = getParameter(type, parameters.dihedral_types) toprint = [] for term in val: if term.phi_k == 0: continue toprint.append(term) # HACK: print at least one dihedral, even if the force constant is 0, otherwise "tleap" is not happy! if len(toprint) == 0: toprint.append(val[0]) for i, term in enumerate(toprint): # All terms of the same dihedral except the last one should be negative. http://ambermd.org/formats.html#frcmod per = term.per if i != len(toprint)-1: per = -per fmt = '{:<2s}-{:<2s}-{:<2s}-{:<2s} {:>4d}{:>15.8f}{:>9.3f}{:>6.1f} SCEE={:1.1f} SCNB={:1.1f}\n' f.write(fmt.format(type[0], type[1], type[2], type[3], 1, term.phi_k, term.phase, per, term.scee, term.scnb)) f.write('\nIMPR\n') types = getSortedAndUniqueTypes(atomtypes[mol.impropers], 'improper_types') for type in types: val, field = getImproperParameter(type, parameters) fmt = '{:<2s}-{:<2s}-{:<2s}-{:<2s} {:>10.8f}{:>9.3f}{:>6.1f}\n' if field == 'improper_periodic_types': if val.phi_k == 0: continue f.write(fmt.format(type[0], type[1], type[2], type[3], val.phi_k, val.phase, val.per)) elif field == 'improper_types': if val.psi_k == 0: continue f.write(fmt.format(type[0], type[1], type[2], type[3], val.psi_k, val.psi_eq, 1)) f.write('\nNONB\n') # Have to iterate over the types in use, which include cloned types, and map them back # to original type (which has the same vdw params), because a copy of a copy won't be in self.nonbonded. types = getSortedAndUniqueTypes(atomtypes, 'atom_types') for type in types: val = parameters.atom_types[type] f.write('{:<2s} {:>10.8f} {:>10.8f}\n'.format(type, val.rmin, val.epsilon)) f.write('\n') f.close()