Created
November 22, 2022 13:46
-
-
Save hemahecodes/afe1e5f93294e54a047a8e6330c818c8 to your computer and use it in GitHub Desktop.
Parse PDB Files to get Atom information.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class PDBAtom: | |
""" | |
It defines a PDB Atom object. | |
""" | |
def __init__(self, line): | |
self.type = self._get_type(line) # from columns 1-7 | |
self.ser_num = self._get_serial_number(line) # from columns 7-11 | |
self.at_name = self._get_atom_name(line) # from columns 13-16 | |
self.alt_loc = self._get_alternate_location_indicator(line) | |
self.res = self._get_residue(line) # from columns 18-20 | |
self.chain = self._get_chain(line) # column 22 | |
self.res_seqnum = self._get_residue_sequence_number(line) | |
self.insertion_rescode = self._get_code_for_insertion_residues(line) | |
self.x = self._get_coords_x(line) # from columns 31-38 | |
self.y = self._get_coords_y(line) # from columns 39-46 | |
self.z = self._get_coords_z(line) # from columns 47-54 | |
self.occupancy = self._get_occupancy(line) # from columns 55-60 | |
self.t_factor = self._get_temperature_factor(line) # from columns 61-66 | |
self.elem_sym = self._get_element_symbol(line) # from columns 77-78 | |
self.charge = self._get_charge(line) | |
@staticmethod | |
def _get_type(line): | |
return line[0:6].strip() | |
@staticmethod | |
def _get_serial_number(line): | |
return int(line[6:11].strip()) | |
@staticmethod | |
def _get_atom_name(line): | |
return line[12:16].strip() | |
@staticmethod | |
def _get_alternate_location_indicator(line): | |
return line[16:17].strip() | |
@staticmethod | |
def _get_residue(line): | |
return line[17:20].strip() | |
@staticmethod | |
def _get_chain(line): | |
return line[21:22].strip() | |
@staticmethod | |
def _get_residue_sequence_number(line): | |
return int(line[22:26].strip()) | |
@staticmethod | |
def _get_code_for_insertion_residues(line): | |
return line[26:27].strip() | |
@staticmethod | |
def _get_coords_x(line): | |
return float(line[30:38].strip()) | |
@staticmethod | |
def _get_coords_y(line): | |
return float(line[38:46].strip()) | |
@staticmethod | |
def _get_coords_z(line): | |
return float(line[46:54].strip()) | |
@staticmethod | |
def _get_occupancy(line): | |
return float(line[55:60].strip()) | |
@staticmethod | |
def _get_temperature_factor(line): | |
return float(line[61:66].strip()) | |
@staticmethod | |
def _get_element_symbol(line): | |
return line[77:78].strip() | |
@staticmethod | |
def _get_charge(line): | |
return line[78:80] | |
if __name__ == '__main__': | |
pdb_file = open(sys.argv[1]).readlines() | |
atoms_pdb = [] | |
for line in pdb_file: | |
if 'ATOM' in line: | |
atom = PDBAtom(line) | |
atoms_pdb.append(atom) | |
for atom in atoms_pdb: | |
# Write PDB File from atom information | |
pattern = "{:6s}{:5d} {:^4s}{:1s}{:3s} {:1s}{:4d}{:1s}" \ | |
" {:8.3f}{:8.3f}{:8.3f}{:6.2f}{:6.2f} {:>2s}{:2s}".format( | |
atom.type, atom.ser_num, atom.at_name, atom.alt_loc, atom.res,atom.chain, | |
atom.res_seqnum, atom.insertion_rescode, atom.x, atom.y, atom.z, atom.occupancy, | |
atom.t_factor, atom.elem_sym, atom.charge | |
) | |
print(pattern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment