def writepdb(mol, atids, fname): wf = open(fname, 'w') print >> wf, 'REMARK, BUILD BY MCPB.PY' resids = [] for i in atids: if mol.atoms[i].resid not in resids: resids.append(mol.atoms[i].resid) reslist = get_reslist(mol, resids) terlist = reslist.nterm + reslist.nonstd + reslist.water terlist = list(set(terlist)) for i in resids: if (i in terlist) and (i != min(resids)): print >> wf, 'TER' for j in mol.residues[i].resconter: atm = mol.atoms[j] gtype = atm.gtype atid = atm.atid if len(atm.atname) == 3: atname = atm.atname else: atname = atm.atname.center(4) crd = atm.crd resid = atm.resid resname = mol.residues[resid].resname print >> wf, "%-6s%5d %4s %3s %1s%4d %8.3f%8.3f%8.3f%6.2f%6.2f" \ %(gtype, atid, atname, resname, 'A', resid, crd[0], crd[1], crd[2], 1.00, 0.00) print >> wf, 'END' wf.close()
def gene_resp_input_file(lgpdbf, ionids, stfpf, ffchoice, mol2fs, chgmod, fixchg_resids, lgchg): libdict, chargedict = get_lib_dict(ffchoice) for mol2f in mol2fs: libdict1, chargedict1 = get_lib_dict(mol2f) libdict.update(libdict1) chargedict.update(chargedict1) mol, atids, resids = get_atominfo_fpdb(lgpdbf) reslist = get_reslist(mol, resids) blist = get_mc_blist(mol, atids, ionids, stfpf) bnoatids = [] #Binding backbone N and C Atom IDs mcresids = [] #Metal site residues stfpff = open(stfpf, 'r') for line in stfpff: if line[0:4] != 'LINK': line = line.split() line = line[0].split('-') mcresids.append(int(line[0])) else: line = line.strip('\n') line = line.split() if (line[-1][-1] in ['N3', 'N', 'O', 'OXT']): atid, atom = line[-1].split('-') bnoatids.append(int(atid)) stfpff.close() bnoresids = [] #Binding Backbone N and C Residue IDs, which are not fitted #with backbone restriction in the charge fitting for i in bnoatids: resid = mol.atoms[i].resid if resid not in bnoresids: bnoresids.append(resid) angresids = [] #ACE, NME, GLY residues for i in resids: if i not in mcresids: angresids.append(i) #Get the total charge of the system------------------------------------- totchg = 0.0 for i in resids: resname = mol.residues[i].resname if i in reslist.nterm: reschg = chargedict['N' + resname] elif i in reslist.cterm: reschg = chargedict['C' + resname] else: reschg = chargedict[resname] totchg = totchg + reschg totchg = int(round(totchg, 0)) if lgchg == -99: lgchg = totchg #------------------------------------------------------------------------- ##############RESP1.IN file############################################### #------------------------------------------------------------------------- print "***Generating the 1st stage resp charge fitting input file..." #print the 1st part, the title fresp1 = open('resp1.in', 'w') print >> fresp1, "Resp charges for organic molecule" print >> fresp1, " " print >> fresp1, " &cntrl" print >> fresp1, " " print >> fresp1, " nmol = 1," print >> fresp1, " ihfree = 1," print >> fresp1, " ioutopt = 1," print >> fresp1, " " print >> fresp1, " &end" print >> fresp1, " 1.0" print >> fresp1, "Resp charges for organic molecule" print >> fresp1, "%5d" %lgchg, print >> fresp1, "%4d" %len(atids) #2. print the 2nd part, the free and fozen atoms--------------------------- natids = [i for i in range(1, len(atids)+1)] #new atids iddict = {} #First number in iddict is new atom id, second is atomic number for i in range(0, len(atids)): iddict[atids[i]] = natids[i] for i in atids: element = mol.atoms[i].element elenum = Atnum[element] iddict[i] = (iddict[i], elenum) for i in resids: get_equal_atoms(mol, i, blist, iddict) #other atoms (except the CH2 and CH3 groups) are frozen for i in atids: if (len(iddict[i]) == 2): iddict[i] = (iddict[i][0], iddict[i][1], -99) for i in atids: #if iddict[i][2] == -99: print >> fresp1, "%5d" %iddict[i][1], print >> fresp1, "%4s" %'0' #else: # print >> fresp1, "%5d" %iddict[i][1], # print >> fresp1, "%4s" %iddict[i][2] fresp1.close() add_restriction('resp1.in', libdict, mol, resids, reslist, mcresids, bnoresids, angresids, iddict, chgmod, fixchg_resids) #------------------------------------------------------------------------- ####################RESP2.IN file######################################### #------------------------------------------------------------------------- print "***Generating the 2nd stage resp charge fitting input file..." #1. print the 1st part, the title------------------------------------------ fresp2 = open('resp2.in', 'w') print >> fresp2, "Resp charges for organic molecule" print >> fresp2, " " print >> fresp2, " &cntrl" print >> fresp2, " " print >> fresp2, " nmol = 1," print >> fresp2, " ihfree = 1," print >> fresp2, " ioutopt = 1," print >> fresp2, " iqopt = 2," print >> fresp2, " qwt = 0.001," print >> fresp2, " " print >> fresp2, " &end" print >> fresp2, " 1.0" print >> fresp2, "Resp charges for organic molecule" print >> fresp2, "%5d" %lgchg, print >> fresp2, "%4d" %len(atids) #2. print the 2nd part, the free or frozen information--------------------- for i in atids: print >> fresp2, "%5d" %iddict[i][1], print >> fresp2, "%4s" %iddict[i][2] fresp2.close() add_restriction('resp2.in', libdict, mol, resids, reslist, mcresids, bnoresids, angresids, iddict, chgmod, fixchg_resids)
def rename_res(mol, atids): #Residue IDs resids = [] for i in atids: if mol.atoms[i].resid not in resids: resids.append(mol.atoms[i].resid) #Correct the names of the HIS, ASP, GLU, LYS, CYS for i in resids: #HIS if mol.residues[i].resname == 'HIS': hasatoms = [] for j in mol.residues[i].resconter: atname = mol.atoms[j].atname hasatoms.append(atname) if ('HD1' in hasatoms) and ('HE2' in hasatoms): mol.residues[i].resname = 'HIP' elif ('HD1' in hasatoms): mol.residues[i].resname = 'HID' elif ('HE2' in hasatoms): mol.residues[i].resname = 'HIE' #ASP elif mol.residues[i].resname == 'ASP': hasatoms = [] for j in mol.residues[i].resconter: atname = mol.atoms[j].atname hasatoms.append(atname) if ('HD1' in hasatoms) or ('HD2' in hasatoms): mol.residues[i].resname = 'ASH' #GLU elif mol.residues[i].resname == 'GLU': hasatoms = [] for j in mol.residues[i].resconter: atname = mol.atoms[j].atname hasatoms.append(atname) if ('HE1' in hasatoms) or ('HE2' in hasatoms): mol.residues[i].resname = 'GLH' #LYS elif mol.residues[i].resname == 'LYS': hasatoms = [] for j in mol.residues[i].resconter: atname = mol.atoms[j].atname hasatoms.append(atname) if ('HZ1' not in hasatoms): mol.residues[i].resname = 'LYN' #CYS elif mol.residues[i].resname == 'CYS': hasatoms = [] for j in mol.residues[i].resconter: atname = mol.atoms[j].atname hasatoms.append(atname) if ('HG' not in hasatoms): ##There are two different situations #for atom in CYS residue for j in mol.residues[i].resconter: if mol.atoms[j].atname == 'SG': sgcrd = mol.atoms[j].crd #for every atom for k in atids: if mol.atoms[k].atname == 'SG' and k != j: atkcrd = mol.atoms[k].crd dis = calc_bond(sgcrd, atkcrd) if dis <= 2.50: mol.residues[i].resname = 'CYX' else: mol.residues[i].resname = 'CYM' reslist = get_reslist(mol, resids) #rename the HN atom to H atom in amino acid residues for i in resids: if i in reslist.std: for j in mol.residues[i].resconter: if mol.atoms[j].atname == 'HN': mol.atoms[j].atname = 'H' return mol
def gene_leaprc(gname, orpdbf, fipdbf, stpdbf, stfpf, ionids,\ ionmol2fs, ioninf, mcresname, naamol2fs, ff_choice, frcmodfs, finfcdf, ileapf, model, watermodel='tip3p', paraset='cm'): print "******************************************************************" print "* *" print "*=================Generating input file for leap=================*" print "* *" print "******************************************************************" #---------------------Generate the new pdb file-------------------------- #mol0 is the old mol while mol is new mol file with new names mol0, atids0, resids0 = get_atominfo_fpdb(orpdbf) reslist0 = get_reslist(mol0, resids0) mol, atids, resids = get_atominfo_fpdb(orpdbf) #rename the residue names into AMBER style, e.g. HIS --> HID, HIP, HIE mol = rename_res(mol, atids) #get the disulfur bond information disul = get_diS_bond(mol, atids) #resname the old residue names to new ones if it is not metal ion if model in [1, 2]: metcenres1 = [] #Old residue ids fp = open(stfpf, 'r') for line in fp: if line[0:4] != "LINK": line = line.split('-') if int(line[0]) not in metcenres1: metcenres1.append(int(line[0])) fp.close() metcenres2 = mcresname #New residue names resndict = {} resns = [] for i in range(0, len(metcenres1)): resndict[metcenres1[i]] = metcenres2[i] resns.append(metcenres2[i]) for i in resndict.keys(): mol.residues[i].resname = resndict[i] writepdb(mol, atids, fipdbf) #----------------------------get the atom names which changed atom type if model in [1, 2]: atomdefs = {} fp0 = open(stfpf, 'r') for line in fp0: if line[0:4] != "LINK": line = line.strip('\n') line = line.split() if line[2] != line[4]: atnewtype = line[4] element = mol.atoms[int(line[1])].element if atnewtype not in atomdefs.keys(): atomdefs[atnewtype] = element else: if element != atomdefs[atnewtype]: raise pymsmtError('There are atoms in fingerprint file ' 'of standard model with same atom type ' 'but different element.') fp0.close() #---------------------Get the bond information, mol2 is the standard model if model == 1: mol2, atids2, resids2 = get_atominfo_fpdb(stpdbf) blist = get_mc_blist(mol2, atids2, ionids, stfpf) blist1 = [(i[0], i[1]) for i in blist] #----------------------Generate the lib file and get the frcmod file name if model == 2: frcmodfs = [] for ionmol2f in ionmol2fs: ionmol, ionatids, ionresids = get_atominfo(ionmol2f) for i in ionatids: element = ionmol.atoms[i].element chg = int(ionmol.atoms[i].charge) frcmodf = get_frcmod_fname(element, chg, watermodel, paraset) if frcmodf not in frcmodfs: frcmodfs.append(frcmodf) elif model == 3 and ioninf != []: #get the metal information metresns = ioninf[0::4] metatns = ioninf[1::4] metelmts = ioninf[2::4] metelmts = [i[0] + i[1:].lower() for i in metelmts] metchgs = ioninf[3::4] metchgs = [int(i) for i in metchgs] #check the charge of the metal ions for metchg in metchgs: if metchg < -1 or metchg > 4: raise pymsmtError('Could not deal with atomic ion which has charge ' 'less than -1 or bigger than +4.') frcmodfs = [] for i in range(0, len(metresns)): if metchgs[i] > 1: #if it is -1 or +1 ions, no need to create the lib file gene_ion_libfile(metresns[i], metatns[i], metelmts[i], metchgs[i]) frcmodf = get_frcmod_fname(metelmts[i], metchgs[i], watermodel, paraset) if frcmodf not in frcmodfs: frcmodfs.append(frcmodf) #-----------------------Generate the leap input file----------------------- print 'Generating the leap input file...' ##Generate the tleap.in file lp = open(ileapf, 'w') if ff_choice in ['ff94', 'ff99', 'ff99SB', 'ff03', 'ff10']: print >> lp, 'source oldff/leaprc.%s' %ff_choice elif ff_choice in ['ff03.r1', 'ff12SB', 'ff14SB']: print >> lp, 'source leaprc.%s' %ff_choice print >> lp, 'source leaprc.gaff' #Add atom types, for bonded model if model in [1, 2]: if atomdefs.keys() != []: print >> lp, 'addAtomTypes {' for i in atomdefs.keys(): print >> lp, ' { "%s" "%s" "sp3" }' %(i, atomdefs[i]) print >> lp, '}' #load lib and frcmod files for monovalent ions (for salt) if ff_choice in ['ff94', 'ff99', 'ff99SB', 'ff03', 'ff03.r1']: print >> lp, 'loadoff atomic_ions.lib' print >> lp, 'loadamberparams frcmod.ions1lsm_hfe_%s' %watermodel #Load mol2 file for the refitting charge residues if model in [1, 2]: for i in resns: print >> lp, '%s = loadmol2 %s.mol2' %(i, i) elif model == 3: for i in naamol2fs: print >> lp, '%s = loadmol2 %s.mol2' %(i, i) #Load frcmod files for non-standard residues and metal site for i in frcmodfs: print >> lp, 'loadamberparams %s' %i if model == 1: print >> lp, 'loadamberparams %s' %finfcdf elif model == 2: for frcmodf in frcmodfs: print >> lp, 'loadamberparams %s' %frcmodf elif model == 3: for metresn in metresns: print >> lp, 'loadoff %s.lib' %metresn for frcmodf in frcmodfs: print >> lp, 'loadamberparams %s' %frcmodf #load pdb file print >> lp, 'mol = loadpdb %s' %fipdbf ##The Disulfur Bond information if disul != []: for i in disul: at1 = i[0] at2 = i[1] resid1 = mol.atoms[at1].resid resid2 = mol.atoms[at2].resid atname1 = mol.atoms[at1].atname atname2 = mol.atoms[at2].atname print >> lp, 'bond', 'mol.' + str(resid1) + '.' + atname1, 'mol.' + \ str(resid2) + '.' + atname2 ##Bond including the metal ions if model == 1: for bond in blist1: if list(set(bond) & set(ionids)) != []: at1 = bond[0] at2 = bond[1] resid1 = mol.atoms[at1].resid resid2 = mol.atoms[at2].resid atname1 = mol.atoms[at1].atname atname2 = mol.atoms[at2].atname print >> lp, 'bond', 'mol.' + str(resid1) + '.' + atname1, 'mol.' + \ str(resid2) + '.' + atname2 ##Nonstandard residues with nearby residues if model in [1, 2]: bondcmds = [] for i in metcenres1: resname = mol0.residues[i].resname print 'Renamed residues includes: ' + str(i) + '-' + resname if i in reslist0.nterm: cmdi = 'bond mol.' + str(i) + '.C' + ' mol.' + str(i+i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) elif i in reslist0.cterm: cmdi = 'bond mol.' + str(i-1) + '.C' + ' mol.' + str(i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) elif i in reslist0.std: cmdi = 'bond mol.' + str(i-1) + '.C' + ' mol.' + str(i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) cmdi = 'bond mol.' + str(i) + '.C' + ' mol.' + str(i+1) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) for j in bondcmds: print >> lp, j #Save dry structure print >> lp, 'savepdb mol %s_dry.pdb' %gname print >> lp, 'saveamberparm mol %s_dry.prmtop %s_dry.inpcrd' \ %(gname, gname) #Solvatebox if watermodel == 'tip3p': print >> lp, 'solvatebox mol TIP3PBOX 10.0' elif watermodel == 'spce': print >> lp, 'solvatebox mol SPCBOX 10.0' print >> lp, 'loadamberparams frcmod.spce' elif watermodel == 'tip4pew': print >> lp, 'solvatebox mol TIP4PEWBOX 10.0' print >> lp, 'loadamberparams frcmod.tip4pew' #Add counter ions print >> lp, 'addions mol Na+ 0' print >> lp, 'addions mol Cl- 0' #Save solvated structure print >> lp, 'savepdb mol %s_solv.pdb' %gname print >> lp, 'saveamberparm mol %s_solv.prmtop %s_solv.inpcrd' \ %(gname, gname) print >> lp, 'quit' print >> lp, ' ' lp.close() print 'Finish generating the leap input file.'
def gene_leaprc(gname, orpdbf, fipdbf, stpdbf, stfpf, ionids,\ ionmol2fs, ioninf, mcresname, naamol2fs, ff_choice, frcmodfs, finfcdf, ileapf, model, watermodel='tip3p', paraset='cm'): print "******************************************************************" print "* *" print "*=================Generating input file for leap=================*" print "* *" print "******************************************************************" #---------------------Generate the new pdb file-------------------------- #mol0 is the old mol while mol is new mol file with new names mol0, atids0, resids0 = get_atominfo_fpdb(orpdbf) reslist0 = get_reslist(mol0, resids0) mol, atids, resids = get_atominfo_fpdb(orpdbf) #rename the residue names into AMBER style, e.g. HIS --> HID, HIP, HIE mol = rename_res(mol, atids) #get the disulfur bond information disul = get_diS_bond(mol, atids) #resname the old residue names to new ones if it is not metal ion if model in [1, 2]: metcenres1 = [] #Old residue ids fp = open(stfpf, 'r') for line in fp: if line[0:4] != "LINK": line = line.split('-') if int(line[0]) not in metcenres1: metcenres1.append(int(line[0])) fp.close() metcenres2 = mcresname #New residue names resndict = {} resns = [] for i in range(0, len(metcenres1)): resndict[metcenres1[i]] = metcenres2[i] resns.append(metcenres2[i]) for i in resndict.keys(): mol.residues[i].resname = resndict[i] writepdb(mol, atids, fipdbf) #----------------------------get the atom names which changed atom type if model in [1, 2]: atomdefs = {} fp0 = open(stfpf, 'r') for line in fp0: if line[0:4] != "LINK": line = line.strip('\n') line = line.split() if line[2] != line[4]: atnewtype = line[4] element = mol.atoms[int(line[1])].element if atnewtype not in atomdefs.keys(): atomdefs[atnewtype] = element else: if element != atomdefs[atnewtype]: raise pymsmtError( 'There are atoms in fingerprint file ' 'of standard model with same atom type ' 'but different element.') fp0.close() #---------------------Get the bond information, mol2 is the standard model if model == 1: mol2, atids2, resids2 = get_atominfo_fpdb(stpdbf) blist = get_mc_blist(mol2, atids2, ionids, stfpf) blist1 = [(i[0], i[1]) for i in blist] #----------------------Generate the lib file and get the frcmod file name if model == 2: frcmodfs = [] for ionmol2f in ionmol2fs: ionmol, ionatids, ionresids = get_atominfo(ionmol2f) for i in ionatids: element = ionmol.atoms[i].element chg = int(ionmol.atoms[i].charge) frcmodf = get_frcmod_fname(element, chg, watermodel, paraset) if frcmodf not in frcmodfs: frcmodfs.append(frcmodf) elif model == 3 and ioninf != []: #get the metal information metresns = ioninf[0::4] metatns = ioninf[1::4] metelmts = ioninf[2::4] metelmts = [i[0] + i[1:].lower() for i in metelmts] metchgs = ioninf[3::4] metchgs = [int(i) for i in metchgs] #check the charge of the metal ions for metchg in metchgs: if metchg < -1 or metchg > 4: raise pymsmtError( 'Could not deal with atomic ion which has charge ' 'less than -1 or bigger than +4.') frcmodfs = [] for i in range(0, len(metresns)): if metchgs[ i] > 1: #if it is -1 or +1 ions, no need to create the lib file gene_ion_libfile(metresns[i], metatns[i], metelmts[i], metchgs[i]) frcmodf = get_frcmod_fname(metelmts[i], metchgs[i], watermodel, paraset) if frcmodf not in frcmodfs: frcmodfs.append(frcmodf) #-----------------------Generate the leap input file----------------------- print 'Generating the leap input file...' ##Generate the tleap.in file lp = open(ileapf, 'w') if ff_choice in ['ff94', 'ff99', 'ff99SB', 'ff03', 'ff10']: print >> lp, 'source oldff/leaprc.%s' % ff_choice elif ff_choice in ['ff03.r1', 'ff12SB', 'ff14SB']: print >> lp, 'source leaprc.%s' % ff_choice print >> lp, 'source leaprc.gaff' #Add atom types, for bonded model if model in [1, 2]: if atomdefs.keys() != []: print >> lp, 'addAtomTypes {' for i in atomdefs.keys(): print >> lp, ' { "%s" "%s" "sp3" }' % (i, atomdefs[i]) print >> lp, '}' #load lib and frcmod files for monovalent ions (for salt) if ff_choice in ['ff94', 'ff99', 'ff99SB', 'ff03', 'ff03.r1']: print >> lp, 'loadoff atomic_ions.lib' print >> lp, 'loadamberparams frcmod.ions1lsm_hfe_%s' % watermodel #Load mol2 file for the refitting charge residues if model in [1, 2]: for i in resns: print >> lp, '%s = loadmol2 %s.mol2' % (i, i) elif model == 3: for i in naamol2fs: print >> lp, '%s = loadmol2 %s.mol2' % (i, i) #Load frcmod files for non-standard residues and metal site for i in frcmodfs: print >> lp, 'loadamberparams %s' % i if model == 1: print >> lp, 'loadamberparams %s' % finfcdf elif model == 2: for frcmodf in frcmodfs: print >> lp, 'loadamberparams %s' % frcmodf elif model == 3: for metresn in metresns: print >> lp, 'loadoff %s.lib' % metresn for frcmodf in frcmodfs: print >> lp, 'loadamberparams %s' % frcmodf #load pdb file print >> lp, 'mol = loadpdb %s' % fipdbf ##The Disulfur Bond information if disul != []: for i in disul: at1 = i[0] at2 = i[1] resid1 = mol.atoms[at1].resid resid2 = mol.atoms[at2].resid atname1 = mol.atoms[at1].atname atname2 = mol.atoms[at2].atname print >> lp, 'bond', 'mol.' + str(resid1) + '.' + atname1, 'mol.' + \ str(resid2) + '.' + atname2 ##Bond including the metal ions if model == 1: for bond in blist1: if list(set(bond) & set(ionids)) != []: at1 = bond[0] at2 = bond[1] resid1 = mol.atoms[at1].resid resid2 = mol.atoms[at2].resid atname1 = mol.atoms[at1].atname atname2 = mol.atoms[at2].atname print >> lp, 'bond', 'mol.' + str(resid1) + '.' + atname1, 'mol.' + \ str(resid2) + '.' + atname2 ##Nonstandard residues with nearby residues if model in [1, 2]: bondcmds = [] for i in metcenres1: resname = mol0.residues[i].resname print 'Renamed residues includes: ' + str(i) + '-' + resname if i in reslist0.nterm: cmdi = 'bond mol.' + str(i) + '.C' + ' mol.' + str(i + i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) elif i in reslist0.cterm: cmdi = 'bond mol.' + str(i - 1) + '.C' + ' mol.' + str(i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) elif i in reslist0.std: cmdi = 'bond mol.' + str(i - 1) + '.C' + ' mol.' + str(i) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) cmdi = 'bond mol.' + str(i) + '.C' + ' mol.' + str(i + 1) + '.N' if cmdi not in bondcmds: bondcmds.append(cmdi) for j in bondcmds: print >> lp, j #Save dry structure print >> lp, 'savepdb mol %s_dry.pdb' % gname print >> lp, 'saveamberparm mol %s_dry.prmtop %s_dry.inpcrd' \ %(gname, gname) #Solvatebox if watermodel == 'tip3p': print >> lp, 'solvatebox mol TIP3PBOX 10.0' elif watermodel == 'spce': print >> lp, 'solvatebox mol SPCBOX 10.0' print >> lp, 'loadamberparams frcmod.spce' elif watermodel == 'tip4pew': print >> lp, 'solvatebox mol TIP4PEWBOX 10.0' print >> lp, 'loadamberparams frcmod.tip4pew' #Add counter ions print >> lp, 'addions mol Na+ 0' print >> lp, 'addions mol Cl- 0' #Save solvated structure print >> lp, 'savepdb mol %s_solv.pdb' % gname print >> lp, 'saveamberparm mol %s_solv.prmtop %s_solv.inpcrd' \ %(gname, gname) print >> lp, 'quit' print >> lp, ' ' lp.close() print 'Finish generating the leap input file.'