Commit 37811976 authored by peastman's avatar peastman
Browse files

Merge pull request #813 from peastman/ids

Topology records PDB ids for chains, residues, and atoms
parents 37ab709f e84f0727
...@@ -88,12 +88,12 @@ class PDBFile(object): ...@@ -88,12 +88,12 @@ class PDBFile(object):
atomByNumber = {} atomByNumber = {}
for chain in pdb.iter_chains(): for chain in pdb.iter_chains():
c = top.addChain() c = top.addChain(chain.chain_id)
for residue in chain.iter_residues(): for residue in chain.iter_residues():
resName = residue.get_name() resName = residue.get_name()
if resName in PDBFile._residueNameReplacements: if resName in PDBFile._residueNameReplacements:
resName = PDBFile._residueNameReplacements[resName] resName = PDBFile._residueNameReplacements[resName]
r = top.addResidue(resName, c) r = top.addResidue(resName, c, str(residue.number))
if resName in PDBFile._atomNameReplacements: if resName in PDBFile._atomNameReplacements:
atomReplacements = PDBFile._atomNameReplacements[resName] atomReplacements = PDBFile._atomNameReplacements[resName]
else: else:
...@@ -129,7 +129,7 @@ class PDBFile(object): ...@@ -129,7 +129,7 @@ class PDBFile(object):
element = elem.get_by_symbol(atomName[0]) element = elem.get_by_symbol(atomName[0])
except KeyError: except KeyError:
pass pass
newAtom = top.addAtom(atomName, element, r) newAtom = top.addAtom(atomName, element, r, str(atom.serial_number))
atomByNumber[atom.serial_number] = newAtom atomByNumber[atom.serial_number] = newAtom
self._positions = [] self._positions = []
for model in pdb.iter_models(True): for model in pdb.iter_models(True):
......
...@@ -78,6 +78,7 @@ class PDBxFile(object): ...@@ -78,6 +78,7 @@ class PDBxFile(object):
atomIdCol = atomData.getAttributeIndex('id') atomIdCol = atomData.getAttributeIndex('id')
resNameCol = atomData.getAttributeIndex('label_comp_id') resNameCol = atomData.getAttributeIndex('label_comp_id')
resIdCol = atomData.getAttributeIndex('label_seq_id') resIdCol = atomData.getAttributeIndex('label_seq_id')
resNumCol = atomData.getAttributeIndex('auth_seq_id')
asymIdCol = atomData.getAttributeIndex('label_asym_id') asymIdCol = atomData.getAttributeIndex('label_asym_id')
chainIdCol = atomData.getAttributeIndex('label_entity_id') chainIdCol = atomData.getAttributeIndex('label_entity_id')
elementCol = atomData.getAttributeIndex('type_symbol') elementCol = atomData.getAttributeIndex('type_symbol')
...@@ -107,13 +108,13 @@ class PDBxFile(object): ...@@ -107,13 +108,13 @@ class PDBxFile(object):
if lastChainId != row[chainIdCol]: if lastChainId != row[chainIdCol]:
# The start of a new chain. # The start of a new chain.
chain = top.addChain() chain = top.addChain(row[chainIdCol])
lastChainId = row[chainIdCol] lastChainId = row[chainIdCol]
lastResId = None lastResId = None
lastAsymId = None lastAsymId = None
if lastResId != row[resIdCol] or lastAsymId != row[asymIdCol]: if lastResId != row[resIdCol] or lastAsymId != row[asymIdCol]:
# The start of a new residue. # The start of a new residue.
res = top.addResidue(row[resNameCol], chain) res = top.addResidue(row[resNameCol], chain, None if resNumCol == -1 else row[resNumCol])
lastResId = row[resIdCol] lastResId = row[resIdCol]
if lastResId == '.': if lastResId == '.':
lastResId = None lastResId = None
...@@ -123,7 +124,7 @@ class PDBxFile(object): ...@@ -123,7 +124,7 @@ class PDBxFile(object):
element = elem.get_by_symbol(row[elementCol]) element = elem.get_by_symbol(row[elementCol])
except KeyError: except KeyError:
pass pass
atom = top.addAtom(row[atomNameCol], element, res) atom = top.addAtom(row[atomNameCol], element, res, row[atomIdCol])
atomTable[atomKey] = atom atomTable[atomKey] = atom
else: else:
# This row defines coordinates for an existing atom in one of the later models. # This row defines coordinates for an existing atom in one of the later models.
......
...@@ -59,38 +59,51 @@ class Topology(object): ...@@ -59,38 +59,51 @@ class Topology(object):
self._bonds = [] self._bonds = []
self._periodicBoxVectors = None self._periodicBoxVectors = None
def addChain(self): def addChain(self, id=None):
"""Create a new Chain and add it to the Topology. """Create a new Chain and add it to the Topology.
Parameters:
- id (string=None) An optional identifier for the chain. If this is omitted, an id
is generated based on the chain index.
Returns: the newly created Chain Returns: the newly created Chain
""" """
chain = Chain(len(self._chains), self) if id is None:
id = str(len(self._chains)+1)
chain = Chain(len(self._chains), self, id)
self._chains.append(chain) self._chains.append(chain)
return chain return chain
def addResidue(self, name, chain): def addResidue(self, name, chain, id=None):
"""Create a new Residue and add it to the Topology. """Create a new Residue and add it to the Topology.
Parameters: Parameters:
- name (string) The name of the residue to add - name (string) The name of the residue to add
- chain (Chain) The Chain to add it to - chain (Chain) The Chain to add it to
- id (string=None) An optional identifier for the residue. If this is omitted, an id
is generated based on the residue index.
Returns: the newly created Residue Returns: the newly created Residue
""" """
residue = Residue(name, self._numResidues, chain) if id is None:
id = str(self._numResidues+1)
residue = Residue(name, self._numResidues, chain, id)
self._numResidues += 1 self._numResidues += 1
chain._residues.append(residue) chain._residues.append(residue)
return residue return residue
def addAtom(self, name, element, residue): def addAtom(self, name, element, residue, id=None):
"""Create a new Atom and add it to the Topology. """Create a new Atom and add it to the Topology.
Parameters: Parameters:
- name (string) The name of the atom to add - name (string) The name of the atom to add
- element (Element) The element of the atom to add - element (Element) The element of the atom to add
- residue (Residue) The Residue to add it to - residue (Residue) The Residue to add it to
- id (string=None) An optional identifier for the atom. If this is omitted, an id
is generated based on the atom index.
Returns: the newly created Atom Returns: the newly created Atom
""" """
atom = Atom(name, element, self._numAtoms, residue) if id is None:
id = str(self._numAtoms+1)
atom = Atom(name, element, self._numAtoms, residue, id)
self._numAtoms += 1 self._numAtoms += 1
residue._atoms.append(atom) residue._atoms.append(atom)
return atom return atom
...@@ -258,12 +271,14 @@ class Topology(object): ...@@ -258,12 +271,14 @@ class Topology(object):
class Chain(object): class Chain(object):
"""A Chain object represents a chain within a Topology.""" """A Chain object represents a chain within a Topology."""
def __init__(self, index, topology): def __init__(self, index, topology, id):
"""Construct a new Chain. You should call addChain() on the Topology instead of calling this directly.""" """Construct a new Chain. You should call addChain() on the Topology instead of calling this directly."""
## The index of the Chain within its Topology ## The index of the Chain within its Topology
self.index = index self.index = index
## The Topology this Chain belongs to ## The Topology this Chain belongs to
self.topology = topology self.topology = topology
## A user defined identifier for this Chain
self.id = id
self._residues = [] self._residues = []
def residues(self): def residues(self):
...@@ -278,7 +293,7 @@ class Chain(object): ...@@ -278,7 +293,7 @@ class Chain(object):
class Residue(object): class Residue(object):
"""A Residue object represents a residue within a Topology.""" """A Residue object represents a residue within a Topology."""
def __init__(self, name, index, chain): def __init__(self, name, index, chain, id):
"""Construct a new Residue. You should call addResidue() on the Topology instead of calling this directly.""" """Construct a new Residue. You should call addResidue() on the Topology instead of calling this directly."""
## The name of the Residue ## The name of the Residue
self.name = name self.name = name
...@@ -286,6 +301,8 @@ class Residue(object): ...@@ -286,6 +301,8 @@ class Residue(object):
self.index = index self.index = index
## The Chain this Residue belongs to ## The Chain this Residue belongs to
self.chain = chain self.chain = chain
## A user defined identifier for this Residue
self.id = id
self._atoms = [] self._atoms = []
def atoms(self): def atoms(self):
...@@ -295,7 +312,7 @@ class Residue(object): ...@@ -295,7 +312,7 @@ class Residue(object):
class Atom(object): class Atom(object):
"""An Atom object represents a residue within a Topology.""" """An Atom object represents a residue within a Topology."""
def __init__(self, name, element, index, residue): def __init__(self, name, element, index, residue, id):
"""Construct a new Atom. You should call addAtom() on the Topology instead of calling this directly.""" """Construct a new Atom. You should call addAtom() on the Topology instead of calling this directly."""
## The name of the Atom ## The name of the Atom
self.name = name self.name = name
...@@ -305,4 +322,6 @@ class Atom(object): ...@@ -305,4 +322,6 @@ class Atom(object):
self.index = index self.index = index
## The Residue this Atom belongs to ## The Residue this Atom belongs to
self.residue = residue self.residue = residue
## A user defined identifier for this Atom
self.id = id
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment