From 3bb4031b9a6f319d978161aadd34e33e7e954df1 Mon Sep 17 00:00:00 2001 From: James Krieger Date: Wed, 17 Jan 2024 18:40:11 +0100 Subject: [PATCH] actually use autoext arg in writePDB --- prody/atomic/atomic.py | 140 +++++++++++++++++++++++++++++++++++++- prody/proteins/pdbfile.py | 2 +- 2 files changed, 138 insertions(+), 4 deletions(-) diff --git a/prody/atomic/atomic.py b/prody/atomic/atomic.py index be692ef1d..9f4a3cedf 100644 --- a/prody/atomic/atomic.py +++ b/prody/atomic/atomic.py @@ -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: @@ -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: @@ -280,4 +280,138 @@ def toTEMPyStructure(self): def numResidues(self): """Returns number of residues.""" - return len(set(self._getResindices())) \ No newline at end of file + 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 \ No newline at end of file diff --git a/prody/proteins/pdbfile.py b/prody/proteins/pdbfile.py index 13c11719b..127aae19a 100644 --- a/prody/proteins/pdbfile.py +++ b/prody/proteins/pdbfile.py @@ -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)