Exemplo n.º 1
0
        public void TestDoubleCharge()
        {
            IRule rule = new NitrogenRule();

            IMolecularFormula formula = MolecularFormulaManipulator.GetMolecularFormula("C22H34N2S2", builder);

            formula.Charge = 2;

            Assert.AreEqual(1.0, rule.Validate(formula), 0.0001);
        }
Exemplo n.º 2
0
        public void TestWithCo()
        {
            IRule rule = new NitrogenRule();

            IMolecularFormula formula = MolecularFormulaManipulator.GetMolecularFormula("C43H50CoN4O16", builder);

            formula.Charge = 0;

            Assert.AreEqual(1.0, rule.Validate(formula), 0.0001);
        }
Exemplo n.º 3
0
        public void TestWithFe()
        {
            IRule rule = new NitrogenRule();

            IMolecularFormula formula = MolecularFormulaManipulator.GetMolecularFormula("C40H46FeN6O8S2", builder);

            formula.Charge = 2;

            Assert.AreEqual(1.0, rule.Validate(formula), 0.0001);
        }
Exemplo n.º 4
0
        public void TestNominalMass()
        {
            IRule rule = new NitrogenRule();

            IMolecularFormula formula = MolecularFormulaManipulator.GetMolecularFormula("C25H53NO7P", builder);

            formula.Charge = 1;

            Assert.AreEqual(1.0, rule.Validate(formula), 0.0001);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            IAtomContainer    mol1  = Chem.MolFromSmiles("[C:1]([C:5]1[CH:10]=[CH:9][C:8]([OH:11])=[CH:7][CH:6]=1)([CH3:4])([CH3:3])[CH3:2]");
            IMolecularFormula form1 = MolecularFormulaManipulator.GetMolecularFormula(mol1);

            Console.WriteLine(MolecularFormulaManipulator.GetString(form1));
            IAtomContainer mol2  = Chem.MolFromFile("/Users/john/My Documents/1,3-diisopropenylbenzene.mol");
            var            form2 = MolecularFormulaManipulator.GetMolecularFormula(mol2);

            Console.WriteLine(MolecularFormulaManipulator.GetString(form2));
        }
Exemplo n.º 6
0
        public string MolFormula => GetMolecularFormula(NativeMol);            // get mol formula

        /// <summary>
        /// GetMolecularFormula
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>

        public static string GetMolecularFormula(IAtomContainer mol)
        {
            if (mol == null)
            {
                return("");
            }
            IMolecularFormula moleculeFormula = MolecularFormulaManipulator.GetMolecularFormula(mol);
            String            formula         = MolecularFormulaManipulator.GetString(moleculeFormula);

            return(formula);
        }
Exemplo n.º 7
0
 private static void filterInorganicsFromIterator(IChemObjectSet <IAtomContainer> rxnIt)
 {
     for (int i = rxnIt.Count - 1; i >= 0; --i)
     {
         var sub     = rxnIt[i];
         var formula = MolecularFormulaManipulator.GetMolecularFormula(sub);
         if (!MolecularFormulaManipulator.ContainsElement(formula, ChemicalElement.C))
         {
             rxnIt.Remove(sub);
         }
     }
 }
Exemplo n.º 8
0
        public SingleStructureRandomGeneratorTest()
        {
            Console.Out.WriteLine("Instantiating MoleculeListViewer");
            Console.Out.WriteLine("Instantiating SingleStructureRandomGenerator");
            ssrg = new SingleStructureRandomGenerator();
            Console.Out.WriteLine("Assining unbonded set of atoms");
            AtomContainer ac = GetBunchOfUnbondedAtoms();

            mf = MolecularFormulaManipulator.GetString(MolecularFormulaManipulator.GetMolecularFormula(ac));
            Console.Out.WriteLine("Molecular Formula is: " + mf);
            ssrg.SetAtomContainer(ac);
        }
        public void TestGeneratorSavesState()
        {
            IsotopePatternGenerator isogen = new IsotopePatternGenerator(.1);

            IMolecularFormula mf1 = MolecularFormulaManipulator.GetMolecularFormula("C6H12O6", builder);
            IsotopePattern    ip1 = isogen.GetIsotopes(mf1);

            Assert.AreEqual(1, ip1.Isotopes.Count);

            IMolecularFormula mf2 = MolecularFormulaManipulator.GetMolecularFormula("C6H12O6", builder);
            IsotopePattern    ip2 = isogen.GetIsotopes(mf2);

            Assert.AreEqual(1, ip2.Isotopes.Count);
        }
Exemplo n.º 10
0
        public void TestMultipleFormulasForAMass()
        {
            var mf     = MolecularFormulaManipulator.GetMolecularFormula("C6Cl2", builder);
            var isogen = new IsotopePatternGenerator(0.1)
                         .SetMinIntensity(0.01)
                         .SetMinResolution(0.01)
                         .SetStoreFormulas(true);
            var pattern  = isogen.GetIsotopes(mf);
            var isotopes = pattern.Isotopes;

            Assert.AreEqual(1, isotopes[0].Formulas.Count);
            Assert.AreEqual(1, isotopes[1].Formulas.Count);
            Assert.AreEqual(2, isotopes[2].Formulas.Count);
            Assert.AreEqual(2, isotopes[3].Formulas.Count);
            Assert.AreEqual(3, isotopes[4].Formulas.Count);
        }
Exemplo n.º 11
0
        public static void Script8_3()
        {
            var parser = new SmilesParser();
            var hAdder = CDKHydrogenAdder.GetInstance();

            var methanol = parser.ParseSmiles("CO");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(methanol);
            hAdder.AddImplicitHydrogens(methanol);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(methanol);

            var dimethoxymethane = parser.ParseSmiles("COC");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(dimethoxymethane);
            hAdder.AddImplicitHydrogens(dimethoxymethane);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(dimethoxymethane);

            var water = parser.ParseSmiles("O");

            AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(water);
            hAdder.AddImplicitHydrogens(water);
            AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(water);

            var reaction = new Reaction();

            reaction.Reactants.Add(methanol, 2.0D);
            reaction.Products.Add(dimethoxymethane);
            reaction.Products.Add(water);

            Console.WriteLine("Reactants:");

            foreach (var reactant in reaction.Reactants)
            {
                var formula = MolecularFormulaManipulator.GetMolecularFormula(reactant);
                Console.WriteLine(MolecularFormulaManipulator.GetString(formula));
            }

            Console.WriteLine("Products: ");
            foreach (var product in reaction.Products)
            {
                var formula = MolecularFormulaManipulator.GetMolecularFormula(product);
                Console.WriteLine(MolecularFormulaManipulator.GetString(formula));
            }
        }
Exemplo n.º 12
0
        public void TestGetIsotopesIMolecularFormulaDeprotonate()
        {
            IsotopePatternGenerator isogen = new IsotopePatternGenerator(.1);

            IMolecularFormula mf1 = MolecularFormulaManipulator.GetMolecularFormula("C6H12O6", builder);

            MolecularFormulaManipulator.AdjustProtonation(mf1, -1);
            IsotopePattern ip1 = isogen.GetIsotopes(mf1);

            Assert.AreEqual(1, ip1.Isotopes.Count);

            isogen = new IsotopePatternGenerator(.1);
            IMolecularFormula mf2 = MolecularFormulaManipulator.GetMolecularFormula("C6H11O6", builder);
            IsotopePattern    ip2 = isogen.GetIsotopes(mf2);

            Assert.AreEqual(1, ip2.Isotopes.Count);

            Assert.AreEqual(ip1.Isotopes[0].Mass, ip2.Isotopes[0].Mass, 0.001);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Generates a fingerprint of the default size for the given
        /// AtomContainer, using path and ring metrics. It contains the
        /// informations from <see cref="Fingerprinter.GetBitFingerprint(IAtomContainer)"/> and bits which tell if the structure
        /// has 0 rings, 1 or less rings, 2 or less rings ... 10 or less rings and
        /// bits which tell if there is a fused ring system with 1,2...8 or more
        /// rings in it. The RingSet used is passed via rs parameter. This must be
        /// a smallesSetOfSmallestRings. The List must be a list of all ring
        /// systems in the molecule.
        /// </summary>
        /// <param name="atomContainer">The AtomContainer for which a Fingerprint is generated</param>
        /// <param name="ringSet">An SSSR RingSet of ac (if not available, use <see cref="GetBitFingerprint(IAtomContainer)"/>, which does the calculation)</param>
        /// <param name="rslist">A list of all ring systems in ac</param>
        /// <exception cref="CDKException">for example if input can not be cloned.</exception>
        /// <returns>a BitArray representing the fingerprint</returns>
        public IBitFingerprint GetBitFingerprint(IAtomContainer atomContainer, IRingSet ringSet, IEnumerable <IRingSet> rslist)
        {
            var container = (IAtomContainer)atomContainer.Clone();

            var fingerprint = fingerprinter.GetBitFingerprint(container);
            var size        = this.Length;
            var weight      = MolecularFormulaManipulator.GetTotalNaturalAbundance(MolecularFormulaManipulator.GetMolecularFormula(container));

            for (int i = 1; i < 11; i++)
            {
                if (weight > (100 * i))
                {
                    fingerprint.Set(size - 26 + i); // 26 := RESERVED_BITS+1
                }
            }
            if (ringSet == null)
            {
                ringSet = Cycles.FindSSSR(container).ToRingSet();
                rslist  = RingPartitioner.PartitionRings(ringSet);
            }
            for (int i = 0; i < 7; i++)
            {
                if (ringSet.Count > i)
                {
                    fingerprint.Set(size - 15 + i); // 15 := RESERVED_BITS+1+10 mass bits
                }
            }
            int maximumringsystemsize = 0;

            foreach (var rs in rslist)
            {
                if (rs.Count > maximumringsystemsize)
                {
                    maximumringsystemsize = rs.Count;
                }
            }
            for (int i = 0; i < maximumringsystemsize && i < 9; i++)
            {
                fingerprint.Set(size - 8 + i - 3);
            }
            return(fingerprint);
        }
Exemplo n.º 14
0
        public void TestHydrogen()
        {
            var mol    = builder.NewAtomContainer();
            var proton = builder.NewAtom("H");

            mol.Atoms.Add(proton);
            var type = matcher.FindMatchingAtomType(mol, proton);

            Assert.IsNotNull(type);
            AtomTypeManipulator.Configure(proton, type);

            adder.AddImplicitHydrogens(mol);

            Assert.AreEqual(1, mol.Atoms.Count);
            IMolecularFormula formula = MolecularFormulaManipulator.GetMolecularFormula(mol);

            Assert.AreEqual(2, MolecularFormulaManipulator.GetElementCount(formula, ChemicalElement.H));
            Assert.AreEqual(0, mol.GetConnectedBonds(proton).Count());
            Assert.IsNotNull(proton.ImplicitHydrogenCount);
            Assert.AreEqual(1, proton.ImplicitHydrogenCount.Value);
        }
Exemplo n.º 15
0
        public void TestGetIsotopesIMolecularFormulaCharged()
        {
            IsotopePatternGenerator isogen = new IsotopePatternGenerator(.1);

            IMolecularFormula mfPositive = MolecularFormulaManipulator.GetMolecularFormula("C6H11O6Na", builder);

            mfPositive.Charge = 1;
            IsotopePattern ip1 = isogen.GetIsotopes(mfPositive);

            Assert.AreEqual(1, ip1.Isotopes.Count);

            isogen = new IsotopePatternGenerator(.1);
            IMolecularFormula mfNeutral = MolecularFormulaManipulator.GetMolecularFormula("C6H12O6Na", builder);

            mfNeutral.Charge = 0;
            IsotopePattern ip2 = isogen.GetIsotopes(mfNeutral);

            Assert.AreEqual(1, ip2.Isotopes.Count);

            Assert.AreNotEqual(ip1.Isotopes[0].Mass, ip2.Isotopes[0].Mass);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Calculates the 3 MI's, 3 ration and the R_gyr value.
        /// The molecule should have hydrogens.
        /// </summary>
        /// <returns>An <see cref="Result"/> containing 7 elements in the order described above</returns>
        public Result Calculate(IAtomContainer container)
        {
            container = (IAtomContainer)container.Clone();

            var factory = CDK.IsotopeFactory;

            factory.ConfigureAtoms(container);

            if (!GeometryUtil.Has3DCoordinates(container))
            {
                throw new ThreeDRequiredException("Molecule must have 3D coordinates");
            }

            var retval = new List <double>(7);

            double ccf = 1.000138;
            double eps = 1e-5;

            var imat         = Arrays.CreateJagged <double>(3, 3);
            var centerOfMass = GeometryUtil.Get3DCentreOfMass(container).Value;

            double xdif;
            double ydif;
            double zdif;
            double xsq;
            double ysq;
            double zsq;

            for (int i = 0; i < container.Atoms.Count; i++)
            {
                var currentAtom = container.Atoms[i];

                var _mass = factory.GetMajorIsotope(currentAtom.Symbol).ExactMass;
                var mass  = _mass == null?factory.GetNaturalMass(currentAtom.Element) : _mass.Value;

                var p = currentAtom.Point3D.Value;
                xdif = p.X - centerOfMass.X;
                ydif = p.Y - centerOfMass.Y;
                zdif = p.Z - centerOfMass.Z;
                xsq  = xdif * xdif;
                ysq  = ydif * ydif;
                zsq  = zdif * zdif;

                imat[0][0] += mass * (ysq + zsq);
                imat[1][1] += mass * (xsq + zsq);
                imat[2][2] += mass * (xsq + ysq);

                imat[1][0] += -1 * mass * ydif * xdif;
                imat[0][1]  = imat[1][0];

                imat[2][0] += -1 * mass * xdif * zdif;
                imat[0][2]  = imat[2][0];

                imat[2][1] += -1 * mass * ydif * zdif;
                imat[1][2]  = imat[2][1];
            }

            // diagonalize the MI tensor
            var tmp = Matrix <double> .Build.DenseOfColumnArrays(imat);

            var eigenDecomp = tmp.Evd();
            var eval        = eigenDecomp.EigenValues.Select(n => n.Real).ToArray();

            retval.Add(eval[2]);
            retval.Add(eval[1]);
            retval.Add(eval[0]);

            var etmp = eval[0];

            eval[0] = eval[2];
            eval[2] = etmp;

            if (Math.Abs(eval[1]) > 1e-3)
            {
                retval.Add(eval[0] / eval[1]);
            }
            else
            {
                retval.Add(1000);
            }

            if (Math.Abs(eval[2]) > 1e-3)
            {
                retval.Add(eval[0] / eval[2]);
                retval.Add(eval[1] / eval[2]);
            }
            else
            {
                retval.Add(1000);
                retval.Add(1000);
            }

            // finally get the radius of gyration
            var formula = MolecularFormulaManipulator.GetMolecularFormula(container);
            var pri     = Math.Abs(eval[2]) > eps
                    ? Math.Pow(eval[0] *eval[1] *eval[2], 1.0 / 3.0)
                    : Math.Sqrt(eval[0] * ccf / MolecularFormulaManipulator.GetTotalExactMass(formula));

            retval.Add(Math.Sqrt(Math.PI * 2 * pri * ccf / MolecularFormulaManipulator.GetTotalExactMass(formula)));

            return(new Result(retval));
        }
Exemplo n.º 17
0
        [TestCategory("VerySlowTest")] // structgen is slow... a single method here currently takes ~6 seconds
        public void TestDoCrossover_IAtomContainer()
        {
            IChemObjectSet <IAtomContainer> som;

            var filename = "NCDK.Data.Smiles.c10h16isomers.smi";

            using (var ins = ResourceLoader.GetAsStream(filename))
                using (SMILESReader reader = new SMILESReader(ins))
                {
                    som = reader.Read(CDK.Builder.NewAtomContainerSet());
                    Assert.AreEqual(99, som.Count, "We must have read 99 structures");
                }

            // Comment out next line to enable time seed random.
            RandomNumbersTool.RandomSeed = 0L;

            CrossoverMachine cm             = new CrossoverMachine();
            string           correctFormula = "C10H16";
            int errorcount = 0;

            for (int i = 0; i < som.Count; i++)
            {
                int[] hydrogencount1 = new int[4];
                foreach (var atom in som[i].Atoms)
                {
                    hydrogencount1[atom.ImplicitHydrogenCount.Value]++;
                }
                for (int k = i + 1; k < som.Count; k++)
                {
                    try
                    {
                        var   result         = cm.DoCrossover(som[i], som[k]);
                        int[] hydrogencount2 = new int[4];
                        foreach (var atom in som[k].Atoms)
                        {
                            hydrogencount2[atom.ImplicitHydrogenCount.Value]++;
                        }
                        Assert.AreEqual(2, result.Count, "Result size must be 2");
                        for (int l = 0; l < 2; l++)
                        {
                            IAtomContainer ac = result[l];
                            Assert.IsTrue(ConnectivityChecker.IsConnected(ac), "Result must be connected");
                            Assert.AreEqual(
                                MolecularFormulaManipulator.GetString(MolecularFormulaManipulator.GetMolecularFormula(ac)),
                                correctFormula,
                                "Molecular formula must be the same as" + "of the input");
                            int[] hydrogencountresult = new int[4];
                            int   hcounttotal         = 0;
                            foreach (var atom in result[l].Atoms)
                            {
                                hydrogencountresult[atom.ImplicitHydrogenCount.Value]++;
                                hcounttotal += atom.ImplicitHydrogenCount.Value;
                            }
                            if (hydrogencount1[0] == hydrogencount2[0])
                            {
                                Assert.AreEqual(
                                    hydrogencount1[0], hydrogencountresult[0],
                                    "Hydrogen count of the result must" + " be same as of input");
                            }
                            if (hydrogencount1[1] == hydrogencount2[1])
                            {
                                Assert.AreEqual(
                                    hydrogencount1[1], hydrogencountresult[1],
                                    "Hydrogen count of the result must" + " be same as of input");
                            }
                            if (hydrogencount1[2] == hydrogencount2[2])
                            {
                                Assert.AreEqual(
                                    hydrogencount1[2], hydrogencountresult[2],
                                    "Hydrogen count of the result must" + " be same as of input");
                            }
                            if (hydrogencount1[3] == hydrogencount2[3])
                            {
                                Assert.AreEqual(
                                    hydrogencount1[3], hydrogencountresult[3],
                                    "Hydrogen count of the result must" + " be same as of input");
                            }
                            Assert.AreEqual(16, hcounttotal);
                        }
                    }
                    catch (CDKException)
                    {
                        errorcount++;
                    }
                }
            }
            Assert.IsTrue(errorcount < 300, "We tolerate up to 300 errors");
        }
Exemplo n.º 18
0
        // Private procedures

        private void WriteCrystal(ICrystal crystal)
        {
            var title = crystal.Title;

            if (title != null && title.Trim().Length > 0)
            {
                Writeln($"TITL {title.Trim()}");
            }
            else
            {
                Writeln("TITL Produced with CDK (http://cdk.sf.net/)");
            }
            Vector3 a       = crystal.A;
            Vector3 b       = crystal.B;
            Vector3 c       = crystal.C;
            double  alength = a.Length();
            double  blength = b.Length();
            double  clength = c.Length();
            double  alpha   = Vectors.RadianToDegree(Vectors.Angle(b, c));
            double  beta    = Vectors.RadianToDegree(Vectors.Angle(a, c));
            double  gamma   = Vectors.RadianToDegree(Vectors.Angle(a, b));

            Write("CELL " + 1.54184.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
            Write(alength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(blength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(clength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(alpha.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Write(beta.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Write(gamma.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Writeln("ZERR " + ((double)crystal.Z).ToString("F5", NumberFormatInfo.InvariantInfo)
                    + "    0.01000  0.01000   0.01000   0.0100   0.0100   0.0100");
            string spaceGroup = crystal.SpaceGroup;

            if (string.Equals("P1", spaceGroup, StringComparison.Ordinal))
            {
                Writeln("LATT  -1");
            }
            else if (string.Equals("P 2_1 2_1 2_1", spaceGroup, StringComparison.Ordinal))
            {
                Writeln("LATT  -1");
                Writeln("SYMM  1/2+X   , 1/2-Y   ,    -Z");
                Writeln("SYMM     -X   , 1/2+Y   , 1/2-Z");
                Writeln("SYMM  1/2-X   ,    -Y   , 1/2+Z");
            }
            string            elemNames  = "";
            string            elemCounts = "";
            IMolecularFormula formula    = MolecularFormulaManipulator.GetMolecularFormula(crystal);
            var asortedElements          = MolecularFormulaManipulator.Elements(formula).ToReadOnlyList();

            foreach (var element in asortedElements)
            {
                string symbol = element.Symbol;
                elemNames += symbol + "    ".Substring(symbol.Length);
                string countS = MolecularFormulaManipulator.GetElementCount(formula, element).ToString(NumberFormatInfo.InvariantInfo);
                elemCounts += countS + "    ".Substring(countS.Length);
            }
            Writeln("SFAC  " + elemNames);
            Writeln("UNIT  " + elemCounts);
            /* write atoms */
            for (int i = 0; i < crystal.Atoms.Count; i++)
            {
                IAtom   atom      = crystal.Atoms[i];
                Vector3 cartCoord = atom.Point3D.Value;
                Vector3 fracCoord = CrystalGeometryTools.CartesianToFractional(a, b, c, cartCoord);
                string  symbol    = atom.Symbol;
                string  output    = symbol + (i + 1);
                Write(output);
                for (int j = 1; j < 5 - output.Length; j++)
                {
                    Write(" ");
                }
                Write("     ");
                string elemID = null;
                for (int elemidx = 0; elemidx < asortedElements.Count; elemidx++)
                {
                    var elem = asortedElements[elemidx];
                    if (elem.Symbol.Equals(symbol, StringComparison.Ordinal))
                    {
                        elemID = (elemidx + 1).ToString(NumberFormatInfo.InvariantInfo);
                        break;
                    }
                }
                Write(elemID);
                Write("    ".Substring(elemID.Length));
                Write(fracCoord.X.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
                Write(fracCoord.Y.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
                Writeln(fracCoord.Y.ToString("F5", NumberFormatInfo.InvariantInfo) + "    11.00000    0.05000");
            }
            Writeln("END");
        }