Skip to content

Instantly share code, notes, and snippets.

@johnmay
Created May 2, 2016 22:40
Show Gist options
  • Save johnmay/9c814049688cc905c806b9738212809b to your computer and use it in GitHub Desktop.
Save johnmay/9c814049688cc905c806b9738212809b to your computer and use it in GitHub Desktop.
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.isomorphism.Pattern;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesGenerator;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.smiles.smarts.SmartsPattern;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CreateAminoAcid {
private static final IChemObjectBuilder BLDR = SilentChemObjectBuilder.getInstance();
private static final Pattern AMINE_PATTERN;
private static final Pattern ACID_PATTERN;
static {
try {
AMINE_PATTERN = SmartsPattern.create("[NX3v3h2]", null);
ACID_PATTERN = SmartsPattern.create("[OX2v2H1][CX3v4]=[OX1v2H0]", null);
} catch (IOException ex) {
throw new InstantiationError("Bad SMARTS!" + ex.getMessage());
}
}
private static List<IAtomContainer> fuse(IAtomContainer amine, IAtomContainer acid) throws
CloneNotSupportedException {
// optional
AtomContainerManipulator.suppressHydrogens(amine);
AtomContainerManipulator.suppressHydrogens(acid);
List<IAtomContainer> result = new ArrayList<>();
int[][] amineHits = AMINE_PATTERN.matchAll(amine).uniqueAtoms().toArray();
int[][] acidHits = ACID_PATTERN.matchAll(acid).uniqueAtoms().toArray();
for (int[] amineHit : amineHits) {
for (int[] acidHit : acidHits) {
IAtomContainer fused = amine.clone();
fused.add(acid.clone());
IAtom nAtom = fused.getAtom(amineHit[0]);
IAtom oAtom = fused.getAtom(amine.getAtomCount() + acidHit[0]);
IAtom cAtom = fused.getAtom(amine.getAtomCount() + acidHit[1]);
// 1. remove bond and adjust N valence
// XXX: this can be much faster by moving to the back first and then deleting
fused.removeAtomAndConnectedElectronContainers(oAtom);
nAtom.setImplicitHydrogenCount(nAtom.getImplicitHydrogenCount()-1);
// This looks confusion but is essentially: 'new Bond(nAtom, cAtom, SINGLE)' using a factory
// (or builder as called here)
fused.addBond(BLDR.newInstance(IBond.class, nAtom, cAtom, IBond.Order.SINGLE));
result.add(fused);
}
}
return result;
}
public static void main(String[] args) throws CDKException, CloneNotSupportedException {
SmilesParser smipar = new SmilesParser(BLDR);
SmilesGenerator smigen = SmilesGenerator.isomeric();
// input list of amines/acids
List<String> amines = Arrays.asList("CC1=CC=CC(C)=C1N");
List<String> acids = Arrays.asList("CC(O)=O");
for (String amine : amines) {
for (String acid : acids) {
IAtomContainer amineMol = smipar.parseSmiles(amine);
IAtomContainer acidMol = smipar.parseSmiles(acid);
for (IAtomContainer fused : fuse(amineMol, acidMol)) {
System.out.println(smigen.create(fused));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment