Last active
April 8, 2016 17:44
-
-
Save sumanth232/42e8c1669ccd09cae4ae4b32b0c3a3dd to your computer and use it in GitHub Desktop.
BioPhysics Atom Class file
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 math | |
class Atom: | |
def __init__(self,line): | |
self.pdb_line=line | |
self.number=int(line[6:11]) | |
self.name=line[12:16].strip() | |
self.residue_name=line[17:20].strip() | |
self.residue_number=int(line[22:26]) | |
self.chain=line[21] | |
self.pdb_line=line | |
self.x_coordinate=float(line[30:38]) | |
self.y_coordinate=float(line[38:46]) | |
self.z_coordinate=float(line[46:54]) | |
self.coordinates=[self.x_coordinate,self.y_coordinate,self.z_coordinate] | |
self.alternal_location_indicator = line[16] | |
self.achar = line[26] | |
self.occupancy = float(line[54:60]) | |
self.temperature_factor = float(line[60:66]) | |
self.segment_identifier = line[72:76] | |
self.element_symbol = line[76:78] | |
self.charge_on_atom = line[78:80] | |
if "'" in self.name and len(self.residue_name)<3: | |
self.label='Sugar' | |
elif "P" in self.name and len(self.residue_name)<3: | |
self.label='Phosphate' | |
elif len(self.residue_name)<3: | |
self.label='Base' | |
elif self.name in ['N','CA','C']: | |
self.label='Backbone' | |
else: | |
self.label='Sidechain' | |
def __repr__(self): | |
return '{0:4s}{1:>5d}{2:<3s}{3:>4s}{4:>4d}'.format(self.name,self.number,self.chain,self.residue_name,self.residue_number) | |
def line_to_file(self): | |
newline = '{0:<6s}{1:>5d} {2:<3s}{3}{4:>3s} {5}{6:>4d}{7} {8:>8.3f}{9:>8.3f}{10:>8.3f}{11:>6.2f}{12:>6.2f} {13:<4s}{14:>2s}{15:>2s}\n'.format('ATOM', | |
self.number, self.name, self.alternal_location_indicator, | |
self.residue_name, self.chain, self.residue_number, self.achar, | |
self.x_coordinate, self.y_coordinate, self.z_coordinate, | |
self.occupancy, self.temperature_factor, self.segment_identifier, self.element_symbol, | |
self.charge_on_atom) | |
return newline | |
def distance(self,other): | |
return math.sqrt(pow(self.x_coordinate-other.x_coordinate,2)+ | |
pow(self.y_coordinate-other.y_coordinate,2)+ | |
pow(self.z_coordinate-other.z_coordinate,2)) | |
def translate_atom(start_idx, end_idx, atom): | |
start = atom_array[start_idx] | |
end = atom_array[end_idx-1] | |
vector.x_coordinate = end.x_coordinate - start.x_coordinate | |
vector.y_coordinate = end.y_coordinate - start.y_coordinate | |
vector.z_coordinate = end.z_coordinate - start.z_coordinate | |
atom.x_coordinate += vector.x_coordinate | |
atom.y_coordinate += vector.y_coordinate | |
atom.z_coordinate += vector.z_coordinate | |
atom.number -= (end_idx - start_idx) | |
return atom | |
def translate_AtomArray(start_idx, end_idx, atom_array): | |
for atom in atom_array: | |
if atom.number >= end_idx: | |
atom = translate_atom(atom_array[start_idx], atom_array[end_idx-1], atom) | |
return atom_array | |
#name=raw_input('Please enter PDB file name:\n') | |
#chain=raw_input('Please enter CHAIN id:\n') | |
name='1A2Y.pdb' | |
outputFileName = 'output.pdb' | |
target = open(outputFileName, 'w') | |
atom_start = 2741 | |
atom_end = 2756 | |
source=open(name) | |
for line in source: | |
if line.startswith('ATOM'): | |
atom = Atom(line) | |
if atom.number < atom_start: | |
target.write(line) | |
else: | |
outline = atom.line_to_file() | |
print outline | |
target.write(outline) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
01234567890123456789012345678901234567890123456789012345678901234567890123456789
ATOM 6338 CA TYR A 456 60.059 70.952 -26.972 1.00 23.16 C
ATOM 2 P U R 601 86.329 55.822 -34.357 1.00 69.36 P