class AmberSystem(object): """ An Amber system with a topology file and input coordinate file """ # ============================== def __init__(self, prmtop, inpcrd): # Only load the inpcrd file if it actually exists. # if os.path.exists(inpcrd): self.prmtop = AmberParm(prmtop, inpcrd) # else: self.prmtop = AmberParm(prmtop) self.prmtop = AmberParm(prmtop) self.inpcrd = inpcrd # ============================== def periodic(self): """ Returns whether or not a topology file is set up for PBC """ # PBCs are indicated by a non-zero IFBOX pointer return bool(self.prmtop.ptr('ifbox')) # ============================== def query_radii(self): """ Returns the radii set in the topology file """ return self.prmtop.parm_data['RADIUS_SET'][0] # ============================== def set_radii(self, igb=0, radius_set='mbondi'): """ Sets the radius set to a new one using parmed """ # If someone sets an igb, change to the appropriate radius set if igb: if igb == 1: radius_set = 'mbondi' elif igb == 2: radius_set = 'mbondi2' elif igb == 5: radius_set = 'mbondi2' elif igb == 7: radius_set = 'bondi' elif igb == 8: radius_set = 'mbondi3' if not radius_set in ['mbondi', 'mbondi2', 'mbondi3', 'bondi', 'amber6']: raise InputError('Bad radius set! Choose from ' + 'mbondi, mbondi2, mbondi3, bondi, and amber6') parmed = which('parmed.py') change_str = ("setOverwrite True\n" + "changeRadii %s\n" % radius_set + "parmout %s\n" % self.prmtop + "go\n") process = Popen([parmed, '-q', '-n', str(self.prmtop)], stdin=PIPE, stderr=PIPE, stdout=PIPE) (output, error) = process.communicate(change_str) if process.wait(): raise ProgramError('parmed.py failed to change radii!') # Reload our topology file now that we've changed radius sets self.prmtop = AmberParm(str(self.prmtop))
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise CpinInputError('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 CpinInputError('-igb must be 2, 5, or 8!') if resnums is not None and notresnums is not None: raise CpinInputError('Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise CpinInputError('Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise CpinInputError('-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 CpinInputError('%s is not a titratable residue!' % resname) titratable_residues.append(resname) else: titratable_residues = residues.titratable_residues[:] print str(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 CpinInputError('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 CpinInputError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise CpinInputError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum-1] if not resname in titratable_residues: raise CpinInputError('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: print resname 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')-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', 0) 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: print >> sys.stderr, "Warning: Carboxylate residues in explicit ", print >> sys.stderr, "solvent simulations require a modified" print >> sys.stderr, "topology file! Use the -op flag to print one." 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: print >> sys.stderr, "A new prmtop is only necessary for explicit ", print >> sys.stderr, "solvent CpHMD simulations." sys.stderr.write('CPIN generation complete!\n')
class AmberSystem(object): """ An Amber system with a topology file and input coordinate file """ # ============================== def __init__(self, prmtop, inpcrd): # Only load the inpcrd file if it actually exists. # if os.path.exists(inpcrd): self.prmtop = AmberParm(prmtop, inpcrd) # else: self.prmtop = AmberParm(prmtop) self.prmtop = AmberParm(prmtop) self.inpcrd = inpcrd # ============================== def periodic(self): """ Returns whether or not a topology file is set up for PBC """ # PBCs are indicated by a non-zero IFBOX pointer return bool(self.prmtop.ptr('ifbox')) # ============================== def query_radii(self): """ Returns the radii set in the topology file """ return self.prmtop.parm_data['RADIUS_SET'][0] # ============================== def set_radii(self, igb=0, radius_set='mbondi'): """ Sets the radius set to a new one using parmed """ # If someone sets an igb, change to the appropriate radius set if igb: if igb == 1: radius_set = 'mbondi' elif igb == 2: radius_set = 'mbondi2' elif igb == 5: radius_set = 'mbondi2' elif igb == 7: radius_set = 'bondi' elif igb == 8: radius_set = 'mbondi3' if not radius_set in [ 'mbondi', 'mbondi2', 'mbondi3', 'bondi', 'amber6' ]: raise InputError('Bad radius set! Choose from ' + 'mbondi, mbondi2, mbondi3, bondi, and amber6') parmed = which('parmed.py') change_str = ("setOverwrite True\n" + "changeRadii %s\n" % radius_set + "parmout %s\n" % self.prmtop + "go\n") process = Popen( [parmed, '-q', '-n', str(self.prmtop)], stdin=PIPE, stderr=PIPE, stdout=PIPE) (output, error) = process.communicate(change_str) if process.wait(): raise ProgramError('parmed.py failed to change radii!') # Reload our topology file now that we've changed radius sets self.prmtop = AmberParm(str(self.prmtop))
def natom(topfile): parm = AmberParm(topfile) return parm.ptr("NATOM")
def resnum(topfile): parm = AmberParm(topfile) return parm.ptr("NRES")
def main(opt): # Check all of the arguments if not os.path.exists(opt.prmtop): raise CpinInputError('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 CpinInputError('-igb must be 2, 5, or 8!') if resnums is not None and notresnums is not None: raise CpinInputError( 'Cannot specify -resnums and -notresnums together') if resnames is not None and notresnames is not None: raise CpinInputError( 'Cannot specify -resnames and -notresnames together') if opt.intdiel != 1 and opt.intdiel != 2: raise CpinInputError('-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 CpinInputError('%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 CpinInputError('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 CpinInputError('%s only has %d residues. (%d chosen)' % (parm, parm.ptr('nres'), resnum)) if resnum <= 0: raise CpinInputError('Cannot select negative residue numbers.') resname = parm.parm_data['RESIDUE_LABEL'][resnum - 1] if not resname in titratable_residues: raise CpinInputError( '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') - 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', 0) 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: print >> sys.stderr, "Warning: Carboxylate residues in explicit ", print >> sys.stderr, "solvent simulations require a modified" print >> sys.stderr, "topology file! Use the -op flag to print one." 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: print >> sys.stderr, "A new prmtop is only necessary for explicit ", print >> sys.stderr, "solvent CpHMD simulations." sys.stderr.write('CPIN generation complete!\n')