Example #1
0
        public void Carbon13Test()
        {
            IElementProvider provider = new MockElementProvider();
            IElement         c        = provider.GetElement("C");
            IElement         c13      = provider.GetElement("C", 13);

            var formula = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(c, 1),
                new EntityCardinality <IElement>(c13, 1),
            });

            var elements = formula.GetElements();

            Assert.AreEqual(2, elements.Count);

            // Check masses
            Assert.AreEqual(c.Isotopes.FirstWithMax(x => x.RelativeAbundance).AtomicMass
                            + c13.Isotopes.FirstWithMax(x => x.RelativeAbundance).AtomicMass,
                            formula.GetMass(MassType.Monoisotopic));
            Assert.AreEqual(c.Isotopes.Sum(x => x.AtomicMass * x.RelativeAbundance)
                            + c13.Isotopes.Sum(x => x.AtomicMass * x.RelativeAbundance),
                            formula.GetMass(MassType.Average));
            Assert.AreEqual(c13.GetMass(MassType.Monoisotopic), c13.GetMass(MassType.Average));
        }
        public MZSpectrum CalculateDistribuition(ChemicalFormula formula, int topNPeaks = int.MaxValue, Normalization normalization = Normalization.Sum)
        {
            double monoisotopicMass = formula.MonoisotopicMass;
            SetResolution(monoisotopicMass);

            List<List<Composition>> elementalComposition = new List<List<Composition>>();

            // Get all the unique elements of the formula
            foreach (Element element in formula.GetElements())
            {
                int count = formula.Count(element);
                List<Composition> isotopeComposition = new List<Composition>();
                foreach (Isotope isotope in element.Isotopes.Values.OrderBy(iso => iso.AtomicMass))
                {
                    double probability = isotope.RelativeAbundance;
                    if (probability <= 0)
                        continue;

                    Composition c = new Composition
                    {
                        Atoms = count,
                        MolecularWeight = isotope.AtomicMass,
                        Power = isotope.AtomicMass,
                        Probability = isotope.RelativeAbundance
                    };

                    isotopeComposition.Add(c);
                }
                elementalComposition.Add(isotopeComposition);
            }

            foreach (List<Composition> compositions in elementalComposition)
            {
                double sumProb = compositions.Sum(t => t.Probability);
                foreach (Composition composition in compositions)
                {
                    composition.Probability /= sumProb;
                    composition.LogProbability = Math.Log(composition.Probability);
                    composition.Power = Math.Floor(composition.MolecularWeight/_mwResolution + 0.5);
                }
            }

            return CalculateFineGrain(elementalComposition, normalization);
        }
Example #3
0
        private IsotopicDistribution Mercury(ChemicalFormula formula, double limit)
        {
            double[] msaMz        = null;
            double[] msaAbundance = null;

            double[] tmpMz          = null;
            double[] tmpAbundance   = null;
            bool     msaInitialized = false;

            foreach (KeyValuePair <Element, int> kvp in formula.GetElements())
            {
                int n = kvp.Value;

                if (n == 0)
                {
                    continue;
                }

                int      isotopeCount = kvp.Key.IsotopeDistribution.Count;
                double[] esaMz        = new double[isotopeCount];
                double[] esaAbundance = new double[isotopeCount];

                int i = 0;
                foreach (var isotope in kvp.Key.IsotopeDistribution)
                {
                    esaMz[i]        = isotope.RelativeAtomicMass;
                    esaAbundance[i] = isotope.Abundance;
                    i++;
                }

                while (true)
                {
                    if ((n & 1) == 1)
                    {
                        if (msaInitialized)
                        {
                            Convolve(ref tmpMz, ref tmpAbundance, msaMz, msaAbundance, esaMz, esaAbundance);

                            msaMz        = this.CopyArray(tmpMz);
                            msaAbundance = this.CopyArray(tmpAbundance);
                        }
                        else
                        {
                            msaMz          = this.CopyArray(esaMz);
                            msaAbundance   = this.CopyArray(esaAbundance);
                            msaInitialized = true;
                        }

                        Prune(ref msaMz, ref msaAbundance, limit);
                    }

                    if (n == 1)
                    {
                        break;
                    }

                    Convolve(ref tmpMz, ref tmpAbundance, esaMz, esaAbundance, esaMz, esaAbundance);

                    esaMz        = this.CopyArray(tmpMz);
                    esaAbundance = this.CopyArray(tmpAbundance);

                    Prune(ref esaMz, ref esaAbundance, limit);
                    n = n >> 1;
                }
            }

            return(new IsotopicDistribution(msaMz, msaAbundance));
        }