1 package search.db;
2
3 import java.sql.SQLException;
4
5 import resource.ResourceLocator;
6 import util.ConnectionUtil;
7 import util.TableOperations;
8 import chemaxon.formats.MolExporter;
9 import chemaxon.formats.MolImporter;
10import chemaxon.jchem.db.JChemSearch;
11import chemaxon.jchem.db.UpdateHandler;
12import chemaxon.sss.SearchConstants;
13import chemaxon.sss.search.JChemSearchOptions;
14import chemaxon.struc.Molecule;
15import chemaxon.util.ConnectionHandler;
16
17
22public final class DiverseSelectionExample {
23
24 private static final String DIVERSE_MOLECULES_TABLE = "diverse_mols";
25 private static final float DISSIM_THRESHOLD = 0.9f;
26
27 private ConnectionHandler connHandler;
28
29 public static void main(String[] args) {
30 try {
31 new DiverseSelectionExample().run();
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 }
36
37 private void run() throws Exception {
38 connHandler = ConnectionUtil.connectToDB();
39 try {
40 createDiverseMoleculeTable();
41 importDiverseMolecules();
42 } finally {
43 ConnectionUtil.closeConnection(connHandler);
44 }
45 }
46
47 private void createDiverseMoleculeTable() throws SQLException {
48 TableOperations.createMoleculeTable(connHandler, DIVERSE_MOLECULES_TABLE);
49 }
50
51
55 private void importDiverseMolecules() throws Exception {
56
57 int count = 0;
58 MolImporter imp = new MolImporter(ResourceLocator.getDefaultInputPath());
59 try {
60 Molecule newMol;
61 while ((newMol = imp.read()) != null) {
62 if (!similarMoleculeExistsInDB(newMol)) {
64 String smilesMol = MolExporter.exportToFormat(newMol, "smiles");
65 System.out.println("New representative found: " + smilesMol);
66 insertMoleculeIntoDB(smilesMol);
67 count++;
68 }
69 }
70 } finally {
71 imp.close();
72 }
73 System.out.println("Number of representatives: " + count);
74 }
75
76 private void insertMoleculeIntoDB(String smilesMolecule) throws SQLException {
77
78 UpdateHandler uh = new UpdateHandler(connHandler, UpdateHandler.INSERT,
79 DIVERSE_MOLECULES_TABLE, null);
80 try {
81 uh.setStructure(smilesMolecule);
82 uh.execute();
83 } finally {
84 uh.close();
85 }
86 }
87
88 private boolean similarMoleculeExistsInDB(Molecule mol) {
89 JChemSearchOptions searchOpts = new JChemSearchOptions(SearchConstants.SIMILARITY);
90 searchOpts.setDissimilarityThreshold(DISSIM_THRESHOLD);
91
92 JChemSearch jcs = new JChemSearch();
93 jcs.setConnectionHandler(connHandler);
94 jcs.setStructureTable(DIVERSE_MOLECULES_TABLE);
95 jcs.setQueryStructure(mol);
96 jcs.setSearchOptions(searchOpts);
97
98 try {
99 jcs.run();
00 } catch (Exception e) {
01 System.out.println("Unexpected error during DB search!");
02 e.printStackTrace();
03 }
04
05 return jcs.getResultCount() > 0;
06 }
07
08}
09