counting contiguous atoms

User 870ab5b546

29-06-2007 17:07:45

Here's a method for counting the largest number of contiguous X atoms (usually C) in a compound.





Code:
    public static int countContiguous(Molecule mol, String element) {


        int maxNumContiguous = 0;


        boolean[] beenThere = new boolean[mol.getAtomCount()];


        for (int atomIdx = 0; atomIdx < mol.getAtomCount(); atomIdx++) {


            int numContiguous = 0;


            MolAtom atom = mol.getAtom(atomIdx);


            if (atom.getSymbol().equals(element) && !beenThere[atomIdx]) {


                numContiguous = countContiguous(mol, atomIdx, beenThere,


                        element, 0);


                if (numContiguous > maxNumContiguous)


                    maxNumContiguous = numContiguous;


            } // if we did a new recursive search


        } // for each atom in molecule


        return maxNumContiguous;


    } // countContiguous(Molecule, String)


           


    private static int countContiguous(Molecule mol, int atom0Idx,


            boolean[] beenThere, String element, int numContiguous) {


        beenThere[atom0Idx] = true;


        numContiguous++;


        MolAtom atom0 = mol.getAtom(atom0Idx);


        for (int ligIdx = 0; ligIdx < atom0.getBondCount(); ligIdx++) {


            MolAtom atom1 = (MolAtom) atom0.getLigand(ligIdx);


            int atom1Idx = mol.indexOf(atom1);


            if (atom1.getSymbol().equals(element) && !beenThere[atom1Idx])


                numContiguous = countContiguous(mol, atom1Idx, beenThere,


                        element, numContiguous);


        } // for each atom attached to atom0


        return numContiguous;


    } // countContiguous(Molecule, MolAtom, boolean[], String, int)