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 #2
0
            public void CalcPeakInfo(ChromatogramCache cache)
            {
                var header              = _chromatogramGroupInfo.Header;
                int numPeaks            = header.NumPeaks;
                var retainedPeakIndexes = new HashSet <int>();

                if (!OptimizedStartTime.HasValue || !OptimizedEndTime.HasValue)
                {
                    retainedPeakIndexes.UnionWith(Enumerable.Range(0, numPeaks));
                }
                else
                {
                    for (int iPeak = 0; iPeak < header.NumPeaks; iPeak++)
                    {
                        bool outsideRange = false;
                        for (var transitionIndex = 0; transitionIndex < header.NumTransitions; transitionIndex++)
                        {
                            if (!RetainedTransitionIndexes.Contains(transitionIndex))
                            {
                                continue;
                            }

                            var peak = cache.GetPeak(header.StartPeakIndex +
                                                     transitionIndex * header.NumPeaks +
                                                     iPeak);
                            if (peak.StartTime < OptimizedStartTime.Value || peak.EndTime > OptimizedEndTime.Value)
                            {
                                outsideRange = true;
                                break;
                            }
                        }
                        if (!outsideRange)
                        {
                            retainedPeakIndexes.Add(iPeak);
                        }
                    }
                }

                NumPeaks = retainedPeakIndexes.Count;
                if (retainedPeakIndexes.Contains(header.MaxPeakIndex))
                {
                    MaxPeakIndex = retainedPeakIndexes.Count(index => index < header.MaxPeakIndex);
                }
                else
                {
                    MaxPeakIndex = -1;
                }

                var peakScores = new List <float>();

                for (int iPeak = 0; iPeak < numPeaks; iPeak++)
                {
                    if (!retainedPeakIndexes.Contains(iPeak))
                    {
                        continue;
                    }

                    int iScores = header.StartScoreIndex + iPeak * cache.ScoreTypesCount;
                    peakScores.AddRange(cache.GetCachedScores(iScores));
                }
                PeakScores = peakScores.ToArray();
                var peaks = new List <ChromPeak>();

                Transitions = new List <ChromTransition>();
                foreach (var originalIndex in RetainedTransitionIndexes)
                {
                    Transitions.Add(cache.GetTransition(originalIndex + header.StartTransitionIndex));
                    for (int iPeak = 0; iPeak < numPeaks; iPeak++)
                    {
                        if (!retainedPeakIndexes.Contains(iPeak))
                        {
                            continue;
                        }

                        var originalPeak = cache.GetPeak(header.StartPeakIndex + originalIndex * numPeaks + iPeak);
                        peaks.Add(originalPeak);
                        TotalPeakCount++;
                    }
                }
                MinimizedPeaks = peaks.ToArray();
            }