Пример #1
0
        /// <summary>
        /// gets the chemical element from the cmlElement CML
        /// </summary>
        /// <param name="cmlElement">XElement containing CML</param>
        /// <returns></returns>
        private ElementBase GetChemicalElement(XElement cmlElement, out string message)
        {
            message = "";
            XAttribute xa = cmlElement.Attribute("elementType");

            if (xa != null)
            {
                string symbol = xa.Value;

                //try to return a chemical element from the Periodic Table
                if (Globals.PeriodicTable.HasElement(symbol))
                {
                    return(Globals.PeriodicTable.Elements[symbol]);
                }

                //if that fails, see if it's a functional group
                // ReSharper disable once InconsistentNaming
                FunctionalGroup newFG;
                if (FunctionalGroups.TryParse(symbol, out newFG))
                {
                    return(newFG);
                }

                //if we got here then it went very wrong
                message = $"Unrecognised element '{symbol}' in {cmlElement}";
            }
            else
            {
                message = $"cml attribute 'elementType' missing from {cmlElement}";
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Initialization method for when EDI data string is known
        /// and the segment seperator if the interchange is known
        /// </summary>
        private void Parse(string value, FormattingOptions options)
        {
            var segmentStringValueCollection = value.Split(options.SegmentTerminator.AsSplitDelimiter(), StringSplitOptions.None);
            var functionalGroupStringArray   = new List <System.Text.StringBuilder>();

            foreach (var segment in segmentStringValueCollection)
            {
                var segmentStringInstance = segment;
                segmentStringInstance = RemoveCharacterFromString(segmentStringInstance, "\r", options);
                segmentStringInstance = RemoveCharacterFromString(segmentStringInstance, "\n", options);

                if (segmentStringInstance.Length < 3)
                {
                    continue;
                }

                switch (segmentStringInstance.Substring(0, 3))
                {
                case InterchangeControlHeader.ElementName:
                    Header = new InterchangeControlHeader(segmentStringInstance, options);
                    break;

                case InterchangeControlTrailer.ElementName:
                    Trailer = new InterchangeControlTrailer(segmentStringInstance, options);
                    break;

                default:
                    if (segmentStringInstance.Substring(0, 2).Equals(FunctionalGroupHeader.ElementName))
                    {
                        functionalGroupStringArray.Add(new System.Text.StringBuilder());
                    }

                    functionalGroupStringArray[functionalGroupStringArray.Count - 1].Append(segmentStringInstance);
                    functionalGroupStringArray[functionalGroupStringArray.Count - 1].Append(options.SegmentTerminator);
                    break;
                }
            }

            foreach (var functionalGroupString in functionalGroupStringArray)
            {
                var fg = new FunctionalGroup.FunctionalGroup(functionalGroupString.ToString(), FormattingOptions);
                FunctionalGroups.Add(fg);
            }
            FunctionalGroups.TrimExcess();
        }
Пример #3
0
        private XElement GetXElement(Atom atom)
        {
            var elementType = "";

            if (atom.Element is Element element)
            {
                elementType = element.Symbol;
            }

            if (atom.Element is FunctionalGroup functionalGroup)
            {
                elementType = FunctionalGroups.GetKey(functionalGroup);
            }

            XElement result = new XElement(CMLNamespaces.cml + CMLConstants.TagAtom,
                                           new XAttribute(CMLConstants.AttributeId, atom.Id),
                                           new XAttribute(CMLConstants.AttributeElementType, elementType),
                                           new XAttribute(CMLConstants.AttributeX2, atom.Position.X.ToString("0.####", CultureInfo.InvariantCulture)),
                                           new XAttribute(CMLConstants.AttributeY2, atom.Position.Y.ToString("0.####", CultureInfo.InvariantCulture))
                                           );

            if (atom.FormalCharge != null && atom.FormalCharge.Value != 0)
            {
                result.Add(new XAttribute(CMLConstants.AttributeFormalCharge, atom.FormalCharge.Value));
            }

            if (atom.IsotopeNumber != null && atom.IsotopeNumber.Value != 0)
            {
                result.Add(new XAttribute(CMLConstants.AttributeIsotopeNumber, atom.IsotopeNumber.Value));
            }

            if (atom.Element is Element element2)
            {
                if (element2 == Globals.PeriodicTable.C && atom.ShowSymbol != null)
                {
                    result.Add(new XAttribute(CMLNamespaces.c4w + CMLConstants.AttributeExplicit, atom.ShowSymbol));
                }
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// gets the chemical element from the cmlElement CML
        /// </summary>
        /// <param name="cmlElement">XElement containing CML</param>
        /// <returns></returns>
        private ElementBase GetChemicalElement(XElement cmlElement)
        {
            string symbol = cmlElement.Attribute("elementType").Value;

            //try to return a chemical element from the Periodic Table
            if (Globals.PeriodicTable.HasElement(symbol))
            {
                return(Globals.PeriodicTable.Elements[symbol]);
            }

            //if that fails, see if it's a functional group
            // ReSharper disable once InconsistentNaming
            FunctionalGroup newFG;

            if (FunctionalGroups.TryParse(symbol, out newFG))
            {
                return(newFG);
            }

            //if we got here then it went very wrong
            throw new ArgumentOutOfRangeException($"Unrecognised element '{symbol}'.");
        }
Пример #5
0
        private string FormatAtomSymbol(Atom atom)
        {
            var elementType = "";

            if (atom.Element is Element element)
            {
                elementType = element.Symbol;
            }

            // Bit of a bodge, but it ensures that it can be re-loaded without exploding
            if (atom.Element is FunctionalGroup functionalGroup)
            {
                elementType = FunctionalGroups.GetKey(functionalGroup);
                if (elementType.Length > 3)
                {
                    elementType = "*";
                }
            }

            // Add three spaces the trim back to three characters
            return($"{elementType}   ".Substring(0, 3));
        }
Пример #6
0
        private static MolJSON ExportMol(Molecule m1)
        {
            MolJSON mj = new MolJSON();

            mj.a = new AtomJSON[m1.Atoms.Count];
            Dictionary <Atom, int> indexLookup = new Dictionary <Atom, int>();

            int iAtom = 0;

            foreach (Atom a in m1.Atoms.Values)
            {
                string elem = null;
                if (a.Element.Symbol != "C")
                {
                    if (a.Element is Element element)
                    {
                        elem = element.Symbol;
                    }

                    if (a.Element is FunctionalGroup functionalGroup)
                    {
                        elem = FunctionalGroups.GetKey(functionalGroup);
                    }
                }
                mj.a[iAtom] = new AtomJSON()
                {
                    i = a.Id,
                    x = a.Position.X,
                    y = a.Position.Y,
                    l = elem
                };
                if (a.FormalCharge != null)
                {
                    mj.a[iAtom].c = a.FormalCharge.Value;
                }
                indexLookup[a] = iAtom;
                iAtom++;
            }

            int iBond = 0;

            if (m1.Bonds.Any())
            {
                mj.b = new BondJSON[m1.Bonds.Count];
                foreach (Bond bond in m1.Bonds)
                {
                    mj.b[iBond] = new BondJSON()
                    {
                        i = bond.Id,
                        b = indexLookup[bond.StartAtom],
                        e = indexLookup[bond.EndAtom]
                    };

                    if (bond.Stereo == Globals.BondStereo.Wedge)
                    {
                        mj.b[iBond].s = Protruding;
                    }
                    else if (bond.Stereo == Globals.BondStereo.Hatch)
                    {
                        mj.b[iBond].s = Recessed;
                    }
                    else if (bond.Stereo == Globals.BondStereo.Indeterminate)
                    {
                        mj.b[iBond].s = Ambiguous;
                    }
                    if (bond.Order != Globals.OrderSingle)
                    {
                        mj.b[iBond].o = bond.OrderValue;
                    }
                    iBond++;
                }
            }
            return(mj);
        }