public void TestBug1649526()
        {
            //Read the original
            var filename = "NCDK.Data.MDL.bug-1649526.mol";
            var ins      = ResourceLoader.GetAsStream(filename);
            var reader   = new MDLReader(ins);
            var mol      = reader.Read(builder.NewAtomContainer());

            reader.Close();
            //Write it as cml
            var writer    = new StringWriter();
            var cmlWriter = new CMLWriter(writer);

            cmlWriter.Write(mol);
            cmlWriter.Close();
            //Read this again
            var cmlreader = new CMLReader(new MemoryStream(Encoding.UTF8.GetBytes(writer.ToString())));
            var file      = (IChemFile)cmlreader.Read(builder.NewChemFile());

            cmlreader.Close();
            //And finally write as mol
            var writermdl = new StringWriter();
            var mdlWriter = new MDLV2000Writer(writermdl);

            mdlWriter.Write(file);
            mdlWriter.Close();
            var output = writermdl.ToString().Replace("\r\n", "\n");

            //if there would be 3 instances (as in the bug), the only instance wouldnt't be right at the end
            Assert.AreEqual(2994, output.IndexOf("M  END"));
            //there would need some $$$$ to be in
            Assert.AreEqual(-1, output.IndexOf("$$$$"));
            //check atom/bond count
            Assert.AreEqual(25, output.IndexOf(" 31 33  0  0  0  0"));
        }
Exemple #2
0
        public void TestEmptyString()
        {
            var emptyString = "";
            var reader      = new MDLReader(new StringReader(emptyString), ChemObjectReaderMode.Strict);
            var mol         = reader.Read(builder.NewAtomContainer());

            reader.Close();
            Assert.IsNull(mol);
        }
Exemple #3
0
        public void TestMissingAtomProperties()
        {
            var ins      = ResourceLoader.GetAsStream("NCDK.Data.MDL.bug3485634.mol");
            var reader   = new MDLReader(ins);
            var molecule = builder.NewAtomContainer();

            molecule = reader.Read(molecule);
            reader.Close();
            Assert.AreEqual(9, molecule.Atoms.Count);
        }
Exemple #4
0
        public void TestHas2DCoordinates_With000()
        {
            var filenameMol = "NCDK.Data.MDL.with000coordinate.mol";
            var ins         = ResourceLoader.GetAsStream(filenameMol);
            var reader      = new MDLReader(ins, ChemObjectReaderMode.Relaxed);
            var molOne      = reader.Read(builder.NewAtomContainer());

            reader.Close();
            Assert.IsNull(molOne.Atoms[0].Point2D);
        }
Exemple #5
0
        public void Properties()
        {
            var ins      = ResourceLoader.GetAsStream("NCDK.Data.MDL.bug1356.sdf");
            var reader   = new MDLReader(ins);
            var chemfile = builder.NewChemFile();

            chemfile = reader.Read(chemfile);
            var container = ChemFileManipulator.GetAllAtomContainers(chemfile).First();

            Assert.IsNotNull(container.GetProperty <object>("first"));
            Assert.IsNotNull(container.GetProperty <object>("second"));
            reader.Close();
        }
Exemple #6
0
        public void TestUndefinedStereo()
        {
            var filename = "NCDK.Data.MDL.ChEBI_26120.mol";

            Trace.TraceInformation("Testing: " + filename);
            var ins    = ResourceLoader.GetAsStream(filename);
            var reader = new MDLReader(ins, ChemObjectReaderMode.Relaxed);
            var mol    = reader.Read(builder.NewAtomContainer());

            reader.Close();
            Assert.AreEqual(BondStereo.EOrZ, mol.Bonds[1].Stereo);
            Assert.AreEqual(BondStereo.EOrZ, mol.Bonds[6].Stereo);
            Assert.AreEqual(BondStereo.EOrZ, mol.Bonds[7].Stereo);
            Assert.AreEqual(BondStereo.EOrZ, mol.Bonds[11].Stereo);
        }
Exemple #7
0
        public void TestReadProton()
        {
            string mdl = "proton.mol\n" + "\n" + "\n" + "  1  0  0  0  0                 1\n"
                         + "   -0.0073   -0.5272    0.9655 H   0  3  0  0  0\n";
            var reader = new MDLReader(new StringReader(mdl), ChemObjectReaderMode.Strict);
            var mol    = reader.Read(builder.NewAtomContainer());

            reader.Close();
            Assert.IsNotNull(mol);
            Assert.AreEqual(1, mol.Atoms.Count);
            Assert.AreEqual(0, mol.Bonds.Count);
            var atom = mol.Atoms[0];

            Assert.AreEqual(1, atom.FormalCharge.Value);
        }
Exemple #8
0
        public void TestBug1542467()
        {
            var filename = "NCDK.Data.MDL.Strychnine_nichtOK.mol";

            Trace.TraceInformation("Testing: " + filename);
            var ins      = ResourceLoader.GetAsStream(filename);
            var reader   = new MDLReader(ins, ChemObjectReaderMode.Strict);
            var chemFile = reader.Read(builder.NewChemFile());

            reader.Close();
            Assert.IsNotNull(chemFile);
            var containersList = ChemFileManipulator.GetAllAtomContainers(chemFile).ToReadOnlyList();

            Assert.AreEqual(1, containersList.Count);
            Assert.IsTrue((containersList[0]).Atoms.Count > 0);
            Assert.IsTrue((containersList[0]).Bonds.Count > 0);
        }
Exemple #9
0
        public void TestStereoReadZeroDefault()
        {
            var filename = "NCDK.Data.MDL.prev2000.sd";

            Trace.TraceInformation("Testing: " + filename);
            var ins      = ResourceLoader.GetAsStream(filename);
            var reader   = new MDLReader(ins, ChemObjectReaderMode.Strict);
            var chemFile = reader.Read(builder.NewChemFile());

            reader.Close();
            Assert.IsNotNull(chemFile);
            var containersList = ChemFileManipulator.GetAllAtomContainers(chemFile).ToReadOnlyList();

            Assert.AreEqual(2, containersList.Count);
            var container = containersList[0];

            Assert.AreEqual(BondStereo.None, container.Bonds[0].Stereo);
        }
Exemple #10
0
        public void TestReadFromStringReader()
        {
            var mdl = "cyclopropane.mol\n" + "\n" + "\n" + "  9  9  0  0\n"
                      + "   -0.0073   -0.5272    0.9655 C   0  0  0  0  0\n"
                      + "   -0.6776   -0.7930   -0.3498 C   0  0  0  0  0\n"
                      + "    0.2103    0.4053   -0.1891 C   0  0  0  0  0\n"
                      + "    0.8019   -1.1711    1.2970 H   0  0  0  0  0\n"
                      + "   -0.6000   -0.2021    1.8155 H   0  0  0  0  0\n"
                      + "   -1.7511   -0.6586   -0.4435 H   0  0  0  0  0\n"
                      + "   -0.3492   -1.6277   -0.9620 H   0  0  0  0  0\n"
                      + "    1.1755    0.4303   -0.6860 H   0  0  0  0  0\n"
                      + "   -0.2264    1.3994   -0.1675 H   0  0  0  0  0\n" + "  1  2  1  6  0  0\n"
                      + "  1  3  1  6  0  0\n" + "  1  4  1  0  0  0\n" + "  1  5  1  1  0  0\n" + "  2  3  1  0  0  0\n"
                      + "  2  6  1  0  0  0\n" + "  2  7  1  6  0  0\n" + "  3  8  1  6  0  0\n" + "  3  9  1  0  0  0\n";
            var reader   = new MDLReader(new StringReader(mdl), ChemObjectReaderMode.Strict);
            var chemFile = reader.Read(builder.NewChemFile());

            reader.Close();
            Assert.IsNotNull(chemFile);
            Assert.AreEqual(1, chemFile.Count);
            var seq = chemFile[0];

            Assert.IsNotNull(seq);
            Assert.AreEqual(1, seq.Count);
            var model = seq[0];

            Assert.IsNotNull(model);

            var som = model.MoleculeSet;

            Assert.IsNotNull(som);
            Assert.AreEqual(1, som.Count);
            var m = som[0];

            Assert.IsNotNull(m);
            Assert.AreEqual(9, m.Atoms.Count);
            Assert.AreEqual(9, m.Bonds.Count);
        }
Exemple #11
0
        /// <summary>
        /// Read a Reaction from a file in MDL RXN format
        ///
        /// <returns>The Reaction that was read from the MDL file.</returns>
        /// </summary>
        private IReaction ReadReaction(IChemObjectBuilder builder)
        {
            Debug.WriteLine("Reading new reaction");
            int       linecount = 0;
            IReaction reaction  = builder.NewReaction();

            try
            {
                input.ReadLine(); // first line should be $RXN
                input.ReadLine(); // second line
                input.ReadLine(); // third line
                input.ReadLine(); // fourth line
            }
            catch (IOException exception)
            {
                Debug.WriteLine(exception);
                throw new CDKException("Error while reading header of RXN file", exception);
            }

            int reactantCount = 0;
            int productCount  = 0;

            try
            {
                string countsLine = input.ReadLine();
                linecount++;
                if (countsLine == null)
                {
                    return(null);
                }
                Debug.WriteLine("Line " + linecount + ": " + countsLine);
                if (countsLine.StartsWith("$$$$", StringComparison.Ordinal))
                {
                    Debug.WriteLine("File is empty, returning empty reaction");
                    return(reaction);
                }

                // this line contains the number of reactants and products
                var tokens = Strings.Tokenize(countsLine);
                reactantCount = int.Parse(tokens[0], NumberFormatInfo.InvariantInfo);
                Trace.TraceInformation("Expecting " + reactantCount + " reactants in file");
                productCount = int.Parse(tokens[1], NumberFormatInfo.InvariantInfo);
                Trace.TraceInformation("Expecting " + productCount + " products in file");
            }
            catch (Exception exception)
            {
                if (exception is IOException | exception is FormatException)
                {
                    Debug.WriteLine(exception);
                    throw new CDKException("Error while counts line of RXN file", exception);
                }
                throw;
            }

            // now read the reactants
            try
            {
                for (int i = 1; i <= reactantCount; i++)
                {
                    var molFile = new StringBuilder();
                    input.ReadLine(); // announceMDLFileLine
                    string molFileLine = "";
                    do
                    {
                        molFileLine = input.ReadLine();
                        molFile.Append(molFileLine);
                        molFile.Append('\n');
                    } while (!string.Equals(molFileLine, "M  END", StringComparison.Ordinal));

                    // read MDL molfile content
                    MDLReader      reader   = new MDLReader(new StringReader(molFile.ToString()));
                    IAtomContainer reactant = (IAtomContainer)reader.Read(builder.NewAtomContainer());
                    reader.Close();

                    // add reactant
                    reaction.Reactants.Add(reactant);
                }
            }
            catch (CDKException)
            {
                // rethrow exception from MDLReader
                throw;
            }
            catch (Exception exception)
            {
                if (exception is IOException | exception is ArgumentException)
                {
                    Debug.WriteLine(exception);
                    throw new CDKException("Error while reading reactant", exception);
                }
                throw;
            }

            // now read the products
            try
            {
                for (int i = 1; i <= productCount; i++)
                {
                    var molFile = new StringBuilder();
                    input.ReadLine(); // string announceMDLFileLine =
                    string molFileLine = "";
                    do
                    {
                        molFileLine = input.ReadLine();
                        molFile.Append(molFileLine);
                        molFile.Append('\n');
                    } while (!string.Equals(molFileLine, "M  END", StringComparison.Ordinal));

                    // read MDL molfile content
                    MDLReader      reader  = new MDLReader(new StringReader(molFile.ToString()), base.ReaderMode);
                    IAtomContainer product = (IAtomContainer)reader.Read(builder.NewAtomContainer());
                    reader.Close();

                    // add reactant
                    reaction.Products.Add(product);
                }
            }
            catch (CDKException)
            {
                // rethrow exception from MDLReader
                throw;
            }
            catch (Exception exception)
            {
                if (exception is IOException | exception is ArgumentException)
                {
                    Debug.WriteLine(exception);
                    throw new CDKException("Error while reading products", exception);
                }
                throw;
            }

            // now try to map things, if wanted
            Trace.TraceInformation("Reading atom-atom mapping from file");
            // distribute all atoms over two AtomContainer's
            IAtomContainer reactingSide = builder.NewAtomContainer();

            foreach (var reactant in reaction.Reactants)
            {
                reactingSide.Add(reactant);
            }
            IAtomContainer producedSide = builder.NewAtomContainer();

            foreach (var reactant in reaction.Products)
            {
                producedSide.Add(reactant);
            }

            // map the atoms
            int mappingCount = 0;

            //        IAtom[] reactantAtoms = reactingSide.GetAtoms();
            //        IAtom[] producedAtoms = producedSide.GetAtoms();
            for (int i = 0; i < reactingSide.Atoms.Count; i++)
            {
                for (int j = 0; j < producedSide.Atoms.Count; j++)
                {
                    IAtom eductAtom   = reactingSide.Atoms[i];
                    IAtom productAtom = producedSide.Atoms[j];
                    if (eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping) != null &&
                        eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping).Equals(
                            productAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping)))
                    {
                        reaction.Mappings.Add(builder.NewMapping(eductAtom, productAtom));
                        mappingCount++;
                        break;
                    }
                }
            }
            Trace.TraceInformation("Mapped atom pairs: " + mappingCount);

            return(reaction);
        }