Calculate the median bond length of an atom container.

@param container structure representation
@return median bond length
@throws java.lang.IllegalArgumentException unset coordinates or no bonds
768057
public static double getBondLengthMedian(final IAtomContainer container) {
        if (container.getBondCount() == 0) throw new IllegalArgumentException("Container has no bonds.");
        double[] lengths = new double[container.getBondCount()];
        for (int i = 0; i < container.getBondCount(); i++) {
            final IBond bond = container.getBond(i);
            final IAtom atom1 = bond.getBegin();
            final IAtom atom2 = bond.getEnd();
            if (atom1.getPoint2d() == null || atom2.getPoint2d() == null)
                throw new IllegalArgumentException("An atom has no 2D coordinates.");
            lengths[i] = getLength2D(bond);
        }
        Arrays.sort(lengths);
        return lengths[lengths.length / 2];
    }
527765
private static boolean familyBond(IAtomContainer container, IBond bond) {
        List<String> normalAt = new ArrayList<String>();
        normalAt.add("C");
        normalAt.add("H");

        if (getDoubleBondNumber(container) > 30) // taking to long
            return false;

        StructureResonanceGenerator gRN = new StructureResonanceGenerator();
        IAtomContainer ac = gRN.getContainer(container, bond);

        if (ac == null) return true;

        if (getDoubleBondNumber(ac) > 15) // taking to long
            return false;

        for (IAtom atom : container.atoms()) {
            if (!normalAt.contains(atom.getSymbol())) if (ac.contains(atom)) return false;
        }

        return true;
    }
194041
private double getDensity(Atom[] ca1subset, Atom[] ca2subset ) throws StructureException{

		Atom centroid1 =  Calc.getCentroid(ca1subset);
		Atom centroid2 = Calc.getCentroid(ca2subset);

		// get Average distance to centroid ...

		double d1 = 0;
		double d2 = 0;

		for ( int i = 0 ; i < ca1subset.length;i++){
			double dd1 = Calc.getDistance(centroid1, ca1subset[i]);
			double dd2 = Calc.getDistance(centroid2, ca2subset[i]);

			d1 += dd1;
			d2 += dd2;

		}

		double avd1 = d1 / ca1subset.length;
		double avd2 = d2 / ca2subset.length;

		return Math.min(avd1,avd2);
	}
784468
private boolean getIfBondIsNotRotatable(IAtomContainer mol, IBond bond, IAtomContainer detected) {
        boolean isBondNotRotatable = false;
        int counter = 0;
        IAtom atom0 = bond.getBegin();
        IAtom atom1 = bond.getEnd();
        if (detected != null) {
            if (detected.contains(bond)) counter += 1;
        }
        if (atom0.getFlag(CDKConstants.ISINRING)) {
            if (atom1.getFlag(CDKConstants.ISINRING)) {
                counter += 1;
            } else {
                if (atom1.getSymbol().equals("H"))
                    counter += 1;
                else
                    counter += 0;
            }
        }
        if (atom0.getSymbol().equals("N") && atom1.getSymbol().equals("C")) {
            if (getIfACarbonIsDoubleBondedToAnOxygen(mol, atom1)) counter += 1;
        }
        if (atom0.getSymbol().equals("C") && atom1.getSymbol().equals("N")) {
            if (getIfACarbonIsDoubleBondedToAnOxygen(mol, atom0)) counter += 1;
        }
        if (counter > 0) isBondNotRotatable = true;
        return isBondNotRotatable;
    }
1346988
@Override
    public DescriptorValue calculate(IAtomContainer atomContainer) {
        if (!GeometryUtil.has3DCoordinates(atomContainer))
            return getDummyDescriptorValue(new CDKException("Molecule must have 3D coordinates"));

        double angle = 10.0;
        double maxLOB = 0;
        double minArea = 1e6;
        double mmLOB = 0;

        double lob, bol, area;
        double[] xyzRanges;

        double[][] coords = new double[atomContainer.getAtomCount()][3];
        for (int i = 0; i < atomContainer.getAtomCount(); i++) {
            coords[i][0] = atomContainer.getAtom(i).getPoint3d().x;
            coords[i][1] = atomContainer.getAtom(i).getPoint3d().y;
            coords[i][2] = atomContainer.getAtom(i).getPoint3d().z;
        }

        // get the com
        Point3d com = GeometryUtil.get3DCentreOfMass(atomContainer);
        if (com == null) 
            return getDummyDescriptorValue(new CDKException("Error in center of mass calculation, has exact mass been set on all atoms?"));

        // translate everything to COM
        for (int i = 0; i < coords.length; i++) {
            coords[i][0] -= com.x;
            coords[i][1] -= com.y;
            coords[i][2] -= com.z;
        }

        int nangle = (int) (90 / angle);
        for (int i = 0; i < nangle; i++) {
            rotateZ(coords, Math.PI / 180.0 * angle);
            try {
                xyzRanges = extents(atomContainer, coords, true);
            } catch (CDKException e) {
                return getDummyDescriptorValue(e);
            }
            lob = xyzRanges[0] / xyzRanges[1];
            bol = 1.0 / lob;
            if (lob < bol) {
                double tmp = lob;
                lob = bol;
                bol = tmp;
            }
            area = xyzRanges[0] * xyzRanges[1];
            if (lob > maxLOB) maxLOB = lob;
            if (area < minArea) {
                minArea = area;
                mmLOB = lob;
            }
        }

        DoubleArrayResult result = new DoubleArrayResult(2);
        result.add(maxLOB);
        result.add(mmLOB);

        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), result,
                getDescriptorNames());
    }
146883
@Override
    public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
        String symbol = atom.getSymbol();
        double vdwradius = PeriodicTable.getVdwRadius(symbol);
        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(),
                new DoubleResult(vdwradius), NAMES);

    }
796143
public IAtom getFarthestAtom(Point3d refAtomPoint, IAtomContainer ac) {
        double distance = 0;
        IAtom atom = null;
        for (int i = 0; i < ac.getAtomCount(); i++) {
            if (ac.getAtom(i).getPoint3d() != null) {
                if (Math.abs(refAtomPoint.distance(ac.getAtom(i).getPoint3d())) > distance) {
                    atom = ac.getAtom(i);
                    distance = Math.abs(refAtomPoint.distance(ac.getAtom(i).getPoint3d()));
                }
            }
        }
        return atom;
    }
190830
private static int getDoubleBondNumber(IAtomContainer container) {
        int doubleNumber = 0;
        for (IBond bond : container.bonds()) {
            if (bond.getOrder().equals(IBond.Order.DOUBLE) || bond.getOrder().equals(IBond.Order.TRIPLE))
                doubleNumber++;
        }
        return doubleNumber;
    }
1182360
@Override
    public ValidationReport validateBond(IBond subject) {
        ValidationReport report = new ValidationReport();
        // only consider two atom bonds
        if (subject.getAtomCount() == 2) {
            double distance = subject.getBegin().getPoint3d().distance(subject.getEnd().getPoint3d());
            if (distance > 3.0) { // should really depend on the elements
                ValidationTest badBondLengthError = new ValidationTest(subject,
                        "Bond length cannot exceed 3 Angstroms.",
                        "A bond length typically is between 0.5 and 3.0 Angstroms.");
                report.addError(badBondLengthError);
            }
        }
        return report;
    }
318945
public IAtom getUnplacedRingHeavyAtom(IAtomContainer molecule, IAtom atom) {
        List<IBond> bonds = molecule.getConnectedBondsList(atom);
        IAtom connectedAtom = null;
        for (IBond bond : bonds) {
            connectedAtom = bond.getOther(atom);
            if (isUnplacedHeavyAtom(connectedAtom) && connectedAtom.getFlag(CDKConstants.ISINRING)) {
                return connectedAtom;
            }
        }
        return connectedAtom;
    }
1159475
public static Rectangle2D calculateBounds(IAtomContainer atomContainer) {
        // this is essential, otherwise a rectangle
        // of (+INF, -INF, +INF, -INF) is returned!
        if (atomContainer.getAtomCount() == 0) {
            return new Rectangle2D.Double();
        } else if (atomContainer.getAtomCount() == 1) {
            Point2d point = atomContainer.getAtom(0).getPoint2d();
            if (point == null) {
                throw new IllegalArgumentException("Cannot calculate bounds when 2D coordinates are missing.");
            }
            return new Rectangle2D.Double(point.x, point.y, 0, 0);
        }

        double xmin = Double.POSITIVE_INFINITY;
        double xmax = Double.NEGATIVE_INFINITY;
        double ymin = Double.POSITIVE_INFINITY;
        double ymax = Double.NEGATIVE_INFINITY;

        for (IAtom atom : atomContainer.atoms()) {
            Point2d point = atom.getPoint2d();
            if (point == null) {
                throw new IllegalArgumentException("Cannot calculate bounds when 2D coordinates are missing.");
            }
            xmin = Math.min(xmin, point.x);
            xmax = Math.max(xmax, point.x);
            ymin = Math.min(ymin, point.y);
            ymax = Math.max(ymax, point.y);
        }
        double width = xmax - xmin;
        double height = ymax - ymin;
        return new Rectangle2D.Double(xmin, ymin, width, height);
    }
1113868
@Override
    public DescriptorValue calculate(IAtomContainer container) {
        int c1sp1 = 0;
        int c2sp1 = 0;
        int c1sp2 = 0;
        int c2sp2 = 0;
        int c3sp2 = 0;
        int c1sp3 = 0;
        int c2sp3 = 0;
        int c3sp3 = 0;
        int c4sp3 = 0;

        for (IAtom atom : container.atoms()) {
            if (!atom.getSymbol().equals("C") && !atom.getSymbol().equals("c")) continue;
            List<IAtom> connectedAtoms = container.getConnectedAtomsList(atom);

            int cc = 0;
            for (IAtom connectedAtom : connectedAtoms) {
                if (connectedAtom.getSymbol().equals("C") || connectedAtom.getSymbol().equals("c")) cc++;
            }

            IBond.Order maxBondOrder = getHighestBondOrder(container, atom);

            if (maxBondOrder == IBond.Order.TRIPLE && cc == 1)
                c1sp1++;
            else if (maxBondOrder == IBond.Order.TRIPLE && cc == 2)
                c2sp1++;
            else if (maxBondOrder == IBond.Order.DOUBLE && cc == 1)
                c1sp2++;
            else if (maxBondOrder == IBond.Order.DOUBLE && cc == 2)
                c2sp2++;
            else if (maxBondOrder == IBond.Order.DOUBLE && cc == 3)
                c3sp2++;
            else if (maxBondOrder == IBond.Order.SINGLE && cc == 1)
                c1sp3++;
            else if (maxBondOrder == IBond.Order.SINGLE && cc == 2)
                c2sp3++;
            else if (maxBondOrder == IBond.Order.SINGLE && cc == 3)
                c3sp3++;
            else if (maxBondOrder == IBond.Order.SINGLE && cc == 4) c4sp3++;
        }

        IntegerArrayResult retval = new IntegerArrayResult(9);
        retval.add(c1sp1);
        retval.add(c2sp1);
        retval.add(c1sp2);
        retval.add(c2sp2);
        retval.add(c3sp2);
        retval.add(c1sp3);
        retval.add(c2sp3);
        retval.add(c3sp3);
        retval.add(c4sp3);

        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), retval,
                getDescriptorNames());
    }
635231
@Override
    public DescriptorValue calculate(IAtomContainer container) {
        IAtomContainer local = AtomContainerManipulator.removeHydrogens(container);

        int natom = local.getAtomCount();
        int[][] admat = AdjacencyMatrix.getMatrix(local);
        int[][] distmat = PathTools.computeFloydAPSP(admat);

        int eccenindex = 0;
        for (int i = 0; i < natom; i++) {
            int max = -1;
            for (int j = 0; j < natom; j++) {
                if (distmat[i][j] > max) max = distmat[i][j];
            }
            int degree = local.getConnectedBondsCount(i);
            eccenindex += max * degree;
        }
        IntegerResult retval = new IntegerResult(eccenindex);
        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), retval,
                getDescriptorNames(), null);
    }
785512
@Override
    public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
        IAtomType atomType;
        try {
            atomType = CDKAtomTypeMatcher.getInstance(atom.getBuilder()).findMatchingAtomType(container, atom);
        } catch (CDKException e) {
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new IntegerResult(
                    (int) Double.NaN), // does that work??
                    getDescriptorNames(), new CDKException("Atom type was null"));
        }
        if (atomType == null) {
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new IntegerResult(
                    (int) Double.NaN), // does that work??
                    getDescriptorNames(), new CDKException("Atom type was null"));

        }

        if (atomType.getHybridization() == null) {
            return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new IntegerResult(
                    (int) Double.NaN), // does that work??
                    getDescriptorNames(), new CDKException("Hybridization was null"));
        }
        int hybridizationCDK = atomType.getHybridization().ordinal();

        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new IntegerResult(
                hybridizationCDK), getDescriptorNames());
    }
891797
public IAtomContainer getUnsetAtomsInAtomContainer(IAtom atom, IAtomContainer ac) {
        List atoms = ac.getConnectedAtomsList(atom);
        IAtomContainer connectedAtoms = atom.getBuilder().newInstance(IAtomContainer.class);
        for (int i = 0; i < atoms.size(); i++) {
            IAtom curAtom = (IAtom) atoms.get(i);
            if (!curAtom.getFlag(CDKConstants.ISPLACED)) {//&& atoms[i].getPoint3d() == null) {
                connectedAtoms.addAtom(curAtom);
            }
        }
        return connectedAtoms;
    }
453059
public IAtom getPlacedHeavyAtom(IAtomContainer molecule, IAtom atom) {
        List<IBond> bonds = molecule.getConnectedBondsList(atom);
        for (IBond bond : bonds) {
            IAtom connectedAtom = bond.getOther(atom);
            if (isPlacedHeavyAtom(connectedAtom)) {
                return connectedAtom;
            }
        }
        return null;
    }
725771
public static double calculate(IAtomContainer molecule) throws CDKException {
        if (atomTypeList == null) {
            atomTypeList = AtomTypeFactory.getInstance("org/openscience/cdk/dict/data/cdk-atom-types.owl",
                    molecule.getBuilder() // take whatever we got first
                    );
        }

        double sum = 0.0;
        int totalHCount = 0;
        for (IAtom atom : molecule.atoms()) {
            Double bondiiVolume = bondiiVolumes.get(atom.getSymbol());
            if (bondiiVolume == null) throw new CDKException("Unsupported element.");

            sum += bondiiVolume;

            // add volumes of implicit hydrogens?
            IAtomType type = atomTypeList.getAtomType(atom.getAtomTypeName());
            if (type == null) throw new CDKException("Unknown atom type for atom: " + atom.getSymbol());
            if (type.getFormalNeighbourCount() == null)
                throw new CDKException("Formal neighbor count not given for : " + type.getAtomTypeName());
            int hCount = type.getFormalNeighbourCount() - molecule.getConnectedBondsCount(atom);
            sum += (hCount * bondiiVolumes.get("H"));
            totalHCount += hCount;
        }
        sum -= 5.92 * (molecule.getBondCount() + totalHCount);

        boolean[] originalFlags = molecule.getFlags();
        Aromaticity.cdkLegacy().apply(molecule);
        IRingSet ringSet = Cycles.sssr(molecule).toRingSet();
        if (ringSet.getAtomContainerCount() > 0) {
            int aromRingCount = 0;
            int nonAromRingCount = 0;
            for (IAtomContainer ring : ringSet.atomContainers()) {
                if (ringIsAromatic(ring)) {
                    aromRingCount++;
                } else {
                    nonAromRingCount++;
                }
            }
            molecule.setFlags(originalFlags);
            sum -= 14.7 * aromRingCount;
            sum -= 3.8 * nonAromRingCount;
        }

        return sum;
    }