예제 #1
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);
        }
        /// <summary>
        /// Calculate XCorr based on implementation in MyriMatch
        /// </summary>
        /// <param name="pkl1">Normalized peak list 1</param>
        /// <param name="pkl2">Normalized peak list 2</param>
        /// <returns></returns>
        public static double Calculate <T>(PeakList <T> pkl1, PeakList <T> pkl2) where T : IPeak, new()
        {
            double massCutOff = Math.Max(pkl1.Last().Mz, pkl2.Last().Mz) + 50;

            int maxBins = massCutOff > 512 ? (int)Math.Ceiling(massCutOff / 1024) * 1024 : 512;

            double[] data1 = GetIntensityArray(pkl1, maxBins);

            //Compute the cumulative spectrum
            for (int i = 0; i < data1.Length; i++)
            {
                for (int j = i - 75; j <= i + 75; j++)
                {
                    if (IsValidIndex(j, maxBins))
                    {
                        data1[i] -= data1[j] / 151;
                    }
                }
            }

            double[] data2 = GetIntensityArray(pkl2, maxBins);

            double result = 0.0;

            for (int index = 0; index < data1.Length; index++)
            {
                result += data1[index] * data2[index];
            }

            result = result / 10000;

            return(result);
        }
        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);
        }
예제 #4
0
        private PeakList <T> NewSubPeakList()
        {
            var result = new PeakList <T>();

            result.AssignInforamtion(this);
            return(result);
        }
        public void TestFindPeak()
        {
            PeakList <Peak> findPeak = pl.FindPeak(446.12, 0.1);

            Assert.AreEqual(2, findPeak.Count);
            Assert.AreSame(pl[2], findPeak[0]);
            Assert.AreSame(pl[3], findPeak[1]);
        }
        /// <summary>
        /// Calculate XCorr based on implementation in MyriMatch
        /// </summary>
        /// <param name="pkl1">Normalized peak list 1</param>
        /// <param name="pkl2">Normalized peak list 2</param>
        /// <returns></returns>
        public static double CalculateUnnormalized <T>(PeakList <T> pkl1, PeakList <T> pkl2) where T : IPeak, new()
        {
            var maxPeakMass = Math.Max(pkl1.Last().Mz, pkl2.Last().Mz);
            var n1          = NormalizePeakIntensities(pkl1, maxPeakMass);
            var n2          = NormalizePeakIntensities(pkl2, maxPeakMass);

            return(Calculate(n1, n2));
        }
예제 #7
0
 public void AssignInforamtion <V>(PeakList <V> source) where V : IPeak
 {
     this.MsLevel      = source.MsLevel;
     this.Precursor    = new PrecursorPeak(source.Precursor);
     this.Experimental = source.Experimental;
     this.scanTimes.Clear();
     this.scanTimes.AddRange(source.scanTimes);
 }
예제 #8
0
 public PeakList(PeakList <T> source)
     : this()
 {
     AssignInforamtion <T>(source);
     AddRange(source);
     foreach (String key in source.annotations.Keys)
     {
         this.annotations.Add(key, source.annotations[key]);
     }
 }
예제 #9
0
        /**
         * For each peak in this peaklist, only the max intensity peak within mzTolerance in argument peakList
         * will be merged. And, the mz, charge will not be changed, intensity will be added.
         */

        public void AddToCurrentPeakListIntensity(PeakList <T> peakList, double mzTolerance)
        {
            foreach (T peak in this)
            {
                PeakList <T> find = peakList.FindPeak(peak.Mz, mzTolerance);
                if (find.Count > 0)
                {
                    T maxIntensity = find.FindMaxIntensityPeak();
                    peak.Intensity = peak.Intensity + maxIntensity.Intensity;
                }
            }
        }
예제 #10
0
        public void TestGetRange()
        {
            double          min        = 440;
            double          max        = 450;
            PeakList <Peak> rangePeaks = pl.GetRange(min, max);

            Assert.AreEqual(4, rangePeaks.Count);
            foreach (Peak p in rangePeaks)
            {
                Assert.Greater(p.Mz, min);
                Assert.Less(p.Mz, max);
            }
        }
 private static double[] GetIntensityArray <T>(PeakList <T> pkl1, int maxBins) where T : IPeak, new()
 {
     double[] data1 = new double[maxBins];
     foreach (var peak in pkl1)
     {
         int mzBin = (int)Math.Round(peak.Mz / binWidth);
         if (IsValidIndex(mzBin, maxBins))
         {
             data1[mzBin] = peak.Intensity;
         }
     }
     return(data1);
 }
예제 #12
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);
        }
예제 #13
0
        public void TestFindEnvelopeDirectly()
        {
            var e = new Envelope(444.12, 1, 4).ToList().ConvertAll(m => new Peak()
            {
                Mz = m
            }).ToList();
            PeakList <Peak> envolopCharge1 = pl.FindEnvelopeDirectly(e, 0.02, () => new Peak());

            Assert.AreEqual(4, envolopCharge1.Count);
            Assert.AreEqual(0, envolopCharge1[0].Intensity);
            Assert.AreSame(pl[1], envolopCharge1[1]);
            Assert.AreSame(pl[3], envolopCharge1[2]);
            Assert.AreSame(pl[4], envolopCharge1[3]);
        }
예제 #14
0
        public bool IsSameScan(PeakList <T> another)
        {
            if (!this.Experimental.Equals(another.Experimental))
            {
                return(false);
            }

            ScanTime firstThis    = GetFirstScanTime();
            ScanTime firstAnother = another.GetFirstScanTime();
            ScanTime lastThis     = GetLastScanTime();
            ScanTime lastAnother  = another.GetLastScanTime();

            return(firstThis.Scan != 0 && firstThis.Scan == firstAnother.Scan &&
                   lastThis.Scan == lastAnother.Scan);
        }
예제 #15
0
        public PeakList <Peak> Process(PeakList <Peak> t)
        {
            if (t.Count == 0)
            {
                return(t);
            }

            t.SortByMz();

            List <Peak> final = new List <Peak>();

            List <Peak> kept = new List <Peak>();

            double minMz  = 0;
            double lastMz = t.Last().Mz;

            int index = 0;

            while (index < t.Count)
            {
                var maxMz = minMz + 100;
                kept.Clear();
                while (index < t.Count)
                {
                    if (t[index].Mz > maxMz)
                    {
                        break;
                    }

                    kept.Add(t[index]);
                    index++;
                }

                if (kept.Count > count)
                {
                    kept.Sort((m1, m2) => m2.Intensity.CompareTo(m1.Intensity));
                    kept.RemoveRange(count, kept.Count - count);
                }

                final.AddRange(kept);
                minMz += 100;
            }

            t.Clear();
            t.AddRange(final);

            return(t);
        }
예제 #16
0
        public void Deduct <T>(PeakList <T> pkl) where T : Peak
        {
            pkl.SortByMz();

            List <T> currPkl = new List <T>();

            currPkl.Add(pkl[0]);
            int end = 1;

            HashSet <T> deletePkl = new HashSet <T>();

            while (true)
            {
                while (end < pkl.Count && pkl[end].Mz - currPkl[0].Mz < WINDOW_SIZE)
                {
                    currPkl.Add(pkl[end]);
                    end++;
                }

                if (currPkl.Count <= maxCount)
                {
                    if (end == pkl.Count)
                    {
                        break;
                    }

                    currPkl.RemoveAt(0);
                }
                else
                {
                    List <T> tmp      = new List <T>(currPkl);
                    var      lastPeak = currPkl[0];
                    tmp.Sort((m1, m2) => - m1.Intensity.CompareTo(m2.Intensity));
                    for (int i = maxCount; i < tmp.Count; i++)
                    {
                        deletePkl.Add(tmp[i]);
                        currPkl.Remove(tmp[i]);
                    }

                    if (currPkl[0] == lastPeak)
                    {
                        currPkl.RemoveAt(0);
                    }
                }
            }

            pkl.RemoveAll(m => deletePkl.Contains(m));
        }
예제 #17
0
        public Dictionary <int, PeakList <T> > GetChargeMap()
        {
            var result = new Dictionary <int, PeakList <T> >();

            foreach (T peak in this)
            {
                if (!result.ContainsKey(peak.Charge))
                {
                    PeakList <T> pkl = NewSubPeakList();
                    result.Add(peak.Charge, pkl);
                }

                result[peak.Charge].Add(peak);
            }
            return(result);
        }
예제 #18
0
        public void TestFindEnvelop()
        {
            PeakList <Peak> envolopCharge1 = pl.FindEnvelope(pl[2], 0.02, false);

            Assert.AreEqual(3, envolopCharge1.Count);
            Assert.AreSame(pl[1], envolopCharge1[0]);
            Assert.AreSame(pl[3], envolopCharge1[1]);
            Assert.AreSame(pl[4], envolopCharge1[2]);

            PeakList <Peak> envolopCharge3 = pl.FindEnvelope(pl[8], 0.02, false);

            Assert.AreEqual(4, envolopCharge3.Count);
            Assert.AreSame(pl[5], envolopCharge3[0]);
            Assert.AreSame(pl[6], envolopCharge3[1]);
            Assert.AreSame(pl[7], envolopCharge3[2]);
            Assert.AreSame(pl[8], envolopCharge3[3]);
        }
예제 #19
0
        //Find peak list constains max intensity peak whose mz is equals to assigned peak and charge is zero or equals to assigned peak
        public static PeakList <T> FindEnvelopeInList(List <PeakList <T> > list, T peak, double mzTolerance)
        {
            PeakList <T> result = null;

            double minMz = peak.Mz - mzTolerance;
            double maxMz = peak.Mz + mzTolerance;

            foreach (var pkl in list)
            {
                if (pkl[pkl.Count - 1].Mz < minMz)
                {
                    continue;
                }

                if (pkl[0].Mz > maxMz)
                {
                    break;
                }

                PeakList <T> peaks = pkl.FindPeak(peak.Mz, mzTolerance);
                if (0 == peaks.Count)
                {
                    continue;
                }

                if (0 != pkl[0].Charge && peak.Charge != pkl[0].Charge)
                {
                    continue;
                }

                if (null == result)
                {
                    result = pkl;
                    continue;
                }

                if (result.FindMaxIntensityPeak().Intensity < pkl.FindMaxIntensityPeak().Intensity)
                {
                    result = pkl;
                    continue;
                }
            }

            return(result);
        }
예제 #20
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);
        }
예제 #21
0
        private double GetGap(PeakList <T> pkl, int highestIndex)
        {
            double totalDistance = 0.0;
            double totalWeight   = 0.0;

            for (int i = 0; i < pkl.Count; i++)
            {
                if (i == highestIndex)
                {
                    continue;
                }

                var gapCount = Math.Abs(i - highestIndex);
                var weight   = 1 / gapCount;
                totalDistance += Math.Abs(pkl[i].Mz - pkl[highestIndex].Mz) / gapCount * weight;
                totalWeight   += weight;
            }
            return(totalDistance / totalWeight);
        }
예제 #22
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));
        }
예제 #23
0
        public void TestFindEnvelopeInList()
        {
            List <PeakList <Peak> > envelopes = pl.GetEnvelopes(20);
            Peak            peak        = new Peak(446.1185, 51638.0, 1);
            double          mzTolerance = PrecursorUtils.ppm2mz(peak.Mz, 20);
            PeakList <Peak> actual      = PeakList <Peak> .FindEnvelopeInList(envelopes, peak, mzTolerance);

            Assert.AreSame(envelopes[1], actual);

            Peak            peak2        = new Peak(631.2642, 129351.8, 1);
            double          mzTolerance2 = PrecursorUtils.ppm2mz(peak2.Mz, 20);
            PeakList <Peak> actual2      = PeakList <Peak> .FindEnvelopeInList(envelopes, peak2, mzTolerance2);

            Assert.AreSame(envelopes[4], actual2);

            Peak            missPeak        = new Peak(731.2642, 129351.8, 1);
            double          missMzTolerance = PrecursorUtils.ppm2mz(missPeak.Mz, 20);
            PeakList <Peak> actualMiss      = PeakList <Peak> .FindEnvelopeInList(envelopes, missPeak, missMzTolerance);

            Assert.AreSame(null, actualMiss);
        }
예제 #24
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);
        }
예제 #25
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);
        }
예제 #26
0
        public int Compare(PeakList <T> x, PeakList <T> y)
        {
            if (0 == x.Count)
            {
                return(-1);
            }

            if (0 == y.Count)
            {
                return(1);
            }

            if (x[0].Mz < y[0].Mz)
            {
                return(-1);
            }

            if (x[0].Mz > y[0].Mz)
            {
                return(1);
            }

            return(0);
        }
        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);
        }
예제 #28
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);
        }
예제 #29
0
        public List <PeakList <T> > GetEnvelopes(double ppmTolerance)
        {
            var tmp = new PeakList <T>(this);

            var result = new List <PeakList <T> >();

            while (tmp.Count > 0)
            {
                double       mzTolerance = 2 * PrecursorUtils.ppm2mz(tmp[0].Mz, ppmTolerance);
                PeakList <T> envelope    = tmp.FindEnvelope(tmp[0], mzTolerance, true);
                result.Add(envelope);

                foreach (T peak in envelope)
                {
                    //Remove all peaks around current peak
                    PeakList <T> findPeaks = tmp.FindPeak(peak.Mz, peak.Charge, mzTolerance);
                    foreach (T findPeak in findPeaks)
                    {
                        tmp.Remove(findPeak);
                    }
                }
            }

            result.Sort(new PeakListMzAscendingComparer <T>());

            int current = 0;

            while (current < result.Count - 1)
            {
                PeakList <T> currentPkl = result[current];
                if (currentPkl[0].Charge > 0)
                {
                    double expectMz    = currentPkl[currentPkl.Count - 1].Mz + ChargeDeconvolution.C_GAP / currentPkl[0].Charge;
                    double mzTolerance = 4 * PrecursorUtils.ppm2mz(currentPkl[0].Mz, ppmTolerance);
                    int    next        = current + 1;
                    while (next < result.Count)
                    {
                        double gap = result[next][0].Mz - expectMz;
                        if (gap >= 2.0)
                        {
                            break;
                        }

                        if (Math.Abs(gap) > mzTolerance)
                        {
                            next++;
                            continue;
                        }

                        if (result[next][0].Charge != currentPkl[0].Charge)
                        {
                            next++;
                            continue;
                        }


                        currentPkl.AddRange(result[next]);

                        result.RemoveAt(next);
                    }
                }

                current++;
            }

            return(result);
        }
예제 #30
0
        public void MergeByMZFirst(PeakList <T> other, double ppmTolerance)
        {
            if (0 == other.Count)
            {
                return;
            }

            if (0 == Count)
            {
                AddRange(other);
                return;
            }

            Sort(new PeakMzAscendingComparer <T>());
            other.Sort(new PeakMzAscendingComparer <T>());

            var result      = new List <T>();
            int iThisIndex  = 0;
            int iOtherIndex = 0;

            while (iThisIndex < Count && iOtherIndex < other.Count)
            {
                double mzThisTolerance = PrecursorUtils.ppm2mz(this[0].Mz, ppmTolerance);
                double gap             = this[iThisIndex].Mz - other[iOtherIndex].Mz;
                if (Math.Abs(gap) < mzThisTolerance)
                {
                    if (this[iThisIndex].Charge == other[iOtherIndex].Charge ||
                        0 == this[iThisIndex].Charge ||
                        0 == other[iOtherIndex].Charge)
                    {
                        double mz = (this[iThisIndex].Mz * this[iThisIndex].Intensity +
                                     other[iOtherIndex].Mz * other[iOtherIndex].Intensity)
                                    / (this[iThisIndex].Intensity + other[iOtherIndex].Intensity);

                        this[iThisIndex].Mz        = mz;
                        this[iThisIndex].Intensity = this[iThisIndex].Intensity + other[iOtherIndex].Intensity;
                        this[iThisIndex].Charge    = Math.Max(this[iThisIndex].Charge, other[iOtherIndex].Charge);
                        iOtherIndex++;
                        continue;
                    }
                }

                if (gap < 0)
                {
                    result.Add(this[iThisIndex]);
                    iThisIndex++;
                }
                else
                {
                    result.Add(other[iOtherIndex]);
                    iOtherIndex++;
                }
            }

            while (iThisIndex < Count)
            {
                result.Add(this[iThisIndex]);
                iThisIndex++;
            }

            while (iOtherIndex < other.Count)
            {
                result.Add(other[iOtherIndex]);
                iOtherIndex++;
            }

            base.Clear();
            base.AddRange(result);
            this.scanTimes.AddRange(other.scanTimes);
            this.PrecursorIntensity += other.PrecursorIntensity;
        }