2.2.12.3. graph/Graph.hpp

2.2.12.3.1. Class graph::Graph

class graph::Graph

This class models an undirected graph with labels on vertices and edges, without loops and without parallel edges. Certain labels are regarded as models of chemical atoms and bonds. See Molecule Encoding for more information on this. See also graph/GraphInterface.hpp for the documentation for the graph interface for this class.

2.2.12.3.1.1. Synopsis

struct MOD_DECL Graph {
    class Vertex;
    class Edge;
    class VertexIterator;
    class VertexRange;
    class EdgeIterator;
    class EdgeRange;
    class IncidentEdgeIterator;
    class IncidentEdgeRange;
public:
    struct Aut;
    struct AutGroup;
    std::string printStereo() const;
    std::string printStereo(const Printer &p) const;
private: // The actual class interface
    Graph(std::unique_ptr<lib::Graph::Single> g);
    Graph(const Graph&) = delete;
    Graph &operator=(const Graph&) = delete;
public:
    ~Graph();
    std::size_t getId() const;
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const Graph &g);
    lib::Graph::Single &getGraph() const;
public: // graph interface
    std::size_t numVertices() const;
    VertexRange vertices() const;
    std::size_t numEdges() const;
    EdgeRange edges() const;
public:
    AutGroup aut(LabelSettings labelSettings) const;
public:
    std::pair<std::string, std::string> print() const;
    std::pair<std::string, std::string> print(const Printer &first, const Printer &second) const;
    void printTermState() const;
    std::string getGMLString(bool withCoords = false) const;
    std::string printGML(bool withCoords = false) const;
    const std::string &getName() const;
    void setName(std::string name) const;
    const std::string &getSmiles() const;
    const std::string &getSmilesWithIds() const;
    const std::string &getGraphDFS() const;
    const std::string &getGraphDFSWithIds() const;
    const std::string &getLinearEncoding() const;
    bool getIsMolecule() const;
    double getEnergy() const;
    void cacheEnergy(double value) const;
    double getExactMass() const;
    unsigned int vLabelCount(const std::string &label) const;
    unsigned int eLabelCount(const std::string &label) const;
    std::size_t isomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches, LabelSettings labelSettings) const;
    std::size_t monomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches, LabelSettings labelSettings) const;
    std::shared_ptr<Graph> makePermutation() const;
    void setImage(std::shared_ptr<Function<std::string()> > image);
    std::shared_ptr<Function<std::string()> > getImage() const;
    void setImageCommand(std::string cmd);
    std::string getImageCommand() const;
    Vertex getVertexFromExternalId(int id) const;
    int getMinExternalId() const;
    int getMaxExternalId() const;
    void instantiateStereo() const;
private:
    struct Pimpl;
    std::unique_ptr<Pimpl> p;
public:
    static std::shared_ptr<Graph> graphGMLString(const std::string &data);
    static std::shared_ptr<Graph> graphGML(const std::string &file);
    static std::shared_ptr<Graph> graphDFS(const std::string &graphDFS);
    static std::shared_ptr<Graph> smiles(const std::string &smiles);
    static std::shared_ptr<Graph> smiles(const std::string &smiles, bool allowAbstract, SmilesClassPolicy classPolicy);
    static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g);
    static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g, std::map<int, std::size_t> externalToInternalIds);
};

2.2.12.3.1.2. Details

std::string printStereo() const
std::string printStereo(const Printer &p) const

Print the stereo configuration for the vertex.

Returns

the name of the PDF-file that will be compiled in post-processing.

std::size_t getId() const
Returns

the unique instance id among Graph objects.

std::size_t numVertices() const
Returns

the number of vertices in the graph.

VertexRange vertices() const
Returns

a range of all vertices in the graph.

std::size_t numEdges() const
Returns

the number of edges in the graph.

EdgeRange edges() const
Returns

a range of all edges in the graph.

AutGroup aut(LabelSettings labelSettings) const
Returns

an object representing the automorphism group of the graph.

std::pair<std::string, std::string> print() const
std::pair<std::string, std::string> print(const Printer &first, const Printer &second) const

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.

Returns

a pair of 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.

void printTermState() const

Print the term state for the graph.

std::string getGMLString(bool withCoords = false) const
Returns

the GML representation of the graph, optionally with generated 2D coordinates.

Throws

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

std::string printGML(bool withCoords = false) const

Print the GML representation of the graph, optionally with generated 2D coordinates.

Returns

the filename of the printed GML file.

Throws

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

const std::string &getName() const
void setName(std::string name) const

Access the name of the graph.

const std::string &getSmiles() const
Returns

the canonical SMILES string of the graph, if it’s a molecule.

Throws

LogicError if the graph is not a molecule.

const std::string &getSmilesWithIds() const
Returns

the canonical SMILES string of the graph, that includes the internal vertex id as a class label on each atom, if it’s a molecule.

Throws

LogicError if the graph is not a molecule.

const std::string &getGraphDFS() const
Returns

a GraphDFS string of the graph.

const std::string &getGraphDFSWithIds() const
Returns

a GraphDFS string of the graph, where each vertices have an explicit id, corresponding to its internal vertex id.

const std::string &getLinearEncoding() const
Returns

the SMILES string if the graph is a molecule, otherwise the GraphDFS string.

bool getIsMolecule() const
Returns

whether or not the graph models a molecule. See Molecule Encoding.

double getEnergy() const
Returns

some energy value if the graph is a molecule. The energy is calculated using Open Babel, unless already calculated or cached by Graph::cacheEnergy().

void cacheEnergy(double value) const

If the graph models a molecule, sets the energy to a given value.

Throws

LogicError if the graph is not a molecule.

double getExactMass() const
Returns

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.

Throws

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

unsigned int vLabelCount(const std::string &label) const
Returns

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

unsigned int eLabelCount(const std::string &label) const
Returns

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

std::size_t isomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches, LabelSettings labelSettings) const
Returns

the number of isomorphisms found from this graph to g, but at most maxNumMatches.

std::size_t monomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches, LabelSettings labelSettings) const
Returns

the number of monomorphisms from this graph to g, though at most maxNumMatches.

std::shared_ptr<Graph> makePermutation() const
Returns

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

void setImage(std::shared_ptr<Function<std::string()>> image)

Set 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. Give nullptr to use auto-generated depiction.

std::shared_ptr<Function<std::string()>> getImage() const
Returns

the current custom depiction file function.

void setImageCommand(std::string cmd)

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

std::string getImageCommand() const
Returns

the current post-processing command.

Vertex getVertexFromExternalId(int id) const

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.

Returns

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

int getMinExternalId() const
int getMaxExternalId() const

If the graph was not loaded from an external data format, then these functions always return 0. Otherwise, return the minimum/maximum external id from which non-null vertices can be obtained from getVertexFromExternalId(). If no such minimum and maximum exists, then 0 will be returned.

void instantiateStereo() const

Make sure that stereo data is instantiated.

Throws

StereoDeductionError if the data was not instantiated and deduction failed.

static std::shared_ptr<Graph> graphGMLString(const std::string &data)
Returns

a graph created from the given GML data.

Throws

InputError on bad input.

static std::shared_ptr<Graph> graphGML(const std::string &file)
Returns

a graph loaded from the given GML file.

Throws

InputError on bad input.

static std::shared_ptr<Graph> graphDFS(const std::string &graphDFS)
Returns

a graph loaded from the given GraphDFS string.

Throws

InputError on bad input.

static std::shared_ptr<Graph> smiles(const std::string &smiles)
static std::shared_ptr<Graph> smiles(const std::string &smiles, bool allowAbstract, SmilesClassPolicy classPolicy)
Returns

a graph representing a molecule, loaded from the given SMILES string. The allowAbstract argument defaults to false and classPolicy defaults to SmilesClassPolicy::NoneOnDuplicate.

Throws

InputError on bad input.

Throws

InputError if classPolicy == SmilesClassPolicy::NoneOnDuplicate and a class label is duplicated.

static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g)
static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g, std::map<int, std::size_t> externalToInternalIds)
Returns

a graph wrapping the given internal graph object. If an id mapping is given, it will be used for the getVertexFromExternalId() function.