Search in sources :

Example 16 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.

the class ParsePDBe method main.

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    File directory = new File("/mnt/data/PDBe/mmcif");
    Map<String, JSONObject> names = new HashMap<>();
    JSONArray poly = new JSONArray();
    JSONArray mono = new JSONArray();
    JSONArray nonpoly = new JSONArray();
    for (File molfile : directory.listFiles()) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(molfile));
            JSONObject jso = new JSONObject();
            String line;
            while ((line = br.readLine()) != null) {
                if (line.startsWith("_chem_comp.pdbx_subcomponent_list")) {
                    if (line.contains("?")) {
                        mono.add(jso);
                        jso.put("desc", jso.get("name"));
                        jso.put("name", jso.get("id"));
                    } else {
                        // Graph spliting
                        String[] split = line.split("\"")[1].split(" ");
                        JSONObject graph = new JSONObject();
                        JSONArray v = new JSONArray();
                        graph.put("V", v);
                        JSONArray e = new JSONArray();
                        graph.put("E", e);
                        for (int i = 0; i < split.length; i++) {
                            v.add(split[i]);
                            if (i > 0) {
                                JSONArray bond = new JSONArray();
                                bond.add(i - 1);
                                bond.add(i);
                                e.add(bond);
                            }
                        }
                        jso.put("graph", graph);
                        // Add in polymers
                        poly.add(jso);
                    }
                } else if (line.startsWith("_chem_comp.id")) {
                    System.out.println(line);
                    line = line.replaceAll("_chem_comp.id", "");
                    line = line.replaceAll(" ", "").replaceAll("\t", "");
                    jso.put("id", line);
                    names.put(line, jso);
                } else if (line.startsWith("_chem_comp.name")) {
                    System.out.println(line);
                    line = line.replaceAll("_chem_comp.name", "");
                    line = line.replaceAll(" ", "").replaceAll("\t", "");
                    jso.put("name", line);
                    jso.put("desc", line);
                } else if (jso.containsKey("id") && line.startsWith((String) jso.get("id")) && line.contains("SMILES") && line.contains("ACDLabs")) {
                    System.out.println(line);
                    if (line.contains("\"")) {
                        String[] split = line.split("\"");
                        line = split[split.length - 2];
                    } else {
                        String[] split = line.split(" ");
                        line = split[split.length - 1];
                    }
                    try {
                        IMolecule mol = SmilesConverter.conv.transform(line);
                        if (ConnectivityChecker.isConnected(mol)) {
                            StructureDiagramGenerator sdg = new StructureDiagramGenerator(mol);
                            sdg.generateCoordinates();
                            jso.put("smiles", line);
                        }
                    } catch (InvalidSmilesException e) {
                    } catch (CDKException e) {
                    } catch (IllegalArgumentException e) {
                    }
                } else if (jso.containsKey("id") && line.startsWith((String) jso.get("id")) && line.contains("SMILES") && !jso.containsKey("smiles")) {
                    if (line.contains("\"")) {
                        String[] split = line.split("\"");
                        if (split.length > 1)
                            line = split[1];
                    } else {
                        String[] split = line.split(" ");
                        line = split[split.length - 1];
                    }
                    try {
                        IMolecule mol = SmilesConverter.conv.transform(line);
                        if (ConnectivityChecker.isConnected(mol)) {
                            StructureDiagramGenerator sdg = new StructureDiagramGenerator(mol);
                            sdg.generateCoordinates();
                            jso.put("smiles", line);
                        }
                    } catch (InvalidSmilesException e) {
                    } catch (CDKException e) {
                    } catch (IllegalArgumentException e) {
                    }
                } else if (line.startsWith("_chem_comp.type")) {
                    if (line.contains("NON-POLYMER"))
                        nonpoly.add(jso);
                }
            }
            if (!jso.containsKey("smiles") || jso.get("smiles") == null) {
                poly.remove(jso);
                mono.remove(jso);
                nonpoly.remove(jso);
                names.remove(jso);
                System.err.println(jso.toJSONString());
            }
            System.out.println();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    HashSet<String> needed = new HashSet<>();
    for (Object o : poly) {
        JSONObject p = (JSONObject) o;
        JSONObject graph = (JSONObject) p.get("graph");
        JSONArray v = (JSONArray) graph.get("V");
        for (Object o2 : v) {
            String name = (String) o2;
            needed.add(name);
        }
    }
    JSONArray minMonos = new JSONArray();
    for (String name : needed) {
        if (names.containsKey(name)) {
            minMonos.add(names.get(name));
            System.out.println(name + " added");
        } else {
            System.err.println("Impossible to add " + name);
        }
    }
    System.out.println(poly.size());
    System.out.println(mono.size());
    System.out.println(nonpoly.size());
    System.out.flush();
    // Save files
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/pdbe_monos.json"));
        bw.write(nonpoly.toJSONString());
        bw.close();
        bw = new BufferedWriter(new FileWriter("data/pdbe_polys.json"));
        bw.write(poly.toJSONString());
        bw.close();
        bw = new BufferedWriter(new FileWriter("data/pdbe_monos_extended.json"));
        bw.write(mono.toJSONString());
        bw.close();
        bw = new BufferedWriter(new FileWriter("data/pdbe_monos_min.json"));
        bw.write(minMonos.toJSONString());
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) CDKException(org.openscience.cdk.exception.CDKException) FileWriter(java.io.FileWriter) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator) BufferedWriter(java.io.BufferedWriter) JSONObject(org.json.simple.JSONObject) IMolecule(org.openscience.cdk.interfaces.IMolecule) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) JSONObject(org.json.simple.JSONObject) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) File(java.io.File) HashSet(java.util.HashSet)

Example 17 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.

the class PictureGenerator method createPNG.

/**
 * Create a PNG picture of molecule with width and height sizes
 * @param smiles SMILES of the molecule to be print
 * @param outfile Out file
 * @throws InvalidSmilesException
 */
public void createPNG(String smiles, File outfile) throws InvalidSmilesException {
    IMolecule molecule = this.transform(smiles);
    // the draw area and the image should be the same size
    Rectangle drawArea = new Rectangle(0, 0, SIZE, SIZE);
    this.renderer.setup(molecule, drawArea);
    Rectangle diagramBounds = this.renderer.calculateDiagramBounds(molecule);
    int width = diagramBounds.width;
    int height = diagramBounds.height;
    int diff = Math.abs(width - height);
    int max = width > height ? width : height;
    int xshift = width > height ? 0 : diff / 2;
    int yshift = width > height ? diff / 2 : 0;
    // Recenter image
    this.renderer.shiftDrawCenter(xshift - diagramBounds.x, yshift - diagramBounds.y);
    Image image = new BufferedImage(max, max, BufferedImage.TYPE_INT_RGB);
    // Drawing options
    this.model.set(BasicAtomGenerator.KekuleStructure.class, true);
    this.model.set(BasicAtomGenerator.ColorByType.class, true);
    // paint the background
    Graphics2D g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, max, max);
    // the paint method also needs a toolkit-specific renderer
    this.renderer.paint(molecule, new AWTDrawVisitor(g2));
    try {
        ImageIO.write((RenderedImage) image, "PNG", outfile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) BasicAtomGenerator(org.openscience.cdk.renderer.generators.BasicAtomGenerator) AWTDrawVisitor(org.openscience.cdk.renderer.visitor.AWTDrawVisitor) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 18 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.

the class PlanarityTest method main.

public static void main(String[] args) {
    String pepDBname = "datas/peptides.csv";
    // Loading databases
    PolymersJsonLoader pcl = new PolymersJsonLoader(new MonomersDB());
    PolymersDB pepDB = pcl.loadFile(pepDBname);
    // Tools
    Planarity pt = new Planarity();
    for (Polymer pep : pepDB.getObjects()) {
        System.out.println(pep.getName() + " : ");
        IMolecule mol = null;
        try {
            mol = SmilesConverter.conv.transform(pep.getSmiles());
        } catch (InvalidSmilesException e) {
            System.err.println("Impossible to parse " + pep.getName() + " id:" + pep.getId());
            continue;
        }
        SimpleGraph g = MoleculeGraphs.getMoleculeGraph(mol);
        System.out.println("planar : " + pt.isPlanar(g) + "\n");
    }
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) PolymersDB(db.PolymersDB) PolymersJsonLoader(io.loaders.json.PolymersJsonLoader) MonomersDB(db.MonomersDB) SimpleGraph(org._3pq.jgrapht.graph.SimpleGraph) Polymer(model.Polymer) Planarity(algorithms.Planarity) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException)

Example 19 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.

the class ResidueJsonLoader method fillLinksJSO.

@SuppressWarnings("unchecked")
private String fillLinksJSO(JSONArray links, Residue res) {
    IMolecule mol = res.getMolecule();
    AtomContainerManipulator.convertImplicitToExplicitHydrogens(mol);
    String smiles = SmilesConverter.conv.mol2Smiles(mol, false);
    List<IAtom> order = SmilesConverter.conv.getOrder();
    for (IAtom a : res.getAtomicLinks().keySet()) {
        JSONObject jso = new JSONObject();
        Rule rule = res.getAtomicLinks().get(a);
        jso.put("name", rule.getName());
        jso.put("atom", order.indexOf(a));
        links.add(jso);
    }
    AtomContainerManipulator.removeHydrogens(mol);
    return smiles;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) JSONObject(org.json.simple.JSONObject) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 20 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.

the class IsomorphismTests method MappingTest.

@Test
public void MappingTest() {
    List<MappedChain> mbs = Isomorphism.searchFromPreviousMapping(this.mb0, this.ext1, MatchingType.STRONG);
    boolean isGood = true;
    for (MappedChain mb : mbs) {
        List<MappedChain> extendedMbs = Isomorphism.searchFromPreviousMapping(mb, this.ext2, MatchingType.STRONG);
        MappedChain newMb = extendedMbs.get(0);
        IMolecule mol = mb.getChemObject().getMolecule();
        IAtom newA = (mol.getAtom(newMb.getAtomsMapping().get(2)));
        if (!(// Same first atom
        (newMb.getAtomsMapping().get(0) == mb.getAtomsMapping().get(0)) && // Same second atom
        (newMb.getAtomsMapping().get(1) == mb.getAtomsMapping().get(1)) && // Extended by C atom
        (newA.getSymbol().equals("C"))))
            isGood = false;
    }
    Assert.assertTrue(isGood);
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) IMolecule(org.openscience.cdk.interfaces.IMolecule) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Aggregations

IMolecule (org.openscience.cdk.interfaces.IMolecule)28 IAtom (org.openscience.cdk.interfaces.IAtom)12 ArrayList (java.util.ArrayList)7 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)7 Residue (model.Residue)6 IBond (org.openscience.cdk.interfaces.IBond)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 CDKException (org.openscience.cdk.exception.CDKException)5 MappedChain (algorithms.isomorphism.chains.MappedChain)4 Match (algorithms.utils.Match)4 HashMap (java.util.HashMap)4 Rule (model.Rule)3 JSONObject (org.json.simple.JSONObject)3 Chain (algorithms.isomorphism.chains.Chain)2 BondMapping (algorithms.isomorphism.chains.Extension.BondMapping)2 MonomersDB (db.MonomersDB)2 PolymersDB (db.PolymersDB)2 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)2 IOException (java.io.IOException)2