Beispiel #1
0
        public void TestCyanoallene()
        {
            IAtomContainer mol = null;

            mol = CDK.SmilesParser.ParseSmiles("C=C=CC#N");
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            AddImplicitHydrogens(mol);
            CDK.LonePairElectronChecker.Saturate(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            var acSet = ConjugatedPiSystemsDetector.Detect(mol);

            Assert.AreEqual(1, acSet.Count);
            IAtomContainer ac1 = acSet[0];

            Assert.AreEqual(4, ac1.Atoms.Count);
            Assert.AreEqual(3, ac1.Bonds.Count);

            for (int i = 0; i < ac1.Atoms.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac1.Atoms[i]));
            }

            for (int i = 0; i < ac1.Bonds.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac1.Bonds[i]));
            }
        }
Beispiel #2
0
        public void TestDetectNaphtalene()
        {
            Trace.TraceInformation("Entering testDetectNaphtalene.");
            IAtomContainer mol      = null;
            var            filename = "NCDK.Data.CML.naphtalene.cml";

            mol = ReadCMLMolecule(filename);

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            var acSet = ConjugatedPiSystemsDetector.Detect(mol);

            Assert.AreEqual(1, acSet.Count);
            IAtomContainer ac = acSet[0];

            Assert.AreEqual(10, ac.Atoms.Count);
            Assert.AreEqual(11, ac.Bonds.Count);

            for (int i = 0; i < ac.Atoms.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac.Atoms[i]));
            }

            for (int i = 0; i < ac.Bonds.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac.Bonds[i]));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns an IAtomContainer which contains all the atoms and bonds which
        /// are involved in ring systems.
        /// </summary>
        /// <seealso cref="GetAllRings"/>
        /// <seealso cref="GetBasicRings"/>
        /// <returns>the IAtomContainer as described above</returns>
        public IAtomContainer GetCyclicFragmentsContainer()
        {
            IAtomContainer fragContainer = this.molecule.Builder.NewAtomContainer();
            IAtomContainer spt           = GetSpanningTree();

            for (int i = 0; i < totalEdgeCount; i++)
            {
                if (!bondsInTree[i])
                {
                    IRing ring = GetRing(spt, molecule.Bonds[i]);
                    for (int b = 0; b < ring.Bonds.Count; b++)
                    {
                        IBond ringBond = ring.Bonds[b];
                        if (!fragContainer.Contains(ringBond))
                        {
                            for (int atomCount = 0; atomCount < ringBond.Atoms.Count; atomCount++)
                            {
                                IAtom atom = ringBond.Atoms[atomCount];
                                if (!fragContainer.Contains(atom))
                                {
                                    atom.IsInRing = true;
                                    fragContainer.Atoms.Add(atom);
                                }
                            }
                            fragContainer.Bonds.Add(ringBond);
                        }
                    }
                }
            }
            return(fragContainer);
        }
Beispiel #4
0
        /// <summary>
        /// Adds a bond to this container.
        /// </summary>
        /// <param name="atom1">the first atom</param>
        /// <param name="atom2">the second atom</param>
        /// <param name="order">bond order</param>
        public static void AddBond(this IAtomContainer mol, IAtom atom1, IAtom atom2, BondOrder order)
        {
            if (!(mol.Contains(atom1) && mol.Contains(atom2)))
            {
                throw new InvalidOperationException();
            }
            IBond bond = mol.Builder.NewBond(atom1, atom2, order);

            mol.Bonds.Add(bond);
        }
Beispiel #5
0
        /// <summary>
        /// Adds a bond to this container.
        /// </summary>
        /// <param name="atom1">the first atom</param>
        /// <param name="atom2">the second atom</param>
        /// <param name="order">bond order</param>
        /// <param name="stereo">Stereochemical orientation</param>
        /// <returns>The bond added</returns>
        public static IBond AddBond(this IAtomContainer mol, IAtom atom1, IAtom atom2, BondOrder order, BondStereo stereo)
        {
            if (!(mol.Contains(atom1) && mol.Contains(atom2)))
            {
                throw new InvalidOperationException();
            }
            var bond = mol.Builder.NewBond(atom1, atom2, order, stereo);

            mol.Bonds.Add(bond);
            return(bond);
        }
Beispiel #6
0
        public static void RemoveElectronContainer(IReaction reaction, IElectronContainer electrons)
        {
            var reactants = reaction.Reactants;

            for (int i = 0; i < reactants.Count; i++)
            {
                IAtomContainer mol = reactants[i];
                if (mol.Contains(electrons))
                {
                    mol.Remove(electrons);
                }
            }
            var agents = reaction.Reactants;

            for (int i = 0; i < agents.Count; i++)
            {
                var mol = agents[i];
                if (mol.Contains(electrons))
                {
                    mol.Remove(electrons);
                }
            }
            var products = reaction.Products;

            for (int i = 0; i < products.Count; i++)
            {
                var mol = products[i];
                if (mol.Contains(electrons))
                {
                    mol.Remove(electrons);
                }
            }
        }
Beispiel #7
0
        public static void RemoveAtomAndConnectedElectronContainers(IReaction reaction, IAtom atom)
        {
            var reactants = reaction.Reactants;

            for (int i = 0; i < reactants.Count; i++)
            {
                IAtomContainer mol = reactants[i];
                if (mol.Contains(atom))
                {
                    mol.RemoveAtom(atom);
                }
            }
            var agents = reaction.Reactants;

            for (int i = 0; i < agents.Count; i++)
            {
                var mol = agents[i];
                if (mol.Contains(atom))
                {
                    mol.RemoveAtom(atom);
                }
            }
            var products = reaction.Products;

            for (int i = 0; i < products.Count; i++)
            {
                var mol = products[i];
                if (mol.Contains(atom))
                {
                    mol.RemoveAtom(atom);
                }
            }
        }
Beispiel #8
0
        public void TestPiSystemWithCarbokation()
        {
            Trace.TraceInformation("Entering testPiSystemWithCarbokation.");
            IAtomContainer mol      = null;
            var            filename = "NCDK.Data.MDL.piSystemWithCarbokation.mol";
            var            ins      = ResourceLoader.GetAsStream(filename);
            MDLReader      reader   = new MDLReader(ins);
            var            chemFile = reader.Read(builder.NewChemFile());

            mol = chemFile[0][0].MoleculeSet[0];

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            var acSet = ConjugatedPiSystemsDetector.Detect(mol);

            Assert.AreEqual(2, acSet.Count);
            IAtomContainer ac1 = acSet[0];

            Assert.AreEqual(4, ac1.Atoms.Count);
            Assert.AreEqual(3, ac1.Bonds.Count);

            for (int i = 0; i < ac1.Atoms.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac1.Atoms[i]));
            }

            for (int i = 0; i < ac1.Bonds.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac1.Bonds[i]));
            }

            IAtomContainer ac2 = acSet[0];

            Assert.AreEqual(4, ac2.Atoms.Count);
            Assert.AreEqual(3, ac2.Bonds.Count);

            for (int i = 0; i < ac2.Atoms.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac2.Atoms[i]));
            }

            for (int i = 0; i < ac2.Bonds.Count; i++)
            {
                Assert.IsTrue(mol.Contains(ac2.Bonds[i]));
            }
        }
        public void TestFusedRingFragments()
        {
            var ringSearch = new RingSearch(fusedRings);
            var fragments  = ringSearch.FusedRingFragments().ToReadOnlyList();

            Assert.AreEqual(1, fragments.Count);
            IAtomContainer fragment = fragments[0];

            foreach (var atom in fusedRings.Atoms)
            {
                Assert.IsTrue(fragment.Contains(atom));
            }
            foreach (var bond in fusedRings.Bonds)
            {
                Assert.IsTrue(fragment.Contains(bond));
            }
        }
Beispiel #10
0
        /// <summary>
        /// Adds a <see cref="ILonePair"/> to this <see cref="IAtom"/>.
        /// </summary>
        /// <param name="atom">The atom to which the <see cref="ILonePair"/> is added</param>
        public static void AddLonePairTo(this IAtomContainer mol, IAtom atom)
        {
            if (!mol.Contains(atom))
            {
                throw new InvalidOperationException();
            }
            var e = mol.Builder.NewLonePair(atom);

            mol.LonePairs.Add(e);
        }
Beispiel #11
0
        /// <summary>
        /// Adds a <see cref="ISingleElectron"/> to this <see cref="IAtom"/>.
        /// </summary>
        /// <param name="atom">The atom to which the <see cref="ISingleElectron"/> is added</param>
        public static void AddSingleElectronTo(this IAtomContainer mol, IAtom atom)
        {
            if (!mol.Contains(atom))
            {
                throw new InvalidOperationException();
            }
            var e = mol.Builder.NewSingleElectron(atom);

            mol.SingleElectrons.Add(e);
        }
Beispiel #12
0
 public void PseuDoAtomReplacement()
 {
     using (MDLV3000Reader reader = new MDLV3000Reader(GetType().Assembly.GetManifestResourceStream(GetType(), "pseudoAtomReplacement.mol")))
     {
         IAtomContainer container = reader.Read(builder.NewAtomContainer());
         foreach (var atom in container.Bonds[9].Atoms)
         {
             Assert.IsTrue(container.Contains(atom));
         }
     }
 }
Beispiel #13
0
        public void TestMakeAtomContainer()
        {
            var builder = CDK.Builder;

            IAtom atom    = builder.NewAtom("C");
            IAtom exclude = builder.NewAtom("C");

            IAtom a1 = builder.NewAtom("C");
            IAtom a2 = builder.NewAtom("C");

            IBond[] bonds = new IBond[] { builder.NewBond(atom, exclude),
                                          builder.NewBond(a1, a2), builder.NewBond(a1, atom),
                                          builder.NewBond(a2, exclude) };

            IAtomContainer part = FragmentUtils.MakeAtomContainer(atom, bonds, exclude);

            Assert.AreEqual(3, part.Atoms.Count);
            Assert.AreEqual(2, part.Bonds.Count);

            Assert.IsTrue(part.Contains(atom));
            Assert.IsTrue(part.Contains(a1));
            Assert.IsTrue(part.Contains(a2));
            Assert.IsFalse(part.Contains(exclude));

            Assert.IsTrue(part.Contains(bonds[1]));
            Assert.IsTrue(part.Contains(bonds[2]));
        }
Beispiel #14
0
        /// <summary>
        /// Set the Flags to atoms and bonds which are not contained in an atomContainer.
        /// </summary>
        /// <param name="container">Container with the flags</param>
        /// <param name="ac">Container to put the flags</param>
        /// <param name="number"><see langword="true"/>, if the the flag is true</param>
        /// <param name="b">Container with added flags</param>
        /// <returns></returns>
        private static IAtomContainer SetAntiFlags(IAtomContainer container, IAtomContainer ac, int number, bool b)
        {
            var bond = ac.Bonds[number];

            if (!container.Contains(bond))
            {
                bond.IsReactiveCenter          = b;
                bond.Atoms[0].IsReactiveCenter = b;
                bond.Atoms[1].IsReactiveCenter = b;
            }
            else
            {
                return(null);
            }
            return(ac);
        }
Beispiel #15
0
        /// Given a list of bonds representing a fragment obtained by splitting the molecule
        /// at a bond, we need to create an IAtomContainer from it, containing *one* of the atoms
        /// of the splitting bond. In addition, the new IAtomContainer should not contain the
        /// splitting bond itself
        internal static IAtomContainer MakeAtomContainer(IAtom atom, IEnumerable <IBond> parts, IAtom excludedAtom)
        {
            IAtomContainer partContainer = atom.Builder.NewAtomContainer();

            partContainer.Atoms.Add(atom);
            foreach (var aBond in parts)
            {
                foreach (var bondedAtom in aBond.Atoms)
                {
                    if (!bondedAtom.Equals(excludedAtom) && !partContainer.Contains(bondedAtom))
                    {
                        partContainer.Atoms.Add(bondedAtom);
                    }
                }
                if (!aBond.Contains(excludedAtom))
                {
                    partContainer.Bonds.Add(aBond);
                }
            }
            return(partContainer);
        }
Beispiel #16
0
        void Main()
        {
            IAtomContainer queryStructure  = null;
            IAtomContainer targetStructure = null;
            {
                #region 1
                IAtomContainer query  = queryStructure;
                IAtomContainer target = targetStructure;

                Mappings mappings = Pattern.FindSubstructure(query).MatchAll(target);
                #endregion

                #region enum_mappings
                foreach (int[] p in mappings)
                {
                    for (int i = 0; i < p.Length; i++)
                    {
                        // query.Atoms[i] is mapped to target.Atoms[p[i]];
                    }
                }
                #endregion

                #region stereochemistry
                foreach (int[] p in mappings.GetStereochemistry())
                {
                    // ...
                }
                #endregion

                #region unique_matches
                foreach (int[] p in mappings.GetUniqueAtoms())
                {
                    // ...
                }

                foreach (int[] p in mappings.GetUniqueBonds())
                {
                    // ...
                }
                #endregion

                {
                    #region toarray
                    int[][] ps = mappings.ToArray();
                    foreach (int[] p in ps)
                    {
                        // ...
                    }
                    #endregion
                }

                {
                    #region limit_matches
                    // first ten matches
                    foreach (int[] p in mappings.Limit(10))
                    {
                        // ...
                    }

                    // first 10 unique matches
                    foreach (int[] p in mappings.GetUniqueAtoms().Limit(10))
                    {
                        // ...
                    }

                    // ensure we don't waste memory and only 'fix' up to 100 unique matches
                    int[][] ps = mappings.GetUniqueAtoms().Limit(100).ToArray();
                    #endregion
                }

                #region all
                // first 100 unique matches
                Mappings m1 = mappings.GetUniqueAtoms().Limit(100);

                // unique matches in the first 100 matches
                Mappings m2 = mappings.Limit(100).GetUniqueAtoms();

                // first 10 unique matches in the first 100 matches
                Mappings m3 = mappings.Limit(100).GetUniqueAtoms().Limit(10);

                // number of unique atom matches
                int n1 = mappings.CountUnique();

                // number of unique atom matches with correct stereochemistry
                int n2 = mappings.GetStereochemistry().CountUnique();
                #endregion
            }

            {
                #region Filter
                IAtomContainer query  = queryStructure;
                IAtomContainer target = targetStructure;

                // obtain only the mappings where the first atom in the query is
                // mapped to the first atom in the target
                Mappings mappings = Pattern.FindSubstructure(query)
                                    .MatchAll(target)
                                    .Filter(input => input[0] == 0);
                #endregion
            }

            {
                #region GetMapping
                IAtomContainer query  = queryStructure;
                IAtomContainer target = targetStructure;

                Mappings mappings = Pattern.FindSubstructure(query).MatchAll(target);
                // a string that indicates the mapping of atom elements and numbers
                IEnumerable <string> strs = mappings.GetMapping(
                    input =>
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < input.Length; i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(query.Atoms[i])
                        .Append(i + 1)
                        .Append(" -> ")
                        .Append(target.Atoms[input[i]])
                        .Append(input[i] + 1);
                    }
                    return(sb.ToString());
                });
                #endregion
            }

            {
                #region ToArray1
                IAtomContainer query  = queryStructure;
                IAtomContainer target = targetStructure;

                Pattern pat = Pattern.FindSubstructure(query);

                // lazily iterator
                foreach (int[] mapping in pat.MatchAll(target))
                {
                    // logic...
                }

                int[][] mappings = pat.MatchAll(target).ToArray();

                // same as lazy iterator but we now can refer to and parse 'mappings'
                // to other methods without regenerating the graph match
                foreach (int[] mapping in mappings)
                {
                    // logic...
                }
                #endregion
            }

            {
                #region ToArray2
                IAtomContainer query  = queryStructure;
                IAtomContainer target = targetStructure;

                Pattern pat = Pattern.FindSubstructure(query);

                // array of the first 5 unique atom mappings
                int[][] mappings = pat.MatchAll(target)
                                   .GetUniqueAtoms()
                                   .Limit(5)
                                   .ToArray();
                #endregion
            }

            {
                Mappings mappings = null;
                #region ToAtomMap
                foreach (IDictionary <IAtom, IAtom> map in mappings.ToAtomMap())
                {
                    foreach (KeyValuePair <IAtom, IAtom> e in map)
                    {
                        IAtom queryAtom  = e.Key;
                        IAtom targetAtom = e.Value;
                    }
                }
                #endregion
            }

            {
                Mappings mappings = null;
                #region ToBondMap
                foreach (IDictionary <IBond, IBond> map in mappings.ToBondMap())
                {
                    foreach (KeyValuePair <IBond, IBond> e in map)
                    {
                        IBond queryBond  = e.Key;
                        IBond targetBond = e.Value;
                    }
                }
                #endregion
            }

            {
                IAtomContainer query    = queryStructure;
                Mappings       mappings = null;
                int            i        = 0;
                #region ToAtomBondMap
                foreach (var map in mappings.ToAtomBondMap())
                {
                    foreach (var e in map)
                    {
                        IChemObject queryObj  = e.Key;
                        IChemObject targetObj = e.Value;
                    }
                    IAtom matchedAtom = (IAtom)map[query.Atoms[i]];
                    IBond matchedBond = (IBond)map[query.Bonds[i]];
                }
                #endregion
            }

            {
                Mappings mappings = null;
                #region ToChemObjects
                foreach (var obj in mappings.ToChemObjects())
                {
                    if (obj is IAtom)
                    {
                        // this atom was 'hit' by the pattern
                    }
                }
                #endregion
            }

            {
                Mappings someMappings = null;
                #region ToSubstructures
                IAtomContainer target   = targetStructure;
                Mappings       mappings = someMappings;
                foreach (var mol in mappings.ToSubstructures())
                {
                    foreach (var atom in mol.Atoms)
                    {
                        target.Contains(atom); // always true
                    }
                    foreach (var atom in target.Atoms)
                    {
                        mol.Contains(atom); // not always true
                    }
                }
                #endregion
            }

            {
                Mappings someMappings = null;
                #region AtLeast
                Mappings mappings = someMappings;

                if (mappings.AtLeast(5))
                {
                    // set bit flag etc.
                }

                // are the at least 5 unique matches?
                if (mappings.GetUniqueAtoms().AtLeast(5))
                {
                    // set bit etc.
                }
                #endregion
            }
        }
Beispiel #17
0
        private void PlaceBridgedRing(IRing ring, IAtomContainer sharedAtoms, Vector2 sharedAtomsCenter, Vector2 ringCenterVector, double bondLength)
        {
            IAtom[]      bridgeAtoms = GetBridgeAtoms(sharedAtoms);
            IAtom        bondAtom1   = bridgeAtoms[0];
            IAtom        bondAtom2   = bridgeAtoms[1];
            List <IAtom> otherAtoms  = new List <IAtom>();

            foreach (IAtom atom in sharedAtoms.Atoms)
            {
                if (atom != bondAtom1 && atom != bondAtom2)
                {
                    otherAtoms.Add(atom);
                }
            }

            bool snap = ring.GetProperty <bool>(SnapHint, false);

            bool swap = snap ? Det(bondAtom1.Point2D.Value, GeometryUtil.Get2DCenter(otherAtoms), bondAtom2.Point2D.Value) < 0
                                : Det(bondAtom1.Point2D.Value, GeometryUtil.Get2DCenter(otherAtoms), bondAtom2.Point2D.Value) > 0;

            if (swap)
            {
                IAtom tmp = bondAtom1;
                bondAtom1 = bondAtom2;
                bondAtom2 = tmp;
            }

            Vector2 bondAtom1Vector = bondAtom1.Point2D.Value;
            Vector2 bondAtom2Vector = bondAtom2.Point2D.Value;

            Vector2 midPoint = GetMidPoint(bondAtom1Vector, bondAtom2Vector);
            Vector2 ringCenter;
            double  radius = GetNativeRingRadius(ring, bondLength);
            double  offset = 0;

            if (snap)
            {
                ringCenter       = midPoint;
                ringCenterVector = GetPerpendicular(bondAtom1Vector, bondAtom2Vector,
                                                    new Vector2(midPoint.X - sharedAtomsCenter.X, midPoint.Y - sharedAtomsCenter.Y));

                offset = 0;
                foreach (IAtom atom in otherAtoms)
                {
                    double dist = Vector2.Distance(atom.Point2D.Value, midPoint);
                    if (dist > offset)
                    {
                        offset = dist;
                    }
                }
            }
            else
            {
                ringCenter = sharedAtomsCenter;
            }

            Vector2.Normalize(ringCenterVector);
            ringCenterVector = ringCenterVector * (radius - offset);
            ringCenter      += ringCenterVector;

            bondAtom1Vector -= ringCenter;
            bondAtom2Vector -= ringCenter;

            int numUnplaced = ring.RingSize - sharedAtoms.Atoms.Count;

            double dot = bondAtom2Vector.X * bondAtom1Vector.X + bondAtom2Vector.Y * bondAtom1Vector.Y;
            double det = bondAtom2Vector.X * bondAtom1Vector.Y - bondAtom2Vector.Y * bondAtom1Vector.X;

            // theta remain/step
            double tRemain = Math.Atan2(det, dot);

            if (tRemain < 0)
            {
                tRemain = Math.PI + (Math.PI + tRemain);
            }
            double tStep = tRemain / (numUnplaced + 1);

            Debug.WriteLine($"placeBridgedRing->tRemain: {Vectors.RadianToDegree(tRemain)}");
            Debug.WriteLine($"placeBridgedRing->tStep: {Vectors.RadianToDegree(tStep)}");

            double startAngle;

            startAngle = GeometryUtil.GetAngle(bondAtom1.Point2D.Value.X - ringCenter.X, bondAtom1.Point2D.Value.Y - ringCenter.Y);

            IAtom currentAtom = bondAtom1;
            IBond currentBond = sharedAtoms.GetConnectedBonds(currentAtom).First();

            List <IAtom> atoms = new List <IAtom>();

            for (int i = 0; i < ring.Bonds.Count; i++)
            {
                currentBond = ring.GetNextBond(currentBond, currentAtom);
                currentAtom = currentBond.GetOther(currentAtom);
                if (!sharedAtoms.Contains(currentAtom))
                {
                    atoms.Add(currentAtom);
                }
            }

#if DEBUG
            Debug.WriteLine($"{nameof(PlaceBridgedRing)}->atomsToPlace: {AtomPlacer.ListNumbers(Molecule, atoms)}");
            Debug.WriteLine($"{nameof(PlaceBridgedRing)}->startAngle: {Vectors.RadianToDegree(startAngle)}");
            Debug.WriteLine($"{nameof(PlaceBridgedRing)}->tStep: {Vectors.RadianToDegree(tStep)}");
#endif
            AtomPlacer.PopulatePolygonCorners(atoms, ringCenter, startAngle, -tStep, radius);
        }
Beispiel #18
0
        /// <summary>
        /// Gets if neighbours of an atom are in an atom container.
        /// </summary>
        /// <param name="neighs">atoms</param>
        /// <param name="ac">container</param>
        /// <returns>The boolean result</returns>
        private static bool IsANeighboorsInAnAtomContainer(IEnumerable <IAtom> neighs, IAtomContainer ac)
        {
            int count = neighs.Where(neigh => ac.Contains(neigh)).Count();

            return(count > 0);
        }