Skip to content

Commit

Permalink
Better excpetion handling in builder3d:
Browse files Browse the repository at this point in the history
* removed unneeded throw clauses
* use CDKException and NoSuchAtomTypeException instead of the
  uninformative general Exception

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
Stefan Kuhn authored and egonw committed Jun 9, 2009
1 parent acc8012 commit bc5837d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
Expand Up @@ -111,9 +111,8 @@ public IAtomContainer markPlaced(IAtomContainer ac) {
* Method assigns 3Dcoordinates to the heavy atoms in an aliphatic chain
*
* @param chain the atoms to be assigned, must be connected
* @exception Exception Description of the Exception
*/
public void placeAliphaticHeavyChain(IAtomContainer molecule, IAtomContainer chain) throws Exception {
public void placeAliphaticHeavyChain(IAtomContainer molecule, IAtomContainer chain) throws CDKException{
//logger.debug("******** Place aliphatic Chain *********");
int[] first = new int[2];
int counter = 1;
Expand Down Expand Up @@ -171,7 +170,7 @@ public void placeAliphaticHeavyChain(IAtomContainer molecule, IAtomContainer cha
==5){
dihedrals[counter] = DIHEDRAL_BRANCHED_CHAIN;
}else{ dihedrals[counter] = DIHEDRAL_EXTENDED_CHAIN;}
}catch(Exception ex1){
}catch(CDKException ex1){
dihedrals[counter] = DIHEDRAL_EXTENDED_CHAIN;
}
} else {
Expand Down Expand Up @@ -287,9 +286,8 @@ private int getHybridisationState(IAtom atom1) {
*@param c coordinates (Point2d) of atom3 connected to bond
*@param d coordinates (Point2d) of atom4 connected to bond
*@return The doubleBondConfiguration2D value
*@exception Exception Description of the Exception
*/
private int getDoubleBondConfiguration2D(IBond bond,Point2d a, Point2d b,Point2d c,Point2d d) throws Exception{
private int getDoubleBondConfiguration2D(IBond bond,Point2d a, Point2d b,Point2d c,Point2d d) throws CDKException{
if (bond.getOrder() != IBond.Order.DOUBLE){
return 0;
}
Expand All @@ -308,9 +306,8 @@ private int getDoubleBondConfiguration2D(IBond bond,Point2d a, Point2d b,Point2d
* @param id1 atom1 id
* @param id2 atom2 id
* @return The distanceValue value from the force field parameter set
* @exception Exception Description of the Exception
*/
public double getBondLengthValue(String id1, String id2) throws Exception {
public double getBondLengthValue(String id1, String id2){
String dkey = "";
if (pSet.containsKey(("bond" + id1 + ";" + id2))) {
dkey="bond" + id1 + ";" + id2;
Expand All @@ -330,9 +327,8 @@ public double getBondLengthValue(String id1, String id2) throws Exception {
* @param id2 Description of the Parameter
* @param id3 Description of the Parameter
* @return The angleKey value
* @exception Exception Description of the Exception
*/
public double getAngleValue(String id1, String id2, String id3) throws Exception {
public double getAngleValue(String id1, String id2, String id3) {
String akey = "";
if (pSet.containsKey(("angle" + id1 + ";" + id2 + ";" + id3))) {
akey = "angle" + id1 + ";" + id2 + ";" + id3;
Expand Down
Expand Up @@ -222,7 +222,7 @@ public Map<String,Object> getParameterSet() {
*
* @param ID Atomtype id of the forcefield
* @return The atomType
* @exception NoSuchAtomTypeException atomType is not known
* @exception NoSuchAtomTypeException atomType is not known.
*/
private IAtomType getAtomType(String ID) throws NoSuchAtomTypeException {
IAtomType at = null;
Expand All @@ -240,9 +240,9 @@ private IAtomType getAtomType(String ID) throws NoSuchAtomTypeException {
* Method assigns atom types to atoms (calculates sssr and aromaticity)
*
*@return sssrf set
*@exception Exception Description of the Exception
*@exception CDKException Problems detecting aromaticity or making hose codes.
*/
public IRingSet assignAtomTyps(IMolecule molecule) throws Exception {
public IRingSet assignAtomTyps(IMolecule molecule) throws CDKException {
IAtom atom = null;
String hoseCode = "";
HOSECodeGenerator hcg = new HOSECodeGenerator();
Expand Down Expand Up @@ -293,7 +293,7 @@ public IRingSet assignAtomTyps(IMolecule molecule) throws Exception {
configureAtom(atom, hoseCode, isInHeteroRing);
} catch (CDKException ex2) {
System.out.println("Could not final configure atom " + i + " due to " + ex2.toString());
throw new Exception("Could not final configure atom due to problems with force field", ex2);
throw new CDKException("Could not final configure atom due to problems with force field", ex2);
}
}

Expand Down Expand Up @@ -349,9 +349,10 @@ private boolean isHeteroRingSystem(IAtomContainer ac) {
*
* @param atom The atom to be aasigned
* @param ID the atom type id
* @exception NoSuchAtomTypeException atomType is not known
* @return the assigned atom
*/
private IAtom setAtom(IAtom atom, String ID) throws Exception {
private IAtom setAtom(IAtom atom, String ID) throws NoSuchAtomTypeException {
IAtomType at = null;
String key = "";
List<?> data = null;
Expand Down Expand Up @@ -385,7 +386,7 @@ private IAtom setAtom(IAtom atom, String ID) throws Exception {
return atom;
}

public IAtom configureAtom(IAtom atom, String hoseCode, boolean _boolean) throws Exception {
public IAtom configureAtom(IAtom atom, String hoseCode, boolean _boolean) throws CDKException {
if (ffName.equals("mm2")){
return configureMM2BasedAtom(atom, hoseCode,_boolean);
}else if (ffName.equals("mmff94")){
Expand All @@ -400,9 +401,9 @@ public IAtom configureAtom(IAtom atom, String hoseCode, boolean _boolean) throws
* @param atom atom to be configured
* @param hoseCode the 4 sphere hose code of the atom
* @return atom
* @exception CDKException Description of the Exception
* @exception NoSuchAtomTypeException atomType is not known
*/
public IAtom configureMM2BasedAtom(IAtom atom, String hoseCode,boolean hetRing) throws Exception {
public IAtom configureMM2BasedAtom(IAtom atom, String hoseCode,boolean hetRing) throws NoSuchAtomTypeException {
//logger.debug("CONFIGURE MM2 ATOM");
List<Pattern> atomTypePattern = null;
MM2BasedAtomTypePattern atp = new MM2BasedAtomTypePattern();
Expand Down Expand Up @@ -558,9 +559,9 @@ public String removeAromaticityFlagsFromHoseCode(String hoseCode){
* @param atom atom to be configured
* @param hoseCode the 4 sphere hose code of the atom
* @return atom
* @exception CDKException Description of the Exception
* @exception NoSuchAtomTypeException atomType is not known
*/
public IAtom configureMMFF94BasedAtom(IAtom atom, String hoseCode, boolean isInHetRing) throws Exception {
public IAtom configureMMFF94BasedAtom(IAtom atom, String hoseCode, boolean isInHetRing) throws NoSuchAtomTypeException {
//logger.debug("****** Configure MMFF94 AtomType ******");
List<Pattern> atomTypePattern = null;
MMFF94BasedAtomTypePattern atp = new MMFF94BasedAtomTypePattern();
Expand Down
49 changes: 18 additions & 31 deletions src/main/org/openscience/cdk/modeling/builder3d/ModelBuilder3D.java
Expand Up @@ -139,7 +139,7 @@ private void setForceField(String ffname) throws CDKException {
forceFieldName = ffname;
ffc.setForceFieldConfigurator(ffname);
parameterSet = ffc.getParameterSet();
} catch (Exception ex1) {
} catch (CDKException ex1) {
logger.error("Problem with ForceField configuration due to>" + ex1.getMessage());
logger.debug(ex1);
throw new CDKException("Problem with ForceField configuration due to>" + ex1.getMessage(), ex1);
Expand All @@ -150,7 +150,7 @@ private void setForceField(String ffname) throws CDKException {
/**
* Generate 3D coordinates with force field information.
*/
public IMolecule generate3DCoordinates(IMolecule molecule, boolean clone) throws Exception {
public IMolecule generate3DCoordinates(IMolecule molecule, boolean clone) throws CDKException, NoSuchAtomTypeException, CloneNotSupportedException, IOException{
String[] originalAtomTypeNames = new String[molecule.getAtomCount()];
for (int i=0; i<originalAtomTypeNames.length; i++) {
originalAtomTypeNames[i] = molecule.getAtom(i).getAtomTypeName();
Expand Down Expand Up @@ -182,7 +182,7 @@ public IMolecule generate3DCoordinates(IMolecule molecule, boolean clone) throws
molecule.getAtom(0).setPoint3d(new Point3d(0.0, 0.0, 0.0));
try {
atlp3d.add3DCoordinatesForSinglyBondedLigands(molecule);
} catch (Exception ex3) {
} catch (CDKException ex3) {
logger.error("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.getMessage());
logger.debug(ex3);
throw new CDKException("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.getMessage(), ex3);
Expand Down Expand Up @@ -227,10 +227,10 @@ public IMolecule generate3DCoordinates(IMolecule molecule, boolean clone) throws
//logger.debug("******* PLACE SUBSTITUENTS ******");
try {
atlp3d.add3DCoordinatesForSinglyBondedLigands(molecule);
} catch (Exception ex3) {
} catch (CDKException ex3) {
logger.error("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.getMessage());
logger.debug(ex3);
throw new Exception("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.getMessage(), ex3);
throw new CDKException("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.getMessage(), ex3);
}
// restore the original atom type names
for (int i=0; i<originalAtomTypeNames.length; i++) {
Expand Down Expand Up @@ -262,9 +262,8 @@ private IRingSet getRingSetOfAtom(List ringSystems, IAtom atom) {
* Layout the molecule, starts with ring systems and than aliphatic chains.
*
*@param ringSetMolecule ringSystems of the molecule
*@exception Exception Description of the Exception
*/
private void layoutMolecule(List ringSetMolecule, IMolecule molecule, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws Exception {
private void layoutMolecule(List ringSetMolecule, IMolecule molecule, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws CDKException, IOException, CloneNotSupportedException {
//logger.debug("****** LAYOUT MOLECULE MAIN *******");
IAtomContainer ac = null;
int safetyCounter = 0;
Expand Down Expand Up @@ -403,9 +402,8 @@ private void layoutRingSystem(Point3d originalCoord, IAtom placedRingAtom, IRing
*@param unplacedAtom The new branchAtom
*@param atomA placed atom to which the unplaced satom is connected
*@param atomNeighbours placed atomNeighbours of atomA
*@exception Exception Description of the Exception
*/
private void setBranchAtom(IMolecule molecule, IAtom unplacedAtom, IAtom atomA, IAtomContainer atomNeighbours, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d) throws Exception {
private void setBranchAtom(IMolecule molecule, IAtom unplacedAtom, IAtom atomA, IAtomContainer atomNeighbours, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d) throws CDKException {
//logger.debug("****** SET Branch Atom ****** >"+molecule.getAtomNumber(unplacedAtom));
IAtomContainer noCoords = molecule.getBuilder().newAtomContainer();
noCoords.addAtom(unplacedAtom);
Expand All @@ -431,15 +429,11 @@ private void setBranchAtom(IMolecule molecule, IAtom unplacedAtom, IAtom atomA,
, length, angle);
double distance = 0;
int farthestPoint = 0;
try {
for (int i = 0; i < branchPoints.length; i++) {
if (Math.abs(branchPoints[i].distance(centerPlacedMolecule)) > Math.abs(distance)) {
distance = branchPoints[i].distance(centerPlacedMolecule);
farthestPoint = i;
}
for (int i = 0; i < branchPoints.length; i++) {
if (Math.abs(branchPoints[i].distance(centerPlacedMolecule)) > Math.abs(distance)) {
distance = branchPoints[i].distance(centerPlacedMolecule);
farthestPoint = i;
}
} catch (Exception ex2) {
throw new IOException("SetBranchAtomERROR: Not enough branch Points");
}

int stereo = -1;
Expand All @@ -463,9 +457,8 @@ private void setBranchAtom(IMolecule molecule, IAtom unplacedAtom, IAtom atomA,
* Search and place branches of a chain or ring.
*
*@param chain AtomContainer if atoms in an aliphatic chain or ring system
*@exception Exception Description of the Exception
*/
private void searchAndPlaceBranches(IMolecule molecule, IAtomContainer chain, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws Exception {
private void searchAndPlaceBranches(IMolecule molecule, IAtomContainer chain, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws CDKException {
//logger.debug("****** SEARCH AND PLACE ****** Chain length: "+chain.getAtomCount());
java.util.List atoms = null;
IAtomContainer branchAtoms = molecule.getBuilder().newAtomContainer();
Expand All @@ -476,18 +469,13 @@ private void searchAndPlaceBranches(IMolecule molecule, IAtomContainer chain, At
IAtom atom = (IAtom)atoms.get(j);
if (!(atom.getSymbol()).equals("H") & !(atom.getFlag(CDKConstants.ISPLACED)) & !(atom.getFlag(CDKConstants.ISINRING))) {
//logger.debug("SEARCH PLACE AND FOUND Branch Atom "+molecule.getAtomNumber(chain.getAtomAt(i))+
// " New Atom:"+molecule.getAtomNumber(atoms[j])+" -> STORE");
try {
connectedAtoms.add(ap3d.getPlacedHeavyAtoms(molecule, chain.getAtom(i)));
//logger.debug("Connected atom1:"+molecule.getAtomNumber(connectedAtoms.getAtomAt(0))+" atom2:"+
//molecule.getAtomNumber(connectedAtoms.getAtomAt(1))+ " Length:"+connectedAtoms.getAtomCount());
} catch (Exception ex1) {
logger.error("SearchAndPlaceBranchERROR: Cannot find connected placed atoms due to" + ex1.toString());
throw new IOException("SearchAndPlaceBranchERROR: Cannot find connected placed atoms");
}
// " New Atom:"+molecule.getAtomNumber(atoms[j])+" -> STORE");
connectedAtoms.add(ap3d.getPlacedHeavyAtoms(molecule, chain.getAtom(i)));
//logger.debug("Connected atom1:"+molecule.getAtomNumber(connectedAtoms.getAtomAt(0))+" atom2:"+
//molecule.getAtomNumber(connectedAtoms.getAtomAt(1))+ " Length:"+connectedAtoms.getAtomCount());
try {
setBranchAtom(molecule, atom, chain.getAtom(i), connectedAtoms, ap3d, atlp3d);
} catch (Exception ex2) {
} catch (CDKException ex2) {
logger.error("SearchAndPlaceBranchERROR: Cannot find enough neighbour atoms due to" + ex2.toString());
throw new CDKException("SearchAndPlaceBranchERROR: Cannot find enough neighbour atoms: " + ex2.getMessage(), ex2);
}
Expand All @@ -505,9 +493,8 @@ private void searchAndPlaceBranches(IMolecule molecule, IAtomContainer chain, At
* Layout all aliphatic chains with ZMatrix.
*
*@param startAtoms AtomContainer of possible start atoms for a chain
*@exception Exception Description of the Exception
*/
private void placeLinearChains3D(IMolecule molecule, IAtomContainer startAtoms, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws Exception {
private void placeLinearChains3D(IMolecule molecule, IAtomContainer startAtoms, AtomPlacer3D ap3d, AtomTetrahedralLigandPlacer3D atlp3d, AtomPlacer atomPlacer) throws CDKException {
//logger.debug("****** PLACE LINEAR CHAINS ******");
IAtom dihPlacedAtom = null;
IAtom thirdPlacedAtom = null;
Expand Down
Expand Up @@ -135,7 +135,7 @@ public void testGetPlacedHeavyAtom_IAtomContainer_IAtom(){
}

@Test
public void testGeometricCenterAllPlacedAtoms_IAtomContainer() throws CDKException, Exception {
public void testGeometricCenterAllPlacedAtoms_IAtomContainer() throws CDKException {
IMolecule ac = MoleculeFactory.makeAlphaPinene();
for(int i=0;i<ac.getAtomCount();i++){
ac.getAtom(i).setFlag(CDKConstants.ISPLACED, true);
Expand Down

0 comments on commit bc5837d

Please sign in to comment.