besmarts.core.codecs module

besmarts.core.codecs

Definition of the graph codec, the class responsible for encoding and decoding SMARTS and SMILES strings to a BESMARTS graph representation.

besmarts.core.codecs.SMILES_ELEMENT(i)[source]
class besmarts.core.codecs.graph_codec(smiles_config: smiles_perception_config, primitive_codecs: Dict[primitive_key, primitive_codec], dtype: bitvec, atom_primitives: Sequence[primitive_key], bond_primitives: Sequence[primitive_key])[source]

Bases: object

A graph codec transforms SMARTS and/or SMILES between string and graph representations.

smarts_decode(smarts: str) graph[source]

Transform a SMARTS string into a subgraph or structure depending if there are mapped atoms present.

Paramters

smartsstr

The SMARTS string to decode

returns:

The graph representation of the SMILES string. If the SMARTS has mapped atoms, then a substructure will be returned instead.

rtype:

graphs.graph

smarts_encode(g: graph) str[source]

Transform a graph into a SMARTS string.

Parameters:

g (graphs.graph) – The graph to encode

Returns:

The SMARTS representation of the graph

Return type:

str

smiles_decode(smiles: str) graph[source]

Transform a SMILES string into a graph

Parameters:

smiles (str) – The SMILES string to decode

Returns:

The graph representation of the SMILES string

Return type:

graph

smiles_encode(g: graph) str[source]

Transform a graph into a SMILES string. The graph must be a fragment, i.e. all primitives have exactly one value set.

Parameters:

g (graphs.graph) – The graph to encode

Returns:

The SMILES representation of the graph

Return type:

str

class besmarts.core.codecs.graph_codec_chain(smiles_config: smiles_perception_config, primitive_codecs: Dict[primitive_key, primitive_codec], dtype: bitvec, atom_primitives: Sequence[primitive_key], bond_primitives: Sequence[primitive_key])[source]

Bases: graph_codec

add_graph_codec()[source]
smarts_decode(smarts)[source]

Transform a SMARTS string into a subgraph or structure depending if there are mapped atoms present.

Paramters

smartsstr

The SMARTS string to decode

returns:

The graph representation of the SMILES string. If the SMARTS has mapped atoms, then a substructure will be returned instead.

rtype:

graphs.graph

smarts_encode(g: graph, primary=None, tag=None)[source]

Transform a graph into a SMARTS string.

Parameters:

g (graphs.graph) – The graph to encode

Returns:

The SMARTS representation of the graph

Return type:

str

smiles_decode(smiles)[source]

Transform a SMILES string into a graph

Parameters:

smiles (str) – The SMILES string to decode

Returns:

The graph representation of the SMILES string

Return type:

graph

smiles_encode(g: graph, primary=None, tag=None)[source]

Transform a graph into a SMILES string. The graph must be a fragment, i.e. all primitives have exactly one value set.

Parameters:

g (graphs.graph) – The graph to encode

Returns:

The SMILES representation of the graph

Return type:

str

class besmarts.core.codecs.intvec_codec(primitive_codecs, atom_primitives, bond_primitives)[source]

Bases: object

A codec that transforms graphs to and from a vector form, allowing for compact serialization.

graph_decode(intvec: intvec) graph[source]
graph_encode(g: graph) intvec[source]
structure_decode(intvec: intvec) structure[source]
structure_encode(g: structure) intvec[source]
subgraph_decode(intvec: intvec) subgraph[source]
subgraph_encode(g: subgraph) intvec[source]
besmarts.core.codecs.intvec_codec_graph_decode(intvec: intvec, atom_primitives, bond_primitives) graph[source]
besmarts.core.codecs.intvec_codec_graph_decode_auto(intvec: intvec, atom_primitives, bond_primitives) Tuple[graph, Sequence[int], int][source]
besmarts.core.codecs.intvec_codec_structure_decode(intvec: intvec, atom_primitives, bond_primitives) structure[source]
besmarts.core.codecs.intvec_codec_subgraph_decode(intvec: intvec, atom_primitives, bond_primitives) subgraph[source]
class besmarts.core.codecs.primitive_codec_aromatic[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the array is null

Return type:

str

tokenize_smarts(smarts: str) List[str][source]
class besmarts.core.codecs.primitive_codec_bond_order[source]

Bases: primitive_codec

decode_smarts(dtype, smarts: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the array is null

Return type:

str

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

tokenize_smarts(smarts) List[str][source]
class besmarts.core.codecs.primitive_codec_bond_ring[source]

Bases: primitive_codec

decode_smarts(dtype: type, smarts: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the array is null

Return type:

str

tokenize_smarts(smarts: str) List[str][source]
class besmarts.core.codecs.primitive_codec_chirality[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the the array is null

Return type:

str

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

tokenize_smarts(smarts) List[str][source]
class besmarts.core.codecs.primitive_codec_connectivity_ring[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

class besmarts.core.codecs.primitive_codec_connectivity_total[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

class besmarts.core.codecs.primitive_codec_element[source]

Bases: primitive_codec

count_carbon_smiles(nodes)[source]
count_element_smiles(nodes, n)[source]
decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

class besmarts.core.codecs.primitive_codec_formal_charge[source]

Bases: primitive_codec

count_charge_smiles(nodes)[source]

Compute the total charge across a set of nodes. Assumes fragments by by only using the first value set.

Parameters:

nodes (Sequence[bechem]) – The nodes to calculate the total charge from

Returns:

The charge of the nodes

Return type:

int

decode_int(x: int) int[source]

Transform an encoded integer. This method overrides the super class method by mapping non-negative integers to all integers.

Parameters:

x (int) – The integer to decode

Returns:

The decoded integer

Return type:

int

decode_smarts(dtype, smarts: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_int(x: int) int[source]

Transform a raw integer to an encoded form. This method overrides the super class method by handling the domain of charges and maps them to the set of non-negative integers.

Parameters:

x (int) – The integer to encode

Returns:

The encoded integer

Return type:

int

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the array is null

Return type:

str

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

tokenize_smarts(smarts: str)[source]
class besmarts.core.codecs.primitive_codec_hydrogen[source]

Bases: primitive_codec

count_hydrogen_smiles(nodes)[source]
decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

Bases: primitive_codec

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

Bases: primitive_codec

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

class besmarts.core.codecs.primitive_codec_ring_smallest[source]

Bases: primitive_codec

decode_int(x: int) int[source]

Transform a decoded integer into its value for SMARTS/SMILES. The default behavior is a passthrough and works for most primitives, such as element and hydrogen. Special behavior for certain primitives e.g. formal charge, must be handled by subclassing.

Parameters:

x (int) – The integer to decode

Returns:

The decoded integer

Return type:

int

decode_smarts(dtype: type, smarts: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_int(x: int) int[source]

Transform an integer parsed from a SMILES or SMARTS representation to a valid value for storing in a bit array. The default behavior is a passthrough and works for most primitives, such as element and hydrogen. Special behavior for certain primitives e.g. formal charge, must be handled by subclassing.

Parameters:

x (int) – The integer to encode

Returns:

The encoded integer

Return type:

int

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive.

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the array is null

Return type:

str

tokenize_smarts(smarts: str) List[str][source]
class besmarts.core.codecs.primitive_codec_unit[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

decode_smiles(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_supersmarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive. This will expand the graph primitive to SMARTS

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the the array is null

Return type:

str

tokenize_smarts(smarts: str)[source]
class besmarts.core.codecs.primitive_codec_unit_index[source]

Bases: primitive_codec

This will refer or extract a subset of the graph primitive

decode_smiles(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

decode_supersmiles(obj) bitvec[source]
encode_supersmarts(arr: bitvec) str[source]
tokenize_smarts(smarts: str)[source]
class besmarts.core.codecs.primitive_codec_valence[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

class besmarts.core.codecs.primitive_codec_variable[source]

Bases: primitive_codec

decode_smarts(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

decode_smiles(dtype, obj: str) bitvec[source]

Parse and transform the SMARTS atom to a binary form. This method overrides the super class method to handle the parsing this primitive.

Parameters:
  • dtype (array) – The class to use to build new arrays

  • smarts (str) – The SMARTS string of the atom to parse

Returns:

The decoded SMARTS string.

Return type:

array

encode_smarts(arr: bitvec) str[source]

Transform the array to a SMARTS string. This method overrides the super class method to handle specifics for this primitive. This will expand the graph primitive to SMARTS

Parameters:

arr (array) – The array to encode

Returns:

The SMARTS string. Note that an invalid SMARTS string will be returned as “_” if the the array is null

Return type:

str

encode_smiles(arr: bitvec) str[source]

Transform the array to a SMILES string. This method overrides the super class method to handle specifics for this primitive, as most primitives are not used in SMILES.

Parameters:

arr (array) – The array to encode

Returns:

The SMILES string.

Return type:

str

tokenize_smarts(smarts: str)[source]
besmarts.core.codecs.primitive_codecs_get() Dict[primitive_key, primitive_codec][source]
besmarts.core.codecs.smarts_decode_distributed(smarts: str, shm=None) graph[source]
besmarts.core.codecs.smiles_decode_distributed(smiles: str, shm=None) graph[source]
besmarts.core.codecs.smiles_decode_list_distributed(smiles: List[str], shm=None) graph[source]