Skip to content

Instantly share code, notes, and snippets.

View johnmay's full-sized avatar

John Mayfield johnmay

View GitHub Profile
class ChangeAtomSymbol extends AbstractUndoableEdit {
IAtomContainer container;
IAtom remove, replace;
ChangeAtomSymbol(IAtomContainer container,
IAtom original,
String newSymbol) {
Atom copy = new Atom(original); // do not clone!
// check for wide labels
boolean blunt = bounds.getWidth() / bounds.getHeight() > 3;
// ...
return new RoundRectangle2D.Double(x - (width/2), y - (height/2),
width, height,
blunt ? height : width, height);
@johnmay
johnmay / gist:4055585
Created November 11, 2012 17:21
transformPointDouble
public double[] transformPointDouble(double xCoord, double yCoord) {
double[] src = new double[] {xCoord, yCoord};
double[] dest = new double[2];
this.transform.transform(src, 0, dest, 0, 1);
return dest;
}
// ... used in the bounds calculation
double[] p = this.transformPointDouble(x, y);
@johnmay
johnmay / CustomChemObjectBuilder.java
Created November 15, 2012 09:40
CustomChemObjectBuilder
class CustomChemObjectBuilder implements IChemObjectBuilder {
private final DynamicFactory factory = new DynamicFactory(200);
public CustomChemObjectBuilder() {
// basic registration
factory.register(Element.class); // discovers Element is an instance of IElement, alternatively...
factory.register(IElement.class, Element.class); // registers Element with IElement explicitly
@johnmay
johnmay / current.java
Created November 16, 2012 10:21
Current/Proposed SMILES parsing
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
SmilesParser parser = new SmilesParser(builder);
IAtomContainer sodiumChloride = parser.parseSmiles("[Na+].[Cl-]");
IAtomContainer adenine = parser.parseSmiles("n1c(c2c(nc1)ncn2)N");
// if you want the separate parts
// import static org.openscience.cdk.graph.ConnectivityChecker.partitionIntoMolecules;
IAtomContainerSet sodiumChlorideSet = partitionIntoMolecules(sodiumChloride);
IAtomContainerSet adenineSet = partitionIntoMolecules(adenine);
@johnmay
johnmay / gist:4136504
Created November 23, 2012 17:11
AtomContainer Shallow Copy Behaviour
IAtomContainer a = new AtomContainer();
a.addAtom(new Atom("C"));
a.addAtom(new Atom("C"));
a.addBond(new Bond(a.getAtom(0), a.getAtom(1), SINGLE));
// copy constructor
IAtomContainer b = new AtomContainer(a);
b.removeAtom(0); // atom at index 0 is removed from b, a is not affected
b.getBond(0).setOrder(DOUBLE); // bond order is change in both a and b
@johnmay
johnmay / gist:4136821
Created November 23, 2012 18:50
AdjacencyList
public class AdjacencyList implements Graph {
private final int[][] vertexes;
private final IAtom[] atoms;
private final int n;
public AdjacencyList(final IAtomContainer container) {
this.n = container.getAtomCount();
@johnmay
johnmay / gist:4136826
Created November 23, 2012 18:53
Iteration
private int iterate(IAtomContainer container) {
int total = 0;
for (int d = 0; d < 8; d++) {
for (IAtom atom : container.atoms()) {
for (IBond bond : container.getConnectedBondsList(atom)) {
IAtom other = bond.getConnectedAtom(atom);
total += other.getAtomicNumber();
}
}
}
@johnmay
johnmay / gist:4155323
Created November 27, 2012 16:37
Number Rotation
public int rotate(int value, int n){
for(int i = 0; i < n; i++)
value = nextInt(value);
return value;
}
public int nextInt(int seed){
// return pseudo random number from the given seed
}
@johnmay
johnmay / gist:4155325
Created November 27, 2012 16:37
Number Rotation
public int rotate(int value, int n){
for(int i = 0; i < n; i++)
value = nextInt(value);
return value;
}
public int nextInt(int seed){
// return pseudo random number from the given seed
}