Skip to content

Commit 29f3f83

Browse files
committed
Cleanup HOST descriptors.
1 parent 3e8bced commit 29f3f83

File tree

2 files changed

+62
-88
lines changed

2 files changed

+62
-88
lines changed

descriptor/qsaratomic/src/main/java/org/openscience/cdk/qsar/descriptors/atomic/IPAtomicHOSEDescriptor.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.openscience.cdk.qsar.DescriptorValue;
3838
import org.openscience.cdk.qsar.result.DoubleResult;
3939
import org.openscience.cdk.tools.HOSECodeGenerator;
40+
import org.openscience.cdk.tools.LoggingToolFactory;
4041
import org.openscience.cdk.tools.LonePairElectronChecker;
4142
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
4243

@@ -199,8 +200,8 @@ public Object getParameterType(String name) {
199200
*/
200201
private class IPdb {
201202

202-
HashMap<String, HashMap<String, Double>> listGroup = new HashMap<String, HashMap<String, Double>>();
203-
HashMap<String, HashMap<String, Double>> listGroupS = new HashMap<String, HashMap<String, Double>>();
203+
public static final String X_IP_HOSE_DB = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_IP_HOSE.db";
204+
public static final String X_IP_HOSE_DB_S = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_IP_HOSE_S.db";
204205

205206
/**
206207
* The constructor of the IPdb.
@@ -219,26 +220,22 @@ public IPdb() {
219220
*/
220221
public double extractIP(IAtomContainer container, IAtom atom) {
221222
// loading the files if they are not done
222-
String name = "";
223-
String nameS = "";
224-
HashMap<String, Double> hoseVSenergy = new HashMap<String, Double>();
225-
HashMap<String, Double> hoseVSenergyS = new HashMap<String, Double>();
223+
HashMap<String, Double> hoseVSenergy;
224+
HashMap<String, Double> hoseVSenergyS;
226225
if (familyHalogen(atom)) {
227-
name = "X_IP_HOSE.db";
228-
nameS = "X_IP_HOSE_S.db";
229-
if (listGroup.containsKey(name)) {
230-
hoseVSenergy = listGroup.get(name);
231-
hoseVSenergyS = listGroupS.get(nameS);
232-
} else {
233-
String path = "org/openscience/cdk/qsar/descriptors/atomic/data/" + name;
234-
String pathS = "org/openscience/cdk/qsar/descriptors/atomic/data/" + nameS;
235-
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(path);
236-
BufferedReader insr = new BufferedReader(new InputStreamReader(ins));
226+
try (InputStream ins = getClass().getResourceAsStream(X_IP_HOSE_DB);
227+
BufferedReader insr = new BufferedReader(new InputStreamReader(ins))) {
237228
hoseVSenergy = extractAttributes(insr);
238-
239-
ins = this.getClass().getClassLoader().getResourceAsStream(pathS);
240-
insr = new BufferedReader(new InputStreamReader(ins));
229+
} catch (IOException e) {
230+
LoggingToolFactory.createLoggingTool(getClass()).error(e);
231+
return 0;
232+
}
233+
try (InputStream ins = getClass().getResourceAsStream(X_IP_HOSE_DB_S);
234+
BufferedReader insr = new BufferedReader(new InputStreamReader(ins))) {
241235
hoseVSenergyS = extractAttributes(insr);
236+
} catch (IOException e) {
237+
LoggingToolFactory.createLoggingTool(getClass()).error(e);
238+
return 0;
242239
}
243240
} else
244241
return 0;
@@ -270,9 +267,9 @@ public double extractIP(IAtomContainer container, IAtom atom) {
270267
int sign = -1;
271268
if (plusMinus == 1) sign = 1;
272269

273-
StringTokenizer st = new StringTokenizer(hoseCode, "()/");
274-
StringBuffer hoseCodeBuffer = new StringBuffer();
275-
int sum = exactSphere + sign * (i + 1);
270+
StringTokenizer st = new StringTokenizer(hoseCode, "()/");
271+
StringBuilder hoseCodeBuffer = new StringBuilder();
272+
int sum = exactSphere + sign * (i + 1);
276273
for (int k = 0; k < sum; k++) {
277274
if (st.hasMoreTokens()) {
278275
String partcode = st.nextToken();
@@ -306,7 +303,7 @@ public double extractIP(IAtomContainer container, IAtom atom) {
306303
* @return HashMap with the Hose vs energy attributes
307304
*/
308305
private HashMap<String, Double> extractAttributes(BufferedReader input) {
309-
HashMap<String, Double> hoseVSenergy = new HashMap<String, Double>();
306+
HashMap<String, Double> hoseVSenergy = new HashMap<>();
310307
String line;
311308

312309
try {

descriptor/qsaratomic/src/main/java/org/openscience/cdk/qsar/descriptors/atomic/ProtonAffinityHOSEDescriptor.java

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
*/
1919
package org.openscience.cdk.qsar.descriptors.atomic;
2020

21-
import java.io.BufferedReader;
22-
import java.io.IOException;
23-
import java.io.InputStream;
24-
import java.io.InputStreamReader;
25-
import java.util.ArrayList;
26-
import java.util.HashMap;
27-
import java.util.List;
28-
import java.util.StringTokenizer;
29-
3021
import org.openscience.cdk.exception.CDKException;
3122
import org.openscience.cdk.interfaces.IAtom;
3223
import org.openscience.cdk.interfaces.IAtomContainer;
@@ -35,9 +26,19 @@
3526
import org.openscience.cdk.qsar.DescriptorValue;
3627
import org.openscience.cdk.qsar.result.DoubleResult;
3728
import org.openscience.cdk.tools.HOSECodeGenerator;
29+
import org.openscience.cdk.tools.LoggingToolFactory;
3830
import org.openscience.cdk.tools.LonePairElectronChecker;
3931
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
4032

33+
import java.io.BufferedReader;
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.InputStreamReader;
37+
import java.util.ArrayList;
38+
import java.util.HashMap;
39+
import java.util.List;
40+
import java.util.StringTokenizer;
41+
4142
/**
4243
* This class returns the proton affinity of an atom containing.
4344
*
@@ -117,7 +118,7 @@ public String[] getDescriptorNames() {
117118
*/
118119
@Override
119120
public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
120-
double value = 0;
121+
double value;
121122

122123
try {
123124
int i = container.indexOf(atom);
@@ -130,10 +131,7 @@ public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
130131
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);
131132
LonePairElectronChecker lpcheck = new LonePairElectronChecker();
132133
lpcheck.saturate(container);
133-
} catch (CDKException e) {
134-
return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
135-
Double.NaN), NAMES, null);
136-
} catch (CloneNotSupportedException e) {
134+
} catch (CDKException | CloneNotSupportedException e) {
137135
return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
138136
Double.NaN), NAMES, null);
139137
}
@@ -185,8 +183,8 @@ public Object getParameterType(String name) {
185183
*/
186184
private class Affinitydb {
187185

188-
HashMap<String, HashMap<String, Double>> listGroup = new HashMap<String, HashMap<String, Double>>();
189-
HashMap<String, HashMap<String, Double>> listGroupS = new HashMap<String, HashMap<String, Double>>();
186+
public static final String X_AFFI_PROTON_HOSE_DB = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_AffiProton_HOSE.db";
187+
public static final String X_AFFI_PROTON_HOSE_S_DB = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_AffiProton_HOSE_S.db";
190188

191189
/**
192190
* The constructor of the IPdb.
@@ -205,26 +203,23 @@ public Affinitydb() {
205203
*/
206204
public double extractAffinity(IAtomContainer container, IAtom atom) {
207205
// loading the files if they are not done
208-
String name = "";
209-
String nameS = "";
210-
HashMap<String, Double> hoseVSenergy = new HashMap<String, Double>();
211-
HashMap<String, Double> hoseVSenergyS = new HashMap<String, Double>();
206+
HashMap<String, Double> hoseVSenergy;
207+
HashMap<String, Double> hoseVSenergyS;
212208

213209
if (familyHalogen(atom)) {
214-
name = "X_AffiProton_HOSE.db";
215-
nameS = "X_AffiProton_HOSE_S.db";
216-
if (listGroup.containsKey(name)) {
217-
hoseVSenergy = listGroup.get(name);
218-
hoseVSenergyS = listGroupS.get(nameS);
219-
} else {
220-
String path = "org/openscience/cdk/qsar/descriptors/atomic/data/" + name;
221-
String pathS = "org/openscience/cdk/qsar/descriptors/atomic/data/" + nameS;
222-
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(path);
223-
BufferedReader insr = new BufferedReader(new InputStreamReader(ins));
210+
try (InputStream ins = getClass().getResourceAsStream(X_AFFI_PROTON_HOSE_DB);
211+
BufferedReader insr = new BufferedReader(new InputStreamReader(ins))) {
224212
hoseVSenergy = extractAttributes(insr);
225-
ins = this.getClass().getClassLoader().getResourceAsStream(pathS);
226-
insr = new BufferedReader(new InputStreamReader(ins));
213+
} catch (IOException e) {
214+
LoggingToolFactory.createLoggingTool(getClass()).error(e);
215+
return 0;
216+
}
217+
try (InputStream ins = getClass().getResourceAsStream(X_AFFI_PROTON_HOSE_S_DB);
218+
BufferedReader insr = new BufferedReader(new InputStreamReader(ins))) {
227219
hoseVSenergyS = extractAttributes(insr);
220+
} catch (IOException e) {
221+
LoggingToolFactory.createLoggingTool(getClass()).error(e);
222+
return 0;
228223
}
229224
} else
230225
return 0;
@@ -292,7 +287,7 @@ public double extractAffinity(IAtomContainer container, IAtom atom) {
292287
* @return HashMap with the Hose vs energy attributes
293288
*/
294289
private HashMap<String, Double> extractAttributes(BufferedReader input) {
295-
HashMap<String, Double> hoseVSenergy = new HashMap<String, Double>();
290+
HashMap<String, Double> hoseVSenergy = new HashMap<>();
296291
String line;
297292

298293
try {
@@ -316,37 +311,19 @@ private HashMap<String, Double> extractAttributes(BufferedReader input) {
316311
* @return List with String = HOSECode and String = energy
317312
*/
318313
private static List<String> extractInfo(String str) {
319-
320-
StringBuffer idEdited = new StringBuffer();
321-
StringBuffer valEdited = new StringBuffer();
322-
323-
int strlen = str.length();
324-
325-
boolean foundSpace = false;
326-
int countSpace = 0;
327-
boolean foundDigit = false;
328-
for (int i = 0; i < strlen; i++) {
329-
if (!foundDigit) if (Character.isLetter(str.charAt(i))) foundDigit = true;
330-
331-
if (foundDigit) {
332-
if (Character.isWhitespace(str.charAt(i))) {
333-
if (countSpace == 0) {
334-
foundSpace = true;
335-
} else
336-
break;
337-
} else {
338-
if (foundSpace) {
339-
valEdited.append(str.charAt(i));
340-
} else {
341-
idEdited.append(str.charAt(i));
342-
}
343-
}
344-
}
345-
}
346-
List<String> objec = new ArrayList<String>();
347-
objec.add(idEdited.toString());
348-
objec.add(valEdited.toString());
349-
return objec;
350-
314+
int beg = 0;
315+
int end = 0;
316+
int len = str.length();
317+
List<String> parts = new ArrayList<>();
318+
while (end < len && !Character.isSpaceChar(str.charAt(end)))
319+
end++;
320+
parts.add(str.substring(beg,end));
321+
while (end < len && Character.isSpaceChar(str.charAt(end)))
322+
end++;
323+
beg = end;
324+
while (end < len && !Character.isSpaceChar(str.charAt(end)))
325+
end++;
326+
parts.add(str.substring(beg,end));
327+
return parts;
351328
}
352329
}

0 commit comments

Comments
 (0)