1 package search;
2 
3 import java.util.Arrays;
4 
5 import chemaxon.formats.MolImporter;
6 import chemaxon.sss.SearchConstants;
7 import chemaxon.sss.search.MolSearch;
8 import chemaxon.sss.search.MolSearchOptions;
9 import chemaxon.sss.search.StandardizedMolSearch;
10import chemaxon.struc.Molecule;
11
12/**
13 * Example code for demonstrating the usage of {@link MolSearch} class.
14 * 
15 * @author JChem Base team, ChemAxon Ltd.
16 */
17public final class MemorySearchExample {
18
19    private static final String QUERY = "CC";
20    private static final String TARGET = "C1CCCCC1";
21
22    public static void main(String[] agrs) {
23        try {
24            new MemorySearchExample().run();
25        } catch (Exception e) {
26            System.err.println("Unexpected error during search!");
27            e.printStackTrace();
28        }
29    }
30
31    private void run() throws Exception {
32
33        // Read query and target molecules
34        Molecule query = MolImporter.importMol(QUERY);
35        Molecule target = MolImporter.importMol(TARGET);
36
37        // Create searcher
38        MolSearch searcher = new StandardizedMolSearch();
39        searcher.setQuery(query);
40        searcher.setTarget(target);
41
42        // Create and set search options
43        MolSearchOptions options = new MolSearchOptions(SearchConstants.SUBSTRUCTURE);
44        searcher.setSearchOptions(options);
45
46        // Is substructure found?
47        System.out.println("Is query matching target?");
48        System.out.println(searcher.isMatching() ? "yes" : "no");
49
50        // Number of hits
51        System.out.println("Number of hits:");
52        System.out.println(searcher.getMatchCount());
53        printHitsWithFindNext(searcher);
54
55        // Order sensitive search
56        System.out.println("Number of order sensitive hits:");
57        options.setOrderSensitiveSearch(true);
58        searcher.setSearchOptions(options);
59        System.out.println(searcher.getMatchCount());
60        printHitsWithFindAll(searcher);
61    }
62
63    /**
64     * Prints the hits of the specified <code>molSearch</code> with demonstrating the usage of
65     * {@link MolSearch#findFirst()} and {@link MolSearch#findNext()} methods.
66     * 
67     * @param molSearch
68     * @throws Exception
69     */
70    private void printHitsWithFindNext(MolSearch molSearch) throws Exception {
71        int[] hit = molSearch.findFirst();
72        while (hit != null) {
73            System.out.println(Arrays.toString(hit));
74            hit = molSearch.findNext();
75        }
76    }
77
78    /**
79     * Prints the hits of the specified <code>molSearch</code> with demonstrating the usage of
80     * {@link MolSearch#findAll()} method.
81     * 
82     * @param molSearch
83     * @throws Exception
84     */
85    private void printHitsWithFindAll(MolSearch molSearch) throws Exception {
86        int[][] allHits = molSearch.findAll();
87        if (allHits == null) {
88            System.out.println("No hits has been found.");
89            return;
90        }
91        for (int[] currHit : allHits) {
92            System.out.println(Arrays.toString(currHit));
93        }
94    }
95
96}
97