Esempio n. 1
0
        public void TestNewPseudoAtom_String_Point3d()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom("Foo", new Vector3(1, 2, 3));

            Assert.IsNotNull(atom);
        }
Esempio n. 2
0
        public void TestNewPseudoAtom_String()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom("Foo");

            Assert.IsNotNull(atom);
        }
Esempio n. 3
0
        public void TestNewPseudoAtom_IAtom()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom(builder.NewAtom());

            Assert.IsNotNull(atom);
        }
Esempio n. 4
0
        public void TestNewPseudoAtom_IElement()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom(ChemicalElement.R);

            Assert.IsNotNull(atom);
        }
Esempio n. 5
0
        public void TestPseudoAtom()
        {
            var molecule = builder.NewAtomContainer();

            molecule.Atoms.Add(builder.NewPseudoAtom("Waterium"));
            FindAndConfigureAtomTypesForAllAtoms(molecule);
            Assert.IsNull(molecule.Atoms[0].ImplicitHydrogenCount);
        }
        public void TestDummy()
        {
            var   mol  = builder.NewAtomContainer();
            IAtom atom = builder.NewPseudoAtom("R");

            mol.Atoms.Add(atom);

            string[] expectedTypes = { "X" };
            AssertAtomTypeNames(testedAtomTypes, expectedTypes, mol);
        }
Esempio n. 7
0
        /// <summary>
        /// Converts a <see cref="IGraph"/> into an <see cref="IAtomContainer"/> using the given <see cref="IChemObjectBuilder"/>.
        /// </summary>
        /// <param name="builder"><see cref="IChemObjectBuilder"/> used to create new <see cref="IChemObject"/>s.</param>
        /// <returns>a <see cref="IAtomContainer"/> deserialized from the RDF graph.</returns>
        public IAtomContainer Model2Molecule(IChemObjectBuilder builder)
        {
            var            mols = g.GetTriplesWithPredicateObject(P_TYPE, R_MOLECULE);
            IAtomContainer mol  = null;

            foreach (var rdfMol in mols.Select(n => n.Subject))
            {
                mol = builder.NewAtomContainer();
                var rdfToCDKAtomMap = new Dictionary <INode, IAtom>();
                var atoms           = g.GetTriplesWithSubjectPredicate(rdfMol, P_HASATOM).Select(n => n.Object);
                foreach (var rdfAtom in atoms)
                {
                    IAtom atom = null;
                    if (g.GetTriplesWithSubjectPredicate(rdfAtom, P_TYPE).Where(n => n.Object.Equals(R_PSEUDOATOM)).Any())
                    {
                        atom = builder.NewPseudoAtom();
                        atom.StereoParity = 0;
                        var label = g.GetTriplesWithSubjectPredicate(rdfAtom, P_HASLABEL).Select(n => n.Object).FirstOrDefault();
                        if (label != null)
                        {
                            ((IPseudoAtom)atom).Label = label.ToString();
                        }
                    }
                    else
                    {
                        atom = builder.NewAtom();
                    }
                    var symbol = g.GetTriplesWithSubjectPredicate(rdfAtom, P_SYMBOL).Select(n => n.Object).FirstOrDefault();
                    if (symbol != null)
                    {
                        atom.Symbol = symbol.ToString();
                    }
                    rdfToCDKAtomMap[rdfAtom] = atom;
                    DeserializeAtomTypeFields(rdfAtom, atom);
                    mol.Atoms.Add(atom);
                }
                var bonds = g.GetTriplesWithSubjectPredicate(rdfMol, P_HASBOND).Select(n => n.Object);
                foreach (var rdfBond in bonds)
                {
                    var bondAtoms = g.GetTriplesWithSubjectPredicate(rdfBond, P_BINDSATOM).Select(n => n.Object);
                    var atomList  = new List <IAtom>();
                    foreach (var rdfAtom in bondAtoms)
                    {
                        IAtom atom = rdfToCDKAtomMap[rdfAtom];
                        atomList.Add(atom);
                    }
                    IBond bond  = builder.NewBond(atomList);
                    var   order = g.GetTriplesWithSubjectPredicate(rdfBond, P_HASORDER).Select(n => n.Object).FirstOrDefault();
                    bond.Order = Resource2Order(order);
                    mol.Bonds.Add(bond);
                    DeserializeElectronContainerFields(rdfBond, bond);
                }
            }
            return(mol);
        }
Esempio n. 8
0
        public void TestM23()
        {
            IAtomContainer expected1 = builder.NewAtomContainer();

            expected1.Atoms.Add(builder.NewPseudoAtom("R"));
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.AddBond(expected1.Atoms[0], expected1.Atoms[1], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.AddBond(expected1.Atoms[1], expected1.Atoms[2], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.Atoms[3].FormalCharge = 1;
            expected1.AddBond(expected1.Atoms[2], expected1.Atoms[3], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.AddBond(expected1.Atoms[3], expected1.Atoms[4], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.AddBond(expected1.Atoms[4], expected1.Atoms[5], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("C"));
            expected1.AddBond(expected1.Atoms[5], expected1.Atoms[6], BondOrder.Single);
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.Atoms.Add(builder.NewAtom("H"));
            expected1.AddBond(expected1.Atoms[1], expected1.Atoms[7], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[1], expected1.Atoms[8], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[2], expected1.Atoms[9], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[2], expected1.Atoms[10], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[3], expected1.Atoms[11], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[4], expected1.Atoms[12], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[4], expected1.Atoms[13], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[5], expected1.Atoms[14], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[5], expected1.Atoms[15], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[6], expected1.Atoms[16], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[6], expected1.Atoms[17], BondOrder.Single);
            expected1.AddBond(expected1.Atoms[6], expected1.Atoms[18], BondOrder.Single);

            string[] expectedTypes = { "X", "C.sp3", "C.sp3", "C.plus.planar", "C.sp3", "C.sp3", "C.sp3", "H", "H", "H",
                                       "H", "H",     "H",     "H",             "H",     "H",     "H",     "H", "H" };
            Assert.AreEqual(expectedTypes.Length, expected1.Atoms.Count);
            for (int i = 0; i < expectedTypes.Length; i++)
            {
                IAtom     nextAtom      = expected1.Atoms[i];
                IAtomType perceivedType = matcher.FindMatchingAtomType(expected1, nextAtom);
                Assert.IsNotNull(perceivedType, "Missing atom type for: " + nextAtom + " " + i + " expected: " + expectedTypes[i]);
                Assert.AreEqual(expectedTypes[i], perceivedType.AtomTypeName, "Incorrect atom type perceived for: " + nextAtom);
            }
        }
Esempio n. 9
0
        public void TestBug890456()
        {
            StringWriter writer   = new StringWriter();
            var          molecule = builder.NewAtomContainer();

            molecule.Atoms.Add(builder.NewPseudoAtom("*"));
            molecule.Atoms.Add(builder.NewAtom("C"));
            molecule.Atoms.Add(builder.NewAtom("C"));

            MDLV2000Writer mdlWriter = new MDLV2000Writer(writer);

            mdlWriter.Write(molecule);
            mdlWriter.Close();
            Assert.IsTrue(writer.ToString().IndexOf("M  END") != -1);
        }
Esempio n. 10
0
        /// <summary>
        /// Create a new CDK <see cref="IAtom"/> from the Beam Atom. If the element is
        /// unknown (i.e. '*') then an pseudo atom is created.
        /// </summary>
        /// <param name="atom">an Atom from the Beam Graph</param>
        /// <returns>the CDK atom to have it's properties set</returns>
        public IAtom NewCDKAtom(Beam.IAtom atom)
        {
            var element = atom.Element;
            var unknown = element == Beam.Element.Unknown;

            if (unknown)
            {
                var pseudoAtom = builder.NewPseudoAtom(element.Symbol);
                pseudoAtom.Symbol = element.Symbol;
                pseudoAtom.Label  = atom.Label;
                return(pseudoAtom);
            }
            return(CreateAtom(element));
        }
Esempio n. 11
0
        /// <summary>
        /// Transfers the CXSMILES state onto the CDK atom/molecule data-structures.
        /// </summary>
        /// <param name="bldr">chem-object builder</param>
        /// <param name="atoms">atoms parsed from the molecule or reaction. Reaction molecules are list left to right.</param>
        /// <param name="atomToMol">look-up of atoms to molecules when connectivity/sgroups need modification</param>
        /// <param name="cxstate">the CXSMILES state to read from</param>
        private void AssignCxSmilesInfo(IChemObjectBuilder bldr,
                                        IChemObject chemObj,
                                        List <IAtom> atoms,
                                        Dictionary <IAtom, IAtomContainer> atomToMol,
                                        CxSmilesState cxstate)
        {
            // atom-labels - must be done first as we replace atoms
            if (cxstate.atomLabels != null)
            {
                foreach (var e in cxstate.atomLabels)
                {
                    // bounds check
                    if (e.Key >= atoms.Count)
                    {
                        continue;
                    }

                    var old    = atoms[e.Key];
                    var pseudo = bldr.NewPseudoAtom();
                    var val    = e.Value;

                    // specialised label handling
                    if (val.EndsWith("_p", StringComparison.Ordinal)) // pseudo label
                    {
                        val = val.Substring(0, val.Length - 2);
                    }
                    else if (val.StartsWith("_AP", StringComparison.Ordinal)) // attachment point
                    {
                        pseudo.AttachPointNum = ParseIntSafe(val.Substring(3));
                    }

                    pseudo.Label                 = val;
                    pseudo.AtomicNumber          = 0;
                    pseudo.ImplicitHydrogenCount = 0;
                    var mol = atomToMol[old];
                    AtomContainerManipulator.ReplaceAtomByAtom(mol, old, pseudo);
                    atomToMol.Add(pseudo, mol);
                    atoms[e.Key] = pseudo;
                }
            }

            // atom-values - set as comment, mirrors Molfile reading behavior
            if (cxstate.atomValues != null)
            {
                foreach (var e in cxstate.atomValues)
                {
                    atoms[e.Key].SetProperty(CDKPropertyName.Comment, e.Value);
                }
            }

            // atom-coordinates
            if (cxstate.atomCoords != null)
            {
                var numAtoms  = atoms.Count;
                var numCoords = cxstate.atomCoords.Count;
                var lim       = Math.Min(numAtoms, numCoords);
                if (cxstate.coordFlag)
                {
                    for (int i = 0; i < lim; i++)
                    {
                        atoms[i].Point3D = new Vector3(
                            cxstate.atomCoords[i][0],
                            cxstate.atomCoords[i][1],
                            cxstate.atomCoords[i][2]);
                    }
                }
                else
                {
                    for (int i = 0; i < lim; i++)
                    {
                        atoms[i].Point2D = new Vector2(
                            cxstate.atomCoords[i][0],
                            cxstate.atomCoords[i][1]);
                    }
                }
            }

            // atom radicals
            if (cxstate.atomRads != null)
            {
                foreach (var e in cxstate.atomRads)
                {
                    // bounds check
                    if (e.Key >= atoms.Count)
                    {
                        continue;
                    }

                    int count = 0;
                    var aa    = e.Value;
                    switch (e.Value)
                    {
                    case CxSmilesState.Radical.Monovalent:
                        count = 1;
                        break;

                    // no distinction in CDK between singled/triplet
                    case CxSmilesState.Radical.Divalent:
                    case CxSmilesState.Radical.DivalentSinglet:
                    case CxSmilesState.Radical.DivalentTriplet:
                        count = 2;
                        break;

                    // no distinction in CDK between doublet/quartet
                    case CxSmilesState.Radical.Trivalent:
                    case CxSmilesState.Radical.TrivalentDoublet:
                    case CxSmilesState.Radical.TrivalentQuartet:
                        count = 3;
                        break;
                    }
                    var atom = atoms[e.Key];
                    var mol  = atomToMol[atom];
                    while (count-- > 0)
                    {
                        mol.SingleElectrons.Add(bldr.NewSingleElectron(atom));
                    }
                }
            }

            var sgroupMap = new MultiDictionary <IAtomContainer, Sgroup>();

            // positional-variation
            if (cxstate.positionVar != null)
            {
                foreach (var e in cxstate.positionVar)
                {
                    var sgroup = new Sgroup {
                        Type = SgroupType.ExtMulticenter
                    };
                    var beg   = atoms[e.Key];
                    var mol   = atomToMol[beg];
                    var bonds = mol.GetConnectedBonds(beg);
                    if (bonds.Count() == 0)
                    {
                        continue; // bad
                    }
                    sgroup.Add(beg);
                    sgroup.Add(bonds.First());
                    foreach (var endpt in e.Value)
                    {
                        sgroup.Add(atoms[endpt]);
                    }
                    sgroupMap.Add(mol, sgroup);
                }
            }

            // data sgroups
            if (cxstate.dataSgroups != null)
            {
                foreach (var dsgroup in cxstate.dataSgroups)
                {
                    if (dsgroup.Field != null && dsgroup.Field.StartsWith("cdk:", StringComparison.Ordinal))
                    {
                        chemObj.SetProperty(dsgroup.Field, dsgroup.Value);
                    }
                }
            }

            // polymer Sgroups
            if (cxstate.sgroups != null)
            {
                foreach (var psgroup in cxstate.sgroups)
                {
                    var            sgroup  = new Sgroup();
                    var            atomset = new HashSet <IAtom>();
                    IAtomContainer mol     = null;
                    foreach (var idx in psgroup.AtomSet)
                    {
                        if (idx >= atoms.Count)
                        {
                            continue;
                        }
                        var atom = atoms[idx];
                        var amol = atomToMol[atom];

                        if (mol == null)
                        {
                            mol = amol;
                        }
                        else if (amol != mol)
                        {
                            goto C_PolySgroup;
                        }

                        atomset.Add(atom);
                    }

                    if (mol == null)
                    {
                        continue;
                    }

                    foreach (var atom in atomset)
                    {
                        foreach (var bond in mol.GetConnectedBonds(atom))
                        {
                            if (!atomset.Contains(bond.GetOther(atom)))
                            {
                                sgroup.Add(bond);
                            }
                        }
                        sgroup.Add(atom);
                    }

                    sgroup.Subscript = psgroup.Subscript;
                    sgroup.PutValue(SgroupKey.CtabConnectivity, psgroup.Supscript);

                    switch (psgroup.Type)
                    {
                    case "n":
                        sgroup.Type = SgroupType.CtabStructureRepeatUnit;
                        break;

                    case "mon":
                        sgroup.Type = SgroupType.CtabMonomer;
                        break;

                    case "mer":
                        sgroup.Type = SgroupType.CtabMer;
                        break;

                    case "co":
                        sgroup.Type = SgroupType.CtabCopolymer;
                        break;

                    case "xl":
                        sgroup.Type = SgroupType.CtabCrossLink;
                        break;

                    case "mod":
                        sgroup.Type = SgroupType.CtabModified;
                        break;

                    case "mix":
                        sgroup.Type = SgroupType.CtabMixture;
                        break;

                    case "f":
                        sgroup.Type = SgroupType.CtabFormulation;
                        break;

                    case "any":
                        sgroup.Type = SgroupType.CtabAnyPolymer;
                        break;

                    case "gen":
                        sgroup.Type = SgroupType.CtabGeneric;
                        break;

                    case "c":
                        sgroup.Type = SgroupType.CtabComponent;
                        break;

                    case "grf":
                        sgroup.Type = SgroupType.CtabGraft;
                        break;

                    case "alt":
                        sgroup.Type = SgroupType.CtabCopolymer;
                        sgroup.PutValue(SgroupKey.CtabSubType, "ALT");
                        break;

                    case "ran":
                        sgroup.Type = SgroupType.CtabCopolymer;
                        sgroup.PutValue(SgroupKey.CtabSubType, "RAN");
                        break;

                    case "blk":
                        sgroup.Type = SgroupType.CtabCopolymer;
                        sgroup.PutValue(SgroupKey.CtabSubType, "BLO");
                        break;
                    }
                    sgroupMap.Add(mol, sgroup);
C_PolySgroup:
                    ;
                }
            }

            // assign Sgroups
            foreach (var e in sgroupMap)
            {
                e.Key.SetCtabSgroups(new List <Sgroup>(e.Value));
            }
        }