Exemplo n.º 1
0
        public bool CheckChargeState(ObservedIsotopeEnvelope envelope)
        {
            var checkCharge = envelope.Charge;

            if (checkCharge > 20)
            {
                return(true);                  //high charge (> +20), just pass
            }
            var peakStartIndex = envelope.MinMzPeak.IndexInSpectrum;
            var peakEndIndex   = envelope.MaxMzPeak.IndexInSpectrum;
            var nPeaks         = peakEndIndex - peakStartIndex + 1;

            if (nPeaks < 10)
            {
                return(false);
            }
            if (envelope.NumberOfPeaks > nPeaks * 0.7)
            {
                return(true);
            }

            var tolerance = new Tolerance(5);
            var threshold = nPeaks * 0.5;
            var mzTol     = tolerance.GetToleranceAsMz(Spectrum.Peaks[peakStartIndex].Mz);

            var minCheckCharge = Math.Max(checkCharge * 2 - 1, 4);
            var maxCheckCharge = Math.Min(checkCharge * 5 + 1, 60);
            var maxDeltaMz     = Constants.C13MinusC12 / minCheckCharge + mzTol;
            var nChargeGaps    = new int[maxCheckCharge - minCheckCharge + 1];

            for (var i = peakStartIndex; i <= peakEndIndex; i++)
            {
                for (var j = i + 1; j <= peakEndIndex; j++)
                {
                    var deltaMz = Spectrum.Peaks[j].Mz - Spectrum.Peaks[i].Mz;

                    if (deltaMz > maxDeltaMz)
                    {
                        break;
                    }
                    for (var c = Math.Round(1 / (deltaMz + mzTol)); c <= Math.Round(1 / (deltaMz - mzTol)); c++)
                    {
                        if (c < minCheckCharge || c > maxCheckCharge)
                        {
                            continue;
                        }
                        var k = (int)c - minCheckCharge;
                        nChargeGaps[k]++;

                        if (nChargeGaps[k] + 1 > threshold && nChargeGaps[k] + 1 > 1.25 * envelope.NumberOfPeaks)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public void UpdateWithDecoyScore(List <Ms1Spectrum> ms1Spectra, int targetMinCharge, int targetMaxCharge)
        {
            var ms1ScanNumToIndex = _run.GetMs1ScanNumToIndex();
            var ms1ScanNums       = _run.GetMs1ScanVector();
            var minCol            = ms1ScanNumToIndex[MinScanNum];
            var maxCol            = ms1ScanNumToIndex[MaxScanNum];

            MinCharge = targetMinCharge;
            MaxCharge = targetMaxCharge;

            var rnd                  = new Random();
            var comparer             = new MzComparerWithBinning(28);
            var mostAbuInternalIndex = TheoreticalEnvelope.IndexOrderByRanking[0];

            var nRows = MaxCharge - MinCharge + 1;
            var nCols = maxCol - minCol + 1;

            Envelopes = new ObservedIsotopeEnvelope[nRows][];
            for (var i = 0; i < nRows; i++)
            {
                Envelopes[i] = new ObservedIsotopeEnvelope[nCols];
            }

            for (var charge = targetMinCharge; charge <= targetMaxCharge; charge++)
            {
                var mostAbuMz = TheoreticalEnvelope.GetIsotopeMz(charge, mostAbuInternalIndex);
                if (_run.MaxMs1Mz < mostAbuMz || mostAbuMz < _run.MinMs1Mz)
                {
                    continue;
                }

                for (var col = minCol; col <= maxCol; col++)
                {
                    var localWin = ms1Spectra[col].GetLocalMzWindow(mostAbuMz);

                    var numMzBins = comparer.GetBinNumber(localWin.MaxMz) - comparer.GetBinNumber(localWin.MinMz) + 1;
                    var peakSet   = new Ms1Peak[TheoreticalEnvelope.Size];

                    for (var k = 0; k < peakSet.Length; k++)
                    {
                        var r = rnd.Next(0, numMzBins);
                        if (r < localWin.PeakCount)
                        {
                            peakSet[k] = (Ms1Peak)ms1Spectra[col].Peaks[r + localWin.PeakStartIndex];
                        }
                    }

                    var env = new ObservedIsotopeEnvelope(Mass, charge, ms1ScanNums[col], peakSet, TheoreticalEnvelope);
                    //AddObservedEnvelope(env);
                    Envelopes[charge - MinCharge][col - minCol] = env;
                }
            }
            UpdateScore(ms1Spectra, false);
        }
Exemplo n.º 3
0
 internal void Expand(ObservedIsotopeEnvelope envelope)
 {
     if (MaxScanNum < 0 || envelope.ScanNum > MaxScanNum)
     {
         MaxScanNum = envelope.ScanNum;
     }
     if (MinScanNum < 0 || envelope.ScanNum < MinScanNum)
     {
         MinScanNum = envelope.ScanNum;
     }
     if (MaxCharge < 0 || envelope.Charge > MaxCharge)
     {
         MaxCharge = envelope.Charge;
     }
     if (MinCharge < 0 || envelope.Charge < MinCharge)
     {
         MinCharge = envelope.Charge;
     }
 }
Exemplo n.º 4
0
        public void AddEnvelopes(int minCharge, int maxCharge, int minScanNum, int maxScanNum,
                                 IList <ObservedIsotopeEnvelope> envelopes = null)
        {
            var ms1ScanNumToIndex = _run.GetMs1ScanNumToIndex();
            var minCol            = ms1ScanNumToIndex[minScanNum];
            var maxCol            = ms1ScanNumToIndex[maxScanNum];

            var nRows = maxCharge - minCharge + 1;
            var nCols = maxCol - minCol + 1;

            MinCharge  = minCharge;
            MaxCharge  = maxCharge;
            MinScanNum = minScanNum;
            MaxScanNum = maxScanNum;

            Envelopes = new ObservedIsotopeEnvelope[nRows][];
            for (var i = 0; i < nRows; i++)
            {
                Envelopes[i] = new ObservedIsotopeEnvelope[nCols];
            }

            if (envelopes == null)
            {
                return;
            }

            foreach (var envelope in envelopes)
            {
                var i = envelope.Charge - MinCharge;
                var j = ms1ScanNumToIndex[envelope.ScanNum] - minCol;

                if (i < 0 || i >= nRows || j < 0 || j >= nCols)
                {
                    continue;
                }
                Envelopes[i][j] = envelope;
            }
        }
Exemplo n.º 5
0
        public IsotopeEnvelopeStatisticalInfo PreformStatisticalSignificanceTest(ObservedIsotopeEnvelope envelope)
        {
            int peakStartIndex;
            Tuple <double, double> mzBoundary;

            //var refPeak = envelope.Peaks[envelope.RefIsotopeInternalIndex];

            var mostAbuMz = 0d;
            var mostAbutPeakInternalIndex = envelope.TheoreticalEnvelope.IndexOrderByRanking[0];

            if (envelope.Peaks[mostAbutPeakInternalIndex] != null)
            {
                mostAbuMz = envelope.Peaks[mostAbutPeakInternalIndex].Mz;
            }
            else
            {
                mostAbuMz = envelope.TheoreticalEnvelope.GetIsotopeMz(envelope.Charge, mostAbutPeakInternalIndex);
            }

            var rankings = GetLocalRankings(mostAbuMz, out peakStartIndex, out mzBoundary);

            // smallest delta_mz = 0.01 (th) ?
            var ret = new IsotopeEnvelopeStatisticalInfo
            {
                LocalMzStart          = mzBoundary.Item1,
                LocalMzEnd            = mzBoundary.Item2,
                NumberOfLocalPeaks    = rankings.Length,
                NumberOfPossiblePeaks = (int)Math.Ceiling(100 * (mzBoundary.Item2 - mzBoundary.Item1)),
                NumberOfIsotopePeaks  = envelope.Size,
            };

            // calculate ranksum test score
            var ranksum  = 0;
            var nRankSum = 0;

            for (var i = 0; i < envelope.Size; i++)
            {
                if (envelope.Peaks[i] == null || !envelope.Peaks[i].Active)
                {
                    continue;
                }
                ret.NumberOfMatchedIsotopePeaks++;

                //if (isotopeList[i].Ratio > RelativeIntesnityThresholdForRankSum)
                //{
                var localIndex = envelope.Peaks[i].IndexInSpectrum - peakStartIndex;
                if (localIndex >= rankings.Length || localIndex < 0)
                {
                    continue;
                }
                ranksum += rankings[localIndex];
                nRankSum++;
                //}
            }

            var pvalue = FitScoreCalculator.GetRankSumPvalue(ret.NumberOfLocalPeaks, nRankSum, ranksum);

            ret.RankSumScore = (pvalue > 0) ? -Math.Log(pvalue, 2) : 50;

            // calculate poisson test score
            var n  = ret.NumberOfPossiblePeaks;
            var k  = ret.NumberOfIsotopePeaks;        // # of theretical isotope ions of the mass within the local window
            var n1 = ret.NumberOfLocalPeaks;          // # of detected ions within the local window
            var k1 = ret.NumberOfMatchedIsotopePeaks; // # of matched ions generating isotope envelope profile

            var lambda = ((double)n1 / (double)n) * k;

            pvalue           = 1 - Poisson.CDF(lambda, k1);
            ret.PoissonScore = (pvalue > 0) ? -Math.Log(pvalue, 2) : 50;
            return(ret);
        }
Exemplo n.º 6
0
        public void UpdateScore(List <Ms1Spectrum> ms1Spectra, bool pValueCheck = true)
        {
            var nRows             = MaxCharge - MinCharge + 1;
            var ms1ScanNumToIndex = _run.GetMs1ScanNumToIndex();
            var minCol            = ms1ScanNumToIndex[MinScanNum];
            var maxCol            = ms1ScanNumToIndex[MaxScanNum];
            var nCols             = maxCol - minCol + 1;
            var mostAbuIdx        = TheoreticalEnvelope.IndexOrderByRanking[0];

            ClearScore();

            var bestChargeDist = new double[] { 10.0d, 10.0d };
            // sum envelopes at each charge
            var summedIntensity = new double[TheoreticalEnvelope.Size];

            var xicLen      = nCols + 18;
            var xicStartIdx = 9;

            /*
             * if (nCols < 13)
             * {
             *  xicLen = 13;
             *  xicStartIdx = (int) Math.Floor((xicLen - nCols)*0.5);
             * }*/

            var xic2 = new double[2][];

            xic2[0] = new double[xicLen];
            xic2[1] = new double[xicLen];
            var chargeXic = new double[nRows][];

            var tempBestBcDist    = 10.0d;
            var repEnvelopeBcDist = 10.0d;
            ObservedIsotopeEnvelope repEnvelope = null;

            var repEnvelopeBcDist2 = 10.0d;
            ObservedIsotopeEnvelope repEnvelope2 = null;

            var tempBestDistanceScoreAcrossCharge = new double[2] {
                10, 10
            };
            var tempBestIntensityScoreAcrossCharge   = new double[2];
            var tempBestCorrelationScoreAcrossCharge = new double[2];

            for (var i = 0; i < nRows; i++)
            {
                var charge    = i + MinCharge;
                var mostAbuMz = TheoreticalEnvelope.GetIsotopeMz(charge, mostAbuIdx);
                Array.Clear(summedIntensity, 0, summedIntensity.Length);

                chargeXic[i] = new double[xicLen];

                var chargeIdx = (charge % 2 == 0) ? EvenCharge : OddCharge;
                var summedMostAbuIsotopeIntensity = 0d;
                var summedReferenceIntensity      = 0d;

                for (var j = 0; j < nCols; j++)
                {
                    var envelope = Envelopes[i][j];
                    var col      = minCol + j;

                    var localWin = ms1Spectra[col].GetLocalMzWindow(mostAbuMz);

                    if (envelope == null)
                    {
                        continue;
                    }

                    envelope.Peaks.SumEnvelopeTo(summedIntensity);
                    var mostAbuPeak = envelope.Peaks[mostAbuIdx];

                    if (mostAbuPeak != null && mostAbuPeak.Active)
                    {
                        summedMostAbuIsotopeIntensity += mostAbuPeak.Intensity;
                        summedReferenceIntensity      += localWin.HighestIntensity;
                    }
                    AbundanceDistributionAcrossCharge[chargeIdx] += envelope.Abundance;

                    var newBcDist = TheoreticalEnvelope.GetBhattacharyyaDistance(envelope.Peaks);
                    var newCorr   = TheoreticalEnvelope.GetPearsonCorrelation(envelope.Peaks);

                    var goodEnvelope = (newBcDist <0.07 || newCorr> 0.7);

                    if (goodEnvelope)
                    {
                        xic2[chargeIdx][xicStartIdx + j] += envelope.Abundance;
                        chargeXic[i][xicStartIdx + j]     = envelope.Abundance;
                    }

                    var levelOneEnvelope = true;
                    var levelTwoEnvelope = true;

                    if (pValueCheck)
                    {
                        var poissonPvalue = localWin.GetPoissonTestPvalue(envelope.Peaks, TheoreticalEnvelope.Size);
                        var rankSumPvalue = localWin.GetRankSumTestPvalue(envelope.Peaks, TheoreticalEnvelope.Size);
                        levelOneEnvelope = (rankSumPvalue < 0.01 && poissonPvalue < 0.01);
                        //levelTwoEnvelope = (rankSumPvalue < 0.05 || poissonPvalue < 0.05);
                    }

                    if (levelOneEnvelope)
                    {
                        if (newBcDist < BestDistanceScoreAcrossCharge[chargeIdx])
                        {
                            BestDistanceScoreAcrossCharge[chargeIdx] = newBcDist;
                            if (localWin.MedianIntensity > 0)
                            {
                                BestIntensityScoreAcrossCharge[chargeIdx] = envelope.HighestIntensity / localWin.HighestIntensity;
                            }
                            else
                            {
                                BestIntensityScoreAcrossCharge[chargeIdx] = 1.0d;
                            }
                        }

                        BestCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(BestCorrelationScoreAcrossCharge[chargeIdx], newCorr);

                        if (newBcDist < repEnvelopeBcDist)
                        {
                            repEnvelopeBcDist = newBcDist;
                            repEnvelope       = envelope;
                        }

                        // in the initial scoring, classify major and minor envelopes
                        if (!_initScore && goodEnvelope)
                        {
                            envelope.GoodEnough = true;
                        }
                    }

                    if (levelTwoEnvelope)
                    {
                        if (newBcDist < tempBestDistanceScoreAcrossCharge[chargeIdx])
                        {
                            tempBestDistanceScoreAcrossCharge[chargeIdx] = newBcDist;
                            if (localWin.MedianIntensity > 0)
                            {
                                tempBestIntensityScoreAcrossCharge[chargeIdx] = envelope.HighestIntensity / localWin.HighestIntensity;
                            }
                            else
                            {
                                tempBestIntensityScoreAcrossCharge[chargeIdx] = 1.0d;
                            }
                        }
                        tempBestCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(tempBestCorrelationScoreAcrossCharge[chargeIdx], newCorr);

                        if (newBcDist < repEnvelopeBcDist2)
                        {
                            repEnvelopeBcDist2 = newBcDist;
                            repEnvelope2       = envelope;
                        }
                    }
                }

                var bcDist = TheoreticalEnvelope.GetBhattacharyyaDistance(summedIntensity);
                EnvelopeDistanceScoreAcrossCharge[chargeIdx]    = Math.Min(bcDist, EnvelopeDistanceScoreAcrossCharge[chargeIdx]);
                EnvelopeCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(TheoreticalEnvelope.GetPearsonCorrelation(summedIntensity), EnvelopeCorrelationScoreAcrossCharge[chargeIdx]);

                if (BestCharge[chargeIdx] < 1 || bcDist < bestChargeDist[chargeIdx])
                {
                    BestCharge[chargeIdx]     = charge;
                    bestChargeDist[chargeIdx] = bcDist;
                    if (summedReferenceIntensity > 0)
                    {
                        EnvelopeIntensityScoreAcrossCharge[chargeIdx] = summedMostAbuIsotopeIntensity / summedReferenceIntensity;
                    }
                    //if (summedMedianIntensity > 0) EnvelopeIntensityScoreAcrossCharge[chargeIdx] = Math.Min(1.0, 0.1*(summedMostAbuIsotopeIntensity / summedMedianIntensity));
                }

                if (bcDist < tempBestBcDist)
                {
                    tempBestBcDist = bcDist;
                    Array.Copy(summedIntensity, RepresentativeSummedEnvelop, RepresentativeSummedEnvelop.Length);
                }
            }

            // when good envellope is observed at only either even or odd charge...
            if (BestCorrelationScoreAcrossCharge[0] > 0.7 && BestCorrelationScoreAcrossCharge[1] < 0.5)
            {
                const int i = 1;
                BestCorrelationScoreAcrossCharge[i] = tempBestCorrelationScoreAcrossCharge[i];
                BestIntensityScoreAcrossCharge[i]   = tempBestIntensityScoreAcrossCharge[i];
                BestDistanceScoreAcrossCharge[i]    = tempBestDistanceScoreAcrossCharge[i];
            }

            if (BestCorrelationScoreAcrossCharge[1] > 0.7 && BestCorrelationScoreAcrossCharge[0] < 0.5)
            {
                const int i = 0;
                BestCorrelationScoreAcrossCharge[i] = tempBestCorrelationScoreAcrossCharge[i];
                BestIntensityScoreAcrossCharge[i]   = tempBestIntensityScoreAcrossCharge[i];
                BestDistanceScoreAcrossCharge[i]    = tempBestDistanceScoreAcrossCharge[i];
            }

            // normalize abudnace across charges
            var s = AbundanceDistributionAcrossCharge[0] + AbundanceDistributionAcrossCharge[1];

            if (s > 0)
            {
                for (var chargeIdx = 0; chargeIdx < 2; chargeIdx++)
                {
                    AbundanceDistributionAcrossCharge[chargeIdx] = AbundanceDistributionAcrossCharge[chargeIdx] / s;
                }
            }

            if (nCols > 1)
            {
                var evenChargeIdx = BestCharge[EvenCharge] - MinCharge;
                var oddChargeIdx  = BestCharge[OddCharge] - MinCharge;
                XicCorrelationBetweenBestCharges[0] = FitScoreCalculator.GetPearsonCorrelation(Smoother.Smooth(chargeXic[evenChargeIdx]), Smoother.Smooth(chargeXic[oddChargeIdx]));
                XicCorrelationBetweenBestCharges[1] = FitScoreCalculator.GetPearsonCorrelation(Smoother.Smooth(xic2[EvenCharge]), Smoother.Smooth(xic2[OddCharge]));
            }

            if (repEnvelope == null && repEnvelope2 != null)
            {
                repEnvelope = repEnvelope2;
            }

            if (repEnvelope != null)
            {
                // set representative charge, mz and scanNum
                RepresentativeCharge  = repEnvelope.Charge;
                RepresentativeMz      = repEnvelope.RepresentativePeak.Mz;
                RepresentativeScanNum = repEnvelope.ScanNum;
            }

            _initScore = true;
        }
Exemplo n.º 7
0
 public LcMsPeakCluster(LcMsRun run, ObservedIsotopeEnvelope observedEnvelope)
     : this(run, observedEnvelope.TheoreticalEnvelope, observedEnvelope.MonoMass, observedEnvelope.Charge,
            observedEnvelope.RepresentativePeak.Mz, observedEnvelope.ScanNum, observedEnvelope.Abundance)
 {
 }