Skip to content

Commit

Permalink
Allow inorganic stereo to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Oct 21, 2024
1 parent cb2c588 commit b9c9d80
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>beam</artifactId>
<groupId>uk.ac.ebi.beam</groupId>
<version>1.3.7</version>
<version>1.3.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
131 changes: 127 additions & 4 deletions core/src/main/java/uk/ac/ebi/beam/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -195,13 +193,25 @@ public GraphBuilder doubleBond(int u, int v) {
* Start building a tetrahedral configuration.
*
* @param u the central atom
* @return a {@link TetrahedralBuilder} to create the stereo-configuration
* @return a {@link AtomStereoBuilder} to create the stereo-configuration
* from
*/
public TetrahedralBuilder tetrahedral(int u) {
return new TetrahedralBuilder(this, u);
}

/**
* Start building an atom stereo configuration (tetrahedral/square planar/
* octahedral/trigonal bipyramidal).
*
* @param u the central atom
* @return a {@link AtomStereoBuilder} to create the stereo-configuration
* from
*/
public AtomStereoBuilder atomStereo(int u, Configuration configuration) {
return new AtomStereoBuilder(this, u).config(configuration);
}

/** Start building the geometric configuration of the double bond 'u' / 'v'. */
public GeometricBuilder geometric(int u, int v) {
GeometricBuilder builder = new GeometricBuilder(this, u, v);
Expand Down Expand Up @@ -231,7 +241,7 @@ public GeometricBuilder extendedGeometric(int u, int v) {
/**
* (internal) Add a topology to the chemical graph. The topologies should be
* created using one of the configuration builders (e.g. {@link
* TetrahedralBuilder}).
* AtomStereoBuilder}).
*
* @param t the topology to add
*/
Expand Down Expand Up @@ -733,6 +743,119 @@ public GraphBuilder build() {
return gb;
}
}

/** @author John Mayfield */
public static final class AtomStereoBuilder {

/**
* Reference to the graph builder we came from - allows us to add the
* topology once the configuration as been built.
*/
final GraphBuilder gb;

/** Central/Focus vertex. */
final int u;

/** The vertex we are looking from. */
int v;

/** The other neighbors */
int[] vs;

/** The configuration of the other neighbors */
Configuration config;

/**
* (internal) - constructor for starting to configure atom stereo.
*
* @param gb the graph builder (where we came from)
* @param u the vertex to
*/
private AtomStereoBuilder(GraphBuilder gb,
int u) {
this.gb = gb;
this.u = u;
}

/**
* Indicate from which vertex the atom stereo. is being 'looked-at'.
*
* @param v the vertex from which we are looking from.
* @return tetrahedral builder for further configuration
*/
public AtomStereoBuilder lookingFrom(int v) {
this.v = v;
return this;
}

/**
* Indicate the other neighbors of atom stereo (excluding the vertex we
* are looking from). There should be exactly 3 neighbors.
*
* @param vs the neighbors
* @return tetrahedral builder for further configuration
* @throws IllegalArgumentException when there was not exactly 3
* neighbors
*/
public AtomStereoBuilder neighbors(int[] vs) {
this.vs = vs;
return this;
}

/**
* Indicate the other neighbors of atom stereo (excluding the vertex we
* are looking from).
*
* @param u a neighbor
* @param v another neighbor
* @param w another neighbor
* @return tetrahedral builder for further configuration
*/
public AtomStereoBuilder neighbors(int u, int v, int w) {
return neighbors(new int[]{u, v, w});
}

/**
* Specify the winding of the {@link #neighbors(int, int, int)}.
*
* @param c configuration
* @return tetrahedral builder for further configuration
*/
public AtomStereoBuilder config(Configuration c) {
this.config = c;
return this;
}

/**
* Finish configuring the atom stereo centre and add it to the graph.
*
* @return the graph-builder to add more atoms/bonds or stereo elements
* @throws IllegalArgumentException configuration was missing
*/
public GraphBuilder build() {
if (config == null)
throw new IllegalArgumentException("no configuration defined");
if (vs == null)
throw new IllegalArgumentException("no neighbors defined");
Topology t = null;
if (config.type() == Configuration.Type.Tetrahedral) {
if (vs.length != 3) throw new IllegalArgumentException("incorrect neighbour count");
t = Topology.tetrahedral(u, new int[]{v, vs[0], vs[1], vs[2]}, config);
} else if (config.type() == Configuration.Type.SquarePlanar) {
if (vs.length != 3) throw new IllegalArgumentException("incorrect neighbour count");
t = Topology.squarePlanar(u, new int[]{v, vs[0], vs[1], vs[2]}, config);
} else if (config.type() == Configuration.Type.TrigonalBipyramidal) {
if (vs.length != 4) throw new IllegalArgumentException("incorrect neighbour count");
t = Topology.trigonalBipyramidal(u, new int[]{v, vs[0], vs[1], vs[2], vs[3]}, config);
} else if (config.type() == Configuration.Type.Octahedral) {
if (vs.length != 5) throw new IllegalArgumentException("incorrect neighbour count");
t = Topology.octahedral(u, new int[]{v, vs[0], vs[1], vs[2], vs[3], vs[4]}, config);
} else
throw new IllegalArgumentException("Unimplemented config type: " + config);
gb.topology(u, t);
return gb;
}
}

/** @author John May */
public static final class ExtendedTetrahedralBuilder {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/uk/ac/ebi/beam/Topology.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static Topology squarePlanar(int u, int[] vs, Configuration configuration) {
}
}

private static Topology trigonalBipyramidal(int u, int[] vs, Configuration c) {
static Topology trigonalBipyramidal(int u, int[] vs, Configuration c) {
if (Configuration.TB1.ordinal() <= c.ordinal() &&
Configuration.TB20.ordinal() >= c.ordinal()) {
int order = 1 + c.ordinal() - Configuration.TB1.ordinal();
Expand All @@ -279,7 +279,7 @@ private static Topology trigonalBipyramidal(int u, int[] vs, Configuration c) {
return null;
}

private static Topology octahedral(int u, int[] vs, Configuration c) {
static Topology octahedral(int u, int[] vs, Configuration c) {
if (Configuration.OH1.ordinal() <= c.ordinal() &&
Configuration.OH30.ordinal() >= c.ordinal()) {
int order = 1 + c.ordinal() - Configuration.OH1.ordinal();
Expand Down
2 changes: 1 addition & 1 deletion exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>beam</artifactId>
<groupId>uk.ac.ebi.beam</groupId>
<version>1.3.7</version>
<version>1.3.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion func/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>beam</artifactId>
<groupId>uk.ac.ebi.beam</groupId>
<version>1.3.7</version>
<version>1.3.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<description>SMILES parsing and generation library for cheminformatics</description>
<url>http://www.github.com/johnmay/beam/</url>
<packaging>pom</packaging>
<version>1.3.7</version>
<version>1.3.8</version>
<modules>
<module>core</module>
<module>func</module>
Expand Down

0 comments on commit b9c9d80

Please sign in to comment.