user2494228
user2494228

Reputation: 21

Parsing a PDB file with multiple structures into an array

I have a PDB file with a few thousand structures, and I would like to save the position coordinates of, say, the alpha carbons of the first ten structures into a numpy array. I can parse a PDB file with a single structure into an array using the code below, but cannot extend this to a file with many structures.

from Bio.PDB.PDBParser import PDBParser
import numpy

pdb_filename ='./1fqy.pdb'
parser = PDBParser(PERMISSIVE=1)
structure = parser.get_structure("1fqy", pdb_filename)
model = structure[0]
chain = model["A"]

S1coor = numpy.zeros(shape=(226, 3))
i = 0

for residue1 in chain:
     resnum = residue1.get_id()[1]
     atom1 = residue1['CA']
     S1coor[i] = atom1.get_coord()
     i = i + 1

Upvotes: 2

Views: 1703

Answers (1)

Viv_bio
Viv_bio

Reputation: 99

from Bio.PDB.PDBParser import PDBParser
import numpy , tempfile ,os , re

models_re = re.compile("MODEL")
pdb_re = re.compile(r"MODEL(.*?)ENDMDL", re.DOTALL)

def PDB_parse(pdb_file_handle):
    model_pos = []
    models = []
    k = open(pdb_file_handle,"r").read()
    for i in models_re.finditer(k):
        model_pos.append(i.start())
    for i in model_pos:
        models.append(pdb_re.search(k,i).group())
    return models

array_all_structure = []

for i in PDB_parse(pdb_file_handle):
    temp_file = tempfile.NamedTemproaryFile(delete = False)
    temp_file.write(i)
    temp_file.close
    structure = parser.get_structure("1fqy", temp_file.name)
    os.remove(temp_file.name)
    model = structure[0]
    chain = model["A"]
    S1coor = numpy.zeros(shape=(226, 3))
    i = 0
    for residue1 in chain:
       resnum = residue1.get_id()[1]
       atom1 = residue1['CA']
       S1coor[i] = atom1.get_coord()
       i = i + 1
       array_all_structure.append(i)

May be this kind of linker will help , where you first isolate pdb files and then read them accordingly.

Upvotes: 1

Related Questions