Пример #1
0
        public void TestMap_Map_Map()
        {
            IChemObjectBuilder builder = ChemObjectBuilder.Instance;

            IAtom c1 = builder.NewAtom("C");
            IAtom o2 = builder.NewAtom("O");
            IAtom n3 = builder.NewAtom("N");
            IAtom c4 = builder.NewAtom("C");
            IAtom h5 = builder.NewAtom("H");

            // new stereo element
            ITetrahedralChirality original = new TetrahedralChirality(c1, new IAtom[] { o2, n3, c4, h5 }, TetrahedralStereo.Clockwise);

            // clone the atoms and place in a map
            var   mapping = new CDKObjectMap();
            IAtom c1clone = (IAtom)c1.Clone();

            mapping.Set(c1, c1clone);
            IAtom o2clone = (IAtom)o2.Clone();

            mapping.Set(o2, o2clone);
            IAtom n3clone = (IAtom)n3.Clone();

            mapping.Set(n3, n3clone);
            IAtom c4clone = (IAtom)c4.Clone();

            mapping.Set(c4, c4clone);
            IAtom h5clone = (IAtom)h5.Clone();

            mapping.Set(h5, h5clone);

            // map the existing element a new element
            ITetrahedralChirality mapped = (ITetrahedralChirality)original.Clone(mapping);

            Assert.AreNotSame(original.ChiralAtom, mapped.ChiralAtom, "mapped chiral atom was the same as the original");
            Assert.AreSame(c1clone, mapped.ChiralAtom, "mapped chiral atom was not the clone");

            var originalLigands = original.Ligands;
            var mappedLigands   = mapped.Ligands;

            Assert.AreNotSame(originalLigands[0], mappedLigands[0], "first ligand was the same as the original");
            Assert.AreSame(o2clone, mappedLigands[0], "first mapped ligand was not the clone");
            Assert.AreNotSame(originalLigands[1], mappedLigands[1], "second ligand was the same as the original");
            Assert.AreSame(n3clone, mappedLigands[1], "second mapped ligand was not the clone");
            Assert.AreNotSame(originalLigands[2], mappedLigands[2], "third ligand was the same as the original");
            Assert.AreSame(c4clone, mappedLigands[2], "third mapped ligand was not the clone");
            Assert.AreNotSame(originalLigands[3], mappedLigands[3], "forth ligand was te same as the original");
            Assert.AreSame(h5clone, mappedLigands[3], "forth mapped ligand was not the clone");

            Assert.AreEqual(original.Stereo, mapped.Stereo, "stereo was not mapped");
        }
Пример #2
0
        public static IAtomContainer SuppressQueryHydrogens(IAtomContainer mol)
        {
            // pre-checks
            foreach (var atom in mol.Atoms)
            {
                if (!(AtomRef.Deref(atom) is QueryAtom))
                {
                    throw new ArgumentException("Non-query atoms found!", nameof(mol));
                }
            }
            foreach (var bond in mol.Bonds)
            {
                if (!(BondRef.Deref(bond) is QueryBond))
                {
                    throw new ArgumentException("Non-query bonds found!", nameof(mol));
                }
            }

            var plainHydrogens = new CDKObjectMap();

            foreach (var atom in mol.Atoms)
            {
                int hcnt = 0;
                foreach (var nbor in mol.GetConnectedAtoms(atom))
                {
                    var qnbor = (QueryAtom)AtomRef.Deref(nbor);
                    if (mol.GetConnectedBonds(nbor).Count() == 1 &&
                        IsSimpleHydrogen(qnbor.Expression))
                    {
                        hcnt++;
                        plainHydrogens.Set(nbor, atom);
                    }
                }
                if (hcnt > 0)
                {
                    var qatom = (QueryAtom)AtomRef.Deref(atom);
                    var e     = qatom.Expression;
                    var hexpr = new Expr();
                    for (int i = 0; i < hcnt; i++)
                    {
                        hexpr.And(new Expr(ExprType.TotalHCount, i).Negate());
                    }
                    e.And(hexpr);
                }
            }

            // nothing to do
            if (!plainHydrogens.Any())
            {
                return(mol);
            }

            var res = new QueryAtomContainer();

            foreach (var atom in mol.Atoms)
            {
                if (!plainHydrogens.ContainsKey(atom))
                {
                    res.Atoms.Add(atom);
                }
            }
            foreach (var bond in mol.Bonds)
            {
                if (!plainHydrogens.ContainsKey(bond.Begin) &&
                    !plainHydrogens.ContainsKey(bond.End))
                {
                    res.Bonds.Add(bond);
                }
            }
            foreach (var se in mol.StereoElements)
            {
                res.StereoElements.Add((IStereoElement <IChemObject, IChemObject>)se.Clone(plainHydrogens));
            }

            return(res);
        }