/// <summary>  Makes an array containing the morgan numbers of the atoms of atomContainer.
 /// 
 /// </summary>
 /// <param name="atomContainer"> The atomContainer to analyse.
 /// </param>
 /// <returns>                The morgan numbers value.
 /// </returns>
 public static int[] getMorganNumbers(IAtomContainer atomContainer)
 {
     int[] morganMatrix;
     int[] tempMorganMatrix;
     int N = atomContainer.AtomCount;
     morganMatrix = new int[N];
     tempMorganMatrix = new int[N];
     IAtom[] atoms = null;
     for (int f = 0; f < N; f++)
     {
         morganMatrix[f] = atomContainer.getBondCount(f);
         tempMorganMatrix[f] = atomContainer.getBondCount(f);
     }
     for (int e = 0; e < N; e++)
     {
         for (int f = 0; f < N; f++)
         {
             morganMatrix[f] = 0;
             atoms = atomContainer.getConnectedAtoms(atomContainer.getAtomAt(f));
             for (int g = 0; g < atoms.Length; g++)
             {
                 morganMatrix[f] += tempMorganMatrix[atomContainer.getAtomNumber(atoms[g])];
             }
         }
         Array.Copy(morganMatrix, 0, tempMorganMatrix, 0, N);
     }
     return tempMorganMatrix;
 }
Example #2
0
        public virtual void  saturateRingSystems(IAtomContainer atomContainer)
        {
            IRingSet rs = new SSSRFinder(atomContainer.Builder.newMolecule(atomContainer)).findSSSR();

            System.Collections.ArrayList ringSets = RingPartitioner.partitionRings(rs);
            IAtomContainer ac   = null;
            IAtom          atom = null;

            int[] temp;
            for (int f = 0; f < ringSets.Count; f++)
            {
                rs   = (IRingSet)ringSets[f];
                ac   = RingSetManipulator.getAllInOneContainer(rs);
                temp = new int[ac.AtomCount];
                for (int g = 0; g < ac.AtomCount; g++)
                {
                    atom    = ac.getAtomAt(g);
                    temp[g] = atom.getHydrogenCount();
                    atom.setHydrogenCount(atomContainer.getBondCount(atom) - ac.getBondCount(atom) - temp[g]);
                }
                saturate(ac);
                for (int g = 0; g < ac.AtomCount; g++)
                {
                    atom = ac.getAtomAt(g);
                    atom.setHydrogenCount(temp[g]);
                }
            }
        }
Example #3
0
        /// <summary>  Description of the Method
        ///
        /// </summary>
        /// <param name="ac">               The AtomContainer to be searched
        /// </param>
        /// <param name="pathes">           A vectoring storing all the pathes
        /// </param>
        /// <param name="ringSet">          A ringset to be extended while we search
        /// </param>
        /// <exception cref="CDKException"> An exception thrown if something goes wrong or if the timeout limit is reached
        /// </exception>
        private void doSearch(IAtomContainer ac, System.Collections.ArrayList pathes, IRingSet ringSet)
        {
            IAtom atom = null;

            /*
             *  First we convert the molecular graph into a a path graph by
             *  creating a set of two membered pathes from all the bonds in the molecule
             */
            initPathGraph(ac, pathes);
            if (debug)
            {
                System.Console.Out.WriteLine("BondCount: " + ac.getBondCount() + ", PathCount: " + pathes.Count);
            }
            do
            {
                atom = selectAtom(ac);
                if (atom != null)
                {
                    remove(atom, ac, pathes, ringSet);
                }
            }while (pathes.Count > 0 && atom != null);
            if (debug)
            {
                System.Console.Out.WriteLine("pathes.size(): " + pathes.Count);
            }
            if (debug)
            {
                System.Console.Out.WriteLine("ringSet.size(): " + ringSet.AtomContainerCount);
            }
        }
Example #4
0
        /// <summary>
        /// Count bonds connecting two heavy atoms
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>

        public static int GetHeavyBondCount(IAtomContainer mol)
        {
            int hbCnt = 0;

            for (int bi = 0; bi < mol.getBondCount(); bi++)
            {
                IBond b = mol.getBond(bi);
                if (b.getAtomCount() != 2)
                {
                    continue;
                }

                IAtom a = b.getAtom(0);                    // first atom
                if (a.getAtomicNumber().intValue() == 1 || // do not count hydrogens
                    a.getSymbol().Equals("H"))
                {
                    a = b.getAtom(1);                      // second atom
                }
                if (a.getAtomicNumber().intValue() == 1 || // do not count hydrogens
                    a.getSymbol().Equals("H"))
                {
                    continue;
                }

                hbCnt++;
            }

            return(hbCnt);
        }
Example #5
0
        /// <summary>
        /// Remove the following features from a molecule
        ///  - Atom non-standard mass
        ///  - Stereo chemistry
        ///  - explicit hydrogens
        /// </summary>
        /// <param name="src"></param>
        /// <returns>Modified mol</returns>

        public static IAtomContainer RemoveIsotopesStereoExplicitHydrogens(
            IAtomContainer src)
        {
            IAtom[] atoms = new IAtom[src.getAtomCount()];
            IBond[] bonds = new IBond[src.getBondCount()];

            IChemObjectBuilder builder = src.getBuilder();

            for (int i = 0; i < atoms.Length; i++)
            {
                IAtom atom  = src.getAtom(i);
                IAtom atom2 = (IAtom)builder.newInstance(typeof(IAtom), atom.getSymbol());
                SetImplicitHydrogenCount(atom2, GetImplicitHydrogenCount(atom));
                atom2.setPoint2d(atom.getPoint2d());
                atoms[i] = atom2;
            }

            for (int i = 0; i < bonds.Length; i++)
            {
                IBond bond = src.getBond(i);

                int   u     = src.getAtomNumber(bond.getAtom(0));
                int   v     = src.getAtomNumber(bond.getAtom(1));
                IBond bond2 = (IBond)builder.newInstance(typeof(IBond), atoms[u], atoms[v]);

                bond2.setIsAromatic(bond.isAromatic());
                bond2.setIsInRing(bond.isInRing());
                bond2.setOrder(bond.getOrder());

                bond2.setFlag(CDKConstants.ISAROMATIC, bond.getFlag(CDKConstants.ISAROMATIC));
                bond2.setFlag(CDKConstants.SINGLE_OR_DOUBLE, bond.getFlag(CDKConstants.SINGLE_OR_DOUBLE));

                bonds[i] = bond2;
            }

            IAtomContainer dest = (IAtomContainer)builder.newInstance(typeof(IAtomContainer));

            dest.setAtoms(atoms);
            dest.setBonds(bonds);

            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(dest);
            dest = AtomContainerManipulator.suppressHydrogens(dest);
            GetHydrogenAdder().addImplicitHydrogens(dest);

            return(dest);
        }
Example #6
0
        /// <summary>  Transforms an AtomContainer into a BitSet (which's size = number of bond
        /// in the atomContainer, all the bit are set to true)
        ///
        /// </summary>
        /// <param name="ac"> AtomContainer to transform
        /// </param>
        /// <returns>     The bitSet
        /// </returns>
        public static System.Collections.BitArray getBitSet(IAtomContainer ac)
        {
            System.Collections.BitArray bs;
            int n = ac.getBondCount();

            if (n != 0)
            {
                bs = new System.Collections.BitArray((n % 64 == 0 ? n / 64 : n / 64 + 1) * 64);
                for (int i = 0; i < n; i++)
                {
                    SupportClass.BitArraySupport.Set(bs, i);
                }
            }
            else
            {
                bs = new System.Collections.BitArray(64);
            }

            return(bs);
        }
        /// <summary>
        /// Creates a molecule graph for use with jgrapht.
        /// Bond orders are not respected.
        /// </summary>
        /// <param name="molecule">the specified molecule</param>
        /// <returns>a graph representing the molecule</returns>
	    static public SimpleGraph getMoleculeGraph(IAtomContainer molecule) {
		    SimpleGraph graph = new SimpleGraph();
		    for (int i=0; i<molecule.AtomCount; i++	) {
			    IAtom atom = molecule.Atoms[i];
			    graph.addVertex(atom);
		    }
    		
		    for (int i=0; i<molecule.getBondCount(); i++	) {
			    IBond bond = molecule.Bonds[i];
    			
			    /*
			    int order = (int) bond.getOrder();
			    for (int j=0; j<order; j++) {
				    graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
			    }
			    */
			    graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
		    }
		    return graph;
	    }
Example #8
0
        /// <summary>  Removes all external aliphatic chains by chopping them off from the
        /// ends
        ///
        /// </summary>
        /// <param name="ac">               The AtomContainer to work with
        /// </param>
        /// <exception cref="CDKException"> An exception thrown if something goes wrong or if the timeout limit is reached
        /// </exception>
        private void removeAliphatic(IAtomContainer ac)
        {
            bool  removedSomething;
            IAtom atom = null;

            do
            {
                removedSomething = false;
                //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
                for (System.Collections.IEnumerator e = ac.atoms(); e.MoveNext();)
                {
                    //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
                    atom = (IAtom)e.Current;
                    if (ac.getBondCount(atom) == 1)
                    {
                        ac.removeAtomAndConnectedElectronContainers(atom);
                        removedSomething = true;
                    }
                }
            }while (removedSomething);
        }
Example #9
0
        /// <summary>  Selects an optimal atom for removal
        /// See {@cdk.cite HAN96} for details
        ///
        /// </summary>
        /// <param name="ac"> The AtomContainer to search
        /// </param>
        /// <returns>     The selected Atom
        /// </returns>
        private IAtom selectAtom(IAtomContainer ac)
        {
            int minDegree = 999;
            // :-)
            int   degree  = minDegree;
            IAtom minAtom = null;
            IAtom atom    = null;

            for (int f = 0; f < ac.AtomCount; f++)
            {
                atom   = ac.getAtomAt(f);
                degree = ac.getBondCount(atom);

                if (degree < minDegree)
                {
                    minAtom   = atom;
                    minDegree = degree;
                }
            }

            return(minAtom);
        }
Example #10
0
        /// <summary>
        /// Creates a molecule graph for use with jgrapht.
        /// Bond orders are not respected.
        /// </summary>
        /// <param name="molecule">the specified molecule</param>
        /// <returns>a graph representing the molecule</returns>
        static public SimpleGraph getMoleculeGraph(IAtomContainer molecule)
        {
            SimpleGraph graph = new SimpleGraph();

            for (int i = 0; i < molecule.AtomCount; i++)
            {
                IAtom atom = molecule.Atoms[i];
                graph.addVertex(atom);
            }

            for (int i = 0; i < molecule.getBondCount(); i++)
            {
                IBond bond = molecule.Bonds[i];

                /*
                 * int order = (int) bond.getOrder();
                 * for (int j=0; j<order; j++) {
                 *      graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
                 * }
                 */
                graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
            }
            return(graph);
        }
Example #11
0
        /// <summary>
        /// Remove explicit and implicit hydrogens bonded to positive nitrogens
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        public int RemoveHydrogensBondedToPositiveNitrogens(IAtomContainer org)
        {
            int chg, impHydCnt;
            int implicitHydRemoved = 0;

            HashSet <IAtom> atomsToRemove = new HashSet <IAtom>();
            HashSet <IBond> bondsToRemove = new HashSet <IBond>();
            int             nOrgAtoms     = org.getAtomCount();
            int             nOrgBonds     = org.getBondCount();

            // Get H atoms and their bonds to pos-charged Nitrogen, adjusting charge

            for (int bi = 0; bi < org.getBondCount(); bi++)
            {
                IBond bond = org.getBond(bi);
                if (bond.getAtomCount() != 2)
                {
                    continue;
                }

                IAtom a1 = bond.getAtom(0);
                IAtom a2 = bond.getAtom(1);
                if (a1.getSymbol() == "H" && a2.getSymbol() == "N" && GetFormalCharge(a2) > 0)
                {
                    chg = GetFormalCharge(a2) - 1;
                    SetFormalCharge(a2, chg);
                    atomsToRemove.Add(a1);
                    bondsToRemove.Add(bond);
                }

                else if (a2.getSymbol() == "H" && a1.getSymbol() == "N" && GetFormalCharge(a1) > 0)
                {
                    chg = GetFormalCharge(a1) - 1;
                    SetFormalCharge(a1, chg);
                    atomsToRemove.Add(a2);
                    bondsToRemove.Add(bond);
                }
            }

            // Check for implicit H attached to pos-charged N

            for (int ai = 0; ai < nOrgAtoms; ai++)
            {
                IAtom a = org.getAtom(ai);
                if (a.getSymbol() == "N" && GetFormalCharge(a) > 0 && GetImplicitHydrogenCount(a) > 0)
                {
                    chg = GetFormalCharge(a) - 1;
                    SetFormalCharge(a, chg);

                    impHydCnt = GetImplicitHydrogenCount(a) - 1;
                    SetImplicitHydrogenCount(a, impHydCnt);
                    implicitHydRemoved++;
                }
            }

            if (implicitHydRemoved > 0)
            {
                implicitHydRemoved = implicitHydRemoved;                                     // debug
            }
            if (atomsToRemove.Count == 0)
            {
                return(implicitHydRemoved);                                      // just return if no explicit H to remove
            }
            // Get list of atoms to keep

            IAtom[] cpyAtoms  = new IAtom[nOrgAtoms - atomsToRemove.Count];
            int     nCpyAtoms = 0;

            for (int ai = 0; ai < nOrgAtoms; ai++)
            {
                IAtom atom = org.getAtom(ai);
                if (!atomsToRemove.Contains(atom))
                {
                    cpyAtoms[nCpyAtoms++] = atom;
                }
            }

            org.setAtoms(cpyAtoms);

            // Get list of bonds to keep

            IBond[] cpyBonds  = new IBond[nOrgBonds - bondsToRemove.Count];
            int     nCpyBonds = 0;

            for (int bi = 0; bi < org.getBondCount(); bi++)
            {
                IBond bond = org.getBond(bi);
                if (!bondsToRemove.Contains(bond))
                {
                    cpyBonds[nCpyBonds++] = bond;
                }
            }

            org.setBonds(cpyBonds);

            return(atomsToRemove.Count + implicitHydRemoved);
        }
Example #12
0
        /// <summary>  Checks some simple heuristics for whether the subgraph query can
        /// realistically be a subgraph of the supergraph. If, for example, the
        /// number of nitrogen atoms in the query is larger than that of the supergraph
        /// it cannot be part of it.
        ///
        /// </summary>
        /// <param name="ac1"> the supergraph to be checked
        /// </param>
        /// <param name="ac2"> the subgraph to be tested for
        /// </param>
        /// <returns>    true if the subgraph ac2 has a chance to be a subgraph of ac1
        ///
        /// </returns>

        private static bool testSubgraphHeuristics(IAtomContainer ac1, IAtomContainer ac2)
        {
            int ac1SingleBondCount   = 0;
            int ac1DoubleBondCount   = 0;
            int ac1TripleBondCount   = 0;
            int ac1AromaticBondCount = 0;
            int ac2SingleBondCount   = 0;
            int ac2DoubleBondCount   = 0;
            int ac2TripleBondCount   = 0;
            int ac2AromaticBondCount = 0;
            int ac1SCount            = 0;
            int ac1OCount            = 0;
            int ac1NCount            = 0;
            int ac1FCount            = 0;
            int ac1ClCount           = 0;
            int ac1BrCount           = 0;
            int ac1ICount            = 0;
            int ac1CCount            = 0;

            int ac2SCount  = 0;
            int ac2OCount  = 0;
            int ac2NCount  = 0;
            int ac2FCount  = 0;
            int ac2ClCount = 0;
            int ac2BrCount = 0;
            int ac2ICount  = 0;
            int ac2CCount  = 0;

            IBond bond;
            IAtom atom;

            for (int i = 0; i < ac1.getBondCount(); i++)
            {
                bond = ac1.getBondAt(i);
                if (bond.getFlag(CDKConstants.ISAROMATIC))
                {
                    ac1AromaticBondCount++;
                }
                else if (bond.Order == 1)
                {
                    ac1SingleBondCount++;
                }
                else if (bond.Order == 2)
                {
                    ac1DoubleBondCount++;
                }
                else if (bond.Order == 3)
                {
                    ac1TripleBondCount++;
                }
            }
            for (int i = 0; i < ac2.getBondCount(); i++)
            {
                bond = ac2.getBondAt(i);
                if (bond is IQueryBond)
                {
                    continue;
                }
                if (bond.getFlag(CDKConstants.ISAROMATIC))
                {
                    ac2AromaticBondCount++;
                }
                else if (bond.Order == 1)
                {
                    ac2SingleBondCount++;
                }
                else if (bond.Order == 2)
                {
                    ac2DoubleBondCount++;
                }
                else if (bond.Order == 3)
                {
                    ac2TripleBondCount++;
                }
            }

            if (ac2SingleBondCount > ac1SingleBondCount)
            {
                return(false);
            }
            if (ac2AromaticBondCount > ac1AromaticBondCount)
            {
                return(false);
            }
            if (ac2DoubleBondCount > ac1DoubleBondCount)
            {
                return(false);
            }
            if (ac2TripleBondCount > ac1TripleBondCount)
            {
                return(false);
            }

            for (int i = 0; i < ac1.AtomCount; i++)
            {
                atom = ac1.getAtomAt(i);
                if (atom.Symbol.Equals("S"))
                {
                    ac1SCount++;
                }
                else if (atom.Symbol.Equals("N"))
                {
                    ac1NCount++;
                }
                else if (atom.Symbol.Equals("O"))
                {
                    ac1OCount++;
                }
                else if (atom.Symbol.Equals("F"))
                {
                    ac1FCount++;
                }
                else if (atom.Symbol.Equals("Cl"))
                {
                    ac1ClCount++;
                }
                else if (atom.Symbol.Equals("Br"))
                {
                    ac1BrCount++;
                }
                else if (atom.Symbol.Equals("I"))
                {
                    ac1ICount++;
                }
                else if (atom.Symbol.Equals("C"))
                {
                    ac1CCount++;
                }
            }
            for (int i = 0; i < ac2.AtomCount; i++)
            {
                atom = ac2.getAtomAt(i);
                if (atom is IQueryAtom)
                {
                    continue;
                }
                if (atom.Symbol.Equals("S"))
                {
                    ac2SCount++;
                }
                else if (atom.Symbol.Equals("N"))
                {
                    ac2NCount++;
                }
                else if (atom.Symbol.Equals("O"))
                {
                    ac2OCount++;
                }
                else if (atom.Symbol.Equals("F"))
                {
                    ac2FCount++;
                }
                else if (atom.Symbol.Equals("Cl"))
                {
                    ac2ClCount++;
                }
                else if (atom.Symbol.Equals("Br"))
                {
                    ac2BrCount++;
                }
                else if (atom.Symbol.Equals("I"))
                {
                    ac2ICount++;
                }
                else if (atom.Symbol.Equals("C"))
                {
                    ac2CCount++;
                }
            }

            if (ac1SCount < ac2SCount)
            {
                return(false);
            }
            if (ac1NCount < ac2NCount)
            {
                return(false);
            }
            if (ac1OCount < ac2OCount)
            {
                return(false);
            }
            if (ac1FCount < ac2FCount)
            {
                return(false);
            }
            if (ac1ClCount < ac2ClCount)
            {
                return(false);
            }
            if (ac1BrCount < ac2BrCount)
            {
                return(false);
            }
            if (ac1ICount < ac2ICount)
            {
                return(false);
            }
            if (ac1CCount < ac2CCount)
            {
                return(false);
            }


            return(true);
        }
Example #13
0
        /// <summary> Kruskal algorithm</summary>
        /// <param name="atomContainer">
        /// </param>
        public virtual void buildSpanningTree(IAtomContainer atomContainer)
        {
            disconnected = false;
            molecule     = atomContainer;

            V = atomContainer.AtomCount;
            E = atomContainer.getBondCount();

            sptSize = 0; edrSize = 0;
            fastFindInit(V);
            for (int i = 0; i < V; i++)
            {
                (atomContainer.getAtomAt(i)).setProperty("ST_ATOMNO", System.Convert.ToString(i + 1));
            }
            IBond bond;
            int   v1, v2;

            bondsInTree = new bool[E];

            for (int b = 0; b < E; b++)
            {
                bondsInTree[b] = false;
                bond           = atomContainer.getBondAt(b);
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                v1 = System.Int32.Parse((bond.getAtomAt(0)).getProperty("ST_ATOMNO").ToString());
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                v2 = System.Int32.Parse((bond.getAtomAt(1)).getProperty("ST_ATOMNO").ToString());
                //this below is a little bit  slower
                //v1 = atomContainer.getAtomNumber(bond.getAtomAt(0))+1;
                //v2 = atomContainer.getAtomNumber(bond.getAtomAt(1))+1;
                if (fastfind(v1, v2, true))
                {
                    bondsInTree[b] = true;
                    sptSize++;
                    //System.out.println("ST : includes bond between atoms "+v1+","+v2);
                }
                if (sptSize >= (V - 1))
                {
                    break;
                }
            }
            // if atomcontainer is connected then the number of bonds in the spanning tree = (No atoms-1)
            //i.e.  edgesRings = new Bond[E-V+1];
            //but to hold all bonds if atomContainer was disconnected then  edgesRings = new Bond[E-sptSize];
            if (sptSize != (V - 1))
            {
                disconnected = true;
            }
            for (int b = 0; b < E; b++)
            {
                if (!bondsInTree[b])
                {
                    //			edgesRings[edrSize] = atomContainer.getBondAt(b);
                    edrSize++;
                }
            }
            cb = new int[edrSize][];
            for (int i2 = 0; i2 < edrSize; i2++)
            {
                cb[i2] = new int[E];
            }
            for (int i = 0; i < edrSize; i++)
            {
                for (int a = 0; a < E; a++)
                {
                    cb[i][a] = 0;
                }
            }
        }
        /// <summary> Produces an AtomContainer without explicit Hs but with H count from one with Hs.
        /// The new molecule is a deep copy.
        /// 
        /// </summary>
        /// <param name="atomContainer">The AtomContainer from which to remove the hydrogens
        /// </param>
        /// <returns>              The molecule without Hs.
        /// </returns>
        /// <cdk.keyword>          hydrogen, removal </cdk.keyword>
        public static IAtomContainer removeHydrogens(IAtomContainer atomContainer)
        {
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.IDictionary map = new System.Collections.Hashtable(); // maps original atoms to clones.
            System.Collections.IList remove = new System.Collections.ArrayList(); // lists removed Hs.

            // Clone atoms except those to be removed.
            IMolecule mol = atomContainer.Builder.newMolecule();
            int count = atomContainer.AtomCount;
            for (int i = 0; i < count; i++)
            {
                // Clone/remove this atom?
                IAtom atom = atomContainer.getAtomAt(i);
                if (!atom.Symbol.Equals("H"))
                {
                    IAtom clonedAtom = null;
                    try
                    {
                        clonedAtom = (IAtom)atom.Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clonedAtom.setHydrogenCount(0);
                    mol.addAtom(clonedAtom);
                    map[atom] = clonedAtom;
                }
                else
                {
                    remove.Add(atom); // maintain list of removed H.
                }
            }

            // Clone bonds except those involving removed atoms.
            count = atomContainer.getBondCount();
            for (int i = 0; i < count; i++)
            {
                // Check bond.
                //UPGRADE_NOTE: Final was removed from the declaration of 'bond '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                IBond bond = atomContainer.getBondAt(i);
                IAtom[] atoms = bond.getAtoms();
                bool removedBond = false;
                //UPGRADE_NOTE: Final was removed from the declaration of 'length '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                int length = atoms.Length;
                for (int k = 0; k < length; k++)
                {
                    if (remove.Contains(atoms[k]))
                    {
                        removedBond = true;
                        break;
                    }
                }

                // Clone/remove this bond?
                if (!removedBond)
                // if (!remove.contains(atoms[0]) && !remove.contains(atoms[1]))
                {
                    IBond clone = null;
                    try
                    {
                        clone = (IBond)atomContainer.getBondAt(i).Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clone.setAtoms(new IAtom[] { (IAtom)map[atoms[0]], (IAtom)map[atoms[1]] });
                    mol.addBond(clone);
                }
            }

            // Recompute hydrogen counts of neighbours of removed Hydrogens.
            //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
            for (System.Collections.IEnumerator i = remove.GetEnumerator(); i.MoveNext(); )
            {
                // Process neighbours.
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
                for (System.Collections.IEnumerator n = atomContainer.getConnectedAtomsVector((IAtom)i.Current).GetEnumerator(); n.MoveNext(); )
                {
                    //UPGRADE_NOTE: Final was removed from the declaration of 'neighb '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                    //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                    IAtom neighb = (IAtom)map[n.Current];
                    neighb.setHydrogenCount(neighb.getHydrogenCount() + 1);
                }
            }
            mol.Properties = atomContainer.Properties;
            mol.Flags = atomContainer.Flags;

            return (mol);
        }
		public virtual void  saturateRingSystems(IAtomContainer atomContainer)
		{
			IRingSet rs = new SSSRFinder(atomContainer.Builder.newMolecule(atomContainer)).findSSSR();
			System.Collections.ArrayList ringSets = RingPartitioner.partitionRings(rs);
			IAtomContainer ac = null;
			IAtom atom = null;
			int[] temp;
			for (int f = 0; f < ringSets.Count; f++)
			{
				rs = (IRingSet) ringSets[f];
				ac = RingSetManipulator.getAllInOneContainer(rs);
				temp = new int[ac.AtomCount];
				for (int g = 0; g < ac.AtomCount; g++)
				{
					atom = ac.getAtomAt(g);
					temp[g] = atom.getHydrogenCount();
					atom.setHydrogenCount(atomContainer.getBondCount(atom) - ac.getBondCount(atom) - temp[g]);
				}
				saturate(ac);
				for (int g = 0; g < ac.AtomCount; g++)
				{
					atom = ac.getAtomAt(g);
					atom.setHydrogenCount(temp[g]);
				}
			}
		}
Example #16
0
 /// <summary>  Removes all external aliphatic chains by chopping them off from the
 /// ends
 /// 
 /// </summary>
 /// <param name="ac">               The AtomContainer to work with
 /// </param>
 /// <exception cref="CDKException"> An exception thrown if something goes wrong or if the timeout limit is reached
 /// </exception>
 private void removeAliphatic(IAtomContainer ac)
 {
     bool removedSomething;
     IAtom atom = null;
     do
     {
         removedSomething = false;
         //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
         for (System.Collections.IEnumerator e = ac.atoms(); e.MoveNext(); )
         {
             //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
             atom = (IAtom)e.Current;
             if (ac.getBondCount(atom) == 1)
             {
                 ac.removeAtomAndConnectedElectronContainers(atom);
                 removedSomething = true;
             }
         }
     }
     while (removedSomething);
 }
        /// <summary>  Parse a branch
        /// 
        /// </summary>
        /// <param name="v">                       Description of Parameter
        /// </param>
        /// <param name="buffer">                  Description of Parameter
        /// </param>
        /// <param name="container">               Description of Parameter
        /// </param>
        /// <param name="parent">                  Description of Parameter
        /// </param>
        /// <param name="chiral">                  Description of Parameter
        /// </param>
        /// <param name="doubleBondConfiguration"> Description of Parameter
        /// </param>
        /// <param name="atomsInOrderOfSmiles">    Description of Parameter
        /// </param>
        private void parseChain(System.Collections.ArrayList v, System.Text.StringBuilder buffer, IAtomContainer container, IAtom parent, bool chiral, bool[] doubleBondConfiguration, System.Collections.ArrayList atomsInOrderOfSmiles)
        {
            int positionInVector = 0;
            IAtom atom;
            //System.out.println("in parse chain. Size of tree: " + v.size());
            for (int h = 0; h < v.Count; h++)
            {
                System.Object o = v[h];
                if (o is IAtom)
                {
                    atom = (IAtom)o;
                    if (parent != null)
                    {
                        parseBond(buffer, atom, parent, container);
                    }
                    else
                    {
                        if (chiral && BondTools.isStereo(container, atom))
                        {
                            parent = (IAtom)((System.Collections.ArrayList)v[1])[0];
                        }
                    }
                    parseAtom(atom, buffer, container, chiral, doubleBondConfiguration, parent, atomsInOrderOfSmiles, v);
                    //System.out.println("in parseChain after parseAtom()");
                    /*
                    *  The principle of making chiral smiles is quite simple, although the code is
                    *  pretty uggly. The Atoms connected to the chiral center are put in sorted[] in the
                    *  order they have to appear in the smiles. Then the Vector v is rearranged according
                    *  to sorted[]
                    */
                    if (chiral && BondTools.isStereo(container, atom) && container.getBond(parent, atom) != null)
                    {
                        //System.out.println("in parseChain in isChiral");
                        IAtom[] sorted = null;
                        System.Collections.IList chiralNeighbours = container.getConnectedAtomsVector(atom);
                        if (BondTools.isTetrahedral(container, atom, false) > 0)
                        {
                            sorted = new IAtom[3];
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 1)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UNDEFINED || container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_NONE)
                            {
                                bool normalBindingIsLeft = false;
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                        {
                                            if (BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom))
                                            {
                                                normalBindingIsLeft = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (normalBindingIsLeft)
                                        {
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                            {
                                                sorted[0] = (IAtom)chiralNeighbours[i];
                                            }
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP)
                                            {
                                                sorted[2] = (IAtom)chiralNeighbours[i];
                                            }
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                            {
                                                sorted[1] = (IAtom)chiralNeighbours[i];
                                            }
                                        }
                                        else
                                        {
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP)
                                            {
                                                sorted[1] = (IAtom)chiralNeighbours[i];
                                            }
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                            {
                                                sorted[0] = (IAtom)chiralNeighbours[i];
                                            }
                                            if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                            {
                                                sorted[2] = (IAtom)chiralNeighbours[i];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 2)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                double angle1 = 0;
                                double angle2 = 0;
                                IAtom atom1 = null;
                                IAtom atom2 = null;
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            if (angle1 == 0)
                                            {
                                                angle1 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom1 = (IAtom)chiralNeighbours[i];
                                            }
                                            else
                                            {
                                                angle2 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom2 = (IAtom)chiralNeighbours[i];
                                            }
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                                if (angle1 < angle2)
                                {
                                    sorted[0] = atom2;
                                    sorted[2] = atom1;
                                }
                                else
                                {
                                    sorted[0] = atom1;
                                    sorted[2] = atom2;
                                }
                            }
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 3)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                //UPGRADE_ISSUE: Class hierarchy differences between 'java.util.TreeMap' and 'System.Collections.SortedList' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
                                //UPGRADE_TODO: Constructor 'java.util.TreeMap.TreeMap' was converted to 'System.Collections.SortedList' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapTreeMap'"
                                System.Collections.SortedList hm = new System.Collections.SortedList();
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                    {
                                        hm[(double)BondTools.giveAngle(atom, parent, ((IAtom)chiralNeighbours[i]))] = (System.Int32)i;
                                    }
                                }
                                //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                                System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                                for (int i = ohere.Length - 1; i > -1; i--)
                                {
                                    sorted[i] = ((IAtom)chiralNeighbours[((System.Int32)ohere[i])]);
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == 0)
                            {
                                double angle1 = 0;
                                double angle2 = 0;
                                IAtom atom1 = null;
                                IAtom atom2 = null;
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            if (angle1 == 0)
                                            {
                                                angle1 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom1 = (IAtom)chiralNeighbours[i];
                                            }
                                            else
                                            {
                                                angle2 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom2 = (IAtom)chiralNeighbours[i];
                                            }
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                                if (angle1 < angle2)
                                {
                                    sorted[1] = atom2;
                                    sorted[2] = atom1;
                                }
                                else
                                {
                                    sorted[1] = atom1;
                                    sorted[2] = atom2;
                                }
                            }
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 4)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                //UPGRADE_ISSUE: Class hierarchy differences between 'java.util.TreeMap' and 'System.Collections.SortedList' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
                                //UPGRADE_TODO: Constructor 'java.util.TreeMap.TreeMap' was converted to 'System.Collections.SortedList' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapTreeMap'"
                                System.Collections.SortedList hm = new System.Collections.SortedList();
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                    {
                                        hm[(double)BondTools.giveAngle(atom, parent, ((IAtom)chiralNeighbours[i]))] = (System.Int32)i;
                                    }
                                }
                                //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                                System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                                for (int i = ohere.Length - 1; i > -1; i--)
                                {
                                    sorted[i] = ((IAtom)chiralNeighbours[((System.Int32)ohere[i])]);
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == 0)
                            {
                                double angle1 = 0;
                                double angle2 = 0;
                                IAtom atom1 = null;
                                IAtom atom2 = null;
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0 && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            if (angle1 == 0)
                                            {
                                                angle1 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom1 = (IAtom)chiralNeighbours[i];
                                            }
                                            else
                                            {
                                                angle2 = BondTools.giveAngle(atom, parent, (IAtom)chiralNeighbours[i]);
                                                atom2 = (IAtom)chiralNeighbours[i];
                                            }
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                                if (angle1 < angle2)
                                {
                                    sorted[1] = atom2;
                                    sorted[0] = atom1;
                                }
                                else
                                {
                                    sorted[1] = atom1;
                                    sorted[0] = atom2;
                                }
                            }
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 5)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP)
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UNDEFINED || container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_NONE)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                        }
                        if (BondTools.isTetrahedral(container, atom, false) == 6)
                        {
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP)
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == 0)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UNDEFINED || container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_NONE)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[2] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_UP && !BondTools.isLeft(((IAtom)chiralNeighbours[i]), parent, atom) && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond((IAtom)chiralNeighbours[i], atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                        {
                                            sorted[1] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                            }
                        }
                        if (BondTools.isSquarePlanar(container, atom))
                        {
                            sorted = new IAtom[3];
                            //This produces a U=SP1 order in every case
                            //UPGRADE_ISSUE: Class hierarchy differences between 'java.util.TreeMap' and 'System.Collections.SortedList' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
                            //UPGRADE_TODO: Constructor 'java.util.TreeMap.TreeMap' was converted to 'System.Collections.SortedList' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapTreeMap'"
                            System.Collections.SortedList hm = new System.Collections.SortedList();
                            for (int i = 0; i < chiralNeighbours.Count; i++)
                            {
                                if (chiralNeighbours[i] != parent && !isBondBroken((IAtom)chiralNeighbours[i], atom))
                                {
                                    hm[(double)BondTools.giveAngle(atom, parent, ((IAtom)chiralNeighbours[i]))] = (System.Int32)i;
                                }
                            }
                            //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                            System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                            for (int i = 0; i < ohere.Length; i++)
                            {
                                sorted[i] = ((IAtom)chiralNeighbours[((System.Int32)ohere[i])]);
                            }
                        }
                        if (BondTools.isTrigonalBipyramidalOrOctahedral(container, atom) != 0)
                        {
                            sorted = new IAtom[container.getConnectedAtoms(atom).Length - 1];
                            //UPGRADE_ISSUE: Class hierarchy differences between 'java.util.TreeMap' and 'System.Collections.SortedList' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
                            //UPGRADE_TODO: Constructor 'java.util.TreeMap.TreeMap' was converted to 'System.Collections.SortedList' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapTreeMap'"
                            System.Collections.SortedList hm = new System.Collections.SortedList();
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_UP)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == 0)
                                    {
                                        hm[(double)BondTools.giveAngle(atom, parent, ((IAtom)chiralNeighbours[i]))] = (System.Int32)i;
                                    }
                                    if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                    {
                                        sorted[sorted.Length - 1] = (IAtom)chiralNeighbours[i];
                                    }
                                }
                                //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                                System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                                for (int i = 0; i < ohere.Length; i++)
                                {
                                    sorted[i] = ((IAtom)chiralNeighbours[((System.Int32)ohere[i])]);
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == CDKConstants.STEREO_BOND_DOWN)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == 0)
                                    {
                                        hm[(double)BondTools.giveAngle(atom, parent, ((IAtom)chiralNeighbours[i]))] = (System.Int32)i;
                                    }
                                    if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == CDKConstants.STEREO_BOND_UP)
                                    {
                                        sorted[sorted.Length - 1] = (IAtom)chiralNeighbours[i];
                                    }
                                }
                                //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                                System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                                for (int i = 0; i < ohere.Length; i++)
                                {
                                    sorted[i] = ((IAtom)chiralNeighbours[((System.Int32)ohere[i])]);
                                }
                            }
                            if (container.getBond(parent, atom).Stereo == 0)
                            {
                                for (int i = 0; i < chiralNeighbours.Count; i++)
                                {
                                    if (chiralNeighbours[i] != parent)
                                    {
                                        if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == 0)
                                        {
                                            hm[(double)(BondTools.giveAngleFromMiddle(atom, parent, ((IAtom)chiralNeighbours[i])))] = (System.Int32)i;
                                        }
                                        if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == CDKConstants.STEREO_BOND_UP)
                                        {
                                            sorted[0] = (IAtom)chiralNeighbours[i];
                                        }
                                        if (container.getBond(atom, (IAtom)chiralNeighbours[i]).Stereo == CDKConstants.STEREO_BOND_DOWN)
                                        {
                                            sorted[sorted.Length - 2] = (IAtom)chiralNeighbours[i];
                                        }
                                    }
                                }
                                //UPGRADE_TODO: Method 'java.util.TreeMap.values' was converted to 'System.Collections.SortedList.Values' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilTreeMapvalues'"
                                System.Object[] ohere = SupportClass.ICollectionSupport.ToArray(hm.Values);
                                sorted[sorted.Length - 1] = ((IAtom)chiralNeighbours[((System.Int32)ohere[ohere.Length - 1])]);
                                if (ohere.Length == 2)
                                {
                                    sorted[sorted.Length - 3] = ((IAtom)chiralNeighbours[((System.Int32)ohere[0])]);
                                    if (BondTools.giveAngleFromMiddle(atom, parent, ((IAtom)chiralNeighbours[((System.Int32)ohere[1])])) < 0)
                                    {
                                        IAtom dummy = sorted[sorted.Length - 2];
                                        sorted[sorted.Length - 2] = sorted[0];
                                        sorted[0] = dummy;
                                    }
                                }
                                if (ohere.Length == 3)
                                {
                                    sorted[sorted.Length - 3] = sorted[sorted.Length - 2];
                                    sorted[sorted.Length - 2] = ((IAtom)chiralNeighbours[((System.Int32)ohere[ohere.Length - 2])]);
                                    sorted[sorted.Length - 4] = ((IAtom)chiralNeighbours[((System.Int32)ohere[ohere.Length - 3])]);
                                }
                            }
                        }
                        //This builds an onew[] containing the objects after the center of the chirality in the order given by sorted[]
                        if (sorted != null)
                        {
                            int numberOfAtoms = 3;
                            if (BondTools.isTrigonalBipyramidalOrOctahedral(container, atom) != 0)
                            {
                                numberOfAtoms = container.getConnectedAtoms(atom).Length - 1;
                            }
                            System.Object[] omy = new System.Object[numberOfAtoms];
                            System.Object[] onew = new System.Object[numberOfAtoms];
                            for (int k = getRingOpenings(atom, null).Count; k < numberOfAtoms; k++)
                            {
                                if (positionInVector + 1 + k - getRingOpenings(atom, null).Count < v.Count)
                                {
                                    omy[k] = v[positionInVector + 1 + k - getRingOpenings(atom, null).Count];
                                }
                            }
                            for (int k = 0; k < sorted.Length; k++)
                            {
                                if (sorted[k] != null)
                                {
                                    for (int m = 0; m < omy.Length; m++)
                                    {
                                        if (omy[m] is IAtom)
                                        {
                                            if (omy[m] == sorted[k])
                                            {
                                                onew[k] = omy[m];
                                            }
                                        }
                                        else
                                        {
                                            if (omy[m] == null)
                                            {
                                                onew[k] = null;
                                            }
                                            else
                                            {
                                                if (((System.Collections.ArrayList)omy[m])[0] == sorted[k])
                                                {
                                                    onew[k] = omy[m];
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    onew[k] = null;
                                }
                            }
                            //This is a workaround for 3624.MOL.2 I don't have a better solution currently
                            bool doubleentry = false;
                            for (int m = 0; m < onew.Length; m++)
                            {
                                for (int k = 0; k < onew.Length; k++)
                                {
                                    if (m != k && onew[k] == onew[m])
                                    {
                                        doubleentry = true;
                                    }
                                }
                            }
                            if (!doubleentry)
                            {
                                //Make sure that the first atom in onew is the first one in the original smiles order. This is important to have a canonical smiles.
                                if (positionInVector + 1 < v.Count)
                                {
                                    System.Object atomAfterCenterInOriginalSmiles = v[positionInVector + 1];
                                    int l = 0;
                                    while (onew[0] != atomAfterCenterInOriginalSmiles)
                                    {
                                        System.Object placeholder = onew[onew.Length - 1];
                                        for (int k = onew.Length - 2; k > -1; k--)
                                        {
                                            onew[k + 1] = onew[k];
                                        }
                                        onew[0] = placeholder;
                                        l++;
                                        if (l > onew.Length)
                                        {
                                            break;
                                        }
                                    }
                                }
                                //This cares about ring openings. Here the ring closure (represendted by a figure) must be the first atom. In onew the closure is null.
                                if (getRingOpenings(atom, null).Count > 0)
                                {
                                    int l = 0;
                                    while (onew[0] != null)
                                    {
                                        System.Object placeholder = onew[0];
                                        for (int k = 1; k < onew.Length; k++)
                                        {
                                            onew[k - 1] = onew[k];
                                        }
                                        onew[onew.Length - 1] = placeholder;
                                        l++;
                                        if (l > onew.Length)
                                        {
                                            break;
                                        }
                                    }
                                }
                                //The last in onew is a vector: This means we need to exchange the rest of the original smiles with the rest of this vector.
                                if (onew[numberOfAtoms - 1] is System.Collections.ArrayList)
                                {
                                    for (int i = 0; i < numberOfAtoms; i++)
                                    {
                                        if (onew[i] is IAtom)
                                        {
                                            System.Collections.ArrayList vtemp = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                                            vtemp.Add(onew[i]);
                                            for (int k = positionInVector + 1 + numberOfAtoms; k < v.Count; k++)
                                            {
                                                vtemp.Add(v[k]);
                                            }
                                            onew[i] = vtemp;
                                            for (int k = v.Count - 1; k > positionInVector + 1 + numberOfAtoms - 1; k--)
                                            {
                                                v.RemoveAt(k);
                                            }
                                            for (int k = 1; k < ((System.Collections.ArrayList)onew[numberOfAtoms - 1]).Count; k++)
                                            {
                                                v.Add(((System.Collections.ArrayList)onew[numberOfAtoms - 1])[k]);
                                            }
                                            onew[numberOfAtoms - 1] = ((System.Collections.ArrayList)onew[numberOfAtoms - 1])[0];
                                            break;
                                        }
                                    }
                                }
                                //Put the onew objects in the original Vector
                                int k2 = 0;
                                for (int m = 0; m < onew.Length; m++)
                                {
                                    if (onew[m] != null)
                                    {
                                        v[positionInVector + 1 + k2] = onew[m];
                                        k2++;
                                    }
                                }
                            }
                        }
                    }
                    parent = atom;
                }
                else
                {
                    //Have Vector
                    //System.out.println("in parseChain after else");
                    bool brackets = true;
                    System.Collections.ArrayList result = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                    addAtoms((System.Collections.ArrayList)o, result);
                    if (isRingOpening(parent, result) && container.getBondCount(parent) < 4)
                    {
                        brackets = false;
                    }
                    if (brackets)
                    {
                        buffer.Append('(');
                    }
                    parseChain((System.Collections.ArrayList)o, buffer, container, parent, chiral, doubleBondConfiguration, atomsInOrderOfSmiles);
                    if (brackets)
                    {
                        buffer.Append(')');
                    }
                }

                positionInVector++;
                //System.out.println("in parseChain after positionVector++");
            }
        }
Example #18
0
        /// <summary>  Build edges of the RGraphs
        /// This method create the edge of the RGraph and
        /// calculates the incompatibility and neighbourhood
        /// relationships between RGraph nodes.
        ///
        /// </summary>
        /// <param name="gr">  the rGraph
        /// </param>
        /// <param name="ac1"> Description of the first molecule
        /// </param>
        /// <param name="ac2"> Description of the second molecule
        /// </param>
        private static void arcConstructor(RGraph gr, IAtomContainer ac1, IAtomContainer ac2)
        {
            // each node is incompatible with himself
            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];
                SupportClass.BitArraySupport.Set(x.Forbidden, i);
            }

            IBond a1;
            IBond a2;
            IBond b1;
            IBond b2;

            IBond[] bondsA1 = ac1.Bonds;
            IBond[] bondsA2 = ac2.Bonds;

            gr.FirstGraphSize  = ac1.getBondCount();
            gr.SecondGraphSize = ac2.getBondCount();

            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];

                // two nodes are neighbours if their adjacency
                // relationship in are equivalent in G1 and G2
                // else they are incompatible.
                for (int j = i + 1; j < gr.Graph.Count; j++)
                {
                    if (timeout > -1 && ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start) > timeout)
                    {
                        throw new CDKException("Timeout exceeded in getOverlaps");
                    }
                    RNode y = (RNode)gr.Graph[j];

                    a1 = bondsA1[((RNode)gr.Graph[i]).RMap.Id1];
                    a2 = bondsA2[((RNode)gr.Graph[i]).RMap.Id2];
                    b1 = bondsA1[((RNode)gr.Graph[j]).RMap.Id1];
                    b2 = bondsA2[((RNode)gr.Graph[j]).RMap.Id2];

                    if (a2 is IQueryBond)
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || !queryAdjacency(a1, b1, a2, b2))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                    else
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || (!getCommonSymbol(a1, b1).Equals(getCommonSymbol(a2, b2))))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                }
            }
        }
        /// <summary>  Build edges of the RGraphs
        /// This method create the edge of the RGraph and
        /// calculates the incompatibility and neighbourhood
        /// relationships between RGraph nodes.
        /// 
        /// </summary>
        /// <param name="gr">  the rGraph
        /// </param>
        /// <param name="ac1"> Description of the first molecule
        /// </param>
        /// <param name="ac2"> Description of the second molecule
        /// </param>
        private static void arcConstructor(RGraph gr, IAtomContainer ac1, IAtomContainer ac2)
        {
            // each node is incompatible with himself
            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];
                SupportClass.BitArraySupport.Set(x.Forbidden, i);
            }

            IBond a1;
            IBond a2;
            IBond b1;
            IBond b2;

            IBond[] bondsA1 = ac1.Bonds;
            IBond[] bondsA2 = ac2.Bonds;

            gr.FirstGraphSize = ac1.getBondCount();
            gr.SecondGraphSize = ac2.getBondCount();

            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];

                // two nodes are neighbours if their adjacency
                // relationship in are equivalent in G1 and G2
                // else they are incompatible.
                for (int j = i + 1; j < gr.Graph.Count; j++)
                {
                    if (timeout > -1 && ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start) > timeout)
                        throw new CDKException("Timeout exceeded in getOverlaps");
                    RNode y = (RNode)gr.Graph[j];

                    a1 = bondsA1[((RNode)gr.Graph[i]).RMap.Id1];
                    a2 = bondsA2[((RNode)gr.Graph[i]).RMap.Id2];
                    b1 = bondsA1[((RNode)gr.Graph[j]).RMap.Id1];
                    b2 = bondsA2[((RNode)gr.Graph[j]).RMap.Id2];

                    if (a2 is IQueryBond)
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || !queryAdjacency(a1, b1, a2, b2))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                    else
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || (!getCommonSymbol(a1, b1).Equals(getCommonSymbol(a2, b2))))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                }
            }
        }
        /// <summary>  Transforms an AtomContainer into a BitSet (which's size = number of bond
        /// in the atomContainer, all the bit are set to true)
        /// 
        /// </summary>
        /// <param name="ac"> AtomContainer to transform
        /// </param>
        /// <returns>     The bitSet
        /// </returns>
        public static System.Collections.BitArray getBitSet(IAtomContainer ac)
        {
            System.Collections.BitArray bs;
            int n = ac.getBondCount();

            if (n != 0)
            {
                bs = new System.Collections.BitArray((n % 64 == 0 ? n / 64 : n / 64 + 1) * 64);
                for (int i = 0; i < n; i++)
                {
                    SupportClass.BitArraySupport.Set(bs, i);
                }
            }
            else
            {
                bs = new System.Collections.BitArray(64);
            }

            return bs;
        }
        /// <summary>  Checks some simple heuristics for whether the subgraph query can
        /// realistically be a subgraph of the supergraph. If, for example, the
        /// number of nitrogen atoms in the query is larger than that of the supergraph
        /// it cannot be part of it.
        /// 
        /// </summary>
        /// <param name="ac1"> the supergraph to be checked
        /// </param>
        /// <param name="ac2"> the subgraph to be tested for
        /// </param>
        /// <returns>    true if the subgraph ac2 has a chance to be a subgraph of ac1
        /// 
        /// </returns>

        private static bool testSubgraphHeuristics(IAtomContainer ac1, IAtomContainer ac2)
        {
            int ac1SingleBondCount = 0;
            int ac1DoubleBondCount = 0;
            int ac1TripleBondCount = 0;
            int ac1AromaticBondCount = 0;
            int ac2SingleBondCount = 0;
            int ac2DoubleBondCount = 0;
            int ac2TripleBondCount = 0;
            int ac2AromaticBondCount = 0;
            int ac1SCount = 0;
            int ac1OCount = 0;
            int ac1NCount = 0;
            int ac1FCount = 0;
            int ac1ClCount = 0;
            int ac1BrCount = 0;
            int ac1ICount = 0;
            int ac1CCount = 0;

            int ac2SCount = 0;
            int ac2OCount = 0;
            int ac2NCount = 0;
            int ac2FCount = 0;
            int ac2ClCount = 0;
            int ac2BrCount = 0;
            int ac2ICount = 0;
            int ac2CCount = 0;

            IBond bond;
            IAtom atom;
            for (int i = 0; i < ac1.getBondCount(); i++)
            {
                bond = ac1.getBondAt(i);
                if (bond.getFlag(CDKConstants.ISAROMATIC))
                    ac1AromaticBondCount++;
                else if (bond.Order == 1)
                    ac1SingleBondCount++;
                else if (bond.Order == 2)
                    ac1DoubleBondCount++;
                else if (bond.Order == 3)
                    ac1TripleBondCount++;
            }
            for (int i = 0; i < ac2.getBondCount(); i++)
            {
                bond = ac2.getBondAt(i);
                if (bond is IQueryBond)
                    continue;
                if (bond.getFlag(CDKConstants.ISAROMATIC))
                    ac2AromaticBondCount++;
                else if (bond.Order == 1)
                    ac2SingleBondCount++;
                else if (bond.Order == 2)
                    ac2DoubleBondCount++;
                else if (bond.Order == 3)
                    ac2TripleBondCount++;
            }

            if (ac2SingleBondCount > ac1SingleBondCount)
                return false;
            if (ac2AromaticBondCount > ac1AromaticBondCount)
                return false;
            if (ac2DoubleBondCount > ac1DoubleBondCount)
                return false;
            if (ac2TripleBondCount > ac1TripleBondCount)
                return false;

            for (int i = 0; i < ac1.AtomCount; i++)
            {
                atom = ac1.getAtomAt(i);
                if (atom.Symbol.Equals("S"))
                    ac1SCount++;
                else if (atom.Symbol.Equals("N"))
                    ac1NCount++;
                else if (atom.Symbol.Equals("O"))
                    ac1OCount++;
                else if (atom.Symbol.Equals("F"))
                    ac1FCount++;
                else if (atom.Symbol.Equals("Cl"))
                    ac1ClCount++;
                else if (atom.Symbol.Equals("Br"))
                    ac1BrCount++;
                else if (atom.Symbol.Equals("I"))
                    ac1ICount++;
                else if (atom.Symbol.Equals("C"))
                    ac1CCount++;
            }
            for (int i = 0; i < ac2.AtomCount; i++)
            {
                atom = ac2.getAtomAt(i);
                if (atom is IQueryAtom)
                    continue;
                if (atom.Symbol.Equals("S"))
                    ac2SCount++;
                else if (atom.Symbol.Equals("N"))
                    ac2NCount++;
                else if (atom.Symbol.Equals("O"))
                    ac2OCount++;
                else if (atom.Symbol.Equals("F"))
                    ac2FCount++;
                else if (atom.Symbol.Equals("Cl"))
                    ac2ClCount++;
                else if (atom.Symbol.Equals("Br"))
                    ac2BrCount++;
                else if (atom.Symbol.Equals("I"))
                    ac2ICount++;
                else if (atom.Symbol.Equals("C"))
                    ac2CCount++;
            }

            if (ac1SCount < ac2SCount)
                return false;
            if (ac1NCount < ac2NCount)
                return false;
            if (ac1OCount < ac2OCount)
                return false;
            if (ac1FCount < ac2FCount)
                return false;
            if (ac1ClCount < ac2ClCount)
                return false;
            if (ac1BrCount < ac2BrCount)
                return false;
            if (ac1ICount < ac2ICount)
                return false;
            if (ac1CCount < ac2CCount)
                return false;


            return true;
        }
Example #22
0
        /// <summary> Produces an AtomContainer without explicit Hs but with H count from one with Hs.
        /// The new molecule is a deep copy.
        ///
        /// </summary>
        /// <param name="atomContainer">The AtomContainer from which to remove the hydrogens
        /// </param>
        /// <returns>              The molecule without Hs.
        /// </returns>
        /// <cdk.keyword>          hydrogen, removal </cdk.keyword>
        public static IAtomContainer removeHydrogens(IAtomContainer atomContainer)
        {
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.IDictionary map    = new System.Collections.Hashtable(); // maps original atoms to clones.
            System.Collections.IList       remove = new System.Collections.ArrayList(); // lists removed Hs.

            // Clone atoms except those to be removed.
            IMolecule mol   = atomContainer.Builder.newMolecule();
            int       count = atomContainer.AtomCount;

            for (int i = 0; i < count; i++)
            {
                // Clone/remove this atom?
                IAtom atom = atomContainer.getAtomAt(i);
                if (!atom.Symbol.Equals("H"))
                {
                    IAtom clonedAtom = null;
                    try
                    {
                        clonedAtom = (IAtom)atom.Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clonedAtom.setHydrogenCount(0);
                    mol.addAtom(clonedAtom);
                    map[atom] = clonedAtom;
                }
                else
                {
                    remove.Add(atom); // maintain list of removed H.
                }
            }

            // Clone bonds except those involving removed atoms.
            count = atomContainer.getBondCount();
            for (int i = 0; i < count; i++)
            {
                // Check bond.
                //UPGRADE_NOTE: Final was removed from the declaration of 'bond '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                IBond   bond        = atomContainer.getBondAt(i);
                IAtom[] atoms       = bond.getAtoms();
                bool    removedBond = false;
                //UPGRADE_NOTE: Final was removed from the declaration of 'length '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                int length = atoms.Length;
                for (int k = 0; k < length; k++)
                {
                    if (remove.Contains(atoms[k]))
                    {
                        removedBond = true;
                        break;
                    }
                }

                // Clone/remove this bond?
                if (!removedBond)
                // if (!remove.contains(atoms[0]) && !remove.contains(atoms[1]))
                {
                    IBond clone = null;
                    try
                    {
                        clone = (IBond)atomContainer.getBondAt(i).Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clone.setAtoms(new IAtom[] { (IAtom)map[atoms[0]], (IAtom)map[atoms[1]] });
                    mol.addBond(clone);
                }
            }

            // Recompute hydrogen counts of neighbours of removed Hydrogens.
            //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
            for (System.Collections.IEnumerator i = remove.GetEnumerator(); i.MoveNext();)
            {
                // Process neighbours.
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
                for (System.Collections.IEnumerator n = atomContainer.getConnectedAtomsVector((IAtom)i.Current).GetEnumerator(); n.MoveNext();)
                {
                    //UPGRADE_NOTE: Final was removed from the declaration of 'neighb '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                    //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                    IAtom neighb = (IAtom)map[n.Current];
                    neighb.setHydrogenCount(neighb.getHydrogenCount() + 1);
                }
            }
            mol.Properties = atomContainer.Properties;
            mol.Flags      = atomContainer.Flags;

            return(mol);
        }
		/**
		 * Very quick and easy isomorphism check.
		 * 
		 * @param molecule1 the molecule1
		 * @param fragsToCompare the frags to compare
		 * 
		 * @return true, if successful
		 */

		private bool identicalAtoms(IAtomContainer molecule1, List<IAtomContainer> fragsToCompare)
		{
			var molFormula = MolecularFormulaTools.GetMolecularFormula(molecule1);
			var molFormulaString = MolecularFormulaTools.GetString(molFormula);

			for (var i = 0; i < fragsToCompare.Count; i++)
			{
				//no match
				if (molecule1.getBondCount() != fragsToCompare[i].getBondCount() && molecule1.getAtomCount() != fragsToCompare[i].getAtomCount())
				{
					continue;
				}

				//Molecular Formula redundancy check
				var molFormulaFrag = MolecularFormulaTools.GetMolecularFormula(fragsToCompare[i]);
				var molFormulaFragString = MolecularFormulaTools.GetString(molFormulaFrag);
				if (molFormulaString.Equals(molFormulaFragString))
				{
					return true;
				}
			}

			//no match found
			return false;
		}
Example #24
0
        /// <summary>
        /// Convert isotope labels to hilighting.
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>

        public string ConvertIsotopeValuesToHilighting(
            IAtomContainer mol)
        {
            bool   hilight;
            string molfile2 = "";
            bool   clearAttachmentPointLabels = true;
            bool   hilightAttachmentBond      = true;

            List <int> atomSet = new List <int>();
            List <int> bondSet = new List <int>();

            for (int bi = 0; bi < mol.getBondCount(); bi++)
            {
                IBond b = mol.getBond(bi);
                if (b.getAtomCount() != 2)
                {
                    continue;
                }

                IAtom a1 = b.getAtom(0);
                IAtom a2 = b.getAtom(1);

                if (hilightAttachmentBond)
                {
                    hilight = (GetMassNumber(a1) != 0 && GetMassNumber(a2) != 0);
                }
                else
                {
                    hilight = (GetMassNumber(a1) < 0 && GetMassNumber(a2) < 0);
                }
                if (hilight)
                {
                    bondSet.Add(bi);
                }
            }

            //String posMap = ""; // debug (note that hydrogens can get repositioned)
            for (int ai = 0; ai < mol.getAtomCount(); ai++)             // scan atoms for hilighting and reset isotope values
            {
                IAtom a = mol.getAtom(ai);

                hilight = GetMassNumber(a) < 0;
                if (hilight)
                {
                    atomSet.Add(ai);
                }

                //posMap += ai.ToString() + ", " + a.getSymbol() + ", " + hilight + "\r\n"; // debug

                int massNo = GetMassNumber(a);
                if (massNo == hilightIsotopeValue)
                {
                    a.setMassNumber(null);                     // set back to original value
                }
                else if (massNo < 0)
                {
                    if (clearAttachmentPointLabels)
                    {
                        a.setMassNumber(null);
                    }

                    else
                    {
                        SetMassNumber(a, -massNo);                      // set back to positive
                    }
                }
            }

            //mol = GenerateCoordinates(mol); // (should already be done)

            try { molfile2 = AtomContainerToMolFileV3000(mol); }
            catch (Exception ex) { ex = ex; }

            if (Lex.IsUndefined(molfile2))             // couldn't convert to v3000, just return unhilighted v2000 file
            {
                molfile2 = AtomContainerToMolfile(mol);
                return(molfile2);
            }

            string txt = "MDLV30/HILITE";

            if (atomSet.Count > 0)
            {
                txt += " " + BuildV3000KeywordList("ATOMS", atomSet);
            }

            if (bondSet.Count > 0)
            {
                txt += " " + BuildV3000KeywordList("BONDS", bondSet);
            }

            txt = BuildV3000Lines(txt);

            bool hasCollection = Lex.Contains(molfile2, "M  V30 END COLLECTION");

            if (hasCollection)             // already have collection begin and end
            {
                txt = txt +
                      "M  V30 END COLLECTION";
                molfile2 = Lex.Replace(molfile2, "M  V30 END COLLECTION", txt);
            }

            else             // add collection begin & end
            {
                txt =
                    "M  V30 BEGIN COLLECTION\n" +
                    txt +
                    "M  V30 END COLLECTION\n" +
                    "M  V30 END CTAB";
                molfile2 = Lex.Replace(molfile2, "M  V30 END CTAB", txt);
            }

            return(molfile2);
        }
Example #25
0
        /// <summary> Kruskal algorithm</summary>
        /// <param name="atomContainer">
        /// </param>
        public virtual void buildSpanningTree(IAtomContainer atomContainer)
        {
            disconnected = false;
            molecule = atomContainer;

            V = atomContainer.AtomCount;
            E = atomContainer.getBondCount();

            sptSize = 0; edrSize = 0;
            fastFindInit(V);
            for (int i = 0; i < V; i++)
            {
                (atomContainer.getAtomAt(i)).setProperty("ST_ATOMNO", System.Convert.ToString(i + 1));
            }
            IBond bond;
            int v1, v2;
            bondsInTree = new bool[E];

            for (int b = 0; b < E; b++)
            {
                bondsInTree[b] = false;
                bond = atomContainer.getBondAt(b);
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                v1 = System.Int32.Parse((bond.getAtomAt(0)).getProperty("ST_ATOMNO").ToString());
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                v2 = System.Int32.Parse((bond.getAtomAt(1)).getProperty("ST_ATOMNO").ToString());
                //this below is a little bit  slower
                //v1 = atomContainer.getAtomNumber(bond.getAtomAt(0))+1; 
                //v2 = atomContainer.getAtomNumber(bond.getAtomAt(1))+1;
                if (fastfind(v1, v2, true))
                {
                    bondsInTree[b] = true;
                    sptSize++;
                    //System.out.println("ST : includes bond between atoms "+v1+","+v2);
                }
                if (sptSize >= (V - 1))
                    break;
            }
            // if atomcontainer is connected then the number of bonds in the spanning tree = (No atoms-1)
            //i.e.  edgesRings = new Bond[E-V+1];
            //but to hold all bonds if atomContainer was disconnected then  edgesRings = new Bond[E-sptSize]; 
            if (sptSize != (V - 1))
                disconnected = true;
            for (int b = 0; b < E; b++)
                if (!bondsInTree[b])
                {
                    //			edgesRings[edrSize] = atomContainer.getBondAt(b);
                    edrSize++;
                }
            cb = new int[edrSize][];
            for (int i2 = 0; i2 < edrSize; i2++)
            {
                cb[i2] = new int[E];
            }
            for (int i = 0; i < edrSize; i++)
                for (int a = 0; a < E; a++)
                    cb[i][a] = 0;
        }
Example #26
0
        /// <summary>
        /// (Converted from Java to C# for possible later modification)
        /// Suppress any explicit hydrogens in the provided container. Only hydrogens
        /// that can be represented as a hydrogen count value on the atom are
        /// suppressed. The container is updated and no elements are copied, please
        /// use either <seealso cref="#copyAndSuppressedHydrogens"/> if you would to preserve
        /// the old instance.
        /// </summary>
        /// <param name="org"> the container from which to remove hydrogens </param>
        /// <returns> the input for convenience </returns>
        /// <seealso cref= #copyAndSuppressedHydrogens </seealso>
        public IAtomContainer suppressHydrogens(IAtomContainer org)
        {
            bool anyHydrogenPresent = false;

            for (int ai = 0; ai < org.getAtomCount(); ai++)
            {
                IAtom atom = org.getAtom(ai);
                if ("H".Equals(atom.getSymbol()))
                {
                    anyHydrogenPresent = true;
                    break;
                }
            }

            if (!anyHydrogenPresent)
            {
                return(org);
            }

            // we need fast adjacency checks (to check for suppression and
            // update hydrogen counts)
            int[][] graph = GraphUtil.toAdjList(org);

            int nOrgAtoms = org.getAtomCount();
            int nOrgBonds = org.getBondCount();

            int nCpyAtoms = 0;
            int nCpyBonds = 0;

            ISet <IAtom> hydrogens = new HashSet <IAtom>();

            IAtom[] cpyAtoms = new IAtom[nOrgAtoms];

            // filter the original container atoms for those that can/can't
            // be suppressed
            for (int v = 0; v < nOrgAtoms; v++)
            {
                IAtom atom = org.getAtom(v);
                if (suppressibleHydrogen(org, graph, v))
                {
                    hydrogens.Add(atom);
                    incrementImplHydrogenCount(org.getAtom(graph[v][0]));
                }
                else
                {
                    cpyAtoms[nCpyAtoms++] = atom;
                }
            }

            // none of the hydrogens could be suppressed - no changes need to be made
            if (hydrogens.Count == 0)
            {
                return(org);
            }

            org.setAtoms(cpyAtoms);

            // we now update the bonds - we have auxiliary variable remaining that
            // bypasses the set membership checks if all suppressed bonds are found
            IBond[] cpyBonds  = new IBond[nOrgBonds - hydrogens.Count];
            int     remaining = hydrogens.Count;

            for (int bi = 0; bi < org.getBondCount(); bi++)
            {
                IBond bond = org.getBond(bi);
                if (remaining > 0 && (hydrogens.Contains(bond.getAtom(0)) || hydrogens.Contains(bond.getAtom(1))))
                {
                    remaining--;
                    continue;
                }
                cpyBonds[nCpyBonds++] = bond;
            }

            // we know how many hydrogens we removed and we should have removed the
            // same number of bonds otherwise the container is strange
            if (nCpyBonds != cpyBonds.Length)
            {
                throw new System.ArgumentException("number of removed bonds was less than the number of removed hydrogens");
            }

            org.setBonds(cpyBonds);

            return(org);
        }
Example #27
0
 /// <summary>  Description of the Method
 /// 
 /// </summary>
 /// <param name="ac">               The AtomContainer to be searched
 /// </param>
 /// <param name="pathes">           A vectoring storing all the pathes
 /// </param>
 /// <param name="ringSet">          A ringset to be extended while we search
 /// </param>
 /// <exception cref="CDKException"> An exception thrown if something goes wrong or if the timeout limit is reached
 /// </exception>
 private void doSearch(IAtomContainer ac, System.Collections.ArrayList pathes, IRingSet ringSet)
 {
     IAtom atom = null;
     /*
     *  First we convert the molecular graph into a a path graph by
     *  creating a set of two membered pathes from all the bonds in the molecule
     */
     initPathGraph(ac, pathes);
     if (debug)
     {
         System.Console.Out.WriteLine("BondCount: " + ac.getBondCount() + ", PathCount: " + pathes.Count);
     }
     do
     {
         atom = selectAtom(ac);
         if (atom != null)
         {
             remove(atom, ac, pathes, ringSet);
         }
     }
     while (pathes.Count > 0 && atom != null);
     if (debug)
     {
         System.Console.Out.WriteLine("pathes.size(): " + pathes.Count);
     }
     if (debug)
     {
         System.Console.Out.WriteLine("ringSet.size(): " + ringSet.AtomContainerCount);
     }
 }
		/// <summary> The method is known to fail for certain compounds. For more information, see
		/// cdk.test.limitations package.
		/// 
		/// </summary>
		public virtual void  saturate(IAtomContainer atomContainer)
		{
			/* newSaturate(atomContainer);
			}
			public void oldSaturate(AtomContainer atomContainer) throws CDKException { */
			IAtom partner = null;
			IAtom atom = null;
			IAtom[] partners = null;
			IAtomType[] atomTypes1 = null;
			IAtomType[] atomTypes2 = null;
			IBond bond = null;
			for (int i = 1; i < 4; i++)
			{
				// handle atoms with degree 1 first and then proceed to higher order
				for (int f = 0; f < atomContainer.AtomCount; f++)
				{
					atom = atomContainer.getAtomAt(f);
					//logger.debug("symbol: ", atom.Symbol);
					atomTypes1 = getAtomTypeFactory(atom.Builder).getAtomTypes(atom.Symbol);
					if (atomTypes1.Length > 0)
					{
						//logger.debug("first atom type: ", atomTypes1[0]);
						if (atomContainer.getBondCount(atom) == i)
						{
							if (atom.getFlag(CDKConstants.ISAROMATIC) && atomContainer.getBondOrderSum(atom) < atomTypes1[0].BondOrderSum - atom.getHydrogenCount())
							{
								partners = atomContainer.getConnectedAtoms(atom);
								for (int g = 0; g < partners.Length; g++)
								{
									partner = partners[g];
									//logger.debug("Atom has " + partners.Length + " partners");
									atomTypes2 = getAtomTypeFactory(atom.Builder).getAtomTypes(partner.Symbol);
									if (atomTypes2.Length == 0)
										return ;
									if (atomContainer.getBond(partner, atom).getFlag(CDKConstants.ISAROMATIC) && atomContainer.getBondOrderSum(partner) < atomTypes2[0].BondOrderSum - partner.getHydrogenCount())
									{
										//logger.debug("Partner has " + atomContainer.getBondOrderSum(partner) + ", may have: " + atomTypes2[0].BondOrderSum);
										bond = atomContainer.getBond(atom, partner);
										//logger.debug("Bond order was " + bond.Order);
										bond.Order = bond.Order + 1;
										//logger.debug("Bond order now " + bond.Order);
										break;
									}
								}
							}
							if (atomContainer.getBondOrderSum(atom) < atomTypes1[0].BondOrderSum - atom.getHydrogenCount())
							{
								//logger.debug("Atom has " + atomContainer.getBondOrderSum(atom) + ", may have: " + atomTypes1[0].BondOrderSum);
								partners = atomContainer.getConnectedAtoms(atom);
								for (int g = 0; g < partners.Length; g++)
								{
									partner = partners[g];
									//logger.debug("Atom has " + partners.Length + " partners");
									atomTypes2 = getAtomTypeFactory(atom.Builder).getAtomTypes(partner.Symbol);
									if (atomTypes2.Length == 0)
										return ;
									if (atomContainer.getBondOrderSum(partner) < atomTypes2[0].BondOrderSum - partner.getHydrogenCount())
									{
										//logger.debug("Partner has " + atomContainer.getBondOrderSum(partner) + ", may have: " + atomTypes2[0].BondOrderSum);
										bond = atomContainer.getBond(atom, partner);
										//logger.debug("Bond order was " + bond.Order);
										bond.Order = bond.Order + 1;
										//logger.debug("Bond order now " + bond.Order);
										break;
									}
								}
							}
						}
					}
				}
			}
		}
Example #29
0
        /// <summary>  Selects an optimal atom for removal
        /// See {@cdk.cite HAN96} for details
        /// 
        /// </summary>
        /// <param name="ac"> The AtomContainer to search
        /// </param>
        /// <returns>     The selected Atom
        /// </returns>
        private IAtom selectAtom(IAtomContainer ac)
        {
            int minDegree = 999;
            // :-)
            int degree = minDegree;
            IAtom minAtom = null;
            IAtom atom = null;
            for (int f = 0; f < ac.AtomCount; f++)
            {
                atom = ac.getAtomAt(f);
                degree = ac.getBondCount(atom);

                if (degree < minDegree)
                {
                    minAtom = atom;
                    minDegree = degree;
                }
            }

            return minAtom;
        }
Example #30
0
        /// <summary> The method is known to fail for certain compounds. For more information, see
        /// cdk.test.limitations package.
        ///
        /// </summary>
        public virtual void  saturate(IAtomContainer atomContainer)
        {
            /* newSaturate(atomContainer);
             * }
             * public void oldSaturate(AtomContainer atomContainer) throws CDKException { */
            IAtom partner = null;
            IAtom atom    = null;

            IAtom[]     partners   = null;
            IAtomType[] atomTypes1 = null;
            IAtomType[] atomTypes2 = null;
            IBond       bond       = null;

            for (int i = 1; i < 4; i++)
            {
                // handle atoms with degree 1 first and then proceed to higher order
                for (int f = 0; f < atomContainer.AtomCount; f++)
                {
                    atom = atomContainer.getAtomAt(f);
                    //logger.debug("symbol: ", atom.Symbol);
                    atomTypes1 = getAtomTypeFactory(atom.Builder).getAtomTypes(atom.Symbol);
                    if (atomTypes1.Length > 0)
                    {
                        //logger.debug("first atom type: ", atomTypes1[0]);
                        if (atomContainer.getBondCount(atom) == i)
                        {
                            if (atom.getFlag(CDKConstants.ISAROMATIC) && atomContainer.getBondOrderSum(atom) < atomTypes1[0].BondOrderSum - atom.getHydrogenCount())
                            {
                                partners = atomContainer.getConnectedAtoms(atom);
                                for (int g = 0; g < partners.Length; g++)
                                {
                                    partner = partners[g];
                                    //logger.debug("Atom has " + partners.Length + " partners");
                                    atomTypes2 = getAtomTypeFactory(atom.Builder).getAtomTypes(partner.Symbol);
                                    if (atomTypes2.Length == 0)
                                    {
                                        return;
                                    }
                                    if (atomContainer.getBond(partner, atom).getFlag(CDKConstants.ISAROMATIC) && atomContainer.getBondOrderSum(partner) < atomTypes2[0].BondOrderSum - partner.getHydrogenCount())
                                    {
                                        //logger.debug("Partner has " + atomContainer.getBondOrderSum(partner) + ", may have: " + atomTypes2[0].BondOrderSum);
                                        bond = atomContainer.getBond(atom, partner);
                                        //logger.debug("Bond order was " + bond.Order);
                                        bond.Order = bond.Order + 1;
                                        //logger.debug("Bond order now " + bond.Order);
                                        break;
                                    }
                                }
                            }
                            if (atomContainer.getBondOrderSum(atom) < atomTypes1[0].BondOrderSum - atom.getHydrogenCount())
                            {
                                //logger.debug("Atom has " + atomContainer.getBondOrderSum(atom) + ", may have: " + atomTypes1[0].BondOrderSum);
                                partners = atomContainer.getConnectedAtoms(atom);
                                for (int g = 0; g < partners.Length; g++)
                                {
                                    partner = partners[g];
                                    //logger.debug("Atom has " + partners.Length + " partners");
                                    atomTypes2 = getAtomTypeFactory(atom.Builder).getAtomTypes(partner.Symbol);
                                    if (atomTypes2.Length == 0)
                                    {
                                        return;
                                    }
                                    if (atomContainer.getBondOrderSum(partner) < atomTypes2[0].BondOrderSum - partner.getHydrogenCount())
                                    {
                                        //logger.debug("Partner has " + atomContainer.getBondOrderSum(partner) + ", may have: " + atomTypes2[0].BondOrderSum);
                                        bond = atomContainer.getBond(atom, partner);
                                        //logger.debug("Bond order was " + bond.Order);
                                        bond.Order = bond.Order + 1;
                                        //logger.debug("Bond order now " + bond.Order);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }