Last active
January 8, 2018 21:02
-
-
Save johnmay/84d49e537c31457d738c21ed6dec26ae to your computer and use it in GitHub Desktop.
Using CDK's RGroupQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws CDKException { | |
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance(); | |
SmilesParser smipar = new SmilesParser(bldr); | |
IAtomContainer root = smipar.parseSmiles("CC1=CC=NC2=C1C=CC(=C2)C1=CC([R1])=C([R2])C([R3])=C1"); | |
Map<IAtom, Map<Integer,IBond>> rootAttach = new HashMap<>(); | |
Map<Integer,RGroupList> rgrpMap = new HashMap<>(); | |
defineRgroup(root, rootAttach, rgrpMap, "R1", newRGroupList("[H].[CH2]CO.[CH2]Cl", 1)); | |
defineRgroup(root, rootAttach, rgrpMap, "R2", newRGroupList("[H].[CH2]CN.[CH2]F", 2)); | |
defineRgroup(root, rootAttach, rgrpMap, "R3", newRGroupList("[H].[CH2]CCl.[CH2]F", 3)); | |
RGroupQuery query = new RGroupQuery(bldr); | |
query.setRootStructure(root); | |
query.setRootAttachmentPoints(rootAttach); | |
query.setRGroupDefinitions(rgrpMap); | |
SmilesGenerator smigen = new SmilesGenerator(SmiFlavor.Isomeric); | |
for (IAtomContainer mol : query.getAllConfigurations()) { | |
System.out.println(smigen.create(mol)); | |
} | |
} | |
private static IAtom selectAtom(IAtomContainer mol, String label) { | |
for (IAtom atom : mol.atoms()) { | |
if (atom instanceof IPseudoAtom && label.equals(((IPseudoAtom) atom).getLabel())) | |
return atom; | |
} | |
return null; | |
} | |
private static IBond selectBond(IAtomContainer mol, IAtom atom) { | |
List<IBond> bonds = mol.getConnectedBondsList(atom); | |
if (bonds.size() != 1) | |
throw new IllegalArgumentException(); | |
return bonds.get(0); | |
} | |
private static RGroup newRGroup(IAtomContainer mol) { | |
RGroup rGroup = new RGroup(); | |
rGroup.setGroup(mol); | |
rGroup.setFirstAttachmentPoint(mol.getAtom(0)); | |
return rGroup; | |
} | |
private static List<RGroup> load(String smi) throws InvalidSmilesException { | |
SmilesParser smipar = new SmilesParser(SilentChemObjectBuilder.getInstance()); | |
IAtomContainer tmp = smipar.parseSmiles(smi); | |
return StreamSupport.stream(ConnectivityChecker.partitionIntoMolecules(tmp) | |
.atomContainers() | |
.spliterator(), false) | |
.map(Main::newRGroup).collect(Collectors.toList()); | |
} | |
private static RGroupList newRGroupList(String smi, int num) throws | |
CDKException { | |
RGroupList rlist = new RGroupList(num, false, "1", 0); | |
rlist.setRGroups(load(smi)); | |
return rlist; | |
} | |
private static void defineRgroup(IAtomContainer root, | |
Map<IAtom, Map<Integer, IBond>> attach, | |
Map<Integer, RGroupList> rgps, | |
String label, | |
RGroupList rGroupList) { | |
attach.put(atom(root, label), | |
Collections.singletonMap(1, selectBond(root, atom(root, label)))); | |
rgps.put(rGroupList.getRGroupNumber(), rGroupList); | |
} | |
private static IAtom atom(IAtomContainer root, String label) { | |
return selectAtom(root, label); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment