4.1.9.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.

id

(Read-only) A unique instance id among Graph objects.

Type:

int

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:

AutGroup

print()
print(first, second=None)

Print the graph, using either the default options or the options in first and second. If first and second 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 to first.

Returns:

the names for the PDF-files that will be compiled in post-processing. If first and second are the same, the two file prefixes are equal.

Return type:

tuple[str, str]

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:

str

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:

str

Raises:

LogicError when coordinates are requested, but none can be generated.

name

The name of the graph. The default name includes the unique instance id.

Type:

str

smiles

(Read-only) If the graph models a molecule, this is the canonical SMILES string for it.

Type:

str

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:

str

Raises:

LogicError if the graph is not a molecule.

graphDFS

(Read-only) This is a GraphDFS of the graph.

Type:

str

graphDFSWithIds

(Read-only) This is a GraphDFS of the graph, where each vertex have an explicit id, corresponding to its internal vertex id.

Type:

str

linearEncoding

(Read-only) If the graph models a molecule this is the SMILES string string, otherwise it is the GraphDFS string.

Type:

str

isMolecule

(Read-only) Whether the graph models a molecule. See Molecule Encoding.

Type:

bool

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:

float

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:

float

Raises:

LogicError if it is not a molecule, including if some isotope has not been tabulated.

vLabelCount(label)
Parameters:

label (str) – some label for querying.

Returns:

the number of vertices in the graph with the given label.

Return type:

int

eLabelCount(label)
Parameters:

label (str) – some label for querying.

Returns:

the number of edges in the graph with the given label.

Return type:

int

isomorphism(codomain, maxNumMatches=1, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))
monomorphism(codomain, maxNumMatches=1, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))
Parameters:
  • codomain (Graph) – the codomain graph for finding morphisms.

  • maxNumMatches (int) – the maximum number of isomorphisms/monomorphisms to search for.

  • labelSettings (LabelSettings) – the label settings to use during the search.

Returns:

the number of isomorphisms/monomorphisms from this graph to other, but at most maxNumMatches.

Return type:

int

Raises:

LogicError – if codomain is null.

enumerateIsomorphisms(codomain, callback, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))
enumerateMonomorphisms(codomain, callback, labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism))

Perform (sub)structure search of this graph into the given codomain graph. Whenever a match is found, the corresponding isomorphism/monomorphism is copied into a vertex map and the given callback is invoked with it.

Parameters:
  • codomain (Graph) – the codomain graph for finding morphisms.

  • callback (Callable[[protocols.VertexMap], bool]) – the function to call with each found isomorphism/monomorphism. If False is returned from it, then the search is stopped.

  • labelSettings (LabelSettings) – the label settings to use during the search.

Raises:
makePermutation()
Returns:

a graph isomorphic to this, but with the vertex indices randomly permuted.

Return type:

Graph

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]

imageCommand

A command to be run in post-processing if a custom depiction is set. The command is only run once.

Type:

str

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.

Parameters:

id (int) – the external id to find the vertex descriptor for.

Returns:

the vertex descriptor for the given external id. The descriptor is null if the external id was not used.

Return type:

Vertex

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:

int

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:

List[Tuple[str, bool]]

4.1.9.2.1. Loading Functions

static Graph.fromGMLString(s, name=None, add=True, printStereoWarnings=True)
static Graph.fromGMLFile(f, name=None, add=True, printStereoWarnings=True)

Load a graph in GML format from a given string, s, or given file f. The graph must be connected. If not, use fromGMLStringMulti() or fromGMLFileMulti().

Parameters:
  • s (str) – the string with the GML data to load from.

  • f (str or CWDPath) – name of the GML file to be loaded.

  • 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.

  • printStereoWarnings (bool) – whether to print warnings due to unhandled stereo information.

Returns:

the loaded graph.

Return type:

Graph

Raises:

InputError on bad input.

static Graph.fromGMLStringMulti(s, add=True, printStereoWarnings=True)
static Graph.fromGMLFileMulti(f, add=True, printStereoWarnings=True)

Load a set of graphs in GML format from a given string, s, or given file f, with each graph being a connected component of the graph specified in the GML data.

See fromGMLString() and fromGMLFile() for a description of the parameters and exceptions.

Returns:

a list of the loaded graphs.

Return type:

list[Graph]

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:
  • s (str) – the GraphDFS string to parse.

  • 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:

Graph

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:
Returns:

the loaded graphs.

Return type:

list[Graph]

Raises:

InputError on bad input.

static Graph.fromSMILES(s, name=None, allowAbstract=False, classPolicy=SmilesClassPolicy.NoneOnDuplicate, add=True, printStereoWarnings=True)

Load a molecule from a SMILES string. The molecule must be a connected graph. If not, use fromSMILESMulti().

Parameters:
  • s (str) – the SMILES string to parse.

  • name (str) – the name of the graph. If none is given the default name is used.

  • allowAbstract (bool) – whether to allow abstract vertex labels in bracketed atoms.

  • add (bool) – whether to append the graph to inputGraphs or not.

  • printStereoWarnings (bool) – whether to print warnings due to unhandled stereo information.

Returns:

the loaded molecule.

Return type:

Graph

Raises:

InputError on bad input.

static Graph.fromSMILESMulti(s, allowAbstract=False, classPolicy=SmilesClassPolicy.NoneOnDuplicate, add=True, printStereoWarnings=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.

Returns:

a list of the loaded molecules.

Return type:

list[Graph]

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() and fromMOLFileMulti().

Parameters:
  • s (str) – the string to parse.

  • f (str or CWDPath) – name of the file to load.

  • 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:

Graph

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() and fromMOLFile() for a description of the parameters and exceptions.

Returns:

a list of the loaded molecules.

Return type:

list[Graph]

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 graphs specified in the data. If any graph is not connected, use fromSDStringMulti() and fromSDFileMulti() instead.

Parameters:
  • s (str) – the string to parse.

  • f (str or CWDPath) – name of the file to load.

  • 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() and fromSDFile() for a description of the parameters and exceptions.

Returns:

a list of lists of the loaded molecules. The items of the outer list correspond to each MOL entry in the SD data.

Return type:

list[list[Graph]]

mod.graphGMLString(...)

Alias of Graph.fromGMLString().

mod.graphGML(...)

Alias of Graph.fromGMLFile().

mod.graphDFS(...)

Alias of Graph.fromDFS().

mod.smiles(...)

Alias of Graph.fromSMILES().

mod.inputGraphs

A list of graphs to which explicitly loaded graphs as default are appended.

Type:

list[Graph]