Esempio n. 1
0
        public void WriteCustomTitle()
        {
            var builder = CDK.Builder;
            var mol     = builder.NewAtomContainer();
            var atom    = builder.NewAtom();

            atom.Symbol = "C";
            atom.ImplicitHydrogenCount = 4;
            atom.Point3D = new Vector3(0.5, 0.5, 0.1);
            mol.Atoms.Add(atom);
            using (var sw = new StringWriter())
            {
                using (var mdlw = new MDLV3000Writer(sw))
                {
                    var sdfWriterProps = new NameValueCollection
                    {
                        [MDLV2000Writer.OptProgramName] = "FakeNews"
                    };
                    mdlw.Listeners.Add(new PropertiesListener(sdfWriterProps));
                    mdlw.CustomizeJob();
                    mdlw.Write(mol);
                }
                Assert.IsTrue(sw.ToString().Contains("FakeNews", StringComparison.Ordinal));
            }
        }
Esempio n. 2
0
        private string WriteToStr(IAtomContainer mol)
        {
            var sw = new StringWriter();

            using (var mdlw = new MDLV3000Writer(sw))
            {
                mdlw.Write(mol);
            }
            return(sw.ToString());
        }
Esempio n. 3
0
        public void WriteDimensionField3D()
        {
            var builder = CDK.Builder;
            var mol     = builder.NewAtomContainer();
            var atom    = builder.NewAtom();

            atom.Symbol = "C";
            atom.ImplicitHydrogenCount = 4;
            atom.Point3D = new Vector3(0.5, 0.5, 0.1);
            mol.Atoms.Add(atom);
            var sw = new StringWriter();

            using (var mdlw = new MDLV3000Writer(sw))
            {
                mdlw.Write(mol);
            }
            Assert.IsTrue(sw.ToString().Contains("3D"));
        }
Esempio n. 4
0
        private void WriteMolecule(IAtomContainer container)
        {
            try
            {
                // write the MDL molfile bits
                StringWriter      stringWriter = new StringWriter();
                IChemObjectWriter mdlWriter;

                if (WriteV3000(container))
                {
                    mdlWriter = new MDLV3000Writer(stringWriter);
                }
                else
                {
                    mdlWriter = new MDLV2000Writer(stringWriter);
                }

                mdlWriter.AddSettings(IOSettings.Settings);
                mdlWriter.Write(container);
                mdlWriter.Close();
                writer.Write(stringWriter.ToString());

                // write non-structural data (mol properties in our case)
                if (paramWriteData.IsSet)
                {
                    var  sdFields           = container.GetProperties();
                    bool writeAllProperties = propertiesToWrite == null;
                    if (sdFields != null)
                    {
                        foreach (var propKey in sdFields.Keys)
                        {
                            string headerKey = propKey.ToString();
                            if (!IsCDKInternalProperty(headerKey))
                            {
                                if (writeAllProperties || propertiesToWrite.Contains(headerKey))
                                {
                                    string cleanHeaderKey = ReplaceInvalidHeaderChars(headerKey);
                                    if (!cleanHeaderKey.Equals(headerKey, StringComparison.Ordinal))
                                    {
                                        Trace.TraceInformation("Replaced characters in SDfile data header: ", headerKey, " written as: ", cleanHeaderKey);
                                    }

                                    Object val = sdFields[propKey];

                                    if (IsPrimitiveDataValue(val))
                                    {
                                        writer.Write("> <" + cleanHeaderKey + ">");
                                        writer.Write('\n');
                                        if (val != null)
                                        {
                                            writer.Write(val.ToString());
                                        }
                                        writer.Write('\n');
                                        writer.Write('\n');
                                    }
                                    else
                                    {
                                        Trace.TraceInformation("Skipped property " + propKey + " because only primitive and string properties can be written by SDFWriter");
                                    }
                                }
                            }
                        }
                    }
                }
                writer.Write("$$$$\n");
            }
            catch (IOException exception)
            {
                throw new CDKException("Error while writing a SD file entry: " + exception.Message, exception);
            }
        }