/** * Given a chemical graph and an edge in that graph, return the explicit * form of that edge. Neither the graph or the edge is modified, if the edge * is already explicit then 'e' is returned. * * @param g chemical graph * @param e an edge of g * @return the edge with specified explicit bond type */ static Edge toExplicitEdge(final Graph g, final Edge e) { final int u = e.either(), v = e.other(u); if (e.bond() == Bond.IMPLICIT) { return new Edge(u, v, type(g.atom(u), g.atom(v))); } return e; }
static void renumber(Graph g) { int maxMapIdx = 0; for (int i = 0; i < g.order(); i++) maxMapIdx = Math.max(maxMapIdx, g.atom(i).atomClass()); if (maxMapIdx == 0) return; State state = new State(g, maxMapIdx); for (int i = 0; i < g.order(); i++) if (!state.visit[i]) traverse(state, i); } }
/** * Given a chemical graph and an edge in that graph, return the implicit * form of that edge. Neither the graph or the edge is modified, if the edge * is already explicit then 'e' is returned. * * @param g chemical graph * @param e an edge of g * @return the edge with specified explicit bond type */ static Edge toImplicitEdge(final Graph g, final Edge e) { final int u = e.either(), v = e.other(u); if (e.bond() == Bond.SINGLE || e.bond() == Bond.AROMATIC) { return new Edge(u, v, type(g.atom(u), g.atom(v), e.bond())); } return e; }
private static void traverse(State s, int idx) { s.visit[idx] = true; int mapIdx = s.g.atom(idx).atomClass(); if (mapIdx != 0) { if (s.map[mapIdx] == 0) s.map[mapIdx] = ++s.nMaps; mapIdx = s.map[mapIdx]; s.g.setAtom(idx, AtomBuilder.fromExisting(s.g.atom(idx)).atomClass(mapIdx).build()); } for (Edge e : s.g.edges(idx)) { int nbr = e.other(idx); if (!s.visit[nbr]) traverse(s, nbr); } }
public Graph apply(Graph g) { Graph h = new Graph(g.order()); for (int u = 0; u < g.order(); u++) { // only attempt subset conversion if no known topology Topology t = g.topologyOf(u); if (t.type() == None) { h.addAtom(toSubset(g.atom(u), g, u)); } else { h.addAtom(g.atom(u)); h.addTopology(t); } } // edges are unchanged for (Edge e : g.edges()) h.addEdge(e); return h; }
h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u));
public Graph apply(Graph g) { Graph h = new Graph(g.order()); for (int u = 0; u < g.order(); u++) { h.addAtom(fromSubset(g.atom(u), g.bondedValence(u), g.degree(u))); h.addTopology(g.topologyOf(u)); } // edges are unchanged for (Edge e : g.edges()) h.addEdge(e); return h; }
h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u));
@Test public void C13_nonIsomeric() throws Exception { IAtomContainer ac = new AtomContainer(); IAtom a = new Atom("C"); a.setMassNumber(13); ac.addAtom(a); Graph g = convert(ac, false, 0); // non-isomeric assertThat(g.atom(0).isotope(), is(-1)); assertThat(g.toSmiles(), is("C")); }
@Test public void oxidanide() throws Exception { IAtomContainer ac = new AtomContainer(); IAtom a = new Atom("O"); a.setFormalCharge(-1); ac.addAtom(a); Graph g = convert(ac, 0); assertThat(g.atom(0).charge(), is(-1)); assertThat(g.toSmiles(), is("[OH-]")); }
@Test public void oxidandiide() throws Exception { IAtomContainer ac = new AtomContainer(); IAtom a = new Atom("O"); a.setFormalCharge(-2); ac.addAtom(a); Graph g = convert(ac, 0); assertThat(g.atom(0).charge(), is(-2)); assertThat(g.toSmiles(), is("[O-2]")); }
@Test public void C13_isomeric() throws Exception { IAtomContainer ac = new AtomContainer(); IAtom a = new Atom("C"); a.setMassNumber(13); ac.addAtom(a); Graph g = convert(ac, SmiFlavor.AtomicMass); assertThat(g.atom(0).isotope(), is(13)); assertThat(g.toSmiles(), is("[13CH4]")); }
@Test public void azanium() throws Exception { IAtomContainer ac = new AtomContainer(); IAtom a = new Atom("N"); a.setFormalCharge(+1); ac.addAtom(a); Graph g = convert(ac, 0); assertThat(g.atom(0).charge(), is(+1)); assertThat(g.toSmiles(), is("[NH4+]")); }
private Atom asBracketAtom(int u, Graph g) { Atom a = g.atom(u); int sum = a.aromatic() ? 1 : 0; for (Edge e : g.edges(u)) { sum += e.bond().order(); } return new AtomImpl.BracketAtom(-1, a.element(), a.aromatic() ? a.element().aromaticImplicitHydrogens(sum) : a.element().implicitHydrogens(sum), 0, 0, a.aromatic()); }
@Override public Graph apply(Graph g) { Traversal traversal = new Traversal(g); Graph h = new Graph(g.order()); h.addFlags(g.getFlags(0xffffffff)); // copy atom/topology information this is unchanged for (int u = 0; u < g.order(); u++) { h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u)); } // change edges (only changed added to replacement) for (int u = 0; u < g.order(); u++) { final int d = g.degree(u); for (int j = 0; j < d; ++j) { final Edge e = g.edgeAt(u, j); if (e.other(u) > u) { if (traversal.acc.containsKey(e)) { h.addEdge(traversal.acc.get(e)); } else { h.addEdge(e); } } } } return h.sort(new Graph.CanOrderFirst()); }
/** * Transform all explicit to implicit bonds. The original graph is * unmodified. * * @param g a chemical graph * @return new chemical graph but with all explicit bonds */ public Graph apply(final Graph g) { Graph h = new Graph(g.order()); // atom/topology information doesn't change for (int u = 0; u < g.order(); u++) { h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u)); } // transform edges for (int u = 0; u < g.order(); u++) { for (final Edge e : g.edges(u)) { if (e.other(u) > u) h.addEdge(toImplicitEdge(g, e)); } } return h; }
public Graph apply(Graph g) { Graph h = new Graph(g.order()); // copy atom/topology information this is unchanged for (int u = 0; u < g.order(); u++) { if (g.topologyOf(u).type() == DoubleBond) { h.addAtom(reducedAtom(g, u)); } else { h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u)); } } Map<Edge, Edge> replacements = new Traversal(g).replacement; // append the edges, replacing any which need to be changed for (int u = 0; u < g.order(); u++) { for (Edge e : g.edges(u)) { if (e.other(u) > u) { Edge replacement = replacements.get(e); if (replacement != null) e = replacement; h.addEdge(e); } } } return h; }
/** * Transform all implicit to explicit bonds. The original graph is * unmodified * * @param g a chemical graph * @return new chemical graph but with all explicit bonds */ public Graph apply(final Graph g) { Graph h = new Graph(g.order()); // copy atom/topology information for (int u = 0; u < g.order(); u++) { h.addAtom(g.atom(u)); h.addTopology(g.topologyOf(u)); } // apply edges for (int u = 0; u < g.order(); u++) { for (final Edge e : g.edges(u)) { if (e.other(u) > u) h.addEdge(toExplicitEdge(g, e)); } } return h; }
Atom a = g.atom(u); if (a.subset() && h.topologyOf(u) != Topology.unknown()) { h.addAtom(asBracketAtom(u, g));