private static void FixPyridineNOxides(IAtomContainer atomContainer, IRingSet ringSet)
        {
            //convert n(=O) to [n+][O-]
            for (int i = 0; i < atomContainer.Atoms.Count; i++)
            {
                var ai = atomContainer.Atoms[i];

                if (ai.AtomicNumber.Equals(AtomicNumbers.N) &&
                    (ai.FormalCharge == null || ai.FormalCharge == 0))
                {
                    if (InRingSet(ai, ringSet))
                    {
                        var ca = atomContainer.GetConnectedAtoms(ai);
                        foreach (var caj in ca)
                        {
                            if (caj.AtomicNumber.Equals(AtomicNumbers.O) &&
                                atomContainer.GetBond(ai, caj).Order == BondOrder.Double)
                            {
                                ai.FormalCharge  = 1;
                                caj.FormalCharge = -1;
                                atomContainer.GetBond(ai, caj).Order = BondOrder.Single;
                            }
                        } // end for (int j=0;j<ca.Count;j++)
                    }     // end if (InRingSet(ai,ringSet)) {
                }         // end if (ai.Symbol.Equals("N") && ai.FormalCharge==0)
            }             // end for (int i=0;i<atomContainer.Atoms.Count;i++)
        }
Exemple #2
0
        /// <summary>
        /// Checks whether the P atom is in a PO environment.
        /// </summary>
        /// <remarks>
        /// This environment is noted in Kier &amp; Hall (1986), page 20
        /// </remarks>
        /// <param name="atom">The P atom in question</param>
        /// <param name="atomContainer">The molecule containing the P atom</param>
        /// <returns>The empirical delta V if present in the above environment, -1 otherwise</returns>
        private static double DeltavPhosphorous(IAtom atom, IAtomContainer atomContainer)
        {
            if (!atom.AtomicNumber.Equals(AtomicNumbers.P))
            {
                return(-1);
            }

            var connected  = atomContainer.GetConnectedAtoms(atom);
            int conditions = 0;

            if (connected.Count() == 4)
            {
                conditions++;
            }

            foreach (var connectedAtom in connected)
            {
                if (connectedAtom.AtomicNumber.Equals(AtomicNumbers.O) &&
                    atomContainer.GetBond(atom, connectedAtom).Order == BondOrder.Double)
                {
                    conditions++;
                }
                if (atomContainer.GetBond(atom, connectedAtom).Order == BondOrder.Single)
                {
                    conditions++;
                }
            }
            if (conditions == 5)
            {
                return(2.22);
            }
            return(-1);
        }
Exemple #3
0
        /// <summary>
        /// Says if an atom is the end of a double bond configuration
        /// </summary>
        /// <param name="atom">The atom which is the end of configuration</param>
        /// <param name="container">The atomContainer the atom is in</param>
        /// <param name="parent">The atom we came from</param>
        /// <param name="doubleBondConfiguration">The array indicating where double bond
        ///     configurations are specified (this method ensures that there is
        ///     actually the possibility of a double bond configuration)</param>
        /// <returns>false=is not end of configuration, true=is</returns>
        private static bool IsEndOfDoubleBond(IAtomContainer container, IAtom atom, IAtom parent,
                                              bool[] doubleBondConfiguration)
        {
            var bondNumber = container.Bonds.IndexOf(container.GetBond(atom, parent));

            if (bondNumber == -1 ||
                doubleBondConfiguration.Length <= bondNumber ||
                !doubleBondConfiguration[bondNumber])
            {
                return(false);
            }

            int hcount = atom.ImplicitHydrogenCount ?? 0;

            int lengthAtom = container.GetConnectedAtoms(atom).Count() + hcount;

            hcount = parent.ImplicitHydrogenCount ?? 0;

            int lengthParent = container.GetConnectedAtoms(parent).Count() + hcount;

            if (container.GetBond(atom, parent) != null)
            {
                if (container.GetBond(atom, parent).Order == BondOrder.Double &&
                    (lengthAtom == 3 || (lengthAtom == 2 && atom.AtomicNumber.Equals(AtomicNumbers.N))) &&
                    (lengthParent == 3 || (lengthParent == 2 && parent.AtomicNumber.Equals(AtomicNumbers.N))))
                {
                    var   atoms = container.GetConnectedAtoms(atom);
                    IAtom one   = null;
                    IAtom two   = null;
                    foreach (var conAtom in atoms)
                    {
                        if (conAtom != parent && one == null)
                        {
                            one = conAtom;
                        }
                        else if (conAtom != parent && one != null)
                        {
                            two = conAtom;
                        }
                    }
                    string[] morgannumbers = MorganNumbersTools.GetMorganNumbersWithElementSymbol(container);
                    if ((one != null && two == null &&
                         atom.AtomicNumber.Equals(AtomicNumbers.N) &&
                         Math.Abs(GiveAngleBothMethods(parent, atom, one, true)) > Math.PI / 10) ||
                        (!atom.AtomicNumber.Equals(AtomicNumbers.N) &&
                         one != null && two != null &&
                         !morgannumbers[container.Atoms.IndexOf(one)].Equals(morgannumbers[container.Atoms.IndexOf(two)], StringComparison.Ordinal)))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Says if an atom is the start of a double bond configuration
        /// </summary>
        /// <param name="a">The atom which is the start of configuration</param>
        /// <param name="container">The atomContainer the atom is in</param>
        /// <param name="parent">The atom we came from</param>
        /// <param name="doubleBondConfiguration">The array indicating where double bond
        ///     configurations are specified (this method ensures that there is
        ///     actually the possibility of a double bond configuration)</param>
        /// <returns>false=is not start of configuration, true=is</returns>
        private static bool IsStartOfDoubleBond(IAtomContainer container, IAtom a, IAtom parent, bool[] doubleBondConfiguration)
        {
            int hcount;

            hcount = a.ImplicitHydrogenCount ?? 0;

            int lengthAtom = container.GetConnectedAtoms(a).Count() + hcount;

            if (lengthAtom != 3 && (lengthAtom != 2 && !(a.AtomicNumber.Equals(AtomicNumbers.N))))
            {
                return(false);
            }
            var   atoms      = container.GetConnectedAtoms(a);
            IAtom one        = null;
            IAtom two        = null;
            bool  doubleBond = false;
            IAtom nextAtom   = null;

            foreach (var atom in atoms)
            {
                if (atom != parent && container.GetBond(atom, a).Order == BondOrder.Double &&
                    IsEndOfDoubleBond(container, atom, a, doubleBondConfiguration))
                {
                    doubleBond = true;
                    nextAtom   = atom;
                }
                if (atom != nextAtom && one == null)
                {
                    one = atom;
                }
                else if (atom != nextAtom && one != null)
                {
                    two = atom;
                }
            }
            string[] morgannumbers = MorganNumbersTools.GetMorganNumbersWithElementSymbol(container);
            if (one != null &&
                ((!a.AtomicNumber.Equals(AtomicNumbers.N) &&
                  two != null &&
                  !morgannumbers[container.Atoms.IndexOf(one)].Equals(morgannumbers[container.Atoms.IndexOf(two)], StringComparison.Ordinal) &&
                  doubleBond &&
                  doubleBondConfiguration[container.Bonds.IndexOf(container.GetBond(a, nextAtom))]) ||
                 (doubleBond && a.AtomicNumber.Equals(AtomicNumbers.N) &&
                  Math.Abs(GiveAngleBothMethods(nextAtom, a, parent, true)) > Math.PI / 10)))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Prepares for a breadth first search within the <see cref="IAtomContainer"/>. The actual
        /// recursion is done in <see cref="NextSphere"/>.
        /// </summary>
        /// <param name="root">The atom at which we start the search</param>
        /// <param name="addTreeNode"></param>
        /// <exception cref="CDKException"> If something goes wrong.</exception>
        private void BreadthFirstSearch(IAtom root, bool addTreeNode)
        {
            sphere = 0;
            TreeNode tempNode = null;
            var      conAtoms = atomContainer.GetConnectedAtoms(root);
            IBond    bond     = null;

            sphereNodes.Clear();
            sphereNodesWithAtoms.Clear();
            foreach (var atom in conAtoms)
            {
                try
                {
                    if (atom.AtomicNumber.Equals(AtomicNumbers.H))
                    {
                        continue;
                    }
                    bond = atomContainer.GetBond(root, atom);

                    // In the first sphere the atoms are labeled with their own atom
                    // atom as source
                    if (bond.IsAromatic)
                    {
                        tempNode = new TreeNode(this, atom.Symbol, new TreeNode(this, root.Symbol, null, root, (double)0, 0,
                                                                                (long)0), atom, 4, atomContainer.GetConnectedBonds(atom).Count(), 0);
                    }
                    else
                    {
                        tempNode = new TreeNode(
                            this, atom.Symbol,
                            new TreeNode(this, root.Symbol, null, root, (double)0, 0, (long)0), atom,
                            bond.Order.Numeric(), atomContainer.GetConnectedBonds(atom).Count(), 0);
                    }

                    sphereNodes.Add(tempNode);
                    if (!addTreeNode)
                    {
                        sphereNodesWithAtoms.Add(atom);
                    }

                    //                rootNode.childs.AddElement(tempNode);
                    atom.IsVisited = true;
                }
                catch (Exception exc)
                {
                    throw new CDKException("Error in HOSECodeGenerator->breadthFirstSearch.", exc);
                }
            }
            sphereNodes.Sort(new TreeNodeComparator());
            NextSphere(sphereNodes);
        }
Exemple #6
0
        public void Z_1_2_difluroethene()
        {
            IAtomContainer ac = Convert("F/C=C\\F");

            var se = ac.StereoElements.First();

            Assert.IsInstanceOfType(se, typeof(IDoubleBondStereochemistry));

            IDoubleBondStereochemistry dbs = (IDoubleBondStereochemistry)se;

            Assert.AreEqual(ac.GetBond(ac.Atoms[1], ac.Atoms[2]), dbs.StereoBond);
            Assert.IsTrue(Compares.AreDeepEqual(new IBond[] { ac.GetBond(ac.Atoms[0], ac.Atoms[1]), ac.GetBond(ac.Atoms[2], ac.Atoms[3]) }, dbs.Bonds));
            Assert.AreEqual(DoubleBondConformation.Together, dbs.Stereo);
        }
Exemple #7
0
        /// <summary>
        /// Helper method to locate two terminal atoms in a container for this
        /// extended tetrahedral element. The atoms are ordered such that the first
        /// index is attached to the first two peripheral atoms and the second index
        /// is attached to the second two peripheral atoms.
        /// </summary>
        /// <param name="container">structure representation</param>
        /// <returns>the terminal atoms (ordered)</returns>
        public IAtom[] FindTerminalAtoms(IAtomContainer container)
        {
            var atoms    = FindTerminalAtoms(container, Focus);
            var carriers = Carriers;

            if (container.GetBond(atoms[0], carriers[2]) != null ||
                container.GetBond(atoms[0], carriers[3]) != null)
            {
                var tmp = atoms[0];
                atoms[0] = atoms[1];
                atoms[1] = tmp;
            }
            return(atoms);
        }
Exemple #8
0
        /// <summary>
        /// Says if of four atoms connected two one atom the up and down bonds are
        /// opposite or not, i. e.if it's tetrahedral or square planar. The method
        /// does not check if there are four atoms and if two or up and two are down
        /// </summary>
        /// <param name="atom">The atom which is the center</param>
        /// <param name="container">The atomContainer the atom is in</param>
        /// <returns>true=are opposite, false=are not</returns>
        public static bool StereosAreOpposite(IAtomContainer container, IAtom atom)
        {
            var atoms = container.GetConnectedAtoms(atom).ToReadOnlyList();
            var hm    = new SortedDictionary <double, int>();

            for (int i = 1; i < atoms.Count; i++)
            {
                hm.Add(GiveAngle(atom, atoms[0], atoms[i]), i);
            }
            var ohere          = hm.Values.ToArray();
            var stereoOne      = container.GetBond(atom, atoms[0]).Stereo;
            var stereoOpposite = container.GetBond(atom, atoms[ohere[1]]).Stereo;

            return(stereoOpposite == stereoOne);
        }
Exemple #9
0
        public void Z_1_2_difluroethene_explicit()
        {
            IAtomContainer ac = Convert("FC(\\[H])=C([H])/F");

            var se = ac.StereoElements.First();

            Assert.IsInstanceOfType(se, typeof(IDoubleBondStereochemistry));

            IDoubleBondStereochemistry dbs = (IDoubleBondStereochemistry)se;

            Assert.AreEqual(ac.GetBond(ac.Atoms[1], ac.Atoms[3]), dbs.StereoBond);
            Assert.IsTrue(Compares.AreDeepEqual(new IBond[] { ac.GetBond(ac.Atoms[1], ac.Atoms[2]), ac.GetBond(ac.Atoms[3], ac.Atoms[5]) }, dbs.Bonds));
            // the two 'F' are together but we use a H so they are 'opposite'
            Assert.AreEqual(DoubleBondConformation.Opposite, dbs.Stereo);
        }
Exemple #10
0
        /// <summary>
        /// Returns Ring set based on Hanser Ring Finding method
        /// <param name="molecule"></param>
        /// </summary>
        /// <returns>report collected the rings</returns>
        /// <seealso cref="IRingFinder.GetRingSet(IAtomContainer)"/>
        public IRingSet GetRingSet(IAtomContainer molecule)
        {
            var cycles = FindRings(molecule);

            IRingSet ringSet = molecule.Builder.NewRingSet();

            foreach (var ringAtoms in cycles)
            {
                IRing ring = molecule.Builder.NewRing();
                foreach (var atom in ringAtoms)
                {
                    atom.IsInRing = true;
                    ring.Atoms.Add(atom);
                    foreach (var atomNext in ringAtoms)
                    {
                        if (!atom.Equals(atomNext))
                        {
                            IBond bond = molecule.GetBond(atom, atomNext);
                            if (bond != null)
                            {
                                bond.IsInRing = true;
                                ring.Bonds.Add(bond);
                            }
                        }
                    }
                }
                ringSet.Add(ring);
            }
            return(ringSet);
        }
Exemple #11
0
        /// <summary>
        /// Encode the provided path of atoms to a string.
        /// </summary>
        /// <param name="path">inclusive array of vertex indices</param>
        /// <returns>encoded path</returns>
        private string Encode(int[] path)
        {
            var sb = new StringBuilder(path.Length * 3);

            for (int i = 0, n = path.Length - 1; i <= n; i++)
            {
                var atom = container.Atoms[path[i]];

                sb.Append(ToAtomPattern(atom));

                if (atom is IPseudoAtom)
                {
                    pseudoAtoms.Add(atom.Symbol);
                    // potential bug, although the atoms are canonical we cannot guarantee the order we will visit them.
                    // sb.Append(PeriodicTable.GetElementCount() + pseudoAtoms.Count());
                }

                // if we are not at the last index, add the connecting bond
                if (i < n)
                {
                    IBond bond = container.GetBond(container.Atoms[path[i]], container.Atoms[path[i + 1]]);
                    sb.Append(GetBondSymbol(bond));
                }
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Constructor for the TopologicalEquivalentClass object.
        /// </summary>
        public EquivalentClassPartitioner(IAtomContainer atomContainer)
        {
            adjaMatrix  = ConnectionMatrix.GetMatrix(atomContainer);
            apspMatrix  = PathTools.ComputeFloydAPSP(adjaMatrix);
            layerNumber = 1;
            nodeNumber  = atomContainer.Atoms.Count;

            for (int i = 1; i < atomContainer.Atoms.Count; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    // define the number of layer equal to the longest path obtained
                    // by calculating the all-pair-shortest path
                    if (apspMatrix[i][j] > layerNumber)
                    {
                        layerNumber = apspMatrix[i][j];
                    }
                    // correct adjacency matrix to consider aromatic bonds as such
                    if (adjaMatrix[i][j] > 0)
                    {
                        IBond bond   = atomContainer.GetBond(atomContainer.Atoms[i], atomContainer.Atoms[j]);
                        bool  isArom = bond.IsAromatic;
                        adjaMatrix[i][j] = (isArom) ? 1.5 : adjaMatrix[i][j];
                        adjaMatrix[j][i] = adjaMatrix[i][j];
                    }
                }
            }
            nodeMatrix = Arrays.CreateJagged <double>(nodeNumber, layerNumber + 1);
            bondMatrix = Arrays.CreateJagged <double>(nodeNumber, layerNumber);
            weight     = new double[nodeNumber + 1];
        }
        /// <inheritdoc/>
        public override bool IsConnected(int index1, int index2)
        {
            IAtom atom1 = atomContainer.Atoms[index1];
            IAtom atom2 = atomContainer.Atoms[index2];

            return(atomContainer.GetBond(atom1, atom2) != null);
        }
Exemple #14
0
        /// <summary>
        ///
        /// Returns bond map between source and target molecules based on the atoms
        /// <param name="ac1">source molecule</param>
        /// <param name="ac2">target molecule</param>
        /// <param name="mapping">mappings between source and target molecule atoms</param>
        /// <returns>bond map between source and target molecules based on the atoms</returns>
        /// </summary>
        public static IReadOnlyDictionary <IBond, IBond> MakeBondMapOfAtomMap(
            IAtomContainer ac1, IAtomContainer ac2,
            IReadOnlyDictionary <IAtom, IAtom> mapping)
        {
            var maps = new Dictionary <IBond, IBond>();

            foreach (var mapS in mapping)
            {
                IAtom indexI = mapS.Key;
                IAtom indexJ = mapS.Value;

                foreach (var mapD in mapping)
                {
                    IAtom indexIPlus = mapD.Key;
                    IAtom indexJPlus = mapD.Value;

                    if (!indexI.Equals(indexIPlus) && !indexJ.Equals(indexJPlus))
                    {
                        IBond ac1Bond = ac1.GetBond(indexI, indexIPlus);
                        if (ac1Bond != null)
                        {
                            IBond ac2Bond = ac2.GetBond(indexJ, indexJPlus);
                            if (ac2Bond != null)
                            {
                                maps[ac1Bond] = ac2Bond;
                            }
                        }
                    }
                }
            }

            return(maps);
        }
Exemple #15
0
 /// <summary>
 /// Obtain the bond between the atoms at index <paramref name="u"/> and 'v'. If the 'bondMap'
 /// is non-null it is used for direct lookup otherwise the slower linear
 /// lookup in 'container' is used.
 /// </summary>
 /// <param name="container">a structure</param>
 /// <param name="bondMap">optimised map of atom indices to bond instances</param>
 /// <param name="u">an atom index</param>
 /// <param name="v">an atom index (connected to u)</param>
 /// <returns>the bond between u and v</returns>
 private static IBond GetBond(IAtomContainer container, EdgeToBondMap bondMap, int u, int v)
 {
     if (bondMap != null)
     {
         return(bondMap[u, v]);
     }
     return(container.GetBond(container.Atoms[u], container.Atoms[v]));
 }
Exemple #16
0
 public static void MakeUpDownBonds(IAtomContainer container)
 {
     for (int i = 0; i < container.Atoms.Count; i++)
     {
         var a = container.Atoms[i];
         var connectedAtoms = container.GetConnectedAtoms(a).ToReadOnlyList();
         if (connectedAtoms.Count == 4)
         {
             int   up   = 0;
             int   down = 0;
             int   hs   = 0;
             IAtom h    = null;
             for (int k = 0; k < 4; k++)
             {
                 IAtom      conAtom = connectedAtoms[k];
                 BondStereo stereo  = container.GetBond(a, conAtom).Stereo;
                 if (stereo == BondStereo.Up)
                 {
                     up++;
                 }
                 else if (stereo == BondStereo.Down)
                 {
                     down++;
                 }
                 else if (stereo == BondStereo.None && conAtom.AtomicNumber.Equals(AtomicNumbers.H))
                 {
                     h = conAtom;
                     hs++;
                 }
                 else
                 {
                     h = null;
                 }
             }
             if (up == 0 && down == 1 && h != null && hs == 1)
             {
                 container.GetBond(a, h).Stereo = BondStereo.Up;
             }
             if (up == 1 && down == 0 && h != null && hs == 1)
             {
                 container.GetBond(a, h).Stereo = BondStereo.Down;
             }
         }
     }
 }
Exemple #17
0
        private static IReadOnlyDictionary <IBond, IBond> MakeBondMapsOfAtomMaps(IAtomContainer ac1, IAtomContainer ac2, IReadOnlyDictionary <int, int> mappings)
        {
            var maps = new Dictionary <IBond, IBond>();

            foreach (var atoms in ac1.Atoms)
            {
                int ac1AtomNumber = ac1.Atoms.IndexOf(atoms);

                if (mappings.ContainsKey(ac1AtomNumber))
                {
                    int ac2AtomNumber = mappings[ac1AtomNumber];

                    var connectedAtoms = ac1.GetConnectedAtoms(atoms);

                    foreach (var cAtoms in connectedAtoms)
                    {
                        int ac1ConnectedAtomNumber = ac1.Atoms.IndexOf(cAtoms);

                        if (mappings.ContainsKey(ac1ConnectedAtomNumber))
                        {
                            {
                                int ac2ConnectedAtomNumber = mappings[ac1ConnectedAtomNumber];

                                IBond ac1Bond = ac1.GetBond(atoms, cAtoms);
                                IBond ac2Bond = ac2.GetBond(ac2.Atoms[ac2AtomNumber], ac2.Atoms[ac2ConnectedAtomNumber]);

                                if (ac2Bond == null)
                                {
                                    ac2Bond = ac2.GetBond(ac2.Atoms[ac2ConnectedAtomNumber], ac2.Atoms[ac2AtomNumber]);
                                }

                                if (ac1Bond != null && ac2Bond != null)
                                {
                                    maps[ac1Bond] = ac2Bond;
                                }
                            }
                        }
                    }
                }
            }
            return(maps);
        }
Exemple #18
0
        internal static bool IsFurtherMappingPossible(IAtomContainer source, IAtomContainer target,
                                                      McgregorHelper mcGregorHelper, bool shouldMatchBonds)
        {
            int neighborBondNumA    = mcGregorHelper.NeighborBondNumA;
            int neighborBondNumB    = mcGregorHelper.NeighborBondNumB;
            var iBondNeighborAtomsA = mcGregorHelper.GetIBondNeighborAtomsA();
            var iBondNeighborAtomsB = mcGregorHelper.GetIBondNeighborAtomsB();
            var cBondNeighborsA     = mcGregorHelper.GetCBondNeighborsA();
            var cBondNeighborsB     = mcGregorHelper.GetCBondNeighborsB();

            for (int row = 0; row < neighborBondNumA; row++)
            {
                //            Console.Out.WriteLine("i " + row);
                string g1A = cBondNeighborsA[row * 4 + 0];
                string g2A = cBondNeighborsA[row * 4 + 1];

                for (int column = 0; column < neighborBondNumB; column++)
                {
                    string g1B = cBondNeighborsB[column * 4 + 0];
                    string g2B = cBondNeighborsB[column * 4 + 1];

                    if (IsAtomMatch(g1A, g2A, g1B, g2B))
                    {
                        try
                        {
                            int indexI      = iBondNeighborAtomsA[row * 3 + 0];
                            int indexIPlus1 = iBondNeighborAtomsA[row * 3 + 1];

                            int indexJ      = iBondNeighborAtomsB[column * 3 + 0];
                            int indexJPlus1 = iBondNeighborAtomsB[column * 3 + 1];

                            IAtom r1A          = source.Atoms[indexI];
                            IAtom r2A          = source.Atoms[indexIPlus1];
                            IBond reactantBond = source.GetBond(r1A, r2A);

                            IAtom p1B         = target.Atoms[indexJ];
                            IAtom p2B         = target.Atoms[indexJPlus1];
                            IBond productBond = target.GetBond(p1B, p2B);

                            if (IsMatchFeasible(source, reactantBond, target, productBond, shouldMatchBonds))
                            {
                                return(true);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Out.WriteLine(e.StackTrace);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #19
0
        private void SetModifedArcs(McgregorHelper mcGregorHelper)
        {
            int neighborBondNumA    = mcGregorHelper.NeighborBondNumA;
            int neighborBondNumB    = mcGregorHelper.NeighborBondNumB;
            var iBondNeighborAtomsA = mcGregorHelper.GetIBondNeighborAtomsA();
            var iBondNeighborAtomsB = mcGregorHelper.GetIBondNeighborAtomsB();
            var cBondNeighborsA     = mcGregorHelper.GetCBondNeighborsA();
            var cBondNeighborsB     = mcGregorHelper.GetCBondNeighborsB();

            for (int row = 0; row < neighborBondNumA; row++)
            {
                for (int column = 0; column < neighborBondNumB; column++)
                {
                    string g1A = cBondNeighborsA[row * 4 + 0];
                    string g2A = cBondNeighborsA[row * 4 + 1];
                    string g1B = cBondNeighborsB[column * 4 + 0];
                    string g2B = cBondNeighborsB[column * 4 + 1];

                    if (MatchGAtoms(g1A, g2A, g1B, g2B))
                    {
                        int indexI      = iBondNeighborAtomsA[row * 3 + 0];
                        int indexIPlus1 = iBondNeighborAtomsA[row * 3 + 1];

                        IAtom r1A          = source.Atoms[indexI];
                        IAtom r2A          = source.Atoms[indexIPlus1];
                        IBond reactantBond = source.GetBond(r1A, r2A);

                        int indexJ      = iBondNeighborAtomsB[column * 3 + 0];
                        int indexJPlus1 = iBondNeighborAtomsB[column * 3 + 1];

                        IAtom p1B         = target.Atoms[indexJ];
                        IAtom p2B         = target.Atoms[indexJPlus1];
                        IBond productBond = target.GetBond(p1B, p2B);
                        if (McGregorChecks.IsMatchFeasible(source, reactantBond, target, productBond, IsBondMatch))
                        {
                            modifiedARCS[row * neighborBondNumB + column] = 1;
                        }
                    }
                }
            }
        }
Exemple #20
0
        /// <summary>
        /// Says if two atoms are in cis or trans position around a double bond.
        /// The atoms have to be given to the method like this:  firstOuterAtom - firstInnerAtom = secondInnterAtom - secondOuterAtom
        /// </summary>
        /// <param name="firstOuterAtom">See above.</param>
        /// <param name="firstInnerAtom">See above.</param>
        /// <param name="secondInnerAtom">See above.</param>
        /// <param name="secondOuterAtom">See above.</param>
        /// <param name="ac">The atom container the atoms are in.</param>
        /// <returns>true=trans, false=cis.</returns>
        /// <exception cref="CDKException"> The atoms are not in a double bond configuration (no double bond in the middle, same atoms on one side)</exception>
        public static bool IsCisTrans(IAtom firstOuterAtom, IAtom firstInnerAtom, IAtom secondInnerAtom,
                                      IAtom secondOuterAtom, IAtomContainer ac)
        {
            if (!IsValidDoubleBondConfiguration(ac, ac.GetBond(firstInnerAtom, secondInnerAtom)))
            {
                throw new CDKException("There is no valid double bond configuration between your inner atoms!");
            }
            bool firstDirection  = IsLeft(firstOuterAtom, firstInnerAtom, secondInnerAtom);
            bool secondDirection = IsLeft(secondOuterAtom, secondInnerAtom, firstInnerAtom);

            return(firstDirection == secondDirection);
        }
        public void GetConnectivityTest()
        {
            string         acpString             = "C0C1C2C3 0:1(1),0:3(1),1:2(2),2:3(1)";
            IAtomContainer ac                    = AtomContainerPrinter.FromString(acpString, builder);
            AtomDiscretePartitionRefiner refiner = new AtomDiscretePartitionRefiner();

            refiner.Refine(ac);
            IBond bond   = ac.GetBond(ac.Atoms[1], ac.Atoms[2]);
            int   orderN = bond.Order.Numeric();

            Assert.AreEqual(orderN, refiner.GetConnectivity(1, 2));
        }
Exemple #22
0
        public void Benzene_kekule()
        {
            IAtomContainer ac = Convert("C=1C=CC=CC1");

            Assert.AreEqual(6, ac.Atoms.Count);
            Assert.AreEqual(6, ac.Bonds.Count);
            foreach (var a in ac.Atoms)
            {
                Assert.AreEqual("C", a.Symbol);
                Assert.AreEqual(1, a.ImplicitHydrogenCount);
            }

            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[0], ac.Atoms[1]).Order);
            Assert.AreEqual(BondOrder.Double, ac.GetBond(ac.Atoms[1], ac.Atoms[2]).Order);
            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[2], ac.Atoms[3]).Order);
            Assert.AreEqual(BondOrder.Double, ac.GetBond(ac.Atoms[3], ac.Atoms[4]).Order);
            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[4], ac.Atoms[5]).Order);
            Assert.AreEqual(BondOrder.Double, ac.GetBond(ac.Atoms[5], ac.Atoms[0]).Order);

            Assert.IsFalse(ac.Bonds[0].IsAromatic);
            Assert.IsFalse(ac.Bonds[1].IsAromatic);
            Assert.IsFalse(ac.Bonds[2].IsAromatic);
            Assert.IsFalse(ac.Bonds[3].IsAromatic);
            Assert.IsFalse(ac.Bonds[4].IsAromatic);
            Assert.IsFalse(ac.Bonds[5].IsAromatic);
        }
Exemple #23
0
        public void Imidazole_kekule()
        {
            IAtomContainer ac = Convert("N1C=CN=C1");

            Assert.AreEqual(5, ac.Atoms.Count);
            Assert.AreEqual(5, ac.Bonds.Count);

            foreach (var a in ac.Atoms)
            {
                Assert.IsFalse(a.IsAromatic);
            }

            Assert.AreEqual("N", ac.Atoms[0].Symbol);
            Assert.AreEqual("C", ac.Atoms[1].Symbol);
            Assert.AreEqual("C", ac.Atoms[2].Symbol);
            Assert.AreEqual("N", ac.Atoms[3].Symbol);
            Assert.AreEqual("C", ac.Atoms[4].Symbol);

            Assert.AreEqual(1, ac.Atoms[0].ImplicitHydrogenCount);
            Assert.AreEqual(1, ac.Atoms[1].ImplicitHydrogenCount);
            Assert.AreEqual(1, ac.Atoms[2].ImplicitHydrogenCount);
            Assert.AreEqual(0, ac.Atoms[3].ImplicitHydrogenCount);
            Assert.AreEqual(1, ac.Atoms[4].ImplicitHydrogenCount);

            foreach (var a in ac.Atoms)
            {
                Assert.IsFalse(a.IsAromatic);
            }

            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[0], ac.Atoms[1]).Order);
            Assert.AreEqual(BondOrder.Double, ac.GetBond(ac.Atoms[1], ac.Atoms[2]).Order);
            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[2], ac.Atoms[3]).Order);
            Assert.AreEqual(BondOrder.Double, ac.GetBond(ac.Atoms[3], ac.Atoms[4]).Order);
            Assert.AreEqual(BondOrder.Single, ac.GetBond(ac.Atoms[4], ac.Atoms[0]).Order);

            foreach (var b in ac.Bonds)
            {
                Assert.IsFalse(b.IsAromatic);
            }
        }
Exemple #24
0
        /// <summary>
        /// Evaluates the empirical delt V for some S environments.
        /// </summary>
        /// <remarks>
        /// The method checks to see whether a S atom is in a -S-S-,
        /// -SO-, -SO2- group and returns the empirical values noted
        /// in Kier &amp; Hall (1986), page 20.
        /// </remarks>
        /// <param name="atom">The S atom in question</param>
        /// <param name="atomContainer">The molecule containing the S</param>
        /// <returns>The empirical delta V if it is present in one of the above environments, -1 otherwise</returns>
        protected internal static double DeltavSulphur(IAtom atom, IAtomContainer atomContainer)
        {
            if (!atom.AtomicNumber.Equals(AtomicNumbers.S))
            {
                return(-1);
            }

            // check whether it's a S in S-S
            var connected = atomContainer.GetConnectedAtoms(atom);

            foreach (var connectedAtom in connected)
            {
                if (connectedAtom.AtomicNumber.Equals(AtomicNumbers.S) &&
                    atomContainer.GetBond(atom, connectedAtom).Order == BondOrder.Single)
                {
                    return(0.89);
                }
            }

            int count = 0;

            foreach (var connectedAtom in connected)
            {
                if (connectedAtom.AtomicNumber.Equals(AtomicNumbers.O) &&
                    atomContainer.GetBond(atom, connectedAtom).Order == BondOrder.Double)
                {
                    count++;
                }
            }
            if (count == 1)
            {
                return(1.33); // check whether it's a S in -SO-
            }
            else if (count == 2)
            {
                return(2.67); // check whether it's a S in -SO2-
            }
            return(-1);
        }
Exemple #25
0
        public static void FixSulphurH(IAtomContainer m)
        {
            // removes extra H's attached to sulphurs
            for (int i = 0; i <= m.Atoms.Count - 1; i++)
            {
                var a = m.Atoms[i];

                if (a.AtomicNumber.Equals(AtomicNumbers.S))
                {
                    var connectedAtoms = m.GetConnectedAtoms(a);

                    int bondOrderSum = 0;

                    foreach (var conAtom in connectedAtoms)
                    {
                        if (!conAtom.AtomicNumber.Equals(AtomicNumbers.H))
                        {
                            IBond bond = m.GetBond(a, conAtom);
                            if (bond.Order == BondOrder.Single)
                            {
                                bondOrderSum += 1;
                            }
                            else if (bond.Order == BondOrder.Double)
                            {
                                bondOrderSum += 2;
                            }
                            else if (bond.Order == BondOrder.Triple)
                            {
                                bondOrderSum += 3;
                            }
                            else if (bond.Order == BondOrder.Quadruple)
                            {
                                bondOrderSum += 4;
                            }
                        }
                    }

                    if (bondOrderSum > 1)
                    {
                        foreach (var conAtom in connectedAtoms)
                        {
                            if (conAtom.AtomicNumber.Equals(AtomicNumbers.H))
                            {
                                m.RemoveAtom(conAtom);
                            }
                        }
                    }
                }
            }
        }
Exemple #26
0
 /// <summary>
 /// Checks whether both atoms are involved in an amide C-N bond: *N(*)C(*)=O.
 ///
 /// Only the most common constitution is considered. Tautomeric, O\C(*)=N\*,
 /// and charged forms, [O-]\C(*)=N\*, are ignored.
 /// </summary>
 /// <param name="atom0">the first bonding partner</param>
 /// <param name="atom1">the second bonding partner</param>
 /// <param name="container">the parent container</param>
 /// <returns>if both partners are involved in an amide C-N bond</returns>
 private static bool IsAmide(IAtom atom0, IAtom atom1, IAtomContainer container)
 {
     if (atom0.AtomicNumber.Equals(AtomicNumbers.C) && atom1.AtomicNumber.Equals(AtomicNumbers.N))
     {
         foreach (var neighbor in container.GetConnectedAtoms(atom0))
         {
             if (neighbor.AtomicNumber.Equals(AtomicNumbers.O) &&
                 container.GetBond(atom0, neighbor).Order == BondOrder.Double)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        private static void ApplyBonds(IAtomContainer m, IList <string> al)
        {
            for (int i = 0; i <= al.Count - 1; i++)
            {
                var s  = al[i];
                var s1 = s.Substring(0, s.IndexOf('-'));
                var s2 = s.Substring(s.IndexOf('-') + 1);

                var i1 = int.Parse(s1, NumberFormatInfo.InvariantInfo);
                var i2 = int.Parse(s2, NumberFormatInfo.InvariantInfo);

                var b = m.GetBond(m.Atoms[i1], m.Atoms[i2]);
                b.Order = BondOrder.Double;
            }
        }
        /// <summary>
        /// Generate Compatibility Graph Nodes Bond Insensitive
        /// </summary>
        /// <exception cref="System.IO.IOException"></exception>
        internal int CompatibilityGraph()
        {
            int compGraphNodesListSize = compGraphNodes.Count;

            cEdges = new List <int>(); //Initialize the cEdges List
            dEdges = new List <int>(); //Initialize the dEdges List

            for (int a = 0; a < compGraphNodesListSize; a += 3)
            {
                int indexA      = compGraphNodes[a];
                int indexAPlus1 = compGraphNodes[a + 1];

                for (int b = a + 3; b < compGraphNodesListSize; b += 3)
                {
                    int indexB      = compGraphNodes[b];
                    int indexBPlus1 = compGraphNodes[b + 1];

                    // if element atomCont !=jIndex and atoms on the adjacent sides of the bonds are not equal
                    if (a != b && indexA != indexB && indexAPlus1 != indexBPlus1)
                    {
                        IBond reactantBond = null;
                        IBond productBond  = null;

                        reactantBond = source.GetBond(source.Atoms[indexA], source.Atoms[indexB]);
                        productBond  = target.GetBond(target.Atoms[indexAPlus1], target.Atoms[indexBPlus1]);
                        if (reactantBond != null && productBond != null)
                        {
                            AddEdges(reactantBond, productBond, a, b);
                        }
                    }
                }
            }
            cEdgesSize = cEdges.Count;
            dEdgesSize = dEdges.Count;
            return(0);
        }
Exemple #29
0
 private void ProcessBondsBlock(int lineCount, IAtomContainer container)
 {
     for (int i = 0; i < lineCount; i++)
     {
         string line  = input.ReadLine();
         int    atom1 = int.Parse(line.Substring(10, 3).Trim(), NumberFormatInfo.InvariantInfo) - 1;
         int    atom2 = int.Parse(line.Substring(16, 3).Trim(), NumberFormatInfo.InvariantInfo) - 1;
         if (container.GetBond(container.Atoms[atom1], container.Atoms[atom2]) == null)
         {
             IBond bond = container.Builder.NewBond(container.Atoms[atom1],
                                                    container.Atoms[atom2]);
             int order = int.Parse(line.Substring(23).Trim(), NumberFormatInfo.InvariantInfo);
             bond.Order = BondManipulator.CreateBondOrder((double)order);
             container.Bonds.Add(bond);
         } // else: bond already present; CTX store the bonds twice
     }
 }
        /// <summary>
        /// Encode the provided path of atoms to a string.
        /// </summary>
        /// <param name="path">inclusive array of vertex indices</param>
        /// <returns>encoded path</returns>
        private string Encode(int[] path)
        {
            var sb = new StringBuilder(path.Length * 3);

            for (int i = 0, n = path.Length - 1; i <= n; i++)
            {
                var atom = container.Atoms[path[i]];
                sb.Append(ToAtomPattern(atom));

                // if we are not at the last index, add the connecting bond
                if (i < n)
                {
                    var bond = container.GetBond(container.Atoms[path[i]], container.Atoms[path[i + 1]]);
                    sb.Append(GetBondSymbol(bond));
                }
            }
            return(sb.ToString());
        }