class AutocorrelationDescriptorCharge extends AbstractMolecularDescriptor implements IMolecularDescriptor {

    @Override
    public DescriptorValue calculate(IAtomContainer atomContainer) {
        IAtomContainer container;
        try {
            container = (IAtomContainer) atomContainer.clone();
            container = AtomContainerManipulator.removeHydrogens(container);
        } catch (CloneNotSupportedException e) {
            DoubleArrayResult result = new DoubleArrayResult(5);
            for (int i = 0; i < 5; i++)
                result.add(Double.NaN);
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), result,
                    getDescriptorNames(), new CDKException("Error during cloner: " + e.getMessage(), e));
        }

        try {
            double[] w = listcharges(container);
            int natom = container.getAtomCount();
            int[][] distancematrix = TopologicalMatrix.getMatrix(container);

            double[] chargeSum = new double[5];

            for (int k = 0; k < 5; k++) {
                for (int i = 0; i < natom; i++) {
                    for (int j = 0; j < natom; j++) {
                        if (distancematrix[i][j] == k) {
                            chargeSum[k] += w[i] * w[j];
                        } else
                            chargeSum[k] += 0.0;
                    }
                }
                if (k > 0) chargeSum[k] = chargeSum[k] / 2;

            }
            DoubleArrayResult result = new DoubleArrayResult(5);
            for (double aChargeSum : chargeSum) {
                result.add(aChargeSum);
            }
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), result, NAMES);

        } catch (Exception ex) {
            DoubleArrayResult result = new DoubleArrayResult(5);
            for (int i = 0; i < 5; i++)
                result.add(Double.NaN);
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), result, NAMES,
                    new CDKException("Error while calculating the ATS_charge descriptor: " + ex.getMessage(), ex));
        }
    }

}