Ngl: parsing getAtomSetWithinSelection result

Created on 15 Jan 2019  路  6Comments  路  Source: nglviewer/ngl

I do not know if I am using this function correctly I want to find all the atoms 4.0 A around my ligand and I am using:

o.structure.getAtomSetWithinSelection('ligand', 4.0) however I have no idea what to do with the result. I never worked with bit arrays. Is there a way to get the atom indexes from the result ?

Best,
Liviu

question

All 6 comments

yes! you can use the toArray() method that will give you the list of the index if the atoms in the atomstore, or the toSeleString() method that gives you the same but as a comma separated list

Just to make sure that I understand. The function, getAtomSetWithinSelection, is good and then I have to use toArray(result).

you can do it in a 2 step way:
let as = structure.getAtomSetWithinSelection(here come all your parameters);
let mySelectedAtoms = as.toArray()
or if you are audacious, in 1 call:
let mySelectedAtoms = structure.getAtomSetWithinSelection(here come all your parameters).toArray()

Once we have a list of indices, as described above, how do we access individual atoms from the atomStore?

Suppose I have the following code:

var selection = new NGL.Selection( ":A and 1" );
var atomSet = component.structure.getAtomSet(selection);
var atomArray = atomSet.toArray();

How do I access the atoms at the indices in atomArray?

Apologies for the simple question, but I can't seem to find this information in the docs.

Access to the AtomStore (and the other store classes) is best done through an AtomProxy (or other proxy classes). This is done to keep the JS object count down (the data is stored in typed arrays and a proxy class accesses the data - the proxy has an index which you update when you want data about a different atom).

You can get an AtomProxy yourself from a structure

var ap = component.structure.getAtomProxy()
ap.index = 72
ap.x      // Retrieve the x coordinate for atom with index 72
ap.index = 105
ap.x      // Now this retrieves the x coordinate for atom at index 105

Full list of things you can get from an AtomProxy here:
http://nglviewer.org/ngl/api/class/src/proxy/atom-proxy.js~AtomProxy.html

Quite a common use case is to iterate over all atoms in a selection, for this you can provide the selection as an optional second argument to the eachAtom method of Structure (structure.eachAtom(callback, selection));

var selection = new NGL.Selection( ":A and 1")
component.structure.eachAtom(function(ap) {
    console.log(ap.bfactor) // The "ap" argument provided here is an AtomProxy instance
}, selection)

Word of warning: Be careful when nesting calls to eachAtom/eachBond/eachResidue, for efficiency reasons structures own a default AtomProxy instance which gets re-used for a lot of methods - if you have re-entrant calls to eachAtom you might have the same AtomProxy object on both the inner and outer loops (and the index on the outer loop will get unexpectedly updated).

Thanks @fredludlow. That was very helpful! You solved my problem, and from there I was able to make a lot of progress. Much appreciated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbliven picture sbliven  路  3Comments

arose picture arose  路  3Comments

martingraham picture martingraham  路  4Comments

arose picture arose  路  6Comments

biharck picture biharck  路  6Comments