clauswilke / PeptideBuilder

A simple Python library to generate model peptides
MIT License
79 stars 30 forks source link

atom coordinates should be numpy.array rather than list or vector #7

Closed hongbo-zhu-cn closed 7 years ago

hongbo-zhu-cn commented 7 years ago

Hi, I encounter ValueError when using PeptideBuilder, coming from the access of atom coordinates. See the following example code:

from __future__ import print_function
from PeptideBuilder import Geometry
import PeptideBuilder

geo = Geometry.geometry('G')
geo.phi=-60
geo.psi_im1=-40
structure = PeptideBuilder.initialize_res(geo)
for i in range(5):
    structure = PeptideBuilder.add_residue(structure, geo)

from Bio.PDB.NeighborSearch import NeighborSearch

ns = NeighborSearch([a for a in structure.get_atoms()])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-0d7132225757> in <module>()
----> 1 ns = NeighborSearch(a for a in structure.get_atoms())

/usr/lib/python2.7/dist-packages/Bio/PDB/NeighborSearch.pyc in __init__(self, atom_list, bucket_size)
     42         coord_list = [a.get_coord() for a in atom_list]
     43         # to Nx3 array of type float
---> 44         self.coords = numpy.array(coord_list).astype("f")
     45         assert(bucket_size > 1)
     46         assert(self.coords.shape[1] == 3)

ValueError: setting an array element with a sequence.

it should be caused by incorrect format of atom.coord in peptidebuilder. See the Atom initialization in BioPython:

class Atom(object): 
    def __init__(self, name, coord, bfactor, occupancy, altloc, fullname, serial_number, 
                 element=None): 
        """Create Atom object. 
        ......

        @param coord: atomic coordinates (x,y,z) 
        @type coord: **Numeric array** (Float0, size 3)  

As far as I can recall, biopython used to use Numeric, not NumPy. So if you convert all the atomic coordinates to numpy.array in PeptideBuilder.py, the Error will be gone (see the pull request).

cheers, hongbo

clauswilke commented 7 years ago

Thanks for pointing out this issue and for providing a fix! This is now addressed in the latest version.