Example #1
0
 public Helper(MassDistribution parent)
 {
     Masses           = parent.Select(keyValuePair => new MassFrequency(keyValuePair.Key, keyValuePair.Value)).ToArray();
     MassCount        = Masses.Length;
     MassResolution   = parent.MassResolution;
     MinimumAbundance = parent.MinimumAbundance;
 }
Example #2
0
        public MassDistribution GetMassDistribution(Molecule molecule, int charge)
        {
            var md     = new MassDistribution(MassResolution, .00001);
            var result = md;

            foreach (var element in molecule)
            {
                result = result.Add(md.Add(IsotopeAbundances[element.Key]).Multiply(element.Value));
            }
            if (charge != 0)
            {
                result = result.OffsetAndDivide(charge * ProtonMass, charge);
            }
            return(result);
        }
        /* If this is brought into use, it must be made aware of non-protonated ions
         * public MassDistribution SetCharge(int charge)
         * {
         *  return OffsetAndDivide(AminoAcidFormulas.ProtonMass*charge, charge);
         * }
         */

        /// <summary>
        /// Performs a weighted average of this MassDistribution with another.
        /// </summary>
        public MassDistribution Average(MassDistribution massDistribution, double weight)
        {
            var dict = new Dictionary <double, double>();

            foreach (var entry in this)
            {
                dict.Add(entry.Key, entry.Value * (1.0 - weight));
            }
            foreach (var entry in massDistribution)
            {
                double currentAbundance;
                dict.TryGetValue(entry.Key, out currentAbundance);
                dict[entry.Key] = currentAbundance + entry.Value * weight;
            }
            return(NewInstance(dict, MassResolution, MinimumAbundance));
        }
        /// <summary>
        /// Combines this MassDistribution with another MassDistribution.
        /// The combined MassDistribution is the result of taking each entry in this
        /// MassDistribution and adding the mass to each entry in the second MassDistribution,
        /// and multiplying the probability with the second MassDistribution.
        /// Afterwards, the result is trimmed down using the MassResolution and Minimum abundance.
        /// </summary>
        public MassDistribution Add(MassDistribution rhs)
        {
            var map = new Dictionary <Double, Double>();

            foreach (var thisEntry in this)
            {
                foreach (var thatEntry in rhs)
                {
                    double mass = thisEntry.Key + thatEntry.Key;
                    double frequency;
                    map.TryGetValue(mass, out frequency);
                    frequency += thisEntry.Value * thatEntry.Value;
                    map[mass]  = frequency;
                }
            }
            return(NewInstance(map, MassResolution, MinimumAbundance));
        }
        /// <summary>
        /// Returns the result of adding this MassDistribution to itself the specified number of times.
        /// </summary>
        public MassDistribution Multiply(int factor)
        {
            if (factor == 0)
            {
                return(new MassDistribution(MassResolution, MinimumAbundance));
            }
            if (factor == 1)
            {
                return(this);
            }
            MassDistribution result = Add(this);

            if (factor >= 4)
            {
                result = result.Multiply(factor / 2);
            }
            if (factor % 2 != 0)
            {
                result = result.Add(this);
            }
            return(result);
        }
Example #6
0
        private Tuple <KeyValuePair <double, double>[], int> PartialAdd(int start, int end, MassDistribution rhs)
        {
            if (end == start + 1)
            {
                var myMass      = _masses[start];
                var myFrequency = _frequencies[start];
                var array       = new KeyValuePair <double, double> [rhs.Count];
                for (int i = 0; i < rhs.Count; i++)
                {
                    array[i] = new KeyValuePair <double, double>(myMass + rhs._masses[i], myFrequency * rhs._frequencies[i]);
                }
                return(Tuple.Create(array, array.Length));
            }
            var mid   = (start + end) / 2;
            var left  = PartialAdd(start, mid, rhs);
            var right = PartialAdd(mid, end, rhs);

            return(Merge(left, right));
        }
Example #7
0
        /// <summary>
        /// Combines this MassDistribution with another MassDistribution.
        /// The combined MassDistribution is the result of taking each entry in this
        /// MassDistribution and adding the mass to each entry in the second MassDistribution,
        /// and multiplying the probability with the second MassDistribution.
        /// Afterwards, the result is trimmed down using the MassResolution and Minimum abundance.
        /// </summary>
        public MassDistribution Add(MassDistribution rhs)
        {
            var arrayCount = PartialAdd(0, Count, rhs);

            return(ApplyBinning(arrayCount.Item1, arrayCount.Item2));
        }
Example #8
0
        /// <summary>
        /// Combines this MassDistribution with another MassDistribution.
        /// The combined MassDistribution is the result of taking each entry in this
        /// MassDistribution and adding the mass to each entry in the second MassDistribution,
        /// and multiplying the probability with the second MassDistribution.
        /// Afterwards, the result is trimmed down using the MassResolution and Minimum abundance.
        /// </summary>
        public MassDistribution Add(MassDistribution rhs)
        {
            var helper = new Helper(this).Add(rhs);

            return(new MassDistribution(helper));
        }