Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit the structure annotation using an atom #690

Open
dargen3 opened this issue Nov 1, 2024 · 2 comments
Open

Edit the structure annotation using an atom #690

dargen3 opened this issue Nov 1, 2024 · 2 comments

Comments

@dargen3
Copy link

dargen3 commented Nov 1, 2024

Hello,

I would like to please ask if there is any way to modify the structure using an atom in the biotite? I know that the data of individual atoms can be accessed via indexing. For example, if I want to change the charge of the n-th atom to -1:

import biotite.structure.io.pdbx as mmCIF
import biotite.structure as biotite_structure

mmCIF_file = mmCIF.CIFFile.read("9bfl.cif")
protein = mmCIF.get_structure(mmCIF_file,
                              model=1,
                              extra_fields=["charge"],
                              include_bonds=True)

protein.charge[10] = -1

But I would like to ask if it is possible to access/change values for an atom without knowing its index? For example, if I want to iterate over residues and then over their atoms:

import biotite.structure.io.pdbx as mmCIF
import biotite.structure as biotite_structure

mmCIF_file = mmCIF.CIFFile.read("9bfl.cif")
protein = mmCIF.get_structure(mmCIF_file,
                              model=1,
                              extra_fields=["charge"],
                              include_bonds=True)

for res in biotite_structure.residue_iter(protein):
    for atom in res:
        if some_nontrivial_magic_function(atom):
            atom.charge = -1

The problem with the code above is that if I store a value in atom.charge, that value is not written to protein.charge.

@padix-key
Copy link
Member

No, unfortunately this is not possible. If would propose to rewrite your snippet similar to the following:

for res_start_i, res_stop_i in itertools.pairwise(biotite_structure.get_residue_starts(protein, add_exclusive_stop=True)):
    for atom_i in range(res_start_i, res_stop_i):
        if some_nontrivial_magic_function(protein[atom_i]):
            protein.charge[atom_i] = -1

Using residue-wise iteration is not necessary in this snippet, but I suppose in your actual application it is.

Whenever possible, I recommend vectorized operations over the entire AtomArray or at least slices of it for performance reasons, though I do not know if this is possible for your some_nontrivial_magic_function().

@dargen3
Copy link
Author

dargen3 commented Nov 2, 2024

Thank you for quick help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants