Search in sources :

Example 1 with ListGraph

use of nl.uu.cs.treewidth.ngraph.ListGraph in project Smiles2Monomers by yoann-dufresne.

the class AtomContainerToNgraph method convert.

public NGraph<GraphInput.InputData> convert(IAtomContainer mol) {
    NGraph<GraphInput.InputData> g = new ListGraph<GraphInput.InputData>();
    int numVertices = mol.getAtomCount();
    Hashtable<String, NVertex<GraphInput.InputData>> vertices = new Hashtable<String, NVertex<GraphInput.InputData>>();
    boolean directGraph = false;
    NVertex<GraphInput.InputData> vertexPrototype = new ListVertex<GraphInput.InputData>();
    for (IBond bond : mol.bonds()) {
        String atom1 = String.valueOf(mol.getAtomNumber(bond.getAtom(0)));
        String atom2 = String.valueOf(mol.getAtomNumber(bond.getAtom(1)));
        NVertex<GraphInput.InputData> v1, v2;
        boolean newNode = false;
        // The first vertex
        if (!vertices.containsKey(atom1)) {
            v1 = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), atom1));
            vertices.put(v1.data.name, v1);
            g.addVertex(v1);
            newNode = true;
        } else {
            v1 = vertices.get(atom1);
        }
        // The second vertex
        if (!vertices.containsKey(atom2)) {
            v2 = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), atom2));
            vertices.put(v2.data.name, v2);
            g.addVertex(v2);
            newNode = true;
        } else {
            v2 = vertices.get(atom2);
        }
        boolean edgeExists = false;
        if (!newNode)
            edgeExists = v1.isNeighbor(v2);
        if (!edgeExists)
            g.addEdge(v1, v2);
        else
            directGraph = true;
    }
    if (directGraph)
        System.err.println("You have loaded a  multigraph. Duplicate edges have been removed!");
    int edgelessVertices = numVertices - vertices.size();
    if (edgelessVertices > 0) {
        System.err.println("There are " + edgelessVertices + " vertices which are not connected to other vertices");
    }
    while (numVertices > vertices.size()) {
        NVertex<GraphInput.InputData> v = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), "New_Vertex_" + vertices.size()));
        vertices.put(v.data.name, v);
        g.addVertex(v);
    }
    confirmProperIDs(g);
    return g;
}
Also used : Hashtable(java.util.Hashtable) GraphInput(nl.uu.cs.treewidth.input.GraphInput) IBond(org.openscience.cdk.interfaces.IBond) ListGraph(nl.uu.cs.treewidth.ngraph.ListGraph) NVertex(nl.uu.cs.treewidth.ngraph.NVertex) ListVertex(nl.uu.cs.treewidth.ngraph.ListVertex)

Aggregations

Hashtable (java.util.Hashtable)1 GraphInput (nl.uu.cs.treewidth.input.GraphInput)1 ListGraph (nl.uu.cs.treewidth.ngraph.ListGraph)1 ListVertex (nl.uu.cs.treewidth.ngraph.ListVertex)1 NVertex (nl.uu.cs.treewidth.ngraph.NVertex)1 IBond (org.openscience.cdk.interfaces.IBond)1