Beispiel #1
0
                       "           other option are, g09 (means Gaussian09), \n"
                       "                       and gms (means GAMESS-US)]")
(options, args) = parser.parse_args()

mol, atids, resids = get_atominfo_fpdb(options.inputfile)

if options.nstdpdb is True:
    for i in atids:
        atname = mol.atoms[i].atname
        if atname[1] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
            mol.atoms[i].element = atname[0]
        else:
            mol.atoms[i].element = atname[0:2]

blist = get_blist(mol, atids)
all_list = get_all_list(mol, blist, atids, 8.0)

natids = {}
for i in range(0, len(atids)):
    natids[atids[i]] = i + 1

#crds after optimization
if options.softversion in ['g03', 'g09']:
    crds = get_crds_from_fchk(options.hessfile, len(atids))
elif options.softversion == 'gms':
    crds = get_crds_from_gms(options.hessfile)

#Whole Hessian Matrix
if options.softversion in ['g03', 'g09']:
    fcmatrix = get_matrix_from_fchk(options.hessfile, 3*len(atids))
elif options.softversion == 'gms':
def gene_pre_frcmod_file(ionids, naamol2f, stpdbf, stfpf, smresf, prefcdf,
                         ffchoice, gaff, frcmodfs, watermodel):

    print("******************************************************************")
    print("*                                                                *")
    print("*===================Generate the Initial frcmod file=============*")
    print("*                                                                *")
    print("******************************************************************")

    libdict = {}
    chargedict = {}

    for mol2f in naamol2f:
        libdict1, chargedict1 = get_lib_dict(mol2f)
        libdict.update(libdict1)
        chargedict.update(chargedict1)

    #get the parameter dicts
    Params = get_parm_dict(ffchoice, gaff, frcmodfs)

    massparms = Params.mass
    bondparms = Params.bond
    angparms = Params.ang
    dihparms = Params.dih
    impparms = Params.imp
    nbparms = Params.nb

    #--------------------------------------------------------------------------

    #############Get the metal center information####################

    #--------------------------------------------------------------------------

    mol, atids, resids = get_atominfo_fpdb(stpdbf)

    #get the blist
    blist = get_mc_blist(mol, atids, ionids, stfpf)

    #get_all_the_lists from standard model
    all_list = get_all_list(mol, blist, atids, 10.0)

    #atom type dictionary, key is the atom id, value is atom type
    attypdict = {}

    #atom ids which has been transfered to another atom type
    atidtrans = []

    #get the information for the three from finger print
    fp = open(stfpf, 'r')
    for line in fp:
        if line[0:4] != "LINK":
            atinfo, atid, attyp1st, symbol, attyp2nd = line.split()
            atid = int(atid)
            attyp1st = addspace(attyp1st)
            attyp2nd = addspace(attyp2nd)
            attypdict[atid] = (attyp1st, attyp2nd)
            if attyp1st != attyp2nd:
                atidtrans.append(atid)
    fp.close()

    print("Atoms which has changed the atom types:", atidtrans)

    for atid in atidtrans:
        resid = mol.atoms[atid].resid
        resname = mol.residues[resid].resname
        attyp1 = attypdict[atid][0]
        attyp2 = attypdict[atid][1]
        print(str(resid) + '-' + resname + '@' + str(atid) + '-' + \
              mol.atoms[atid].atname + ' : ' + attyp1, '-->', attyp2)

    #-------------------------------------------------------------------------

    ########Print the pre-generated frcmod files##############

    #-------------------------------------------------------------------------

    fmf = open(prefcdf, 'w')

    #for atoms which changed atom types
    print('REMARK GOES HERE, THIS FILE IS GENERATED BY MCPB.PY', file=fmf)
    print('MASS', file=fmf)

    #for metal ions
    for i in ionids:
        attyp = attypdict[i][1]
        atname = mol.atoms[i].atname
        if len(atname) > 1:
            atname = atname[0] + atname[1:].lower()
        massi = Mass[atname]
        print('YES', attyp + ' '  + str(round(massi, 2))  + \
              '                              ' + atname + ' ion', file=fmf)

    #for ligating atoms
    for atid in atidtrans:
        if atid not in ionids: #not include metal ion
            atyp1 = attypdict[atid][0]
            atyp2 = attypdict[atid][1]
            print('YES', atyp2 + massparms[atyp1], file=fmf)

    #--------------------------------------------------------------------------
    bondparamsdict1 = {} #For metal ions
    bondparamsdict2 = {} #For the others

    print(' ', file=fmf)
    print('BOND', file=fmf)

    #for bond
    for bonds in all_list.bondlist:
        i = bonds[0]
        j = bonds[1]
        if list(set(ionids) & set([i, j])) != []:
            #The bonds which related to the ions
            bondtyp2 = (attypdict[i][1], attypdict[j][1])
            if (bondtyp2 not in list(bondparamsdict1.keys())) and (bondtyp2[::-1] \
                not in list(bondparamsdict1.keys())):
                bondparamsdict1[bondtyp2] = ' '
        elif list(set(atidtrans) & set([i, j])) != []:
            #The bonds related to the atoms which changed their atom types
            bondtyp1 = (attypdict[i][0], attypdict[j][0])
            bondtyp2 = (attypdict[i][1], attypdict[j][1])
            if (bondtyp2 not in list(bondparamsdict2.keys())) and (bondtyp2[::-1] \
                not in list(bondparamsdict2.keys())):
                if bondtyp1 in list(bondparms.keys()):
                    bondparamsdict2[bondtyp2] = bondparms[bondtyp1]
                elif bondtyp1[::-1] in list(bondparms.keys()):
                    bondparamsdict2[bondtyp2] = bondparms[bondtyp1[::-1]]

    for i in sorted(list(bondparamsdict1.keys())):
        print('NON', i[0] + '-' + i[1] + bondparamsdict1[i], file=fmf)

    for i in sorted(list(bondparamsdict2.keys())):
        print('YES', i[0] + '-' + i[1] + bondparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    coparas = []

    r_smresf = open(smresf, 'r')
    for line in r_smresf:
        line = line.strip('\n')
        line = line.split('-')
        resid = int(line[1])
        if ('GLY' in line) or ('KCO' in line) or ('ACE' in line):
            if resid+1 not in resids:
                coparas.append(resid)
    r_smresf.close()

    angparamsdict1 = {} #For metal ions
    angparamsdict2 = {} #For the others
    print(' ', file=fmf)
    print('ANGL', file=fmf)
    #for angle
    for angs in all_list.anglist:
        i = angs[0]
        j = angs[1]
        k = angs[2]
        if list(set(ionids) & set(angs)) != []:
            #The angles which related to the ions
            angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
            if (angtyp2 not in list(angparamsdict1.keys())) and (angtyp2[::-1] \
                not in list(angparamsdict1.keys())):
                angparamsdict1[angtyp2] = ' '
            #print >> fmf, 'NON', attypdict[i][1] + '-' + attypdict[j][1] + \
            #'-' + attypdict[k][1]
        elif list(set(atidtrans) & set(angs)) != []:
            #The angles related to the atoms
            #which changed their atom types
            angtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0])
            angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
            if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
                not in list(angparamsdict2.keys())):
                if angtyp1 in list(angparms.keys()):
                    angparamsdict2[angtyp2] = angparms[angtyp1]
                elif angtyp1[::-1] in list(angparms.keys()):
                    angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    #Add for a specific situation
    for i in coparas:
        for atid in atidtrans:
            if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
                angtyp1 = (attypdict[atid][0], 'C ', 'N ')
                angtyp2 = (attypdict[atid][1], 'C ', 'N ')
                if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
                    not in list(angparamsdict2.keys())):
                    if angtyp1 in list(angparms.keys()):
                        angparamsdict2[angtyp2] = angparms[angtyp1]
                    elif angtyp1[::-1] in list(angparms.keys()):
                        angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    for i in sorted(list(angparamsdict1.keys())):
        print('NON', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict1[i], file=fmf)
    for i in sorted(list(angparamsdict2.keys())):
        print('YES', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    dihparamsdict = {}

    #for dihedral
    for dihs in all_list.dihlist:
        i = dihs[0]
        j = dihs[1]
        k = dihs[2]
        l = dihs[3]
        if list(set(ionids) & set(dihs)) != []:
            #The dihedral related to the metal ions
            dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                       attypdict[l][0])
            dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                        attypdict[l][1])
            if (dihtyp1 not in list(dihparamsdict.keys())) and (dihtyp1[::-1] \
               not in list(dihparamsdict.keys())):
                dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                           '          3.   ', ' ']
        elif list(set(atidtrans) & set(dihs)) != []:
            if list(set(atidtrans) & set(dihs[0::3])) == []:
            #Neither the 1st and 4th atom change atom types
            #There is 2nd or 3rd or both changed atom type(s)
                dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                           attypdict[l][0])
                dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                            attypdict[l][1])
                dihtyp2 = ('X ', attypdict[j][0], attypdict[k][0], 'X ')
                dihtyp2n = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

                if dihtyp1 in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
                elif dihtyp1[::-1] in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
                elif dihtyp2 in list(dihparms.keys()):
                    if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp2n] = dihparms[dihtyp2]
                elif dihtyp2[::-1] in list(dihparms.keys()):
                    if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp2n] = dihparms[dihtyp2[::-1]]
                else:
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                                   '          3.   ', ' ']
            else:
            #There is 1st or 4th atoms or both changed the atom type(s)
                dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                           attypdict[l][0])
                dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                            attypdict[l][1])
                dihtyp2 = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

                if dihtyp1 in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
                elif dihtyp1[::-1] in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
                else:
                    if (dihtyp2 in list(dihparms.keys())) or (dihtyp2[::-1] \
                        in list(dihparms.keys())):
                        continue
                    elif (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                          not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                                   '          3.   ', ' ']

    #Add for a specfic situation
    for i in coparas:
        for atid in atidtrans:
            if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
                for ionid in ionids:
                    if ((atid, ionid, 1) in all_list.bondlist) or \
                       ((ionid, atid, 1) in all_list.bondlist):
                        dihtyp1 = (attypdict[ionid][0], attypdict[atid][0], 'C ', 'N ')
                        dihtyp2 = (attypdict[ionid][1], attypdict[atid][1], 'C ', 'N ')
                        if (dihtyp2 not in dihparamsdict) and (dihtyp2[::-1] not in dihparamsdict):
                            if dihtyp1 in list(dihparms.keys()):
                                dihparamsdict[dihtyp2] = dihparms[dihtyp1]
                            elif dihtyp1[::-1] in list(dihparms.keys()):
                                dihparamsdict[dihtyp2] = dihparms[dihtyp1[::-1]]
                            else:
                                dihparamsdict[dihtyp2] = ['   3    0.00          0.0   ', \
                                                       '          3.   ', ' ']

    print(' ', file=fmf)
    print('DIHE', file=fmf)
    for keyv in sorted(list(dihparamsdict.keys())):
        if 'X ' not in keyv: #For types don't contain X
            valv = dihparamsdict[keyv]
            keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
            terms = len(valv)//3
            for i in range(0, terms):
                temp = i * 3
                print('YES', keyv, valv[temp] + valv[temp+1] + \
                         valv[temp+2], file=fmf)

    for keyv in sorted(list(dihparamsdict.keys())):
        if 'X ' in keyv:     #For types contain X
            valv = dihparamsdict[keyv]
            keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
            terms = len(valv)//3
            for i in range(0, terms):
                temp = i * 3
                print('YES', keyv, valv[temp] + valv[temp+1] + \
                         valv[temp+2], file=fmf)
    #--------------------------------------------------------------------------
    impparamsdict = {}

    #For improper torsion
    for imps in all_list.implist:
        i = imps[0]
        j = imps[1]
        k = imps[2]
        l = imps[3]

        if list(set(ionids) & set(imps)) != []:
            continue
        elif list(set(atidtrans) & set(imps)) != []:
            imptyps = {}

            #1 situation
            imptyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                       attypdict[l][0])
            imptyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                        attypdict[l][1])

            #3 situations
            imptyp2 = ('X ', 'X ', attypdict[k][0], attypdict[i][0])
            imptyp2n = ('X ', 'X ', attypdict[k][1], attypdict[i][1])

            imptyp3 = ('X ', 'X ', attypdict[k][0], attypdict[j][0])
            imptyp3n = ('X ', 'X ', attypdict[k][1], attypdict[j][1])

            imptyp4 = ('X ', 'X ', attypdict[k][0], attypdict[l][0])
            imptyp4n = ('X ', 'X ', attypdict[k][1], attypdict[l][1])

            #6 situations
            imptyp5 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[j][0])
            imptyp5n = ('X ', attypdict[i][1], attypdict[k][1], attypdict[j][1])

            imptyp6 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[i][0])
            imptyp6n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[i][1])

            imptyp7 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[l][0])
            imptyp7n = ('X ', attypdict[i][0], attypdict[k][1], attypdict[l][1])

            imptyp8 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[i][0])
            imptyp8n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[i][1])

            imptyp9 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[l][0])
            imptyp9n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[l][1])

            imptyp10 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[j][0])
            imptyp10n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[j][1])

            imptyps[imptyp1] = imptyp1n
            imptyps[imptyp2] = imptyp2n
            imptyps[imptyp3] = imptyp3n
            imptyps[imptyp4] = imptyp4n
            imptyps[imptyp5] = imptyp5n
            imptyps[imptyp6] = imptyp6n
            imptyps[imptyp7] = imptyp7n
            imptyps[imptyp8] = imptyp8n
            imptyps[imptyp9] = imptyp9n
            imptyps[imptyp10] = imptyp10n

            for imptypkey in list(imptyps.keys()):
                if imptypkey in list(impparms.keys()):
                    impparamsdict[imptyps[imptypkey]] = impparms[imptypkey]

    print(' ', file=fmf)
    print('IMPR', file=fmf)
    #for improper torsion
    for i in sorted(list(impparamsdict.keys())):
        if i not in list(impparms.keys()):
            imptypkey = i[0] + '-' + i[1] + '-' + i[2] + '-' + i[3]
            print('YES', imptypkey, impparamsdict[i], file=fmf)
    #--------------------------------------------------------------------------
    #for nb
    print(' ', file=fmf)
    print('NONB', file=fmf)

    #For metal ions
    IonLJParaDict = get_ionljparadict(watermodel)

    for i in ionids:
        element = mol.atoms[i].element
        chg = int(round(chargedict[mol.atoms[i].resname], 0))
        attyp2 = attypdict[i][1]

        mnum = max([9-chg, chg])
        for j in range(0, mnum):

            fchg1 = chg - j
            fchg2 = chg + j

            if j == 0 and element+str(fchg1) in list(IonLJParaDict.keys()):
                rmin = IonLJParaDict[element + str(fchg1)][0]
                ep = IonLJParaDict[element + str(fchg1)][1]
                annot = IonLJParaDict[element + str(fchg1)][2]
                break
            elif fchg1 > 0 and element+str(fchg1) in list(IonLJParaDict.keys()):
                print("Could not find VDW radius for element %s with charge "
                      "+%d, use the one of charge +%d" %(element, chg, fchg1))
                rmin = IonLJParaDict[element + str(fchg1)][0]
                ep = IonLJParaDict[element + str(fchg1)][1]
                annot = IonLJParaDict[element + str(fchg1)][2]
                break
            elif fchg2 <= 8 and element+str(fchg2) in list(IonLJParaDict.keys()):
                print("Could not find VDW radius for element %s with charge "
                      "+%d, use the one of charge +%d" %(element, chg, fchg2))
                rmin = IonLJParaDict[element + str(fchg2)][0]
                ep = IonLJParaDict[element + str(fchg2)][1]
                annot = IonLJParaDict[element + str(fchg2)][2]
                break

        if rmin is None:
            raise pymsmtError("Could not find VDW parameters/radius for "
                              "element %s with charge +%d " %(element, chg))
        print('YES   %s        %8.4f %13.10f       %-s' %(attyp2, rmin, \
                 ep, annot), file=fmf)

    #For the others
    for atid in atidtrans:
        if atid not in ionids:
            attyp1 = attypdict[atid][0]
            attyp2 = attypdict[atid][1]
            print('YES  ', attyp2 + nbparms[attyp1], file=fmf)

    print(' ', file=fmf)
    print(' ', file=fmf)

    fmf.close()
    #--------------------------------------------------------------------------

    #Move the file, delete the repeat parts
    os.system("mv %s temp" %prefcdf)
    os.system("uniq temp > %s" %prefcdf)
    os.system("rm temp")
Beispiel #3
0
    "           other option are, g09 (means Gaussian09), \n"
    "                       and gms (means GAMESS-US)]")
(options, args) = parser.parse_args()

mol, atids, resids = get_atominfo_fpdb(options.inputfile)

if options.nstdpdb is True:
    for i in atids:
        atname = mol.atoms[i].atname
        if atname[1] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
            mol.atoms[i].element = atname[0]
        else:
            mol.atoms[i].element = atname[0:2]

blist = get_blist(mol, atids)
all_list = get_all_list(mol, blist, atids, 8.0)

natids = {}
for i in range(0, len(atids)):
    natids[atids[i]] = i + 1

#crds after optimization
if options.softversion in ['g03', 'g09']:
    crds = get_crds_from_fchk(options.hessfile, len(atids))
elif options.softversion == 'gms':
    crds = get_crds_from_gms(options.hessfile)

#Whole Hessian Matrix
if options.softversion in ['g03', 'g09']:
    fcmatrix = get_matrix_from_fchk(options.hessfile, 3 * len(atids))
elif options.softversion == 'gms':
Beispiel #4
0
def get_all_fc_with_sem(mol, atids, hessf, prog, scalef, bondavg, avg13,
                        angavg):
    """Generate all the force constants with Z-matrix method"""

    print_method_title('S')

    # Get the new atom ids
    natids = {}
    for i in xrange(0, len(atids)):
        natids[atids[i]] = i + 1

    # Get the connection list
    blist = get_blist(mol, atids)
    all_list = get_all_list(mol, blist, atids, 8.0)

    # Crds after optimization
    if prog in ['g03', 'g09']:
        crds = get_crds_from_fchk(hessf, len(atids))
    elif prog == 'gms':
        crds = get_crds_from_gms(hessf)

    # Whole Hessian Matrix
    if prog in ['g03', 'g09']:
        fcmatrix = get_matrix_from_fchk(hessf, 3 * len(atids))
    elif prog == 'gms':
        fcmatrix = get_matrix_from_gms(hessf, 3 * len(atids))

    # Print the bond part
    if bondavg is True:
        print_bond_title_wsd()
    else:
        print_bond_title()

    for i in all_list.bondlist:
        at1 = i[0]
        at2 = i[1]
        nat1 = natids[at1]
        nat2 = natids[at2]
        at1_rep = atom_rep(mol, at1)
        at2_rep = atom_rep(mol, at2)

        if bondavg is True:
            dis, fcfinal, stdv = get_bond_fc_with_sem(crds, fcmatrix, nat1,
                                                      nat2, scalef, 1)
            print_bond_inf_wsd(at1_rep, at2_rep, fcfinal, stdv, dis)
        else:
            dis, fcfinal = get_bond_fc_with_sem(crds, fcmatrix, nat1, nat2,
                                                scalef, 0)
            print_bond_inf(at1_rep, at2_rep, fcfinal, dis)

    # Print the 1-3 part
    if avg13 is True:
        print_13_title_wsd()
    else:
        print_13_title()

    for i in all_list.anglist:
        at1 = i[0]
        at3 = i[2]
        nat1 = natids[at1]
        nat3 = natids[at3]
        at1_rep = atom_rep(mol, at1)
        at3_rep = atom_rep(mol, at3)

        if avg13 is True:
            dis, fcfinal, stdv = get_bond_fc_with_sem(crds, fcmatrix, nat1,
                                                      nat3, scalef, 1)
            print_13_inf_wsd(at1_rep, at3_rep, fcfinal, stdv, dis)
        else:
            dis, fcfinal = get_bond_fc_with_sem(crds, fcmatrix, nat1, nat3,
                                                scalef, 0)
            print_13_inf(at1_rep, at3_rep, fcfinal, dis)

    # Print the Angle part
    if angavg is True:
        print_angle_title_wsd()
    else:
        print_angle_title()

    for i in all_list.anglist:
        at1 = i[0]
        at2 = i[1]
        at3 = i[2]
        nat1 = natids[at1]
        nat2 = natids[at2]
        nat3 = natids[at3]
        at1_rep = atom_rep(mol, at1)
        at2_rep = atom_rep(mol, at2)
        at3_rep = atom_rep(mol, at3)

        if angavg is True:
            angval, fcfinal, stdv = get_ang_fc_with_sem(
                crds, fcmatrix, nat1, nat2, nat3, scalef, 1)
            print_angle_inf_wsd(at1_rep, at2_rep, at3_rep, fcfinal, stdv,
                                angval)
        else:
            angval, fcfinal = get_ang_fc_with_sem(crds, fcmatrix, nat1, nat2,
                                                  nat3, scalef, 0)
            print_angle_inf(at1_rep, at2_rep, at3_rep, fcfinal, angval)

    # Print the Dihedral part
    print_dih_title()
    for i in all_list.dihlist:
        at1 = i[0]
        at2 = i[1]
        at3 = i[2]
        at4 = i[3]
        nat1 = natids[at1]
        nat2 = natids[at2]
        nat3 = natids[at3]
        nat4 = natids[at4]
        at1_rep = atom_rep(mol, at1)
        at2_rep = atom_rep(mol, at2)
        at3_rep = atom_rep(mol, at3)
        at4_rep = atom_rep(mol, at4)
        dihval, fcfinal = get_dih_fc_with_sem(crds, fcmatrix, nat1, nat2, nat3,
                                              nat4, scalef)
        print_dih_inf(at1_rep, at2_rep, at3_rep, at4_rep, fcfinal, dihval)

    # Print the Improper part
    print_imp_title()
    for i in all_list.implist:
        at1 = i[0]
        at2 = i[1]
        at3 = i[2]  #Central atom
        at4 = i[3]
        nat1 = natids[at1]
        nat2 = natids[at2]
        nat3 = natids[at3]
        nat4 = natids[at4]
        at1_rep = atom_rep(mol, at1)
        at2_rep = atom_rep(mol, at2)
        at3_rep = atom_rep(mol, at3)
        at4_rep = atom_rep(mol, at4)

        if mol.atoms[at1].element == mol.atoms[at2].element:
            fcfinal, dis = get_imp_fc_with_sem(
                crds, fcmatrix, nat3, nat1, nat2, nat4,
                scalef)  #Treat the central atom first
            print_imp_inf(at1_rep, at2_rep, at3_rep, at4_rep, fcfinal, dis)
        elif mol.atoms[at1].element == mol.atoms[at4].element:
            fcfinal, dis = get_imp_fc_with_sem(
                crds, fcmatrix, nat3, nat1, nat4, nat2,
                scalef)  #Treat the central atom first
            print_imp_inf(at1_rep, at4_rep, at3_rep, at2_rep, fcfinal, dis)
        elif mol.atoms[at2].element == mol.atoms[at4].element:
            fcfinal, dis = get_imp_fc_with_sem(
                crds, fcmatrix, nat3, nat2, nat4, nat1,
                scalef)  #Treat the central atom first
            print_imp_inf(at4_rep, at2_rep, at3_rep, at1_rep, fcfinal, dis)
        else:
            fcfinal, dis = get_imp_fc_with_sem(
                crds, fcmatrix, nat3, nat1, nat2, nat4,
                scalef)  #Treat the central atom first
            print_imp_inf(at1_rep, at2_rep, at3_rep, at4_rep, fcfinal, dis)
Beispiel #5
0
def gene_pre_frcmod_file(ionids, naamol2f, stpdbf, stfpf, smresf, prefcdf,
                         ffchoice, gaff, frcmodfs, watermodel):

    print("******************************************************************")
    print("*                                                                *")
    print("*===================Generate the Initial frcmod file=============*")
    print("*                                                                *")
    print("******************************************************************")

    libdict = {}
    chargedict = {}

    for mol2f in naamol2f:
        libdict1, chargedict1 = get_lib_dict(mol2f)
        libdict.update(libdict1)
        chargedict.update(chargedict1)

    #get the parameter dicts
    Params = get_parm_dict(ffchoice, gaff, frcmodfs)

    massparms = Params.mass
    bondparms = Params.bond
    angparms = Params.ang
    dihparms = Params.dih
    impparms = Params.imp
    nbparms = Params.nb
    ljedparms = Params.ljed

    #--------------------------------------------------------------------------

    #############Get the metal center information####################

    #--------------------------------------------------------------------------

    mol, atids, resids = get_atominfo_fpdb(stpdbf)

    #get the blist
    blist = get_mc_blist(mol, atids, ionids, stfpf)

    #get_all_the_lists from standard model
    all_list = get_all_list(mol, blist, atids, 10.0)

    #atom type dictionary, key is the atom id, value is atom type
    attypdict = {}

    #atom ids which has been transfered to another atom type
    atidtrans = []

    #get the information for the three from finger print
    fp = open(stfpf, 'r')
    for line in fp:
        if line[0:4] != "LINK":
            atinfo, atid, attyp1st, symbol, attyp2nd = line.split()
            atid = int(atid)
            attyp1st = addspace(attyp1st)
            attyp2nd = addspace(attyp2nd)
            attypdict[atid] = (attyp1st, attyp2nd)
            if attyp1st != attyp2nd:
                atidtrans.append(atid)
    fp.close()

    print("Atoms which has changed the atom types:", atidtrans)

    for atid in atidtrans:
        resid = mol.atoms[atid].resid
        resname = mol.residues[resid].resname
        attyp1 = attypdict[atid][0]
        attyp2 = attypdict[atid][1]
        print(str(resid) + '-' + resname + '@' + str(atid) + '-' + \
              mol.atoms[atid].atname + ' : ' + attyp1, '-->', attyp2)

    #-------------------------------------------------------------------------

    ########Print the pre-generated frcmod files##############

    #-------------------------------------------------------------------------

    fmf = open(prefcdf, 'w')

    #for atoms which changed atom types
    print('REMARK GOES HERE, THIS FILE IS GENERATED BY MCPB.PY', file=fmf)
    print('MASS', file=fmf)

    #for metal ions
    for i in ionids:
        attyp = attypdict[i][1]
        element = mol.atoms[i].element
        massi = Mass[element]
        print('YES', attyp + ' '  + str(round(massi, 2))  + \
              '                              ' + element + ' ion', file=fmf)

    #for ligating atoms
    for atid in atidtrans:
        if atid not in ionids: #not include metal ion
            atyp1 = attypdict[atid][0]
            atyp2 = attypdict[atid][1]
            print('YES', atyp2 + massparms[atyp1], file=fmf)

    #--------------------------------------------------------------------------
    bondparamsdict1 = {} #For metal ions
    bondparamsdict2 = {} #For the others

    print(' ', file=fmf)
    print('BOND', file=fmf)

    #for bond
    for bonds in all_list.bondlist:
        i = bonds[0]
        j = bonds[1]
        if list(set(ionids) & set([i, j])) != []:
            #The bonds which related to the ions
            bondtyp2 = (attypdict[i][1], attypdict[j][1])
            if (bondtyp2 not in list(bondparamsdict1.keys())) and (bondtyp2[::-1] \
                not in list(bondparamsdict1.keys())):
                bondparamsdict1[bondtyp2] = ' '
        elif list(set(atidtrans) & set([i, j])) != []:
            #The bonds related to the atoms which changed their atom types
            bondtyp1 = (attypdict[i][0], attypdict[j][0])
            bondtyp2 = (attypdict[i][1], attypdict[j][1])
            if (bondtyp2 not in list(bondparamsdict2.keys())) and (bondtyp2[::-1] \
                not in list(bondparamsdict2.keys())):
                if bondtyp1 in list(bondparms.keys()):
                    bondparamsdict2[bondtyp2] = bondparms[bondtyp1]
                elif bondtyp1[::-1] in list(bondparms.keys()):
                    bondparamsdict2[bondtyp2] = bondparms[bondtyp1[::-1]]

    for i in sorted(list(bondparamsdict1.keys())):
        print('NON', i[0] + '-' + i[1] + bondparamsdict1[i], file=fmf)

    for i in sorted(list(bondparamsdict2.keys())):
        print('YES', i[0] + '-' + i[1] + bondparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    coparas = []

    r_smresf = open(smresf, 'r')
    for line in r_smresf:
        line = line.strip('\n')
        line = line.split('-')
        resid = int(line[1])
        if ('GLY' in line) or ('KCO' in line) or ('ACE' in line):
            if resid+1 not in resids:
                coparas.append(resid)
    r_smresf.close()

    angparamsdict1 = {} #For metal ions
    angparamsdict2 = {} #For the others
    print(' ', file=fmf)
    print('ANGL', file=fmf)
    #for angle
    for angs in all_list.anglist:
        i = angs[0]
        j = angs[1]
        k = angs[2]
        if list(set(ionids) & set(angs)) != []:
            #The angles which related to the ions
            angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
            if (angtyp2 not in list(angparamsdict1.keys())) and (angtyp2[::-1] \
                not in list(angparamsdict1.keys())):
                angparamsdict1[angtyp2] = ' '
            #print >> fmf, 'NON', attypdict[i][1] + '-' + attypdict[j][1] + \
            #'-' + attypdict[k][1]
        elif list(set(atidtrans) & set(angs)) != []:
            #The angles related to the atoms
            #which changed their atom types
            angtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0])
            angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
            if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
                not in list(angparamsdict2.keys())):
                if angtyp1 in list(angparms.keys()):
                    angparamsdict2[angtyp2] = angparms[angtyp1]
                elif angtyp1[::-1] in list(angparms.keys()):
                    angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    #Add for a specific situation
    for i in coparas:
        for atid in atidtrans:
            if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
                angtyp1 = (attypdict[atid][0], 'C ', 'N ')
                angtyp2 = (attypdict[atid][1], 'C ', 'N ')
                if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
                    not in list(angparamsdict2.keys())):
                    if angtyp1 in list(angparms.keys()):
                        angparamsdict2[angtyp2] = angparms[angtyp1]
                    elif angtyp1[::-1] in list(angparms.keys()):
                        angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    for i in sorted(list(angparamsdict1.keys())):
        print('NON', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict1[i], file=fmf)
    for i in sorted(list(angparamsdict2.keys())):
        print('YES', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    dihparamsdict = {}

    #for dihedral
    for dihs in all_list.dihlist:
        i = dihs[0]
        j = dihs[1]
        k = dihs[2]
        l = dihs[3]
        if list(set(ionids) & set(dihs)) != []:
            # Treat the dihedrals related to the metal ions as zero
            dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                       attypdict[l][0])
            dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                        attypdict[l][1])
            if (dihtyp1 not in list(dihparamsdict.keys())) and (dihtyp1[::-1] \
               not in list(dihparamsdict.keys())):
                dihparamsdict[dihtyp1n] = ['    3       0.00       0.00 ', 3, '    Treat as zero by MCPB.py']

        elif list(set(atidtrans) & set(dihs)) != []:
            if list(set(atidtrans) & set(dihs[0::3])) == []:
            #Neither the 1st and 4th atom change atom types
            #There is 2nd or 3rd or both changed atom type(s)
                dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                           attypdict[l][0])
                dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                            attypdict[l][1])
                dihtyp2 = ('X ', attypdict[j][0], attypdict[k][0], 'X ')
                dihtyp2n = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

                if dihtyp1 in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
                elif dihtyp1[::-1] in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
                elif dihtyp2 in list(dihparms.keys()):
                    if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp2n] = dihparms[dihtyp2]
                elif dihtyp2[::-1] in list(dihparms.keys()):
                    if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp2n] = dihparms[dihtyp2[::-1]]
                else:
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = ['    3       0.00       0.00 ', 3, '    Treat as zero by MCPB.py']

            else:
            #There is 1st or 4th atoms or both changed the atom type(s)
                dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                           attypdict[l][0])
                dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                            attypdict[l][1])
                dihtyp2 = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

                if dihtyp1 in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
                elif dihtyp1[::-1] in list(dihparms.keys()):
                    if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                        not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
                else:
                    if (dihtyp2 in list(dihparms.keys())) or (dihtyp2[::-1] \
                        in list(dihparms.keys())):
                        continue
                    elif (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                          not in list(dihparamsdict.keys())):
                        dihparamsdict[dihtyp1n] =  ['    3       0.00       0.00 ', 3, '    Treat as zero by MCPB.py']

    #Add for a specfic situation
    for i in coparas:
        for atid in atidtrans:
            if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
                for ionid in ionids:
                    if ((atid, ionid, 1) in all_list.bondlist) or \
                       ((ionid, atid, 1) in all_list.bondlist):
                        dihtyp1 = (attypdict[ionid][0], attypdict[atid][0], 'C ', 'N ')
                        dihtyp2 = (attypdict[ionid][1], attypdict[atid][1], 'C ', 'N ')
                        if (dihtyp2 not in dihparamsdict) and (dihtyp2[::-1] not in dihparamsdict):
                            if dihtyp1 in list(dihparms.keys()):
                                dihparamsdict[dihtyp2] = dihparms[dihtyp1]
                            elif dihtyp1[::-1] in list(dihparms.keys()):
                                dihparamsdict[dihtyp2] = dihparms[dihtyp1[::-1]]
                            else:
                                dihparamsdict[dihtyp2] = ['    3       0.00       0.00 ', 3, '    Treat as zero by MCPB.py']

    print(' ', file=fmf)
    print('DIHE', file=fmf)
    # First is the term contains X and then the others, consistent with Amber
    # manual
    for keyv in sorted(list(dihparamsdict.keys())):
        if 'X ' in keyv:     #For types contain X
            valv = dihparamsdict[keyv]
            keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
            terms = len(valv)//3
            for i in range(0, terms):
                temp = i * 3
                print('YES', keyv + valv[temp] + str(valv[temp+1]).rjust(3) + \
                    '.0' + valv[temp+2], file=fmf)

    for keyv in sorted(list(dihparamsdict.keys())):
        if 'X ' not in keyv: #For types don't contain X
            valv = dihparamsdict[keyv]
            keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
            terms = len(valv)//3
            for i in range(0, terms):
                temp = i * 3
                print('YES', keyv + valv[temp] + str(valv[temp+1]).rjust(3) + \
                    '.0' + valv[temp+2], file=fmf)
    #--------------------------------------------------------------------------
    impparamsdict = {} # Dict for the new improper parameters

    # For improper torsion, which is only not necessary term in Amber force
    # field here the code only duplicates the available improper parameters
    # for changed atom types
    for imps in all_list.implist:
        i = imps[0]
        j = imps[1]
        k = imps[2]
        l = imps[3]

        # For improper torsions related to metal ions, doesn't consider them
        if list(set(ionids) & set(imps)) != []:
            continue
        elif list(set(atidtrans) & set(imps)) != []:
        # Find all possible improper torsions which are related to the atoms
        # whose atom types were changed, and get them into a dict

            imptyps = {}

            #1 situation for all atom representation
            imptyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                       attypdict[l][0])
            imptyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                        attypdict[l][1])
            imptyp1 = seq_imp(imptyp1)
            imptyp1n = seq_imp(imptyp1n)

            #3 situations for two X used in the representation, i, j, l
            imptyp2 = ('X ', 'X ', attypdict[k][0], attypdict[i][0])
            imptyp2n = ('X ', 'X ', attypdict[k][1], attypdict[i][1])
            imptyp2 = seq_imp(imptyp2)
            imptyp2n = seq_imp(imptyp2n)

            imptyp3 = ('X ', 'X ', attypdict[k][0], attypdict[j][0])
            imptyp3n = ('X ', 'X ', attypdict[k][1], attypdict[j][1])
            imptyp3 = seq_imp(imptyp3)
            imptyp3n = seq_imp(imptyp3n)

            imptyp4 = ('X ', 'X ', attypdict[k][0], attypdict[l][0])
            imptyp4n = ('X ', 'X ', attypdict[k][1], attypdict[l][1])
            imptyp4 = seq_imp(imptyp4)
            imptyp4n = seq_imp(imptyp4n)

            #6 situations for one X used in the representation: ij, ji, il, li, jl, lj
            imptyp5 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[j][0])
            imptyp5n = ('X ', attypdict[i][1], attypdict[k][1], attypdict[j][1])
            imptyp5 = seq_imp(imptyp5)
            imptyp5n = seq_imp(imptyp5n)

            imptyp6 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[i][0])
            imptyp6n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[i][1])
            imptyp6 = seq_imp(imptyp6)
            imptyp6n = seq_imp(imptyp6n)

            imptyp7 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[l][0])
            imptyp7n = ('X ', attypdict[i][0], attypdict[k][1], attypdict[l][1])
            imptyp7 = seq_imp(imptyp7)
            imptyp7n = seq_imp(imptyp7n)

            imptyp8 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[i][0])
            imptyp8n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[i][1])
            imptyp8 = seq_imp(imptyp8)
            imptyp8n = seq_imp(imptyp8n)

            imptyp9 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[l][0])
            imptyp9n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[l][1])
            imptyp9 = seq_imp(imptyp9)
            imptyp9n = seq_imp(imptyp9n)

            imptyp10 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[j][0])
            imptyp10n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[j][1])
            imptyp10 = seq_imp(imptyp10)
            imptyp10n = seq_imp(imptyp10n)

            # If the improper torsion don't change, don't consider them, this
            # is due to the old force field lib would be enough to deal with
            # these situations
            if imptyp1n != imptyp1:
                imptyps[imptyp1] = imptyp1n

            if imptyp2n != imptyp2:
                imptyps[imptyp2] = imptyp2n

            if imptyp3n != imptyp3:
                imptyps[imptyp3] = imptyp3n

            if imptyp4n != imptyp4:
                imptyps[imptyp4] = imptyp4n

            if imptyp5n != imptyp5:
                imptyps[imptyp5] = imptyp5n

            if imptyp6n != imptyp6:
                imptyps[imptyp6] = imptyp6n

            if imptyp7n != imptyp7:
                imptyps[imptyp7] = imptyp7n

            if imptyp8n != imptyp8:
                imptyps[imptyp8] = imptyp8n

            if imptyp9n != imptyp9:
                imptyps[imptyp9] = imptyp9n

            if imptyp10n != imptyp10:
                imptyps[imptyp10] = imptyp10n

            # If the corresponding old improper types available in the lib, then
            # add the parameters to the impparamsdict for new improper types
            for imptypkey in list(imptyps.keys()):
                if imptypkey in list(impparms.keys()):
                    impparamsdict[imptyps[imptypkey]] = impparms[imptypkey]

    print(' ', file=fmf)
    print('IMPR', file=fmf)
    # Print improper torsion, first has two Xs, then one X, then the others
    for i in list(impparamsdict.keys()):
        if i[0] == 'X ' and i[1] == 'X ':
            imptypkey = i[0] + '-' + i[1] + '-' + i[2] + '-' + i[3]
            print('YES', imptypkey + impparamsdict[i], file=fmf)

    for i in list(impparamsdict.keys()):
        if i[0] == 'X ' and i[1] != 'X ':
            imptypkey = i[0] + '-' + i[1] + '-' + i[2] + '-' + i[3]
            print('YES', imptypkey + impparamsdict[i], file=fmf)

    for i in list(impparamsdict.keys()):
        if 'X ' not in i:
            imptypkey = i[0] + '-' + i[1] + '-' + i[2] + '-' + i[3]
            print('YES', imptypkey + impparamsdict[i], file=fmf)
    #--------------------------------------------------------------------------
    #for nb
    print(' ', file=fmf)
    print('NONB', file=fmf)

    #For metal ions
    IonLJParaDict = get_ionljparadict(watermodel)

    for i in ionids:
        element = mol.atoms[i].element
        chg = int(round(chargedict[mol.atoms[i].resname], 0))
        attyp2 = attypdict[i][1]

        mnum = max([9-chg, chg])
        for j in range(0, mnum):

            fchg1 = chg - j
            fchg2 = chg + j

            if j == 0 and element+str(fchg1) in list(IonLJParaDict.keys()):
                rmin = IonLJParaDict[element + str(fchg1)][0]
                ep = IonLJParaDict[element + str(fchg1)][1]
                annot = IonLJParaDict[element + str(fchg1)][2]
                break
            elif fchg1 > 0 and element+str(fchg1) in list(IonLJParaDict.keys()):
                print("Could not find VDW radius for element %s with charge "
                      "+%d, use the one of charge +%d" %(element, chg, fchg1))
                rmin = IonLJParaDict[element + str(fchg1)][0]
                ep = IonLJParaDict[element + str(fchg1)][1]
                annot = IonLJParaDict[element + str(fchg1)][2]
                break
            elif fchg2 <= 8 and element+str(fchg2) in list(IonLJParaDict.keys()):
                print("Could not find VDW radius for element %s with charge "
                      "+%d, use the one of charge +%d" %(element, chg, fchg2))
                rmin = IonLJParaDict[element + str(fchg2)][0]
                ep = IonLJParaDict[element + str(fchg2)][1]
                annot = IonLJParaDict[element + str(fchg2)][2]
                break

        if rmin is None:
            raise pymsmtError("Could not find VDW parameters/radius for "
                              "element %s with charge +%d " %(element, chg))
        print('YES   %s        %8.4f %13.10f       %-s' %(attyp2, rmin, \
                 ep, annot), file=fmf)

    #For the others
    for atid in atidtrans:
        if atid not in ionids:
            attyp1 = attypdict[atid][0]
            attyp2 = attypdict[atid][1]
            print('YES  ', attyp2 + nbparms[attyp1], file=fmf)

    print(' ', file=fmf)
    print(' ', file=fmf)

    fmf.close()
    #--------------------------------------------------------------------------

    #Move the file, delete the repeat parts
    os.system("mv %s temp" %prefcdf)
    os.system("uniq temp > %s" %prefcdf)
    os.system("rm temp")
def gene_pre_frcmod_file(ionids, naamol2f, stpdbf, stfpf, smresf, prefcdf,
                         ffchoice, gaff, frcmodfs, watermodel):

    print("******************************************************************")
    print("*                                                                *")
    print("*===================Generate the Initial frcmod file=============*")
    print("*                                                                *")
    print("******************************************************************")
 
    libdict = {}
    chargedict = {}

    for mol2f in naamol2f:
      libdict1, chargedict1 = get_lib_dict(mol2f)
      libdict.update(libdict1)
      chargedict.update(chargedict1)

    #get the parameter dicts
    Params = get_parm_dict(ffchoice, gaff, frcmodfs)

    massparms = Params.mass
    bondparms = Params.bond
    angparms = Params.ang
    dihparms = Params.dih
    impparms = Params.imp
    nbparms = Params.nb

    #--------------------------------------------------------------------------

    #############Get the metal center information####################

    #--------------------------------------------------------------------------

    mol, atids, resids = get_atominfo_fpdb(stpdbf)

    #get the blist
    blist = get_mc_blist(mol, atids, ionids, stfpf)

    #get_all_the_lists from standard model
    all_list = get_all_list(mol, blist, atids, 10.0)

    #atom type dictionary, key is the atom id, value is atom type
    attypdict = {}

    #atom ids which has been transfered to another atom type
    atidtrans = []

    #get the information for the three from finger print
    fp = open(stfpf, 'r')
    for line in fp:
      if line[0:4] != "LINK":
        atinfo, atid, attyp1st, symbol, attyp2nd = line.split()
        atid = int(atid)
        attyp1st = addspace(attyp1st)
        attyp2nd = addspace(attyp2nd)
        attypdict[atid] = (attyp1st, attyp2nd)
        if attyp1st != attyp2nd:
          atidtrans.append(atid)
    fp.close()

    print("Atoms which has changed the atom types:", atidtrans)

    for atid in atidtrans:
      resid = mol.atoms[atid].resid
      resname = mol.residues[resid].resname
      attyp1 = attypdict[atid][0]
      attyp2 = attypdict[atid][1]
      print(str(resid) + '-' + resname + '@' + str(atid) + '-' + \
            mol.atoms[atid].atname + ' : ' + attyp1, '-->', attyp2)

    #-------------------------------------------------------------------------

    ########Print the pre-generated frcmod files##############

    #-------------------------------------------------------------------------

    fmf = open(prefcdf, 'w')

    #for atoms which changed atom types
    print('REMARK GOES HERE, THIS FILE IS GENERATED BY MCPB.PY', file=fmf)
    print('MASS', file=fmf)

    #for metal ions
    for i in ionids:
      attyp = attypdict[i][1]
      atname = mol.atoms[i].atname
      if len(atname) > 1:
        atname = atname[0] + atname[1:].lower()
      massi = Mass[atname]
      print('YES', attyp + ' '  + str(round(massi, 2))  + \
            '                              ' + atname + ' ion', file=fmf)

    #for ligating atoms
    for atid in atidtrans:
      if atid not in ionids: #not include metal ion
        atyp1 = attypdict[atid][0]
        atyp2 = attypdict[atid][1]
        print('YES', atyp2 + massparms[atyp1], file=fmf)

    #--------------------------------------------------------------------------
    bondparamsdict1 = {} #For metal ions
    bondparamsdict2 = {} #For the others

    print(' ', file=fmf)
    print('BOND', file=fmf)

    #for bond
    for bonds in all_list.bondlist:
      i = bonds[0]
      j = bonds[1]
      if list(set(ionids) & set(bonds)) != []:
        #The bonds which related to the ions
        bondtyp2 = (attypdict[i][1], attypdict[j][1])
        if (bondtyp2 not in list(bondparamsdict1.keys())) and (bondtyp2[::-1] \
            not in list(bondparamsdict1.keys())):
          bondparamsdict1[bondtyp2] = ' '
      elif list(set(atidtrans) & set(bonds)) != []:
        #The bonds related to the atoms which changed their atom types
        bondtyp1 = (attypdict[i][0], attypdict[j][0])
        bondtyp2 = (attypdict[i][1], attypdict[j][1])
        if (bondtyp2 not in list(bondparamsdict2.keys())) and (bondtyp2[::-1] \
            not in list(bondparamsdict2.keys())):
          if bondtyp1 in list(bondparms.keys()):
            bondparamsdict2[bondtyp2] = bondparms[bondtyp1]
          elif bondtyp1[::-1] in list(bondparms.keys()):
            bondparamsdict2[bondtyp2] = bondparms[bondtyp1[::-1]]

    for i in sorted(list(bondparamsdict1.keys())):
      print('NON', i[0] + '-' + i[1] + bondparamsdict1[i], file=fmf)

    for i in sorted(list(bondparamsdict2.keys())):
      print('YES', i[0] + '-' + i[1] + bondparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    coparas = []

    r_smresf = open(smresf, 'r')
    for line in r_smresf:
      line = line.strip('\n')
      line = line.split('-')
      resid = int(line[1])
      if ('GLY' in line) or ('KCO' in line) or ('ACE' in line):
        if resid+1 not in resids:
          coparas.append(resid)
    r_smresf.close()

    angparamsdict1 = {} #For metal ions
    angparamsdict2 = {} #For the others
    print(' ', file=fmf)
    print('ANGL', file=fmf)
    #for angle
    for angs in all_list.anglist:
      i = angs[0]
      j = angs[1]
      k = angs[2]
      if list(set(ionids) & set(angs)) != []:
        #The angles which related to the ions
        angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
        if (angtyp2 not in list(angparamsdict1.keys())) and (angtyp2[::-1] \
            not in list(angparamsdict1.keys())):
          angparamsdict1[angtyp2] = ' '
        #print >> fmf, 'NON', attypdict[i][1] + '-' + attypdict[j][1] + \
        #'-' + attypdict[k][1]
      elif list(set(atidtrans) & set(angs)) != []:
        #The angles related to the atoms
        #which changed their atom types
        angtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0])
        angtyp2 = (attypdict[i][1], attypdict[j][1], attypdict[k][1])
        if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
            not in list(angparamsdict2.keys())):
          if angtyp1 in list(angparms.keys()):
            angparamsdict2[angtyp2] = angparms[angtyp1]
          elif angtyp1[::-1] in list(angparms.keys()):
            angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    #Add for a specific situation
    for i in coparas:
      for atid in atidtrans:
        if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
          angtyp1 = (attypdict[atid][0], 'C ', 'N ')
          angtyp2 = (attypdict[atid][1], 'C ', 'N ')
          if (angtyp2 not in list(angparamsdict2.keys())) and (angtyp2[::-1] \
              not in list(angparamsdict2.keys())):
            if angtyp1 in list(angparms.keys()):
              angparamsdict2[angtyp2] = angparms[angtyp1]
            elif angtyp1[::-1] in list(angparms.keys()):
              angparamsdict2[angtyp2] = angparms[angtyp1[::-1]]

    for i in sorted(list(angparamsdict1.keys())):
      print('NON', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict1[i], file=fmf)
    for i in sorted(list(angparamsdict2.keys())):
      print('YES', i[0] + '-' + i[1] + '-' + i[2] + angparamsdict2[i], file=fmf)

    #--------------------------------------------------------------------------
    dihparamsdict = {}

    #for dihedral
    for dihs in all_list.dihlist:
      i = dihs[0]
      j = dihs[1]
      k = dihs[2]
      l = dihs[3]
      if list(set(ionids) & set(dihs)) != []:
        #The dihedral related to the metal ions
        dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                   attypdict[l][0])
        dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                    attypdict[l][1])
        if (dihtyp1 not in list(dihparamsdict.keys())) and (dihtyp1[::-1] \
           not in list(dihparamsdict.keys())):
          dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                     '          3.   ', ' ']
      elif list(set(atidtrans) & set(dihs)) != []:
        if list(set(atidtrans) & set(dihs[0::3])) == []:
        #Neither the 1st and 4th atom change atom types
        #There is 2nd or 3rd or both changed atom type(s)
          dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                     attypdict[l][0])
          dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                      attypdict[l][1])
          dihtyp2 = ('X ', attypdict[j][0], attypdict[k][0], 'X ')
          dihtyp2n = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

          if dihtyp1 in list(dihparms.keys()):
            if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
          elif dihtyp1[::-1] in list(dihparms.keys()):
            if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
          elif dihtyp2 in list(dihparms.keys()):
            if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp2n] = dihparms[dihtyp2]
          elif dihtyp2[::-1] in list(dihparms.keys()):
            if (dihtyp2n not in list(dihparamsdict.keys())) and (dihtyp2n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp2n] = dihparms[dihtyp2[::-1]]
          else:
            if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                         '          3.   ', ' ']
        else:
        #There is 1st or 4th atoms or both changed the atom type(s)
          dihtyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                     attypdict[l][0])
          dihtyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                      attypdict[l][1])
          dihtyp2 = ('X ', attypdict[j][1], attypdict[k][1], 'X ')

          if dihtyp1 in list(dihparms.keys()):
            if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = dihparms[dihtyp1]
          elif dihtyp1[::-1] in list(dihparms.keys()):
            if (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = dihparms[dihtyp1[::-1]]
          else:
            if (dihtyp2 in list(dihparms.keys())) or (dihtyp2[::-1] \
                in list(dihparms.keys())):
              continue
            elif (dihtyp1n not in list(dihparamsdict.keys())) and (dihtyp1n[::-1] \
                  not in list(dihparamsdict.keys())):
              dihparamsdict[dihtyp1n] = ['   3    0.00          0.0   ', \
                                         '          3.   ', ' ']

    #Add for a specfic situation
    for i in coparas:
      for atid in atidtrans:
        if mol.atoms[atid].atname == 'O' and mol.atoms[atid].resid == i:
          for ionid in ionids:
            if ((atid, ionid, 1) in all_list.bondlist) or \
               ((ionid, atid, 1) in all_list.bondlist):
              dihtyp1 = (attypdict[ionid][0], attypdict[atid][0], 'C ', 'N ')
              dihtyp2 = (attypdict[ionid][1], attypdict[atid][1], 'C ', 'N ')
              if (dihtyp2 not in dihparamsdict) and (dihtyp2[::-1] not in dihparamsdict):
                if dihtyp1 in list(dihparms.keys()):
                  dihparamsdict[dihtyp2] = dihparms[dihtyp1]
                elif dihtyp1[::-1] in list(dihparms.keys()):
                  dihparamsdict[dihtyp2] = dihparms[dihtyp1[::-1]]
                else:
                  dihparamsdict[dihtyp2] = ['   3    0.00          0.0   ', \
                                         '          3.   ', ' ']

    print(' ', file=fmf)
    print('DIHE', file=fmf)
    for keyv in sorted(list(dihparamsdict.keys())):
      if 'X ' not in keyv: #For types don't contain X
        valv = dihparamsdict[keyv]
        keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
        terms = len(valv)//3
        for i in range(0, terms):
          temp = i * 3
          print('YES', keyv, valv[temp] + valv[temp+1] + \
                   valv[temp+2], file=fmf)

    for keyv in sorted(list(dihparamsdict.keys())):
      if 'X ' in keyv:     #For types contain X
        valv = dihparamsdict[keyv]
        keyv = keyv[0] + '-' + keyv[1] + '-' + keyv[2] + '-' + keyv[3]
        terms = len(valv)//3
        for i in range(0, terms):
          temp = i * 3
          print('YES', keyv, valv[temp] + valv[temp+1] + \
                   valv[temp+2], file=fmf)
    #--------------------------------------------------------------------------
    impparamsdict = {}

    #For improper torsion
    for imps in all_list.implist:
      i = imps[0]
      j = imps[1]
      k = imps[2]
      l = imps[3]

      if list(set(ionids) & set(imps)) != []:
        continue
      elif list(set(atidtrans) & set(imps)) != []:
        imptyps = {}

        #1 situation
        imptyp1 = (attypdict[i][0], attypdict[j][0], attypdict[k][0], \
                   attypdict[l][0])
        imptyp1n = (attypdict[i][1], attypdict[j][1], attypdict[k][1], \
                    attypdict[l][1])

        #3 situations
        imptyp2 = ('X ', 'X ', attypdict[k][0], attypdict[i][0])
        imptyp2n = ('X ', 'X ', attypdict[k][1], attypdict[i][1])

        imptyp3 = ('X ', 'X ', attypdict[k][0], attypdict[j][0])
        imptyp3n = ('X ', 'X ', attypdict[k][1], attypdict[j][1])

        imptyp4 = ('X ', 'X ', attypdict[k][0], attypdict[l][0])
        imptyp4n = ('X ', 'X ', attypdict[k][1], attypdict[l][1])

        #6 situations
        imptyp5 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[j][0])
        imptyp5n = ('X ', attypdict[i][1], attypdict[k][1], attypdict[j][1])

        imptyp6 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[i][0])
        imptyp6n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[i][1])

        imptyp7 = ('X ', attypdict[i][0], attypdict[k][0], attypdict[l][0])
        imptyp7n = ('X ', attypdict[i][0], attypdict[k][1], attypdict[l][1])

        imptyp8 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[i][0])
        imptyp8n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[i][1])

        imptyp9 = ('X ', attypdict[j][0], attypdict[k][0], attypdict[l][0])
        imptyp9n = ('X ', attypdict[j][1], attypdict[k][1], attypdict[l][1])

        imptyp10 = ('X ', attypdict[l][0], attypdict[k][0], attypdict[j][0])
        imptyp10n = ('X ', attypdict[l][1], attypdict[k][1], attypdict[j][1])

        imptyps[imptyp1] = imptyp1n
        imptyps[imptyp2] = imptyp2n
        imptyps[imptyp3] = imptyp3n
        imptyps[imptyp4] = imptyp4n
        imptyps[imptyp5] = imptyp5n
        imptyps[imptyp6] = imptyp6n
        imptyps[imptyp7] = imptyp7n
        imptyps[imptyp8] = imptyp8n
        imptyps[imptyp9] = imptyp9n
        imptyps[imptyp10] = imptyp10n

        for imptypkey in list(imptyps.keys()):
          if imptypkey in list(impparms.keys()):
            impparamsdict[imptyps[imptypkey]] = impparms[imptypkey]

    print(' ', file=fmf)
    print('IMPR', file=fmf)
    #for improper torsion
    for i in sorted(list(impparamsdict.keys())):
      if i not in list(impparms.keys()):
        imptypkey = i[0] + '-' + i[1] + '-' + i[2] + '-' + i[3]
        print('YES', imptypkey, impparamsdict[i], file=fmf)
    #--------------------------------------------------------------------------
    #for nb
    print(' ', file=fmf)
    print('NONB', file=fmf)

    #For metal ions
    IonLJParaDict = get_ionljparadict(watermodel)
    for i in ionids:
      element = mol.atoms[i].element
      chg = str(int(chargedict[mol.atoms[i].resname]))
      attyp2 = attypdict[i][1]
      rmin = IonLJParaDict[element + chg][0]
      ep = IonLJParaDict[element + chg][1]
      annot = IonLJParaDict[element + chg][2]
      print('YES   %s    %11.3f  %13.10f       %-s' %(attyp2, rmin, \
               ep, annot), file=fmf)

    #For the others
    for atid in atidtrans: 
      if atid not in ionids:
        attyp1 = attypdict[atid][0]
        attyp2 = attypdict[atid][1]
        print('YES  ', attyp2 + nbparms[attyp1], file=fmf)

    print(' ', file=fmf)
    print(' ', file=fmf)

    fmf.close()
    #--------------------------------------------------------------------------

    #Move the file, delete the repeat parts
    os.system("mv %s temp" %prefcdf)
    os.system("uniq temp > %s" %prefcdf)
    os.system("rm temp")