GravitationalIndexDescriptor extends AbstractMolecularDescriptor implements IMolecularDescriptor { @Override public DescriptorValue calculate(IAtomContainer container) { if (!GeometryUtil.has3DCoordinates(container)) return getDummyDescriptorValue(new CDKException("Molecule must have 3D coordinates")); IsotopeFactory factory = null; double mass1; double mass2; try { factory = Isotopes.getInstance(); } catch (Exception e) { logger.debug(e); } double sum = 0; for (int i = 0; i < container.getBondCount(); i++) { IBond bond = container.getBond(i); if (bond.getAtomCount() != 2) { return getDummyDescriptorValue(new CDKException("GravitationalIndex: Only handles 2 center bonds")); } mass1 = factory.getMajorIsotope(bond.getBegin().getSymbol()).getMassNumber(); mass2 = factory.getMajorIsotope(bond.getEnd().getSymbol()).getMassNumber(); Point3d p1 = bond.getBegin().getPoint3d(); Point3d p2 = bond.getEnd().getPoint3d(); double x1 = p1.x; double y1 = p1.y; double z1 = p1.z; double x2 = p2.x; double y2 = p2.y; double z2 = p2.z; double dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2); sum += (mass1 * mass2) / dist; } double heavysum = 0; for (int i = 0; i < container.getBondCount(); i++) { IBond b = container.getBond(i); if (b.getAtomCount() != 2) { return getDummyDescriptorValue(new CDKException("GravitationalIndex: Only handles 2 center bonds")); } if (b.getBegin().getAtomicNumber() == IElement.H || b.getEnd().getAtomicNumber() == IElement.H) continue; mass1 = factory.getMajorIsotope(b.getBegin().getSymbol()).getMassNumber(); mass2 = factory.getMajorIsotope(b.getEnd().getSymbol()).getMassNumber(); Point3d point0 = b.getBegin().getPoint3d(); Point3d point1 = b.getEnd().getPoint3d(); double x1 = point0.x; double y1 = point0.y; double z1 = point0.z; double x2 = point1.x; double y2 = point1.y; double z2 = point1.z; double dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2); heavysum += (mass1 * mass2) / dist; } ArrayList<Integer> x = new ArrayList<Integer>(); for (int i = 0; i < container.getAtomCount(); i++) { if (container.getAtom(i).getAtomicNumber() != IElement.H) x.add(i); } int npair = x.size() * (x.size() - 1) / 2; pair[] p = new pair[npair]; for (int i = 0; i < npair; i++) p[i] = new pair(); int pcount = 0; for (int i = 0; i < x.size() - 1; i++) { for (int j = i + 1; j < x.size(); j++) { int present = 0; int a = x.get(i); int b = x.get(j); for (int k = 0; k < pcount; k++) { if ((p[k].x == a && p[k].y == b) || (p[k].y == a && p[k].x == b)) present = 1; } if (present == 1) continue; p[pcount].x = a; p[pcount].y = b; pcount += 1; } } double allheavysum = 0; for (pair aP : p) { int atomNumber1 = aP.x; int atomNumber2 = aP.y; mass1 = factory.getMajorIsotope(container.getAtom(atomNumber1).getSymbol()).getMassNumber(); mass2 = factory.getMajorIsotope(container.getAtom(atomNumber2).getSymbol()).getMassNumber(); double x1 = container.getAtom(atomNumber1).getPoint3d().x; double y1 = container.getAtom(atomNumber1).getPoint3d().y; double z1 = container.getAtom(atomNumber1).getPoint3d().z; double x2 = container.getAtom(atomNumber2).getPoint3d().x; double y2 = container.getAtom(atomNumber2).getPoint3d().y; double z2 = container.getAtom(atomNumber2).getPoint3d().z; double dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2); allheavysum += (mass1 * mass2) / dist; } DoubleArrayResult retval = new DoubleArrayResult(9); retval.add(heavysum); retval.add(Math.sqrt(heavysum)); retval.add(Math.pow(heavysum, 1.0 / 3.0)); retval.add(sum); retval.add(Math.sqrt(sum)); retval.add(Math.pow(sum, 1.0 / 3.0)); retval.add(allheavysum); retval.add(Math.sqrt(allheavysum)); retval.add(Math.pow(allheavysum, 1.0 / 3.0)); return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), retval, getDescriptorNames()); } GravitationalIndexDescriptor(); @Override DescriptorSpecification getSpecification(); @Override void setParameters(Object[] params); @Override Object[] getParameters(); @Override String[] getDescriptorNames(); @Override String[] getParameterNames(); @Override Object getParameterType(String name); @Override DescriptorValue calculate(IAtomContainer container); @Override IDescriptorResult getDescriptorResultType();  }
@Test public void testGravitationalIndex() throws ClassNotFoundException, CDKException, java.lang.Exception { String filename = "data/hin/gravindex.hin"; InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); ISimpleChemObjectReader reader = new HINReader(ins); ChemFile content = (ChemFile) reader.read((ChemObject) new ChemFile()); List cList = ChemFileManipulator.getAllAtomContainers(content); IAtomContainer ac = (IAtomContainer) cList.get(0); DoubleArrayResult retval = (DoubleArrayResult) descriptor.calculate(ac).getValue(); Assert.assertEquals(1756.5060703860984, retval.get(0), 0.00000001); Assert.assertEquals(41.91069159994975, retval.get(1), 0.00000001); Assert.assertEquals(12.06562671430088, retval.get(2), 0.00000001); Assert.assertEquals(1976.6432599699767, retval.get(3), 0.00000001); Assert.assertEquals(44.45945636161082, retval.get(4), 0.00000001); Assert.assertEquals(12.549972243701887, retval.get(5), 0.00000001); Assert.assertEquals(4333.097373073368, retval.get(6), 0.00000001); Assert.assertEquals(65.82626658920714, retval.get(7), 0.00000001); Assert.assertEquals(16.302948232909483, retval.get(8), 0.00000001); }