Source code for PDBParser
## TEMPY is a Python library designed to help the user manipulate and analyse atomic structures and density maps from 3D EM.
## Copyright (c) 2013 Daven Vasishtan,Irene Farabella, Arun Prasad Pandurangan, Harpal Sahota, Frank Alber and Maya Topf
#from ProtRep import *
# to use biopy and the possibility to use it with TEMPY
from ProtRep_Biopy import *
# to use normal
#from ProtRep_ire3 import *
[docs]class PDBParser:
"""A class to read PDB files either directly from the pdb or a structure instance from Biopython"""
def __init__(self):
pass
@staticmethod
[docs] def read_PDB_file(filename, hetatm=False, surface = False):
"""
Read PDB file and create Structure instance based upon it.
Argument:
*filename*
name of pdb file
*hetatm*
Boolean representing whether the PDB file contains hetatom.
*surface*
Boolean representing whether the PDB file contains surface accessibility data.
"""
#need to fix hetatm and water here
atomList = []
footer = ''
header = ''
switch = False
f = open(filename, 'r')
for line in f.readlines():
info = line.split()
if info:
if(info[0] == 'ATOM'):
switch = True
atomList.append(Atom(line, surface=surface))
elif info[0] == 'TER' and len(atomList)!= 0:
atomList[-1].isTerm = True
elif info[0][:6] == 'HETATM' and hetatm:
atomList.append(Atom(line, surface=surface))
else:
if(switch):
footer += line
else:
header += line
f.close()
return Structure(atomList, filename=filename, header=header, footer=footer, surface=surface)
@staticmethod
[docs] def fetch_PDB(id, output_file):
url = 'http://www.rcsb.org/pdb/files/%s.pdb' % id
a = urllib.urlretrieve(url, output_file)
return PDBParser.read_PDB_file(output_file)
#===============================================================================
#first function to use biopython here it is use directly the PDB file
#better to use the from structure
# @staticmethod
# def bio_to_TEMpy(filename,hetatm=False, surface = False):
# #imported if and when the function is executed.
# from Bio.PDB import PDBParser as PDBParserBiopy
# atomList = []
# hetatomList=[]
# footer = ''
# header = ''
# pdb_code=filename.split("/")[-1]#use os.
# p=PDBParserBiopy()#permissive default True
# structure_id="%s" % pdb_code[:-4]
# structure=p.get_structure(structure_id, filename)
# residues = structure.get_residues()
# for res in residues:
# hetfield=res.get_id()[0]
# if hetfield[0]=="H":
# for atom in res:
# BioPyAtom(atom)
# hetatomList.append(BioPyAtom(atom))
# if hetfield[0]=="W":
# for atom in res:
# BioPyAtom(atom)
# hetatomList.append(BioPyAtom(atom))
# else:
# for atom in res:
# BioPyAtom(atom)
# atomList.append(BioPyAtom(atom))
# # print "here",BioPyAtom(atom)
# return BioPy_Structure(atomList, filename=filename, header=header, footer=footer, surface=surface)
#===============================================================================
@staticmethod
[docs] def bio_strcuture_to_TEMpy(filename,structure,hetatm=False, surface = False):
#imported if and when the function is executed.
"""filename = name of pdb file
hetatm = Boolean representing whether to add hetatm to the structure.Default and Raccomanded is False.
surface = Boolean representing whether the PDB file contains surface accessibility data."""
#NEED TO MAKE A RETURN AND LOOK AT THE CODE FOR hetatm=T.
#THERE IS A HETATM DEF FROM DAVEN add_het_atoms
#NEED TO CHECK IT
from Bio.PDB import PDBParser as PDBParserBiopy
atomList = []
hetatomList=[]
footer = ''
header = ''
pdb_code=filename.split("/")[-1]#use os.
p=PDBParserBiopy()#permissive default True
structure_id="%s" % pdb_code[:-4]
structure=p.get_structure(structure_id, filename)
residues = structure.get_residues()
for res in residues:
hetfield=res.get_id()[0]
if hetfield[0]=="H":
for atom in res:
BioPyAtom(atom)
hetatomList.append(BioPyAtom(atom))
if hetfield[0]=="W":
for atom in res:
BioPyAtom(atom)
hetatomList.append(BioPyAtom(atom))
else:
for atom in res:
BioPyAtom(atom)
atomList.append(BioPyAtom(atom))
return BioPy_Structure(atomList, filename=filename, header=header, footer=footer, surface=surface)
#===========================================================================
# def convert_bio_atom_to_TEMpy(bio_atom):
# pass
#===========================================================================