4.1.11.2. graph/Graph¶
- class mod.Graph¶
This class models an undirected graph with labels on vertices and edges, without loops and without parallel edges. See Graph, Rule, and Molecule Model for more details.
The class implements the
protocols.LabelledGraph
. See graph/GraphInterface for additional guarantees.- aut(labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))¶
- Parameters
labelSettings (LabelSettings) – the label settings to use.
- Returns
an object representing the automorphism group of the graph, with the given label settings.
- Return type
- print()¶
- print(first, second=None)
Print the graph, using either the default options or the options in
first
andsecond
. Iffirst
andsecond
are the same, only one depiction will be made.- Parameters
first (GraphPrinter) – the printing options used for the first depiction.
second (GraphPrinter) – the printing options used for the second depiction. If it is
None
then it is set tofirst
.
- Returns
the names for the PDF-files that will be compiled in post-processing. If
first
andsecond
are the same, the two file prefixes are equal.- Return type
- printTermState()¶
Print the term state for the graph.
- getGMLString(withCoords=False)¶
- Returns
the GML representation of the graph, optionally with generated 2D coordinates.
- Return type
- Raises
LogicError
when coordinates are requested, but none can be generated.
- printGML(withCoords=False)¶
Print the GML representation of the graph, optionally with generated 2D coordinates.
- Returns
the filename of the printed GML file.
- Return type
- Raises
LogicError
when coordinates are requested, but none can be generated.
- smiles¶
(Read-only) If the graph models a molecule, this is the canonical SMILES string for it.
- Type
- Raises
LogicError
if the graph is not a molecule.
- smilesWithIds¶
(Read-only) If the graph models a molecule, this is the canonical SMILES string for it, that includes the internal vertex id as a class label on each atom.
- Type
- Raises
LogicError
if the graph is not a molecule.
- graphDFSWithIds¶
(Read-only) This is a GraphDFS of the graph, where each vertices have an explicit id, corresponding to its internal vertex id.
- Type
- linearEncoding¶
(Read-only) If the graph models a molecule this is the SMILES string string, otherwise it is the GraphDFS string.
- Type
- isMolecule¶
(Read-only) Whether or not the graph models a molecule. See Molecule Encoding.
- Type
- energy¶
(Read-only) If the graph models a molecule, this is some energy value. The energy is calculated using Open Babel, unless already calculated or cached by
Graph.cacheEnergy()
.- Type
- cacheEnergy(e)¶
If the graph models a molecule, sets the energy to a given value.
- Parameters
e (float) – the value for the energy to be set.
- exactMass¶
(Read-only) The exact mass of the graph, if it is a molecule. It is the sum of the exact mass of each atom, with the mass of electrons subtracted corresponding to the integer charge. That is, the mass is \(\sum_a (mass(a) - mass(e)\cdot charge(a))\). If an atom has no specified isotope, then the most abundant is used.
- Type
- Raises
LogicError
if it is not a molecule, including if some isotope has not been tabulated.
- vLabelCount(label)¶
- eLabelCount(label)¶
- isomorphism(other, maxNumMatches=1, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))¶
- Parameters
maxNumMatches (int) – the maximum number of isomorphisms to search for.
labelSettings (LabelSettings) – the label settings to use during the search.
- Returns
the number of isomorphisms from this graph to
other
, but at mostmaxNumMatches
.- Return type
- monomorphism(other, maxNumMatches=1, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))¶
- Parameters
maxNumMatches (int) – the maximum number of monomorphisms to search for.
labelSettings (LabelSettings) – the label settings to use during the search.
- Returns
the number of monomorphisms from this graph to
other
, though at mostmaxNumMatches
.- Return type
- makePermutation()¶
- Returns
a graph isomorphic to this, but with the vertex indices randomly permuted.
- Return type
- image¶
(Write-only) A custom depiction for the graph. The depiction file used will be the string returned by the given function, with
.pdf
appended. The function will only be called once.- Type
Callable[[], str]
- imageCommad¶
A command to be run in post-processing if a custom depiction is set. The command is only run once.
- Type
- instantiateStereo()¶
Make sure that stereo data is instantiated.
- Raises
StereoDeductionError
if the data was not instantiated and deduction failed.
- getVertexFromExternalId(id)¶
If the graph was not loaded from an external data format, then this function always return a null descriptor. If the graph was loaded from a SMILES string, but any class label was not unique, then the function always return a null descriptor.
Note
In general there is no correlation between external and internal ids.
- minExternalId¶
- maxExternalId¶
(Read-only) If the graph was not loaded from an external data format, then these attributes are always return 0. Otherwise, they are the minimum/maximum external id from which non-null vertices can be obtained from
getVertexFromExternalId()
. If no such minimum and maximum exists, then they are 0.- Type
- loadingWarnings¶
(Read-only) The list of warnings stored when the graph was created from an external format. Each entry is a message and then an indicator of whether the warning was printed before construction (
True
), or was a silenced warning (False
).- Raises
LogicError
if the graph does not have data from external loading- Type
4.1.11.2.1. Loading Functions¶
- static Graph.fromGMLString(s, name=None, add=True)¶
- static Graph.fromGMLFile(f, name=None, add=True)¶
Load a graph in GML format from a given string,
s
, or given filef
. The graph must be connected. If not, usefromGMLStringMulti()
orfromGMLFileMulti()
.- Parameters
- Returns
the loaded graph.
- Return type
- Raises
InputError
on bad input.
- static Graph.fromGMLStringMulti(s, add=True)¶
- static Graph.fromGMLFileMulti(f, add=True)¶
Load a set of graphs in GML format from a given string,
s
, or given filef
, with each graph being a connected component of the graph specified in the GML data.See
fromGMLString()
andfromGMLFile()
for a description of the parameters and exceptions.
- static Graph.fromDFS(s, name=None, add=True)¶
Load a graph from a GraphDFS string. The graph must be connected. If not, use
Graph.fromDFSMulti()
.- Parameters
name (str) – the name of the graph. If none is given the default name is used.
add (bool) – whether to append the graph to
inputGraphs
or not.
- Returns
the loaded graph.
- Return type
- Raises
InputError
on bad input.
- static Graph.fromDFSMulti(s, add=True)¶
Load a set of graphs from a GraphDFS string, with each graph being a connected component of the graph specified in the DFS data.
- Parameters
add (bool) – whether to append the graphs to
inputGraphs
or not.
- Returns
the loaded graphs.
- Return type
- Raises
InputError
on bad input.
- static Graph.fromSMILES(s, name=None, allowAbstract=False, classPolicy=SmilesClassPolicy.NoneOnDuplicate, add=True)¶
Load a molecule from a SMILES string. The molecule must be a connected graph. If not, use
fromSMILESMulti()
.- Parameters
- Returns
the loaded molecule.
- Return type
- Raises
InputError
on bad input.
- static Graph.fromSMILESMulti(s, allowAbstract=False, classPolicy=SmilesClassPolicy.NoneOnDuplicate, add=True)¶
Load a set of molecules from a SMILES string, with each molecule being a connected component of the graph specified in the SMILES string.
See
fromSMILES()
for a description of the parameters and exceptions.
- static Graph.fromMOLString(s, name=None, options=MDLOptions(), add=True)¶
- static Graph.fromMOLFile(f, name=None, options=MDLOptions(), add=True)¶
Load a molecule in MOL format from a given string or file. The molecule must be a connected graph. If not, use
fromMOLStringMulti()
andfromMOLFileMulti()
.- Parameters
s (str) – the string to parse.
name (str) – the name of the graph. If none is given the default name is used.
options (MDLOptions) – the options to use for loading.
add (bool) – whether to append the graph to
inputGraphs
or not.
- Returns
the loaded molecule.
- Return type
- Raises
InputError
on bad input.
- static Graph.fromMOLStringMulti(s, options=MDLOptions(), add=True)¶
- static Graph.fromMOLFileMulti(f, options=MDLOptions(), add=True)¶
Load a set of molecules from a given string or file with MOL data, with each molecule being a connected component of the graph specified in the data.
See
fromMOLString()
andfromMOLFile()
for a description of the parameters and exceptions.
- static Graph.fromSDString(s, options=MDLOptions(), add=True)¶
- static Graph.fromSDFile(f, options=MDLOptions(), add=True)¶
Load a list of molecules in SD format from a given string or file, with each molecule being a connected component of each of the the graphs specified in the data. If any graph is not connected, use
fromSDStringMulti()
andfromSDFileMulti()
instead.- Parameters
s (str) – the string to parse.
options (MDLOptions) – the options to use for loading.
add (bool) – whether to append the graphs to
inputGraphs
or not.
- Returns
a list of the loaded molecules.
- Return type
list of
Graph
- Raises
InputError
on bad input.
- static Graph.fromSDStringMulti(s, options=MDLOptions(), add=True)¶
- static Graph.fromSDFileMulti(f, options=MDLOptions(), add=True)¶
Load a list of molecules in SD format from a given string or file. Each molecule is returned as a list of graphs, with each corresponding to a connected component of the MOL entry.
See
fromSDString()
andfromSDFile()
for a description of the parameters and exceptions.
- mod.graphGMLString(s, name=None, add=True)¶
Alias of
Graph.fromGMLString()
.
- mod.graphGML(f, name=None, add=True)¶
Alias of
Graph.fromGMLFile()
.
- mod.graphDFS(s, name=None, add=True)¶
Alias of
Graph.fromDFS()
.
- mod.smiles(s, name=None, allowAbstract=False, classPolicy=SmilesClassPolicy.NoneOnDuplicate, add=True)¶
Alias of
Graph.fromSMILES()
.