private void invertExistingDirectionalLabels(Graph g, BitSet visited, Map<Edge, Edge> replacement, int u) { visited.set(u); if (g.topologyOf(u) == null) return; for (Edge e : g.edges(u)) { int v = e.other(u); if (!visited.get(v)) { Edge f = replacement.get(e); if (f != null && f.bond().directional()) { replacement.put(e, f.inverse()); } else if (e.bond().directional()) { replacement.put(e, e.inverse()); } invertExistingDirectionalLabels(g, visited, replacement, v); } } }
/** * Given a double bond edge traverse the neighbors of both endpoints and accumulate any explicit * replacements in the 'acc' accumulator. * * @param g the chemical graph * @param e a edge in the graph ('double bond type') * @param acc accumulator for new edges * @throws InvalidSmilesException thrown if the edge could not be converted */ private Status replaceImplWithExpl(Graph g, Edge e, Map<Edge, Edge> acc) throws InvalidSmilesException { int u = e.either(), v = e.other(u); Status ustat = replaceImplWithExpl(g, e, u, acc); Status vstat = replaceImplWithExpl(g, e, v, acc); if (ustat == vstat) return ustat; else if (ustat == Status.INVALID && vstat != Status.WAITING) return Status.INVALID; else if (vstat == Status.INVALID && ustat != Status.WAITING) return Status.INVALID; else return Status.WAITING; }
altered = false; for (final Edge e : remain) { Status status = replaceImplWithExpl(g, e, replacements); if (status != Status.WAITING) { completed.add(e); for (int j = 0; j < d; ++j) { final Edge f = g.edgeAt(u, j); if (isDirectional(f, replacements) && safeToClean(g, f.other(u), replacements)) { replacements.put(f, new Edge(u, f.other(u), Bond.IMPLICIT)); for (int j = 0; j < d2; ++j) { final Edge f = g.edgeAt(v, j); if (isDirectional(f, replacements) && safeToClean(g, f.other(v), replacements)) replacements.put(f, new Edge(v, f.other(v), Bond.IMPLICIT));
boolean safeToClean(Graph g, int v, Map<Edge,Edge> replacements) { final int d = g.degree(v); for (int j = 0; j < d; ++j) { final Edge e = g.edgeAt(v, j); if (e.bond().order() == 2) { int w = e.other(v); final int d2 = g.degree(w); for (int j2 = 0; j2 < d2; ++j2) { final Edge f = g.edgeAt(w, j2); if (isDirectional(f, replacements)) return false; } } } return true; }
BitSet visited = new BitSet(); visited.set(u); invertExistingDirectionalLabels(g, visited, acc, f2 .other(u));