$darkmode
DENOPTIM
ManySMARTSQuery.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.utils;
20
21import java.util.HashMap;
22import java.util.Map;
23
24import org.openscience.cdk.interfaces.IAtomContainer;
25import org.openscience.cdk.isomorphism.Mappings;
26import org.openscience.cdk.isomorphism.Pattern;
27import org.openscience.cdk.smarts.SmartsPattern;
28
29
36public class ManySMARTSQuery
37{
38 //Container for all matches
39 private Map<String,Mappings> allMatches = new HashMap<>();
40
41 //Counts for all matches
42 private Map<String,Integer> numMatches = new HashMap<>();
43
44 //Utils for detecting problems
45 private Throwable problem;
46 private boolean problems = false;
47 private String message = "";
48
49//------------------------------------------------------------------------------
50
51 public ManySMARTSQuery(IAtomContainer mol, Map<String, String> smarts) {
52 String err="";
53 try {
54 for (String smartsRef : smarts.keySet())
55 {
56 //get the new query
57 String oneSmarts = smarts.get(smartsRef);
58 err = smartsRef;
59
60 Pattern sp = SmartsPattern.create(oneSmarts);
61
62 // WARNING: assumptions on implicit H count and bond orders!
65
66 if (sp.matches(mol))
67 {
68 Mappings listOfIds = sp.matchAll(mol);
69 allMatches.put(smartsRef,listOfIds);
70 numMatches.put(smartsRef,listOfIds.count());
71 }
72 }
73 } catch (Throwable t) {
74 java.lang.StackTraceElement[] stes = t.getStackTrace();
75 String cause = "";
76 int s = stes.length;
77 if (s >= 1) {
78 java.lang.StackTraceElement ste = stes[0];
79 cause = ste.getClassName();
80 } else {
81 cause = "'unknown' (try to process this molecule alone to "
82 + "get more info)";
83 }
84 err = "WARNING! For query " + err + " => Exception returned "
85 + "by " + cause;
86 problems = true;
87 problem = t;
88 message = err;
89 }
90 }
91
92//------------------------------------------------------------------------------
93
94 public boolean hasProblems()
95 {
96 return problems;
97 }
98
99//------------------------------------------------------------------------------
100
101 public Throwable getProblem()
102 {
103 return problem;
104 }
105
106//------------------------------------------------------------------------------
107
108 public String getMessage()
109 {
110 return message;
111 }
112
113//------------------------------------------------------------------------------
114
115 public Map<String, Mappings> getAllMatches()
116 {
117 return allMatches;
118 }
119
120//------------------------------------------------------------------------------
121
122 public int getNumMatchesOfQuery(String query)
123 {
124 return numMatches.getOrDefault(query, 0);
125 }
126
127//------------------------------------------------------------------------------
128
129 public Mappings getMatchesOfSMARTS(String ref)
130 {
131 return allMatches.get(ref);
132 }
133
134//------------------------------------------------------------------------------
135
136}
Container of lists of atoms matching a list of SMARTS.
Map< String, Mappings > getAllMatches()
Mappings getMatchesOfSMARTS(String ref)
Map< String, Integer > numMatches
Map< String, Mappings > allMatches
ManySMARTSQuery(IAtomContainer mol, Map< String, String > smarts)
int getNumMatchesOfQuery(String query)
Utilities for molecule conversion.
static void setZeroImplicitHydrogensToAllAtoms(IAtomContainer iac)
Sets zero implicit hydrogen count to all atoms.
static void ensureNoUnsetBondOrdersSilent(IAtomContainer iac)
Sets bond order = single to all otherwise unset bonds.