public void TestSplit()
        {
            var          mol       = smilesParser.ParseSmiles("C1CC1C2CCC2");
            SpanningTree st        = new SpanningTree(mol);
            IRingSet     rings     = st.GetAllRings();
            IBond        splitBond = null;

            for (int i = 0; i < mol.Bonds.Count; i++)
            {
                if (rings.GetRings(mol.Bonds[i]).Count() == 0)
                {
                    splitBond = mol.Bonds[i];
                    break;
                }
            }
            var             frags       = FragmentUtils.SplitMolecule(mol, splitBond);
            SmilesGenerator sg          = new SmilesGenerator();
            var             uniqueFrags = new HashSet <string>();

            foreach (var frag in frags)
            {
                uniqueFrags.Add(sg.Create(frag));
            }
            Assert.AreEqual(2, uniqueFrags.Count);
            // You can put the fragments back together with a ring closure and dot
            // [CH]12CC1.[CH]12CCC1
            Assert.IsTrue(uniqueFrags.IsSupersetOf(new[] { "[CH]1CC1", "[CH]1CCC1" }));
        }
        private string GetRingcode(IAtom root, IAtomContainer ac)
        {
            if (ac != acold)
            {
                soar = Cycles.FindSSSR(ac).ToRingSet();
            }
            bool[] bool_ = new bool[1000];
            var    sb    = new StringBuilder();

            foreach (var soar_ring in soar.GetRings(root))
            {
                if (soar_ring.Atoms.Count < bool_.Length)
                {
                    bool_[soar_ring.Atoms.Count] = true;
                }
            }
            for (int i = 0; i < bool_.Length; i++)
            {
                if (bool_[i])
                {
                    sb.Append(i + "");
                }
            }
            if (sb.Length == 0)
            {
                return("");
            }
            else
            {
                return("-" + sb.ToString());
            }
        }
Exemple #3
0
        /// <summary>
        /// We define the heaviest ring as the one with the highest number of double bonds.
        /// Needed for example for the placement of in-ring double bonds.
        /// </summary>
        /// <param name="ringSet">The collection of rings</param>
        /// <param name="bond">A bond which must be contained by the heaviest ring</param>
        /// <returns>The ring with the highest number of double bonds connected to a given bond</returns>
        public static IRing GetHeaviestRing(IRingSet ringSet, IBond bond)
        {
            var   rings       = ringSet.GetRings(bond);
            IRing ring        = null;
            int   maxOrderSum = 0;

            foreach (var ring1 in rings)
            {
                if (maxOrderSum < ((IRing)ring1).GetBondOrderSum())
                {
                    ring        = (IRing)ring1;
                    maxOrderSum = ring.GetBondOrderSum();
                }
            }
            return(ring);
        }
        public virtual void TestGetRings_IBond()
        {
            IRingSet ringset = (IRingSet)NewChemObject();

            IAtom ring1Atom1  = ringset.Builder.NewAtom("C"); // rather artificial molecule
            IAtom ring1Atom2  = ringset.Builder.NewAtom("C");
            IAtom sharedAtom1 = ringset.Builder.NewAtom("C");
            IAtom sharedAtom2 = ringset.Builder.NewAtom("C");
            IAtom ring2Atom1  = ringset.Builder.NewAtom("C");
            IAtom ring2Atom2  = ringset.Builder.NewAtom("C");
            IBond ring1Bond1  = ringset.Builder.NewBond(ring1Atom1, ring1Atom2);
            IBond ring1Bond2  = ringset.Builder.NewBond(sharedAtom1, ring1Atom1);
            IBond ring1Bond3  = ringset.Builder.NewBond(sharedAtom2, ring1Atom2);
            IBond sharedBond  = ringset.Builder.NewBond(sharedAtom1, sharedAtom2);
            IBond ring2Bond1  = ringset.Builder.NewBond(ring2Atom1, ring2Atom2);
            IBond ring2Bond2  = ringset.Builder.NewBond(sharedAtom1, ring2Atom1);
            IBond ring2Bond3  = ringset.Builder.NewBond(sharedAtom2, ring2Atom2);

            IRing ring1 = ringset.Builder.NewRing();

            ring1.Atoms.Add(ring1Atom1);
            ring1.Atoms.Add(ring1Atom2);
            ring1.Atoms.Add(sharedAtom1);
            ring1.Atoms.Add(sharedAtom2);
            ring1.Bonds.Add(ring1Bond1);
            ring1.Bonds.Add(ring1Bond2);
            ring1.Bonds.Add(ring1Bond3);
            ring1.Bonds.Add(sharedBond);
            IRing ring2 = ringset.Builder.NewRing();

            ring2.Atoms.Add(ring2Atom1);
            ring2.Atoms.Add(ring2Atom2);
            ring2.Atoms.Add(sharedAtom1);
            ring2.Atoms.Add(sharedAtom2);
            ring2.Bonds.Add(ring2Bond1);
            ring2.Bonds.Add(ring2Bond2);
            ring2.Bonds.Add(ring2Bond3);
            ring2.Bonds.Add(sharedBond);

            ringset.Add(ring1);
            ringset.Add(ring2);

            Assert.AreEqual(1, ringset.GetRings(ring1Bond1).Count());
            Assert.AreEqual(1, ringset.GetRings(ring1Bond2).Count());
            Assert.AreEqual(1, ringset.GetRings(ring1Bond3).Count());
            Assert.AreEqual(2, ringset.GetRings(sharedBond).Count());
            Assert.AreEqual(1, ringset.GetRings(ring2Bond1).Count());
            Assert.AreEqual(1, ringset.GetRings(ring2Bond2).Count());
            Assert.AreEqual(1, ringset.GetRings(ring2Bond3).Count());
        }
Exemple #5
0
        /// <summary>
        /// Positions the aliphatic substituents of a ring system
        /// </summary>
        /// <param name="rs">The RingSystem for which the substituents are to be laid out</param>
        /// <returns>A list of atoms that where laid out</returns>
        public IAtomContainer PlaceRingSubstituents(IRingSet rs, double bondLength)
        {
            Debug.WriteLine("RingPlacer.PlaceRingSubstituents() start");
            IRing          ring             = null;
            IAtom          atom             = null;
            IAtomContainer unplacedPartners = rs.Builder.NewAtomContainer();
            IAtomContainer sharedAtoms      = rs.Builder.NewAtomContainer();
            IAtomContainer primaryAtoms     = rs.Builder.NewAtomContainer();
            IAtomContainer treatedAtoms     = rs.Builder.NewAtomContainer();

            for (int j = 0; j < rs.Count; j++)
            {
                ring = (IRing)rs[j]; // Get the j-th Ring in RingSet rs
                for (int k = 0; k < ring.Atoms.Count; k++)
                {
                    unplacedPartners.RemoveAllElements();
                    sharedAtoms.RemoveAllElements();
                    primaryAtoms.RemoveAllElements();
                    atom = ring.Atoms[k];
                    var rings = rs.GetRings(atom);
                    var centerOfRingGravity = GeometryUtil.Get2DCenter(rings);
                    AtomPlacer.PartitionPartners(atom, unplacedPartners, sharedAtoms);
                    AtomPlacer.MarkNotPlaced(unplacedPartners);
                    try
                    {
                        for (int f = 0; f < unplacedPartners.Atoms.Count; f++)
                        {
                            Debug.WriteLine("placeRingSubstituents->unplacedPartners: "
                                            + (Molecule.Atoms.IndexOf(unplacedPartners.Atoms[f]) + 1));
                        }
                    }
                    catch (Exception)
                    {
                    }

                    treatedAtoms.Add(unplacedPartners);
                    if (unplacedPartners.Atoms.Count > 0)
                    {
                        AtomPlacer.DistributePartners(atom, sharedAtoms, centerOfRingGravity, unplacedPartners, bondLength);
                    }
                }
            }
            Debug.WriteLine("RingPlacer.PlaceRingSubstituents() end");
            return(treatedAtoms);
        }
        /// <summary>
        ///  Method assigns atom types to atoms (calculates sssr and aromaticity)
        /// </summary>
        /// <returns>sssrf set</returns>
        /// <exception cref="CDKException"> Problems detecting aromaticity or making hose codes.</exception>
        public IRingSet AssignAtomTyps(IAtomContainer molecule)
        {
            IAtom             atom     = null;
            string            hoseCode = "";
            HOSECodeGenerator hcg      = new HOSECodeGenerator();
            int      NumberOfRingAtoms = 0;
            IRingSet ringSetMolecule   = Cycles.FindSSSR(molecule).ToRingSet();
            bool     isInHeteroRing    = false;

            try
            {
                AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule);
                Aromaticity.CDKLegacy.Apply(molecule);
            }
            catch (Exception cdk1)
            {
                throw new CDKException("AROMATICITYError: Cannot determine aromaticity due to: " + cdk1.Message, cdk1);
            }

            for (int i = 0; i < molecule.Atoms.Count; i++)
            {
                atom = molecule.Atoms[i];
                if (ringSetMolecule.Contains(atom))
                {
                    NumberOfRingAtoms = NumberOfRingAtoms + 1;
                    atom.IsInRing     = true;
                    atom.IsAliphatic  = false;
                    var ringSetA = ringSetMolecule.GetRings(atom).ToList();
                    RingSetManipulator.Sort(ringSetA);
                    IRing sring = (IRing)ringSetA[ringSetA.Count - 1];
                    atom.SetProperty("RING_SIZE", sring.RingSize);
                    foreach (var ring in ringSetA)
                    {
                        if (IsHeteroRingSystem(ring))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    atom.IsAliphatic = true;
                    atom.IsInRing    = false;
                    isInHeteroRing   = false;
                }
                atom.SetProperty("MAX_BOND_ORDER", molecule.GetMaximumBondOrder(atom).Numeric());

                try
                {
                    hoseCode = hcg.GetHOSECode(molecule, atom, 3);
                    //Debug.WriteLine("HOSECODE GENERATION: ATOM "+i+" HoseCode: "+hoseCode+" ");
                }
                catch (CDKException ex1)
                {
                    Console.Out.WriteLine("Could not build HOSECode from atom " + i + " due to " + ex1.ToString());
                    throw new CDKException("Could not build HOSECode from atom " + i + " due to " + ex1.ToString(), ex1);
                }
                try
                {
                    ConfigureAtom(atom, hoseCode, isInHeteroRing);
                }
                catch (CDKException ex2)
                {
                    Console.Out.WriteLine("Could not final configure atom " + i + " due to " + ex2.ToString());
                    throw new CDKException("Could not final configure atom due to problems with force field", ex2);
                }
            }

            //        IBond[] bond = molecule.Bonds;
            string bondType;

            foreach (var bond in molecule.Bonds)
            {
                //Debug.WriteLine("bond[" + i + "] properties : " + molecule.Bonds[i].GetProperties());
                bondType = "0";
                if (bond.Order == BondOrder.Single)
                {
                    if ((bond.Begin.AtomTypeName.Equals("Csp2", StringComparison.Ordinal)) &&
                        ((bond.End.AtomTypeName.Equals("Csp2", StringComparison.Ordinal)) ||
                         (bond.End.AtomTypeName.Equals("C=", StringComparison.Ordinal))))
                    {
                        bondType = "1";
                    }

                    if ((bond.Begin.AtomTypeName.Equals("C=", StringComparison.Ordinal)) &&
                        ((bond.End.AtomTypeName.Equals("Csp2", StringComparison.Ordinal)) ||
                         (bond.End.AtomTypeName.Equals("C=", StringComparison.Ordinal))))
                    {
                        bondType = "1";
                    }

                    if ((bond.Begin.AtomTypeName.Equals("Csp", StringComparison.Ordinal)) &&
                        (bond.End.AtomTypeName.Equals("Csp", StringComparison.Ordinal)))
                    {
                        bondType = "1";
                    }
                }
                //            molecule.Bonds[i].SetProperty("MMFF94 bond type", bondType);
                bond.SetProperty("MMFF94 bond type", bondType);
                //Debug.WriteLine("bond[" + i + "] properties : " + molecule.Bonds[i].GetProperties());
            }

            return(ringSetMolecule);
        }