Пример #1
0
        private MsDataSpectrum GetSpectrumFromJObject(JObject jObject, int msLevel)
        {
            // ReSharper disable NonLocalizedString
            string strMzs         = jObject["mzs-base64"].ToString();
            string strIntensities = jObject["intensities-base64"].ToString();

            byte[]   mzBytes        = Convert.FromBase64String(strMzs);
            byte[]   intensityBytes = Convert.FromBase64String(strIntensities);
            double[] mzs            = PrimitiveArrays.FromBytes <double>(
                PrimitiveArrays.ReverseBytesInBlocks(mzBytes, sizeof(double)));
            float[] intensityFloats = PrimitiveArrays.FromBytes <float>(
                PrimitiveArrays.ReverseBytesInBlocks(intensityBytes, sizeof(float)));
            double[] intensities = intensityFloats.Select(f => (double)f).ToArray();
            double?  driftTime   = null;
            JToken   jDriftTime;

            if (jObject.TryGetValue("driftTime", out jDriftTime))
            {
                driftTime = jDriftTime.ToObject <double>();
            }
            MsDataSpectrum spectrum = new MsDataSpectrum
            {
                Index         = jObject["index"].ToObject <int>(),
                RetentionTime = jObject["rt"].ToObject <double>(),
                Mzs           = mzs,
                Intensities   = intensities,
                DriftTimeMsec = driftTime,
            };

            return(spectrum);
            // ReSharper restore NonLocalizedString
        }
Пример #2
0
        /// <summary>
        /// Demultiplexes a real .mxML file in a case where the answer is simple, and checks the answer is correct.
        /// </summary>
        private static void TestSpectrumDemultiplex(AbstractDemultiplexer demultiplexer,
                                                    int spectrumIndex,
                                                    MsDataSpectrum originalSpectrum,
                                                    int[] intensityIndices,
                                                    double[] intensityValues,
                                                    int deconvIndex)
        {
            var deconvSpectra  = demultiplexer.GetDeconvolvedSpectra(spectrumIndex, originalSpectrum);
            int numberMzPoints = originalSpectrum.Intensities.Length;

            double[] peakSums = new double[numberMzPoints];
            for (int i = 0; i < numberMzPoints; ++i)
            {
                peakSums[i] = 0.0;
            }
            foreach (var deconvSpectrum in deconvSpectra)
            {
                Assert.AreEqual(deconvSpectrum.Intensities.Length, originalSpectrum.Intensities.Length);
                for (int i = 0; i < numberMzPoints; ++i)
                {
                    peakSums[i] += deconvSpectrum.Intensities[i];
                }
            }
            for (int i = 0; i < numberMzPoints; ++i)
            {
                Assert.AreEqual(peakSums[i], originalSpectrum.Intensities[i], 0.01);
            }
            for (int i = 0; i < intensityIndices.Length; ++i)
            {
                Assert.AreEqual(deconvSpectra[deconvIndex].Intensities[intensityIndices[i]], intensityValues[i], 0.1);
            }
        }
Пример #3
0
        public bool IsSimSpectrum(MsDataSpectrum dataSpectrum, MsDataSpectrum[] spectra)
        {
            if (!EnabledMs || _mseLevel > 0)
            {
                return(false);
            }
            bool isSimSpectrum = dataSpectrum.Level == 1 &&
                                 IsSimIsolation(GetIsolationWindows(dataSpectrum.Precursors).FirstOrDefault());

            if (isSimSpectrum && spectra.Length > 1)
            {
                // If this is actually a run of IMS bins, look at the aggregate isolation window
                var    rt     = spectra[0].RetentionTime;
                var    win    = GetIsolationWindows(spectra[0].Precursors).FirstOrDefault();
                double mzLow  = win.IsolationMz.Value - win.IsolationWidth.Value / 2;
                double mzHigh = win.IsolationMz.Value + win.IsolationWidth.Value / 2;
                for (var i = 1; i < spectra.Length; i++)
                {
                    var spec = spectra[i];
                    if (!Equals(spec.RetentionTime, rt) || !IonMobilityValue.IsExpectedValueOrdering(spectra[i - 1].IonMobility, spec.IonMobility))
                    {
                        return(true);  // Not a run of IMS bins, must have been actual SIM scan
                    }
                    win    = GetIsolationWindows(spec.Precursors).FirstOrDefault();
                    mzLow  = Math.Min(mzLow, win.IsolationMz.Value - win.IsolationWidth.Value / 2);
                    mzHigh = Math.Max(mzHigh, win.IsolationMz.Value + win.IsolationWidth.Value / 2);
                }
                var width = mzHigh - mzLow;
                isSimSpectrum = IsSimIsolation(new IsolationWindowFilter(new SignedMz(mzLow + width / 2), width));
            }
            return(isSimSpectrum);
        }
Пример #4
0
 public bool IsSimSpectrum(MsDataSpectrum dataSpectrum)
 {
     if (!EnabledMs || _mseLevel > 0)
     {
         return(false);
     }
     return(dataSpectrum.Level == 1 &&
            IsSimIsolation(GetIsolationWindows(dataSpectrum.Precursors).FirstOrDefault()));
 }
Пример #5
0
        public void GetChromatograms(List <Protein> targets, double tolerance)
        {
            MsDataSpectrum defaultSpectrum = new MsDataSpectrum();

            MsDataSpectrum[] msDataSpectrums = new MsDataSpectrum[1];
            msDataSpectrums[0] = defaultSpectrum;
            _chromatograms     = new List <Chromatogram>();
            var Channels = (from spectrum in _msDataFileImpl.MsDataSpectrums
                            group spectrum by spectrum.PrecursorMZ into spectrumgroup
                            select new { ChannelMS1 = spectrumgroup.Key, ChannelSpectrums = spectrumgroup }).ToDictionary(di => di.ChannelMS1, di => di.ChannelSpectrums);
            var Proteins = targets;
            var Targets  = from protein in Proteins
                           from peptide in protein.Peptides
                           from precursor in peptide.Precursors
                           select new
            {
                ProteinName      = protein.Name,
                PeptideName      = peptide.Name,
                PrecursorIsoform = precursor.IsotopeLabelType,
                PrecursorMZ      = precursor.PrecursorMZ,
                Products         = precursor.Products
            };

            foreach (var Target in Targets)
            {
                var SpectrumsForChannel = from channel in Channels
                                          where ProcessRawDataTools.InMZTolerance(channel.Key, Target.PrecursorMZ, tolerance) == true
                                          select channel.Value;
                if (SpectrumsForChannel.Any())
                {
                    var ExtraChromatograms = from mzspectrum in SpectrumsForChannel.Single()
                                             select new
                    {
                        mzspectrum.PrecursorMZ,
                        mzspectrum.RetentionTime,
                        mzspectrum.IonIT,
                        mzspectrum.TIC,
                        SumOfIntensities   = mzspectrum.Intensities.Sum(),
                        SumOfPositiveMatch = ProcessRawDataTools.AggIonCounts(mzspectrum.Mzs, mzspectrum.Intensities, Target.Products, tolerance)[0],
                        SumOfNegativeMatch = ProcessRawDataTools.AggIonCounts(mzspectrum.Mzs, mzspectrum.Intensities, Target.Products, tolerance)[1]
                    };
                    _chromatograms.Add(new Chromatogram()
                    {
                        Protein            = Target.ProteinName,
                        Peptide            = Target.ProteinName,
                        IsotopeLabelType   = Target.PrecursorIsoform,
                        PrecursorMZ        = Target.PrecursorMZ,
                        RetentionTimes     = ExtraChromatograms.Select(ec => ec.RetentionTime.GetValueOrDefault(0)).ToArray(),
                        IonInjectionTimes  = ExtraChromatograms.Select(ec => ec.IonIT.GetValueOrDefault(0)).ToArray(),
                        SumOfIntensities   = ExtraChromatograms.Select(ec => ec.SumOfIntensities).ToArray(),
                        SumOfPositiveMatch = ExtraChromatograms.Select(ec => ec.SumOfPositiveMatch).ToArray(),
                        SumOfNegativeMatch = ExtraChromatograms.Select(ec => ec.SumOfNegativeMatch).ToArray()
                    });
                }
            }
        }
Пример #6
0
        private int Compare(MsDataSpectrum spectrum, double ionMobilityValue)
        {
            var sim = spectrum.IonMobility.Mobility;

            if (!sim.HasValue)
            {
                return(0);
            }

            return(Compare(sim.Value, ionMobilityValue));
        }
Пример #7
0
 public bool IsMsMsSpectrum(MsDataSpectrum dataSpectrum)
 {
     if (!EnabledMsMs)
     {
         return(false);
     }
     if (_mseLevel > 0)
     {
         return(UpdateMseLevel(dataSpectrum) == 2);
     }
     return(dataSpectrum.Level == 2);
 }
Пример #8
0
 private static MsDataSpectrum MakeDeconvSpectrum(MsDataSpectrum originalSpectrum, List <KeyValuePair <double, double> > peaks)
 {
     return(new MsDataSpectrum
     {
         RetentionTime = originalSpectrum.RetentionTime,
         Centroided = originalSpectrum.Centroided,
         IonMobility = originalSpectrum.IonMobility,
         Level = originalSpectrum.Level,
         Mzs = peaks.Select(p => p.Key).ToArray(),
         Intensities = peaks.Select(p => p.Value).ToArray(),
     });
 }
Пример #9
0
        private double GetIntensity(MsDataSpectrum spectrum, double mz)
        {
            if (spectrum == null)
            {
                // if we're at a boundary, return minimum value. This means if the peak
                // is not in the other stripe, we get it. Otherwise, they essentially
                // get the intensity.
                return(double.Epsilon);
            }
            var indices = _fragmentTolerance.GetIndices(spectrum.Mzs, mz);

            return(indices.Sum(i => spectrum.Intensities[i]));
        }
Пример #10
0
        private int UpdateMseLevel(MsDataSpectrum dataSpectrum)
        {
            int returnval;

            if ((_mseLastSpectrum == null) || !ReferenceEquals(dataSpectrum, _mseLastSpectrum)) // is this the same one we were just asked about?
            {
                // Waters MSe is enumerated in interleaved scans ("functions" in the raw data) 1==MS 2==MSMS 3=ignore
                // Bruker MSe is enumerated in interleaved MS1 and MS/MS scans
                // Agilent MSe is a series of MS1 scans with ramped CE (SpectrumList_Agilent returns these as MS1,MS2,MS2,...)
                //    but with ion mobility, as of June 2014, it's just a series of MS2 scans with a single nonzero CE, or MS1 scans with 0 CE
                if (_isAgilentMse)
                {
                    if (1 == dataSpectrum.Level)
                    {
                        _mseLevel = 1; // Expecting a series of MS2 scans to follow after this
                        returnval = 1; // Report as MS1
                    }
                    else if ((2 == dataSpectrum.Level) &&
                             (_mseLastSpectrum != null)) // Sometimes the file doesn't contain that leading MS1 scan
                    {
                        _mseLevel = 2;
                        returnval = 2;
                    }
                    else
                    {
                        returnval = 0; // Not useful - probably the file started off mid-cycle, with MS2 CE>0
                    }
                }
                else if (!_isWatersMse)
                {
                    // Bruker - Alternate between 1 and 2
                    _mseLevel = (_mseLevel % 2) + 1;
                    returnval = _mseLevel;
                }
                else
                {
                    // Waters - mse level 1 in raw data "function 1", mse level 2 in raw data "function 2", and "function 3" which we ignore (lockmass?)
                    // All are declared mslevel=1, but we can tell these apart by inspecting the Id which is formatted as <function>.<process>.<scan>
                    _mseLevel = int.Parse(dataSpectrum.Id.Split('.')[0]);
                    returnval = _mseLevel;
                }
                _mseLastSpectrumLevel = returnval;
            }
            else
            {
                returnval = _mseLastSpectrumLevel; // we were just asked about this spectrum, no update this time
            }
            _mseLastSpectrum = dataSpectrum;
            return(returnval);
        }
Пример #11
0
        public bool IsBeyondRange(MsDataSpectrum spectrum)
        {
            if (!_endIonMobilityValue.HasValue)
            {
                return(false);
            }

            double im = _endIonMobilityValue.Value;

            if (_endOffset != 0)
            {
                im += _endOffset;   // For breakpoint
            }
            return(Compare(spectrum, im) > 0);
        }
Пример #12
0
        private Spectrum GenerateMSAmandaSpectrum(MsDataSpectrum spectrum, int index)
        {
            Spectrum amandaSpectrum = new Spectrum()
            {
                RT = spectrum.RetentionTime.Value, SpectrumId = index
            };

            amandaSpectrum.FragmentsPeaks = GetFragmentPeaks(spectrum.Mzs, spectrum.Intensities);
            amandaSpectrum.ScanNumber     = spectrum.Index;
            if (spectrum.Precursors[0].ChargeState.HasValue && spectrum.Precursors[0].PrecursorMz.HasValue)
            {
                amandaSpectrum.Precursor.SetMassCharge(spectrum.Precursors[0].PrecursorMz.Value, spectrum.Precursors[0].ChargeState.Value, true);
            }
            //SpectTitleMap.Add(index, spectrum.Id);
            return(amandaSpectrum);
        }
Пример #13
0
        public List <Spectrum> ParseNextSpectra(int numberOfSpectraToRead, out int nrOfParsed, CancellationToken cancellationToken = new CancellationToken())
        {
            List <Spectrum> spectra = new List <Spectrum>();

            nrOfParsed = 0;
            while (nrOfParsed < numberOfSpectraToRead && specId < spectrumFileReader.SpectrumCount)
            {
                MsDataSpectrum spectrum = spectrumFileReader.GetSpectrum(specId);
                cancellationToken.ThrowIfCancellationRequested();
                ++specId;
                if (spectrum.Level != 2)
                {
                    continue;
                }
                Spectrum amandaSpectrum = GenerateMSAmandaSpectrum(spectrum, amandaId);
                if (amandaSpectrum.Precursor.Charge == 0)
                {
                    foreach (int charge in consideredCharges)
                    {
                        Spectrum newSpect = GenerateSpectrum(amandaSpectrum, amandaId,
                                                             spectrum.Precursors[0].PrecursorMz.Value, charge);
                        SpectTitleMap.Add(amandaId, spectrum.Id);
                        ++amandaId;
                        spectra.Add(newSpect);
                    }
                }
                else
                {
                    SpectTitleMap.Add(amandaId, spectrum.Id);
                    ++amandaId;
                    spectra.Add(amandaSpectrum);
                }

                ++nrOfParsed;
            }

            CurrentSpectrum += nrOfParsed;

            return(spectra);
        }
Пример #14
0
        private MsDataSpectrum GetSpectrumFromJObject(JObject jObject, int msLevel)
        {
            // ReSharper disable NonLocalizedString
            string strMzs         = jObject["mzs-base64"].ToString();
            string strIntensities = jObject["intensities-base64"].ToString();

            byte[]   mzBytes        = Convert.FromBase64String(strMzs);
            byte[]   intensityBytes = Convert.FromBase64String(strIntensities);
            double[] mzs            = PrimitiveArrays.FromBytes <double>(
                PrimitiveArrays.ReverseBytesInBlocks(mzBytes, sizeof(double)));
            float[] intensityFloats = PrimitiveArrays.FromBytes <float>(
                PrimitiveArrays.ReverseBytesInBlocks(intensityBytes, sizeof(float)));
            double[]         intensities = intensityFloats.Select(f => (double)f).ToArray();
            IonMobilityValue ionMobility = IonMobilityValue.EMPTY;
            JToken           jDriftTime;

            if (jObject.TryGetValue("driftTime", out jDriftTime))
            {
                var driftTime = jDriftTime.ToObject <double>();
                if (driftTime != 0)
                {
                    ionMobility = IonMobilityValue.GetIonMobilityValue(driftTime, MsDataFileImpl.eIonMobilityUnits.drift_time_msec);
                }
            }
            MsDataSpectrum spectrum = new MsDataSpectrum
            {
                Index         = jObject["index"].ToObject <int>(),
                RetentionTime = jObject["rt"].ToObject <double>(),
                Mzs           = mzs,
                Intensities   = intensities,
                IonMobility   = ionMobility,
            };

            return(spectrum);
            // ReSharper restore NonLocalizedString
        }
Пример #15
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;
        }
Пример #16
0
        private static TransitionBinner TestTransitionBinnerOverlap(MsDataSpectrum spectrum,
                                                                    AbstractIsoWindowMapper isoMapper, double[] binnedIntensities)
        {
            // Multiple transitions overlapping the same peak from the same precursor
            var precursorList  = new List <double>();
            var transitionList = new List <KeyValuePair <double, double> >();

            precursorList.Add(710.5);                                           //transition: start - stop     index
            transitionList.Add(new KeyValuePair <double, double>(380.19, 1.0)); // 379.69-380.69     Bin3
            precursorList.Add(710.5);
            transitionList.Add(new KeyValuePair <double, double>(380.44, 1.0)); // 379.94-380.94     Bin4
            precursorList.Add(710.5);
            transitionList.Add(new KeyValuePair <double, double>(380.49, 0.6)); // 380.19-380.79     Bin5
            // And a non-overlapping transition for good measure
            precursorList.Add(710.5);
            transitionList.Add(new KeyValuePair <double, double>(389.19, 0.3));   // 389.04 - 389.34   Bin7
            // And a transition that won't have any peaks in the spectrum
            precursorList.Add(710.5);
            transitionList.Add(new KeyValuePair <double, double>(377.0, 0.7));    // 376.65-377.35     Bin0

            // And now another precursor with some of the transitions overlapping the first
            precursorList.Add(595.0);
            transitionList.Add(new KeyValuePair <double, double>(380.0, 1.0));   // 379.5 - 380.5     Bin2
            precursorList.Add(595.0);
            transitionList.Add(new KeyValuePair <double, double>(379.55, 1.0));  // 379.05 - 380.05   Bin1
            precursorList.Add(595.0);
            transitionList.Add(new KeyValuePair <double, double>(389.15, 0.4));  // 388.95 - 389.35   Bin6

            var transitionBinner = new TransitionBinner(precursorList, transitionList, isoMapper);

            double[] binnedData = new double[transitionList.Count];
            transitionBinner.BinData(spectrum.Mzs, spectrum.Intensities, ref binnedData);

            // Test that m/z, intensity pairs were binned correctly into their transitions
            for (int i = 0; i < binnedData.Length; ++i)
            {
                Assert.AreEqual(binnedIntensities[i], binnedData[i], 0.001);
            }

            // Get the precursor index for this precursor
            // make sure the precursor -> transition mapping is working for the first precursor
            int windowIndexOne;

            Assert.IsTrue(isoMapper.TryGetDeconvFromMz(710.5, out windowIndexOne));
            // Use the precursor index to get the bins mapping to this precursor
            var precursorBins         = new HashSet <int>(transitionBinner.BinsForDeconvWindows(new[] { windowIndexOne }));
            var expectedPrecursorBins = new HashSet <int>(new[] { 0, 3, 4, 5, 7 });
            var setComparer           = HashSet <int> .CreateSetComparer();

            Assert.IsTrue(setComparer.Equals(expectedPrecursorBins, precursorBins));

            // Make sure the precursor -> transition mapping is working for the second precursor
            int windowIndexTwo;

            Assert.IsTrue(isoMapper.TryGetDeconvFromMz(595.0, out windowIndexTwo));
            precursorBins         = new HashSet <int>(transitionBinner.BinsForDeconvWindows(new[] { windowIndexOne, windowIndexTwo }));
            expectedPrecursorBins = new HashSet <int>(new[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            Assert.IsTrue(setComparer.Equals(expectedPrecursorBins, precursorBins));

            // m/z values to test mapping of m/z value -> multiplex matching transition bins
            var queryVals = new[] { 252.0, 377.0, 379.92, 379.95 };
            var bins      = transitionBinner.BinsFromValues(queryVals, true).ToList();

            var expectedBins = new List <KeyValuePair <int, int> >
            {
                new KeyValuePair <int, int>(1, 0),
                new KeyValuePair <int, int>(2, 1),
                new KeyValuePair <int, int>(2, 2),
                new KeyValuePair <int, int>(2, 3),
                new KeyValuePair <int, int>(3, 1),
                new KeyValuePair <int, int>(3, 2),
                new KeyValuePair <int, int>(3, 3),
                new KeyValuePair <int, int>(3, 4)
            };

            AssertEx.AreEqualDeep(expectedBins, bins);
            Assert.AreEqual(376.65, transitionBinner.LowerValueFromBin(0), 0.0001);
            Assert.AreEqual(377.35, transitionBinner.UpperValueFromBin(0), 0.0001);
            Assert.AreEqual(377.0, transitionBinner.CenterValueFromBin(0), 0.0001);
            return(transitionBinner);
        }
Пример #17
0
        /// <summary>
        /// Given an original spectrum, return two spectra each with an isolation window that is half of the original spectrum.
        /// </summary>
        public MsDataSpectrum[] GetDeconvolvedSpectra(int index, MsDataSpectrum originalSpectrum)
        {
            if (originalSpectrum.Precursors.Count != 1)
            {
                return(new[] { originalSpectrum });
            }
            MsPrecursor originalPrecursor = originalSpectrum.Precursors.First();
            double?     lowerMzNullable   = GetLowerMz(originalPrecursor);
            double?     upperMzNullable   = GetUpperMz(originalPrecursor);

            if (!lowerMzNullable.HasValue || !upperMzNullable.HasValue)
            {
                return(new[] { originalSpectrum });
            }
            double lowerMz = lowerMzNullable.Value;
            double upperMz = upperMzNullable.Value;

            MsDataSpectrum earlyLow  = FindOverlappingSpectrum(lowerMz, Enumerable.Range(1, index - 1).Select(i => index - i), originalPrecursor);
            MsDataSpectrum earlyHigh = FindOverlappingSpectrum(upperMz, Enumerable.Range(1, index - 1).Select(i => index - i), originalPrecursor);
            MsDataSpectrum lateLow   = FindOverlappingSpectrum(lowerMz,
                                                               Enumerable.Range(index + 1, _dataFile.SpectrumCount - index - 1), originalPrecursor);
            MsDataSpectrum lateHigh = FindOverlappingSpectrum(upperMz,
                                                              Enumerable.Range(index + 1, _dataFile.SpectrumCount - index - 1), originalPrecursor);
            double?middlePrecursorMz = GetMiddlePrecursorMz(new[] { earlyLow, lateLow }, new[] { earlyHigh, lateHigh });

            if (!middlePrecursorMz.HasValue)
            {
                return(new[] { originalSpectrum });
            }
            double[] mzs = originalSpectrum.Mzs;
            List <KeyValuePair <double, double> > lowerPeaks = new List <KeyValuePair <double, double> >();
            List <KeyValuePair <double, double> > upperPeaks = new List <KeyValuePair <double, double> >();
            // ReSharper disable CollectionNeverQueried.Local
            List <KeyValuePair <double, double> > removedPeaks = new List <KeyValuePair <double, double> >();

            // ReSharper restore CollectionNeverQueried.Local
            for (int i = 0; i < mzs.Length; i++)
            {
                double mz = mzs[i];
                double earlyLowIntensity  = GetIntensity(earlyLow, mz);
                double earlyHighIntensity = GetIntensity(earlyHigh, mz);
                double lateLowIntensity   = GetIntensity(lateLow, mz);
                double lateHighIntensity  = GetIntensity(lateHigh, mz);

                double totalLow  = earlyLowIntensity + lateLowIntensity;
                double totalHigh = earlyHighIntensity + lateHighIntensity;
                double total     = totalLow + totalHigh;

                if (total > 0)
                {
                    double fractionLow = totalLow / total;
                    if (fractionLow > 0.0f)
                    {
                        double intensity = originalSpectrum.Intensities[i] * fractionLow;
                        lowerPeaks.Add(new KeyValuePair <double, double>(mz, intensity));
                    }

                    double fractionHigh = totalHigh / total;
                    if (fractionHigh > 0.0f)
                    {
                        double intensity = originalSpectrum.Intensities[i] * fractionHigh;
                        upperPeaks.Add(new KeyValuePair <double, double>(mz, intensity));
                    }
                }
                else
                {
                    removedPeaks.Add(new KeyValuePair <double, double>(mz, originalSpectrum.Intensities[i]));
                }
            }

            MsDataSpectrum lowerSpectrum = MakeDeconvSpectrum(originalSpectrum, lowerPeaks);

            lowerSpectrum.Precursors = ImmutableList.Singleton(MakeMsPrecursor(lowerMz, middlePrecursorMz.Value));
            MsDataSpectrum upperSpectrum = MakeDeconvSpectrum(originalSpectrum, upperPeaks);

            upperSpectrum.Precursors = ImmutableList.Singleton(MakeMsPrecursor(middlePrecursorMz.Value, upperMz));
            return(new[] { lowerSpectrum, upperSpectrum });
        }