Skip to content

Commit

Permalink
actually use autoext arg in writePDB
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmkrieger committed Jan 17, 2024
1 parent 55d624d commit 3bb4031
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 4 deletions.
140 changes: 137 additions & 3 deletions prody/atomic/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def getSequence(self, **kwargs):
return seq

def toTEMPyAtoms(self):
"""Returns a BioPy.PDB Atom or Structure object as appropriate"""
"""Returns a :class:`TEMPy.protein.prot_rep_biopy.Atom` or list of them as appropriate"""
try:
from TEMPy.protein.prot_rep_biopy import Atom as TEMPyAtom
except ImportError:
Expand All @@ -269,7 +269,7 @@ def toTEMPyAtoms(self):
return [self.toTEMPyAtom()]

def toTEMPyStructure(self):
"""Returns a BioPy.PDB Atom or Structure object as appropriate"""
"""Returns a :class:`.protein.prot_rep_biopy.Structure` object"""
try:
from TEMPy.protein.prot_rep_biopy import BioPy_Structure
except ImportError:
Expand All @@ -280,4 +280,138 @@ def toTEMPyStructure(self):
def numResidues(self):
"""Returns number of residues."""

return len(set(self._getResindices()))
return len(set(self._getResindices()))


def toBioPythonStructure(self, header=None, **kwargs):
"""Returns a :class:`Bio.PDB.Structure` object
:arg atoms: an object with atom and coordinate data
:type atoms: :class:`.Atomic`
:arg csets: coordinate set indices, default is all coordinate sets
"""
try:
from Bio.PDB.Structure import Structure
from Bio.PDB.StructureBuilder import StructureBuilder
from Bio.PDB.PDBParser import PDBParser
from Bio.PDB.PDBExceptions import PDBConstructionException
except ImportError:
raise ImportError('Bio StructureBuilder could not be imported. '
'Reinstall ProDy or install Biopython '
'to solve the problem.')

origACSI = self.getACSIndex()

csets = kwargs.get('csets', range(self.numCoordsets()))

structure_builder = StructureBuilder()
structure_builder.init_structure(self.getTitle())
if header is not None:
structure_builder.set_header(header)

result = structure_builder.get_structure()
result.is_pqr = (self.getCharges() is not None
and self.getRadii() is not None)

for i in csets:
self.setACSIndex(i)
structure_builder.init_model(i)

current_segid = None
current_chain_id = None
current_residue_id = None

for global_line_counter, atom in enumerate(self):
segid = atom.getSegname()
if current_segid != segid:
current_segid = segid
structure_builder.init_seg(current_segid)

chainid = atom.getChid()
resname = atom.getResname()

if atom.getFlag('hetatm'):
if atom.getFlag('water'):
hetero_flag = 'W'
else:
hetero_flag = 'H'
else:
hetero_flag = ' '

resseq = atom.getResnum()
icode = atom.getIcode()
residue_id = (hetero_flag, resseq, icode)

if current_chain_id != chainid:
current_chain_id = chainid
structure_builder.init_chain(current_chain_id)

current_residue_id = residue_id
current_resname = resname
try:
structure_builder.init_residue(
resname, hetero_flag, resseq, icode
)
except PDBConstructionException as message:
result._handle_PDB_exception(message, global_line_counter)
elif current_residue_id != residue_id or current_resname != resname:
current_residue_id = residue_id
current_resname = resname
try:
structure_builder.init_residue(
resname, hetero_flag, resseq, icode
)
except PDBConstructionException as message:
result._handle_PDB_exception(message, global_line_counter)

name = atom.getName()
coord = atom.getCoords()
altloc = atom.getAltloc()
fullname = atom.getName()
serial_number = atom.getSerial()
element = atom.getElement()

if not result.is_pqr:
# init atom with pdb fields
try:
structure_builder.init_atom(
name,
coord,
atom.getBeta(),
atom.getOccupancy(),
altloc,
fullname,
serial_number,
element,
)
except PDBConstructionException as message:
result._handle_PDB_exception(message, global_line_counter)
else:
try:
structure_builder.init_atom(
name,
coord,
atom.getCharge(),
atom.getRadius(),
altloc,
fullname,
serial_number,
element,
atom.getCharge(),
atom.getRadius(),
result.is_pqr,
)
except PDBConstructionException as message:
result._handle_PDB_exception(message, global_line_counter)

if atom.getAnisou() is not None:
structure_builder.set_anisou(atom.getAnisou())

if atom.getAnistd() is not None:
structure_builder.set_siguij(atom.getAnistd())

self.setACSIndex(origACSI)

return result
2 changes: 1 addition & 1 deletion prody/proteins/pdbfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ def writePDB(filename, atoms, csets=None, autoext=True, **kwargs):
"""

if not (filename.lower().endswith('.pdb') or filename.lower().endswith('.pdb.gz') or
filename.lower().endswith('.ent') or filename.lower().endswith('.ent.gz')):
filename.lower().endswith('.ent') or filename.lower().endswith('.ent.gz')) and autoext:
filename += '.pdb'
out = openFile(filename, 'wt')
writePDBStream(out, atoms, csets, **kwargs)
Expand Down

0 comments on commit 3bb4031

Please sign in to comment.