Commit b0ecf372 authored by peastman's avatar peastman
Browse files

Merge pull request #843 from peastman/ids

Added keepIds flag to PDBFile.writeFile()
parents 410674c0 6dc44c0e
...@@ -229,16 +229,19 @@ class PDBFile(object): ...@@ -229,16 +229,19 @@ class PDBFile(object):
map[atom.attrib[id]] = name map[atom.attrib[id]] = name
@staticmethod @staticmethod
def writeFile(topology, positions, file=sys.stdout, modelIndex=None): def writeFile(topology, positions, file=sys.stdout, keepIds=False):
"""Write a PDB file containing a single model. """Write a PDB file containing a single model.
Parameters: Parameters:
- topology (Topology) The Topology defining the model to write - topology (Topology) The Topology defining the model to write
- positions (list) The list of atomic positions to write - positions (list) The list of atomic positions to write
- file (file=stdout) A file to write to - file (file=stdout) A file to write to
- keepIds (bool=False) If True, keep the residue and chain IDs specified in the Topology rather than generating
new ones. Warning: It is up to the caller to make sure these are valid IDs that satisfy the requirements of
the PDB format. Otherwise, the output file will be invalid.
""" """
PDBFile.writeHeader(topology, file) PDBFile.writeHeader(topology, file)
PDBFile.writeModel(topology, positions, file) PDBFile.writeModel(topology, positions, file, keepIds=keepIds)
PDBFile.writeFooter(topology, file) PDBFile.writeFooter(topology, file)
@staticmethod @staticmethod
...@@ -262,7 +265,7 @@ class PDBFile(object): ...@@ -262,7 +265,7 @@ class PDBFile(object):
print >>file, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f P 1 1 " % (a_length, b_length, c_length, alpha, beta, gamma) print >>file, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f P 1 1 " % (a_length, b_length, c_length, alpha, beta, gamma)
@staticmethod @staticmethod
def writeModel(topology, positions, file=sys.stdout, modelIndex=None): def writeModel(topology, positions, file=sys.stdout, modelIndex=None, keepIds=False):
"""Write out a model to a PDB file. """Write out a model to a PDB file.
Parameters: Parameters:
...@@ -270,6 +273,9 @@ class PDBFile(object): ...@@ -270,6 +273,9 @@ class PDBFile(object):
- positions (list) The list of atomic positions to write - positions (list) The list of atomic positions to write
- file (file=stdout) A file to write the model to - file (file=stdout) A file to write the model to
- modelIndex (int=None) If not None, the model will be surrounded by MODEL/ENDMDL records with this index - modelIndex (int=None) If not None, the model will be surrounded by MODEL/ENDMDL records with this index
- keepIds (bool=False) If True, keep the residue and chain IDs specified in the Topology rather than generating
new ones. Warning: It is up to the caller to make sure these are valid IDs that satisfy the requirements of
the PDB format. Otherwise, the output file will be invalid.
""" """
if len(list(topology.atoms())) != len(positions): if len(list(topology.atoms())) != len(positions):
raise ValueError('The number of positions must match the number of atoms') raise ValueError('The number of positions must match the number of atoms')
...@@ -284,13 +290,20 @@ class PDBFile(object): ...@@ -284,13 +290,20 @@ class PDBFile(object):
if modelIndex is not None: if modelIndex is not None:
print >>file, "MODEL %4d" % modelIndex print >>file, "MODEL %4d" % modelIndex
for (chainIndex, chain) in enumerate(topology.chains()): for (chainIndex, chain) in enumerate(topology.chains()):
chainName = chr(ord('A')+chainIndex%26) if keepIds:
chainName = chain.id
else:
chainName = chr(ord('A')+chainIndex%26)
residues = list(chain.residues()) residues = list(chain.residues())
for (resIndex, res) in enumerate(residues): for (resIndex, res) in enumerate(residues):
if len(res.name) > 3: if len(res.name) > 3:
resName = res.name[:3] resName = res.name[:3]
else: else:
resName = res.name resName = res.name
if keepIds:
resId = res.id
else:
resId = "%4d" % ((resIndex+1)%10000)
for atom in res.atoms(): for atom in res.atoms():
if len(atom.name) < 4 and atom.name[:1].isalpha() and (atom.element is None or len(atom.element.symbol) < 2): if len(atom.name) < 4 and atom.name[:1].isalpha() and (atom.element is None or len(atom.element.symbol) < 2):
atomName = ' '+atom.name atomName = ' '+atom.name
...@@ -303,16 +316,15 @@ class PDBFile(object): ...@@ -303,16 +316,15 @@ class PDBFile(object):
symbol = atom.element.symbol symbol = atom.element.symbol
else: else:
symbol = ' ' symbol = ' '
line = "ATOM %5d %-4s %3s %s%4d %s%s%s 1.00 0.00 %2s " % ( line = "ATOM %5d %-4s %3s %s%4s %s%s%s 1.00 0.00 %2s " % (
atomIndex%100000, atomName, resName, chainName, atomIndex%100000, atomName, resName, chainName, resId, _format_83(coords[0]),
(resIndex+1)%10000, _format_83(coords[0]),
_format_83(coords[1]), _format_83(coords[2]), symbol) _format_83(coords[1]), _format_83(coords[2]), symbol)
assert len(line) == 80, 'Fixed width overflow detected' assert len(line) == 80, 'Fixed width overflow detected'
print >>file, line print >>file, line
posIndex += 1 posIndex += 1
atomIndex += 1 atomIndex += 1
if resIndex == len(residues)-1: if resIndex == len(residues)-1:
print >>file, "TER %5d %3s %s%4d" % (atomIndex, resName, chainName, resIndex+1) print >>file, "TER %5d %3s %s%4s" % (atomIndex, resName, chainName, resId)
atomIndex += 1 atomIndex += 1
if modelIndex is not None: if modelIndex is not None:
print >>file, "ENDMDL" print >>file, "ENDMDL"
......
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