user3305567
user3305567

Reputation: 33

biopython PDB: how to reference atom list by an atom's serial number

i'm quite struggling with biopython. i am successful in getting a list of all atoms from a structure object as well as getting/setting coordinates and retrieving a serial number:

from Bio import PDB

pdb = "file.pdb"
name = "file"
p = PDB.PDBParser()
struct_in = p.get_structure(name, pdb)

for atom in PDB.Selection.unfold_entities(struct_in, target_level='A'):
    atom.set_coord(np.array((1,2,3)))
    atom.transform(unity3,(2,-2,1))
    print atom.get_serial_number()

however, i am not able to reference a certain atom (index 23) by its serial number in order to change its coordinates. i am looking for something similar to the fictive "get_atom_by_index()" function in the second line of the following (which doesn't work like that):

atomList = PDB.Selection.unfold_entities(struct_in, target_level='A')
atomList.get_atom_by_index(23).set_coord(newCoord)

of course, i could do something like

for i in atomList:
    if atomList[i].get_serial_number() == 23:
        atomList[i].set_coord(newCoord)

but i would prefer avoiding this additional loop. there MUST be a more convenient way of doing it!

thanks in advance fred

Upvotes: 1

Views: 1639

Answers (2)

xbello
xbello

Reputation: 7443

You can put the PDB in a dict creating the keys from the atoms serial numbers:

selection = PDB.Selection.unfold_entities(struct_in, target_level='A')
serial_numbers = [atom.serial_number for atom in selection]

selection_dict = dict(zip(serial_numbers, selection))

# selection_dict is now something like:
# {1: <Atom N>, 2: <Atom CA>, 3: <Atom C>, ...}

Now you can access the dict with the keys, without looping:

selection_dict[23].set_coord(np.array((1,2,3)))

Upvotes: 0

Sharif Mamun
Sharif Mamun

Reputation: 3574

If you know the specific index number, then why don't you simply do this:

atomList[23].set_coord(newCoord)

Upvotes: 0

Related Questions