Skip to content

Instantly share code, notes, and snippets.

View johnmay's full-sized avatar

John Mayfield johnmay

View GitHub Profile
@johnmay
johnmay / gist:3637359
Created September 5, 2012 14:19
CDK IteratingMDLReader with skip
Reader reader = new BufferedReader(new FileReader(new File("ChEBI_lite.sdf")),
2048);
// instantiate the iterating reader with skip=true in the constructor
IteratingMDLReader sdf = new IteratingMDLReader(reader,
SilentChemObjectBuilder.getInstance(),
Boolean.TRUE);
while (sdf.hasNext()){
IAtomContainer container = sdf.next();
@johnmay
johnmay / ServiceDemo.java
Created September 5, 2012 14:35
MDK Apps Service
package demo;
import org.openscience.cdk.interfaces.IAtomContainer;
import uk.ac.ebi.mdk.domain.identifier.HMDBIdentifier;
import uk.ac.ebi.mdk.service.DefaultServiceManager;
import uk.ac.ebi.mdk.service.ServiceManager;
import uk.ac.ebi.mdk.service.query.data.MolecularFormulaService;
import uk.ac.ebi.mdk.service.query.name.IUPACNameService;
import uk.ac.ebi.mdk.service.query.name.PreferredNameService;
import uk.ac.ebi.mdk.service.query.name.SynonymService;
@johnmay
johnmay / gist:3741888
Created September 18, 2012 07:56
CDK iterators
IAtomContainer container = ...
Iterator<IAtom> it = container.atoms();
while(it.hasNext()){
IAtom atom = it.next();
if("H".equals(atom.getSymbol())){
it.remove(); // invokes container.removeBond(IBond);
}
}
public List<IAtom> getAtoms() {
return Collections.unmodifiableList(atoms);
}
@johnmay
johnmay / gist:3810816
Created October 1, 2012 10:33
Aromaticity Detection Interface
public interface AromaticityDetector {
public void detect(IAtomContainer container);
}
@johnmay
johnmay / gist:3981487
Created October 30, 2012 16:52
getTextBounds(String, double, double, Graphics2D)
public Rectangle2D getTextBounds(String text, double x, double y,
Graphics2D graphics) {
FontMetrics fontMetrics = graphics.getFontMetrics();
Rectangle2D bounds = fontMetrics.getStringBounds(text, graphics);
double widthPad = 3;
double heightPad = 1;
double width = bounds.getWidth() + widthPad;
@johnmay
johnmay / gist:3981529
Created October 30, 2012 16:59
TextLayout
TextLayout layout = new TextLayout(text, graphics.getFont(),
graphics.getFontRenderContext());
Rectangle2D bounds = layout.getBounds();
@johnmay
johnmay / gist:4017856
Created November 5, 2012 15:41
CDK SVG Rendering with Batik
DOMImplementation impl = GenericDOMImplementation.getDOMImplementation();
Document document = impl.createDocument("http://www.w3.org/2000/svg",
"svg",
null);
// SVGGraphics2D implements java.awt.Graphics2D
SVGGeneratorContext context = SVGGeneratorContext.createDefault(document)
SVGGraphics2D g2 = new SVGGraphics2D(context,
true); // convert fonts to SVG Font glyphs
// render
@johnmay
johnmay / gist:4026441
Created November 6, 2012 18:04
Padding text bounds
double width = bounds.getWidth() + bounds.getHeight() * PADDING_RATIO;
double height = bounds.getHeight() + bounds.getHeight() * PADDING_RATIO;
@johnmay
johnmay / gist:4046794
Created November 9, 2012 16:50
Building the HashGenerator<?>
HashGenerator<Integer> generator = new HashGeneratorMaker().withDepth(8)
.charged()
.chiral()
.isotopic()
.radicals()
.withBondOrderSum()
.nullable()
.build();
// integer representation
Integer hashcode = generator.generate(container);