예제 #1
0
 private void AddDistinctFormula(IsotopeContainer container, IMolecularFormula mf)
 {
     foreach (var curr in container.Formulas)
     {
         if (MolecularFormulaManipulator.Compare(curr, mf))
         {
             return;
         }
     }
     container.AddFormula(mf);
 }
예제 #2
0
        /// <summary>
        /// Calculates the mass and abundance of all isotopes generated by adding one
        /// atom. Receives the periodic table element and calculate the isotopes, if
        /// there exist a previous calculation, add these new isotopes. In the
        /// process of adding the new isotopes, remove those that has an abundance
        /// less than setup parameter minIntensity, and remove duplicated masses.
        /// </summary>
        /// <param name="additional">additional isotopes to 'multiple' the current pattern by</param>
        /// <returns>the calculation was successful</returns>
        private IsotopePattern CalculateAbundanceAndMass(IsotopePattern current, List <IsotopeContainer> additional)
        {
            if (additional == null || additional.Count == 0)
            {
                return(current);
            }

            var containers = new List <IsotopeContainer>();

            // Verify if there is a previous calculation. If it exists, add the new
            // isotopes
            if (current == null)
            {
                current = new IsotopePattern();
                foreach (var container in additional)
                {
                    current.isotopes.Add(container);
                }
            }
            else
            {
                foreach (var container in current.Isotopes)
                {
                    foreach (IsotopeContainer other in additional)
                    {
                        var abundance = container.Intensity * other.Intensity * 0.01;
                        var mass      = container.Mass + other.Mass;

                        // merge duplicates with some resolution
                        var existing = FindExisting(containers, mass, resolution);
                        if (existing != null)
                        {
                            var newIntensity = existing.Intensity + abundance;
                            // moving weighted avg
                            existing.Mass = (existing.Mass * existing.Intensity +
                                             mass * abundance) / newIntensity;
                            existing.Intensity = newIntensity;
                            if (storeFormula)
                            {
                                foreach (var mf in container.Formulas)
                                {
                                    AddDistinctFormula(existing, Union(mf, other.Formula));
                                }
                            }
                            continue;
                        }

                        // Filter isotopes too small
                        if (abundance > minAbundance)
                        {
                            var newcontainer = new IsotopeContainer(mass, abundance);
                            if (storeFormula)
                            {
                                foreach (var mf in container.Formulas)
                                {
                                    newcontainer.AddFormula(Union(mf, other.Formula));
                                }
                            }
                            containers.Add(newcontainer);
                        }
                    }
                }

                current = new IsotopePattern();
                foreach (var container in containers)
                {
                    current.isotopes.Add(container);
                }
            }
            return(current);
        }