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.
- 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:
objectA 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
- 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- 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
- class besmarts.core.codecs.intvec_codec(primitive_codecs, atom_primitives, bond_primitives)[source]¶
Bases:
objectA codec that transforms graphs to and from a vector form, allowing for compact serialization.
- 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
- 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
- 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
- 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
- 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- 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_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
- class besmarts.core.codecs.primitive_codec_hydrogen[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_link_dst[source]¶
Bases:
primitive_codec- 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
- class besmarts.core.codecs.primitive_codec_link_src[source]¶
Bases:
primitive_codec- 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
- 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
- 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
- class besmarts.core.codecs.primitive_codec_unit_index[source]¶
Bases:
primitive_codecThis 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
- 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
- besmarts.core.codecs.primitive_codecs_get() Dict[primitive_key, primitive_codec][source]¶