douweschulte / pdbtbx

A library to open/edit/save (crystallographic) Protein Data Bank (PDB) and mmCIF files in Rust.

Home Page:https://crates.io/crates/pdbtbx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

atom() method Index

DocKDE opened this issue · comments

Hi,
I have a test PDB file. Excerpt shown here:

ATOM     26  N   TYR     3      71.828  47.686  89.455  0.00  0.00           N
ATOM     27  H   TYR     3      71.781  46.672  89.403  0.00  0.00           H
ATOM     28  CA  TYR     3      72.947  48.274  90.203  0.00  0.00           C
ATOM     29  HA  TYR     3      73.190  49.241  89.761  0.00  0.00           H
ATOM     30  CB  TYR     3      72.523  48.510  91.664  0.00  0.00           C
ATOM     31 HB2  TYR     3      73.354  48.961  92.204  0.00  0.00           H
ATOM     32 HB3  TYR     3      71.713  49.240  91.671  0.00  0.00           H
ATOM     33  CG  TYR     3      72.079  47.277  92.434  0.00  0.00           C

When I call pdb.atom(26) I find that, in fact, The H atom of TYR is returned. Is this intentional? I must say I find this counterintuitive. If unintentional this is probably due to 0- vs. 1-based indexing in Rust and PDB files respectively.

As described in the documentation that function gives the atom based on index, so indeed 0 Vs 1 based indexing gives rise to the problem shown. For other searching methods there is the iterator trait can be used, in particular there is a function conveniently called find which takes a lambda function to select the right atom. So you could do pdb.atoms().find(|a| a.serial_number() == NUM). I hope that cleared up the confusion for you. I choose to do it this way because iterators are very common in rust code, so reusing all existing functions build for them is easier then rewriting everything ;-).

I see, seems I overlooked this piece of information. Probably because in the docstring of the function the argument is called "index" and in my personal naming scheme this is the same as the atom serial number.
Still got a lot to learn, thanks for being educational!