Beispiel #1
0
def reparametrize(diheds, selection, start_conf, start_xvg, end_conf, end_xvg,
                  top):
    Nswarms = len(diheds[0])
    rsel = res_selection.res_select('%s' % start_conf, '%s' % selection)

    # calculate average drift in collective variables space
    sys.stderr.write('Residue selection: %s' % rsel)
    newpts = []
    for interp in range(len(diheds)):
        avg = []
        for r in rsel:
            driftList = []
            for i in range(len(diheds[interp])):
                vec = []
                xvg = open(diheds[interp][i], 'r')
                for line in xvg:
                    if re.search(r'\-%d\n' % r, line):
                        phi_val = float(line.split()[0])
                        psi_val = float(line.split()[1])
                        vec += [phi_val, psi_val]
                driftList.append(vec)
            # driftList has phi,psi values for residue in every swarm
            driftdat = open('dihedrals%d.dat' % interp, 'w')
            for pt in driftList:
                driftdat.write('%f %f\n' % (pt[0], pt[1]))
            avg += [scale((1 / float(Nswarms)), reduce(mapadd, driftList))]
        newpts += avg

    # extract initial and target dihedral values
    initpt = []
    for r in rsel:
        xvg = open(start_xvg, 'r')
        for line in xvg:
            if re.search(r'\-%d\n' % r, line):
                phi_val = float(line.split()[0])
                psi_val = float(line.split()[1])
                initpt += [phi_val, psi_val]

    targetpt = []
    for r in rsel:
        xvg = open(end_xvg, 'r')
        for line in xvg:
            if re.search(r'\-%d\n' % r, line):
                phi_val = float(line.split()[0])
                psi_val = float(line.split()[1])
                targetpt += [phi_val, psi_val]
    # something with 1 indexing makes this padding necessary.
    paddingpt = [0] * len(initpt)
    newpts.insert(0, initpt)
    newpts.append(targetpt)
    newpts.append(paddingpt)
    sys.stderr.write('The new list of points is: %s\n' % newpts)
    for pt in newpts:
        sys.stderr.write('%s %s\n' % (pt[0], pt[1]))
    adjusted = rep_pts(newpts)
    # TODO implement a dist_treshold=1.0
    iters = [adjusted]
    for i in range(100):
        iters.append(rep_pts(iters[i]))

    adjusted = iters[-1]
    # delete the padding point
    adjusted = adjusted[:-1]
    sys.stderr.write('The adjusted points are:\n')
    for pt in adjusted:
        sys.stderr.write('%s %s\n' % (pt[0], pt[1]))

    # calculate reparam distance

    # TODO measure the distance between the reparametrized points and the input points

    # write the topology for the next iteration
    # treat the reparam values as a stack

    # temporary additional restraints for alanine dipeptide
    theta_val = [
        1.6, 1.48, 1.36, 1.24, 1.12, 1.0, 0.8799999999999999,
        0.7599999999999999, 0.6399999999999999, 0.5199999999999998,
        0.3999999999999999, 0.2799999999999998, 0.1599999999999997,
        0.039999999999999813, -0.0800000000000003, -0.20000000000000018,
        -0.3200000000000003, -0.4400000000000004, -0.5600000000000005, -0.8
    ]
    zeta_val = [
        -4.3, -3.8, -3.3, -2.8, -2.3, -1.7999999999999998, -1.2999999999999998,
        -0.7999999999999998, -0.2999999999999998, 0.20000000000000018,
        0.7000000000000002, 1.2000000000000002, 1.7000000000000002, 2.2, 2.7,
        3.2, 3.7, 4.2, 4.7, 5.7
    ]

    top = open(top, 'r').read().split('#include dihedral_restraints')
    for k in range(1, len(adjusted) - 1):
        newtop = open('%d.top' % k, 'w')
        newtop.write('%s' % top[0])
        sys.stderr.write("Writing restraints for interpolant index %i\n" % k)
        newtop.write("[ dihedral_restraints ]\n")
        newtop.write(
            "; ai   aj   ak   al  type  label  phi  dphi  kfac  power\n")
        stack = adjusted[k]
        protein = res_selection.protein('%s' % start_conf)
        for r in rsel:
            # there may be multiple residues matching the resnr, e.g., dimers
            phi = [
                a for a in protein
                if (a.resnr == int(r) and (a.atomname == 'CA' or a.atomname ==
                                           'N' or a.atomname == 'C')) or
                (a.resnr == int(r) - 1 and a.atomname == 'C')
            ]

            psi = [
                a for a in protein
                if (a.resnr == int(r) and (a.atomname == 'N' or a.atomname ==
                                           'CA' or a.atomname == 'C')) or
                (a.resnr == int(r) + 1 and a.atomname == 'N')
            ]

            # get phi and psi values from the reparametrization vector
            numres = len(phi) / 4
            for i in range(numres):
                phi_val = stack[i]
                psi_val = stack[i + 1]
                # write phi, psi angles
                # TODO EXPLICIT DIHEDRALS FOR ALANINE DIPEPTIDE
                newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n" %
                             (5, 7, 9, 15, 1, 1, phi_val, 0, 1, 2))
                newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n" %
                             (7, 9, 15, 17, 1, 1, psi_val, 0, 1, 2))

                newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n" %
                             (1, 5, 7, 9, 1, 1, theta_val[k], 0, 1, 2))
                newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n" %
                             (9, 15, 17, 19, 1, 1, zeta_val[k], 0, 1, 2))

                #newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(phi[i*4].atomnr,phi[i*4+1].atomnr,
                #                  phi[i*4+2].atomnr,phi[i*4+3].atomnr,1,1,phi_val,0,1,2))
                #newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(psi[i*4].atomnr,psi[i*4+1].atomnr,
                #                  psi[i*4+2].atomnr,psi[i*4+3].atomnr,1,1,psi_val,0,1,2))

            # delete the already added values from the stack
            stack = stack[numres * 2 - 1:]
        newtop.write('%s' % top[1])
Beispiel #2
0
def reparametrize(diheds, selection, start_conf, start_xvg, end_conf, end_xvg, top): 
    Nswarms = len(diheds[0])
    rsel = res_selection.res_select('%s'%start_conf,'%s'%selection)
    
    # calculate average drift in collective variables space
    sys.stderr.write('Residue selection: %s' %rsel)
    newpts = []
    for interp in range(len(diheds)):
            avg = []
            for r in rsel:
                    driftList = []
                    for i in range(len(diheds[interp])):
                            vec=[]
                            xvg = open(diheds[interp][i],'r')
                            for line in xvg:
                                    if re.search(r'\-%d\n'%r,line):
                                            phi_val = float(line.split()[0])
                                            psi_val = float(line.split()[1])
                                            vec+=[phi_val,psi_val]
                            driftList.append(vec)
                    # driftList has phi,psi values for residue in every swarm
                    driftdat=open('dihedrals%d.dat'%interp,'w')
                    for pt in driftList:
                        driftdat.write('%f %f\n'%(pt[0],pt[1]))
                    avg+=[scale((1/float(Nswarms)),reduce(mapadd,driftList))]
            newpts+=avg

    # extract initial and target dihedral values
    initpt = []
    for r in rsel:
            xvg = open(start_xvg,'r')
            for line in xvg:
                    if re.search(r'\-%d\n'%r,line):
                            phi_val = float(line.split()[0])
                            psi_val = float(line.split()[1])
                            initpt+=[phi_val,psi_val]

    targetpt = []
    for r in rsel:
            xvg = open(end_xvg,'r')
            for line in xvg:
                    if re.search(r'\-%d\n'%r,line):
                            phi_val = float(line.split()[0])
                            psi_val = float(line.split()[1])
                            targetpt+=[phi_val,psi_val]
    # something with 1 indexing makes this padding necessary.
    paddingpt=[0]*len(initpt)
    newpts.insert(0,initpt)
    newpts.append(targetpt)
    newpts.append(paddingpt)
    sys.stderr.write('The new list of points is: %s\n' %newpts)
    for pt in newpts:
        sys.stderr.write('%s %s\n'%(pt[0],pt[1]))
    adjusted=rep_pts(newpts)
    # TODO implement a dist_treshold=1.0
    iters=[adjusted]
    for i in range(100):
        iters.append(rep_pts(iters[i]))

    adjusted=iters[-1]
    # delete the padding point
    adjusted=adjusted[:-1]
    sys.stderr.write('The adjusted points are:\n')
    for pt in adjusted:
        sys.stderr.write('%s %s\n'%(pt[0],pt[1]))

    # calculate reparam distance

    # TODO measure the distance between the reparametrized points and the input points

    # write the topology for the next iteration
    # treat the reparam values as a stack
    
    # temporary additional restraints for alanine dipeptide
    theta_val=[1.6, 1.48, 1.36, 1.24, 1.12, 1.0, 0.8799999999999999, 0.7599999999999999, 0.6399999999999999, 0.5199999999999998, 0.3999999999999999, 0.2799999999999998, 0.1599999999999997, 0.039999999999999813, -0.0800000000000003, -0.20000000000000018, -0.3200000000000003, -0.4400000000000004, -0.5600000000000005, -0.8]
    zeta_val=[-4.3, -3.8, -3.3, -2.8, -2.3, -1.7999999999999998, -1.2999999999999998, -0.7999999999999998, -0.2999999999999998, 0.20000000000000018, 0.7000000000000002, 1.2000000000000002, 1.7000000000000002, 2.2, 2.7, 3.2, 3.7, 4.2, 4.7, 5.7]

    top=open(top,'r').read().split('#include dihedral_restraints')
    for k in range(1,len(adjusted)-1):
            newtop=open('%d.top'%k,'w')
            newtop.write('%s'%top[0])
            sys.stderr.write("Writing restraints for interpolant index %i\n" %k)
            newtop.write("[ dihedral_restraints ]\n")
            newtop.write("; ai   aj   ak   al  type  label  phi  dphi  kfac  power\n")
            stack=adjusted[k]
            protein = res_selection.protein('%s'%start_conf)
            for r in rsel:
                    # there may be multiple residues matching the resnr, e.g., dimers
                    phi = [a for a in protein if (a.resnr == int(r) and
                          (a.atomname == 'CA' or a.atomname == 'N' or a.atomname == 'C')) or
                          (a.resnr == int(r)-1 and a.atomname == 'C')]

                    psi = [a for a in protein if (a.resnr == int(r) and
                          (a.atomname == 'N' or a.atomname == 'CA' or a.atomname == 'C')) or
                          (a.resnr == int(r)+1 and a.atomname == 'N')]

                    # get phi and psi values from the reparametrization vector
                    numres = len(phi)/4
                    for i in range(numres):
                            phi_val=stack[i]
                            psi_val=stack[i+1]
                            # write phi, psi angles
                            # TODO EXPLICIT DIHEDRALS FOR ALANINE DIPEPTIDE
                            newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(5,7,9,15,1,1,phi_val,0,1,2))
                            newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(7,9,15,17,1,1,psi_val,0,1,2))

                            newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(1,5,7,9,1,1,theta_val[k],0,1,2))
                            newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(9,15,17,19,1,1,zeta_val[k],0,1,2))

                            #newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(phi[i*4].atomnr,phi[i*4+1].atomnr,
                            #                  phi[i*4+2].atomnr,phi[i*4+3].atomnr,1,1,phi_val,0,1,2))
                            #newtop.write("%5d%5d%5d%5d%5d%5d%8.4f%5d%5d%5d\n"%(psi[i*4].atomnr,psi[i*4+1].atomnr,
                            #                  psi[i*4+2].atomnr,psi[i*4+3].atomnr,1,1,psi_val,0,1,2))

                    # delete the already added values from the stack
                    stack = stack[numres*2-1:]
            newtop.write('%s'%top[1])
import argparse
import res_selection

parser = argparse.ArgumentParser()
parser.add_argument('-i',required=True, help='The initial .gro file')
parser.add_argument('-n',required=True, help='The number of interpolation steps')
parser.add_argument('-x',required=True, help='The desired index file (.ndx)')
parser.add_argument('-p',required=True, help='A topology file for the restraints, must contain the line "#include dihedral_restraints" where the restraints are to be written')
args = vars(parser.parse_args())

conf=args['i']
ndx=args['x']
n=int(args['n'])
top = open(args['p'],'r').read()
selection = res_selection.res_select(conf,ndx)
protein = res_selection.protein(conf)

top=top.split('#include dihedral_restraints')
print selection

theta_val=[1.6, 1.48, 1.36, 1.24, 1.12, 1.0, 0.8799999999999999, 0.7599999999999999, 0.6399999999999999, 0.5199999999999998, 0.3999999999999999, 0.2799999999999998, 0.1599999999999997, 0.039999999999999813, -0.0800000000000003, -0.20000000000000018, -0.3200000000000003, -0.4400000000000004, -0.5600000000000005, -0.8]
zeta_val=[-4.3, -3.8, -3.3, -2.8, -2.3, -1.7999999999999998, -1.2999999999999998, -0.7999999999999998, -0.2999999999999998, 0.20000000000000018, 0.7000000000000002, 1.2000000000000002, 1.7000000000000002, 2.2, 2.7, 3.2, 3.7, 4.2, 4.7, 5.7]


for k in range(1,n):
    newtop=open('%s.top'%k,'w')
    # write the initial part of the topology file
    newtop.write('%s'%top[0])
    xvg = open('%s.xvg'%k,'r').readlines()
    #print "Writing restraints for interpolant number %i" %k
    newtop.write("[ dihedral_restraints ]\n")