Пример #1
0
 public ProteoformGroup(IReadOnlyList <IResidue> residues, IProteoformMassDelta?nTerminalModification,
                        IProteoformMassDelta?cTerminalModification,
                        IReadOnlyCollection <IProteoformLocalizedModification>?localizedModifications,
                        IReadOnlyCollection <IProteoformUnlocalizedModification>?unlocalizedModifications,
                        IReadOnlyCollection <IProteoformModificationGroup>?modificationGroups,
                        IReadOnlyCollection <IProteoformGlobalModification>?globalModifications,
                        IChemicalFormula water)
 {
     Residues = residues;
     NTerminalModification    = nTerminalModification;
     CTerminalModification    = cTerminalModification;
     LocalizedModifications   = localizedModifications;
     UnlocalizedModifications = unlocalizedModifications;
     ModificationGroups       = modificationGroups;
     GlobalModifications      = globalModifications;
     Water = water;
 }
Пример #2
0
        /// <summary>
        /// Saves the current modifications and isotopologues
        /// </summary>
        public static void SaveTo(string filePath)
        {
            using (XmlWriter writer = XmlWriter.Create(filePath, new XmlWriterSettings {
                Indent = true
            }))
            {
                writer.WriteStartDocument();

                writer.WriteStartElement("Modifications");

                foreach (var mod in Modifications.Values.Distinct())
                {
                    writer.WriteStartElement("Modification");
                    writer.WriteAttributeString("name", mod.Name);

                    IChemicalFormula chemFormula = mod as IChemicalFormula;
                    if (chemFormula != null)
                    {
                        writer.WriteElementString("ChemicalFormula", chemFormula.ToString());
                    }
                    else
                    {
                        writer.WriteElementString("DeltaMass", mod.MonoisotopicMass.ToString("F10"));
                    }

                    foreach (ModificationSites site in mod.Sites.GetActiveSites())
                    {
                        writer.WriteElementString("ModificationSite", site.ToString());
                    }

                    foreach (KeyValuePair <string, Modification> mods in Modifications)
                    {
                        if (mods.Value.Equals(mod))
                        {
                            writer.WriteElementString("AlternativeName", mods.Key);
                        }
                    }

                    writer.WriteEndElement();
                }

                writer.WriteEndElement(); // end Modifications

                writer.WriteEndDocument();
            }
        }
Пример #3
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.
        /// </returns>
        public bool Equals(IChemicalFormula other)
        {
            if (other == null)
            {
                return(false);
            }

            if (this == other)
            {
                return(true);
            }

            IReadOnlyCollection <IEntityCardinality <IElement> > otherElements = other.GetElements();

            if (_elements.Count != otherElements.Count)
            {
                return(false);
            }

            if (_elements.Sum(x => x.Count) != otherElements.Sum(x => x.Count))
            {
                return(false);
            }

            foreach (IEntityCardinality <IElement> element in _elements)
            {
                var otherElement = otherElements.SingleOrDefault(x => x.Entity.Equals(element.Entity));

                // Check that the other chemical formula has this element.
                if (otherElement == null)
                {
                    return(false);
                }

                // Check the counts.
                if (element.Count != otherElement.Count)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #4
0
        public void IsotopeTest()
        {
            const string formula = "C(-9) 13C(9)";

            var composition   = UnimodComposition.CreateFromFormula(formula, atomProvider);
            var cardinalities = composition.GetAtomCardinalities();

            Assert.AreEqual(2, cardinalities.Count);
            Assert.AreEqual(atomProvider.GetUnimodCompositionAtom("C"), cardinalities[0].Atom);
            Assert.AreEqual(-9, cardinalities[0].Count);
            Assert.AreEqual(atomProvider.GetUnimodCompositionAtom("13C"), cardinalities[1].Atom);
            Assert.AreEqual(9, cardinalities[1].Count);

            IChemicalFormula chemicalFormula = composition.GetChemicalFormula();
            var elements = chemicalFormula.GetElements();

            Assert.AreEqual(2, elements.Count);
            Assert.AreEqual(-9, elements.Single(x => x.Entity.Symbol == "C").Count);
            Assert.AreEqual(9, elements.Single(x => x.Entity.Symbol == "13C").Count);
        }
Пример #5
0
        /// <summary>Gets the chemical formula.</summary>
        /// <returns></returns>
        public IChemicalFormula GetChemicalFormula()
        {
            IChemicalFormula formula = ChemicalFormula.Empty;

            foreach (UnimodCompositionAtomCardinality atom in _atomCardinalities)
            {
                IChemicalFormula atomFormula = atom.Atom.GetChemicalFormula().Multiply(atom.Count);

                if (formula == null)
                {
                    formula = atomFormula;
                }
                else
                {
                    formula = formula.Add(atomFormula);
                }
            }

            return(formula);
        }
Пример #6
0
        /// <summary>
        /// Adds the specified formula.
        /// </summary>
        /// <param name="otherFormula">The formula.</param>
        /// <returns></returns>
        public IChemicalFormula Add(IChemicalFormula otherFormula)
        {
            if (otherFormula == null)
            {
                return(this);
            }

            var otherElements = otherFormula.GetElements().ToList();

            if (otherElements.Count == 0)
            {
                return(this);
            }

            var formula = new ChemicalFormula();

            // Add everything from this formula
            foreach (var element in _elements)
            {
                var otherElement = otherElements.SingleOrDefault(x => x.Entity.Equals(element.Entity));

                if (otherElement == null)
                {
                    formula._elements.Add(element);
                }
                else
                {
                    formula._elements.Add(new EntityCardinality <IElement>(element.Entity, element.Count + otherElement.Count));
                    otherElements.Remove(otherElement);
                }
            }

            // Add unique things from other formula
            foreach (var otherElement in otherElements)
            {
                formula._elements.Add(otherElement);
            }

            return(formula);
        }
Пример #7
0
        private List <IChargedIsotopicDistribution> Mercury(IChemicalFormula cf, int firstcharge, int lastcharge, double limit)
        {
            IIsotopicDistribution dist = this.Mercury(cf, limit);
            var mercury7ResultList     = new List <IChargedIsotopicDistribution>();

            if (firstcharge > 0)
            {
                for (int j = firstcharge; j < lastcharge + 1; j++)
                {
                    mercury7ResultList.Add(dist.CreateChargedDistribution(j));
                }
            }
            else if (firstcharge < 0)
            {
                for (int j = firstcharge; j > lastcharge - 1; j--)
                {
                    mercury7ResultList.Add(dist.CreateChargedDistribution(j, false));
                }
            }

            return(mercury7ResultList);
        }
Пример #8
0
 public ModificationWrapper(T modification, IChemicalFormula chemicalFormula)
 {
     this.Modification = modification;
     _chemicalFormula  = chemicalFormula;
 }
Пример #9
0
        /// <summary>Attempts to parse the string into a ChemicalFormula.</summary>
        /// <param name="formula">The chemical formula as a string.</param>
        /// <param name="elementProvider">The element provider.</param>
        /// <param name="chemicalFormula">The chemical formula or null if string was not formatted correctly.</param>
        /// <returns>True if successful, otherwise false.</returns>
        public static bool TryParseString(ReadOnlySpan <char> formula, IElementProvider elementProvider, out IChemicalFormula chemicalFormula)
        {
            chemicalFormula = ChemicalFormula.Empty; // Set to null in case of failure.

            IDictionary <string, IEntityCardinality <IElement> > elementList = new Dictionary <string, IEntityCardinality <IElement> >();

            int  symbolStart = 0, symbolEnd = 0;
            int  digitStart = 0, digitEnd = 0;
            int  isotopeStart = 0, isotopeEnd = 0;
            bool sawUpperCaseLetter = false;

            for (int i = 0; i < formula.Length; i++)
            {
                // Check to see if this is the start of a new element
                if (i > 0 && (formula[i] == '[' || (sawUpperCaseLetter && char.IsUpper(formula[i]))))
                {
                    if (!HandleNewElement(formula, elementProvider, elementList, symbolStart, symbolEnd, digitStart, digitEnd, isotopeStart, isotopeEnd))
                    {
                        return(false);
                    }

                    // Reset things
                    sawUpperCaseLetter = false;
                    digitStart         = 0;
                    digitEnd           = 0;
                    isotopeStart       = 0;
                    isotopeEnd         = 0;
                }

                if (formula[i] == '[')
                {
                    isotopeStart = i++ + 1;
                }
                else if (formula[i] == ']')
                {
                    // Ignore because a new element is starting on the next loop
                }
                else if (char.IsLetter(formula[i]))
                {
                    if (char.IsUpper(formula[i]))
                    {
                        // New element
                        sawUpperCaseLetter = true;
                        symbolStart        = i;
                        symbolEnd          = i;

                        if (isotopeStart != 0)
                        {
                            isotopeEnd = i - 1;
                        }
                    }
                    else
                    {
                        // Lowercase letter continuing the current element.
                        symbolEnd = i;
                    }
                }
                else if (isotopeStart != 0 && isotopeEnd == 0)
                {
                    // In an isotope block looking for isotope numbers, do nothing
                }
                else if (formula[i] == '-')
                {
                    // Handle negative cardinalities
                    if (digitStart == 0)
                    {
                        digitStart = i;
                        digitEnd   = i;
                    }
                }
                else if (char.IsNumber(formula[i]))
                {
                    if (digitStart == 0)
                    {
                        digitStart = i;
                    }

                    digitEnd = i;
                }
                else if (char.IsWhiteSpace(formula[i]))
                {
                    // Ignore white space
                }
                else
                {
                    // Known character, fail!
                    return(false);
                }
            }

            // Add the last element
            if (!HandleNewElement(formula, elementProvider, elementList, symbolStart, symbolEnd, digitStart, digitEnd, isotopeStart, isotopeEnd))
            {
                return(false);
            }

            // Success!
            chemicalFormula = new ChemicalFormula(elementList.Values);
            return(true);
        }
Пример #10
0
 /// <summary>
 /// Generates the isotopic distribution.
 /// </summary>
 /// <param name="chemicalFormula">The chemical formula.</param>
 /// <param name="fineResolution">The fine resolution.</param>
 /// <param name="minProbability">The minimum probability.</param>
 /// <returns></returns>
 public IIsotopicDistribution GenerateIsotopicDistribution(IChemicalFormula chemicalFormula, double fineResolution, double minProbability)
 {
     return(this.GetDistribution(chemicalFormula, fineResolution, minProbability, defaultMolecularWeightResolution));
 }
Пример #11
0
 public MZSpectrum CalculateDistribuition(IChemicalFormula obj, int topNPeaks = int.MaxValue, Normalization normalization = Normalization.Sum)
 {
     return(CalculateDistribuition(obj.ChemicalFormula, topNPeaks, normalization));
 }
Пример #12
0
 private IChargedIsotopicDistribution Mercury(IChemicalFormula cf, int charge, double limit)
 {
     return(this.Mercury(cf, charge, charge, limit).Single());
 }
Пример #13
0
 /// <summary>
 /// Generates a list of charged isotopic distributions using the Mercury algorithm.
 /// </summary>
 /// <param name="chemicalFormula">The chemical formula.</param>
 /// <param name="firstCharge">The first charge.</param>
 /// <param name="lastCharge">The last charge.</param>
 /// <returns></returns>
 public IList <IChargedIsotopicDistribution> GenerateChargedIsotopicDistributions(IChemicalFormula chemicalFormula, int firstCharge, int lastCharge)
 {
     return(this.Mercury(chemicalFormula, firstCharge, lastCharge, this._limit));
 }
Пример #14
0
 /// <summary>
 /// Calculates the expected charged isotopic distribution for a given composition and charge.
 /// </summary>
 /// <param name="chemicalFormula">The chemical formula.</param>
 /// <param name="charge">The charge.</param>
 /// <returns></returns>
 public IChargedIsotopicDistribution GenerateChargedIsotopicDistribution(IChemicalFormula chemicalFormula, int charge)
 {
     return(this.Mercury(chemicalFormula, charge, this._limit));
 }
Пример #15
0
 /// <summary>
 /// Calculates the expected isotopic distribution for a given composition.
 /// </summary>
 /// <param name="chemicalFormula">The chemical formula.</param>
 /// <returns></returns>
 public IIsotopicDistribution GenerateIsotopicDistribution(IChemicalFormula chemicalFormula)
 {
     return(this.Mercury(chemicalFormula, this._limit));
 }
Пример #16
0
 public MZSpectrum CalculateDistribuition(IChemicalFormula obj, int topNPeaks = int.MaxValue, Normalization normalization = Normalization.Sum)
 {
     return CalculateDistribuition(obj.ChemicalFormula, topNPeaks, normalization);
 }
Пример #17
0
        private IIsotopicDistribution Mercury(IChemicalFormula cf, double limit)
        {
            // Build up the molecular super atom (MSA) until it is the entire molecule
            // A "molecular super atom" refers to a fictitious chemical compound whose
            //  formula is a partial composition of the target compound.
            double[]? msaMz        = null;
            double[]? msaAbundance = null;

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

            foreach (IEntityCardinality <IElement> kvp in cf.GetElements())
            {
                uint n = (uint)kvp.Count;

                if (n == 0)
                {
                    continue;
                }

                int      isotopeCount = kvp.Entity.Isotopes.Count;
                double[] esaMz        = new double[isotopeCount];
                double[] esaAbundance = new double[isotopeCount];

                int i = 0;
                foreach (var iso in kvp.Entity.Isotopes.OrderBy(x => x.AtomicMass)) // Algorithm requires it to be sorted.
                {
                    esaMz[i]        = iso.AtomicMass;
                    esaAbundance[i] = iso.RelativeAbundance;
                    i++;
                }

                while (true)
                {
                    // This is an implicit FFT that decomposes the number of a particular element
                    // into the sum of its powers of 2.
                    // Check if we need to do the MSA update - only if n is odd
                    if ((n & 1) == 1)
                    {
                        // MSA update
                        if (msaInitialized)
                        {
                            // normal update
                            Convolve(ref tmpMz, ref tmpAbundance, msaMz, msaAbundance, esaMz, esaAbundance);

                            msaMz        = this.CopyArray(tmpMz);
                            msaAbundance = this.CopyArray(tmpAbundance);
                        }
                        else
                        {
                            // for first assignment, MSA = ESA
                            msaMz          = this.CopyArray(esaMz);
                            msaAbundance   = this.CopyArray(esaAbundance);
                            msaInitialized = true;
                        }

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

                    // The ESA update is always carried out (with the exception of the last time, i.e., when n == 1)
                    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;
                }
            }

            if (msaMz != null && msaAbundance != null)
            {
                return(new IsotopicDistribution(msaMz, msaAbundance));
            }

            throw new Exception("Couldn't create masses or abundances.");
        }
Пример #18
0
 /// <summary>
 /// Create an chemical formula from an item that contains a chemical formula
 /// </summary>
 /// <param name="item">The item of which a new chemical formula will be made from</param>
 public ChemicalFormula(IChemicalFormula item)
     : this(item.ChemicalFormula)
 {
 }
Пример #19
0
 public FormulaModification(IChemicalFormula chemicalFormula)
 {
     _chemicalFormula = chemicalFormula;
 }