Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in AtomTools with reference heteroatoms #960

Open
RamiManaf opened this issue Mar 11, 2023 · 0 comments
Open

Error in AtomTools with reference heteroatoms #960

RamiManaf opened this issue Mar 11, 2023 · 0 comments

Comments

@RamiManaf
Copy link

In org.openscience.cdk.geometry.AtomTool the method add3DCoordinates1(IAtomContainer atomContainer) is used to add coordinates for single bonded atoms without coordinates (like hydrogens). The method specify the atoms without coordinates that are attached to a single reference atom with nwanted variable. However, if the reference atom is heteroatom like O,N, S, etc. the method assign nwanted = 3 even if we need coordinates for just one hydrogen.

if (elementType.equals("N") || elementType.equals("O") || elementType.equals("S")) {
nwanted = 3;
}

I don't know the purpose of this line but the code get but the code reaches the method calculate3DCoordinates2(Point3d aPoint, Point3d bPoint, Point3d cPoint, int nwanted, double length, double angle) it gets broken as this method will calculate the position of the third attached hydrogen based on the coordinates of two attached atoms to the reference atom. The method expected nwanted to be 1 or 2 as it already knows that there is already two attached atoms with coordinates. This makes the total 3 or 4 attached atoms to the central reference atom (two known coordinates atoms and one or two attached hydrogens).
public static Point3d[] calculate3DCoordinates2(Point3d aPoint, Point3d bPoint, Point3d cPoint, int nwanted,
double length, double angle) {
Point3d[] newPoints = new Point3d[0];
double ang2 = angle / 2.0;
Vector3d ba = new Vector3d(aPoint);
ba.sub(bPoint);
Vector3d ca = new Vector3d(aPoint);
ca.sub(cPoint);
Vector3d baxca = new Vector3d();
baxca.cross(ba, ca);
if (baxca.length() < 0.00000001) {
// linear
} else if (nwanted == 1) {
newPoints = new Point3d[1];
Vector3d ax = new Vector3d(ba);
ax.add(ca);
ax.normalize();
ax.scale(length);
newPoints[0] = new Point3d(aPoint);
newPoints[0].add(ax);
} else if (nwanted == 2) {
newPoints = new Point3d[2];
Vector3d ax = new Vector3d(ba);
ax.add(ca);
ax.normalize();
baxca.normalize();
baxca.scale(Math.sin(ang2) * length);
ax.scale(Math.cos(ang2) * length);
newPoints[0] = new Point3d(aPoint);
newPoints[0].add(ax);
newPoints[0].add(baxca);
newPoints[1] = new Point3d(aPoint);
newPoints[1].add(ax);
newPoints[1].sub(baxca);
}
return newPoints;
}

However, if the central atom is a heteroatom the nwanted=3. Therefore, calculate3DCoordinates2 return empty coordinates array Point3d[] newPoints = new Point3d[0]; as there is no case for nwanted = 3.
This behavior ends with ArrayIndexOutOfBoundsException as a result of the empty coordinates array.

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at org.openscience.cdk.geometry.AtomTools.add3DCoordinates1(AtomTools.java:100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant