Search in sources :

Example 1 with CDKException

use of org.openscience.cdk.exception.CDKException in project MassBank-web by MassBank.

the class StructureToSvgStringGenerator method structureFromInChI.

public static IAtomContainer structureFromInChI(String inchi) {
    IAtomContainer mol = null;
    try {
        // get atom container
        InChIGeneratorFactory inchiFactory = InChIGeneratorFactory.getInstance();
        InChIToStructure inchi2structure = inchiFactory.getInChIToStructure(inchi, DefaultChemObjectBuilder.getInstance());
        mol = inchi2structure.getAtomContainer();
    } catch (CDKException e) {
        System.out.println("Warning: " + e.getLocalizedMessage());
    }
    return mol;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) InChIToStructure(org.openscience.cdk.inchi.InChIToStructure) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 2 with CDKException

use of org.openscience.cdk.exception.CDKException in project Smiles2Monomers by yoann-dufresne.

the class ResidueCreator method getResiduesWithRule.

/*
	 * Search Residues for one monomer and one rule.
	 */
private Set<Residue> getResiduesWithRule(Residue res, Rule rule) {
    Set<Residue> residues = new HashSet<>();
    IMolecule ruleMol = null;
    try {
        ruleMol = SmilesConverter.conv.transform(rule.getFormula(), false, false, true);
    } catch (InvalidSmilesException e) {
        System.err.println("Impossible to parse " + rule.getName() + " rule");
        return residues;
    }
    boolean status = false;
    try {
        status = UniversalIsomorphismTester.isSubgraph(res.getMolecule(), ruleMol);
    } catch (CDKException e) {
        e.printStackTrace();
    }
    // If rule is found
    if (status) {
        List<List<RMap>> matches = null;
        try {
            matches = UniversalIsomorphismTester.getSubgraphAtomsMaps(res.getMolecule(), ruleMol);
        } catch (CDKException e) {
            e.printStackTrace();
        }
        if (!"".equals(rule.getExclusion()))
            for (String exclusion : rule.getExclusion()) this.removeExclusions(matches, exclusion, res.getMolecule());
        for (List<RMap> match : matches) {
            Set<Residue> residuesByMatch = this.createResidue(match, rule, res);
            residues.addAll(residuesByMatch);
        }
    }
    return residues;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) CDKException(org.openscience.cdk.exception.CDKException) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) RMap(org.openscience.cdk.isomorphism.mcss.RMap) HashSet(java.util.HashSet)

Example 3 with CDKException

use of org.openscience.cdk.exception.CDKException in project MassBank-web by MassBank.

the class StructureToSvgStringGenerator method structureFromSMILES.

public static IAtomContainer structureFromSMILES(String smiles) {
    IAtomContainer mol = null;
    try {
        // get atom container
        SmilesParser smipar = new SmilesParser(DefaultChemObjectBuilder.getInstance());
        mol = smipar.parseSmiles(smiles);
    } catch (CDKException e) {
        System.out.println("Warning: " + e.getLocalizedMessage());
    }
    return mol;
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException)

Example 4 with CDKException

use of org.openscience.cdk.exception.CDKException 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 5 with CDKException

use of org.openscience.cdk.exception.CDKException in project Smiles2Monomers by yoann-dufresne.

the class PictureGenerator method transform.

/**/
public Molecule transform(String smiles) throws InvalidSmilesException {
    Molecule imol = null;
    imol = new Molecule(this.sp.parseSmiles(smiles));
    for (IAtom a : imol.atoms()) a.setImplicitHydrogenCount(0);
    StructureDiagramGenerator sdg = new StructureDiagramGenerator(imol);
    try {
        sdg.generateCoordinates();
    } catch (CDKException e) {
        System.err.println(smiles);
        e.printStackTrace();
    }
    imol = new Molecule(sdg.getMolecule());
    return imol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) IAtom(org.openscience.cdk.interfaces.IAtom) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator)

Aggregations

CDKException (org.openscience.cdk.exception.CDKException)11 IMolecule (org.openscience.cdk.interfaces.IMolecule)7 IAtom (org.openscience.cdk.interfaces.IAtom)5 ArrayList (java.util.ArrayList)4 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)4 HashSet (java.util.HashSet)3 List (java.util.List)3 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)3 StructureDiagramGenerator (org.openscience.cdk.layout.StructureDiagramGenerator)3 Residue (model.Residue)2 Molecule (org.openscience.cdk.Molecule)2 IBond (org.openscience.cdk.interfaces.IBond)2 RMap (org.openscience.cdk.isomorphism.mcss.RMap)2 Coverage (algorithms.utils.Coverage)1 Match (algorithms.utils.Match)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1