This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Biopython: Weird error when adding coordinates of two sets

I have a pdb file having 30 frames. I want to calculate the average structure. Two add the coordinates, I wrote the following python code:

#!/usr/bin/python

from Bio.PDB.PDBParser import PDBParser

import numpy as np

parser=PDBParser(PERMISSIVE=1)

structure_id="mode_7"
filename="mode_7.pdb"
structure=parser.get_structure(structure_id, filename)
model1=structure[0]
newc=[]
coord=[]
for chain1 in model1.get_list():
    for residue1 in chain1.get_list():
        ca1=residue1["CA"]
        coord1=ca1.get_coord()
        newc.append(coord1)
for i in range(1,29):
    model=structure[i]
    for chain in model.get_list():
        for residue in chain.get_list():
            ca=residue["CA"]
            coord.append(ca.get_coord())
    newc=np.add(newc,coord)

print newc

print "END"

It gives out the following error:

Traceback (most recent call last):
  File "./average.py", line 26, in <module>
    newc=np.add(newc,coord)
ValueError: operands could not be broadcast together with shapes (124,3) (248,3)

I can not figure out how it takes 248 atoms. Because, each of the models has only 124 atoms.

Can you help???

biopython python structure model

1 answer

Guess you already realize the mistake in the code but just in case, the coord list is appended at each iteration of your range(1, 29). The first iteration newc and coord have shapes (124,3) and (124,3) respectively. However, when i = 2 (second iteration), coord is appended 124 atoms and takes the shape (248,3). You should put coord = [], 'nested' in range(1,29) so that it is emptied each time:

`

for i in range(1,29):
    coord = []
    model=structure[i]
    # Rest of code

`

Log in to answer this question.