SBNodeIndexer#

This class describes a node indexer (a list of nodes).

You can get node indexers directly from the active document using the SAMSON facade (SAMSON.getNodes() samson.SAMSON.getNodes>()) or for from a node using the SBNode.getNodes() function. To select specific nodes you can use the Node Specification Language.

Getting nodes using the SAMSON facade (SAMSON):

Getting nodes from the active document#
# get all nodes in the active document
nodeIndexer = SAMSON.getNodes()

# get all atoms in the active document
nodeIndexer = SAMSON.getNodes('node.type atom')

# the same using short names
nodeIndexer = SAMSON.getNodes('n.t a')

# you can check the size of the indexer
print(len(nodeIndexer))

Getting node’s children nodes:

Getting nodes from a node#
# get all atoms in the node
nodeIndexer = node.getNodes('node.type atom')

You can loop over the node indexer in a pythonic way:

Printing node information#
for node in nodeIndexer:
        print(node)

See also

SAMSON SDK: SBDDataGraphNodeIndexer

class samson.SBNodeIndexer(*args, **kwargs)#

Bases: pybind11_object

This class describes a node indexer.

Overloaded function.

  1. __init__(self: samson.SBNodeIndexer) -> None

Constructs a node indexer

  1. __init__(self: samson.SBNodeIndexer, initialSize: int) -> None

Constructs a node indexer with a pre-allocated initialSize

  1. __init__(self: samson.SBNodeIndexer, other: samson.SBNodeIndexer) -> None

Copy constructor

  1. __init__(self: samson.SBNodeIndexer, iterable: Iterable) -> None

Constructor from iterable

addNode(self: samson.SBNodeIndexer, node: samson.SBNode) int#

Adds a node to the indexer and returns the index of the node.

Parameters:

node (samson.SBNode) – A node

clear(self: samson.SBNodeIndexer) None#

Clears the node indexer

getIndex(self: samson.SBNodeIndexer, node: samson.SBNode) int#

Returns the index associated to the node.

Parameters:

node (samson.SBNode) – A node

getNode(self: samson.SBNodeIndexer, index: int) samson.SBNode#

Returns a node with the given index.

Parameters:

index (int) – An index

getNodes(*args, **kwargs)#

Overloaded function.

  1. getNodes(self: samson.SBNodeIndexer, selectionString: str = ‘*’, visitString: str = ‘*’, includeDependencies: bool = False) -> samson.SBNodeIndexer

Returns nodes (in a node indexer), based on a selectionString and a visitString, with or without dependencies).

For each node in the node indexer, this function calls the samson.SBNode.getNodes function.

Parameters:
  • selectionString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be selected.

  • visitString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be visited.

  • includeDependencies (bool, default=False) – Whether to include node dependencies or not.

Returns:

A node indexer.

Return type:

samson.SBNodeIndexer

  1. getNodes(self: samson.SBNodeIndexer, nodeIndexer: samson.SBNodeIndexer, selectionString: str = ‘*’, visitString: str = ‘*’, includeDependencies: bool = False) -> None

Fills in the node indexer nodeIndexer based on a selectionString and a visitString, with or without dependencies).

Note that the nodeIndexer is not cleared when entering this function.

For each node in the node indexer, this function calls the samson.SBNode.getNodes function.

Parameters:
  • nodeIndexer (samson.SBNodeIndexer) – A node indexer. It is not cleared by this function.

  • selectionString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be selected.

  • visitString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be visited.

  • includeDependencies (bool, default=False) – Whether to include node dependencies or not.

getRootNodes(self: samson.SBNodeIndexer) samson.SBNodeIndexer#

Returns a node indexer with the root nodes among the indexed nodes, i.e. the nodes that do not have indexed ascendants.

For example, if the indexed nodes contain two nodes - an atom and its direct parent node (e.g. a backbone node) - then only this parent node will be added in the resulting node indexer.

Returns:

A node indexer.

Return type:

samson.SBNodeIndexer

hasIndex(self: samson.SBNodeIndexer, node: samson.SBNode) bool#

Returns True if the node is present in the node indexer, i.e. it has an index.

Parameters:

node (samson.SBNode) – A node

hasNode(*args, **kwargs)#

Overloaded function.

  1. hasNode(self: samson.SBNodeIndexer, node: samson.SBNode) -> bool

Returns True if the node is present in the node indexer.

Parameters:

node (samson.SBNode) – A node

  1. hasNode(self: samson.SBNodeIndexer, selectionString: str = ‘*’, visitString: str = ‘*’, includeDependencies: bool = False) -> bool

Checks for nodes in the node indexer based on the provided selection filter selectionString and other parameters.

For each node in the node indexer, this function calls the samson.SBNode.hasNode function.

This function traverses nodes in the node indexer for which a predicate based on the visitString is True (depth-first), and stores checks for the nodes for which the selectionString is true. Precisely, if the predicate based on the visitString returns false, the node is not visited, and neither are its descendants. If the predicate based on the visitString returns true, the node is visited, and this visit predicate will be used to decide whether its children are visited or not. Note that the node itself must satisfy the predicate based on the visitString else nothing is visited.

When includeDependencies is true, the function also checks for nodes that are not descendants of this node, but are dependencies nonetheless. For example, in a SBBond, the two atoms connected by the bond are not children of the bond, but are still dependencies of the bond. Hence, provided they satisfy the selectionString and visitString, they are checked for if includeDependencies is true.

Parameters:
  • selectionString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be checked.

  • visitString (str, default='*') – A Node Specification Language expression (SAMSON API: Node Specification Language) that describes what nodes should be visited.

  • includeDependencies (bool, default=False) – Whether to include node dependencies or not.

Returns:

Whether the nodes are present

Return type:

bool

Examples

Checks for the presence of atoms in a node indexer:

>>> res = node_indexer.hasNode('node.type atom')
index(self: samson.SBNodeIndexer, node: samson.SBNode) int#

Returns the index associated to the node.

Parameters:

node (samson.SBNode) – A node

printDebugInfo(self: samson.SBNodeIndexer) None#

Prints some debugging information in stdout

removeNode(self: samson.SBNodeIndexer, node: samson.SBNode) int#

Removes the node from the indexer.

Parameters:

node (samson.SBNode) – A node

property isEmpty#

True if empty

property size#

The size of the node indexer