def __init__(self, file): """Load a prmtop file.""" top = Topology() ## The Topology read from the prmtop file self.topology = top # Load the prmtop file prmtop = amber_file_parser.PrmtopLoader(file) self._prmtop = prmtop # Add atoms to the topology PDBFile._loadNameReplacementTables() lastResidue = None c = top.addChain() for index in range(prmtop.getNumAtoms()): resNumber = prmtop.getResidueNumber(index) if resNumber != lastResidue: lastResidue = resNumber resName = prmtop.getResidueLabel(iAtom=index).strip() if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[resName] else: atomReplacements = {} atomName = prmtop.getAtomName(index).strip() if atomName in atomReplacements: atomName = atomReplacements[atomName] # Try to guess the element. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None top.addAtom(atomName, element, r) # Add bonds to the topology atoms = list(top.atoms()) for bond in prmtop.getBondsWithH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) for bond in prmtop.getBondsNoH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) # Set the periodic box size. if prmtop.getIfBox(): top.setUnitCellDimensions(tuple(x.value_in_unit(unit.nanometer) for x in prmtop.getBoxBetaAndDimensions()[1:4])*unit.nanometer)
def __init__(self, file, periodicBoxVectors=None, unitCellDimensions=None, includeDir=None, defines=None): """Load a top file. Parameters ---------- file : str the name of the file to load periodicBoxVectors : tuple of Vec3=None the vectors defining the periodic box unitCellDimensions : Vec3=None the dimensions of the crystallographic unit cell. For non-rectangular unit cells, specify periodicBoxVectors instead. includeDir : string=None A directory in which to look for other files included from the top file. If not specified, we will attempt to locate a gromacs installation on your system. When gromacs is installed in /usr/local, this will resolve to /usr/local/gromacs/share/gromacs/top defines : dict={} preprocessor definitions that should be predefined when parsing the file """ if includeDir is None: includeDir = _defaultGromacsIncludeDir() self._includeDirs = (os.path.dirname(file), includeDir) # Most of the gromacs water itp files for different forcefields, # unless the preprocessor #define FLEXIBLE is given, don't define # bonds between the water hydrogen and oxygens, but only give the # constraint distances and exclusions. self._defines = OrderedDict() self._defines['FLEXIBLE'] = True self._genpairs = True if defines is not None: for define, value in defines.iteritems(): self._defines[define] = value # Parse the file. self._currentCategory = None self._ifStack = [] self._elseStack = [] self._moleculeTypes = {} self._molecules = [] self._currentMoleculeType = None self._atomTypes = {} self._bondTypes= {} self._angleTypes = {} self._dihedralTypes = {} self._implicitTypes = {} self._pairTypes = {} self._cmapTypes = {} self._processFile(file) # Create the Topology from it. top = Topology() ## The Topology read from the prmtop file self.topology = top if periodicBoxVectors is not None: if unitCellDimensions is not None: raise ValueError("specify either periodicBoxVectors or unitCellDimensions, but not both") top.setPeriodicBoxVectors(periodicBoxVectors) else: top.setUnitCellDimensions(unitCellDimensions) PDBFile._loadNameReplacementTables() for moleculeName, moleculeCount in self._molecules: if moleculeName not in self._moleculeTypes: raise ValueError("Unknown molecule type: "+moleculeName) moleculeType = self._moleculeTypes[moleculeName] if moleculeCount > 0 and moleculeType.has_virtual_sites: raise ValueError('Virtual sites not yet supported by Gromacs parsers') # Create the specified number of molecules of this type. for i in range(moleculeCount): atoms = [] lastResidue = None c = top.addChain() for index, fields in enumerate(moleculeType.atoms): resNumber = fields[2] if resNumber != lastResidue: lastResidue = resNumber resName = fields[3] if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[resName] else: atomReplacements = {} atomName = fields[4] if atomName in atomReplacements: atomName = atomReplacements[atomName] # Try to guess the element. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None atoms.append(top.addAtom(atomName, element, r)) # Add bonds to the topology for fields in moleculeType.bonds: top.addBond(atoms[int(fields[0])-1], atoms[int(fields[1])-1])
def __init__(self, file): """Load a prmtop file.""" top = Topology() ## The Topology read from the prmtop file self.topology = top self.elements = [] # Load the prmtop file prmtop = amber_file_parser.PrmtopLoader(file) self._prmtop = prmtop # Add atoms to the topology PDBFile._loadNameReplacementTables() lastResidue = None c = top.addChain() for index in range(prmtop.getNumAtoms()): resNumber = prmtop.getResidueNumber(index) if resNumber != lastResidue: lastResidue = resNumber resName = prmtop.getResidueLabel(iAtom=index).strip() if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[resName] else: atomReplacements = {} atomName = prmtop.getAtomName(index).strip() if atomName in atomReplacements: atomName = atomReplacements[atomName] # Get the element from the prmtop file if available if prmtop.has_atomic_number: try: element = elem.Element.getByAtomicNumber( int(prmtop._raw_data['ATOMIC_NUMBER'][index])) except KeyError: element = None else: # Try to guess the element from the atom name. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium elif upper.startswith('ZN'): element = elem.zinc else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None top.addAtom(atomName, element, r) self.elements.append(element) # Add bonds to the topology atoms = list(top.atoms()) for bond in prmtop.getBondsWithH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) for bond in prmtop.getBondsNoH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) # Set the periodic box size. if prmtop.getIfBox(): top.setUnitCellDimensions( tuple( x.value_in_unit(unit.nanometer) for x in prmtop.getBoxBetaAndDimensions()[1:4]) * unit.nanometer)
def __init__(self, file): """Load a prmtop file.""" ## The Topology read from the prmtop file self.topology = top = Topology() self.elements = [] # Load the prmtop file prmtop = amber_file_parser.PrmtopLoader(file) self._prmtop = prmtop # Add atoms to the topology PDBFile._loadNameReplacementTables() lastResidue = None c = top.addChain() for index in range(prmtop.getNumAtoms()): resNumber = prmtop.getResidueNumber(index) if resNumber != lastResidue: lastResidue = resNumber resName = prmtop.getResidueLabel(iAtom=index).strip() if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[resName] else: atomReplacements = {} atomName = prmtop.getAtomName(index).strip() if atomName in atomReplacements: atomName = atomReplacements[atomName] # Get the element from the prmtop file if available if prmtop.has_atomic_number: try: element = elem.Element.getByAtomicNumber(int(prmtop._raw_data['ATOMIC_NUMBER'][index])) except KeyError: element = None else: # Try to guess the element from the atom name. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium elif upper.startswith('ZN'): element = elem.zinc else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None top.addAtom(atomName, element, r) self.elements.append(element) # Add bonds to the topology atoms = list(top.atoms()) for bond in prmtop.getBondsWithH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) for bond in prmtop.getBondsNoH(): top.addBond(atoms[bond[0]], atoms[bond[1]]) # Set the periodic box size. if prmtop.getIfBox(): box = prmtop.getBoxBetaAndDimensions() top.setPeriodicBoxVectors(computePeriodicBoxVectors(*(box[1:4] + box[0:1]*3)))
def __init__(self, file, unitCellDimensions=None, includeDir='/usr/local/gromacs/share/gromacs/top', defines={}): """Load a top file. Parameters: - file (string) the name of the file to load - unitCellDimensions (Vec3=None) the dimensions of the crystallographic unit cell - includeDir (string=/usr/local/gromacs/share/gromacs/top) a directory in which to look for other files included from the top file - defines (map={}) preprocessor definitions that should be predefined when parsing the file """ self._includeDirs = (os.path.dirname(file), includeDir) self._defines = defines # Parse the file. self._currentCategory = None self._ifStack = [] self._moleculeTypes = {} self._molecules = [] self._currentMoleculeType = None self._atomTypes = {} self._bondTypes = {} self._angleTypes = {} self._dihedralTypes = {} self._implicitTypes = {} self._pairTypes = {} self._cmapTypes = {} self._processFile(file) # Create the Topology from it. top = Topology() ## The Topology read from the prmtop file self.topology = top top.setUnitCellDimensions(unitCellDimensions) PDBFile._loadNameReplacementTables() for moleculeName, moleculeCount in self._molecules: if moleculeName not in self._moleculeTypes: raise ValueError("Unknown molecule type: " + moleculeName) moleculeType = self._moleculeTypes[moleculeName] # Create the specified number of molecules of this type. for i in range(moleculeCount): atoms = [] lastResidue = None c = top.addChain() for index, fields in enumerate(moleculeType.atoms): resNumber = fields[2] if resNumber != lastResidue: lastResidue = resNumber resName = fields[3] if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[ resName] else: atomReplacements = {} atomName = fields[4] if atomName in atomReplacements: atomName = atomReplacements[atomName] # Try to guess the element. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None atoms.append(top.addAtom(atomName, element, r)) # Add bonds to the topology for fields in moleculeType.bonds: top.addBond(atoms[int(fields[0]) - 1], atoms[int(fields[1]) - 1])
def __init__(self, file, unitCellDimensions=None, includeDir='/usr/local/gromacs/share/gromacs/top', defines={}): """Load a top file. Parameters: - file (string) the name of the file to load - unitCellDimensions (Vec3=None) the dimensions of the crystallographic unit cell - includeDir (string=/usr/local/gromacs/share/gromacs/top) a directory in which to look for other files included from the top file - defines (map={}) preprocessor definitions that should be predefined when parsing the file """ self._includeDirs = (os.path.dirname(file), includeDir) self._defines = defines # Parse the file. self._currentCategory = None self._ifStack = [] self._moleculeTypes = {} self._molecules = [] self._currentMoleculeType = None self._atomTypes = {} self._bondTypes= {} self._angleTypes = {} self._dihedralTypes = {} self._implicitTypes = {} self._pairTypes = {} self._cmapTypes = {} self._processFile(file) # Create the Topology from it. top = Topology() ## The Topology read from the prmtop file self.topology = top top.setUnitCellDimensions(unitCellDimensions) PDBFile._loadNameReplacementTables() for moleculeName, moleculeCount in self._molecules: if moleculeName not in self._moleculeTypes: raise ValueError("Unknown molecule type: "+moleculeName) moleculeType = self._moleculeTypes[moleculeName] # Create the specified number of molecules of this type. for i in range(moleculeCount): atoms = [] lastResidue = None c = top.addChain() for index, fields in enumerate(moleculeType.atoms): resNumber = fields[2] if resNumber != lastResidue: lastResidue = resNumber resName = fields[3] if resName in PDBFile._residueNameReplacements: resName = PDBFile._residueNameReplacements[resName] r = top.addResidue(resName, c) if resName in PDBFile._atomNameReplacements: atomReplacements = PDBFile._atomNameReplacements[resName] else: atomReplacements = {} atomName = fields[4] if atomName in atomReplacements: atomName = atomReplacements[atomName] # Try to guess the element. upper = atomName.upper() if upper.startswith('CL'): element = elem.chlorine elif upper.startswith('NA'): element = elem.sodium elif upper.startswith('MG'): element = elem.magnesium else: try: element = elem.get_by_symbol(atomName[0]) except KeyError: element = None atoms.append(top.addAtom(atomName, element, r)) # Add bonds to the topology for fields in moleculeType.bonds: top.addBond(atoms[int(fields[0])-1], atoms[int(fields[1])-1])