Skip to content

Commit f6d5c5e

Browse files
committed
Fix bugs across aamtool, mechanism, and mapping packages
aamtool: - ChemicalFormatParser: replace System.exit(1) with proper exceptions - Annotator: fix copy-paste bug (reactant rank fetched product data) - ECRgroupFrequency: fix MDLRXNV2000Reader resource leak - MultiReactionContainer: add bounds checks for enzyme level splits - Helper: fix PrintWriter closing System.out in printHelp methods mechanism: - ReactionMechanismTool: fix NPE on null reactor in isMappingSolutionAcceptable(); fix regex bug split("$") -> split("\\$") - BondChangeCalculator: log swallowed CDKExceptions in kekulization - MatrixPrinter: fix FileWriter resource leak in write methods - AtomAtomMappingContainer: add null/bounds checks for atom IDs mapping: - BaseGameTheory: fix NPE on null getMessage(); move null check before dereference in refillMatrixWithNewData() - GameTheoryMax: log swallowed CloneNotSupportedException - GameTheoryMatrix: null-safe getID().trim() calls - CaseHandler: null guard for RING_CONNECTIONS property auto-unbox - GraphMatcher: null-safe atom ID comparison - GraphMatching: replace Runtime.exit(1) with RuntimeException All 40 tests pass. Author: Syed Asad Rahman <asad.rahman@bioinceptionlabs.com>
1 parent ac7c7f3 commit f6d5c5e

13 files changed

Lines changed: 52 additions & 27 deletions

File tree

src/main/java/uk/ac/ebi/reactionblast/mapping/algorithm/BaseGameTheory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public synchronized void UpdateMatrix(Holder mh, boolean removeHydrogen) throws
177177
}
178178
}
179179
} catch (Exception e) {
180-
LOGGER.error("Error in matching molecules, check Graph Matcher module! ", e.getMessage().toString());
180+
LOGGER.error("Error in matching molecules, check Graph Matcher module! ", e.getMessage());
181181
}
182182

183183
try {
@@ -240,7 +240,7 @@ public synchronized void UpdateMatrix(Collection<MCSSolution> mcsSolutions, Hold
240240
*/
241241
resetFLAGS(mh);
242242
} catch (Exception e) {
243-
LOGGER.error("Error in matching molecules, check Graph Matcher module! ", e.getMessage().toString());
243+
LOGGER.error("Error in matching molecules, check Graph Matcher module! ", e.getMessage());
244244
}
245245
}
246246

@@ -272,14 +272,14 @@ private synchronized void refillMatrixWithNewData(
272272

273273
MCSSolution atomatomMapping = getMappings(substrateIndex, productIndex, educt, product, mcsSolutions);
274274

275-
carbonCount = atomatomMapping.getAtomAtomMapping().getMappingsByAtoms().keySet().stream().filter((atom)
276-
-> (atom.getSymbol().equalsIgnoreCase("C"))).map((IAtom _item) -> 1.0).reduce(carbonCount, (accumulator, _item)
277-
-> accumulator + 1);
278-
279275
if (atomatomMapping == null) {
280276
throw new CDKException("atom-atom mapping is null");
281277
}
282278

279+
carbonCount = atomatomMapping.getAtomAtomMapping().getMappingsByAtoms().keySet().stream().filter((atom)
280+
-> (atom.getSymbol().equalsIgnoreCase("C"))).map((IAtom _item) -> 1.0).reduce(carbonCount, (accumulator, _item)
281+
-> accumulator + 1);
282+
283283
LOGGER.debug("set matching atoms");
284284
LOGGER.debug("Q " + educt.getID() + ": " + educt.getAtomCount());
285285
LOGGER.debug(", P " + product.getID() + ": " + product.getAtomCount());

src/main/java/uk/ac/ebi/reactionblast/mapping/algorithm/CaseHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ private boolean findAndChipBondBetweenRings(IAtomContainer container) {
306306
List<IBond> bond_to_be_removed = new ArrayList<>();
307307
for (IAtom atom : container.atoms()) {
308308
if (atom.getSymbol().equals("O") && !atom.isAromatic()) {
309-
int number_of_rings = ((Integer) atom.getProperty(RING_CONNECTIONS));
309+
Integer ringConns = atom.getProperty(RING_CONNECTIONS);
310+
int number_of_rings = ringConns != null ? ringConns : 0;
310311
LOGGER.debug("number_of_rings " + number_of_rings);
311312

312313
List<IBond> bonds = container.getConnectedBondsList(atom);

src/main/java/uk/ac/ebi/reactionblast/mapping/algorithm/GameTheoryMatrix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private synchronized void StoichiometricCoefficientReplicator_Structure_FingerPr
192192
for (int key = 0; key < ac.size(); key++) {
193193
try {
194194
IAtomContainer mol = ac.get(key).clone();
195-
String eductID = ac.get(key).getID().trim();
195+
String eductID = ac.get(key).getID() != null ? ac.get(key).getID().trim() : String.valueOf(key);
196196
mol.setID(eductID);
197197

198198
BitSet FP;
@@ -217,7 +217,7 @@ private synchronized void StoichiometricCoefficientReplicator_Structure_FingerPr
217217
for (int key = 0; key < pd.size(); key++) {
218218
try {
219219
IAtomContainer mol = pd.get(key).clone();
220-
String productID = pd.get(key).getID().trim();
220+
String productID = pd.get(key).getID() != null ? pd.get(key).getID().trim() : String.valueOf(key);
221221
mol.setID(productID);
222222

223223
BitSet fingerPrint;

src/main/java/uk/ac/ebi/reactionblast/mapping/algorithm/GameTheoryMax.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ private synchronized void UpdateMapping() throws Exception {
217217
CDKSMILES cdkSmiles = new CDKSMILES(mol, true, false);
218218
map.setMatchedSMILES(cdkSmiles.getCanonicalSMILES(), ++stepIndex);
219219
} catch (CloneNotSupportedException e) {
220+
LOGGER.error("Error in cloning molecule: ", e.getMessage());
220221
}
221222
});
222223
}

src/main/java/uk/ac/ebi/reactionblast/mapping/fixer/MappingReferenceResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void resolveReferences(IReaction reaction) {
7878
// System.out.println("now has " + reaction.getMappingCount() + " mappings");
7979
// clear and replace the mappings
8080
int count = reaction.getMappingCount();
81-
for (int i = count; i > 0; i--) {
81+
for (int i = count - 1; i >= 0; i--) {
8282
reaction.removeMapping(i);
8383
}
8484
// System.out.println("now has " + reaction.getMappingCount() + " mappings");

src/main/java/uk/ac/ebi/reactionblast/mapping/graph/GraphMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ private static IAtom getAtomByID(IAtomContainer ac, IAtom atom) {
415415
return null;
416416
}
417417
for (IAtom a : ac.atoms()) {
418-
if (a.getID().equals(atom.getID())) {
418+
if (atom.getID().equals(a.getID())) {
419419
return a;
420420
}
421421
}

src/main/java/uk/ac/ebi/reactionblast/mapping/graph/GraphMatching.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ public synchronized int removeMatchedAtomsAndUpdateAAM(IReaction reaction) {
196196
+ educt.getAtomCount() + " , " + product.getID() + " : " + product.getAtomCount()
197197
+ ", Mapping count: " + bestAtomMappingList.size() + "...atom ids did not matched!", ex);
198198

199-
Runtime.getRuntime().exit(1);
199+
throw new RuntimeException("Failed to remove matched parts between " + educt.getID() + ": "
200+
+ educt.getAtomCount() + " , " + product.getID() + " : " + product.getAtomCount()
201+
+ ", Mapping count: " + bestAtomMappingList.size() + "...atom ids did not matched!", ex);
200202
}
201203
}
202204
return delta;

src/main/java/uk/ac/ebi/reactionblast/mapping/helper/MappingHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MappingHandler extends BasicDebugger {
3939
*/
4040
public static void cleanMapping(IReaction MappedReaction) {
4141
int count = MappedReaction.getMappingCount();
42-
for (int i = count; i > 0; i--) {
42+
for (int i = count - 1; i >= 0; i--) {
4343
MappedReaction.removeMapping(i);
4444
}
4545

src/main/java/uk/ac/ebi/reactionblast/mechanism/BondChangeCalculator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ public IReaction getReactionWithCompressUnChangedHydrogens() {
733733
hAdder.addImplicitHydrogens(mol);
734734
kekulize(mol);
735735
} catch (CDKException ex) {
736+
LOGGER.error(SEVERE, "Failed to kekulize reactant molecule", ex);
736737
}
737738
}
738739
}
@@ -751,6 +752,7 @@ public IReaction getReactionWithCompressUnChangedHydrogens() {
751752
cdkHAdder.addImplicitHydrogens(mol);
752753
kekulize(mol);
753754
} catch (CDKException ex) {
755+
LOGGER.error(SEVERE, "Failed to kekulize product molecule", ex);
754756
}
755757
}
756758
}

src/main/java/uk/ac/ebi/reactionblast/mechanism/ReactionMechanismTool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ private synchronized boolean isMappingSolutionAcceptable(Reactor reactor,
352352
boolean generate2D,
353353
boolean generate3D
354354
) throws Exception {
355-
if (reactor.getMappingCount() > 500 && reactor.getMappingCount() < 1000) {
355+
if (reactor != null && reactor.getMappingCount() > 500 && reactor.getMappingCount() < 1000) {
356356
LOGGER.warn("wolla...are after something big?...so many atoms to compute bond changes!");
357357
LOGGER.warn("...Let me try..hold on your horses!");
358358
}
359-
if (reactor.getMappingCount() > 1000) {
359+
if (reactor != null && reactor.getMappingCount() > 1000) {
360360
LOGGER.warn("...wolla...are after something big?...!");
361361
LOGGER.warn("...This might drive me bit crazy ... have to compute so many atoms for bond changes...!");
362362
LOGGER.warn("...Let me try..hold on your horses!");
@@ -691,7 +691,7 @@ private synchronized int getTotalBondChangeEnergy(IPatternFingerprinter fingerpr
691691
total += val * energy;
692692
}
693693
} else if (key.contains("$")) {
694-
String[] temp = key.split("$");
694+
String[] temp = key.split("\\$");
695695
if (skipHydrogen && (temp[0].equals("H") || temp[1].equals("H"))) {
696696
continue;
697697
}

0 commit comments

Comments
 (0)