Exemple #1
0
        /// <summary>
        /// generates a fake deconvolution solution for the given test spectrum
        /// in order to make sure that the deconvolution results are being applied
        /// correctly to scale peaks in the test spectrum
        /// </summary>
        private static void TestPeakIntensityCorrection(MsDataSpectrum testSpectrum,
                                                        AbstractIsoWindowMapper isoMapper,
                                                        AbstractDemultiplexer demultiplexer)
        {
            // Store a reference to the old spectrum processor
            var oldProcessor          = demultiplexer.SpectrumProcessor;
            MsxDeconvSolverHandler db = new MsxDeconvSolverHandler(100, 120, 35);

            int[]    isoIndices;
            int[]    deconvIndices;
            double[] mask = new double[100];
            isoMapper.GetWindowMask(testSpectrum, out isoIndices, out deconvIndices, ref mask);
            db.SetDeconvIndices(deconvIndices);
            Assert.IsTrue(deconvIndices.Contains(50));
            Assert.IsTrue(deconvIndices.Contains(47));
            Assert.IsTrue(deconvIndices.Contains(15));
            Assert.IsTrue(deconvIndices.Contains(17));
            Assert.IsTrue(deconvIndices.Contains(74));
            Assert.AreEqual(5, deconvIndices.Length);

            // Prepare the transition binner with a precursor containing transitions overlapping
            // peaks in the test spectrum
            List <double> precursors = new List <double>();
            List <KeyValuePair <double, double> > transitions = new List <KeyValuePair <double, double> >();
            var precVals    = new[] { 710.57 };
            var transMzs    = new[] { 372.18, 373.1588, 373.1794, 375.2, 379.23, 387.19 };
            var transWidths = new[] { 0.3, 0.04, 0.02, 1.0, 1.0, 1.0 };

            foreach (var precMz in precVals)
            {
                for (int i = 0; i < isoIndices.Length; ++i)
                {
                    var transMz    = transMzs[i];
                    var transWidth = transWidths[i];
                    precursors.Add(precMz);
                    transitions.Add(new KeyValuePair <double, double>(transMz, transWidth));
                }
            }

            var transBinner   = new TransitionBinner(precursors, transitions, isoMapper);
            var binIndicesSet = new HashSet <int>(transBinner.BinsForDeconvWindows(deconvIndices));

            double[][] deconvIntensities = new double[deconvIndices.Length][];
            double[][] deconvMzs         = new double[deconvIndices.Length][];

            for (int i = 0; i < deconvIndices.Length; ++i)
            {
                deconvIntensities[i] = new double[testSpectrum.Mzs.Length];
                deconvMzs[i]         = new double[testSpectrum.Mzs.Length];
            }

            List <int> binIndicesList = binIndicesSet.ToList();

            binIndicesList.Sort();

            // Use each transition bin index to test a different possible
            // type of demultiplexed solution
            int numDeMultiplexedTrans = binIndicesSet.Count;

            double[] peakSums = new double[numDeMultiplexedTrans];
            // Initialize the deconvBlock
            db.Solution.Resize(100, binIndicesList.Count);
            db.Solution.Clear();
            // Transition index 0, each row is a precursor
            // the values for each precursor are the relative contribution
            // of that precursor to the observed (convolved) intensity of the peak
            db.Solution.Matrix[2, 0] = 0.0;
            db.Solution.Matrix[1, 0] = 0.0;
            db.Solution.Matrix[0, 0] = 0.0;
            db.Solution.Matrix[3, 0] = 0.0;
            db.Solution.Matrix[4, 0] = 0.0;
            peakSums[0] = 0.0;
            // Transition index 1, each row is a precursor
            db.Solution.Matrix[2, 1] = 1.0;
            db.Solution.Matrix[1, 1] = 1.0;
            db.Solution.Matrix[0, 1] = 2.0;
            db.Solution.Matrix[3, 1] = 1.0;
            db.Solution.Matrix[4, 1] = 1.0;
            peakSums[1] = 6.0;
            // Transition index 2, each row is a precursor
            db.Solution.Matrix[2, 2] = 2.0;
            db.Solution.Matrix[1, 2] = 1.0;
            db.Solution.Matrix[0, 2] = 3.0;
            db.Solution.Matrix[3, 2] = 3.0;
            db.Solution.Matrix[4, 2] = 1.0;
            peakSums[2] = 10.0;
            // Transition index 3, each row is a precursor
            db.Solution.Matrix[2, 3] = 0.0;
            db.Solution.Matrix[1, 3] = 0.0;
            db.Solution.Matrix[0, 3] = 1.0;
            db.Solution.Matrix[3, 3] = 0.0;
            db.Solution.Matrix[4, 3] = 0.0;
            peakSums[3] = 1.0;
            // Transition index 4, each row is a precursor
            db.Solution.Matrix[2, 4] = 1.0;
            db.Solution.Matrix[1, 4] = 1.0;
            db.Solution.Matrix[0, 4] = 1.0;
            db.Solution.Matrix[3, 4] = 1.0;
            db.Solution.Matrix[4, 4] = 1.0;
            peakSums[4] = 5.0;

            // Transition bin index (in transBinner) -> solution transition index (0-4 above), needed for
            // CorrectPeakIntensities call
            Dictionary <int, int> binToDeconvIndex = new Dictionary <int, int>(numDeMultiplexedTrans);
            int numBinIndices = binIndicesList.Count;

            for (int i = 0; i < numBinIndices; ++i)
            {
                binToDeconvIndex[binIndicesList[i]] = i;
            }
            var queryBinEnumerator = transBinner.BinsFromValues(testSpectrum.Mzs, true);

            demultiplexer.SpectrumProcessor = new SpectrumProcessor(165, isoMapper, transBinner);
            // Apply the peak intensity correction
            demultiplexer.CorrectPeakIntensitiesTest(testSpectrum, binIndicesSet, peakSums,
                                                     queryBinEnumerator, db, ref deconvIntensities,
                                                     ref deconvMzs);

            // Check that the five solutions defined above were applied correctly
            // by CorrectPeakIntensities

            // If all precursors contribute 0 intensity, the peak should be zero for every
            // deconvolved spectrum
            for (int i = 0; i < deconvIndices.Length; ++i)
            {
                Assert.AreEqual(deconvIntensities[i][binToDeconvIndex[0]], 0.0);
            }

            // Find the index of the demultiplexed spectrum corresponding to the precursor
            // at 710.57 m/z for which we generated the dummy demultiplexing solution for
            int windowIndex;

            isoMapper.TryGetDeconvFromMz(710.57, out windowIndex);
            Assert.AreEqual(50, windowIndex);
            int isoIndex = deconvIndices.IndexOf(i => i == windowIndex);

            Assert.AreNotEqual(-1, isoIndex);

            // This peak falls in one transition bin (transition index 1)
            // 47 is the index of a peak in the test spectrum
            var originalIntensity = testSpectrum.Intensities[47];
            var expectedIntensity = originalIntensity * (2.0 / 6.0);

            Assert.AreEqual(deconvIntensities[isoIndex][47], expectedIntensity, 0.00001);

            // This peak falls in one transitions bin (transition index 2)
            originalIntensity = testSpectrum.Intensities[50];
            expectedIntensity = originalIntensity * (3.0 / 10.0);
            Assert.AreEqual(deconvIntensities[isoIndex][50], expectedIntensity, 0.00001);

            // This peak falls in both transition indices 1 and 2, the expected adjusted
            // intensity should be the average of the solution from each transition bin
            originalIntensity = testSpectrum.Intensities[49];
            expectedIntensity = originalIntensity * (19.0 / 60.0);
            Assert.AreEqual(deconvIntensities[isoIndex][49], expectedIntensity, 0.00001);

            // This peak falls in transition index 3, and the demultiplexing spectra
            // indicates that all of the intensity of teh original peak comes from
            // the precursor window at isoIndex that's being queried.  The peak intensity
            // should not change
            originalIntensity = testSpectrum.Intensities[108];
            expectedIntensity = originalIntensity;
            Assert.AreEqual(deconvIntensities[isoIndex][108], expectedIntensity, 0.00001);

            // This peak falls in one transition bin (transition index 5)
            originalIntensity = testSpectrum.Intensities[149];
            expectedIntensity = originalIntensity * (1.0 / 5.0);
            Assert.AreEqual(deconvIntensities[isoIndex][149], expectedIntensity, 0.00001);

            // Undo changes to the spectrum processor in case this demultiplexer object is
            // reused
            demultiplexer.SpectrumProcessor = oldProcessor;
        }
Exemple #2
0
        /// <summary>
        /// generates a fake deconvolution solution for the given test spectrum
        /// in order to make sure that the deconvolution results are being applied
        /// correctly to scale peaks in the test spectrum
        /// </summary>
        private static void TestPeakIntensityCorrection(MsDataSpectrum testSpectrum,
            AbstractIsoWindowMapper isoMapper,
            AbstractDemultiplexer demultiplexer)
        {
            // Store a reference to the old spectrum processor
            var oldProcessor = demultiplexer.SpectrumProcessor;
            MsxDeconvSolverHandler db = new MsxDeconvSolverHandler(100, 120, 35);
            int[] isoIndices;
            int[] deconvIndices;
            double[] mask = new double[100];
            isoMapper.GetWindowMask(testSpectrum, out isoIndices, out deconvIndices, ref mask);
            db.SetDeconvIndices(deconvIndices);
            Assert.IsTrue(deconvIndices.Contains(50));
            Assert.IsTrue(deconvIndices.Contains(47));
            Assert.IsTrue(deconvIndices.Contains(15));
            Assert.IsTrue(deconvIndices.Contains(17));
            Assert.IsTrue(deconvIndices.Contains(74));
            Assert.AreEqual(5, deconvIndices.Length);

            // Prepare the transition binner with a precursor containing transitions overlapping
            // peaks in the test spectrum
            List<double> precursors = new List<double>();
            List<KeyValuePair<double, double>> transitions = new List<KeyValuePair<double, double>>();
            var precVals = new[] {710.57};
            var transMzs = new[] {372.18, 373.1588, 373.1794, 375.2, 379.23, 387.19};
            var transWidths = new[] {0.3, 0.04, 0.02, 1.0, 1.0, 1.0};
            foreach (var precMz in precVals)
            {
                for (int i = 0; i < isoIndices.Length; ++i)
                {
                    var transMz = transMzs[i];
                    var transWidth = transWidths[i];
                    precursors.Add(precMz);
                    transitions.Add(new KeyValuePair<double, double>(transMz, transWidth));
                }
            }

            var transBinner = new TransitionBinner(precursors, transitions, isoMapper);
            var binIndicesSet = new HashSet<int>(transBinner.BinsForDeconvWindows(deconvIndices));

            double[][] deconvIntensities = new double[deconvIndices.Length][];
            double[][] deconvMzs = new double[deconvIndices.Length][];

            for (int i = 0; i < deconvIndices.Length; ++i)
            {
                deconvIntensities[i] = new double[testSpectrum.Mzs.Length];
                deconvMzs[i] = new double[testSpectrum.Mzs.Length];
            }

            List<int> binIndicesList = binIndicesSet.ToList();
            binIndicesList.Sort();

            // Use each transition bin index to test a different possible
            // type of demultiplexed solution
            int numDeMultiplexedTrans = binIndicesSet.Count;
            double[] peakSums = new double[numDeMultiplexedTrans];
            // Initialize the deconvBlock
            db.Solution.Resize(100, binIndicesList.Count);
            db.Solution.Clear();
            // Transition index 0, each row is a precursor
            // the values for each precursor are the relative contribution
            // of that precursor to the observed (convolved) intensity of the peak
            db.Solution.Matrix[2, 0] = 0.0;
            db.Solution.Matrix[1, 0] = 0.0;
            db.Solution.Matrix[0, 0] = 0.0;
            db.Solution.Matrix[3, 0] = 0.0;
            db.Solution.Matrix[4, 0] = 0.0;
            peakSums[0] = 0.0;
            // Transition index 1, each row is a precursor
            db.Solution.Matrix[2, 1] = 1.0;
            db.Solution.Matrix[1, 1] = 1.0;
            db.Solution.Matrix[0, 1] = 2.0;
            db.Solution.Matrix[3, 1] = 1.0;
            db.Solution.Matrix[4, 1] = 1.0;
            peakSums[1] = 6.0;
            // Transition index 2, each row is a precursor
            db.Solution.Matrix[2, 2] = 2.0;
            db.Solution.Matrix[1, 2] = 1.0;
            db.Solution.Matrix[0, 2] = 3.0;
            db.Solution.Matrix[3, 2] = 3.0;
            db.Solution.Matrix[4, 2] = 1.0;
            peakSums[2] = 10.0;
            // Transition index 3, each row is a precursor
            db.Solution.Matrix[2, 3] = 0.0;
            db.Solution.Matrix[1, 3] = 0.0;
            db.Solution.Matrix[0, 3] = 1.0;
            db.Solution.Matrix[3, 3] = 0.0;
            db.Solution.Matrix[4, 3] = 0.0;
            peakSums[3] = 1.0;
            // Transition index 4, each row is a precursor
            db.Solution.Matrix[2, 4] = 1.0;
            db.Solution.Matrix[1, 4] = 1.0;
            db.Solution.Matrix[0, 4] = 1.0;
            db.Solution.Matrix[3, 4] = 1.0;
            db.Solution.Matrix[4, 4] = 1.0;
            peakSums[4] = 5.0;

            // Transition bin index (in transBinner) -> solution transition index (0-4 above), needed for
            // CorrectPeakIntensities call
            Dictionary<int, int> binToDeconvIndex = new Dictionary<int, int>(numDeMultiplexedTrans);
            int numBinIndices = binIndicesList.Count;
            for (int i = 0; i < numBinIndices; ++i)
                binToDeconvIndex[binIndicesList[i]] = i;
            var queryBinEnumerator = transBinner.BinsFromValues(testSpectrum.Mzs, true);
            demultiplexer.SpectrumProcessor = new SpectrumProcessor(165, isoMapper, transBinner);
            // Apply the peak intensity correction
            demultiplexer.CorrectPeakIntensitiesTest(testSpectrum, binIndicesSet, peakSums,
                                                     queryBinEnumerator, db, ref deconvIntensities,
                                                     ref deconvMzs);

            // Check that the five solutions defined above were applied correctly
            // by CorrectPeakIntensities

            // If all precursors contribute 0 intensity, the peak should be zero for every
            // deconvolved spectrum
            for (int i = 0; i < deconvIndices.Length; ++i)
            {
                Assert.AreEqual(deconvIntensities[i][binToDeconvIndex[0]], 0.0);
            }

            // Find the index of the demultiplexed spectrum corresponding to the precursor
            // at 710.57 m/z for which we generated the dummy demultiplexing solution for
            int windowIndex;
            isoMapper.TryGetDeconvFromMz(710.57, out windowIndex);
            Assert.AreEqual(50, windowIndex);
            int isoIndex = deconvIndices.IndexOf(i => i == windowIndex);
            Assert.AreNotEqual(-1 , isoIndex);

            // This peak falls in one transition bin (transition index 1)
            // 47 is the index of a peak in the test spectrum
            var originalIntensity = testSpectrum.Intensities[47];
            var expectedIntensity = originalIntensity*(2.0/6.0);
            Assert.AreEqual(deconvIntensities[isoIndex][47], expectedIntensity, 0.00001);

            // This peak falls in one transitions bin (transition index 2)
            originalIntensity = testSpectrum.Intensities[50];
            expectedIntensity = originalIntensity*(3.0/10.0);
            Assert.AreEqual(deconvIntensities[isoIndex][50], expectedIntensity, 0.00001);

            // This peak falls in both transition indices 1 and 2, the expected adjusted
            // intensity should be the average of the solution from each transition bin
            originalIntensity = testSpectrum.Intensities[49];
            expectedIntensity = originalIntensity*(19.0/60.0);
            Assert.AreEqual(deconvIntensities[isoIndex][49], expectedIntensity, 0.00001);

            // This peak falls in transition index 3, and the demultiplexing spectra
            // indicates that all of the intensity of teh original peak comes from
            // the precursor window at isoIndex that's being queried.  The peak intensity
            // should not change
            originalIntensity = testSpectrum.Intensities[108];
            expectedIntensity = originalIntensity;
            Assert.AreEqual(deconvIntensities[isoIndex][108], expectedIntensity, 0.00001);

            // This peak falls in one transition bin (transition index 5)
            originalIntensity = testSpectrum.Intensities[149];
            expectedIntensity = originalIntensity * (1.0/5.0);
            Assert.AreEqual(deconvIntensities[isoIndex][149], expectedIntensity, 0.00001);

            // Undo changes to the spectrum processor in case this demultiplexer object is
            // reused
            demultiplexer.SpectrumProcessor = oldProcessor;
        }
Exemple #3
0
        private static void TestIsolationWindowMapper(AbstractIsoWindowMapper isoMapper, MsDataFileImpl file, double width)
        {
            // Check the first five spectra in the file
            for (int checkIndex = 0; checkIndex < 5; ++checkIndex)
            {
                var        spectrum   = file.GetSpectrum(checkIndex);
                var        precursors = spectrum.Precursors;
                List <int> isoIndices = new List <int>();
                // Make sure that each precursor read from the file is included in the isolation
                // window mapper
                foreach (var precursor in precursors)
                {
                    int index;
                    Assert.IsTrue(precursor.IsolationMz.HasValue);
                    Assert.IsTrue(isoMapper.TryGetWindowIndex(precursor.IsolationMz.Value, out index));
                    isoIndices.Add(index);
                }
                int[]    isoIndicesFromMapper;
                int[]    overlapIndicesFromMapper;
                double[] mask = new double[isoMapper.NumWindows];
                // Make sure the isolation windows called from GetWindowMask match those
                // extracted from the spectrum manually above using TryGetWindowIndex
                isoMapper.GetWindowMask(spectrum, out isoIndicesFromMapper, out overlapIndicesFromMapper, ref mask);
                Assert.AreEqual(isoIndices.Count, isoIndicesFromMapper.Length);
                foreach (int index in isoIndicesFromMapper)
                {
                    Assert.IsTrue(isoIndices.Contains(index));
                }
                // Make sure the overlapIndicesFromMapper matches the mask
                for (int i = 0; i < mask.Length; ++i)
                {
                    if (mask[i] < 0.5)
                    {
                        Assert.AreEqual(0.0, mask[i]);
                    }
                    else
                    {
                        Assert.AreEqual(1.0, mask[i]);
                        Assert.IsTrue(overlapIndicesFromMapper.Contains(i));
                    }
                }
            }

            var testMzs = new[] { 553.49, 623.7, 859, 658.55, 768.7, 621.52 };

            // For each test m/z, get the matching isolation window from isoMapper and
            // make sure the m/z does indeed fall within the window
            foreach (var checkMz in testMzs)
            {
                int windowIndex;
                Assert.IsTrue(isoMapper.TryGetWindowFromMz(checkMz, out windowIndex));
                var matchWindowCenterMz = isoMapper.GetPrecursor(windowIndex);
                Assert.IsTrue(matchWindowCenterMz.IsolationMz.HasValue);
                // Check that mz is within window (width/2 )
                Assert.IsTrue(Math.Abs(matchWindowCenterMz.IsolationMz.Value - checkMz) < width / 2);
            }

            // Test out of range precursors
            int dummyIndex;

            Assert.IsFalse(isoMapper.TryGetDeconvFromMz(315.25, out dummyIndex));
            Assert.AreEqual(-1, dummyIndex);
            Assert.IsFalse(isoMapper.TryGetDeconvFromMz(1005.2, out dummyIndex));
            Assert.AreEqual(-1, dummyIndex);
        }
Exemple #4
0
        private static void TestIsolationWindowMapper(AbstractIsoWindowMapper isoMapper, MsDataFileImpl file, double width)
        {
            // Check the first five spectra in the file
            for (int checkIndex = 0 ; checkIndex < 5; ++checkIndex)
            {
                var spectrum = file.GetSpectrum(checkIndex);
                var precursors = spectrum.Precursors;
                List<int> isoIndices = new List<int>();
                // Make sure that each precursor read from the file is included in the isolation
                // window mapper
                foreach (var precursor in precursors)
                {
                    int index;
                    Assert.IsTrue(precursor.IsolationMz.HasValue);
                    Assert.IsTrue(isoMapper.TryGetWindowIndex(precursor.IsolationMz.Value, out index));
                    isoIndices.Add(index);
                }
                int[] isoIndicesFromMapper;
                int[] overlapIndicesFromMapper;
                double[] mask = new double[isoMapper.NumWindows];
                // Make sure the isolation windows called from GetWindowMask match those
                // extracted from the spectrum manually above using TryGetWindowIndex
                isoMapper.GetWindowMask(spectrum, out isoIndicesFromMapper, out overlapIndicesFromMapper, ref mask);
                Assert.AreEqual(isoIndices.Count,isoIndicesFromMapper.Length);
                foreach (int index in isoIndicesFromMapper)
                {
                    Assert.IsTrue(isoIndices.Contains(index));
                }
                // Make sure the overlapIndicesFromMapper matches the mask
                for (int i = 0; i < mask.Length; ++i)
                {
                    if (mask[i] < 0.5)
                        Assert.AreEqual(0.0, mask[i]);
                    else
                    {
                        Assert.AreEqual(1.0, mask[i]);
                        Assert.IsTrue(overlapIndicesFromMapper.Contains(i));
                    }
                }
            }

            var testMzs = new[] {553.49, 623.7, 859, 658.55, 768.7, 621.52};
            // For each test m/z, get the matching isolation window from isoMapper and
            // make sure the m/z does indeed fall within the window
            foreach (var checkMz in testMzs)
            {
                int windowIndex;
                Assert.IsTrue(isoMapper.TryGetWindowFromMz(checkMz, out windowIndex));
                var matchWindowCenterMz = isoMapper.GetPrecursor(windowIndex);
                Assert.IsTrue(matchWindowCenterMz.IsolationMz.HasValue);
                // Check that mz is within window (width/2 )
                Assert.IsTrue(Math.Abs(matchWindowCenterMz.IsolationMz.Value - checkMz) < width/2);
            }

            // Test out of range precursors
            int dummyIndex;
            Assert.IsFalse(isoMapper.TryGetDeconvFromMz(315.25, out dummyIndex));
            Assert.AreEqual(-1, dummyIndex);
            Assert.IsFalse(isoMapper.TryGetDeconvFromMz(1005.2, out dummyIndex));
            Assert.AreEqual(-1, dummyIndex);
        }