Exemple #1
0
            public IonAbundances(TransitionGroupDocNode nodeTranGroup, ChromatogramGroupInfo chromGroupInfo,
                                 float mzMatchTolerance, int peakIndex,
                                 OptimizableRegression regression) : this()
            {
                foreach (var nodeTran in nodeTranGroup.Transitions)
                {
                    var chromInfoCached = chromGroupInfo.GetTransitionInfo(nodeTran, mzMatchTolerance, regression);
                    if (chromInfoCached == null)
                    {
                        continue;
                    }

                    Add(nodeTran, chromInfoCached.GetPeak(peakIndex).Area);
                }
            }
Exemple #2
0
 private static ChromPeak GetLargestPeak(TransitionGroupDocNode nodeTranGroup,
     ChromatogramGroupInfo chromGroupInfo, int peakIndex, float mzMatchTolerance)
 {
     var largestPeak = ChromPeak.EMPTY;
     foreach (var peak in
              from transitionDocNode in nodeTranGroup.Transitions
              select chromGroupInfo.GetTransitionInfo((float) transitionDocNode.Mz, mzMatchTolerance)
              into chromInfo where chromInfo != null
              select chromInfo.GetPeak(peakIndex))
     {
         if (peak.Height > largestPeak.Height)
             largestPeak = peak;
     }
     return largestPeak;
 }
Exemple #3
0
            public PeakMatchData(TransitionGroupDocNode nodeTranGroup, ChromatogramGroupInfo chromGroupInfo,
                                 float mzMatchTolerance, int peakIndex, double totalChromArea)
            {
                Abundances  = new IonAbundances(nodeTranGroup, chromGroupInfo, mzMatchTolerance, peakIndex);
                PercentArea = Abundances.Sum() / totalChromArea;
                var peak = GetLargestPeak(nodeTranGroup, chromGroupInfo, peakIndex, mzMatchTolerance);

                RetentionTime = peak.RetentionTime;
                StartTime     = peak.StartTime;
                EndTime       = peak.EndTime;
                ShiftLeft     = 0;
                ShiftRight    = 0;
                MinTime       = chromGroupInfo.Times.First();
                MaxTime       = chromGroupInfo.Times.Last();
            }
Exemple #4
0
            public PeakMatchData(TransitionGroupDocNode nodeTranGroup, ChromatogramGroupInfo chromGroupInfo,
                                 float mzMatchTolerance, int peakIndex, double totalChromArea,
                                 OptimizableRegression regression)
            {
                Abundances  = new IonAbundances(nodeTranGroup, chromGroupInfo, mzMatchTolerance, peakIndex, regression);
                PercentArea = Abundances.Sum() / totalChromArea;
                var peak = GetLargestPeak(nodeTranGroup, chromGroupInfo, peakIndex, mzMatchTolerance, regression);

                RetentionTime = peak.RetentionTime;
                StartTime     = peak.StartTime;
                EndTime       = peak.EndTime;
                ShiftLeft     = 0;
                ShiftRight    = 0;
                MinTime       = chromGroupInfo.TimeIntensitiesGroup.MinTime;
                MaxTime       = chromGroupInfo.TimeIntensitiesGroup.MaxTime;
            }
Exemple #5
0
        private static double MaxPeakTime(ChromatogramGroupInfo chromGroupInfo)
        {
            double maxIntensity = 0;
            double maxTime      = 0;
            int    iBest        = chromGroupInfo.BestPeakIndex;

            foreach (var chromInfo in chromGroupInfo.TransitionPointSets)
            {
                var peak = chromInfo.GetPeak(iBest);
                if (!peak.IsForcedIntegration && peak.Height > maxIntensity)
                {
                    maxIntensity = peak.Height;
                    maxTime      = peak.RetentionTime;
                }
            }
            return(maxTime);
        }
Exemple #6
0
        private static ChromPeak GetLargestPeak(TransitionGroupDocNode nodeTranGroup,
                                                ChromatogramGroupInfo chromGroupInfo, int peakIndex, float mzMatchTolerance)
        {
            var largestPeak = ChromPeak.EMPTY;

            foreach (var peak in
                     from transitionDocNode in nodeTranGroup.Transitions
                     select chromGroupInfo.GetTransitionInfo((float)transitionDocNode.Mz, mzMatchTolerance)
                     into chromInfo where chromInfo != null
                     select chromInfo.GetPeak(peakIndex))
            {
                if (peak.Height > largestPeak.Height)
                {
                    largestPeak = peak;
                }
            }
            return(largestPeak);
        }
Exemple #7
0
        public override bool GetChromatogram(
            int id, string modifiedSequence, Color peptideColor,
            out ChromExtra extra, out float[] times, out int[] scanIndexes, out float[] intensities, out float[] massErrors)
        {
            var chromKeyIndices = _chromKeyIndices[id];

            if (_lastChromGroupInfo == null || _lastIndices.GroupIndex != chromKeyIndices.GroupIndex)
            {
                _lastChromGroupInfo = _cache.LoadChromatogramInfo(chromKeyIndices.GroupIndex);
                _lastChromGroupInfo.ReadChromatogram(_cache);
            }
            _lastIndices = chromKeyIndices;
            var tranInfo = _lastChromGroupInfo.GetTransitionInfo(chromKeyIndices.TranIndex);

            times       = tranInfo.Times;
            intensities = tranInfo.Intensities;
            massErrors  = null;
            if (tranInfo.MassError10Xs != null)
            {
                massErrors = tranInfo.MassError10Xs.Select(m => m / 10.0f).ToArray();
            }
            scanIndexes = null;
            if (tranInfo.ScanIndexes != null)
            {
                scanIndexes = tranInfo.ScanIndexes[(int)chromKeyIndices.Key.Source];
            }

            SetPercentComplete(100 * id / _chromKeyIndices.Length);

            extra = new ChromExtra(chromKeyIndices.StatusId, chromKeyIndices.StatusRank);

            // Display in AllChromatogramsGraph
            if (chromKeyIndices.Key.Precursor != 0)
            {
                LoadingStatus.Transitions.AddTransition(
                    modifiedSequence,
                    peptideColor,
                    chromKeyIndices.StatusId,
                    chromKeyIndices.StatusRank,
                    times,
                    intensities);
            }
            return(true);
        }
Exemple #8
0
        public static void MakeChromatogramInfo(SignedMz precursorMz, LibraryChromGroup chromGroup, LibraryChromGroup.ChromData chromData, out ChromatogramInfo chromatogramInfo, out TransitionChromInfo transitionChromInfo)
        {
            var timeIntensities = new TimeIntensities(chromGroup.Times, chromData.Intensities, null, null);
            var crawPeakFinder  = Crawdads.NewCrawdadPeakFinder();

            crawPeakFinder.SetChromatogram(chromGroup.Times, chromData.Intensities);
            var crawdadPeak =
                crawPeakFinder.GetPeak(
                    FindNearestIndex(chromGroup.Times, (float)chromGroup.StartTime),
                    FindNearestIndex(chromGroup.Times, (float)chromGroup.EndTime));
            var chromPeak = new ChromPeak(crawPeakFinder, crawdadPeak, 0, timeIntensities, null);

            transitionChromInfo = new TransitionChromInfo(null, 0, chromPeak,
                                                          IonMobilityFilter.EMPTY, // CONSIDER(bspratt) IMS in chromatogram libraries?
                                                          new float?[0], Annotations.EMPTY,
                                                          UserSet.FALSE);
            var peaks  = new[] { chromPeak };
            var header = new ChromGroupHeaderInfo(precursorMz,
                                                  0,                                                    // file index
                                                  1,                                                    // numTransitions
                                                  0,                                                    // startTransitionIndex
                                                  peaks.Length,                                         // numPeaks
                                                  0,                                                    // startPeakIndex
                                                  0,                                                    // startscoreindex
                                                  0,                                                    // maxPeakIndex
                                                  chromGroup.Times.Length,                              // numPoints
                                                  0,                                                    // compressedSize
                                                  0,                                                    // uncompressedsize
                                                  0,                                                    //location
                                                  0, -1, -1, null, null, null, eIonMobilityUnits.none); // CONSIDER(bspratt) IMS in chromatogram libraries?
            var driftTimeFilter = IonMobilityFilter.EMPTY;                                              // CONSIDER(bspratt) IMS in chromatogram libraries?
            var groupInfo       = new ChromatogramGroupInfo(header,
                                                            new Dictionary <Type, int>(),
                                                            new byte[0],
                                                            new ChromCachedFile[0],
                                                            new[] { new ChromTransition(chromData.Mz, 0, (float)(driftTimeFilter.IonMobility.Mobility ?? 0), (float)(driftTimeFilter.IonMobilityExtractionWindowWidth ?? 0), ChromSource.unknown), },
                                                            peaks,
                                                            null)
            {
                TimeIntensitiesGroup = TimeIntensitiesGroup.Singleton(timeIntensities)
            };

            chromatogramInfo = new ChromatogramInfo(groupInfo, 0);
        }
Exemple #9
0
        private bool ProcessChromInfo(MsDataFileUri filePath, ChromatogramGroupInfo chromInfo, PeptidePrecursorPair pair,
                                      TransitionGroupDocNode nodeGroup, float tolerance, LibKey libKey)
        {
            if (chromInfo.NumPeaks == 0)  // Due to data polarity mismatch, probably
            {
                return(true);
            }
            Assume.IsTrue(chromInfo.BestPeakIndex != -1);
            var resultIndex = _document.Settings.MeasuredResults.Chromatograms.IndexOf(c => c.GetFileInfo(filePath) != null);

            if (resultIndex == -1)
            {
                return(true);
            }
            var chromFileInfo = _document.Settings.MeasuredResults.Chromatograms[resultIndex].GetFileInfo(filePath);

            Assume.IsTrue(Equals(chromFileInfo.FilePath.GetLockMassParameters(), filePath.GetLockMassParameters()));

            // Determine apex RT for DT measurement using most intense MS1 peak
            var apexRT = GetApexRT(nodeGroup, resultIndex, chromFileInfo, true) ??
                         GetApexRT(nodeGroup, resultIndex, chromFileInfo, false);

            if (!apexRT.HasValue)
            {
                return(true);
            }
            Assume.IsTrue(chromInfo.PrecursorMz.CompareTolerant(pair.NodeGroup.PrecursorMz, 1.0E-9f) == 0, @"mismatch in precursor values");
            // Only use the transitions currently enabled
            var transitionPointSets = chromInfo.TransitionPointSets.Where(
                tp => nodeGroup.Transitions.Any(
                    t => (t.Mz - (tp.ExtractionWidth ?? tolerance) / 2) <= tp.ProductMz &&
                    (t.Mz + (tp.ExtractionWidth ?? tolerance) / 2) >= tp.ProductMz))
                                      .ToArray();

            for (var msLevel = 1; msLevel <= 2; msLevel++)
            {
                if (!ProcessMSLevel(filePath, msLevel, transitionPointSets, chromInfo, apexRT, nodeGroup, libKey, tolerance))
                {
                    return(false); // User cancelled
                }
            }
            return(true);
        }
        public PeakTransitionGroupId(PeptideGroupDocNode nodePepGroup,
                                     PeptideDocNode nodePep,
                                     IsotopeLabelType labelType,
                                     ChromatogramSet chromatogramSet,
                                     ChromatogramGroupInfo chromGroupInfo,
                                     IDictionary <int, int> runEnumDict)
        {
            NodePepGroup    = nodePepGroup;
            LabelType       = labelType;
            ChromatogramSet = chromatogramSet;
            FilePath        = chromGroupInfo.FilePath;
            FileId          = chromatogramSet.FindFile(chromGroupInfo);
            Run             = runEnumDict[FileId.GlobalIndex];

            // Avoid hanging onto the peptide, since it can end up being the primary memory root
            // for large-scale command-line processing
            RawTextId           = nodePep.ModifiedTarget.ToString();
            RawUnmodifiedTextId = nodePep.Target.ToString();
            IsDecoy             = nodePep.IsDecoy;
            Key = new PeakTransitionGroupIdKey(nodePep.Id.GlobalIndex, FileId.GlobalIndex);
        }
Exemple #11
0
        public override bool GetChromatogram(int id, Target modifiedSequence, Color peptideColor, out ChromExtra extra, out TimeIntensities timeIntensities)
        {
            var chromKeyIndices = _chromKeyIndices[id];

            if (_lastChromGroupInfo == null || _lastIndices.GroupIndex != chromKeyIndices.GroupIndex)
            {
                _lastChromGroupInfo = _cache.LoadChromatogramInfo(chromKeyIndices.GroupIndex);
                _lastChromGroupInfo.ReadChromatogram(_cache);
            }
            _lastIndices = chromKeyIndices;
            var tranInfo = _lastChromGroupInfo.GetTransitionInfo(chromKeyIndices.TranIndex);

            timeIntensities = tranInfo.TimeIntensities;

            // Assume that each chromatogram will be read once, though this may
            // not always be completely true.
            _readChromatograms++;

            // But avoid reaching 100% before reading is actually complete
            SetPercentComplete(Math.Min(99, 100 * _readChromatograms / _chromKeyIndices.Length));

            extra = new ChromExtra(chromKeyIndices.StatusId, chromKeyIndices.StatusRank);

            // Display in AllChromatogramsGraph
            if (chromKeyIndices.Key.Precursor != 0 && Status is ChromatogramLoadingStatus)
            {
                ((ChromatogramLoadingStatus)Status).Transitions.AddTransition(
                    modifiedSequence,
                    peptideColor,
                    chromKeyIndices.StatusId,
                    chromKeyIndices.StatusRank,
                    timeIntensities.Times,
                    timeIntensities.Intensities);
            }
            return(true);
        }
Exemple #12
0
            public void WriteChromGroup(ChromatogramGroupInfo originalChromGroup, MinimizedChromGroup minimizedChromGroup)
            {
                if (minimizedChromGroup.RetainedTransitionIndexes.Count == 0)
                {
                    return;
                }

                var originalHeader       = originalChromGroup.Header;
                int fileIndex            = originalHeader.FileIndex;
                int startTransitionIndex = _transitions.Count;
                int startPeakIndex       = _peakCount;
                int startScoreIndex      = _scoreCount;

                _scoreCount += minimizedChromGroup.PeakScores.Length;
                PrimitiveArrays.Write(_outputStreamScores, minimizedChromGroup.PeakScores);

                _peakCount += minimizedChromGroup.TotalPeakCount;
                _transitions.AddRange(minimizedChromGroup.Transitions);

                var          flags                  = originalHeader.Flags;
                MemoryStream pointsStream           = new MemoryStream();
                var          transitionChromSources = minimizedChromGroup.Transitions.Select(tran => tran.Source).ToArray();
                var          timeIntensitiesGroup   = minimizedChromGroup.MinimizedTimeIntensitiesGroup;

                if (timeIntensitiesGroup is RawTimeIntensities && _cacheFormat.FormatVersion < CacheFormatVersion.Twelve)
                {
                    timeIntensitiesGroup = ((RawTimeIntensities)minimizedChromGroup.MinimizedTimeIntensitiesGroup)
                                           .Interpolate(transitionChromSources);
                }
                timeIntensitiesGroup.WriteToStream(pointsStream);
                if (timeIntensitiesGroup is RawTimeIntensities)
                {
                    flags |= ChromGroupHeaderInfo.FlagValues.raw_chromatograms;
                }
                else
                {
                    flags &= ~ChromGroupHeaderInfo.FlagValues.raw_chromatograms;
                    var interpolatedTimeIntensities = timeIntensitiesGroup as InterpolatedTimeIntensities;
                    if (interpolatedTimeIntensities != null)
                    {
                        flags &=
                            ~(ChromGroupHeaderInfo.FlagValues.has_frag_scan_ids |
                              ChromGroupHeaderInfo.FlagValues.has_sim_scan_ids |
                              ChromGroupHeaderInfo.FlagValues.has_ms1_scan_ids);
                        var scanIdsByChromSource = interpolatedTimeIntensities.ScanIdsByChromSource();
                        if (scanIdsByChromSource.ContainsKey(ChromSource.fragment))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_frag_scan_ids;
                        }
                        if (scanIdsByChromSource.ContainsKey(ChromSource.ms1))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_ms1_scan_ids;
                        }
                        if (scanIdsByChromSource.ContainsKey(ChromSource.sim))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_sim_scan_ids;
                        }
                    }
                }

                int numPeaks     = minimizedChromGroup.NumPeaks;
                int maxPeakIndex = minimizedChromGroup.MaxPeakIndex;

                _cacheFormat.ChromPeakSerializer().WriteItems(_outputStreamPeaks, minimizedChromGroup.MinimizedPeaks);

                long location        = _outputStream.Position;
                int  lenUncompressed = (int)pointsStream.Length;

                byte[] pointsCompressed = pointsStream.ToArray().Compress(3);
                int    lenCompressed    = pointsCompressed.Length;

                _outputStream.Write(pointsCompressed, 0, lenCompressed);
                int textIdIndex;
                int textIdLen;
                var newTextId = GetNewTextId(originalHeader);

                if (newTextId == null)
                {
                    textIdIndex = -1;
                    textIdLen   = 0;
                }
                else
                {
                    textIdIndex = CalcTextIdOffset(newTextId);
                    textIdLen   = newTextId.Count;
                }

                var header = new ChromGroupHeaderInfo(originalHeader.Precursor,
                                                      textIdIndex,
                                                      textIdLen,
                                                      fileIndex,
                                                      _transitions.Count - startTransitionIndex,
                                                      startTransitionIndex,
                                                      numPeaks,
                                                      startPeakIndex,
                                                      startScoreIndex,
                                                      maxPeakIndex,
                                                      timeIntensitiesGroup.NumInterpolatedPoints,
                                                      lenCompressed,
                                                      lenUncompressed,
                                                      location,
                                                      flags,
                                                      originalHeader.StatusId,
                                                      originalHeader.StatusRank,
                                                      originalHeader.StartTime, originalHeader.EndTime,
                                                      originalHeader.CollisionalCrossSection,
                                                      originalHeader.IonMobilityUnits);

                _chromGroupHeaderInfos.Add(header);
            }
Exemple #13
0
 public ChromFileInfoId FindFile(ChromatogramGroupInfo chromGroupInfo)
 {
     return FindFile(chromGroupInfo.FilePath);
 }
        /// <summary>
        /// Collects statistics on how much space savings minimizing will achieve, and (if outStream
        /// is not null) writes out the minimized cache file.
        /// </summary>
        public void Minimize(Settings settings, ProgressCallback progressCallback, Stream outStream,
            FileStream outStreamScans = null, FileStream outStreamPeaks = null, FileStream outStreamScores = null)
        {
            var writer = outStream == null ? null : new Writer(ChromatogramCache, outStream, outStreamScans, outStreamPeaks, outStreamScores);
            var statisticsCollector = new MinStatisticsCollector(this);
            bool readChromatograms = settings.NoiseTimeRange.HasValue || writer != null;

            var chromGroupHeaderToIndex =
                ChromGroupHeaderInfos
                    .Select((cghi, index) => new KeyValuePair<ChromGroupHeaderInfo5, int>(cghi, index))
                    .ToDictionary(kvp => kvp.Key, kvp=>kvp.Value);
            var chromGroups = new ChromatogramGroupInfo[ChromGroupHeaderInfos.Count];
            var transitionGroups = new List<TransitionGroupDocNode>[ChromGroupHeaderInfos.Count];
            foreach (var nodePep in Document.Molecules)
            {
                foreach (var nodeGroup in nodePep.TransitionGroups)
                {
                    ChromatogramGroupInfo[] groupInfos;
                    ChromatogramCache.TryLoadChromatogramInfo(nodePep, nodeGroup, _tolerance, out groupInfos);
                    foreach (var chromGroupInfo in groupInfos)
                    {
                        int headerIndex = chromGroupHeaderToIndex[chromGroupInfo.Header];
                        if (chromGroups[headerIndex] == null)
                        {
                            chromGroups[headerIndex] = chromGroupInfo;
                            transitionGroups[headerIndex] = new List<TransitionGroupDocNode>();
                        }
                        transitionGroups[headerIndex].Add(nodeGroup);
                    }
                }
            }

            for (int iHeader = 0; iHeader < ChromGroupHeaderInfos.Count; iHeader++)
            {
                var chromGroupInfo = chromGroups[iHeader];
                IList<TransitionGroupDocNode> transitionGroupDocNodes;
                if (chromGroupInfo == null)
                {
                    chromGroupInfo = ChromatogramCache.LoadChromatogramInfo(ChromGroupHeaderInfos[iHeader]);
                    transitionGroupDocNodes = new TransitionGroupDocNode[0];
                }
                else
                {
                    transitionGroupDocNodes = transitionGroups[iHeader];
                }
                if (readChromatograms)
                {
                    try
                    {
                        chromGroupInfo.ReadChromatogram(ChromatogramCache);
                    }
                    catch (Exception exception)
                    {
                        Trace.TraceWarning("Unable to read chromatogram {0}", exception); // Not L10N
                    }
                }
                MinimizedChromGroup minimizedChromGroup = MinimizeChromGroup(settings,
                    chromGroupInfo, transitionGroupDocNodes);
                statisticsCollector.ProcessChromGroup(minimizedChromGroup);
                if (progressCallback != null)
                {
                    progressCallback.Invoke(statisticsCollector.GetStatistics());
                }
                if (writer != null)
                {
                    writer.WriteChromGroup(chromGroupInfo, minimizedChromGroup);
                }
                // Null out the ChromGroup in our array so it can be garbage collected.
                chromGroups[iHeader] = null;
            }
            if (progressCallback != null)
            {
                progressCallback.Invoke(statisticsCollector.GetStatistics());
            }
            if (writer != null)
            {
                writer.WriteEndOfFile();
            }
        }
Exemple #15
0
        public void TestNoiseTimeLimit()
        {
            var         testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);
            string      docPath      = testFilesDir.GetTestPath("BSA_Protea_label_free_20100323_meth3_multi.sky");
            SrmDocument doc          = ResultsUtil.DeserializeDocument(docPath);

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                // Import the first RAW file (or mzML for international)
                string rawPath = testFilesDir.GetTestPath("ah_20101011y_BSA_MS-MS_only_5-2" +
                                                          ExtensionTestContext.ExtThermoRaw);
                var measuredResults = new MeasuredResults(new[] { new ChromatogramSet("Single", new[] { rawPath }) });

                SrmDocument docResults = docContainer.ChangeMeasuredResults(measuredResults, 3, 3, 21);
                var         tolerance  = (float)docResults.Settings.TransitionSettings.Instrument.MzMatchTolerance;


                ChromCacheMinimizer.Settings settings = new ChromCacheMinimizer.Settings()
                                                        .ChangeDiscardUnmatchedChromatograms(false)
                                                        .ChangeNoiseTimeRange(1.0);
                string minimized1Path = testFilesDir.GetTestPath("NoiseTimeLimited1.sky");
                string minimized2Path = testFilesDir.GetTestPath("NoiseTimeLimited2.sky");
                using (var docContainerMinimized1Min = MinimizeCacheFile(docResults,
                                                                         settings.ChangeNoiseTimeRange(1.0),
                                                                         minimized1Path))
                    using (var docContainerMinimized2Min = MinimizeCacheFile(docResults,
                                                                             settings.ChangeNoiseTimeRange(2.0),
                                                                             minimized2Path))
                    {
                        SrmDocument     docMinimized1Min = docContainerMinimized1Min.Document;
                        SrmDocument     docMinimized2Min = docContainerMinimized2Min.Document;
                        ChromatogramSet chromSet1Min     = docMinimized1Min.Settings.MeasuredResults.Chromatograms[0];
                        ChromatogramSet chromSet2Min     = docMinimized2Min.Settings.MeasuredResults.Chromatograms[0];
                        ChromatogramSet chromSetOriginal = docResults.Settings.MeasuredResults.Chromatograms[0];
                        foreach (var pair in docResults.PeptidePrecursorPairs)
                        {
                            ChromatogramGroupInfo[] chromGroupsOriginal;
                            ChromatogramGroupInfo[] chromGroups1;
                            ChromatogramGroupInfo[] chromGroups2;

                            docMinimized1Min.Settings.MeasuredResults.TryLoadChromatogram(chromSet1Min,
                                                                                          pair.NodePep, pair.NodeGroup, tolerance, true, out chromGroups1);
                            docMinimized2Min.Settings.MeasuredResults.TryLoadChromatogram(chromSet2Min,
                                                                                          pair.NodePep, pair.NodeGroup, tolerance, true, out chromGroups2);
                            docResults.Settings.MeasuredResults.TryLoadChromatogram(chromSetOriginal,
                                                                                    pair.NodePep, pair.NodeGroup, tolerance, true, out chromGroupsOriginal);
                            Assert.AreEqual(chromGroups1.Length, chromGroups2.Length);
                            Assert.AreEqual(chromGroups1.Length, chromGroupsOriginal.Length);
                            for (int iChromGroup = 0; iChromGroup < chromGroups1.Length; iChromGroup++)
                            {
                                ChromatogramGroupInfo chromGroup1        = chromGroups1[iChromGroup];
                                ChromatogramGroupInfo chromGroup2        = chromGroups2[iChromGroup];
                                ChromatogramGroupInfo chromGroupOriginal = chromGroupsOriginal[iChromGroup];
                                var times = new[]
                                {
                                    GetTimes(chromGroupOriginal)[0],
                                    GetTimes(chromGroup2)[0],
                                    GetTimes(chromGroup1)[0],
                                    GetTimes(chromGroup1).Last(),
                                    GetTimes(chromGroup2).Last(),
                                    GetTimes(chromGroupOriginal).Last()
                                };
                                // The two minute window around the peak might overlap with either the start or end of the original chromatogram,
                                // but will never overlap with both.
                                Assert.IsTrue(GetTimes(chromGroup2)[0] > GetTimes(chromGroupOriginal)[0]
                                              ||
                                              GetTimes(chromGroup2).Last() <
                                              GetTimes(chromGroupOriginal).Last());
                                // If the two minute window does not overlap with the start/end of the original chromatogram, then the difference
                                // in time between the one minute window and the two minute window will be approximately 1 minute.
                                if (GetTimes(chromGroup2)[0] > GetTimes(chromGroupOriginal)[0])
                                {
                                    Assert.AreEqual(GetTimes(chromGroup2)[0], GetTimes(chromGroup1)[0] - 1, .1);
                                }
                                if (GetTimes(chromGroup2).Last() <
                                    GetTimes(chromGroupOriginal).Last())
                                {
                                    Assert.AreEqual(GetTimes(chromGroup2).Last(),
                                                    GetTimes(chromGroup1).Last() + 1, .1);
                                }
                                float[] timesSorted = times.ToArray();
                                Array.Sort(timesSorted);
                                CollectionAssert.AreEqual(times, timesSorted);
                            }
                        }
                    }
            }
        }
Exemple #16
0
 public ChromFileInfoId FindFile(ChromatogramGroupInfo chromGroupInfo)
 {
     return(FindFile(chromGroupInfo.FilePath));
 }
 /// <summary>
 /// Decides what information from a ChromatogramGroupInfo should be thrown away.
 /// ChromatogramGroupInfo's can have the the retention time range shortened, and certain
 /// transitions discarded.
 /// </summary>
 private MinimizedChromGroup MinimizeChromGroup(Settings settings, ChromatogramGroupInfo chromatogramGroupInfo, IList<TransitionGroupDocNode> transitionGroups)
 {
     var fileIndexes = new List<int>();
     for (int fileIndex = 0; fileIndex < Document.Settings.MeasuredResults.Chromatograms.Count; fileIndex++)
     {
         var chromatogramSet = Document.Settings.MeasuredResults.Chromatograms[fileIndex];
         if (chromatogramSet.MSDataFilePaths.Any(path=>Equals(path, chromatogramGroupInfo.FilePath)))
         {
             fileIndexes.Add(fileIndex);
         }
     }
     var chromatograms = chromatogramGroupInfo.TransitionPointSets.ToArray();
     Assume.IsTrue(Equals(chromatogramGroupInfo.NumTransitions, chromatograms.Length));
     var keptTransitionIndexes = new List<int>();
     double minRetentionTime = Double.MaxValue;
     double maxRetentionTime = -Double.MaxValue;
     for (int i = 0; i < chromatograms.Length; i++)
     {
         var chromatogram = chromatograms[i];
         var matchingTransitions = new List<TransitionDocNode>();
         foreach (var transitionDocNode in transitionGroups.SelectMany(tg => tg.Transitions))
         {
             if (0!=ChromKey.CompareTolerant((float) chromatogram.ProductMz, (float) transitionDocNode.Mz, _tolerance))
             {
                 continue;
             }
             matchingTransitions.Add(transitionDocNode);
             foreach (var fileIndex in fileIndexes)
             {
                 var chromatogramSet = transitionDocNode.Results[fileIndex];
                 if (chromatogramSet == null)
                 {
                     continue;
                 }
                 foreach (var transitionChromInfo in chromatogramSet)
                 {
                     if (transitionChromInfo.IsEmpty)
                     {
                         continue;
                     }
                     minRetentionTime = Math.Min(minRetentionTime, transitionChromInfo.StartRetentionTime);
                     maxRetentionTime = Math.Max(maxRetentionTime, transitionChromInfo.EndRetentionTime);
                 }
             }
         }
         bool kept = !settings.DiscardUnmatchedChromatograms
             || matchingTransitions.Count > 0;
         if (kept)
         {
             keptTransitionIndexes.Add(i);
         }
     }
     var result = new MinimizedChromGroup(chromatogramGroupInfo.Header)
                      {
                          RetainedTransitionIndexes = keptTransitionIndexes,
                      };
     if (settings.NoiseTimeRange.HasValue && minRetentionTime < maxRetentionTime)
     {
         if (null == chromatogramGroupInfo.Times)
         {
             // Chromatogram was unreadable.
             result.RetainedTransitionIndexes.Clear();
         }
         else
         {
             result.SetStartEndTime(chromatogramGroupInfo.Times, (float)(minRetentionTime - settings.NoiseTimeRange), (float)(maxRetentionTime + settings.NoiseTimeRange));
         }
     }
     return result;
 }
        private bool EnsureChromInfo(MeasuredResults results,
            ChromatogramSet chromatograms,
            PeptideDocNode[] nodePeps,
            TransitionGroupDocNode[] nodeGroups,
            IdentityPath[] groupPaths,
            float mzMatchTolerance,
            out bool changedGroups,
            out bool changedGroupIds)
        {
            if (UpdateGroups(nodeGroups, groupPaths, out changedGroups, out changedGroupIds) && !_extractor.HasValue)
                return true;

            _extractor = null;

            bool success = false;
            try
            {
                // Get chromatogram sets for all transition groups, recording unique
                // file paths in the process.
                var listArrayChromInfo = new List<ChromatogramGroupInfo[]>();
                var listFiles = new List<MsDataFileUri>();
                for (int i = 0; i < nodeGroups.Length; i++)
                {
                    ChromatogramGroupInfo[] arrayChromInfo;
                    if (!results.TryLoadChromatogram(
                        chromatograms,
                        nodePeps[i],
                        nodeGroups[i],
                        mzMatchTolerance,
                        true,
                        out arrayChromInfo))
                    {
                        listArrayChromInfo.Add(null);
                        continue;
                    }

                    listArrayChromInfo.Add(arrayChromInfo);
                    foreach (var chromInfo in arrayChromInfo)
                    {
                        var filePath = chromInfo.FilePath;
                        if (!listFiles.Contains(filePath))
                            listFiles.Add(filePath);
                    }
                }

                // If no data was found, then return false
                if (listFiles.Count == 0)
                    return false;

                // Make a list of chromatogram info by unique file path corresponding
                // to the groups passed in.
                _arrayChromInfo = new ChromatogramGroupInfo[listFiles.Count][];
                for (int i = 0; i < _arrayChromInfo.Length; i++)
                {
                    var arrayNew = new ChromatogramGroupInfo[listArrayChromInfo.Count];
                    for (int j = 0; j < arrayNew.Length; j++)
                    {
                        var arrayChromInfo = listArrayChromInfo[j];
                        if (arrayChromInfo == null)
                            continue;
                        foreach (var chromInfo in arrayChromInfo)
                        {
                            if (arrayNew[j] == null && Equals(listFiles[i], chromInfo.FilePath))
                                arrayNew[j] = chromInfo;
                        }
                    }
                    _arrayChromInfo[i] = arrayNew;
                }

                // If multiple replicate files contain mutually exclusive data, create "all files" option.
                var mergedChromGroupInfo = GetMergedChromInfo();
                _hasMergedChromInfo = (mergedChromGroupInfo != null);
                if (_hasMergedChromInfo)
                {
                    var arrayNew = new ChromatogramGroupInfo[_arrayChromInfo.Length + 1][];
                    arrayNew[0] = mergedChromGroupInfo;
                    for (int i = 1; i < arrayNew.Length; i++)
                        arrayNew[i] = _arrayChromInfo[i - 1];
                    _arrayChromInfo = arrayNew;
                }

                success = true;
            }
            finally
            {
                // Make sure the info array is set to null on failure.
                if (!success)
                    _arrayChromInfo = null;
            }

            return true;
        }
Exemple #19
0
 public PeakMatchData(TransitionGroupDocNode nodeTranGroup, ChromatogramGroupInfo chromGroupInfo,
     float mzMatchTolerance, int peakIndex, double totalChromArea)
 {
     Abundances = new IonAbundances(nodeTranGroup, chromGroupInfo, mzMatchTolerance, peakIndex);
     PercentArea = Abundances.Sum()/totalChromArea;
     var peak = GetLargestPeak(nodeTranGroup, chromGroupInfo, peakIndex, mzMatchTolerance);
     RetentionTime = peak.RetentionTime;
     StartTime = peak.StartTime;
     EndTime = peak.EndTime;
     ShiftLeft = 0;
     ShiftRight = 0;
     MinTime = chromGroupInfo.Times.First();
     MaxTime = chromGroupInfo.Times.Last();
 }
        private void DisplayTransitions(IRegressionFunction timeRegressionFunction,
            TransitionDocNode nodeTranSelected,
            ChromatogramSet chromatograms,
            float mzMatchTolerance,
            TransitionGroupDocNode nodeGroup,
            ChromatogramGroupInfo chromGroupInfo,
            PaneKey graphPaneKey,
            DisplayTypeChrom displayType,
            ref double bestStartTime,
            ref double bestEndTime)
        {
            var fileId = chromatograms.FindFile(chromGroupInfo);

            // Get points for all transitions, and pick maximum peaks.
            ChromatogramInfo[] arrayChromInfo;
            var displayTrans = GetDisplayTransitions(nodeGroup, displayType).ToArray();
            int numTrans = displayTrans.Length;
            int numSteps = 0;
            bool allowEmpty = false;

            if (IsSingleTransitionDisplay && nodeTranSelected != null)
            {
                if (!displayTrans.Contains(nodeTranSelected))
                {
                    arrayChromInfo = new ChromatogramInfo[0];
                    displayTrans = new TransitionDocNode[0];
                    numTrans = 0;
                }
                else
                {
                    arrayChromInfo = chromGroupInfo.GetAllTransitionInfo((float) nodeTranSelected.Mz,
                                                                            mzMatchTolerance,
                                                                            chromatograms.OptimizationFunction);

                    if (chromatograms.OptimizationFunction != null)
                    {
                        // Make sure the number of steps matches what will show up in the summary
                        // graphs, or the colors won't match up.
                        int numStepsExpected = chromatograms.OptimizationFunction.StepCount*2 + 1;
                        if (arrayChromInfo.Length != numStepsExpected)
                        {
                            arrayChromInfo = ResizeArrayChromInfo(arrayChromInfo, numStepsExpected);
                            allowEmpty = true;
                        }
                    }

                    numTrans = arrayChromInfo.Length;
                    displayTrans = new TransitionDocNode[numTrans];
                    for (int i = 0; i < numTrans; i++)
                        displayTrans[i] = nodeTranSelected;
                }
                numSteps = numTrans/2;
            }
            else
            {
                arrayChromInfo = new ChromatogramInfo[numTrans];
                for (int i = 0; i < numTrans; i++)
                {
                    var nodeTran = displayTrans[i];
                    // Get chromatogram info for this transition
                    arrayChromInfo[i] = chromGroupInfo.GetTransitionInfo((float) nodeTran.Mz, mzMatchTolerance);
                }
            }

            int bestPeakTran = -1;
            TransitionChromInfo tranPeakInfo = null;
            float maxPeakHeight = float.MinValue;
            int numPeaks = chromGroupInfo.NumPeaks;
            var maxPeakTrans = new int[numPeaks];
            var maxPeakHeights = new float[numPeaks];
            for (int i = 0; i < numPeaks; i++)
                maxPeakHeights[i] = float.MinValue;
            var transform = Transform;
            // Prepare arrays of values for library dot-product
            double[] expectedIntensities = null;
            double[][] peakAreas = null;
            bool isShowingMs = displayTrans.Any(nodeTran => nodeTran.IsMs1);
            bool isShowingMsMs = displayTrans.Any(nodeTran => !nodeTran.IsMs1);
            bool isFullScanMs = DocumentUI.Settings.TransitionSettings.FullScan.IsEnabledMs && isShowingMs;
            if ((isFullScanMs && !isShowingMsMs && nodeGroup.HasIsotopeDist) ||
                (!isFullScanMs && nodeGroup.HasLibInfo))
            {
                expectedIntensities = new double[numTrans];
                peakAreas = new double[numPeaks][];
                for (int i = 0; i < numPeaks; i++)
                    peakAreas[i] = new double[numTrans];
            }

            // Find the transition with the maximum peak height for the best peak
            for (int i = 0; i < numTrans; i++)
            {
                var nodeTran = displayTrans[i];
                int step = (numSteps > 0 ? i - numSteps : 0);
                var transitionChromInfo = GetTransitionChromInfo(nodeTran, _chromIndex, fileId, step);
                if (transitionChromInfo == null)
                    continue;

                if (maxPeakHeight < transitionChromInfo.Height)
                {
                    maxPeakHeight = transitionChromInfo.Height;
                    bestPeakTran = i;
                    tranPeakInfo = transitionChromInfo;
                }
                AddBestPeakTimes(transitionChromInfo, ref bestStartTime, ref bestEndTime);
            }

            for (int i = 0; i < numTrans; i++)
            {
                var nodeTran = displayTrans[i];

                // Store library intensities for dot-product
                if (expectedIntensities != null)
                {
                    if (isFullScanMs)
                        expectedIntensities[i] = nodeTran.HasDistInfo ? nodeTran.IsotopeDistInfo.Proportion : 0;
                    else
                        expectedIntensities[i] = nodeTran.HasLibInfo ? nodeTran.LibInfo.Intensity : 0;
                }

                var info = arrayChromInfo[i];
                if (info == null)
                    continue;

                // Apply any active transform
                info.Transform(transform);

                for (int j = 0; j < numPeaks; j++)
                {
                    var peak = info.GetPeak(j);

                    // Exclude any peaks between the boundaries of the chosen peak.
                    if (IntersectPeaks(peak, tranPeakInfo))
                        continue;

                    // Store peak intensity for dot-product
                    if (peakAreas != null)
                        peakAreas[j][i] = peak.Area;

                    // Keep track of which transition has the max height for each peak
                    if (maxPeakHeights[j] < peak.Height)
                    {
                        maxPeakHeights[j] = peak.Height;
                        maxPeakTrans[j] = i;
                    }
                }
            }

            // Calculate library dot-products, if possible
            double[] dotProducts = null;
            double bestProduct = 0;
            int minProductTrans = isFullScanMs
                                        ? TransitionGroupDocNode.MIN_DOT_PRODUCT_MS1_TRANSITIONS
                                        : TransitionGroupDocNode.MIN_DOT_PRODUCT_TRANSITIONS;
            if (peakAreas != null && numTrans >= minProductTrans)
            {
                var tranGroupChromInfo = GetTransitionGroupChromInfo(nodeGroup, fileId, _chromIndex);
                double? dotProduct = null;
                if (tranGroupChromInfo != null)
                {
                    dotProduct = isFullScanMs
                                        ? tranGroupChromInfo.IsotopeDotProduct
                                        : tranGroupChromInfo.LibraryDotProduct;
                }
                if (dotProduct.HasValue)
                {
                    bestProduct = dotProduct.Value;

                    var statExpectedIntensities = new Statistics(expectedIntensities);
                    for (int i = 0; i < peakAreas.Length; i++)
                    {
                        var statPeakAreas = new Statistics(peakAreas[i]);
                        double dotProductCurrent = statPeakAreas.NormalizedContrastAngleSqrt(statExpectedIntensities);
                        // Only show products that are greater than the best peak product,
                        // and by enough to be a significant improvement.  Also the library product
                        // on the group node is stored as a float, which means the version
                        // hear calculated as a double can be larger, but really represent
                        // the same number.
                        if (dotProductCurrent > bestProduct &&
                            dotProductCurrent > 0.5 &&
                            dotProductCurrent - bestProduct > 0.05)
                        {
                            if (dotProducts == null)
                                dotProducts = new double[numPeaks];
                            dotProducts[i] = dotProductCurrent;
                        }
                    }
                }
            }

            // Create graph items
            int iColor = 0;
            int lineWidth = LineWidth;
            float fontSize = FontSize;
            // We want the product ion colors to stay the same whether they are displayed:
            // 1. In a single pane with the precursor ions (Transitions -> All)
            // 2. In a separate pane of the split graph (Transitions -> All AND Transitions -> Split Graph)
            // 3. In a single pane by themselves (Transition -> Products)
            // We will use an offset in the colors array for cases 2 and 3 so that we do not reuse the precursor ion colors.
            var nodeDisplayType = GetDisplayType(DocumentUI, nodeGroup);
            int colorOffset = 0;
            if(displayType == DisplayTypeChrom.products &&
                (nodeDisplayType != DisplayTypeChrom.single ||
                (nodeDisplayType == DisplayTypeChrom.single && chromatograms.OptimizationFunction == null)))
            {
                colorOffset = GetDisplayTransitions(nodeGroup, DisplayTypeChrom.precursors).Count();
            }

            for (int i = 0; i < numTrans; i++)
            {
                var info = arrayChromInfo[i];
                if (info == null && !allowEmpty)
                    continue;

                var nodeTran = displayTrans[i];
                int step = numSteps != 0 ? i - numSteps : 0;

                Color color;
                bool shade = false;
                int width = lineWidth;
                if ((numSteps == 0 && ReferenceEquals(nodeTran, nodeTranSelected) ||
                     (numSteps > 0 && step == 0)))
                {
                    color = ChromGraphItem.ColorSelected;
                    shade = true;
                    width++;
                }
                else
                {
                    color = COLORS_LIBRARY[(iColor + colorOffset) % COLORS_LIBRARY.Length];
                }

                TransitionChromInfo tranPeakInfoGraph = null;
                if (bestPeakTran == i)
                    tranPeakInfoGraph = tranPeakInfo;

                var scanName = nodeTran.FragmentIonName;
                if (nodeTran.Transition.Charge != 1)  // Positive singly charged is uninteresting
                    scanName += Transition.GetChargeIndicator(nodeTran.Transition.Charge);
                if (nodeTran.Transition.MassIndex != 0)
                    scanName += Environment.NewLine + Transition.GetMassIndexText(nodeTran.Transition.MassIndex);
                var fullScanInfo = new FullScanInfo
                {
                    ChromInfo = info,
                    ScanName = scanName
                };
                if (fullScanInfo.ChromInfo != null && fullScanInfo.ChromInfo.ExtractionWidth > 0)
                    _enableTrackingDot = true;
                var graphItem = new ChromGraphItem(nodeGroup,
                    nodeTran,
                    info,
                    tranPeakInfoGraph,
                    timeRegressionFunction,
                    GetAnnotationFlags(i, maxPeakTrans, maxPeakHeights),
                    dotProducts,
                    bestProduct,
                    isFullScanMs,
                    false,
                    step,
                    color,
                    fontSize,
                    width,
                    fullScanInfo);
                _graphHelper.AddChromatogram(graphPaneKey, graphItem);
                if (shade)
                {
                    ShadeGraph(tranPeakInfo,info,timeRegressionFunction,dotProducts,bestProduct,isFullScanMs,step,fontSize,width,fullScanInfo,graphPaneKey);
                }
                iColor++;
            }

            var graphPane = _graphHelper.GetGraphPane(graphPaneKey);
            if (graphPane == null)
                _enableTrackingDot = false;
            if (_enableTrackingDot)
            {
                graphPane.CurveList.Insert(FULLSCAN_TRACKING_INDEX, CreateScanPoint(Color.Black));
                graphPane.CurveList.Insert(FULLSCAN_SELECTED_INDEX, CreateScanPoint(Color.Red));
            }
        }
        private bool EnsureChromInfo(MeasuredResults results,
            ChromatogramSet chromatograms,
            TransitionGroupDocNode[] nodeGroups,
            IdentityPath[] groupPaths,
            ChromExtractor extractor,
            out bool changedGroups,
            out bool changedGroupIds)
        {
            if (UpdateGroups(nodeGroups, groupPaths, out changedGroups, out changedGroupIds) && _extractor == extractor)
                return true;

            _extractor = extractor;

            bool success = false;
            try
            {
                // Get chromatogram sets for all transition groups, recording unique
                // file paths in the process.
                var listArrayChromInfo = new List<ChromatogramGroupInfo[]>();
                var listFiles = new List<MsDataFileUri>();
                ChromatogramGroupInfo[] arrayAllIonsChromInfo;
                if (!results.TryLoadAllIonsChromatogram(chromatograms, extractor, true,
                                                        out arrayAllIonsChromInfo))
                {
                    return false;
                }
                else
                {
                    listArrayChromInfo.Add(arrayAllIonsChromInfo);
                    foreach (var chromInfo in arrayAllIonsChromInfo)
                    {
                        var filePath = chromInfo.FilePath;
                        if (!listFiles.Contains(filePath))
                            listFiles.Add(filePath);
                    }
                }

                _hasMergedChromInfo = false;
                _arrayChromInfo = new ChromatogramGroupInfo[listFiles.Count][];
                for (int i = 0; i < _arrayChromInfo.Length; i++)
                {
                    var arrayNew = new ChromatogramGroupInfo[listArrayChromInfo.Count];
                    for (int j = 0; j < arrayNew.Length; j++)
                    {
                        var arrayChromInfo = listArrayChromInfo[j];
                        if (arrayChromInfo == null)
                            continue;
                        foreach (var chromInfo in arrayChromInfo)
                        {
                            if (arrayNew[j] == null && Equals(listFiles[i], chromInfo.FilePath))
                                arrayNew[j] = chromInfo;
                        }
                    }
                    _arrayChromInfo[i] = arrayNew;
                }

                success = true;
            }
            finally
            {
                // Make sure the info array is set to null on failure.
                if (!success)
                    _arrayChromInfo = null;
            }

            return true;
        }
        private bool ProcessMSLevel(MsDataFileUri filePath, int msLevel, IEnumerable<ChromatogramInfo> transitionPointSets,
            ChromatogramGroupInfo chromInfo, double? apexRT, TransitionGroupDocNode nodeGroup, LibKey libKey, float tolerance)
        {
            var transitions = new List<TransitionFullScanInfo>();
            var chromSource = (msLevel == 1) ? ChromSource.ms1 : ChromSource.fragment;
            foreach (var tranPointSet in transitionPointSets.Where(t => t.Source == chromSource))
            {
                transitions.Add(new TransitionFullScanInfo
                {
                    //Name = tranPointSet.Header.,
                    Source = chromSource,
                    ScanIndexes = chromInfo.ScanIndexes,
                    PrecursorMz = chromInfo.PrecursorMz,
                    ProductMz = tranPointSet.ProductMz,
                    ExtractionWidth = tranPointSet.ExtractionWidth,
                    //Id = nodeTran.Id
                });
            }
            var chorusUrl = filePath as ChorusUrl;
            IScanProvider scanProvider;
            if (null == chorusUrl)
            {
                scanProvider = new ScanProvider(_documentFilePath,
                    filePath,
                    chromSource, chromInfo.Times, transitions.ToArray(),
                    () => _document.Settings.MeasuredResults.LoadMSDataFileScanIds(filePath));
            }
            else
            {
                scanProvider = new ChorusScanProvider(_documentFilePath,
                    chorusUrl,
                    chromSource, chromInfo.Times, transitions.ToArray());
            }

            // Across all spectra at the peak retention time, find the one with max total
            // intensity for the mz's of interest (ie the isotopic distribution) and note its drift time.
            var scanIndex = chromInfo.ScanIndexes != null
                ? MsDataFileScanHelper.FindScanIndex(chromInfo, apexRT.Value)
                : -1;
            _msDataFileScanHelper.UpdateScanProvider(scanProvider, 0, scanIndex);
            _msDataFileScanHelper.MsDataSpectra = null; // Reset
            scanIndex = _msDataFileScanHelper.GetScanIndex();
            _msDataFileScanHelper.ScanProvider.SetScanForBackgroundLoad(scanIndex);
            lock (this)
            {
                while (_msDataFileScanHelper.MsDataSpectra == null && _dataFileScanHelperException == null)
                {
                    if (_progressMonitor != null && _progressMonitor.IsCanceled)
                        return false;
                    Monitor.Wait(this, 500); // Let background loader do its thing
                }
            }
            if (_dataFileScanHelperException != null)
            {
                throw new IOException(TextUtil.LineSeparate(Resources.DriftTimeFinder_HandleLoadScanException_Problem_using_results_to_populate_drift_time_library__, _dataFileScanHelperException.Message), _dataFileScanHelperException);
            }
            if (_progressMonitor != null && !ReferenceEquals(nodeGroup, _currentDisplayedTransitionGroupDocNode))
            {
                // Do this after scan load so first group after file switch doesn't seem laggy
                _progressStatus = _progressStatus.ChangeMessage(TextUtil.LineSeparate(filePath.GetFileName(), nodeGroup.ToString())).
                                     UpdatePercentCompleteProgress(_progressMonitor, _currentStep++, _totalSteps);
                _currentDisplayedTransitionGroupDocNode = nodeGroup;
            }
            EvaluateBestDriftTime(msLevel, libKey, tolerance, transitions);
            return true;
        }
Exemple #23
0
 private static double MaxPeakTime(ChromatogramGroupInfo chromGroupInfo)
 {
     double maxIntensity = 0;
     double maxTime = 0;
     int iBest = chromGroupInfo.BestPeakIndex;
     foreach (var chromInfo in chromGroupInfo.TransitionPointSets)
     {
         var peak = chromInfo.GetPeak(iBest);
         if (!peak.IsForcedIntegration && peak.Height > maxIntensity)
         {
             maxIntensity = peak.Height;
             maxTime = peak.RetentionTime;
         }
     }
     return maxTime;
 }
Exemple #24
0
        /// <summary>
        /// Collects statistics on how much space savings minimizing will achieve, and (if outStream
        /// is not null) writes out the minimized cache file.
        /// </summary>
        public void Minimize(Settings settings, ProgressCallback progressCallback, Stream outStream,
                             FileStream outStreamScans = null, FileStream outStreamPeaks = null, FileStream outStreamScores = null)
        {
            var  writer = outStream == null ? null : new Writer(ChromatogramCache, settings.CacheFormat, outStream, outStreamScans, outStreamPeaks, outStreamScores);
            var  statisticsCollector = new MinStatisticsCollector(this);
            bool readChromatograms   = settings.NoiseTimeRange.HasValue || writer != null;

            var chromGroupHeaderToIndex = new Dictionary <long, int>(ChromGroupHeaderInfos.Count);

            for (int i = 0; i < ChromGroupHeaderInfos.Count; i++)
            {
                var cghi = ChromGroupHeaderInfos[i];
                chromGroupHeaderToIndex.Add(cghi.LocationPoints, i);
            }
            var chromGroups      = new ChromatogramGroupInfo[ChromGroupHeaderInfos.Count];
            var transitionGroups = new List <TransitionGroupDocNode> [ChromGroupHeaderInfos.Count];

            foreach (var nodePep in Document.Molecules)
            {
                foreach (var nodeGroup in nodePep.TransitionGroups)
                {
                    foreach (var chromGroupInfo in ChromatogramCache.LoadChromatogramInfos(nodePep, nodeGroup, _tolerance, null))
                    {
                        int headerIndex = chromGroupHeaderToIndex[chromGroupInfo.Header.LocationPoints];
                        if (chromGroups[headerIndex] == null)
                        {
                            chromGroups[headerIndex]      = chromGroupInfo;
                            transitionGroups[headerIndex] = new List <TransitionGroupDocNode>();
                        }
                        transitionGroups[headerIndex].Add(nodeGroup);
                    }
                }
            }

            var minimizer = new QueueWorker <MinimizeParams>(null, MinimizeAndWrite);

            minimizer.RunAsync(MINIMIZING_THREADS, @"Minimizing/Writing", MAX_GROUP_READ_AHEAD);

            for (int iHeader = 0; iHeader < ChromGroupHeaderInfos.Count; iHeader++)
            {
                var chromGroupInfo = chromGroups[iHeader];
                IList <TransitionGroupDocNode> transitionGroupDocNodes;
                if (chromGroupInfo == null)
                {
                    chromGroupInfo          = ChromatogramCache.LoadChromatogramInfo(ChromGroupHeaderInfos[iHeader]);
                    transitionGroupDocNodes = new TransitionGroupDocNode[0];
                }
                else
                {
                    transitionGroupDocNodes = transitionGroups[iHeader];
                }

                if (readChromatograms)
                {
                    try
                    {
                        chromGroupInfo.ReadChromatogram(ChromatogramCache, true);
                    }
                    catch (Exception exception)
                    {
                        Trace.TraceWarning(@"Unable to read chromatogram {0}", exception);
                    }
                }

                if (minimizer.Exception != null)
                {
                    break;
                }

                minimizer.Add(new MinimizeParams(writer, settings, chromGroupInfo, transitionGroupDocNodes, progressCallback, statisticsCollector));

                // Null out the ChromGroup in our array so it can be garbage collected.
                chromGroups[iHeader] = null;
            }

            minimizer.DoneAdding(true);
            if (minimizer.Exception != null)
            {
                throw minimizer.Exception;
            }

            statisticsCollector.ReportProgress(progressCallback, true);

            if (writer != null)
            {
                writer.WriteEndOfFile();
            }
        }
Exemple #25
0
 public ChromFileInfo GetFileInfo(ChromatogramGroupInfo chromGroupInfo)
 {
     return GetFileInfo(chromGroupInfo.FilePath);
 }
        private bool ProcessChromInfo(MsDataFileUri filePath, ChromatogramGroupInfo chromInfo, PeptidePrecursorPair pair,
            TransitionGroupDocNode nodeGroup, float tolerance, LibKey libKey)
        {
            Assume.IsTrue(chromInfo.BestPeakIndex != -1);
            var resultIndex = _document.Settings.MeasuredResults.Chromatograms.IndexOf(c => c.GetFileInfo(filePath) != null);
            if (resultIndex == -1)
                return true;
            var chromFileInfo = _document.Settings.MeasuredResults.Chromatograms[resultIndex].GetFileInfo(filePath);
            Assume.IsTrue(Equals(chromFileInfo.FilePath.GetLockMassParameters(), filePath.GetLockMassParameters()));

            // Determine apex RT for DT measurement using most intense MS1 peak
            var apexRT = GetApexRT(nodeGroup, resultIndex, chromFileInfo, true) ??
                GetApexRT(nodeGroup, resultIndex, chromFileInfo, false);

            Assume.IsTrue(chromInfo.PrecursorMz == pair.NodeGroup.PrecursorMz);
            // Only use the transitions currently enabled
            var transitionPointSets = chromInfo.TransitionPointSets.Where(
                tp => nodeGroup.Transitions.Any(
                    t => (t.Mz - (tp.ExtractionWidth ?? tolerance)/2) <= tp.ProductMz &&
                         (t.Mz + (tp.ExtractionWidth ?? tolerance)/2) >= tp.ProductMz))
                .ToArray();

            for (var msLevel = 1; msLevel <= 2; msLevel++)
            {
                if (!ProcessMSLevel(filePath, msLevel, transitionPointSets, chromInfo, apexRT, nodeGroup, libKey, tolerance))
                    return false; // User cancelled
            }
            return true;
        }
Exemple #27
0
        /// <summary>
        /// Decides what information from a ChromatogramGroupInfo should be thrown away.
        /// ChromatogramGroupInfo's can have the the retention time range shortened, and certain
        /// transitions discarded.
        /// </summary>
        private MinimizedChromGroup MinimizeChromGroup(Settings settings, ChromatogramGroupInfo chromatogramGroupInfo, IList <TransitionGroupDocNode> transitionGroups)
        {
            chromatogramGroupInfo.EnsureDecompressed();

            var fileIndexes = new List <int>();

            for (int fileIndex = 0; fileIndex < Document.Settings.MeasuredResults.Chromatograms.Count; fileIndex++)
            {
                var chromatogramSet = Document.Settings.MeasuredResults.Chromatograms[fileIndex];
                if (chromatogramSet.MSDataFilePaths.Any(path => Equals(path, chromatogramGroupInfo.FilePath)))
                {
                    fileIndexes.Add(fileIndex);
                }
            }
            var chromatograms = Enumerable.Range(0, chromatogramGroupInfo.NumTransitions)
                                .Select(chromatogramGroupInfo.GetRawTransitionInfo).ToArray();

            Assume.IsTrue(Equals(chromatogramGroupInfo.NumTransitions, chromatograms.Length));
            var    keptTransitionIndexes = new List <int>();
            double minRetentionTime      = Double.MaxValue;
            double maxRetentionTime      = -Double.MaxValue;

            for (int i = 0; i < chromatograms.Length; i++)
            {
                var chromatogram        = chromatograms[i];
                var matchingTransitions = new List <TransitionDocNode>();
                foreach (var transitionDocNode in transitionGroups.SelectMany(tg => tg.Transitions))
                {
                    if (0 != chromatogram.ProductMz.CompareTolerant(transitionDocNode.Mz, _tolerance))
                    {
                        continue;
                    }
                    matchingTransitions.Add(transitionDocNode);
                    foreach (var fileIndex in fileIndexes)
                    {
                        var chromatogramSet = transitionDocNode.Results[fileIndex];
                        if (chromatogramSet.IsEmpty)
                        {
                            continue;
                        }
                        foreach (var transitionChromInfo in chromatogramSet)
                        {
                            if (transitionChromInfo.IsEmpty)
                            {
                                continue;
                            }
                            minRetentionTime = Math.Min(minRetentionTime, transitionChromInfo.StartRetentionTime);
                            maxRetentionTime = Math.Max(maxRetentionTime, transitionChromInfo.EndRetentionTime);
                        }
                    }
                }
                bool kept = !settings.DiscardUnmatchedChromatograms ||
                            matchingTransitions.Count > 0 ||
                            (chromatogram.PrecursorMz.Equals(SignedMz.ZERO) && !settings.DiscardAllIonsChromatograms);
                if (kept)
                {
                    keptTransitionIndexes.Add(i);
                }
            }
            var result = new MinimizedChromGroup(chromatogramGroupInfo)
            {
                RetainedTransitionIndexes = keptTransitionIndexes,
            };

            if (settings.NoiseTimeRange.HasValue && minRetentionTime < maxRetentionTime)
            {
                result.SetStartEndTime((float)(minRetentionTime - settings.NoiseTimeRange), (float)(maxRetentionTime + settings.NoiseTimeRange));
            }
            return(result);
        }
 public override void Dispose()
 {
     if (_cache != null)
         _cache.ReadStream.CloseStream();
     _cache = null;
     _chromKeyIndices = null;
     _lastChromGroupInfo = null;
 }
Exemple #29
0
            public IonAbundances(TransitionGroupDocNode nodeTranGroup, ChromatogramGroupInfo chromGroupInfo,
                float mzMatchTolerance, int peakIndex)
                : this()
            {
                foreach (var nodeTran in nodeTranGroup.Transitions)
                {
                    var chromInfoCached = chromGroupInfo.GetTransitionInfo((float) nodeTran.Mz, mzMatchTolerance);
                    if (chromInfoCached == null)
                        continue;

                    Add(nodeTran, chromInfoCached.GetPeak(peakIndex).Area);
                }
            }
        public override bool GetChromatogram(
            int id, string modifiedSequence, Color peptideColor,
            out ChromExtra extra, out float[] times, out int[] scanIndexes, out float[] intensities, out float[] massErrors)
        {
            var chromKeyIndices = _chromKeyIndices[id];
            if (_lastChromGroupInfo == null || _lastIndices.GroupIndex != chromKeyIndices.GroupIndex)
            {
                _lastChromGroupInfo = _cache.LoadChromatogramInfo(chromKeyIndices.GroupIndex);
                _lastChromGroupInfo.ReadChromatogram(_cache);
            }
            _lastIndices = chromKeyIndices;
            var tranInfo = _lastChromGroupInfo.GetTransitionInfo(chromKeyIndices.TranIndex);
            times = tranInfo.Times;
            intensities = tranInfo.Intensities;
            massErrors = null;
            if (tranInfo.MassError10Xs != null)
                massErrors = tranInfo.MassError10Xs.Select(m => m/10.0f).ToArray();
            scanIndexes = null;
            if (tranInfo.ScanIndexes != null)
                scanIndexes = tranInfo.ScanIndexes[(int) chromKeyIndices.Key.Source];

            SetPercentComplete(100 * id / _chromKeyIndices.Length);

            extra = new ChromExtra(chromKeyIndices.StatusId, chromKeyIndices.StatusRank);

            // Display in AllChromatogramsGraph
            if (chromKeyIndices.Key.Precursor != 0)
            {
                LoadingStatus.Transitions.AddTransition(
                    modifiedSequence,
                    peptideColor,
                    chromKeyIndices.StatusId,
                    chromKeyIndices.StatusRank,
                    times,
                    intensities);
            }
            return true;
        }
Exemple #31
0
        private bool ProcessMSLevel(MsDataFileUri filePath, int msLevel, IEnumerable <ChromatogramInfo> transitionPointSets,
                                    ChromatogramGroupInfo chromInfo, double?apexRT, TransitionGroupDocNode nodeGroup, LibKey libKey, float tolerance)
        {
            var           transitions = new List <TransitionFullScanInfo>();
            var           chromSource = (msLevel == 1) ? ChromSource.ms1 : ChromSource.fragment;
            IList <float> times       = null;

            foreach (var tranPointSet in transitionPointSets.Where(t => t.Source == chromSource))
            {
                transitions.Add(new TransitionFullScanInfo
                {
                    //Name = tranPointSet.Header.,
                    Source          = chromSource,
                    TimeIntensities = tranPointSet.TimeIntensities,
                    PrecursorMz     = chromInfo.PrecursorMz,
                    ProductMz       = tranPointSet.ProductMz,
                    ExtractionWidth = tranPointSet.ExtractionWidth,
                    //Id = nodeTran.Id
                });
                times = tranPointSet.Times;
            }

            if (!transitions.Any())
            {
                return(true); // Nothing to do at this ms level
            }

            IScanProvider scanProvider = new ScanProvider(_documentFilePath,
                                                          filePath,
                                                          chromSource, times, transitions.ToArray(),
                                                          _document.Settings.MeasuredResults,
                                                          () => _document.Settings.MeasuredResults.LoadMSDataFileScanIds(filePath));

            // Across all spectra at the peak retention time, find the one with max total
            // intensity for the mz's of interest (ie the isotopic distribution) and note its ion mobility.
            var scanIndex = MsDataFileScanHelper.FindScanIndex(times, apexRT.Value);

            _msDataFileScanHelper.UpdateScanProvider(scanProvider, 0, scanIndex);
            _msDataFileScanHelper.MsDataSpectra = null; // Reset
            scanIndex = _msDataFileScanHelper.GetScanIndex();
            _msDataFileScanHelper.ScanProvider.SetScanForBackgroundLoad(scanIndex);
            lock (this)
            {
                while (_msDataFileScanHelper.MsDataSpectra == null && _dataFileScanHelperException == null)
                {
                    if (_progressMonitor != null && _progressMonitor.IsCanceled)
                    {
                        return(false);
                    }
                    Monitor.Wait(this, 500); // Let background loader do its thing
                }
            }
            if (_dataFileScanHelperException != null)
            {
                throw new IOException(TextUtil.LineSeparate(Resources.DriftTimeFinder_HandleLoadScanException_Problem_using_results_to_populate_drift_time_library__, _dataFileScanHelperException.Message), _dataFileScanHelperException);
            }
            if (_progressMonitor != null && !ReferenceEquals(nodeGroup, _currentDisplayedTransitionGroupDocNode))
            {
                // Do this after scan load so first group after file switch doesn't seem laggy
                _progressStatus = _progressStatus.ChangeMessage(TextUtil.LineSeparate(filePath.GetFileName(), nodeGroup.ToString())).
                                  UpdatePercentCompleteProgress(_progressMonitor, _currentStep++, _totalSteps);
                _currentDisplayedTransitionGroupDocNode = nodeGroup;
            }
            EvaluateBestIonMobilityValue(msLevel, libKey, tolerance, transitions);
            return(true);
        }
        /// <summary>
        /// Decides what information from a ChromatogramGroupInfo should be thrown away.
        /// ChromatogramGroupInfo's can have the the retention time range shortened, and certain
        /// transitions discarded.
        /// </summary>
        private MinimizedChromGroup MinimizeChromGroup(Settings settings, ChromatogramGroupInfo chromatogramGroupInfo, IList <TransitionGroupDocNode> transitionGroups)
        {
            var fileIndexes = new List <int>();

            for (int fileIndex = 0; fileIndex < Document.Settings.MeasuredResults.Chromatograms.Count; fileIndex++)
            {
                var chromatogramSet = Document.Settings.MeasuredResults.Chromatograms[fileIndex];
                if (chromatogramSet.MSDataFilePaths.Any(path => Equals(path, chromatogramGroupInfo.FilePath)))
                {
                    fileIndexes.Add(fileIndex);
                }
            }
            var chromatograms = chromatogramGroupInfo.TransitionPointSets.ToArray();

            Assume.IsTrue(Equals(chromatogramGroupInfo.NumTransitions, chromatograms.Length));
            var    keptTransitionIndexes = new List <int>();
            double minRetentionTime      = Double.MaxValue;
            double maxRetentionTime      = -Double.MaxValue;

            for (int i = 0; i < chromatograms.Length; i++)
            {
                var chromatogram        = chromatograms[i];
                var matchingTransitions = new List <TransitionDocNode>();
                foreach (var transitionDocNode in transitionGroups.SelectMany(tg => tg.Transitions))
                {
                    if (0 != ChromKey.CompareTolerant((float)chromatogram.ProductMz, (float)transitionDocNode.Mz, _tolerance))
                    {
                        continue;
                    }
                    matchingTransitions.Add(transitionDocNode);
                    foreach (var fileIndex in fileIndexes)
                    {
                        var chromatogramSet = transitionDocNode.Results[fileIndex];
                        if (chromatogramSet == null)
                        {
                            continue;
                        }
                        foreach (var transitionChromInfo in chromatogramSet)
                        {
                            if (transitionChromInfo.IsEmpty)
                            {
                                continue;
                            }
                            minRetentionTime = Math.Min(minRetentionTime, transitionChromInfo.StartRetentionTime);
                            maxRetentionTime = Math.Max(maxRetentionTime, transitionChromInfo.EndRetentionTime);
                        }
                    }
                }
                bool kept = !settings.DiscardUnmatchedChromatograms ||
                            matchingTransitions.Count > 0;
                if (kept)
                {
                    keptTransitionIndexes.Add(i);
                }
            }
            var result = new MinimizedChromGroup(chromatogramGroupInfo.Header)
            {
                RetainedTransitionIndexes = keptTransitionIndexes,
            };

            if (settings.NoiseTimeRange.HasValue && minRetentionTime < maxRetentionTime)
            {
                if (null == chromatogramGroupInfo.Times)
                {
                    // Chromatogram was unreadable.
                    result.RetainedTransitionIndexes.Clear();
                }
                else
                {
                    result.SetStartEndTime(chromatogramGroupInfo.Times, (float)(minRetentionTime - settings.NoiseTimeRange), (float)(maxRetentionTime + settings.NoiseTimeRange));
                }
            }
            return(result);
        }
            public void WriteChromGroup(ChromatogramGroupInfo originalChromGroup, MinimizedChromGroup minimizedChromGroup)
            {
                if (minimizedChromGroup.RetainedTransitionIndexes.Count == 0)
                {
                    return;
                }

                var originalHeader = originalChromGroup.Header;
                int fileIndex = originalHeader.FileIndex;
                int startTransitionIndex = _transitions.Count;
                int startPeakIndex = _peakCount;
                int startScoreIndex = _scoreCount;
                for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                {
                    int iScores = originalHeader.StartScoreIndex + iPeak*_scoreTypes.Count;
                    var scores = _originalCache.GetCachedScores(iScores).ToArray();
                    PrimitiveArrays.Write(_outputStreamScores, scores);
                    _scoreCount += scores.Length;
                }
                int numPoints = minimizedChromGroup.OptimizedLastScan - minimizedChromGroup.OptimizedFirstScan + 1;
                var retainedPeakIndexes = new HashSet<int>();
                if (minimizedChromGroup.OptimizedStartTime.HasValue && minimizedChromGroup.OptimizedEndTime.HasValue)
                {
                    for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                    {
                        bool outsideRange = false;
                        for (var transitionIndex = 0; transitionIndex < originalHeader.NumTransitions; transitionIndex++)
                        {
                            if (!minimizedChromGroup.RetainedTransitionIndexes.Contains(transitionIndex))
                                continue;

                            var peak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                       transitionIndex*originalHeader.NumPeaks + iPeak);
                            if (peak.StartTime < minimizedChromGroup.OptimizedStartTime.Value ||
                                peak.EndTime > minimizedChromGroup.OptimizedEndTime.Value)
                            {
                                outsideRange = true;
                                break;
                            }
                        }
                        if (!outsideRange)
                        {
                            retainedPeakIndexes.Add(iPeak);
                        }
                    }
                }
                else
                {
                    retainedPeakIndexes.UnionWith(Enumerable.Range(0, originalHeader.NumPeaks));
                }
                int numPeaks = retainedPeakIndexes.Count;
                int maxPeakIndex;
                if (retainedPeakIndexes.Contains(originalHeader.MaxPeakIndex))
                {
                    maxPeakIndex = retainedPeakIndexes.Count(index => index < originalHeader.MaxPeakIndex);
                }
                else
                {
                    maxPeakIndex = -1;
                }
                long location = _outputStream.Position;

                float[] times = originalChromGroup.Times.Skip(minimizedChromGroup.OptimizedFirstScan).Take(numPoints).ToArray();
                List<float[]> intensities = new List<float[]>();
                List<short[]> massError10Xs = originalChromGroup.MassError10XArray != null
                                                  ? new List<short[]>()
                                                  : null;
                int[][] scanIndexes = null;
                if (originalChromGroup.ScanIndexes != null)
                {
                    scanIndexes = new int[originalChromGroup.ScanIndexes.Length][];
                    for (int i = 0; i < scanIndexes.Length; i++)
                    {
                        if (originalChromGroup.ScanIndexes[i] != null)
                        {
                            scanIndexes[i] =
                                originalChromGroup.ScanIndexes[i].Skip(minimizedChromGroup.OptimizedFirstScan)
                                    .Take(numPoints)
                                    .ToArray();
                        }
                    }
                }

                foreach (var originalIndex in minimizedChromGroup.RetainedTransitionIndexes)
                {
                    _transitions.Add(_originalCache.GetTransition(originalIndex + originalHeader.StartTransitionIndex));
                    for (int originalPeakIndex = 0; originalPeakIndex < originalHeader.NumPeaks; originalPeakIndex++)
                    {
                        if (!retainedPeakIndexes.Contains(originalPeakIndex))
                            continue;

                        var originalPeak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                                  originalIndex*originalHeader.NumPeaks +
                                                                  originalPeakIndex);
                        _peakCount++;
                        ChromPeak.WriteArray(_outputStreamPeaks.SafeFileHandle, new[] {originalPeak});
                    }
                    intensities.Add(originalChromGroup.IntensityArray[originalIndex]
                        .Skip(minimizedChromGroup.OptimizedFirstScan)
                        .Take(numPoints).ToArray());
                    if (massError10Xs != null)
                    {
                        massError10Xs.Add(originalChromGroup.MassError10XArray[originalIndex]
                            .Skip(minimizedChromGroup.OptimizedFirstScan)
                            .Take(numPoints).ToArray());
                    }
                }
                var massError10XArray = massError10Xs != null ? massError10Xs.ToArray() : null;
                byte[] points = ChromatogramCache.TimeIntensitiesToBytes(times, intensities.ToArray(), massError10XArray, scanIndexes);
                // Compress the data (can be huge for AB data with lots of zeros)
                byte[] pointsCompressed = points.Compress(3);
                int lenCompressed = pointsCompressed.Length;
                _outputStream.Write(pointsCompressed, 0, lenCompressed);
                var header = new ChromGroupHeaderInfo5(originalHeader.Precursor,
                                                      originalHeader.TextIdIndex,
                                                      originalHeader.TextIdLen,
                                                      fileIndex,
                                                      _transitions.Count - startTransitionIndex,
                                                      startTransitionIndex,
                                                      numPeaks,
                                                      startPeakIndex,
                                                      startScoreIndex,
                                                      maxPeakIndex,
                                                      numPoints,
                                                      pointsCompressed.Length,
                                                      location,
                                                      originalHeader.Flags,
                                                      originalHeader.StatusId,
                                                      originalHeader.StatusRank);
                _chromGroupHeaderInfos.Add(header);
            }
            public void WriteChromGroup(ChromatogramGroupInfo originalChromGroup, MinimizedChromGroup minimizedChromGroup)
            {
                if (minimizedChromGroup.RetainedTransitionIndexes.Count == 0)
                {
                    return;
                }

                var originalHeader       = originalChromGroup.Header;
                int fileIndex            = originalHeader.FileIndex;
                int startTransitionIndex = _transitions.Count;
                int startPeakIndex       = _peakCount;
                int startScoreIndex      = _scoreCount;

                for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                {
                    int iScores = originalHeader.StartScoreIndex + iPeak * _scoreTypes.Count;
                    var scores  = _originalCache.GetCachedScores(iScores).ToArray();
                    PrimitiveArrays.Write(_outputStreamScores, scores);
                    _scoreCount += scores.Length;
                }
                int numPoints           = minimizedChromGroup.OptimizedLastScan - minimizedChromGroup.OptimizedFirstScan + 1;
                var retainedPeakIndexes = new HashSet <int>();

                if (minimizedChromGroup.OptimizedStartTime.HasValue && minimizedChromGroup.OptimizedEndTime.HasValue)
                {
                    for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                    {
                        bool outsideRange = false;
                        for (var transitionIndex = 0; transitionIndex < originalHeader.NumTransitions; transitionIndex++)
                        {
                            if (!minimizedChromGroup.RetainedTransitionIndexes.Contains(transitionIndex))
                            {
                                continue;
                            }

                            var peak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                              transitionIndex * originalHeader.NumPeaks + iPeak);
                            if (peak.StartTime < minimizedChromGroup.OptimizedStartTime.Value ||
                                peak.EndTime > minimizedChromGroup.OptimizedEndTime.Value)
                            {
                                outsideRange = true;
                                break;
                            }
                        }
                        if (!outsideRange)
                        {
                            retainedPeakIndexes.Add(iPeak);
                        }
                    }
                }
                else
                {
                    retainedPeakIndexes.UnionWith(Enumerable.Range(0, originalHeader.NumPeaks));
                }
                int numPeaks = retainedPeakIndexes.Count;
                int maxPeakIndex;

                if (retainedPeakIndexes.Contains(originalHeader.MaxPeakIndex))
                {
                    maxPeakIndex = retainedPeakIndexes.Count(index => index < originalHeader.MaxPeakIndex);
                }
                else
                {
                    maxPeakIndex = -1;
                }
                long location = _outputStream.Position;

                float[]        times         = originalChromGroup.Times.Skip(minimizedChromGroup.OptimizedFirstScan).Take(numPoints).ToArray();
                List <float[]> intensities   = new List <float[]>();
                List <short[]> massError10Xs = originalChromGroup.MassError10XArray != null
                                                  ? new List <short[]>()
                                                  : null;

                int[][] scanIndexes = null;
                if (originalChromGroup.ScanIndexes != null)
                {
                    scanIndexes = new int[originalChromGroup.ScanIndexes.Length][];
                    for (int i = 0; i < scanIndexes.Length; i++)
                    {
                        if (originalChromGroup.ScanIndexes[i] != null)
                        {
                            scanIndexes[i] =
                                originalChromGroup.ScanIndexes[i].Skip(minimizedChromGroup.OptimizedFirstScan)
                                .Take(numPoints)
                                .ToArray();
                        }
                    }
                }

                foreach (var originalIndex in minimizedChromGroup.RetainedTransitionIndexes)
                {
                    _transitions.Add(_originalCache.GetTransition(originalIndex + originalHeader.StartTransitionIndex));
                    for (int originalPeakIndex = 0; originalPeakIndex < originalHeader.NumPeaks; originalPeakIndex++)
                    {
                        if (!retainedPeakIndexes.Contains(originalPeakIndex))
                        {
                            continue;
                        }

                        var originalPeak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                                  originalIndex * originalHeader.NumPeaks +
                                                                  originalPeakIndex);
                        _peakCount++;
                        ChromPeak.WriteArray(_outputStreamPeaks.SafeFileHandle, new[] { originalPeak });
                    }
                    intensities.Add(originalChromGroup.IntensityArray[originalIndex]
                                    .Skip(minimizedChromGroup.OptimizedFirstScan)
                                    .Take(numPoints).ToArray());
                    if (massError10Xs != null)
                    {
                        massError10Xs.Add(originalChromGroup.MassError10XArray[originalIndex]
                                          .Skip(minimizedChromGroup.OptimizedFirstScan)
                                          .Take(numPoints).ToArray());
                    }
                }
                var massError10XArray = massError10Xs != null?massError10Xs.ToArray() : null;

                byte[] points = ChromatogramCache.TimeIntensitiesToBytes(times, intensities.ToArray(), massError10XArray, scanIndexes);
                // Compress the data (can be huge for AB data with lots of zeros)
                byte[] pointsCompressed = points.Compress(3);
                int    lenCompressed    = pointsCompressed.Length;

                _outputStream.Write(pointsCompressed, 0, lenCompressed);
                var header = new ChromGroupHeaderInfo5(originalHeader.Precursor,
                                                       originalHeader.TextIdIndex,
                                                       originalHeader.TextIdLen,
                                                       fileIndex,
                                                       _transitions.Count - startTransitionIndex,
                                                       startTransitionIndex,
                                                       numPeaks,
                                                       startPeakIndex,
                                                       startScoreIndex,
                                                       maxPeakIndex,
                                                       numPoints,
                                                       pointsCompressed.Length,
                                                       location,
                                                       originalHeader.Flags,
                                                       originalHeader.StatusId,
                                                       originalHeader.StatusRank);

                _chromGroupHeaderInfos.Add(header);
            }
Exemple #35
0
 public ChromFileInfo GetFileInfo(ChromatogramGroupInfo chromGroupInfo)
 {
     return(GetFileInfo(chromGroupInfo.FilePath));
 }
        /// <summary>
        /// Collects statistics on how much space savings minimizing will achieve, and (if outStream
        /// is not null) writes out the minimized cache file.
        /// </summary>
        public void Minimize(Settings settings, ProgressCallback progressCallback, Stream outStream,
                             FileStream outStreamScans = null, FileStream outStreamPeaks = null, FileStream outStreamScores = null)
        {
            var  writer = outStream == null ? null : new Writer(ChromatogramCache, outStream, outStreamScans, outStreamPeaks, outStreamScores);
            var  statisticsCollector = new MinStatisticsCollector(this);
            bool readChromatograms   = settings.NoiseTimeRange.HasValue || writer != null;

            var chromGroupHeaderToIndex =
                ChromGroupHeaderInfos
                .Select((cghi, index) => new KeyValuePair <ChromGroupHeaderInfo5, int>(cghi, index))
                .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            var chromGroups      = new ChromatogramGroupInfo[ChromGroupHeaderInfos.Count];
            var transitionGroups = new List <TransitionGroupDocNode> [ChromGroupHeaderInfos.Count];

            foreach (var nodePep in Document.Molecules)
            {
                foreach (var nodeGroup in nodePep.TransitionGroups)
                {
                    ChromatogramGroupInfo[] groupInfos;
                    ChromatogramCache.TryLoadChromatogramInfo(nodePep, nodeGroup, _tolerance, out groupInfos);
                    foreach (var chromGroupInfo in groupInfos)
                    {
                        int headerIndex = chromGroupHeaderToIndex[chromGroupInfo.Header];
                        if (chromGroups[headerIndex] == null)
                        {
                            chromGroups[headerIndex]      = chromGroupInfo;
                            transitionGroups[headerIndex] = new List <TransitionGroupDocNode>();
                        }
                        transitionGroups[headerIndex].Add(nodeGroup);
                    }
                }
            }

            for (int iHeader = 0; iHeader < ChromGroupHeaderInfos.Count; iHeader++)
            {
                var chromGroupInfo = chromGroups[iHeader];
                IList <TransitionGroupDocNode> transitionGroupDocNodes;
                if (chromGroupInfo == null)
                {
                    chromGroupInfo          = ChromatogramCache.LoadChromatogramInfo(ChromGroupHeaderInfos[iHeader]);
                    transitionGroupDocNodes = new TransitionGroupDocNode[0];
                }
                else
                {
                    transitionGroupDocNodes = transitionGroups[iHeader];
                }
                if (readChromatograms)
                {
                    try
                    {
                        chromGroupInfo.ReadChromatogram(ChromatogramCache);
                    }
                    catch (Exception exception)
                    {
                        Trace.TraceWarning("Unable to read chromatogram {0}", exception); // Not L10N
                    }
                }
                MinimizedChromGroup minimizedChromGroup = MinimizeChromGroup(settings,
                                                                             chromGroupInfo, transitionGroupDocNodes);
                statisticsCollector.ProcessChromGroup(minimizedChromGroup);
                if (progressCallback != null)
                {
                    progressCallback.Invoke(statisticsCollector.GetStatistics());
                }
                if (writer != null)
                {
                    writer.WriteChromGroup(chromGroupInfo, minimizedChromGroup);
                }
                // Null out the ChromGroup in our array so it can be garbage collected.
                chromGroups[iHeader] = null;
            }
            if (progressCallback != null)
            {
                progressCallback.Invoke(statisticsCollector.GetStatistics());
            }
            if (writer != null)
            {
                writer.WriteEndOfFile();
            }
        }
        /// <summary>
        /// If multiple replicate files contain mutually exclusive chromatogram groups, create
        /// a merged chromatogram group.  If there are any collisions, return null.
        /// </summary>
        private ChromatogramGroupInfo[] GetMergedChromInfo()
        {
            if (!_showPeptideTotals || _arrayChromInfo.Length < 2)
                return null;

            var mergedChromGroupInfo = new ChromatogramGroupInfo[_arrayChromInfo[0].Length];
            for (int i = 0; i < _arrayChromInfo.Length; i++)
            {
                for (int j = 0; j < mergedChromGroupInfo.Length; j++)
                {
                    if (_arrayChromInfo[i][j] != null)
                    {
                        if (mergedChromGroupInfo[j] != null)
                            return null;
                        mergedChromGroupInfo[j] = _arrayChromInfo[i][j];
                    }
                }
            }

            return mergedChromGroupInfo;
        }
Exemple #38
0
 public MinimizedChromGroup(ChromatogramGroupInfo chromGroupHeaderInfo)
 {
     _chromatogramGroupInfo = chromGroupHeaderInfo;
 }