Search in sources :

Example 6 with InvalidSmilesException

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

the class FamilyDB method init.

public void init(MonomersDB monoDB) {
    Map<String, List<Monomer>> clusters = new HashMap<>();
    for (Monomer mono : monoDB.getObjects()) {
        String smiles = null;
        try {
            smiles = SmilesConverter.conv.toCanonicalSmiles(mono.getSmiles());
        } catch (InvalidSmilesException e) {
            System.err.println("Impossible to parse " + mono.getName() + " id:" + mono.getId());
            System.err.println(mono.getSmiles());
            continue;
        }
        List<Monomer> cluster = clusters.containsKey(smiles) ? clusters.get(smiles) : new ArrayList<Monomer>();
        cluster.add(mono);
        clusters.put(smiles, cluster);
    }
    for (String smiles : clusters.keySet()) {
        Family family = new Family();
        for (Monomer mono : clusters.get(smiles)) {
            family.addMonomer(mono);
            this.addObject(mono.getCode(), family);
        }
    }
}
Also used : HashMap(java.util.HashMap) Family(model.Family) List(java.util.List) ArrayList(java.util.ArrayList) Monomer(model.Monomer) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException)

Example 7 with InvalidSmilesException

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

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

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

the class PolymersTreeWidths method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("Command line : java main.PeptidesTreeWidths <peptidesFile> [outfile]");
        System.exit(-1);
    }
    // Parse arguments
    String pepDBname = args[0];
    BufferedWriter bw = null;
    if (args.length > 1) {
        File out = new File(args[1]);
        if (out.exists())
            out.delete();
        try {
            bw = new BufferedWriter(new FileWriter(out));
            bw.write("peptide;treewidth\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // Loading databases
    MonomersDB monoDB = new MonomersDB();
    PolymersJsonLoader pcl = new PolymersJsonLoader(monoDB);
    PolymersDB pepDB = pcl.loadFile(pepDBname);
    // Tools for treewidth
    MolTreeWidth mtw = new MolTreeWidth();
    for (Polymer pep : pepDB.getObjects()) {
        int tw;
        try {
            tw = mtw.calculateTreeWidth(SmilesConverter.conv.transform(pep.getSmiles()));
        } catch (InvalidSmilesException e) {
            System.err.println("Impossible to parse " + pep.getName() + " id:" + pep.getId());
            continue;
        }
        if (args.length > 1) {
            try {
                bw.write(pep.getName() + ";" + tw + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(pep.getName() + " : " + tw);
        }
    }
    if (args.length > 1) {
        try {
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : PolymersDB(db.PolymersDB) MolTreeWidth(nl.uu.cs.treewidth.MolTreeWidth) FileWriter(java.io.FileWriter) MonomersDB(db.MonomersDB) PolymersJsonLoader(io.loaders.json.PolymersJsonLoader) Polymer(model.Polymer) IOException(java.io.IOException) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 10 with InvalidSmilesException

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

the class SmilesConverter method transform.

public Molecule transform(String smiles, boolean addHydrogens, boolean calculateCoordinate, boolean expliciteHydrogens) throws InvalidSmilesException {
    Molecule imol = null;
    try {
        imol = new Molecule(sp.parseSmiles(smiles));
        // Add hydrogens
        if (addHydrogens) {
            CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(imol.getBuilder());
            try {
                adder.addImplicitHydrogens(imol);
                if (expliciteHydrogens)
                    AtomContainerManipulator.convertImplicitToExplicitHydrogens(imol);
            } catch (CDKException e) {
                e.printStackTrace();
            }
        } else {
            for (IAtom a : imol.atoms()) a.setImplicitHydrogenCount(0);
            if (!expliciteHydrogens)
                imol = new Molecule(AtomContainerManipulator.removeHydrogens(imol));
        }
        if (calculateCoordinate) {
            StructureDiagramGenerator sdg = new StructureDiagramGenerator(imol);
            try {
                sdg.generateCoordinates();
            } catch (CDKException e) {
                System.err.println(smiles);
                e.printStackTrace();
            }
            imol = new Molecule(sdg.getMolecule());
        }
    } catch (InvalidSmilesException e) {
        throw e;
    }
    return imol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) IAtom(org.openscience.cdk.interfaces.IAtom) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator)

Aggregations

InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)14 IMolecule (org.openscience.cdk.interfaces.IMolecule)8 Polymer (model.Polymer)4 Test (org.junit.Test)4 CDKException (org.openscience.cdk.exception.CDKException)4 MonomersDB (db.MonomersDB)3 PolymersDB (db.PolymersDB)3 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Monomer (model.Monomer)3 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Molecule (org.openscience.cdk.Molecule)2 RMap (org.openscience.cdk.isomorphism.mcss.RMap)2 StructureDiagramGenerator (org.openscience.cdk.layout.StructureDiagramGenerator)2