Ejemplo n.º 1
0
        public void TestMergeWith()
        {
            PeakList <Peak> pl1 = new PeakList <Peak>();

            pl1.Add(new Peak(420, 10, 1));
            pl1.Add(new Peak(445, 10, 1));

            PeakList <Peak> pl2 = new PeakList <Peak>();

            pl2.Add(new Peak(420.004, 30, 2));
            pl2.Add(new Peak(445.004, 30, 1));

            pl1.MergeByMZFirst(pl2, 50);

            Assert.AreEqual(3, pl1.Count);
            Assert.AreEqual(420, pl1[0].Mz, 0.001);
            Assert.AreEqual(1, pl1[0].Charge);
            Assert.AreEqual(420.004, pl1[1].Mz, 0.001);
            Assert.AreEqual(2, pl1[1].Charge);
            Assert.AreEqual(445.003, pl1[2].Mz, 0.001);
            Assert.AreEqual(1, pl1[2].Charge);

            Assert.AreEqual(10, pl1[0].Intensity);
            Assert.AreEqual(30, pl1[1].Intensity);
            Assert.AreEqual(40, pl1[2].Intensity);
        }
Ejemplo n.º 2
0
        public PeakList <T> FindPeak(double mz, int charge, double mzTolerance)
        {
            PeakList <T> result = NewSubPeakList();

            double minMz = mz - mzTolerance;
            double maxMz = mz + mzTolerance;

            foreach (var p in this)
            {
                if (p.Mz < minMz)
                {
                    continue;
                }

                if (p.Mz > maxMz)
                {
                    break;
                }

                if (p.Charge != charge)
                {
                    continue;
                }

                result.Add(p);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public PeakList <T> Filter(IFilter <T> filter)
        {
            PeakList <T> result = NewSubPeakList();

            foreach (T peak in this)
            {
                if (filter.Accept(peak))
                {
                    result.Add(peak);
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        public PeakList <T> _FindEnvelopeDirectly(List <Peak> profile, double mzTolerance, Func <T> allocator)
        {
            var result = new PeakList <T>();

            result.scanTimes.AddRange(this.scanTimes);

            foreach (var curMz in profile)
            {
                PeakList <T> find = FindPeak(curMz.Mz, mzTolerance);
                if (find.Count > 0)
                {
                    result.Add(find.FindMaxIntensityPeak());
                }
                else
                {
                    T p = allocator();
                    p.Mz        = curMz.Mz;
                    p.Intensity = 0.0;
                    result.Add(p);
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public PeakList <T> FindCharge(int charge)
        {
            if (0 == charge)
            {
                throw new ArgumentException("Charge cannot be zero.");
            }

            PeakList <T> result = NewSubPeakList();

            foreach (T peak in this)
            {
                if (charge == peak.Charge)
                {
                    result.Add(peak);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        public PeakList <T> GetRange(double minMz, double maxMz)
        {
            if (minMz > maxMz)
            {
                throw new ArgumentException(MyConvert.Format("minMz {0} should less than maxMz {1}", minMz, maxMz));
            }

            var result = new PeakList <T>();

            foreach (T peak in this)
            {
                if (peak.Mz < minMz)
                {
                    continue;
                }
                if (peak.Mz > maxMz)
                {
                    break;
                }
                result.Add(peak);
            }

            return(result);
        }
        public static PeakList <T> NormalizePeakIntensities <T>(PeakList <T> pkl, double maxPeakMass) where T : IPeak, new()
        {
            PeakList <T> result = new PeakList <T>();

            result.AssignInforamtion(pkl);
            foreach (var p in pkl)
            {
                result.Add(new T()
                {
                    Mz        = p.Mz,
                    Intensity = p.Intensity,
                    Charge    = p.Charge
                });
            }

            result.RemoveAll(m => m.Mz > maxPeakMass);

            int maxBins;

            if (maxPeakMass > 512)
            {
                maxBins = (int)Math.Ceiling(maxPeakMass / 1024) * 1024;
            }
            else
            {
                maxBins = 512;
            }

            // Section the original peak array in 10 regions and find the
            // base peak in each region. Also, square-root the peak intensities
            const int numberOfRegions = 10;

            double[] basePeakIntensityByRegion = new double[numberOfRegions];
            for (int i = 0; i < basePeakIntensityByRegion.Length; i++)
            {
                basePeakIntensityByRegion[i] = 1;
            }

            int regionSelector = (int)(maxPeakMass / numberOfRegions);

            foreach (var peak in result)
            {
                peak.Intensity = Math.Sqrt(peak.Intensity);

                int mzBin = (int)Math.Round(peak.Mz / binWidth);
                int normalizationIndex = mzBin / regionSelector;
                if (IsValidIndex(normalizationIndex, numberOfRegions))
                {
                    basePeakIntensityByRegion[normalizationIndex] = Math.Max(basePeakIntensityByRegion[normalizationIndex], peak.Intensity);
                }
            }

            // Normalize peaks in each region from 0 to 50.
            // Use base peak in each region for normalization.
            foreach (var peak in result)
            {
                int mzBin = (int)Math.Round(peak.Mz / binWidth);
                int normalizationIndex = mzBin / regionSelector;
                if (IsValidIndex(normalizationIndex, numberOfRegions))
                {
                    peak.Intensity = (peak.Intensity / basePeakIntensityByRegion[normalizationIndex]) * 50;
                }
            }

            return(result);
        }
Ejemplo n.º 8
0
        public void SetUp()
        {
            pl = new PeakList <Peak>();
            pl.Add(new Peak(430.0884, 43875.5, 1));

            pl.Add(new Peak(445.1205, 335180.5, 1));
            pl.Add(new Peak(446.1185, 51638.0, 1));
            pl.Add(new Peak(446.1255, 129918.6, 1));
            pl.Add(new Peak(447.1170, 30164.7, 1));

            pl.Add(new Peak(491.9578, 442529.3, 3));
            pl.Add(new Peak(492.2919, 206717.3, 3));
            pl.Add(new Peak(492.6270, 137434.5, 3));
            pl.Add(new Peak(492.9613, 26216.6, 3));

            pl.Add(new Peak(531.2642, 129351.8, 4));

            pl.Add(new Peak(631.2642, 129351.8, 0));
        }
Ejemplo n.º 9
0
        public PeakList <T> FindEnvelopeDirectly(List <double> profile, int maxProfile, double mzTolerance, Func <T> allocator)
        {
            var result = new PeakList <T>();

            result.scanTimes.AddRange(this.scanTimes);

            int start = SearchFirstLarger(profile[0] - mzTolerance);

            int iPro    = 0;
            int iThis   = start;
            int lenPro  = Math.Min(profile.Count, maxProfile);
            int lenThis = this.Count;

            if (iThis < lenThis)
            {
                var nMzTolerance = -mzTolerance;

                T curProPeak = default(T);
                while (true)
                {
                    double dis = this[iThis].Mz - profile[iPro];
                    if (dis < nMzTolerance)
                    {
                        iThis++;
                        if (iThis == lenThis)
                        {
                            break;
                        }

                        continue;
                    }

                    if (dis > mzTolerance)
                    {
                        if (curProPeak == null)
                        {
                            T p = allocator();
                            p.Mz        = profile[iPro];
                            p.Intensity = 0.0;
                            result.Add(p);
                        }
                        else
                        {
                            result.Add(curProPeak);
                        }

                        iPro++;
                        if (iPro == lenPro)
                        {
                            break;
                        }

                        curProPeak = default(T);
                        continue;
                    }

                    if (curProPeak == null || curProPeak.Intensity < this[iThis].Intensity)
                    {
                        curProPeak = this[iThis];
                    }

                    iThis++;
                    if (iThis == lenThis)
                    {
                        if (curProPeak == null)
                        {
                            T p = allocator();
                            p.Mz        = profile[iPro];
                            p.Intensity = 0.0;
                            result.Add(p);
                        }
                        else
                        {
                            result.Add(curProPeak);
                        }
                        break;
                    }
                }
            }

            for (int i = result.Count; i < profile.Count; i++)
            {
                T p = allocator();
                p.Mz        = profile[i];
                p.Intensity = 0.0;
                result.Add(p);
            }

            return(result);
        }
Ejemplo n.º 10
0
        public PeakList <T> FindEnvelope(double ionMass, int ionCharge, double mzTolerance, bool forwardOnly)
        {
            PeakList <T> result = NewSubPeakList();

            PeakList <T> curPeaks = null;
            int          charge   = ionCharge;

            if (0 != charge)
            {
                curPeaks = FindPeak(ionMass, charge, mzTolerance);
                if (0 == curPeaks.Count)
                {
                    charge = 0;
                }
            }

            if (0 == charge)
            {
                curPeaks = FindPeak(ionMass, mzTolerance);
            }

            if (curPeaks.Count == 0)
            {
                return(result);
            }
            else
            {
                result.Add(curPeaks.FindMaxIntensityPeak());

                //Console.WriteLine("1 : {0:0.0000}", result[0].Mz - peak.Mz);
            }

            if (0 == ionCharge)
            {
                return(result);
            }

            var gapMz        = ChargeDeconvolution.C_GAP / ionCharge;
            int highestIndex = 0;

            double gapTolerance = 0.0001;

            bool bCalculateGap = true;

            int count = 1;

            if (!forwardOnly)
            {
                //backward
                while (true)
                {
                    double expectMz = result[highestIndex].Mz - count * gapMz;
                    count++;

                    PeakList <T> findPeaks = 0 == charge?FindPeak(expectMz, mzTolerance) : FindPeak(expectMz, charge, mzTolerance);

                    if (findPeaks.Count > 0)
                    {
                        var curPeak = findPeaks.FindMaxIntensityPeak();
                        result.Insert(0, curPeak);
                        highestIndex++;

                        if (curPeak.Intensity > result[highestIndex].Intensity && 1 == highestIndex)
                        {
                            highestIndex  = 0;
                            count         = 1;
                            bCalculateGap = true;
                        }

                        if (bCalculateGap)
                        {
                            var newGap = GetGap(result, highestIndex);
                            if (Math.Abs(newGap - gapMz) < gapTolerance)
                            {
                                bCalculateGap = false;
                            }
                            gapMz = newGap;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //forward
            count         = result.Count - highestIndex;
            bCalculateGap = true;
            while (true)
            {
                double expectMz = result[highestIndex].Mz + gapMz * count;
                count++;

                PeakList <T> findPeaks = 0 == charge?FindPeak(expectMz, mzTolerance) : FindPeak(expectMz, charge, mzTolerance);

                if (findPeaks.Count > 0)
                {
                    var curPeak = findPeaks.FindMaxIntensityPeak();
                    result.Add(curPeak);

                    if (curPeak.Intensity > result[highestIndex].Intensity && 2 == count)
                    {
                        highestIndex  = result.Count - 1;
                        count         = 1;
                        bCalculateGap = true;
                    }

                    if (bCalculateGap)
                    {
                        var newGap = GetGap(result, highestIndex);
                        if (Math.Abs(newGap - gapMz) < gapTolerance)
                        {
                            bCalculateGap = false;
                        }
                        gapMz = newGap;
                    }

                    //Console.WriteLine("{0} : {1:0.0000} : {2:0.0000}", result.Count, gapMz, result[result.Count - 1].Mz - expectMz);
                }
                else
                {
                    break;
                }
            }

            //Console.WriteLine("{0}\t{1:0.0000}\t{2}\t{3:0.000000}\t{4}", result.FirstScan, peak.Mz, peak.Charge, gapMz * peak.Charge, result.Count);

            return(result);
        }