Skip to content

Commit

Permalink
Update to v1.27.4
Browse files Browse the repository at this point in the history
* Add RDKitRuntimeExceptionHandler class to allow RDKit version before
4.1 or after 4.1 to work seemlessly
  • Loading branch information
sroughley committed Dec 1, 2020
1 parent 348435a commit ebc1d9f
Show file tree
Hide file tree
Showing 31 changed files with 2,281 additions and 1,818 deletions.
2 changes: 1 addition & 1 deletion com.vernalis.knime.chem.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: com.vernalis.knime.chem.core; singleton:=true
Bundle-Version: 1.27.3.qualifier
Bundle-Version: 1.27.4.qualifier
Bundle-ClassPath: vernalischemcore.jar
Bundle-Activator: com.vernalis.knime.chem.core.VernalisChemCoreNodePlugin
Bundle-Vendor: %Bundle-Vendor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*******************************************************************************
* Copyright (c) 2020, Vernalis (R&D) Ltd
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, Version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>
******************************************************************************/
package com.vernalis.knime.chem.rdkit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.RDKit.AtomKekulizeException;
import org.RDKit.AtomSanitizeException;
import org.RDKit.AtomValenceException;
import org.RDKit.ChemicalReactionException;
import org.RDKit.ChemicalReactionParserException;
import org.RDKit.ConformerException;
import org.RDKit.GenericRDKitException;
import org.RDKit.KekulizeException;
import org.RDKit.KeyErrorException;
import org.RDKit.MolPicklerException;
import org.RDKit.MolSanitizeException;
import org.RDKit.MolSanitizeException_Vect;
import org.RDKit.SmilesParseException;

/**
* This class wraps RDKit {@link RuntimeException}s and obtains the message via
* reflection from either the {@code message} or {@code what} methods. If
* neither method is found, it falls back on the standard {@link #getMessage()}
* method. The original 'cause' RDKit exception is available via
* {@link #getCause()}
*
* @author S.Roughley knime@vernalis.com
* @since com.vernalis.knime.chem.core 1.27.4
*/
@SuppressWarnings("serial")
public class RDKitRuntimeExceptionHandler extends RuntimeException {

// The following from RDKitExceptions.i
/**
* Constructor from {@link ChemicalReactionException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(ChemicalReactionException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link ChemicalReactionParserException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(ChemicalReactionParserException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link ConformerException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(ConformerException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link MolPicklerException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(MolPicklerException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link MolSanitizeException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(MolSanitizeException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link SmilesParseException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(SmilesParseException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link KeyErrorException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(KeyErrorException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link GenericRDKitException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(GenericRDKitException e) {
this((RuntimeException) e);
}

// The following subclasses of MolSanitizeException are defined in
// SanitException.i
/**
* Constructor from {@link AtomSanitizeException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(AtomSanitizeException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link AtomValenceException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(AtomValenceException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link AtomKekulizeException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(AtomKekulizeException e) {
this((RuntimeException) e);
}

/**
* Constructor from {@link KekulizeException}
*
* @param e
* The cause
*/
public RDKitRuntimeExceptionHandler(KekulizeException e) {
this((RuntimeException) e);
}

private RDKitRuntimeExceptionHandler(RuntimeException cause) {
super(getRDKitMessage(cause), cause);

}

/**
* @return The class of the initial RDKit Exception
*/
public Class<?> getRDKitExceptionClass() {
return getCause().getClass();
}

private static String getRDKitMessage(RuntimeException cause) {
Class<?> clazz = cause.getClass();
String msg = null;
Method mtd = null;
methodloop: for (Method m : clazz.getMethods()) {
switch (m.getName()) {
case "what":
case "message":
mtd = m;
break methodloop;
default:
// Nothing to do
}
}
if (mtd != null) {
mtd.setAccessible(true);
try {
msg = (String) mtd.invoke(cause);
} catch (ClassCastException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
// Do nothing
}
}
if (msg == null) {
msg = cause.getMessage();
}
return msg;
}

/**
* Static factory method to handle {@link MolSanitizeException_Vect}
*
* @param msev
* The cause vector
* @return A List of handlers for the individual
* {@link MolSanitizeException}s
*/
public static List<RDKitRuntimeExceptionHandler>
fromMolSanitizeExceptionVect(MolSanitizeException_Vect msev) {
List<RDKitRuntimeExceptionHandler> retVal = new ArrayList<>();
for (int i = 0; i < msev.size(); i++) {
retVal.add(new RDKitRuntimeExceptionHandler(msev.get(i)));
}
return retVal;
}
}
7 changes: 4 additions & 3 deletions com.vernalis.knime.chem.mmp/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: com.vernalis.knime.chem.mmp;singleton:=true
Bundle-Version: 1.27.3.qualifier
Bundle-Version: 1.27.4.qualifier
Bundle-ClassPath: matchedpairsmultiplecuts.jar
Bundle-Activator: com.vernalis.knime.mmp.MatchedPairsMultipleCutsNodePlugin
Bundle-Vendor: %Bundle-Vendor
Expand All @@ -12,11 +12,12 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.13.0,4.0.0)",
org.knime.base;bundle-version="[3.0.0,5.0.0)",
org.knime.chem.base;bundle-version="[3.0.0,5.0.0)",
org.knime.chem.types;bundle-version="[3.0.0,5.0.0)",
org.rdkit.knime.types;bundle-version="[4.1.0,5.0.0)",
org.rdkit.knime.types;bundle-version="[4.0.0,5.0.0)",
com.vernalis.knime.core;bundle-version="[1.5.0,2.0.0)",
org.eclipse.osgi;bundle-version="[3.12.0,4.0.0)",
org.knime.ext.svg;bundle-version="[3.0.0,5.0.0)",
com.vernalis.knime.chem.speedysmiles;bundle-version="[1.12.2,2.0.0)"
com.vernalis.knime.chem.speedysmiles;bundle-version="[1.12.2,2.0.0)",
com.vernalis.knime.chem.core;bundle-version="[1.27.4,2.0.0)"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: com.vernalis.knime.mmp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.RDKit.UInt_Vect;
import org.knime.core.node.NodeLogger;

import com.vernalis.knime.chem.rdkit.RDKitRuntimeExceptionHandler;
import com.vernalis.knime.mmp.MulticomponentSmilesFragmentParser;
import com.vernalis.knime.mmp.RDKitBondIdentifier;
import com.vernalis.knime.swiggc.SWIGObjectGarbageCollector;
Expand Down Expand Up @@ -1153,8 +1154,11 @@ private void assignAPChirality(RWMol component, int localGcWave)
RDKFuncs.assignStereochemistry(component, false, true, true);
} catch (MolSanitizeException e) {
if (verboseLogging) {
logger.info("Problem assigning double bond geometry"
+ e.what() == null ? "" : e.what());
String msg = new RDKitRuntimeExceptionHandler(e).getMessage();
logger.info(
"Problem assigning double bond geometry" + msg == null
? ""
: msg);
}
return;
} catch (Exception e) {
Expand Down Expand Up @@ -1237,10 +1241,10 @@ private void assignCreatedDblBondGeometry(RWMol component,
RDKFuncs.findPotentialStereoBonds(component, false);
System.out.println("Found potential stereobonds...");
} catch (MolSanitizeException e) {
if (verboseLogging) {
logger.info("Problem assigning double bond geometry"
+ e.what() == null ? "" : e.what());
}
String msg = new RDKitRuntimeExceptionHandler(e).getMessage();
logger.info(
"Problem assigning double bond geometry" + msg == null ? ""
: msg);
return;
} catch (Exception e) {
if (verboseLogging) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import org.RDKit.GenericRDKitException;
import org.RDKit.MolSanitizeException;
import org.RDKit.RDKFuncs;
import org.RDKit.ROMol;
Expand Down Expand Up @@ -53,6 +52,7 @@
import org.rdkit.knime.types.RDKitMolValue;

import com.vernalis.exceptions.RowExecutionException;
import com.vernalis.knime.chem.rdkit.RDKitRuntimeExceptionHandler;
import com.vernalis.knime.mmp.FragmentationTypes;
import com.vernalis.knime.mmp.MatchedPairsMultipleCutsNodePlugin;
import com.vernalis.knime.mmp.RDKitFragmentationUtils;
Expand Down Expand Up @@ -240,19 +240,11 @@ protected ROMol getROMolFromCell(DataCell cell)
} catch (MolSanitizeException e) {
// MolSanitizeException returns null for #getMessage()
throw new RowExecutionException("Error in sanitizing molecule: "
+ ((StringValue) cell).getStringValue() + " : " + e.what());
+ ((StringValue) cell).getStringValue() + " : "
+ new RDKitRuntimeExceptionHandler(e).getMessage());
} catch (Exception e) {
String msg = e.getMessage();
if (msg == null || "".equals(msg)) {
// Try to do something useful if we have a different RDKit
// Exception - at least try to report the error type!
msg = e.getClass().getSimpleName();
try {
msg += " : " + ((GenericRDKitException) e).what();
} catch (Exception e1) {
// Do nothing
}
}

if (msg.equals("Cell is not a recognised molecule type")) {
throw new RowExecutionException(msg);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;

import org.RDKit.GenericRDKitException;
import org.RDKit.MolSanitizeException;
import org.RDKit.RDKFuncs;
import org.RDKit.ROMol;
Expand Down Expand Up @@ -55,6 +54,7 @@
import org.rdkit.knime.types.RDKitMolValue;

import com.vernalis.exceptions.RowExecutionException;
import com.vernalis.knime.chem.rdkit.RDKitRuntimeExceptionHandler;
import com.vernalis.knime.mmp.FragmentationTypes;
import com.vernalis.knime.mmp.MatchedPairsMultipleCutsNodePlugin;
import com.vernalis.knime.mmp.RDKitFragmentationUtils;
Expand Down Expand Up @@ -361,19 +361,11 @@ protected static ROMol getROMolFromCell(DataCell cell)
} catch (MolSanitizeException e) {
// MolSanitizeException returns null for #getMessage()
throw new RowExecutionException("Error in sanitizing molecule: "
+ ((StringValue) cell).getStringValue() + " : " + e.what());
+ ((StringValue) cell).getStringValue() + " : "
+ new RDKitRuntimeExceptionHandler(e).getMessage());
} catch (Exception e) {
String msg = e.getMessage();
if (msg == null || "".equals(msg)) {
// Try to do something useful if we have a different RDKit
// Exception - at least try to report the error type!
msg = e.getClass().getSimpleName();
try {
msg += " : " + ((GenericRDKitException) e).what();
} catch (Exception e1) {
// Do nothing
}
}

if (msg.equals("Cell is not a recognised molecule type")) {
throw new RowExecutionException(msg);
} else {
Expand Down

0 comments on commit ebc1d9f

Please sign in to comment.