def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) minpka = opt.minpka maxpka = opt.maxpka if not opt.igb in (1, 2, 5, 7, 8): raise AmberError('-igb must be 1, 2, 5, 7, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Print warning about old format if opt.oldfmt: sys.stderr.write( 'Warning: The old format of the CPIN file can only be used for simulations with temp0=300.0!\n' ' You should use the new format for simulations with temperatures other than 300.0 Kelvins\n' ) # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) elif not getattr(residues, resname).typ == "ph": raise AmberError('%s is not a pH titratable residue!' % resname) titratable_residues.append(resname) else: for resname in residues.titratable_residues: if getattr(residues, resname).typ == "ph": titratable_residues.append(resname) solvent_ions = [ 'WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2' ] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: if getattr(residues, res).pKa < minpka: continue if getattr(residues, res).pKa > maxpka: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum - 1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum - 1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum - 1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum - 1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum - 1]) # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel, opt.oldfmt, "ph") if opt.output is not None: output.close() if solvated: if opt.outparm is None: has_carboxylate = False for res in main_reslist: if res is residues.AS4 or res is residues.GL4 or res is residues.PRN: has_carboxylate = True break if has_carboxylate: sys.stderr.write( 'Warning: Carboxylate residues in explicit solvent ' 'simulations require a modified topology file!\n' 'Use the -op flag to print one.\n') else: changeRadii(parm, 'mbondi2').execute() change(parm, 'RADII', ':AS4,GL4,PRN@OD=,OE=,O1=,O2=', 1.3).execute() parm.overwrite = True parm.write_parm(opt.outparm) else: if opt.outparm is not None: sys.stderr.write( 'A new prmtop is only necessary for explicit solvent ' 'CpHMD/pH-REMD simulations.\n') sys.stderr.write('CPIN generation complete!\n')
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) mineo = opt.mineo maxeo = opt.maxeo if not opt.igb in (1, 2, 5, 7, 8): raise AmberError('-igb must be 1, 2, 5, 7, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) elif not getattr(residues, resname).typ == "redox": raise AmberError( '%s is not a redox-active titratable residue!' % resname) titratable_residues.append(resname) else: for resname in residues.titratable_residues: if getattr(residues, resname).typ == "redox": titratable_residues.append(resname) solvent_ions = [ 'WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2' ] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: if getattr(residues, res).Eo < mineo: continue if getattr(residues, res).Eo > maxeo: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum - 1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) trescnt = 0 for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum - 1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum - 1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum - 1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum - 1]) trescnt += 1 # Prints a warning if the number of titratable residues is larger than 50 if trescnt > 50: sys.stderr.write( 'Warning: Your CEIN file has more than 50 titratable residues! pmemd and sander have a\n' ' default limit of 50 titrable residues, thus this CEIN file can only be used\n' ' if the definitions for this limit are modified at the top of\n' ' $AMBERHOME/src/pmemd/src/constante.F90 or $AMBERHOME/AmberTools/src/sander/constante.F90.\n' ' AMBER needs to be recompilied after these files are modified.\n' ) # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel, False, "redox") if opt.output is not None: output.close() sys.stderr.write('CEIN generation complete!\n')
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) minpka = opt.minpka maxpka = opt.maxpka if not opt.igb in (1, 2, 5, 7, 8): raise AmberError('-igb must be 1, 2, 5, 7, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Print warning about old format if opt.oldfmt: sys.stderr.write('Warning: The old format of the CPIN file can only be used for simulations with temp0=300.0!\n' ' You should use the new format for simulations with temperatures other than 300.0 Kelvins\n') # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) elif not getattr(residues, resname).typ == "ph": raise AmberError('%s is not a pH-active titratable residue!' % resname) titratable_residues.append(resname) else: for resname in residues.titratable_residues: if getattr(residues, resname).typ == "ph": titratable_residues.append(resname) solvent_ions = ['WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2'] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: # @jaimergp: If None values are not filtered out, comparisons # will fail in Py3k. This patch was discussed and approved in # GitLab issue 122 (@vwcruzeiro, @swails) # Error obtained in serial tests in conda-forge builds: # Traceback (most recent call last): # File "/home/conda/amber/bin/cpinutil.py", line 325, in <module> # main(opt) # File "/home/conda/amber/bin/cpinutil.py", line 191, in main # if getattr(residues, res).pKa < minpka: continue # TypeError: '<' not supported between instances of 'NoneType' and 'int' # ./Run.cpin: Program error # make[1]: *** [test.cpinutil] Error 1 if getattr(residues, res).pKa is None: continue # /@jaimergp if getattr(residues, res).pKa < minpka: continue if getattr(residues, res).pKa > maxpka: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) trescnt = 0 for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum-1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum-1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum-1]) trescnt += 1 # Prints a warning if the number of titratable residues is larger than 50 if trescnt > 50: sys.stderr.write('Warning: Your CPIN file has more than 50 titratable residues! pmemd and sander have a\n' ' default limit of 50 titrable residues, thus this CPIN file can only be used\n' ' if the definitions for this limit are modified at the top of\n' ' $AMBERHOME/src/pmemd/src/constantph.F90 or $AMBERHOME/AmberTools/src/sander/constantph.F90.\n' ' AMBER needs to be recompilied after these files are modified.\n') # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel, opt.oldfmt, "ph") if opt.output is not None: output.close() if solvated: if opt.outparm is None: has_carboxylate = False for res in main_reslist: if res is residues.AS4 or res is residues.GL4 or res is residues.PRN: has_carboxylate = True break if has_carboxylate: sys.stderr.write( 'Warning: Carboxylate residues in explicit solvent ' 'simulations require a modified topology file!\n' ' Use the -op flag to print one.\n' ) else: changeRadii(parm, 'mbondi2').execute() change(parm, 'RADII', ':AS4,GL4,PRN@OD=,OE=,O1=,O2=', 1.3).execute() parm.overwrite = True parm.write_parm(opt.outparm) else: if opt.outparm is not None: sys.stderr.write( 'A new prmtop is only necessary for explicit solvent ' 'CpHMD/pH-REMD simulations.\n' ) sys.stderr.write('CPIN generation complete!\n')
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) minpka = opt.minpka maxpka = opt.maxpka if not opt.igb in (1, 2, 5, 7, 8): raise AmberError('-igb must be 1, 2, 5, 7, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Print warning about old format if opt.oldfmt: sys.stderr.write('Warning: The old format of the CPIN file can only be used for simulations with temp0=300.0!\n' ' You should use the new format for simulations with temperatures other than 300.0 Kelvins\n') # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) elif not getattr(residues, resname).typ == "ph": raise AmberError('%s is not a pH-active titratable residue!' % resname) titratable_residues.append(resname) else: for resname in residues.titratable_residues: if getattr(residues, resname).typ == "ph": titratable_residues.append(resname) solvent_ions = ['WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2'] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: if getattr(residues, res).pKa < minpka: continue if getattr(residues, res).pKa > maxpka: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) trescnt = 0 for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum-1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum-1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum-1]) trescnt += 1 # Prints a warning if the number of titratable residues is larger than 50 if trescnt > 50: sys.stderr.write('Warning: Your CPIN file has more than 50 titratable residues! pmemd and sander have a\n' ' default limit of 50 titrable residues, thus this CPIN file can only be used\n' ' if the definitions for this limit are modified at the top of\n' ' $AMBERHOME/src/pmemd/src/constantph.F90 or $AMBERHOME/AmberTools/src/sander/constantph.F90.\n' ' AMBER needs to be recompilied after these files are modified.\n') # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel, opt.oldfmt, "ph") if opt.output is not None: output.close() if solvated: if opt.outparm is None: has_carboxylate = False for res in main_reslist: if res is residues.AS4 or res is residues.GL4 or res is residues.PRN: has_carboxylate = True break if has_carboxylate: sys.stderr.write( 'Warning: Carboxylate residues in explicit solvent ' 'simulations require a modified topology file!\n' ' Use the -op flag to print one.\n' ) else: changeRadii(parm, 'mbondi2').execute() change(parm, 'RADII', ':AS4,GL4,PRN@OD=,OE=,O1=,O2=', 1.3).execute() parm.overwrite = True parm.write_parm(opt.outparm) else: if opt.outparm is not None: sys.stderr.write( 'A new prmtop is only necessary for explicit solvent ' 'CpHMD/pH-REMD simulations.\n' ) sys.stderr.write('CPIN generation complete!\n')
class TestTrajRestartReporter(FileIOTestCase): def setUp(self): super().setUp() self.amber_gas = AmberParm(self.get_fn('ash.parm7'), self.get_fn('ash.rst7')) def test_reporters(self): """ Test NetCDF and ASCII restart and trajectory reporters (no PBC) """ with self.assertRaises(ValueError): NetCDFReporter(self.get_fn('blah', written=True), 1, crds=False) with self.assertRaises(ValueError): MdcrdReporter(self.get_fn('blah', written=True), 1, crds=False) with self.assertRaises(ValueError): MdcrdReporter(self.get_fn('blah', written=True), 1, crds=True, vels=True) system = self.amber_gas.createSystem() integrator = mm.LangevinIntegrator(300 * u.kelvin, 5.0 / u.picoseconds, 1.0 * u.femtoseconds) sim = app.Simulation(self.amber_gas.topology, system, integrator, platform=CPU) sim.context.setPositions(self.amber_gas.positions) sim.reporters.extend([ NetCDFReporter(self.get_fn('traj1.nc', written=True), 10), NetCDFReporter(self.get_fn('traj2.nc', written=True), 10, vels=True), NetCDFReporter(self.get_fn('traj3.nc', written=True), 10, frcs=True), NetCDFReporter(self.get_fn('traj4.nc', written=True), 10, vels=True, frcs=True), NetCDFReporter(self.get_fn('traj5.nc', written=True), 10, crds=False, vels=True), NetCDFReporter(self.get_fn('traj6.nc', written=True), 10, crds=False, frcs=True), NetCDFReporter(self.get_fn('traj7.nc', written=True), 10, crds=False, vels=True, frcs=True), MdcrdReporter(self.get_fn('traj1.mdcrd', written=True), 10), MdcrdReporter(self.get_fn('traj2.mdcrd', written=True), 10, crds=False, vels=True), MdcrdReporter(self.get_fn('traj3.mdcrd', written=True), 10, crds=False, frcs=True), RestartReporter(self.get_fn('restart.ncrst', written=True), 10, write_multiple=True, netcdf=True), RestartReporter(self.get_fn('restart.rst7', written=True), 10), ]) sim.step(500) for reporter in sim.reporters: reporter.finalize() self.assertEqual(len(os.listdir(self._temporary_directory.name)), 61) ntraj = [ NetCDFTraj.open_old(self.get_fn('traj1.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj2.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj3.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj4.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj5.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj6.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj7.nc', written=True)) ] atraj = [ AmberMdcrd(self.get_fn('traj1.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(self.get_fn('traj2.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(self.get_fn('traj3.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), ] for traj in ntraj: self.assertEqual(traj.frame, 50) self.assertEqual(traj.Conventions, 'AMBER') self.assertEqual(traj.ConventionVersion, '1.0') self.assertEqual(traj.application, 'AmberTools') self.assertEqual(traj.program, 'ParmEd') self.assertFalse(traj.hasbox) self.assertTrue(ntraj[0].hascrds) self.assertFalse(ntraj[0].hasvels) self.assertFalse(ntraj[0].hasfrcs) self.assertTrue(ntraj[1].hascrds) self.assertTrue(ntraj[1].hasvels) self.assertFalse(ntraj[1].hasfrcs) self.assertTrue(ntraj[2].hascrds) self.assertFalse(ntraj[2].hasvels) self.assertTrue(ntraj[2].hasfrcs) self.assertTrue(ntraj[3].hascrds) self.assertTrue(ntraj[3].hasvels) self.assertTrue(ntraj[3].hasfrcs) self.assertFalse(ntraj[4].hascrds) self.assertTrue(ntraj[4].hasvels) self.assertFalse(ntraj[4].hasfrcs) self.assertFalse(ntraj[5].hascrds) self.assertFalse(ntraj[5].hasvels) self.assertTrue(ntraj[5].hasfrcs) self.assertFalse(ntraj[6].hascrds) self.assertTrue(ntraj[6].hasvels) self.assertTrue(ntraj[6].hasfrcs) for i in (0, 2, 3, 4, 5, 6): ntraj[i].close() # still need the 2nd for traj in atraj: traj.close() # Now test the NetCDF restart files fn = self.get_fn('restart.ncrst.%d', written=True) for i, j in enumerate(range(10, 501, 10)): ncrst = NetCDFRestart.open_old(fn % j) self.assertEqual(ncrst.coordinates.shape, (1, 25, 3)) self.assertEqual(ncrst.velocities.shape, (1, 25, 3)) np.testing.assert_allclose(ncrst.coordinates[0], ntraj[1].coordinates[i]) np.testing.assert_allclose(ncrst.velocities[0], ntraj[1].velocities[i], rtol=1e-6) # Now test the ASCII restart file f = AmberAsciiRestart(self.get_fn('restart.rst7', written=True), 'r') # Compare to ncrst and make sure it's the same data np.testing.assert_allclose(ncrst.coordinates, f.coordinates, atol=1e-3) np.testing.assert_allclose(ncrst.velocities, f.velocities, rtol=1e-3) # Make sure the EnergyMinimizerReporter does not fail f = StringIO() rep = EnergyMinimizerReporter(f) rep.report(sim, frame=10) rep.finalize() @unittest.skipUnless(HAS_GROMACS, 'Cannot test without GROMACS') def test_reporters_pbc(self): """ Test NetCDF and ASCII restart and trajectory reporters (w/ PBC) """ systemsolv = load_file(self.get_fn('ildn.solv.top'), xyz=self.get_fn('ildn.solv.gro')) system = systemsolv.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=8 * u.angstroms) integrator = mm.LangevinIntegrator(300 * u.kelvin, 5.0 / u.picoseconds, 1.0 * u.femtoseconds) sim = app.Simulation(systemsolv.topology, system, integrator, CPU) sim.context.setPositions(systemsolv.positions) sim.reporters.extend([ NetCDFReporter(self.get_fn('traj.nc', written=True), 1, vels=True, frcs=True), MdcrdReporter(self.get_fn('traj.mdcrd', written=True), 1), RestartReporter(self.get_fn('restart.ncrst', written=True), 1, netcdf=True), RestartReporter(self.get_fn('restart.rst7', written=True), 1), StateDataReporter(self.get_fn('state.o', written=True), 1, volume=True, density=True, systemMass=1) ]) sim.step(5) for reporter in sim.reporters: reporter.finalize() ntraj = NetCDFTraj.open_old(self.get_fn('traj.nc', written=True)) atraj = AmberMdcrd(self.get_fn('traj.mdcrd', written=True), len(systemsolv.atoms), True, mode='r') nrst = NetCDFRestart.open_old( self.get_fn('restart.ncrst', written=True)) arst = AmberAsciiRestart(self.get_fn('restart.rst7', written=True), 'r') self.assertEqual(ntraj.frame, 5) self.assertEqual(atraj.frame, 5) self.assertTrue(ntraj.hasvels) self.assertTrue(ntraj.hasfrcs) for i in range(ntraj.frame): for x1, x2 in zip(ntraj.box[i], atraj.box[i]): self.assertAlmostEqual(x1, x2, places=3) self.assertEqual(len(nrst.box), 6) self.assertEqual(len(arst.box), 6) # Make sure the EnergyMinimizerReporter does not fail f = StringIO() rep = EnergyMinimizerReporter(f, volume=True) rep.report(sim) rep.finalize() def test_private_functions(self): """ Tests private helper functions for OMM reporters """ self.assertEqual(_format_time(7200), (2, 'hr.')) self.assertEqual(_format_time(3600), (60, 'min.')) self.assertEqual(_format_time(40), (40, 'sec.'))
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) mineo = opt.mineo maxeo = opt.maxeo if not opt.igb in (1, 2, 5, 7, 8): raise AmberError('-igb must be 1, 2, 5, 7, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) elif not getattr(residues, resname).typ == "redox": raise AmberError('%s is not a redox-active titratable residue!' % resname) titratable_residues.append(resname) else: for resname in residues.titratable_residues: if getattr(residues, resname).typ == "redox": titratable_residues.append(resname) solvent_ions = ['WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2'] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: if getattr(residues, res).Eo < mineo: continue if getattr(residues, res).Eo > maxeo: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) trescnt = 0 for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum-1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum-1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum-1]) trescnt += 1 # Prints a warning if the number of titratable residues is larger than 50 if trescnt > 50: sys.stderr.write('Warning: Your CEIN file has more than 50 titratable residues! pmemd and sander have a\n' ' default limit of 50 titrable residues, thus this CEIN file can only be used\n' ' if the definitions for this limit are modified at the top of\n' ' $AMBERHOME/src/pmemd/src/constante.F90 or $AMBERHOME/AmberTools/src/sander/constante.F90.\n' ' AMBER needs to be recompilied after these files are modified.\n') # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel, False, "redox") if opt.output is not None: output.close() sys.stderr.write('CEIN generation complete!\n')
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise AmberError('prmtop file (%s) is missing' % opt.prmtop) # Process the arguments that take multiple args resstates = process_arglist(opt.resstates, int) resnums = process_arglist(opt.resnums, int) notresnums = process_arglist(opt.notresnums, int) resnames = process_arglist(opt.resnames, str) notresnames = process_arglist(opt.notresnames, str) minpka = opt.minpka maxpka = opt.maxpka if not opt.igb in (2, 5, 8): raise AmberError('-igb must be 2, 5, or 8!') if resnums is not None and notresnums is not None: raise AmberError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise AmberError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise AmberError('-intdiel must be either 1 or 2 currently') # Set the list of residue names we will be willing to titrate titratable_residues = [] if notresnames is not None: for resname in residues.titratable_residues: if resname in notresnames: continue titratable_residues.append(resname) elif resnames is not None: for resname in resnames: if not resname in residues.titratable_residues: raise AmberError('%s is not a titratable residue!' % resname) titratable_residues.append(resname) else: titratable_residues = residues.titratable_residues[:] solvent_ions = ['WAT', 'Na+', 'Br-', 'Cl-', 'Cs+', 'F-', 'I-', 'K+', 'Li+', 'Mg+', 'Rb+', 'CIO', 'IB', 'MG2'] # Filter titratable residues based on min and max pKa new_reslist = [] for res in titratable_residues: if getattr(residues, res).pKa < minpka: continue if getattr(residues, res).pKa > maxpka: continue new_reslist.append(res) titratable_residues = new_reslist del new_reslist # Make sure we still have a couple residues if len(titratable_residues) == 0: raise AmberError('No titratable residues fit your criteria!') # Load the topology file parm = AmberParm(opt.prmtop) # Replace an un-set notresnums with an empty list so we get __contains__() if notresnums is None: notresnums = [] # If we have a list of residue numbers, check that they're all titratable if resnums is not None: for resnum in resnums: if resnum > parm.ptr('nres'): raise AmberError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise AmberError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: raise AmberError('Residue number %s [%s] is not titratable' % (resnum, resname)) else: # Select every residue except those in notresnums resnums = [] for resnum in range(1, parm.ptr('nres') + 1): if resnum in notresnums: continue resnums.append(resnum) solvated = False first_solvent = 0 if 'WAT' in parm.parm_data['RESIDUE_LABEL']: solvated = True for i, res in enumerate(parm.parm_data['RESIDUE_LABEL']): if res in solvent_ions: first_solvent = parm.parm_data['RESIDUE_POINTER'][i] break main_reslist = TitratableResidueList(system_name=opt.system, solvated=solvated, first_solvent=first_solvent) for resnum in resnums: resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: continue res = getattr(residues, resname) # Filter out termini (make sure the residue in the prmtop has as many # atoms as the titratable residue defined in residues.py) if resnum == parm.ptr('nres'): natoms = (parm.ptr('natom') + 1 - parm.parm_data['RESIDUE_POINTER'][resnum-1]) else: natoms = (parm.parm_data['RESIDUE_POINTER'][resnum] - parm.parm_data['RESIDUE_POINTER'][resnum-1]) if natoms != len(res.atom_list): continue # If we have gotten this far, add it to the list. main_reslist.add_residue(res, resnum, parm.parm_data['RESIDUE_POINTER'][resnum-1]) # Set the states if requested if resstates is not None: main_reslist.set_states(resstates) # Open the output file if opt.output is None: output = sys.stdout else: output = open(opt.output, 'w') main_reslist.write_cpin(output, opt.igb, opt.intdiel) if opt.output is not None: output.close() if solvated: if opt.outparm is None: has_carboxylate = False for res in main_reslist: if res is residues.AS4 or res is residues.GL4: has_carboxylate = True break if has_carboxylate: sys.stderr.write( 'Warning: Carboxylate residues in explicit solvent ' 'simulations require a modified topology file!\n' 'Use the -op flag to print one.\n' ) else: changeRadii(parm, 'mbondi2').execute() change(parm, 'RADII', ':AS4,GL4@OD=,OE=', 1.3).execute() parm.overwrite = True parm.writeParm(opt.outparm) else: if opt.outparm is not None: sys.stderr.write( 'A new prmtop is only necessary for explicit solvent ' 'CpHMD/pH-REMD simulations.\n' )