コード例 #1
0
ファイル: ReaderFactoryTest.cs プロジェクト: roddickchen/NCDK
        public void TestCreateReader_IChemFormat()
        {
            IChemFormat             format = (IChemFormat)XYZFormat.Instance;
            ISimpleChemObjectReader reader = factory.CreateReader(format, new StringReader(""));

            Assert.IsNotNull(reader);
            Assert.AreEqual(format.FormatName, reader.Format.FormatName);
        }
コード例 #2
0
        /// <summary>
        /// Detects the format of the Reader input, and if known, it will return
        /// a CDK Reader to read the format, or null when the reader is not
        /// implemented.
        /// </summary>
        /// <param name="input"></param>
        /// <returns><see langword="null"/> if CDK does not contain a reader for the detected format.</returns>
        /// <seealso cref="CreateReader(TextReader)"/>
        public ISimpleChemObjectReader CreateReader(Stream input)
        {
            IChemFormat             format = null;
            ISimpleChemObjectReader reader = null;

            if (input is GZipStream)
            {
                var istreamToRead = new ReadSeekableStream(input, 65536);
                format = formatFactory.GuessFormat(istreamToRead);
                var type = GetReaderType(format);
                if (type != null)
                {
                    try
                    {
                        reader = (ISimpleChemObjectReader)type.GetConstructor(new Type[] { typeof(Stream) }).Invoke(new object[] { istreamToRead });
                    }
                    catch (CDKException e1)
                    {
                        var wrapper = new IOException("Exception while setting the Stream: " + e1.Message, e1);
                        throw wrapper;
                    }
                }
            }
            else
            {
                var bistream      = input;
                var istreamToRead = bistream; // if gzip test fails, then take default
                                              //                bistream.Mark(5);
                int countRead = 0;
                var abMagic   = new byte[4];
                countRead = bistream.Read(abMagic, 0, 4);
                bistream.Seek(0, SeekOrigin.Begin);
                if (countRead == 4)
                {
                    if (abMagic[0] == (byte)0x1F && abMagic[1] == (byte)0x8B)
                    {
                        istreamToRead = new GZipStream(bistream, CompressionMode.Decompress);
                        return(CreateReader(istreamToRead));
                    }
                }
                format = formatFactory.GuessFormat(istreamToRead);
                var type = GetReaderType(format);
                if (type != null)
                {
                    try
                    {
                        reader = (ISimpleChemObjectReader)type.GetConstructor(new Type[] { typeof(Stream) }).Invoke(new object[] { istreamToRead });
                    }
                    catch (CDKException e1)
                    {
                        var wrapper = new IOException("Exception while setting the Stream: " + e1.Message, e1);
                        throw wrapper;
                    }
                }
            }
            return(reader);
        }
コード例 #3
0
ファイル: PDBReaderTest.cs プロジェクト: roddickchen/NCDK
        public IChemFile GetChemFile(ISimpleChemObjectReader reader, bool useRebond)
        {
            Assert.IsNotNull(reader);

            reader.IOSettings["UseRebondTool"].Setting = useRebond.ToString();

            var chemFile = reader.Read(builder.NewChemFile());

            Assert.IsNotNull(chemFile);
            return(chemFile);
        }
コード例 #4
0
ファイル: ReaderFactoryTest.cs プロジェクト: roddickchen/NCDK
        public void TestReadGzWithGzipDetection()
        {
            var filename = "NCDK.Data.XYZ.bf3.xyz.gz";
            var input    = ResourceLoader.GetAsStream(filename);
            // ok, if format ok, try instantiating a reader
            ISimpleChemObjectReader reader = factory.CreateReader(input);

            Assert.IsNotNull(reader);
            Assert.AreEqual(((IChemFormat)XYZFormat.Instance).ReaderClassName, reader.GetType().FullName);
            // now try reading something from it
            var chemFile = reader.Read(builder.NewChemFile());
            var molecule = builder.NewAtomContainer();

            foreach (var container in ChemFileManipulator.GetAllAtomContainers(chemFile))
            {
                molecule.Add(container);
            }
            Assert.IsNotNull(molecule);
            Assert.AreEqual(4, molecule.Atoms.Count);
        }
コード例 #5
0
ファイル: ReaderFactoryTest.cs プロジェクト: roddickchen/NCDK
        public void TestBug2153298()
        {
            var filename = "NCDK.Data.ASN.PubChem.cid1145.xml";
            var ins      = ResourceLoader.GetAsStream(filename);

            Assert.IsNotNull(ins, "Cannot find file: " + filename);
            IChemFormatMatcher realFormat = (IChemFormatMatcher)PubChemCompoundXMLFormat.Instance;

            factory.RegisterFormat(realFormat);
            // ok, if format ok, try instantiating a reader
            ins = ResourceLoader.GetAsStream(filename);
            ISimpleChemObjectReader reader = factory.CreateReader(ins);

            Assert.IsNotNull(reader);
            Assert.AreEqual(((IChemFormat)PubChemCompoundXMLFormat.Instance).ReaderClassName, reader.GetType().FullName);
            // now try reading something from it
            IAtomContainer molecule = (IAtomContainer)reader.Read(builder.NewAtomContainer());

            Assert.IsNotNull(molecule);
            Assert.AreNotSame(0, molecule.Atoms.Count);
            Assert.AreNotSame(0, molecule.Bonds.Count);
        }
コード例 #6
0
        internal void ExpectReader(string filename, IResourceFormat expectedFormat, int expectedAtomCount, int expectedBondCount)
        {
            var ins = ResourceLoader.GetAsStream(filename);

            Assert.IsNotNull(ins, "Cannot find file: " + filename);
            if (expectedFormat is IChemFormatMatcher)
            {
                factory.RegisterFormat((IChemFormatMatcher)expectedFormat);
            }
            ISimpleChemObjectReader reader = factory.CreateReader(ins);

            Assert.IsNotNull(reader);
            Assert.AreEqual(((IChemFormat)expectedFormat).ReaderClassName, reader.GetType().FullName);
            // now try reading something from it
            IChemObject[] objects = { new ChemFile(), new ChemModel(), new AtomContainer(), new Reaction() };
            bool          read    = false;

            for (int i = 0; (i < objects.Length && !read); i++)
            {
                if (reader.Accepts(objects[i].GetType()))
                {
                    IChemObject chemObject = reader.Read(objects[i]);
                    Assert.IsNotNull(chemObject, "Reader accepted a " + objects[i].GetType().Name + " but failed to read it");
                    AssertAtomCount(expectedAtomCount, chemObject);
                    AssertBondCount(expectedBondCount, chemObject);
                    read = true;
                }
            }
            if (read)
            {
                // ok, reseting worked
            }
            else
            {
                Assert.Fail("Reading an IChemObject from the Reader did not work properly.");
            }
        }
コード例 #7
0
ファイル: PDBReaderTest.cs プロジェクト: roddickchen/NCDK
 public IChemFile GetChemFile(ISimpleChemObjectReader reader)
 {
     return(GetChemFile(reader, false));
 }
コード例 #8
0
        public override IEnumerator <IAtomContainer> GetEnumerator()
        {
            // buffer to store pre-read Mol records in
            var buffer = new StringBuilder(10000);

            // now try to parse the next Molecule
            currentFormat = (IChemFormat)MDLFormat.Instance;
            int lineNum = 0;

            buffer.Length = 0;

            while ((currentLine = input.ReadLine()) != null)
            {
                // still in a molecule
                buffer.Append(currentLine).Append('\n');
                lineNum++;

                // do MDL molfile version checking
                if (lineNum == 4)
                {
                    var versionMatcher = MDL_Version.Match(currentLine);
                    if (versionMatcher.Success)
                    {
                        currentFormat = string.Equals("2000", versionMatcher.Groups[1].Value, StringComparison.Ordinal)
                            ? (IChemFormat)MDLV2000Format.Instance
                            : (IChemFormat)MDLV3000Format.Instance;
                    }
                }

                if (currentLine.StartsWith(M_END, StringComparison.Ordinal))
                {
                    Debug.WriteLine($"MDL file part read: {buffer}");

                    IAtomContainer molecule = null;

                    try
                    {
                        ISimpleChemObjectReader reader = GetReader(currentFormat, new StringReader(buffer.ToString()));
                        molecule = reader.Read(builder.NewAtomContainer());
                    }
                    catch (Exception exception)
                    {
                        Trace.TraceError($"Error while reading next molecule: {exception.Message}");
                        Debug.WriteLine(exception);
                    }

                    if (molecule != null)
                    {
                        ReadDataBlockInto(molecule);
                        yield return(molecule);
                    }
                    else if (Skip)
                    {
                        // null molecule and skip = true, eat up the rest of the entry until '$$$$'
                        string line;
                        while ((line = input.ReadLine()) != null)
                        {
                            if (line.StartsWith(SDF_RECORD_SEPARATOR, StringComparison.Ordinal))
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        // don't yield
                    }

                    // empty the buffer
                    buffer.Clear();
                    lineNum = 0;
                }

                // found SDF record separator ($$$$) without parsing a molecule (separator is detected
                // in ReadDataBlockInto()) the buffer is cleared and the iterator continues reading
                if (currentLine == null)
                {
                    break;
                }
                if (currentLine.StartsWith(SDF_RECORD_SEPARATOR, StringComparison.Ordinal))
                {
                    buffer.Clear();
                    lineNum = 0;
                }
            }

            yield break;
        }