molstar / molstar

A comprehensive macromolecular library

Home Page:https://molstar.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does one convert an object `ref` into a `Loci` selection?

rtviii opened this issue · comments

I'm trying to do something really basic: trigger a selection of a whole structure. I can't figure a way to either construct an MS query or convert a Ref to Loci. (I still don't really know how to conceptualize the state tree)

Let's say i have a structure, 5AFI loaded into the viewer. I can select/focus/highlight its individual chains no problem:

// Say, chain `A1`
    const ctx  = window.molstar_plugin;
    const data = ctx.managers.structure.hierarchy.current.structures[0]?.cell.obj?.data;
    const sel = Script.getStructureSelection(Q => Q.struct.generator.atomGroups({
        'chain-test': Q.core.rel.eq([Q.struct.atomProperty.macromolecular.auth_asym_id(), `A1`]),
    }), data);

    let loci = StructureSelection.toLociWithSourceUnits(sel);
    plugin.managers.interactivity.lociHighlights.highlight({ loci });

But what if i wanted to select a whole structure? I'm not:

  • finding a way to call a Structure by its PDB ID through Q.struct.generator (might have just missed something)
  • and neither can i figure out how to feed a Ref to managers.selection:
         const structureData      = ctx.managers.structure.hierarchy.current.structures[0]
        const cell_transform_ref = structureData.cell.transform.ref
        const state_data         = ctx.state.data.select(cell_transform_ref)[0].obj as PluginStateObject.Molecule.Structure
        ctx.managers.structure.selection( ?????????? ) // <<<<<<<<<<<

I feel like i'm missing something really basic: how ref hashes correspond to state objects and whether Molstar-"internal" things like Selection have distinct namespace from the actual atomic structure components inside the state.

Anyway, if somebody could toss in a way

  1. to not just a way to select one (aka first:[0]) whole structure via ctx.managers.structure.hierarchy.current.structures[0], but select any structure by its ID, i feel like something might click for me.
  2. a reliable way to convert a ref to a Loci

Thanks a ton

try with Q.struct.generator.all()

Thanks! What if i have two structures in the tree? Say, 5AFI and 3J7Z. How do i go about selecting just the 5AFI?
Best

Thanks! What if i have two structures in the tree? Say, 5AFI and 3J7Z. How do i go about selecting just the 5AFI?

One way would be to iterate over ctx.managers.structure.hierarchy.current.structures , e.g.

for (const s of plugin.managers.structure.hierarchy.current.structures) {
    const model = s.model?.cell.obj?.data;
    // check model.entry / entryId (it depends how you originally loaded your data)
}

Another ways:

  • Query the state tree directly with plugin.state.data.selectQ to select the correct structure
  • Store the ref to the parent structures at the time you load them

Got it. Thanks a lot!