Example #1
0
        public override void TestBug934819()
        {
            IAtomContainer subStructure   = Bug934819_1();
            IAtomContainer superStructure = Bug934819_2();

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(superStructure);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(subStructure);
            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            IFingerprinter  fpr       = new PubchemFingerprinter();
            IBitFingerprint superBits = fpr.GetBitFingerprint(superStructure);
            IBitFingerprint subBits   = fpr.GetBitFingerprint(subStructure);

            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(9, 10, 14, 18, 19, 33, 143, 146, 255, 256, 283, 284, 285, 293, 301, 332, 344, 349, 351,
                                       353, 355, 368, 370, 371, 376, 383, 384, 395, 401, 412, 416, 421, 423, 434, 441, 446, 449, 454,
                                       455, 464, 470, 471, 480, 489, 490, 500, 502, 507, 513, 514, 516, 520, 524, 531, 532, 545, 546,
                                       549, 552, 556, 558, 564, 570, 586, 592, 599, 600, 607, 633, 658, 665),
                              subBits.AsBitSet()));
            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(9, 10, 11, 14, 18, 19, 33, 34, 143, 146, 150, 153, 255, 256, 257, 258, 283, 284, 285, 293,
                                       301, 332, 344, 349, 351, 353, 355, 368, 370, 371, 374, 376, 383, 384, 395, 401, 412, 416, 417,
                                       421, 423, 427, 434, 441, 446, 449, 454, 455, 460, 464, 470, 471, 479, 480, 489, 490, 500, 502,
                                       507, 513, 514, 516, 520, 524, 531, 532, 545, 546, 549, 552, 556, 558, 564, 570, 578, 582, 584,
                                       586, 592, 595, 600, 603, 607, 608, 633, 634, 640, 658, 660, 664, 665, 668, 677, 678, 683),
                              superBits.AsBitSet()));
        }
Example #2
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (GetType() != obj.GetType())
            {
                return(false);
            }
            var other = (BitSetFingerprint)obj;

            if (bitset == null)
            {
                if (other.bitset != null)
                {
                    return(false);
                }
            }
            else if (!BitArrays.Equals(bitset, other.bitset))
            {
                return(false);
            }
            return(true);
        }
Example #3
0
        public override void TestBug706786()
        {
            IAtomContainer superStructure = Bug706786_1();
            IAtomContainer subStructure   = Bug706786_2();

            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            // SMARTS is now correct and D will include H atoms, CDK had this wrong
            // for years (had it has non-H count). Whilst you can set the optional
            // SMARTS flavor CDK_LEGACY this is not correct
            AtomContainerManipulator.SuppressHydrogens(superStructure);
            AtomContainerManipulator.SuppressHydrogens(subStructure);

            IFingerprinter  fpr       = GetBitFingerprinter();
            IBitFingerprint superBits = fpr.GetBitFingerprint(superStructure);
            IBitFingerprint subBits   = fpr.GetBitFingerprint(subStructure);

            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(0, 11, 13, 17, 40, 48, 136, 273, 274, 278, 286, 294, 299, 301, 304, 306),
                              superBits.AsBitSet()));
            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(1, 17, 273, 274, 278, 294, 306),
                              subBits.AsBitSet()));
        }
Example #4
0
        /// <summary>
        /// Checks whether all the positive bits in BitArray bs2 occur in BitArray bs1. If
        /// so, the molecular structure from which bs2 was generated is a possible
        /// substructure of bs1.
        /// </summary>
        /// <example>
        /// <include file='IncludeExamples.xml' path='Comments/Codes[@id="NCDK.Fingerprints.FingerprinterTool_Example.cs+IsSubset"]/*' />
        /// </example>
        /// <param name="bs1">The reference <see cref="BitArray"/></param>
        /// <param name="bs2">The <see cref="BitArray"/> which is compared with bs1</param>
        /// <returns>True, if bs2 is a subset of bs1</returns>
        // @cdk.keyword    substructure search
        public static bool IsSubset(BitArray bs1, BitArray bs2)
        {
            BitArray clone = (BitArray)bs1.Clone();

            BitArrays.And(clone, bs2);
            if (BitArrays.Equals(clone, bs2))
            {
                return(true);
            }
            return(false);
        }
        public virtual void TestBug934819()
        {
            // inlined molecules - note this test fails if implicit hydrogens are
            // included. generally PubCheMFingerprint can't be used for substructure filter
            IAtomContainer superStructure = Bug934819_2();
            IAtomContainer subStructure   = Bug934819_1();

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(superStructure);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(subStructure);
            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            IFingerprinter fingerprinter = GetBitFingerprinter();
            BitArray       superBS       = fingerprinter.GetBitFingerprint(superStructure).AsBitSet();
            BitArray       subBS         = fingerprinter.GetBitFingerprint(subStructure).AsBitSet();

            Assert.IsTrue(BitArrays.Equals(subBS, And(superBS, subBS)));
        }
Example #6
0
        public void TestCID25181289()
        {
            var mol = parser.ParseSmiles("C=C(C1=CC=C(C=C1)O)NNC2=C(C(=NC(=C2Cl)Cl)C(=O)O)Cl");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            var adder = CDK.HydrogenAdder;

            adder.AddImplicitHydrogens(mol);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            IFingerprinter printer = new PubchemFingerprinter();
            BitArray       fp      = printer.GetBitFingerprint(mol).AsBitSet();
            BitArray       ref_    = PubchemFingerprinter
                                     .Decode("AAADccBzMAAGAAAAAAAAAAAAAAAAAAAAAAA8QAAAAAAAAAABwAAAHgIYCAAADA6BniAwzpJqEgCoAyTyTASChCAnJiIYumGmTtgKJnLD1/PEdQhkwBHY3Qe82AAOIAAAAAAAAABAAAAAAAAAAAAAAAAAAA==");

            Assert.IsTrue(BitArrays.Equals(ref_, fp));
        }
Example #7
0
        public void TestGetBitFingerprint()
        {
            Assert.IsTrue(trivialMol != null);
            var circ   = new CircularFingerprinter();
            var result = circ.GetBitFingerprint(trivialMol);

            BitArray wantBits = new BitArray(0), gotBits = result.AsBitSet();

            int[] REQUIRE_BITS = { 19, 152, 293, 340, 439, 480, 507, 726, 762, 947, 993 };
            foreach (var b in REQUIRE_BITS)
            {
                BitArrays.SetValue(wantBits, b, true);
            }
            if (!BitArrays.Equals(wantBits, gotBits))
            {
                throw new CDKException("Got " + gotBits + ", wanted " + wantBits);
            }
        }
Example #8
0
        public void TestBenzene()
        {
            var mol = parser.ParseSmiles("c1ccccc1");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            var adder = CDK.HydrogenAdder;

            adder.AddImplicitHydrogens(mol);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol);

            Aromaticity.CDKLegacy.Apply(mol);
            IFingerprinter printer = new PubchemFingerprinter();
            BitArray       fp      = printer.GetBitFingerprint(mol).AsBitSet();
            BitArray       ref_    = PubchemFingerprinter
                                     .Decode("AAADcYBgAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAABAAAAGAAAAAAACACAEAAwAIAAAACAACBCAAACAAAgAAAIiAAAAIgIICKAERCAIAAggAAIiAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==");

            Assert.IsTrue(BitArrays.Equals(ref_, fp));
        }
Example #9
0
        public void TestCID5934166()
        {
            var mol = parser.ParseSmiles("C1=CC=C(C=C1)C[N+]2=C(C=C(C=C2C=CC3=CC=CC=C3)C4=CC=CC=C4)C5=CC=CC=C5");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            var adder = CDK.HydrogenAdder;

            adder.AddImplicitHydrogens(mol);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            IFingerprinter printer = new PubchemFingerprinter();
            BitArray       fp      = printer.GetBitFingerprint(mol).AsBitSet();
            BitArray       ref_    = PubchemFingerprinter
                                     .Decode("AAADceB+AAAAAAAAAAAAAAAAAAAAAAAAAAA8YMGCAAAAAAAB1AAAHAAAAAAADAjBHgQwgJMMEACgAyRiRACCgCAhAiAI2CA4ZJgIIOLAkZGEIAhggADIyAcQgMAOgAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==");

            Assert.IsTrue(BitArrays.Equals(ref_, fp));
        }
Example #10
0
        public void TestCID2518130()
        {
            var mol = parser.ParseSmiles("COC1C(C(C(C(O1)CO)OC2C(C(C(C(O2)CO)S)O)O)O)O");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol);
            var adder = CDK.HydrogenAdder;

            adder.AddImplicitHydrogens(mol);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol);
            Aromaticity.CDKLegacy.Apply(mol);

            IFingerprinter printer = new PubchemFingerprinter();
            BitArray       fp      = printer.GetBitFingerprint(mol).AsBitSet();
            BitArray       ref_    = PubchemFingerprinter
                                     .Decode("AAADceBwPABAAAAAAAAAAAAAAAAAAAAAAAAkSAAAAAAAAAAAAAAAGgQACAAACBS0wAOCCAAABgQAAAAAAAAAAAAAAAAAAAAAAAAREAIAAAAiQAAFAAAHAAHAYAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==");

            Assert.IsTrue(BitArrays.Equals(ref_, fp));
        }
Example #11
0
        /// <summary>
        ///  Determine if there are potential solution remaining.
        /// </summary>
        /// <param name="potentialNode">set of remaining potential nodes</param>
        /// <returns>true if it is worse to continue the search</returns>
        private bool MustContinue(BitArray potentialNode)
        {
            bool     result = true;
            bool     cancel = false;
            BitArray projG1 = ProjectG1(potentialNode);
            BitArray projG2 = ProjectG2(potentialNode);

            // if we reached the maximum number of
            // search iterations than do not continue
            if (maxIteration != -1 && nbIteration >= maxIteration)
            {
                return(false);
            }

            // if constrains may no more be fulfilled then stop.
            if (!IsContainedIn(c1, projG1) || !IsContainedIn(c2, projG2))
            {
                return(false);
            }

            // check if the solution potential is not included in an already
            // existing solution
            foreach (var sol in solutionList)
            {
                if (cancel)
                {
                    break;
                }

                // if we want every 'mappings' do not stop
                if (findAllMap && (BitArrays.Equals(projG1, ProjectG1(sol)) || BitArrays.Equals(projG2, ProjectG2(sol))))
                {
                    // do nothing
                }
                // if it is not possible to do better than an already existing solution than stop.
                else if (IsContainedIn(projG1, ProjectG1(sol)) || IsContainedIn(projG2, ProjectG2(sol)))
                {
                    result = false;
                    cancel = true;
                }
            }

            return(result);
        }
        public virtual void TestBug706786()
        {
            // inlined molecules - note this test fails if implicit hydrogens are
            // included. generally MACCS and ESTATE can't be used for substructure filter
            // check those subclasses which check the bits are set
            IAtomContainer superStructure = Bug706786_1();
            IAtomContainer subStructure   = Bug706786_2();

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(superStructure);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(subStructure);
            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            IFingerprinter fingerprinter = GetBitFingerprinter();
            BitArray       superBS       = fingerprinter.GetBitFingerprint(superStructure).AsBitSet();
            BitArray       subBS         = fingerprinter.GetBitFingerprint(subStructure).AsBitSet();

            Assert.IsTrue(BitArrays.Equals(subBS, And(superBS, subBS)));
        }
Example #13
0
        public void TestMultithReadedUsage()
        {
            var            mol1 = parser.ParseSmiles("C=C(C1=CC=C(C=C1)O)NNC2=C(C(=NC(=C2Cl)Cl)C(=O)O)Cl");
            IAtomContainer mol2 = parser
                                  .ParseSmiles("C1=CC=C(C=C1)C[N+]2=C(C=C(C=C2C=CC3=CC=CC=C3)C4=CC=CC=C4)C5=CC=CC=C5");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol1);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol2);

            var adder = CDK.HydrogenAdder;

            adder.AddImplicitHydrogens(mol1);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol1);
            Aromaticity.CDKLegacy.Apply(mol1);

            adder.AddImplicitHydrogens(mol2);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(mol2);
            Aromaticity.CDKLegacy.Apply(mol2);

            IFingerprinter fp  = new PubchemFingerprinter();
            BitArray       bs1 = fp.GetBitFingerprint(mol1).AsBitSet();
            BitArray       bs2 = fp.GetBitFingerprint(mol2).AsBitSet();

            // now lets run some threads
            var objs = new List <FpRunner>
            {
                new FpRunner(mol1),
                new FpRunner(mol2)
            };
            var ret = Parallel.ForEach(objs, o => o.Call());

            Assert.IsTrue(ret.IsCompleted);
            BitArray fb1 = objs[0].Result;

            Assert.IsNotNull(fb1);

            BitArray fb2 = objs[1].Result;

            Assert.IsNotNull(fb2);

            Assert.IsTrue(BitArrays.Equals(bs1, fb1));
            Assert.IsTrue(BitArrays.Equals(bs2, fb2));
        }
Example #14
0
        /// <summary>
        /// Test if set sourceBitSet is contained in set targetBitSet.
        /// </summary>
        /// <param name="sourceBitSet">a bitSet</param>
        /// <param name="targetBitSet">a bitSet</param>
        /// <returns>true if sourceBitSet is contained in targetBitSet</returns>
        private static bool IsContainedIn(BitArray sourceBitSet, BitArray targetBitSet)
        {
            bool result = false;

            if (BitArrays.IsEmpty(sourceBitSet))
            {
                return(true);
            }

            BitArray setA = (BitArray)sourceBitSet.Clone();

            setA.And(targetBitSet);

            if (BitArrays.Equals(setA, sourceBitSet))
            {
                result = true;
            }

            return(result);
        }
Example #15
0
        /// <summary>
        /// Test if set A is contained in set B.
        /// </summary>
        /// <param name="A">a bitSet</param>
        /// <param name="B">a bitSet</param>
        /// <returns>true if A is contained in B</returns>
        private static bool IsContainedIn(BitArray A, BitArray B)
        {
            bool result = false;

            if (BitArrays.IsEmpty(A))
            {
                return(true);
            }

            BitArray setA = (BitArray)A.Clone();

            setA.And(B);

            if (BitArrays.Equals(setA, A))
            {
                result = true;
            }

            return(result);
        }
Example #16
0
        public override void TestBug706786()
        {
            IAtomContainer superStructure = Bug706786_1();
            IAtomContainer subStructure   = Bug706786_2();

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(superStructure);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(subStructure);
            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            IFingerprinter  fpr       = new MACCSFingerprinter();
            IBitFingerprint superBits = fpr.GetBitFingerprint(superStructure);
            IBitFingerprint subBits   = fpr.GetBitFingerprint(subStructure);

            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(53, 56, 65, 71, 73, 88, 97, 104, 111, 112, 126, 130, 136, 138, 139, 140, 142, 143,
                                       144, 145, 148, 149, 151, 153, 156, 158, 159, 161, 162, 163, 164),
                              superBits.AsBitSet()));
            Assert.IsTrue(BitArrays.Equals(
                              AsBitSet(56, 97, 104, 108, 112, 117, 131, 136, 143, 144, 146, 151, 152, 156, 161, 162, 163, 164),
                              subBits.AsBitSet()));
        }
Example #17
0
        public override void TestBug706786()
        {
            IAtomContainer superStructure = Bug706786_1();
            IAtomContainer subStructure   = Bug706786_2();

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(superStructure);
            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(subStructure);
            AddImplicitHydrogens(superStructure);
            AddImplicitHydrogens(subStructure);

            // SMARTS is now correct and D will include H atoms, CDK had this wrong
            // for years (had it has non-H count). Whilst you can set the optional
            // SMARTS flavor CDK_LEGACY this is not correct
            AtomContainerManipulator.SuppressHydrogens(superStructure);
            AtomContainerManipulator.SuppressHydrogens(subStructure);

            IFingerprinter  fpr       = new EStateFingerprinter();
            IBitFingerprint superBits = fpr.GetBitFingerprint(superStructure);
            IBitFingerprint subBits   = fpr.GetBitFingerprint(subStructure);

            Assert.IsTrue(BitArrays.Equals(AsBitSet(6, 11, 12, 15, 16, 18, 33, 34, 35), superBits.AsBitSet()));
            Assert.IsTrue(BitArrays.Equals(AsBitSet(8, 11, 16, 35), subBits.AsBitSet()));
        }
Example #18
0
        /// <summary>
        /// Checks if a potential solution is a real one
        /// (not included in a previous solution)
        ///  and add this solution to the solution list
        /// in case of success.
        ///
        /// <param name="traversed">new potential solution</param>
        /// </summary>
        private void Solution(BitArray traversed)
        {
            bool     included = false;
            BitArray projG1   = ProjectG1(traversed);
            BitArray projG2   = ProjectG2(traversed);

            // the solution must follows the search constrains
            // (must contain the mandatory elements in G1 an G2)
            if (IsContainedIn(SourceBitSet, projG1) && IsContainedIn(TargetBitSet, projG2))
            {
                // the solution should not be included in a previous solution
                // at the CDKRGraph level. So we check against all previous solution
                // On the other hand if a previous solution is included in the
                // new one, the previous solution is removed.
                var solutionIndexesToRemove = new List <int>();
                for (int ii = 0; ii < Solutions.Count; ii++)
                {
                    if (included)
                    {
                        break;
                    }
                    var sol = Solutions[ii];

                    CheckTimeOut();
                    if (!BitArrays.Equals(sol, traversed))
                    {
                        // if we asked to save all 'mappings' then keep this mapping
                        if (IsFindAllMap && (BitArrays.Equals(projG1, ProjectG1(sol)) || BitArrays.Equals(projG2, ProjectG2(sol))))
                        {
                            // do nothing
                        } // if the new solution is included mark maxIterator as included
                        else if (IsContainedIn(projG1, ProjectG1(sol)) || IsContainedIn(projG2, ProjectG2(sol)))
                        {
                            included = true;
                        } // if the previous solution is contained in the new one, remove the previous solution
                        else if (IsContainedIn(ProjectG1(sol), projG1) || IsContainedIn(ProjectG2(sol), projG2))
                        {
                            solutionIndexesToRemove.Add(ii);
                        }
                    }
                    else
                    {
                        // solution already exists
                        included = true;
                    }
                }
                solutionIndexesToRemove.Reverse();
                foreach (var i in solutionIndexesToRemove)
                {
                    Solutions.RemoveAt(i);
                }

                if (included == false)
                {
                    // if maxIterator is really a new solution add maxIterator to the
                    // list of current solution
                    Solutions.Add(traversed);
                }

                if (!IsFindAllStructure)
                {
                    // if we need only one solution
                    // stop the search process
                    // (e.g. substructure search)
                    IsStop = true;
                }
            }
        }
Example #19
0
        /// <summary>
        /// Checks if a potential solution is a real one
        /// (not included in a previous solution)
        ///  and add this solution to the solution list
        /// in case of success.
        /// </summary>
        /// <param name="traversed">new potential solution</param>
        private void Solution(BitArray traversed)
        {
            bool     included = false;
            BitArray projG1   = ProjectG1(traversed);
            BitArray projG2   = ProjectG2(traversed);

            // the solution must follows the search constrains
            // (must contain the mandatory elements in G1 an G2)
            if (IsContainedIn(c1, projG1) && IsContainedIn(c2, projG2))
            {
                // the solution should not be included in a previous solution
                // at the RGraph level. So we check against all previous solution
                // On the other hand if a previous solution is included in the
                // new one, the previous solution is removed.
                var removeList = new List <int>();
                for (var pp = 0; pp < solutionList.Count; pp++)
                {
                    if (included)
                    {
                        break;
                    }
                    BitArray sol = solutionList[pp];

                    if (!BitArrays.Equals(sol, traversed))
                    {
                        // if we asked to save all 'mappings' then keep this mapping
                        if (findAllMap &&
                            (BitArrays.Equals(projG1, ProjectG1(sol)) || BitArrays.Equals(projG2, ProjectG2(sol))))
                        {
                            // do nothing
                        }
                        // if the new solution is included mark it as included
                        else if (IsContainedIn(projG1, ProjectG1(sol)) || IsContainedIn(projG2, ProjectG2(sol)))
                        {
                            included = true;
                        }
                        // if the previous solution is contained in the new one, remove the previous solution
                        else if (IsContainedIn(ProjectG1(sol), projG1) || IsContainedIn(ProjectG2(sol), projG2))
                        {
                            removeList.Add(pp);
                        }
                    }
                    else
                    {
                        // solution already exists
                        included = true;
                    }
                }
                removeList.Reverse();
                foreach (var i in removeList)
                {
                    solutionList.RemoveAt(i);
                }

                if (included == false)
                {
                    // if it is really a new solution add it to the
                    // list of current solution
                    solutionList.Add(traversed);
                }

                if (!findAllStructure)
                {
                    // if we need only one solution
                    // stop the search process
                    // (e.g. substructure search)
                    stop = true;
                }
            }
        }