예제 #1
0
파일: OptC4.py 프로젝트: pengfeili1/pymsmt
def get_rmsd(initparas):

    global idxs, mcresids2, atompairs

    #Modify the C4 terms in the prmtop file
    for i in range(0, len(idxs)):
        prmtop.parm_data['LENNARD_JONES_CCOEF'][idxs[i]] = initparas[i]

    #Overwrite the prmtop file
    prmtop.write_parm('OptC4.top')

    #Perform the OpenMM optimization
    #Use AmberParm function to transfer the topology and
    #coordinate file to the object OpenMM can use
    Ambermol = AmberParm('OptC4.top', options.cfile)

    # Create the OpenMM system
    print('Creating OpenMM System')
    if options.simupha == 'gas':
        system = Ambermol.createSystem(nonbondedMethod=app.NoCutoff)
    elif options.simupha == 'liquid':
        system = Ambermol.createSystem(nonbondedMethod=app.PME,
                                       nonbondedCutoff=8.0*u.angstroms,
                                       constraints=app.HBonds,)

    #Add restraints
    force = mm.CustomExternalForce("k*((x-x0)^2+(y-y0)^2+(z-z0)^2)")
    force.addGlobalParameter("k", 200.0)
    force.addPerParticleParameter("x0")
    force.addPerParticleParameter("y0")
    force.addPerParticleParameter("z0")
    #for i in range(0, len(Ambermol.atoms)):
    for i, atom_crd in enumerate(Ambermol.positions):
        #if (Ambermol.atoms[i].residue.number+1 not in mcresids2) and \
        if (i+1 not in mcresids2) and \
          (Ambermol.atoms[i].residue.name not in ['WAT', 'HOH']) and \
          (Ambermol.atoms[i].name in ['CA', 'C', 'N']):
            force.addParticle(i, atom_crd.value_in_unit(u.nanometers))
    system.addForce(force)

    # Create the integrator to do Langevin dynamics
    # Temperature of heat bath, Friction coefficient, Time step
    integrator = mm.LangevinIntegrator(300*u.kelvin, 1.0/u.picoseconds,
                                       1.0*u.femtoseconds,)

    # Define the platform to use; CUDA, OpenCL, CPU, or Reference. Or do not
    # specify the platform to use the default (fastest) platform
    # Create the Simulation object
    if options.platf == 'ref':
        platform = mm.Platform.getPlatformByName('Reference')
        sim = app.Simulation(Ambermol.topology, system, integrator, platform)
    elif options.platf == 'cpu':
        platform = mm.Platform.getPlatformByName('CPU')
        sim = app.Simulation(Ambermol.topology, system, integrator, platform)
    elif options.platf == 'cuda':
        platform = mm.Platform.getPlatformByName('CUDA')
        prop = dict(CudaPrecision='mixed')
        sim = app.Simulation(Ambermol.topology, system, integrator, platform,
                             prop)
    elif options.platf == 'opencl':
        platform = mm.Platform.getPlatformByName('OpenCL')
        prop = dict(CudaPrecision='mixed')
        sim = app.Simulation(Ambermol.topology, system, integrator, platform,
                             prop)

    # Set the particle positions
    sim.context.setPositions(Ambermol.positions)

    # Output the rst file
    restrt = RestartReporter(options.rfile, 100, write_velocities=False)
    sim.reporters.append(restrt)

    # Minimize the energy
    print('Minimizing energy ' + str(options.maxsteps) + ' steps.')
    sim.minimizeEnergy(maxIterations=options.maxsteps)

    # Overwrite the final file
    state = sim.context.getState(getPositions=True, enforcePeriodicBox=True)
    restrt.report(sim, state)

    val_aft_min = []
    crds_aft_min = read_rstf(options.rfile)
    for i in atompairs:
        if len(i) == 2:
            crd1 = crds_aft_min[i[0]-1]
            crd2 = crds_aft_min[i[1]-1]
            bond = calc_bond(crd1, crd2)
            val_aft_min.append(('bond', bond))
        elif len(i) == 3:
            crd1 = crds_aft_min[i[0]-1]
            crd2 = crds_aft_min[i[1]-1]
            crd3 = crds_aft_min[i[2]-1]
            angle = calc_angle(crd1, crd2, crd3)
            val_aft_min.append(('angle', angle))
        elif len(i) == 4:
            crd1 = crds_aft_min[i[0]-1]
            crd2 = crds_aft_min[i[1]-1]
            crd3 = crds_aft_min[i[2]-1]
            crd4 = crds_aft_min[i[3]-1]
            dih = calc_dih(crd1, crd2, crd3, crd4)
            val_aft_min.append(('dih', dih))

    valdiffs = []
    for i in range(0, len(atompairs)):
        if val_bf_min[i][0] == 'bond':
            valdiff = abs(val_aft_min[i][1] - val_bf_min[i][1]) * 1.0 / 100.0
        elif val_bf_min[i][0] == 'angle':
            valdiff = abs(val_aft_min[i][1] - val_bf_min[i][1]) * 1.0 / 2.0
        elif val_bf_min[i][0] == 'dih':
            valdiff = abs(val_aft_min[i][1] - val_bf_min[i][1])
            if (360.0 - valdiff < valdiff):
                valdiff = 360.0 - valdiff
        valdiffs.append(valdiff)

    fnldiff = numpy.sum(valdiffs)
    print(fnldiff)

    return fnldiff
예제 #2
0
def get_rmsd(initparas):

    global idxs

    print(initparas)

    #Modify the C4 terms in the prmtop file
    for i in range(0, len(idxs)):
      prmtop.parm_data['LENNARD_JONES_CCOEF'][idxs[i]] = initparas[i]

    #Overwrite the prmtop file
    prmtop.write_parm('OptC4.top')

    #Perform the OpenMM optimization
    #Use AmberParm function to transfer the topology and
    #coordinate file to the object OpenMM can use
    Ambermol = AmberParm('OptC4.top', options.cfile)

    # Create the OpenMM system
    print('Creating OpenMM System')
    if options.simupha == 'gas':
      system = Ambermol.createSystem(nonbondedMethod=app.NoCutoff)
    elif options.simupha == 'liquid':
      system = Ambermol.createSystem(nonbondedMethod=app.PME,
                                     nonbondedCutoff=8.0*u.angstroms,
                                     constraints=app.HBonds,)

    # Create the integrator to do Langevin dynamics
    # Temperature of heat bath, Friction coefficient, Time step
    integrator = mm.LangevinIntegrator(300*u.kelvin, 1.0/u.picoseconds,
                                       1.0*u.femtoseconds,)

    # Define the platform to use; CUDA, OpenCL, CPU, or Reference. Or do not
    # specify the platform to use the default (fastest) platform
    # Create the Simulation object
    if options.platf == 'ref':
       platform = mm.Platform.getPlatformByName('Reference')
       sim = app.Simulation(Ambermol.topology, system, integrator, platform)
    elif options.platf == 'cpu':
       platform = mm.Platform.getPlatformByName('CPU')
       sim = app.Simulation(Ambermol.topology, system, integrator, platform)
    elif options.platf == 'cuda':
       platform = mm.Platform.getPlatformByName('CUDA')
       prop = dict(CudaPrecision='mixed')
       sim = app.Simulation(Ambermol.topology, system, integrator, platform,
                            prop)
    elif options.platf == 'opencl':
       platform = mm.Platform.getPlatformByName('OpenCL')
       prop = dict(CudaPrecision='mixed')
       sim = app.Simulation(Ambermol.topology, system, integrator, platform,
                            prop)

    # Set the particle positions
    sim.context.setPositions(Ambermol.positions)

    # Output the rst file
    restrt = RestartReporter(options.rfile, 100, write_velocities=False)
    sim.reporters.append(restrt)

    # Minimize the energy
    print('Minimizing energy ' + str(options.maxsteps) + ' steps.')
    sim.minimizeEnergy(maxIterations=options.maxsteps)

    # Overwrite the final file
    state = sim.context.getState(getPositions=True, enforcePeriodicBox=True)
    restrt.report(sim, state)

    print('Calculate the RMSD')
    # Perform the RMSD calcualtion, using ptraj in AmberTools
    os.system("cpptraj -p OptC4.top -i OptC4_ptraj.in > OptC4_ptraj.out")

    ptrajof = open('OptC4_rmsd.txt', 'r')
    ln = 1
    for line in ptrajof:
      if ln == 3:
        rmsd = float(line[12:21])
      ln += 1
    ptrajof.close()

    print('RMSD is: ', rmsd)
    return rmsd