Example #1
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);
        }
Example #2
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;
        }