Skip to content

Instantly share code, notes, and snippets.

@gilleain
Created April 15, 2009 13:19
Show Gist options
  • Save gilleain/95758 to your computer and use it in GitHub Desktop.
Save gilleain/95758 to your computer and use it in GitHub Desktop.
package biojava;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Point3d;
import org.biojava.bio.structure.Atom;
import org.biojava.bio.structure.Chain;
import org.biojava.bio.structure.Group;
import org.biojava.bio.structure.Structure;
import org.biojava.bio.structure.io.PDBFileReader;
import org.openscience.cdk.config.AtomTypeFactory;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.graph.rebond.RebondTool;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesGenerator;
public class LigandExtractor {
private static IChemObjectBuilder builder =
NoNotificationChemObjectBuilder.getInstance();
public static List<IMolecule> extractLigands(String path) throws CDKException {
List<IMolecule> ligands = new ArrayList<IMolecule>();
RebondTool tool = new RebondTool(2.0, 0.5, 0.5);
try {
String code = new String(path).replace(".pdb", "");
PDBFileReader pdbreader = new PDBFileReader();
pdbreader.setPath(".");
pdbreader.setParseSecStruc(true);// parse the secondary structure
// information from PDB file
pdbreader.setAlignSeqRes(true); // align SEQRES and ATOM records
pdbreader.setAutoFetch(false); // fetch PDB files from web if they
// can't be found locally
Structure struc = pdbreader.getStructureById(code);
int modelnr = 0; // also is 0 if structure is an XRAY structure.
List<Chain> chains = struc.getChains(modelnr);
for (Chain cha : chains) {
List<Group> hgr = cha.getAtomGroups("hetatm");
LigandExtractor.makeLigandsFromGroups(hgr, ligands);
}
} catch (Exception e) {
e.printStackTrace();
}
AtomTypeFactory pdbFactory = AtomTypeFactory.getInstance(
"org/openscience/cdk/config/data/jmol_atomtypes.txt", builder);
for (IMolecule ligand : ligands) {
try {
for (IAtom atom : ligand.atoms()) {
pdbFactory.configure(atom);
}
tool.rebond(ligand);
} catch (Exception e) {
e.printStackTrace();
}
}
return ligands;
}
public static void makeLigandsFromGroups(
List<Group> groups, List<IMolecule> ligands) {
for (Group group : groups) {
IMolecule molecule = builder.newMolecule();
for (Atom atom : group.getAtoms()) {
String elementSymbol =
LigandExtractor.convertNameToElementSymbol(atom.getName());
Point3d p = new Point3d(atom.getX(), atom.getY(), atom.getZ());
molecule.addAtom(builder.newAtom(elementSymbol, p));
}
ligands.add(molecule);
}
}
public static String convertNameToElementSymbol(String atomName) {
return atomName.substring(0, 1);
}
public static void main(String[] args) {
try {
SmilesGenerator generator = new SmilesGenerator();
for (IMolecule ligand : new LigandExtractor().extractLigands("1hxb.pdb")) {
int i = ligand.getAtomCount();
if (i > 1) {
System.err.println(generator.createSMILES(ligand));
}
}
} catch (CDKException c) {
c.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment